Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions harfrust_capi/include/hr-hb.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
#define hb_direction_from_string hr_direction_from_string
#define hb_direction_t hr_direction_t
#define hb_direction_to_string hr_direction_to_string
#define hb_face_count hr_face_count
#define hb_face_create hr_face_create
#define hb_face_create_for_tables hr_face_create_for_tables
#define hb_face_create_or_fail hr_face_create_or_fail
Expand Down Expand Up @@ -141,7 +142,11 @@
#define hb_font_funcs_reference hr_font_funcs_reference
#define hb_font_funcs_set_glyph_extents_func hr_font_funcs_set_glyph_extents_func
#define hb_font_funcs_set_glyph_h_advance_func hr_font_funcs_set_glyph_h_advance_func
#define hb_font_funcs_set_glyph_h_advances_func hr_font_funcs_set_glyph_h_advances_func
#define hb_font_funcs_set_glyph_h_kerning_func hr_font_funcs_set_glyph_h_kerning_func
#define hb_font_funcs_set_glyph_h_origin_func hr_font_funcs_set_glyph_h_origin_func
#define hb_font_funcs_set_glyph_v_advance_func hr_font_funcs_set_glyph_v_advance_func
#define hb_font_funcs_set_glyph_v_kerning_func hr_font_funcs_set_glyph_v_kerning_func
#define hb_font_funcs_set_glyph_v_origin_func hr_font_funcs_set_glyph_v_origin_func
#define hb_font_funcs_set_nominal_glyph_func hr_font_funcs_set_nominal_glyph_func
#define hb_font_funcs_set_user_data hr_font_funcs_set_user_data
Expand All @@ -153,18 +158,26 @@
#define hb_font_get_glyph_advance_for_direction hr_font_get_glyph_advance_for_direction
#define hb_font_get_glyph_advance_func_t hr_font_get_glyph_advance_func_t
#define hb_font_get_glyph_advances_for_direction hr_font_get_glyph_advances_for_direction
#define hb_font_get_glyph_advances_func_t hr_font_get_glyph_advances_func_t
#define hb_font_get_glyph_extents hr_font_get_glyph_extents
#define hb_font_get_glyph_extents_for_origin hr_font_get_glyph_extents_for_origin
#define hb_font_get_glyph_extents_func_t hr_font_get_glyph_extents_func_t
#define hb_font_get_glyph_from_name hr_font_get_glyph_from_name
#define hb_font_get_glyph_h_advance hr_font_get_glyph_h_advance
#define hb_font_get_glyph_h_advances hr_font_get_glyph_h_advances
#define hb_font_get_glyph_h_advances_func_t hr_font_get_glyph_h_advances_func_t
#define hb_font_get_glyph_h_kerning hr_font_get_glyph_h_kerning
#define hb_font_get_glyph_h_kerning_func_t hr_font_get_glyph_h_kerning_func_t
#define hb_font_get_glyph_h_origin hr_font_get_glyph_h_origin
#define hb_font_get_glyph_h_origin_func_t hr_font_get_glyph_h_origin_func_t
#define hb_font_get_glyph_kerning_func_t hr_font_get_glyph_kerning_func_t
#define hb_font_get_glyph_name hr_font_get_glyph_name
#define hb_font_get_glyph_origin_for_direction hr_font_get_glyph_origin_for_direction
#define hb_font_get_glyph_origin_func_t hr_font_get_glyph_origin_func_t
#define hb_font_get_glyph_v_advance hr_font_get_glyph_v_advance
#define hb_font_get_glyph_v_advances hr_font_get_glyph_v_advances
#define hb_font_get_glyph_v_kerning hr_font_get_glyph_v_kerning
#define hb_font_get_glyph_v_kerning_func_t hr_font_get_glyph_v_kerning_func_t
#define hb_font_get_glyph_v_origin hr_font_get_glyph_v_origin
#define hb_font_get_nominal_glyph hr_font_get_nominal_glyph
#define hb_font_get_nominal_glyph_func_t hr_font_get_nominal_glyph_func_t
Expand Down
112 changes: 112 additions & 0 deletions harfrust_capi/include/hr.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,20 @@ typedef hr_position_t (*hr_font_get_glyph_advance_func_t)(struct hr_font_t *font
hr_codepoint_t glyph,
void *user_data);

/**
* Fills a strided run of horizontal glyph advances.
*/
typedef void (*hr_font_get_glyph_advances_func_t)(struct hr_font_t *font,
void *font_data,
unsigned int count,
const hr_codepoint_t *first_glyph,
unsigned int glyph_stride,
hr_position_t *first_advance,
unsigned int advance_stride,
void *user_data);

typedef hr_font_get_glyph_advances_func_t hr_font_get_glyph_h_advances_func_t;

/**
* Returns a glyph's origin along the current direction.
*/
Expand All @@ -404,6 +418,21 @@ typedef hr_bool_t (*hr_font_get_glyph_origin_func_t)(struct hr_font_t *font,
hr_position_t *y,
void *user_data);

typedef hr_font_get_glyph_origin_func_t hr_font_get_glyph_h_origin_func_t;

/**
* Returns the spacing adjustment for a pair of glyphs.
*/
typedef hr_position_t (*hr_font_get_glyph_kerning_func_t)(struct hr_font_t *font,
void *font_data,
hr_codepoint_t first_glyph,
hr_codepoint_t second_glyph,
void *user_data);

typedef hr_font_get_glyph_kerning_func_t hr_font_get_glyph_h_kerning_func_t;

typedef hr_font_get_glyph_kerning_func_t hr_font_get_glyph_v_kerning_func_t;

/**
* Returns a glyph's ink extents.
*/
Expand Down Expand Up @@ -2559,6 +2588,15 @@ hr_bool_t hr_variation_from_string(const char *str_, int len, struct hr_variatio
*/
void hr_variation_to_string(const struct hr_variation_t *variation, char *buf, unsigned int size);

/**
* Returns the number of faces in a font blob, or zero if it is invalid.
*
* # Safety
*
* `blob` must be `NULL` or a live blob.
*/
unsigned int hr_face_count(struct hr_blob_t *blob);

/**
* Creates a face over the font at `index` within `blob`.
*
Expand Down Expand Up @@ -3111,6 +3149,28 @@ hr_bool_t hr_font_get_glyph_h_origin(struct hr_font_t *font,
hr_position_t *x,
hr_position_t *y);

/**
* Returns the horizontal kerning supplied by font callbacks, or zero.
*
* # Safety
*
* `font` must be `NULL` or a live font.
*/
hr_position_t hr_font_get_glyph_h_kerning(struct hr_font_t *font,
hr_codepoint_t first_glyph,
hr_codepoint_t second_glyph);

/**
* Returns the vertical kerning supplied by font callbacks, or zero.
*
* # Safety
*
* `font` must be `NULL` or a live font.
*/
hr_position_t hr_font_get_glyph_v_kerning(struct hr_font_t *font,
hr_codepoint_t first_glyph,
hr_codepoint_t second_glyph);

/**
* Where a glyph hangs from when text runs vertically, returning false when
* nothing can say.
Expand Down Expand Up @@ -3423,6 +3483,58 @@ void hr_font_funcs_set_glyph_h_advance_func(struct hr_font_funcs_t *ffuncs,
void *user_data,
hr_destroy_func_t destroy);

/**
* Installs the batched horizontal advance callback.
* Takes ownership of `user_data`, including when the callback is cleared.
*
* # Safety
*
* `ffuncs` must be `NULL` or live; the callback and its data must be thread safe.
*/
void hr_font_funcs_set_glyph_h_advances_func(struct hr_font_funcs_t *ffuncs,
hr_font_get_glyph_h_advances_func_t func,
void *user_data,
hr_destroy_func_t destroy);

/**
* Installs the horizontal origin callback.
* Takes ownership of `user_data`, including when the callback is cleared.
*
* # Safety
*
* `ffuncs` must be `NULL` or live; the callback and its data must be thread safe.
*/
void hr_font_funcs_set_glyph_h_origin_func(struct hr_font_funcs_t *ffuncs,
hr_font_get_glyph_h_origin_func_t func,
void *user_data,
hr_destroy_func_t destroy);

/**
* Installs the horizontal kerning callback.
* Takes ownership of `user_data`, including when the callback is cleared.
*
* # Safety
*
* `ffuncs` must be `NULL` or live; the callback and its data must be thread safe.
*/
void hr_font_funcs_set_glyph_h_kerning_func(struct hr_font_funcs_t *ffuncs,
hr_font_get_glyph_h_kerning_func_t func,
void *user_data,
hr_destroy_func_t destroy);

/**
* Installs the vertical kerning callback.
* Takes ownership of `user_data`, including when the callback is cleared.
*
* # Safety
*
* `ffuncs` must be `NULL` or live; the callback and its data must be thread safe.
*/
void hr_font_funcs_set_glyph_v_kerning_func(struct hr_font_funcs_t *ffuncs,
hr_font_get_glyph_v_kerning_func_t func,
void *user_data,
hr_destroy_func_t destroy);

/**
* Sets the callback returning a glyph's vertical advance.
*
Expand Down
18 changes: 17 additions & 1 deletion harfrust_capi/src/face.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::sync::{Arc, OnceLock};

use harfrust::font::{Blob, Font, TableFunction};
use harfrust::Tag;
use read_fonts::TableProvider;
use read_fonts::{model::Format, TableProvider};

use crate::blob::hr_blob_t;
use crate::common::{hr_bool_t, hr_tag_t, tag_from_rust, tag_to_rust};
Expand Down Expand Up @@ -143,6 +143,22 @@ impl Object for hr_face_t {
}
}

/// Returns the number of SFNT faces in a font blob, or zero otherwise.
///
/// # Safety
///
/// `blob` must be `NULL` or a live blob.
#[no_mangle]
pub unsafe extern "C" fn hr_face_count(blob: *mut hr_blob_t) -> c_uint {
let Some(blob) = (unsafe { blob.as_ref() }) else {
return 0;
};
match Format::new(blob.bytes()) {
Some(Format::Sfnt(count)) => count,
_ => 0,
}
}

/// Creates a face over the font at `index` within `blob`.
///
/// The face takes its own reference to the blob. Never returns `NULL`; a blob
Expand Down
66 changes: 59 additions & 7 deletions harfrust_capi/src/font.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,19 @@ unsafe fn advances(
return;
}
let state = unsafe { object::or_empty(font.cast_const()) };
if horizontal
&& unsafe {
crate::font_funcs::FontFuncsAdapter::new(font, state).call_h_advances(
count,
first_glyph,
glyph_stride,
first_advance,
advance_stride,
)
}
{
return;
}
let mut glyph = first_glyph.cast::<u8>();
let mut advance = first_advance.cast::<u8>();
for _ in 0..count {
Expand Down Expand Up @@ -1062,14 +1075,53 @@ pub unsafe extern "C" fn hr_font_get_glyph_h_origin(
x: *mut hr_position_t,
y: *mut hr_position_t,
) -> hr_bool_t {
let _ = (font, glyph);
let state = unsafe { object::or_empty(font.cast_const()) };
let origin = crate::font_funcs::FontFuncsAdapter::new(font, state).call_h_origin(glyph);
if let Some(out) = unsafe { x.as_mut() } {
*out = 0;
*out = origin.map_or(0, |origin| origin.0);
}
if let Some(out) = unsafe { y.as_mut() } {
*out = 0;
*out = origin.map_or(0, |origin| origin.1);
}
true.into()
origin.is_some().into()
}

/// Returns the horizontal kerning supplied by font callbacks, or zero.
///
/// # Safety
///
/// `font` must be `NULL` or a live font.
#[no_mangle]
pub unsafe extern "C" fn hr_font_get_glyph_h_kerning(
font: *mut hr_font_t,
first_glyph: hr_codepoint_t,
second_glyph: hr_codepoint_t,
) -> hr_position_t {
let state = unsafe { object::or_empty(font.cast_const()) };
crate::font_funcs::FontFuncsAdapter::new(font, state).call_kerning(
first_glyph,
second_glyph,
false,
)
}

/// Returns the vertical kerning supplied by font callbacks, or zero.
///
/// # Safety
///
/// `font` must be `NULL` or a live font.
#[no_mangle]
pub unsafe extern "C" fn hr_font_get_glyph_v_kerning(
font: *mut hr_font_t,
first_glyph: hr_codepoint_t,
second_glyph: hr_codepoint_t,
) -> hr_position_t {
let state = unsafe { object::or_empty(font.cast_const()) };
crate::font_funcs::FontFuncsAdapter::new(font, state).call_kerning(
first_glyph,
second_glyph,
true,
)
}

/// Where a glyph hangs from when text runs vertically, returning false when
Expand Down Expand Up @@ -1212,9 +1264,9 @@ unsafe fn origin_for_direction(
) -> (hr_position_t, hr_position_t) {
let state = unsafe { object::or_empty(font.cast_const()) };
if crate::common::hr_direction_is_horizontal(direction) != 0 {
// A glyph's horizontal origin is its own, and nothing is guessed for
// it from the vertical one.
return (0, 0);
return crate::font_funcs::FontFuncsAdapter::new(font, state)
.call_h_origin(glyph)
.unwrap_or((0, 0));
}
if let Some(origin) = state.glyph_v_origin(font, glyph) {
return origin;
Expand Down
Loading
Loading