From 9b6da299719de324b812f53cd394a7c7ca47d441 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 2 Mar 2025 17:00:14 -0700 Subject: [PATCH] [font/var] Try to regain performance of all-normalized-coords=0 --- src/OT/Layout/GPOS/AnchorFormat3.hh | 6 +++--- src/OT/glyf/glyf.hh | 5 +++-- src/hb-font.cc | 20 ++++++++++++++++---- src/hb-font.hh | 1 + src/hb-ot-font.cc | 8 ++++---- src/hb-ot-hmtx-table.hh | 4 ++-- src/hb-ot-var-avar-table.hh | 13 +++++++++++++ 7 files changed, 42 insertions(+), 15 deletions(-) diff --git a/src/OT/Layout/GPOS/AnchorFormat3.hh b/src/OT/Layout/GPOS/AnchorFormat3.hh index b5422652c..b683a0a0c 100644 --- a/src/OT/Layout/GPOS/AnchorFormat3.hh +++ b/src/OT/Layout/GPOS/AnchorFormat3.hh @@ -37,12 +37,12 @@ struct AnchorFormat3 *x = font->em_fscale_x (xCoordinate); *y = font->em_fscale_y (yCoordinate); - if ((font->x_ppem || font->num_coords) && xDeviceTable.sanitize (&c->sanitizer, this)) + if ((font->x_ppem || font->has_nonzero_coords) && xDeviceTable.sanitize (&c->sanitizer, this)) { hb_barrier (); *x += (this+xDeviceTable).get_x_delta (font, c->var_store, c->var_store_cache); } - if ((font->y_ppem || font->num_coords) && yDeviceTable.sanitize (&c->sanitizer, this)) + if ((font->y_ppem || font->has_nonzero_coords) && yDeviceTable.sanitize (&c->sanitizer, this)) { hb_barrier (); *y += (this+yDeviceTable).get_y_delta (font, c->var_store, c->var_store_cache); @@ -63,7 +63,7 @@ struct AnchorFormat3 hb_pair_t *new_varidx_delta; if (!c->plan->layout_variation_idx_delta_map.has (x_varidx, &new_varidx_delta)) return_trace (false); - + x_varidx = hb_first (*new_varidx_delta); int delta = hb_second (*new_varidx_delta); if (delta != 0) diff --git a/src/OT/glyf/glyf.hh b/src/OT/glyf/glyf.hh index c1847b275..eb4d5523d 100644 --- a/src/OT/glyf/glyf.hh +++ b/src/OT/glyf/glyf.hh @@ -379,7 +379,7 @@ struct glyf_accelerator_t bool success = false; contour_point_t phantoms[glyf_impl::PHANTOM_COUNT]; - if (font->num_coords) + if (font->has_nonzero_coords) { hb_glyf_scratch_t scratch; success = get_points (font, gid, points_aggregator_t (font, nullptr, phantoms, false), @@ -433,7 +433,8 @@ struct glyf_accelerator_t bool get_extents (hb_font_t *font, hb_codepoint_t gid, hb_glyph_extents_t *extents) const - { return get_extents_at (font, gid, extents, hb_array (font->coords, font->num_coords)); } + { return get_extents_at (font, gid, extents, hb_array (font->coords, + font->has_nonzer_coords ? font->num_coords : 0)); } bool get_extents_at (hb_font_t *font, hb_codepoint_t gid, diff --git a/src/hb-font.cc b/src/hb-font.cc index 46c4edc97..997afac5e 100644 --- a/src/hb-font.cc +++ b/src/hb-font.cc @@ -1797,6 +1797,7 @@ DEFINE_NULL_INSTANCE (hb_font_t) = 0, /* ptem */ HB_FONT_NO_VAR_NAMED_INSTANCE, /* instance_index */ + false, /* has_nonzero_coords */ 0, /* num_coords */ nullptr, /* coords */ nullptr, /* design_coords */ @@ -1861,10 +1862,13 @@ hb_font_create (hb_face_t *face) #ifndef HB_NO_VAR // Initialize variations. - if (face && face->index >> 16) - hb_font_set_var_named_instance (font, (face->index >> 16) - 1); - else - hb_font_set_variations (font, nullptr, 0); + if (likely (face)) + { + if (face->index >> 16) + hb_font_set_var_named_instance (font, (face->index >> 16) - 1); + else if (face->table.avar->is_biased ()) + hb_font_set_variations (font, nullptr, 0); + } #endif return font; @@ -1883,6 +1887,14 @@ _hb_font_adopt_var_coords (hb_font_t *font, font->design_coords = design_coords; font->num_coords = coords_length; + font->has_nonzero_coords = false; + for (unsigned int i = 0; i < coords_length; i++) + if (coords[i]) + { + font->has_nonzero_coords = true; + break; + } + font->mults_changed (); // Easiest to call this to drop cached data } diff --git a/src/hb-font.hh b/src/hb-font.hh index 4c8190b0d..eae10f44c 100644 --- a/src/hb-font.hh +++ b/src/hb-font.hh @@ -135,6 +135,7 @@ struct hb_font_t /* Font variation coordinates. */ unsigned int instance_index; + bool has_nonzero_coords; unsigned int num_coords; int *coords; float *design_coords; diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc index d32c1d614..59b3e3079 100644 --- a/src/hb-ot-font.cc +++ b/src/hb-ot-font.cc @@ -226,9 +226,9 @@ hb_ot_get_glyph_h_advances (hb_font_t* font, void* font_data, #if !defined(HB_NO_VAR) && !defined(HB_NO_OT_FONT_ADVANCE_CACHE) const OT::HVAR &HVAR = *hmtx.var_table; const OT::ItemVariationStore &varStore = &HVAR + HVAR.varStore; - OT::ItemVariationStore::cache_t *varStore_cache = font->num_coords * count >= 128 ? varStore.create_cache () : nullptr; + OT::ItemVariationStore::cache_t *varStore_cache = font->has_nonzero_coords && font->num_coords * count >= 128 ? varStore.create_cache () : nullptr; - bool use_cache = font->num_coords; + bool use_cache = font->has_nonzero_coords; #else OT::ItemVariationStore::cache_t *varStore_cache = nullptr; bool use_cache = false; @@ -345,7 +345,7 @@ hb_ot_get_glyph_v_advances (hb_font_t* font, void* font_data, #if !defined(HB_NO_VAR) && !defined(HB_NO_OT_FONT_ADVANCE_CACHE) const OT::VVAR &VVAR = *vmtx.var_table; const OT::ItemVariationStore &varStore = &VVAR + VVAR.varStore; - OT::ItemVariationStore::cache_t *varStore_cache = font->num_coords ? varStore.create_cache () : nullptr; + OT::ItemVariationStore::cache_t *varStore_cache = font->has_nonzero_coords ? varStore.create_cache () : nullptr; #else OT::ItemVariationStore::cache_t *varStore_cache = nullptr; #endif @@ -425,7 +425,7 @@ hb_ot_get_glyph_v_origin (hb_font_t *font, #ifndef HB_NO_VAR const OT::vmtx_accelerator_t &vmtx = *ot_face->vmtx; const OT::VVAR &VVAR = *vmtx.var_table; - if (font->num_coords) + if (font->has_nonzero_coords) VVAR.get_vorg_delta_unscaled (glyph, font->coords, font->num_coords, &delta); diff --git a/src/hb-ot-hmtx-table.hh b/src/hb-ot-hmtx-table.hh index 8e44f143b..92466df27 100644 --- a/src/hb-ot-hmtx-table.hh +++ b/src/hb-ot-hmtx-table.hh @@ -347,7 +347,7 @@ struct hmtxvmtx hb_codepoint_t glyph, int *lsb) const { - if (!font->num_coords) + if (!font->has_nonzero_coords) return get_leading_bearing_without_var_unscaled (glyph, lsb); #ifndef HB_NO_VAR @@ -403,7 +403,7 @@ struct hmtxvmtx unsigned int advance = get_advance_without_var_unscaled (glyph); #ifndef HB_NO_VAR - if (unlikely (glyph >= num_bearings) || !font->num_coords) + if (unlikely (glyph >= num_bearings) || !font->has_nonzero_coords) return advance; if (var_table.get_length ()) diff --git a/src/hb-ot-var-avar-table.hh b/src/hb-ot-var-avar-table.hh index 0eb09ef67..12c0150e2 100644 --- a/src/hb-ot-var-avar-table.hh +++ b/src/hb-ot-var-avar-table.hh @@ -386,6 +386,19 @@ struct avar } } + bool is_biased () const + { + // Whether this table can map a 0 to a non-0 value. + const SegmentMaps *map = &firstAxisSegmentMaps; + for (unsigned int i = 0; i < axisCount; i++) + { + if (map->map (0) != 0) + return true; + map = &StructAfter (*map); + } + return false; + } + bool subset (hb_subset_context_t *c) const { TRACE_SUBSET (this);