mirror of
https://github.com/storytold/harfbuzz.git
synced 2026-11-24 16:31:14 +01:00
[rust] Batch Fontations vertical origins
Register the vertical-origins batch callback. Compute X using HarfBuzz's batched horizontal advances, then compute Y with v_origin_y_batched. Retain HarfBuzz line and glyph-extents callbacks for fallback behavior. Preserve scalar write order for aliased inputs, overlapping outputs, and repeated X slots while batching normal interleaved position buffers. Add default/varied glyf, CFF, and CFF2 coverage with strided, unaligned, in-place, repeated, invalid-glyph, and zero-count queries. Tests: full Meson suite (281 passed, 1 skipped); font-only Clippy; unchanged 144-case metric snapshots. Another 48 full-font batch cases (53,622 glyph origins) matched the old scalar implementation. Clippy used opt-level=0 for the nightly compiler workaround. Seven-run origin medians improved about 19% overall, about 36-38% for unvaried glyf fonts; varied glyf and CFF/CFF2 remain dominated by extents. Assisted-by: OpenAI Codex
This commit is contained in:
1 parent
71fc895c78
commit
cdf5edb49e
2 files changed
+163
-18
No files matched your search
+75
-18
@@ -297,6 +297,23 @@ impl<'a> Iterator for StridedGlyphs<'a> {
|
||||
}
|
||||
}
|
||||
|
||||
fn strided_ranges_overlap<T, U>(
|
||||
count: u32,
|
||||
first_a: *const T,
|
||||
stride_a: u32,
|
||||
first_b: *const U,
|
||||
stride_b: u32,
|
||||
) -> bool {
|
||||
if count == 0 {
|
||||
return false;
|
||||
}
|
||||
let end_a =
|
||||
first_a as usize + (count - 1) as usize * stride_a as usize + std::mem::size_of::<T>();
|
||||
let end_b =
|
||||
first_b as usize + (count - 1) as usize * stride_b as usize + std::mem::size_of::<U>();
|
||||
end_a > first_b as usize && end_b > first_a as usize
|
||||
}
|
||||
|
||||
// SAFETY: All input/output slots must be readable/writable for this call.
|
||||
// The callback must access these buffers only through the supplied iterator.
|
||||
unsafe fn with_strided_glyphs(
|
||||
@@ -310,13 +327,8 @@ unsafe fn with_strided_glyphs(
|
||||
if count == 0 {
|
||||
return;
|
||||
}
|
||||
let glyph_end = first_glyph as usize
|
||||
+ (count - 1) as usize * glyph_stride as usize
|
||||
+ std::mem::size_of::<hb_codepoint_t>();
|
||||
let value_end = first_value as usize
|
||||
+ (count - 1) as usize * value_stride as usize
|
||||
+ std::mem::size_of::<hb_position_t>();
|
||||
let disjoint = glyph_end <= first_value as usize || value_end <= first_glyph as usize;
|
||||
let disjoint =
|
||||
!strided_ranges_overlap(count, first_glyph, glyph_stride, first_value, value_stride);
|
||||
if disjoint
|
||||
&& first_value.is_aligned()
|
||||
&& (count == 1
|
||||
@@ -518,19 +530,54 @@ extern "C" fn _hb_fontations_get_glyph_v_advances(
|
||||
}
|
||||
}
|
||||
|
||||
extern "C" fn _hb_fontations_get_glyph_v_origin(
|
||||
extern "C" fn _hb_fontations_get_glyph_v_origins(
|
||||
font: *mut hb_font_t,
|
||||
font_data: *mut ::std::os::raw::c_void,
|
||||
glyph: hb_codepoint_t,
|
||||
x: *mut hb_position_t,
|
||||
y: *mut hb_position_t,
|
||||
count: ::std::os::raw::c_uint,
|
||||
first_glyph: *const hb_codepoint_t,
|
||||
glyph_stride: ::std::os::raw::c_uint,
|
||||
first_x: *mut hb_position_t,
|
||||
x_stride: ::std::os::raw::c_uint,
|
||||
first_y: *mut hb_position_t,
|
||||
y_stride: ::std::os::raw::c_uint,
|
||||
_user_data: *mut ::std::os::raw::c_void,
|
||||
) -> hb_bool_t {
|
||||
// Preserve scalar write order for aliased buffers. Equal output strides
|
||||
// can share an interleaved buffer while still having disjoint slots.
|
||||
let outputs_overlap = strided_ranges_overlap(count, first_x, x_stride, first_y, y_stride);
|
||||
let interleaved = x_stride == y_stride && x_stride != 0 && {
|
||||
let gap = (first_x as usize).abs_diff(first_y as usize) % x_stride as usize;
|
||||
gap >= std::mem::size_of::<hb_position_t>()
|
||||
&& x_stride as usize - gap >= std::mem::size_of::<hb_position_t>()
|
||||
};
|
||||
if (count > 1 && (x_stride as usize) < std::mem::size_of::<hb_position_t>())
|
||||
|| strided_ranges_overlap(count, first_glyph, glyph_stride, first_x, x_stride)
|
||||
|| strided_ranges_overlap(count, first_glyph, glyph_stride, first_y, y_stride)
|
||||
|| (outputs_overlap && !interleaved)
|
||||
{
|
||||
for i in 0..count {
|
||||
let glyph = unsafe { struct_at_offset(first_glyph, i, glyph_stride) };
|
||||
let (mut x, mut y) = (0, 0);
|
||||
_hb_fontations_get_glyph_v_origins(
|
||||
font, font_data, 1, &glyph, 0, &mut x, 0, &mut y, 0, _user_data,
|
||||
);
|
||||
unsafe {
|
||||
write_struct_at_offset(first_x, i, x_stride, x);
|
||||
write_struct_at_offset(first_y, i, y_stride, y);
|
||||
}
|
||||
}
|
||||
return true as hb_bool_t;
|
||||
}
|
||||
|
||||
let data = unsafe { &mut *(font_data as *mut FontationsData) };
|
||||
data.check_for_updates();
|
||||
|
||||
unsafe {
|
||||
*x = hb_font_get_glyph_h_advance(font, glyph) / 2;
|
||||
hb_font_get_glyph_h_advances(font, count, first_glyph, glyph_stride, first_x, x_stride);
|
||||
for i in 0..count {
|
||||
let x = struct_at_offset(first_x, i, x_stride) / 2;
|
||||
write_struct_at_offset(first_x, i, x_stride, x);
|
||||
}
|
||||
}
|
||||
|
||||
let line = font_line_extents(font);
|
||||
@@ -546,12 +593,22 @@ extern "C" fn _hb_fontations_get_glyph_v_origin(
|
||||
height: -(extents.height as i64),
|
||||
})
|
||||
};
|
||||
let y_origin = data
|
||||
let metrics = data
|
||||
.glyph_metrics()
|
||||
.with_line_extents(Some(line))
|
||||
.with_glyph_extents(Some(&extents))
|
||||
.v_origin_y(GlyphId::new(glyph));
|
||||
unsafe { *y = hb_position(y_origin) };
|
||||
.with_glyph_extents(Some(&extents));
|
||||
unsafe {
|
||||
with_strided_glyphs(
|
||||
count,
|
||||
first_glyph,
|
||||
glyph_stride,
|
||||
first_y,
|
||||
y_stride,
|
||||
|glyphs| {
|
||||
metrics.v_origin_y_batched(hb_position, glyphs);
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
true as hb_bool_t
|
||||
}
|
||||
@@ -1424,9 +1481,9 @@ fn _hb_fontations_font_funcs_get() -> *mut hb_font_funcs_t {
|
||||
null_mut(),
|
||||
None,
|
||||
);
|
||||
hb_font_funcs_set_glyph_v_origin_func(
|
||||
hb_font_funcs_set_glyph_v_origins_func(
|
||||
ffuncs,
|
||||
Some(_hb_fontations_get_glyph_v_origin),
|
||||
Some(_hb_fontations_get_glyph_v_origins),
|
||||
null_mut(),
|
||||
None,
|
||||
);
|
||||
|
||||
@@ -895,6 +895,93 @@ test_fontations_strides (void)
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
|
||||
static void
|
||||
test_fontations_v_origins (void)
|
||||
{
|
||||
const char *fonts[] = {
|
||||
"fonts/Roboto-Regular.abc.ttf",
|
||||
"fonts/Roboto-Variable.abc.ttf",
|
||||
"fonts/SourceSerifVariable-Roman-VVAR.abc.ttf",
|
||||
"fonts/AdobeVFPrototype.abc.otf",
|
||||
"../shape/data/in-house/fonts/NotoSansCJK-VF.abc.ttf",
|
||||
"../shape/data/in-house/fonts/NotoSansCJK-VF.abc.otf",
|
||||
};
|
||||
const int coords[] = {0, 16384, -16384, 0};
|
||||
const hb_codepoint_t glyphs[] = {0, 1, 2, 3, HB_CODEPOINT_INVALID};
|
||||
for (unsigned f = 0; f < G_N_ELEMENTS (fonts); f++)
|
||||
{
|
||||
hb_face_t *face = hb_test_open_font_file (fonts[f]);
|
||||
hb_font_t *font = hb_font_create (face);
|
||||
hb_fontations_font_set_funcs (font);
|
||||
int upem = hb_face_get_upem (face);
|
||||
for (int sign = 1; sign >= -1; sign -= 2)
|
||||
{
|
||||
hb_font_set_scale (font, sign * upem * 2, sign * upem * 3);
|
||||
for (unsigned c = 0; c < G_N_ELEMENTS (coords); c++)
|
||||
{
|
||||
hb_font_set_var_coords_normalized (font, &coords[c], 1);
|
||||
hb_position_t expected_x[G_N_ELEMENTS (glyphs)], expected_y[G_N_ELEMENTS (glyphs)];
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
|
||||
g_assert_true (hb_font_get_glyph_v_origin (font, glyphs[i], &expected_x[i], &expected_y[i]));
|
||||
|
||||
hb_position_t origins[4 * G_N_ELEMENTS (glyphs)];
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (origins); i++)
|
||||
origins[i] = 123456;
|
||||
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
|
||||
glyphs, sizeof (glyphs[0]),
|
||||
origins, 4 * sizeof (origins[0]),
|
||||
origins + 1, 4 * sizeof (origins[0])));
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
|
||||
{
|
||||
g_assert_cmpint (origins[4 * i], ==, expected_x[i]);
|
||||
g_assert_cmpint (origins[4 * i + 1], ==, expected_y[i]);
|
||||
g_assert_cmpint (origins[4 * i + 2], ==, 123456);
|
||||
g_assert_cmpint (origins[4 * i + 3], ==, 123456);
|
||||
}
|
||||
|
||||
hb_position_t last_x = 0, last_y = 0;
|
||||
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
|
||||
glyphs, sizeof (glyphs[0]), &last_x, 0, &last_y, 0));
|
||||
g_assert_cmpint (last_x, ==, expected_x[G_N_ELEMENTS (glyphs) - 1]);
|
||||
g_assert_cmpint (last_y, ==, expected_y[G_N_ELEMENTS (glyphs) - 1]);
|
||||
|
||||
hb_codepoint_t in_place[G_N_ELEMENTS (glyphs)];
|
||||
memcpy (in_place, glyphs, sizeof (glyphs));
|
||||
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
|
||||
in_place, sizeof (in_place[0]),
|
||||
(hb_position_t *) in_place, sizeof (in_place[0]),
|
||||
origins, sizeof (origins[0])));
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
|
||||
{
|
||||
g_assert_cmpint ((hb_position_t) in_place[i], ==, expected_x[i]);
|
||||
g_assert_cmpint (origins[i], ==, expected_y[i]);
|
||||
}
|
||||
|
||||
uint8_t x_bytes[1 + 5 * G_N_ELEMENTS (glyphs)], y_bytes[sizeof (x_bytes)];
|
||||
memset (x_bytes, 0xA5, sizeof (x_bytes));
|
||||
memset (y_bytes, 0xA5, sizeof (y_bytes));
|
||||
g_assert_true (hb_font_get_glyph_v_origins (font, G_N_ELEMENTS (glyphs),
|
||||
glyphs, sizeof (glyphs[0]),
|
||||
(hb_position_t *) (x_bytes + 1), 5,
|
||||
(hb_position_t *) (y_bytes + 1), 5));
|
||||
for (unsigned i = 0; i < G_N_ELEMENTS (glyphs); i++)
|
||||
{
|
||||
hb_position_t x, y;
|
||||
memcpy (&x, x_bytes + 1 + 5 * i, sizeof (x));
|
||||
memcpy (&y, y_bytes + 1 + 5 * i, sizeof (y));
|
||||
g_assert_cmpint (x, ==, expected_x[i]);
|
||||
g_assert_cmpint (y, ==, expected_y[i]);
|
||||
g_assert_cmpuint (x_bytes[5 * i + 5], ==, 0xA5);
|
||||
g_assert_cmpuint (y_bytes[5 * i + 5], ==, 0xA5);
|
||||
}
|
||||
g_assert_true (hb_font_get_glyph_v_origins (font, 0, NULL, 0, NULL, 0, NULL, 0));
|
||||
}
|
||||
}
|
||||
hb_font_destroy (font);
|
||||
hb_face_destroy (face);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
test_fontations_advances (void)
|
||||
{
|
||||
@@ -988,6 +1075,7 @@ main (int argc, char **argv)
|
||||
hb_test_add (test_fontations_glyph_from_name_threads);
|
||||
hb_test_add (test_fontations_scale_changes);
|
||||
hb_test_add (test_fontations_strides);
|
||||
hb_test_add (test_fontations_v_origins);
|
||||
hb_test_add (test_fontations_advances);
|
||||
#endif
|
||||
|
||||
|
||||
Reference in new issue
Block a user