From 5eb832337116680c8ec5c7ecf5febbabdbf6896f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 10 Oct 2026 20:41:56 -0600 Subject: [PATCH] [ot] Evaluate constant item variation regions at default coordinates A zero-peak region ignores every axis and can contribute a nonzero variation delta even when coordinates are absent or zero. Default shortcuts skipped these deltas in HVAR/VVAR advances, vertical origins, GPOS value records, anchors, and generic VariationDevice access. Cache whether metrics and GDEF stores have regions active at the default when their accelerators load. Preserve existing fast paths for ordinary stores and evaluate constant-region stores at default coordinates. Add a generated CFF2 font exercising constant and ordinary regions, empty and short coordinate arrays, and returning to default after non-default shaping. Skera and FontTools full instances give the same metrics and positioning. All 254 configured Meson tests pass; core files also compile with variation, vertical, and layout support disabled. Assisted-by: Codex --- src/OT/Layout/GDEF/GDEF.hh | 3 + src/OT/Layout/GPOS/AnchorFormat3.hh | 5 +- src/OT/Layout/GPOS/ValueFormat.hh | 5 +- src/hb-ot-font.cc | 10 +-- src/hb-ot-hmtx-table.hh | 4 + src/hb-ot-layout-common.hh | 15 +++- test/api/fonts/default-item-variations.otf | Bin 0 -> 1420 bytes .../fonts/generate-default-item-variations.py | 78 ++++++++++++++++++ test/api/test-ot-metrics.c | 44 ++++++++++ 9 files changed, 153 insertions(+), 11 deletions(-) create mode 100644 test/api/fonts/default-item-variations.otf create mode 100644 test/api/fonts/generate-default-item-variations.py diff --git a/src/OT/Layout/GDEF/GDEF.hh b/src/OT/Layout/GDEF/GDEF.hh index e82210ba8..5561318c5 100644 --- a/src/OT/Layout/GDEF/GDEF.hh +++ b/src/OT/Layout/GDEF/GDEF.hh @@ -985,6 +985,8 @@ struct GDEF table = hb_blob_get_empty (); } + has_default_variations = table->get_var_store ().has_default_regions (); + #ifndef HB_NO_GDEF_CACHE /* Flatten each mark set whose glyphs span fewer than PAGE_BITS * gids into a single bit-page biased at its first glyph, for @@ -1086,6 +1088,7 @@ struct GDEF } hb_blob_ptr_t table; + bool has_default_variations; #ifndef HB_NO_GDEF_CACHE struct mark_glyph_set_bitmap_t { diff --git a/src/OT/Layout/GPOS/AnchorFormat3.hh b/src/OT/Layout/GPOS/AnchorFormat3.hh index c49705bea..affa54526 100644 --- a/src/OT/Layout/GPOS/AnchorFormat3.hh +++ b/src/OT/Layout/GPOS/AnchorFormat3.hh @@ -37,12 +37,13 @@ struct AnchorFormat3 *x = font->em_fscale_x (xCoordinate); *y = font->em_fscale_y (yCoordinate); - if ((font->x_ppem || font->has_nonzero_coords) && xDeviceTable.sanitize (&c->sanitizer, this)) + bool use_variations = font->has_nonzero_coords || c->gdef_accel.has_default_variations; + if ((font->x_ppem || use_variations) && 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->has_nonzero_coords) && yDeviceTable.sanitize (&c->sanitizer, this)) + if ((font->y_ppem || use_variations) && yDeviceTable.sanitize (&c->sanitizer, this)) { hb_barrier (); *y += (this+yDeviceTable).get_y_delta (font, c->var_store, c->var_store_cache); diff --git a/src/OT/Layout/GPOS/ValueFormat.hh b/src/OT/Layout/GPOS/ValueFormat.hh index 08e55cbc6..006f84130 100644 --- a/src/OT/Layout/GPOS/ValueFormat.hh +++ b/src/OT/Layout/GPOS/ValueFormat.hh @@ -120,8 +120,9 @@ struct ValueFormat : HBUINT16 if (!has_device ()) return ret; - bool use_x_device = font->x_ppem || font->has_nonzero_coords; - bool use_y_device = font->y_ppem || font->has_nonzero_coords; + bool use_variations = font->has_nonzero_coords || c->gdef_accel.has_default_variations; + bool use_x_device = font->x_ppem || use_variations; + bool use_y_device = font->y_ppem || use_variations; if (!use_x_device && !use_y_device) return ret; diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc index 38b406816..f0cfd006a 100644 --- a/src/hb-ot-font.cc +++ b/src/hb-ot-font.cc @@ -406,7 +406,7 @@ hb_ot_get_glyph_h_advances (hb_font_t* font, void* font_data, } #ifndef HB_NO_VAR - if (!font->has_nonzero_coords) + if (!font->has_nonzero_coords && !hmtx.has_default_variations) { fallback: #else @@ -423,7 +423,7 @@ hb_ot_get_glyph_h_advances (hb_font_t* font, void* font_data, } #ifndef HB_NO_VAR - /* has_nonzero_coords. */ + /* Non-default coordinates or constant variation regions. */ ot_font->check_serial (font); hb_ot_font_advance_cache_t *advance_cache = ot_font->h.acquire_advance_cache (); @@ -531,7 +531,7 @@ hb_ot_get_glyph_v_advances (hb_font_t* font, void* font_data, } #ifndef HB_NO_VAR - if (!font->has_nonzero_coords) + if (!font->has_nonzero_coords && !vmtx.has_default_variations) { fallback: #else @@ -548,7 +548,7 @@ hb_ot_get_glyph_v_advances (hb_font_t* font, void* font_data, } #ifndef HB_NO_VAR - /* has_nonzero_coords. */ + /* Non-default coordinates or constant variation regions. */ ot_font->check_serial (font); hb_ot_font_advance_cache_t *advance_cache = ot_font->v.acquire_advance_cache (); @@ -668,7 +668,7 @@ hb_ot_get_glyph_v_origins (hb_font_t *font, if (origin_cache && VORG.has_data ()) { #ifndef HB_NO_VAR - if (!font->has_nonzero_coords) + if (!font->has_nonzero_coords && !ot_face->vmtx->has_default_variations) #endif { for (unsigned i = 0; i < count; i++) diff --git a/src/hb-ot-hmtx-table.hh b/src/hb-ot-hmtx-table.hh index 6b83a34fb..64e5dc33e 100644 --- a/src/hb-ot-hmtx-table.hh +++ b/src/hb-ot-hmtx-table.hh @@ -453,6 +453,7 @@ struct hmtx_accelerator_t : hb_dual_accelerator_t (face) { var_table = hb_sanitize_context_t ().reference_table (face); + has_default_variations = var_table->get_var_store ().has_default_regions (); } ~hmtx_accelerator_t () @@ -484,6 +485,7 @@ struct hmtx_accelerator_t : hb_dual_accelerator_t var_table; + bool has_default_variations; }; #ifndef HB_NO_VERTICAL @@ -494,6 +496,7 @@ struct vmtx_accelerator_t : hb_dual_accelerator_t (face) { var_table = hb_sanitize_context_t ().reference_table (face); + has_default_variations = var_table->get_var_store ().has_default_regions (); } ~vmtx_accelerator_t () @@ -525,6 +528,7 @@ struct vmtx_accelerator_t : hb_dual_accelerator_t var_table; + bool has_default_variations; }; #else struct vmtx_accelerator_t : vmtx::accelerator_t { diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 983b77f28..93c4d36bd 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -3567,6 +3567,17 @@ struct ItemVariationStore } public: + bool has_default_regions () const + { +#ifdef HB_NO_VAR + return false; +#endif + const auto ®ion_list = this+regions; + for (unsigned i = 0; i < region_list.regionCount; i++) + if (region_list.evaluate (i, nullptr, 0)) return true; + return false; + } + bool has_delta_set (unsigned int index) const { #ifdef HB_NO_VAR @@ -6184,12 +6195,12 @@ struct VariationDevice hb_position_t get_x_delta (hb_font_t *font, const ItemVariationStore &store, hb_scalar_cache_t *store_cache = nullptr) const - { return !font->has_nonzero_coords ? 0 : font->em_scalef_x (get_delta (font, store, store_cache)); } + { return font->em_scalef_x (get_delta (font, store, store_cache)); } hb_position_t get_y_delta (hb_font_t *font, const ItemVariationStore &store, hb_scalar_cache_t *store_cache = nullptr) const - { return !font->has_nonzero_coords ? 0 : font->em_scalef_y (get_delta (font, store, store_cache)); } + { return font->em_scalef_y (get_delta (font, store, store_cache)); } VariationDevice* copy (hb_serialize_context_t *c, const hb_hashmap_t> *layout_variation_idx_delta_map) const diff --git a/test/api/fonts/default-item-variations.otf b/test/api/fonts/default-item-variations.otf new file mode 100644 index 0000000000000000000000000000000000000000..a2b9da83834d9cd3c1f236154901d949ad32cb95 GIT binary patch literal 1420 zcmbtUO=uHQ5dK~^No!5DEdhIwl4_I+MN1JWQn1xDwV))VArVCEVm3`^^JB98Ie3WT z&5L*ty!dwz4?Ps=&3Z2$J@q6NFFi={P7dW zO`jW{xF3B2s2cH1Dwdcu`l-RYM%JEU{DX4!GVwa`+?|41_PzVn$M|o=quE8Xva|KM z4)_L$M{<^#Nt>^>=s!+;AxDF8LJbkidEs2aUN*L=L2Z!*3g&XzNmVTq`-^758p79) zfJgTGQZ7|(7}Q_{$U=*HKgrNHLmXag?&o_*+zysSp6Ty9*KZ#k{Q<){?cS-?8a-%8 z(Mbka2~AvMgz6()yt{94HAgTDmQ->v)UJ9AAM5{j8gC7`L?Si~<(y@Nu+i$L9)gk# znpvtufPxw_(= z!g7lD5G`kM#??JIg0!PEJ^;(r6*_P?Vr9*Rc{?;^TZK@{tmI8QUn*7;*6hN(SxH;@ z*_@ruXY3pzT$qK41vsKy5+We?K3eG!vpTR#kFG~R^Yg6MfiuQ1CaYRVTl8em$etVy z?sW$D27-G|#XES#i%^m1wIn;Dr-mJqzA|>ssuXdgU{>bXStnk&bk>j4l!9cghq1)V zw6R+W+<+wRsmV8>;SbQJ(8~@wtA)!?IxJk`yqw)Z&pP|oC5DGCY1fjxmrpBY^!fjOmeV<@)kb`?`81|N^6#S0qbnO&;v_Z?)fG`#lEG|{ TW79Qr-_Pxa>wr|wR|WVD&+(&7 literal 0 HcmV?d00001 diff --git a/test/api/fonts/generate-default-item-variations.py b/test/api/fonts/generate-default-item-variations.py new file mode 100644 index 000000000..ecafe92c4 --- /dev/null +++ b/test/api/fonts/generate-default-item-variations.py @@ -0,0 +1,78 @@ +"""Generate constant HVAR/VVAR and GPOS regions at the default instance.""" + +from pathlib import Path +from copy import deepcopy +from fontTools.fontBuilder import FontBuilder +from fontTools.pens.t2CharStringPen import T2CharStringPen +from fontTools.feaLib.builder import addOpenTypeFeaturesFromString +from fontTools.ttLib import newTable +from fontTools.ttLib.tables import otTables as ot +from fontTools.varLib.builder import buildVarRegionList, buildVarData, buildVarStore +from fontTools.varLib.builder import buildVarDevTable, buildDeltaSetIndexMap + +fb = FontBuilder(1000, isTTF=False) +names = [".notdef", "a", "acutecomb"] +fb.setupGlyphOrder(names) +fb.setupCharacterMap({0x61: "a", 0x301: "acutecomb"}) +fb.setupHorizontalMetrics( + {name: (0 if name == "acutecomb" else 500, 0) for name in names} +) +fb.setupHorizontalHeader(ascent=800, descent=-200) +fb.setupVerticalMetrics({name: (1000, 0) for name in names}) +fb.setupVerticalHeader(ascent=800, descent=-200) +fb.setupVerticalOrigins({name: 800 for name in names}) +fb.setupNameTable({"familyName": "Default Item Variations", "styleName": "Regular"}) +fb.setupOS2(sTypoAscender=800, sTypoDescender=-200, usWinAscent=800, usWinDescent=200) +fb.setupPost() +fb.setupFvar([("wght", 0, 0, 1, "Weight"), ("wdth", 0, 0, 1, "Width")], []) +pen = T2CharStringPen(None, None, CFF2=True) +fb.setupCFF2({name: pen.getCharString() for name in names}) +regions = buildVarRegionList( + [{}, {"wght": (0, 1, 1)}, {"wdth": (0, 1, 1)}], ["wght", "wdth"] +) +for tag, delta in [("HVAR", 100), ("VVAR", 200)]: + table = newTable(tag) + table.table = getattr(ot, tag)() + table.table.Version = 0x00010000 + table.table.VarStore = buildVarStore( + deepcopy(regions), [buildVarData([0, 1, 2], [[delta, 80, -40]] * len(names))] + ) + if tag == "HVAR": + table.table.AdvWidthMap = table.table.LsbMap = table.table.RsbMap = None + else: + table.table.AdvHeightMap = table.table.TsbMap = table.table.BsbMap = None + table.table.VOrgMap = buildDeltaSetIndexMap([0] * len(names)) + fb.font[tag] = table +addOpenTypeFeaturesFromString( + fb.font, + "feature kern { pos a <5 7 30 0>; } kern; " + "markClass acutecomb @top; " + "feature mark { pos base a mark @top; } mark;", +) +value = fb.font["GPOS"].table.LookupList.Lookup[0].SubTable[0].Value +value.XPlaDevice = buildVarDevTable(0) +value.YPlaDevice = buildVarDevTable(1) +value.XAdvDevice = buildVarDevTable(2) +fb.font["GPOS"].table.LookupList.Lookup[0].SubTable[0].ValueFormat |= 0x70 +anchor = ( + fb.font["GPOS"] + .table.LookupList.Lookup[1] + .SubTable[0] + .BaseArray.BaseRecord[0] + .BaseAnchor[0] +) +anchor.Format = 3 +anchor.XDeviceTable = buildVarDevTable(0) +anchor.YDeviceTable = buildVarDevTable(1) +gdef = fb.font["GDEF"].table +gdef.Version = 0x00010003 +gdef.GlyphClassDef = gdef.AttachList = gdef.LigCaretList = gdef.MarkAttachClassDef = ( + None +) +gdef.MarkGlyphSetsDef = None +gdef.VarStore = buildVarStore( + regions, [buildVarData([0, 1, 2], [[11, 20, -10], [13, 20, -10], [40, 20, -10]])] +) +fb.font.recalcTimestamp = False +fb.font["head"].created = fb.font["head"].modified = 2082844800 +fb.save(Path(__file__).parent / "default-item-variations.otf") diff --git a/test/api/test-ot-metrics.c b/test/api/test-ot-metrics.c index 2c57d7b82..c0f5c8fdd 100644 --- a/test/api/test-ot-metrics.c +++ b/test/api/test-ot-metrics.c @@ -68,11 +68,55 @@ test_ot_metrics_get_var (void) hb_face_destroy (face); } +static void +test_ot_metrics_constant_regions (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/default-item-variations.otf"); + hb_font_t *font = hb_font_create (face); + for (unsigned i = 0; i < 6; i++) + { + int coords[][2] = {{0, 0}, {0, 0}, {0, 0}, {8192, 0}, {0, 8192}, {0, 0}}; + unsigned lengths[] = {0, 1, 2, 2, 2, 0}; + hb_font_set_var_coords_normalized (font, coords[i], lengths[i]); + int delta = i == 3 ? 40 : i == 4 ? -20 : 0; + g_assert_cmpint (hb_font_get_glyph_h_advance (font, 1), ==, 600 + delta); + g_assert_cmpint (hb_font_get_glyph_v_advance (font, 1), ==, -1200 - delta); + hb_position_t origin_x, origin_y; + g_assert_true (hb_font_get_glyph_v_origin (font, 1, &origin_x, &origin_y)); + g_assert_cmpint (origin_x, ==, 300 + delta / 2); + g_assert_cmpint (origin_y, ==, 1000 + delta); + hb_buffer_t *buffer = hb_buffer_create (); + hb_buffer_add_utf8 (buffer, "a", -1, 0, -1); + hb_buffer_guess_segment_properties (buffer); + hb_shape (font, buffer, NULL, 0); + unsigned count; + hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, &count); + g_assert_cmpuint (count, ==, 1); + int positioning_delta = i == 3 ? 10 : i == 4 ? -5 : 0; + g_assert_cmpint (pos[0].x_advance, ==, 670 + delta + positioning_delta); + g_assert_cmpint (pos[0].x_offset, ==, 16 + positioning_delta); + g_assert_cmpint (pos[0].y_offset, ==, 20 + positioning_delta); + hb_buffer_reset (buffer); + hb_buffer_add_utf8 (buffer, "a\xCC\x81", -1, 0, -1); + hb_buffer_guess_segment_properties (buffer); + hb_shape (font, buffer, NULL, 0); + pos = hb_buffer_get_glyph_positions (buffer, &count); + g_assert_cmpuint (count, ==, 2); + g_assert_cmpint (pos[1].x_advance, ==, 0); + g_assert_cmpint (pos[1].x_offset, ==, -343 - delta + positioning_delta); + g_assert_cmpint (pos[1].y_offset, ==, 733 + 2 * positioning_delta); + hb_buffer_destroy (buffer); + } + hb_font_destroy (font); + hb_face_destroy (face); +} + int main (int argc, char **argv) { hb_test_init (&argc, &argv); hb_test_add (test_ot_metrics_get_no_var); hb_test_add (test_ot_metrics_get_var); + hb_test_add (test_ot_metrics_constant_regions); return hb_test_run (); }