[ot-font] Rewrite h/v-advance code

Faster, better cached...

24% faster across the benchmarks for hmtx-only path.

Comparing before to after
Benchmark                                                                       Time             CPU      Time Old      Time New       CPU Old       CPU New
------------------------------------------------------------------------------------------------------------------------------------------------------------
BM_Font/glyph_h_advances/Roboto-Regular.ttf/ot                               -0.2410         -0.2411             2             1             2             1
BM_Font/glyph_h_advances/RobotoFlex-Variable.ttf/ot                          -0.2482         -0.2484             1             1             1             1
BM_Font/glyph_h_advances/RobotoFlex-Variable.ttf/var/ot                      -0.0296         -0.0296            48            47            48            47
BM_Font/glyph_h_advances/SourceSansPro-Regular.otf/ot                        -0.2412         -0.2416             2             2             2             2
BM_Font/glyph_h_advances/SourceSansPro-Regular.otf/var/ot                    -0.2381         -0.2384             2             2             2             2
BM_Font/glyph_h_advances/AdobeVFPrototype.otf/ot                             -0.2347         -0.2349             0             0             0             0
BM_Font/glyph_h_advances/AdobeVFPrototype.otf/var/ot                         -0.0343         -0.0346             2             2             2             2
BM_Font/glyph_h_advances/SourceSerifVariable-Roman.ttf/ot                    -0.2623         -0.2621             2             1             2             1
BM_Font/glyph_h_advances/SourceSerifVariable-Roman.ttf/var/ot                -0.0388         -0.0401            24            23            24            23
BM_Font/glyph_h_advances/Comfortaa-Regular-new.ttf/ot                        -0.2396         -0.2394             1             1             1             1
BM_Font/glyph_h_advances/NotoNastaliqUrdu-Regular.ttf/ot                     -0.2344         -0.2346             2             1             2             1
BM_Font/glyph_h_advances/NotoSerifMyanmar-Regular.otf/ot                     -0.2366         -0.2367             1             1             1             1
OVERALL_GEOMEAN                                                              -0.1945         -0.1948             0             0             0             0
This commit is contained in:
Behdad Esfahbod committed 2025-07-20 07:19:23 -06:00
1 parent dd85aeba88
commit b359c83cdd
5 files changed
+177 -71

No files matched your search

+14 -14
View File
@@ -372,34 +372,34 @@ struct glyf_accelerator_t
contour_point_t *get_phantoms_sink () { return phantoms; }
};
#ifndef HB_NO_VAR
unsigned
get_advance_with_var_unscaled (hb_font_t *font, hb_codepoint_t gid, bool is_vertical) const
get_advance_with_var_unscaled (hb_codepoint_t gid,
hb_font_t *font,
bool is_vertical,
hb_scalar_cache_t *gvar_cache = nullptr) const
{
if (unlikely (gid >= num_glyphs)) return 0;
bool success = false;
contour_point_t phantoms[glyf_impl::PHANTOM_COUNT];
if (font->has_nonzero_coords)
{
hb_glyf_scratch_t scratch;
success = get_points (font, gid, points_aggregator_t (font, nullptr, phantoms, false),
hb_array (font->coords, font->num_coords),
scratch);
}
hb_glyf_scratch_t scratch;
success = get_points (font, gid, points_aggregator_t (font, nullptr, phantoms, false),
hb_array (font->coords, font->num_coords),
scratch, gvar_cache);
if (unlikely (!success))
return
#ifndef HB_NO_VERTICAL
is_vertical ? vmtx->get_advance_without_var_unscaled (gid) :
#endif
hmtx->get_advance_without_var_unscaled (gid);
{
unsigned upem = font->face->get_upem ();
return is_vertical ? upem : upem / 2;
}
float result = is_vertical
? phantoms[glyf_impl::PHANTOM_TOP].y - phantoms[glyf_impl::PHANTOM_BOTTOM].y
: phantoms[glyf_impl::PHANTOM_RIGHT].x - phantoms[glyf_impl::PHANTOM_LEFT].x;
return hb_clamp (roundf (result), 0.f, (float) UINT_MAX / 2);
}
#endif
bool get_leading_bearing_with_var_unscaled (hb_font_t *font, hb_codepoint_t gid, bool is_vertical, int *lsb) const
{
+154 -31
View File
@@ -289,37 +289,57 @@ hb_ot_get_glyph_h_advances (hb_font_t* font, void* font_data,
unsigned advance_stride,
void *user_data HB_UNUSED)
{
// Duplicated in v_advances. Ugly. Keep in sync'ish.
const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
const hb_ot_face_t *ot_face = ot_font->ot_face;
const OT::hmtx_accelerator_t &hmtx = *ot_face->hmtx;
ot_font->check_serial (font);
const OT::HVAR &HVAR = *hmtx.var_table;
const OT::ItemVariationStore &varStore = &HVAR + HVAR.varStore;
OT::hb_scalar_cache_t *varStore_cache = font->has_nonzero_coords ? ot_font->h.acquire_varStore_cache (varStore) : nullptr;
hb_ot_font_advance_cache_t *advance_cache = nullptr;
bool use_cache = font->has_nonzero_coords;
if (use_cache)
{
advance_cache = ot_font->h.acquire_advance_cache ();
if (!advance_cache)
use_cache = false;
}
if (!use_cache)
if (unlikely (!hmtx.has_data ()))
{
hb_position_t advance = font->face->get_upem () / 2;
advance = font->em_scale_x (advance);
for (unsigned int i = 0; i < count; i++)
{
*first_advance = font->em_scale_x (hmtx.get_advance_with_var_unscaled (*first_glyph, font, varStore_cache));
*first_advance = advance;
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
}
return;
}
#ifndef HB_NO_VAR
if (!font->has_nonzero_coords)
#endif
{
fallback:
// Just plain htmx data. No need to cache.
for (unsigned int i = 0; i < count; i++)
{
*first_advance = font->em_scale_x (hmtx.get_advance_without_var_unscaled (*first_glyph));
first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
}
return;
}
else
{ /* Use cache. */
#ifndef HB_NO_VAR
/* has_nonzero_coords. */
ot_font->check_serial (font);
hb_ot_font_advance_cache_t *advance_cache = ot_font->h.acquire_advance_cache ();
if (!advance_cache)
{
// malloc failure. Just use the fallback non-variable path.
goto fallback;
}
/* If HVAR is present, use it.*/
const OT::HVAR &HVAR = *hmtx.var_table;
if (HVAR.has_data ())
{
const OT::ItemVariationStore &varStore = &HVAR + HVAR.varStore;
OT::hb_scalar_cache_t *varStore_cache = ot_font->h.acquire_varStore_cache (varStore);
for (unsigned int i = 0; i < count; i++)
{
hb_position_t v;
@@ -336,10 +356,42 @@ hb_ot_get_glyph_h_advances (hb_font_t* font, void* font_data,
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
}
ot_font->h.release_varStore_cache (varStore_cache);
ot_font->h.release_advance_cache (advance_cache);
return;
}
ot_font->h.release_varStore_cache (varStore_cache);
const auto &gvar = *ot_face->gvar;
if (gvar.has_data ())
{
const auto &glyf = *ot_face->glyf;
OT::hb_scalar_cache_t *gvar_cache = ot_font->draw.acquire_gvar_cache (gvar);
for (unsigned int i = 0; i < count; i++)
{
hb_position_t v;
unsigned cv;
if (advance_cache->get (*first_glyph, &cv))
v = cv;
else
{
v = glyf.get_advance_with_var_unscaled (*first_glyph, font, false, gvar_cache);
advance_cache->set (*first_glyph, v);
}
*first_advance = font->em_scale_x (v);
first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
}
ot_font->draw.release_gvar_cache (gvar_cache);
ot_font->h.release_advance_cache (advance_cache);
return;
}
ot_font->h.release_advance_cache (advance_cache);
// No HVAR or GVAR. Just use the fallback non-variable path.
goto fallback;
#endif
}
#ifndef HB_NO_VERTICAL
@@ -352,40 +404,111 @@ hb_ot_get_glyph_v_advances (hb_font_t* font, void* font_data,
unsigned advance_stride,
void *user_data HB_UNUSED)
{
// Duplicated from h_advances. Ugly. Keep in sync'ish.
const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data;
const hb_ot_face_t *ot_face = ot_font->ot_face;
const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx;
if (vmtx.has_data ())
if (unlikely (!vmtx.has_data ()))
{
hb_font_extents_t font_extents;
font->get_h_extents_with_fallback (&font_extents);
hb_position_t advance = font_extents.ascender - font_extents.descender;
advance = font->em_scale_y (- (int) advance);
for (unsigned int i = 0; i < count; i++)
{
*first_advance = advance;
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
}
return;
}
#ifndef HB_NO_VAR
if (!font->has_nonzero_coords)
#endif
{
fallback:
// Just plain vtmx data. No need to cache.
for (unsigned int i = 0; i < count; i++)
{
*first_advance = font->em_scale_y (- (int) vmtx.get_advance_without_var_unscaled (*first_glyph));
first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
}
return;
}
#ifndef HB_NO_VAR
/* has_nonzero_coords. */
ot_font->check_serial (font);
hb_ot_font_advance_cache_t *advance_cache = ot_font->v.acquire_advance_cache ();
if (!advance_cache)
{
// malloc failure. Just use the fallback non-variable path.
goto fallback;
}
/* If VVAR is present, use it.*/
const OT::VVAR &VVAR = *vmtx.var_table;
if (VVAR.has_data ())
{
ot_font->check_serial (font);
const OT::VVAR &VVAR = *vmtx.var_table;
const OT::ItemVariationStore &varStore = &VVAR + VVAR.varStore;
OT::hb_scalar_cache_t *varStore_cache = font->has_nonzero_coords ? ot_font->v.acquire_varStore_cache (varStore) : nullptr;
// TODO Use advance_cache.
OT::hb_scalar_cache_t *varStore_cache = ot_font->v.acquire_varStore_cache (varStore);
for (unsigned int i = 0; i < count; i++)
{
*first_advance = font->em_scale_y (-(int) vmtx.get_advance_with_var_unscaled (*first_glyph, font, varStore_cache));
hb_position_t v;
unsigned cv;
if (advance_cache->get (*first_glyph, &cv))
v = cv;
else
{
v = vmtx.get_advance_with_var_unscaled (*first_glyph, font, varStore_cache);
advance_cache->set (*first_glyph, v);
}
*first_advance = font->em_scale_y (- (int) v);
first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
}
ot_font->v.release_varStore_cache (varStore_cache);
ot_font->v.release_advance_cache (advance_cache);
return;
}
else
const auto &gvar = *ot_face->gvar;
if (gvar.has_data ())
{
hb_font_extents_t font_extents;
font->get_h_extents_with_fallback (&font_extents);
hb_position_t advance = -(font_extents.ascender - font_extents.descender);
const auto &glyf = *ot_face->glyf;
OT::hb_scalar_cache_t *gvar_cache = ot_font->draw.acquire_gvar_cache (gvar);
for (unsigned int i = 0; i < count; i++)
{
*first_advance = advance;
hb_position_t v;
unsigned cv;
if (advance_cache->get (*first_glyph, &cv))
v = cv;
else
{
v = glyf.get_advance_with_var_unscaled (*first_glyph, font, true, gvar_cache);
advance_cache->set (*first_glyph, v);
}
*first_advance = font->em_scale_y (- (int) v);
first_glyph = &StructAtOffsetUnaligned<hb_codepoint_t> (first_glyph, glyph_stride);
first_advance = &StructAtOffsetUnaligned<hb_position_t> (first_advance, advance_stride);
}
ot_font->draw.release_gvar_cache (gvar_cache);
ot_font->v.release_advance_cache (advance_cache);
return;
}
ot_font->v.release_advance_cache (advance_cache);
// No HVAR or GVAR. Just use the fallback non-variable path.
goto fallback;
#endif
}
#endif
+7 -20
View File
@@ -48,9 +48,6 @@
HB_INTERNAL bool
_glyf_get_leading_bearing_with_var_unscaled (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical, int *lsb);
HB_INTERNAL unsigned
_glyf_get_advance_with_var_unscaled (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical);
HB_INTERNAL bool
_glyf_get_leading_bearing_without_var_unscaled (hb_face_t *face, hb_codepoint_t gid, bool is_vertical, int *lsb);
@@ -402,27 +399,17 @@ struct hmtxvmtx
return advances[hb_min (glyph - num_bearings, num_advances - num_bearings - 1)];
}
unsigned get_advance_with_var_unscaled (hb_codepoint_t glyph,
hb_font_t *font,
#ifndef HB_NO_VAR
unsigned get_advance_with_var_unscaled (hb_codepoint_t glyph,
hb_font_t *font,
hb_scalar_cache_t *store_cache = nullptr) const
{
unsigned int advance = get_advance_without_var_unscaled (glyph);
#ifndef HB_NO_VAR
if (unlikely (glyph >= num_bearings) || !font->has_nonzero_coords)
return advance;
if (var_table.get_length ())
return advance + roundf (var_table->get_advance_delta_unscaled (glyph,
font->coords, font->num_coords,
store_cache));
unsigned glyf_advance = _glyf_get_advance_with_var_unscaled (font, glyph, T::tableTag == HB_OT_TAG_vmtx);
return glyf_advance ? glyf_advance : advance;
#else
return advance;
#endif
return advance + roundf (var_table->get_advance_delta_unscaled (glyph,
font->coords, font->num_coords,
store_cache));
}
#endif
protected:
// 0 <= num_long_metrics <= num_bearings <= num_advances <= num_glyphs
+2
View File
@@ -284,6 +284,8 @@ struct HVARVVAR
static constexpr hb_tag_t HVARTag = HB_OT_TAG_HVAR;
static constexpr hb_tag_t VVARTag = HB_OT_TAG_VVAR;
bool has_data () const { return version.major != 0; }
bool sanitize (hb_sanitize_context_t *c) const
{
TRACE_SANITIZE (this);
-6
View File
@@ -121,12 +121,6 @@ _glyf_get_leading_bearing_with_var_unscaled (hb_font_t *font, hb_codepoint_t gly
{
return font->face->table.glyf->get_leading_bearing_with_var_unscaled (font, glyph, is_vertical, lsb);
}
unsigned
_glyf_get_advance_with_var_unscaled (hb_font_t *font, hb_codepoint_t glyph, bool is_vertical)
{
return font->face->table.glyf->get_advance_with_var_unscaled (font, glyph, is_vertical);
}
#endif
bool