From fac913b1deffd91f4046608cd26a12f1af42583b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 11 Oct 2026 00:41:07 -0600 Subject: [PATCH] [math] Preserve and apply shared GDEF variation indices (#6385) MATH devices were evaluated against an empty variation store, and the subsetter dropped them without folding deltas into the base values. MATH users were also omitted when pruning GDEF's shared store. Evaluate MATH VariationIndex devices with GDEF's store. Collect indices from retained constants, glyph information, and assemblies, then fold pinned deltas and remap residual devices during subsetting. Preserve ordinary ppem device corrections and reject unrepresentable values. Add a reproducible two-axis fixture covering every MathValueRecord location, axis orders, full and partial instances, and glyph pruning. All 255 configured tests pass; reduced-feature syntax checks pass. Assisted-by: Codex --- src/hb-ot-layout-common.hh | 14 +- src/hb-ot-math-table.hh | 128 +++++++++++++++-- src/hb-subset-plan-layout.cc | 13 ++ test/api/fonts/generate-math-variations.py | 122 ++++++++++++++++ test/api/fonts/math-variations.ttf | Bin 0 -> 1352 bytes test/api/meson.build | 1 + test/api/test-subset-math.c | 160 +++++++++++++++++++++ 7 files changed, 418 insertions(+), 20 deletions(-) create mode 100644 test/api/fonts/generate-math-variations.py create mode 100644 test/api/fonts/math-variations.ttf create mode 100644 test/api/test-subset-math.c diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 983b77f28..e7bf3f795 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -386,13 +386,15 @@ struct serialize_math_record_array_t { serialize_math_record_array_t (hb_serialize_context_t *serialize_context_, OutputArray& out_, - const void *base_) : serialize_context (serialize_context_), - out (out_), base (base_) {} + const void *base_, + const hb_hashmap_t> *variation_map_) : + serialize_context (serialize_context_), + out (out_), base (base_), variation_map (variation_map_) {} template bool operator () (T&& record) { - if (!serialize_context->copy (record, base)) return false; + if (!serialize_context->copy (record, base, variation_map)) return false; out.len++; return true; } @@ -401,6 +403,7 @@ struct serialize_math_record_array_t hb_serialize_context_t *serialize_context; OutputArray &out; const void *base; + const hb_hashmap_t> *variation_map; }; /* @@ -411,8 +414,9 @@ struct template serialize_math_record_array_t operator () (hb_serialize_context_t *serialize_context, OutputArray& out, - const void *base) const - { return serialize_math_record_array_t (serialize_context, out, base); } + const void *base, + const hb_hashmap_t> *variation_map) const + { return serialize_math_record_array_t (serialize_context, out, base, variation_map); } } HB_FUNCOBJ (serialize_math_record_array); diff --git a/src/hb-ot-math-table.hh b/src/hb-ot-math-table.hh index ccade5649..fc6349a59 100644 --- a/src/hb-ot-math-table.hh +++ b/src/hb-ot-math-table.hh @@ -29,6 +29,7 @@ #include "hb-open-type.hh" #include "hb-ot-layout-common.hh" +#include "hb-ot-layout-gdef-table.hh" #include "hb-ot-math.h" #include "hb-depend-data.hh" @@ -38,20 +39,38 @@ namespace OT { struct MathValueRecord { hb_position_t get_x_value (hb_font_t *font, const void *base) const - { return hb_saturate_add (font->em_scale_x (value), (base+deviceTable).get_x_delta (font)); } + { return hb_saturate_add (font->em_scale_x (value), (base+deviceTable).get_x_delta (font, get_var_store (font, base))); } hb_position_t get_y_value (hb_font_t *font, const void *base) const - { return hb_saturate_add (font->em_scale_y (value), (base+deviceTable).get_y_delta (font)); } + { return hb_saturate_add (font->em_scale_y (value), (base+deviceTable).get_y_delta (font, get_var_store (font, base))); } - MathValueRecord* copy (hb_serialize_context_t *c, const void *base) const + const ItemVariationStore &get_var_store (hb_font_t *font, const void *base) const + { +#if !defined(HB_NO_VAR) && !defined(HB_NO_OT_LAYOUT) + if ((base+deviceTable).is_variation_device ()) + return font->face->table.GDEF->table->get_var_store (); +#endif + return Null (ItemVariationStore); + } + + MathValueRecord* copy (hb_serialize_context_t *c, const void *base, + const hb_hashmap_t> *variation_map) const { TRACE_SERIALIZE (this); auto *out = c->embed (this); if (unlikely (!out)) return_trace (nullptr); - out->deviceTable.serialize_copy (c, deviceTable, base, 0, hb_serialize_context_t::Head); + hb_pair_t *varidx_delta; + if (variation_map->has ((base+deviceTable).get_variation_index (), &varidx_delta) && + !c->check_assign (out->value, (int64_t) value + hb_second (*varidx_delta), + HB_SERIALIZE_ERROR_INT_OVERFLOW)) + return_trace (nullptr); + out->deviceTable.serialize_copy (c, deviceTable, base, 0, hb_serialize_context_t::Head, variation_map); return_trace (out); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c, const void *base) const + { (base+deviceTable).collect_variation_indices (c); } + bool sanitize (hb_sanitize_context_t *c, const void *base) const { TRACE_SANITIZE (this); @@ -72,7 +91,8 @@ struct MathConstants { friend struct MATH; - MathConstants* copy (hb_serialize_context_t *c) const + MathConstants* copy (hb_serialize_context_t *c, + const hb_hashmap_t> *variation_map) const { TRACE_SERIALIZE (this); auto *out = c->start_embed (this); @@ -87,13 +107,19 @@ struct MathConstants unsigned count = ARRAY_LENGTH (mathValueRecords); for (unsigned i = 0; i < count; i++) - if (!c->copy (mathValueRecords[i], this)) + if (!c->copy (mathValueRecords[i], this, variation_map)) return_trace (nullptr); if (!c->embed (radicalDegreeBottomRaisePercent)) return_trace (nullptr); return_trace (out); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c) const + { + for (const auto &record : mathValueRecords) + record.collect_variation_indices (c, this); + } + bool sanitize_math_value_records (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -212,7 +238,7 @@ struct MathItalicsCorrectionInfo hb_sorted_vector_t new_coverage; + hb_zip (this+coverage, italicsCorrection) | hb_filter (glyphset, hb_first) - | hb_filter (serialize_math_record_array (c->serializer, out->italicsCorrection, this), hb_second) + | hb_filter (serialize_math_record_array (c->serializer, out->italicsCorrection, this, &c->plan->layout_variation_idx_delta_map), hb_second) | hb_map (hb_first) | hb_map (glyph_map) | hb_sink (new_coverage) @@ -222,6 +248,13 @@ struct MathItalicsCorrectionInfo return_trace (true); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c) const + { + for (const auto &entry : hb_zip (this+coverage, italicsCorrection)) + if (c->glyph_set->has (hb_first (entry))) + hb_second (entry).collect_variation_indices (c, this); + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -265,7 +298,7 @@ struct MathTopAccentAttachment hb_sorted_vector_t new_coverage; + hb_zip (this+topAccentCoverage, topAccentAttachment) | hb_filter (glyphset, hb_first) - | hb_filter (serialize_math_record_array (c->serializer, out->topAccentAttachment, this), hb_second) + | hb_filter (serialize_math_record_array (c->serializer, out->topAccentAttachment, this, &c->plan->layout_variation_idx_delta_map), hb_second) | hb_map (hb_first) | hb_map (glyph_map) | hb_sink (new_coverage) @@ -275,6 +308,13 @@ struct MathTopAccentAttachment return_trace (true); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c) const + { + for (const auto &entry : hb_zip (this+topAccentCoverage, topAccentAttachment)) + if (c->glyph_set->has (hb_first (entry))) + hb_second (entry).collect_variation_indices (c, this); + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -308,7 +348,8 @@ struct MathTopAccentAttachment struct MathKern { - MathKern* copy (hb_serialize_context_t *c) const + MathKern* copy (hb_serialize_context_t *c, + const hb_hashmap_t> *variation_map) const { TRACE_SERIALIZE (this); auto *out = c->start_embed (this); @@ -317,12 +358,18 @@ struct MathKern unsigned count = 2 * heightCount + 1; for (unsigned i = 0; i < count; i++) - if (!c->copy (mathValueRecordsZ.arrayZ[i], this)) + if (!c->copy (mathValueRecordsZ.arrayZ[i], this, variation_map)) return_trace (nullptr); return_trace (out); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c) const + { + for (const auto &record : mathValueRecordsZ.as_array (2 * heightCount + 1)) + record.collect_variation_indices (c, this); + } + bool sanitize_math_value_records (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -417,7 +464,8 @@ struct MathKern struct MathKernInfoRecord { - MathKernInfoRecord* copy (hb_serialize_context_t *c, const void *base) const + MathKernInfoRecord* copy (hb_serialize_context_t *c, const void *base, + const hb_hashmap_t> *variation_map) const { TRACE_SERIALIZE (this); auto *out = c->embed (this); @@ -425,11 +473,17 @@ struct MathKernInfoRecord unsigned count = ARRAY_LENGTH (mathKern); for (unsigned i = 0; i < count; i++) - out->mathKern[i].serialize_copy (c, mathKern[i], base, 0, hb_serialize_context_t::Head); + out->mathKern[i].serialize_copy (c, mathKern[i], base, 0, hb_serialize_context_t::Head, variation_map); return_trace (out); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c, const void *base) const + { + for (const auto &offset : mathKern) + (base+offset).collect_variation_indices (c); + } + bool sanitize (hb_sanitize_context_t *c, const void *base) const { TRACE_SANITIZE (this); @@ -493,7 +547,7 @@ struct MathKernInfo hb_sorted_vector_t new_coverage; + hb_zip (this+mathKernCoverage, mathKernInfoRecords) | hb_filter (glyphset, hb_first) - | hb_filter (serialize_math_record_array (c->serializer, out->mathKernInfoRecords, this), hb_second) + | hb_filter (serialize_math_record_array (c->serializer, out->mathKernInfoRecords, this, &c->plan->layout_variation_idx_delta_map), hb_second) | hb_map (hb_first) | hb_map (glyph_map) | hb_sink (new_coverage) @@ -503,6 +557,13 @@ struct MathKernInfo return_trace (true); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c) const + { + for (const auto &entry : hb_zip (this+mathKernCoverage, mathKernInfoRecords)) + if (c->glyph_set->has (hb_first (entry))) + hb_second (entry).collect_variation_indices (c, this); + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -582,6 +643,13 @@ struct MathGlyphInfo return_trace (true); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c) const + { + (this+mathItalicsCorrectionInfo).collect_variation_indices (c); + (this+mathTopAccentAttachment).collect_variation_indices (c); + (this+mathKernInfo).collect_variation_indices (c); + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -763,7 +831,7 @@ struct MathGlyphAssembly { TRACE_SUBSET (this); - if (!c->serializer->copy (italicsCorrection, this)) return_trace (false); + if (!c->serializer->copy (italicsCorrection, this, &c->plan->layout_variation_idx_delta_map)) return_trace (false); if (!c->serializer->copy (partRecords.len)) return_trace (false); for (const auto& record : partRecords.iter ()) @@ -771,6 +839,11 @@ struct MathGlyphAssembly return_trace (true); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c) const + { + italicsCorrection.collect_variation_indices (c, this); + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -845,6 +918,11 @@ struct MathGlyphConstruction return_trace (true); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c) const + { + (this+glyphAssembly).collect_variation_indices (c); + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -1017,6 +1095,16 @@ struct MathVariants return_trace (true); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c) const + { + for (const auto &entry : hb_zip (this+vertGlyphCoverage, glyphConstruction.as_array (vertGlyphCount))) + if (c->glyph_set->has (hb_first (entry))) + (this+hb_second (entry)).collect_variation_indices (c); + for (const auto &entry : hb_zip (this+horizGlyphCoverage, glyphConstruction.as_array (vertGlyphCount + horizGlyphCount).sub_array (vertGlyphCount))) + if (c->glyph_set->has (hb_first (entry))) + (this+hb_second (entry)).collect_variation_indices (c); + } + bool sanitize_offsets (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); @@ -1147,12 +1235,22 @@ struct MATH auto *out = c->serializer->embed (*this); if (unlikely (!out)) return_trace (false); - out->mathConstants.serialize_copy (c->serializer, mathConstants, this, 0, hb_serialize_context_t::Head); + out->mathConstants.serialize_copy (c->serializer, mathConstants, this, 0, hb_serialize_context_t::Head, &c->plan->layout_variation_idx_delta_map); out->mathGlyphInfo.serialize_subset (c, mathGlyphInfo, this); out->mathVariants.serialize_subset (c, mathVariants, this); return_trace (true); } + void collect_variation_indices (hb_collect_variation_indices_context_t *c, const hb_set_t *variant_glyph_set) const + { + (this+mathConstants).collect_variation_indices (c); + (this+mathGlyphInfo).collect_variation_indices (c); + auto *glyph_set = c->glyph_set; + c->glyph_set = variant_glyph_set; + (this+mathVariants).collect_variation_indices (c); + c->glyph_set = glyph_set; + } + bool sanitize (hb_sanitize_context_t *c) const { TRACE_SANITIZE (this); diff --git a/src/hb-subset-plan-layout.cc b/src/hb-subset-plan-layout.cc index 27409168e..2dbcf32ff 100644 --- a/src/hb-subset-plan-layout.cc +++ b/src/hb-subset-plan-layout.cc @@ -29,6 +29,7 @@ #include "hb-ot-layout-gdef-table.hh" #include "hb-ot-layout-gpos-table.hh" #include "hb-ot-layout-gsub-table.hh" +#include "hb-ot-math-table.hh" using OT::Layout::GSUB; using OT::Layout::GPOS; @@ -561,6 +562,18 @@ collect_layout_variation_indices (hb_subset_plan_t* plan) gpos->collect_variation_indices (&c); } +#ifndef HB_NO_MATH + if (!plan->drop_tables.has (HB_OT_TAG_MATH)) + { + hb_blob_ptr_t math = plan->source_table (); + auto *glyph_set = c.glyph_set; + c.glyph_set = &plan->_glyphset_mathed; + math->collect_variation_indices (&c, &plan->_glyphset_cmaped); + c.glyph_set = glyph_set; + math.destroy (); + } +#endif + remap_variation_indices (gdef->get_var_store (), varidx_set, plan->normalized_coords, !plan->pinned_at_default, diff --git a/test/api/fonts/generate-math-variations.py b/test/api/fonts/generate-math-variations.py new file mode 100644 index 000000000..a92b85491 --- /dev/null +++ b/test/api/fonts/generate-math-variations.py @@ -0,0 +1,122 @@ +"""Generate MATH VariationIndex records sharing a two-axis GDEF store.""" + +from pathlib import Path +from io import BytesIO + +from fontTools.fontBuilder import FontBuilder +from fontTools.otlLib.builder import buildMathTable, buildDevice +from fontTools.pens.ttGlyphPen import TTGlyphPen +from fontTools.ttLib import TTFont, newTable +from fontTools.ttLib.tables import otTables as ot +from fontTools.varLib.builder import ( + buildVarDevTable, + buildVarData, + buildVarRegionList, + buildVarStore, +) + +BASES = { + "leading": (100, 10, 20), + "axis": (200, -20, 30), + "italic_a": (10, 30, -10), + "italic_b": (20, 40, -20), + "accent_a": (300, 50, 5), + "accent_b": (400, 60, -5), + "height": (50, 70, 7), + "kern_0": (70, 80, 8), + "kern_1": (80, 90, 9), + "assembly": (40, 100, 10), +} + + +def values(font): + math = font["MATH"].table + result = { + "leading": math.MathConstants.MathLeading, + "axis": math.MathConstants.AxisHeight, + } + glyphs = math.MathGlyphInfo + info = glyphs.MathItalicsCorrectionInfo + result.update( + ("italic_" + name, value) + for name, value in zip(info.Coverage.glyphs, info.ItalicsCorrection) + ) + info = glyphs.MathTopAccentAttachment + result.update( + ("accent_" + name, value) + for name, value in zip(info.TopAccentCoverage.glyphs, info.TopAccentAttachment) + ) + kern = glyphs.MathKernInfo.MathKernInfoRecords[0].TopRightMathKern + result["height"] = kern.CorrectionHeight[0] + result.update(("kern_" + str(i), value) for i, value in enumerate(kern.KernValue)) + result["assembly"] = math.MathVariants.VertGlyphConstruction[ + 0 + ].GlyphAssembly.ItalicsCorrection + return result + + +def roundtrip(font): + stream = BytesIO() + font.save(stream) + stream.seek(0) + return TTFont(stream) + + +def font(): + fb = FontBuilder(1000, isTTF=True) + names = [".notdef", "a", "b"] + fb.setupGlyphOrder(names) + fb.setupCharacterMap({0x61: "a", 0x62: "b"}) + pen = TTGlyphPen(None) + pen.moveTo((0, 0)) + pen.lineTo((500, 0)) + pen.lineTo((500, 500)) + pen.closePath() + fb.setupGlyf({name: pen.glyph() for name in names}) + fb.setupHorizontalMetrics({name: (600, 0) for name in names}) + fb.setupHorizontalHeader(ascent=800, descent=-200) + fb.setupNameTable({"familyName": "Math 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")], []) + font = fb.font + buildMathTable( + font, + constants={ + "MathLeading": 100, + "AxisHeight": 200, + "AccentBaseHeight": 500, + "FlattenedAccentBaseHeight": 600, + }, + italicsCorrections={"a": 10, "b": 20}, + topAccentAttachments={"a": 300, "b": 400}, + mathKerns={"a": {"TopRight": ([50], [70, 80])}}, + vertGlyphAssembly={"a": ([("a", 0, 0, 0, 500)], 40)}, + ) + rows = [[999, -999], [0, 0]] + for name, record in values(font).items(): + record.DeviceTable = buildVarDevTable(len(rows)) + rows.append(list(BASES[name][1:])) + constants = font["MATH"].table.MathConstants + constants.AccentBaseHeight.DeviceTable = buildVarDevTable(ot.NO_VARIATION_INDEX) + constants.FlattenedAccentBaseHeight.DeviceTable = buildDevice({10: 1}) + font["GDEF"] = newTable("GDEF") + gdef = font["GDEF"].table = ot.GDEF() + gdef.Version = 0x00010003 + gdef.populateDefaults() + gdef.VarStore = buildVarStore( + buildVarRegionList( + [{"wght": (0, 1, 1)}, {"wdth": (0, 1, 1)}], ["wght", "wdth"] + ), + [buildVarData([0, 1], rows)], + ) + return roundtrip(font) + + + +font = font() +font["head"].created = font["head"].modified = 2082844800 +font.recalcTimestamp = False +font.save(Path(__file__).with_name("math-variations.ttf")) diff --git a/test/api/fonts/math-variations.ttf b/test/api/fonts/math-variations.ttf new file mode 100644 index 0000000000000000000000000000000000000000..3f232b69a79bb394cdfb203e768bccbb3b069130 GIT binary patch literal 1352 zcmdT^O=}ZT6g_VyX_857V;PDqQl?Nbf*=-=EDCL*t-2^JRh!n0eQiEwAZbE6{SdmC zm22@A2>t;#f=j_oUFb?&6)7UR6{qWLG12B_h{h5h7H+SDyv&1`qcszUi%E;8j)nT|X|KqyLDw&n?N~(%q-;Xz%g8?vjZ2i9`Br;$gSuZyPEz#0#oq zwZ1BmMj!Ek!Zo?wK%5Ump4eHFwbG~TtG9rm*1vAl*L~c=6!AWB4(e=tGl~Aq`#q!I zVVYmaVCTZKpz~K8==>%O#ZtJ&bVBZ-t0~f}GIfg3jLEJ=p5v7#F{ZI79}xtg`t;0k zb?9uX5sXfv+=>!-azdT1|Kwd#Ga#>zGFg6>OlYeFY+q z*yV}>t)gDZ%KyF@k{zNtH_4e1tgTae_1Q97=a_|g#`&BU&1C3Py9o31QNQ+Eu*0wH z!(e!b9IX-dXEgkUX(TYm-rhrs1zI|{g`c9_&RWEK-u50R?jvVUPix!vKN?1$sx|{~ z`a;^4I`z1kFNgd%hS@oblVPKmI2KSLPR*W*%dq(c4X{gwemQa4pPfZ%O2TH5BEO#`Rk9BNu8WpWY`53UJ KhVxh=PW%QCxR6@_ literal 0 HcmV?d00001 diff --git a/test/api/meson.build b/test/api/meson.build index 5e586fd56..f47f1d262 100644 --- a/test/api/meson.build +++ b/test/api/meson.build @@ -68,6 +68,7 @@ tests = [ 'test-subset-varc.c', 'test-subset-sbix.c', 'test-subset-gpos.c', + 'test-subset-math.c', 'test-subset-colr.c', 'test-subset-cbdt.c', 'test-unicode.c', diff --git a/test/api/test-subset-math.c b/test/api/test-subset-math.c new file mode 100644 index 000000000..7f43c2f78 --- /dev/null +++ b/test/api/test-subset-math.c @@ -0,0 +1,160 @@ +/* + * Copyright © 2018 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Garret Rieger + */ + +#include "hb-test.h" +#include "hb-subset-test.h" +#include "hb-ot.h" +#include + +/* MATH VariationIndex devices share GDEF's item variation store. */ + +static hb_face_t * +subset_math (hb_face_t *face, const hb_tag_t *tags, const float *values, + unsigned count, hb_bool_t only_a) +{ + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_t *glyphs = hb_subset_input_glyph_set (input); + hb_set_add_range (glyphs, 0, only_a ? 1 : 2); + for (unsigned i = 0; i < count; i++) + g_assert_true (hb_subset_input_pin_axis_location (input, face, tags[i], values[i])); + hb_face_t *subset = hb_subset_or_fail (face, input); + g_assert_nonnull (subset); + hb_subset_input_destroy (input); + return subset; +} + +static hb_position_t +expected_value (int base, int dw, int dd, float weight, float width) +{ + return base + (int) floor (weight * dw + .5f) + (int) floor (width * dd + .5f); +} + +static void +check_math (hb_face_t *face, float weight, float width, hb_bool_t only_a) +{ + hb_font_t *font = hb_font_create (face); + hb_ot_font_set_funcs (font); + hb_font_set_scale (font, 1000, 1000); + hb_variation_t variations[] = { + {HB_TAG ('w', 'g', 'h', 't'), weight}, + {HB_TAG ('w', 'd', 't', 'h'), width} + }; + hb_font_set_variations (font, variations, 2); +#define CHECK_VALUE(actual, base, dw, dd) \ + g_assert_cmpint (actual, ==, expected_value (base, dw, dd, weight, width)) + CHECK_VALUE (hb_ot_math_get_constant (font, HB_OT_MATH_CONSTANT_MATH_LEADING), 100, 10, 20); + CHECK_VALUE (hb_ot_math_get_constant (font, HB_OT_MATH_CONSTANT_AXIS_HEIGHT), 200, -20, 30); + CHECK_VALUE (hb_ot_math_get_glyph_italics_correction (font, 1), 10, 30, -10); + CHECK_VALUE (hb_ot_math_get_glyph_top_accent_attachment (font, 1), 300, 50, 5); + if (!only_a) + { + CHECK_VALUE (hb_ot_math_get_glyph_italics_correction (font, 2), 20, 40, -20); + CHECK_VALUE (hb_ot_math_get_glyph_top_accent_attachment (font, 2), 400, 60, -5); + } + hb_ot_math_kern_entry_t entries[2]; + unsigned count = 2; + g_assert_cmpuint (hb_ot_math_get_glyph_kernings (font, 1, HB_OT_MATH_KERN_TOP_RIGHT, + 0, &count, entries), ==, 2); + g_assert_cmpuint (count, ==, 2); + CHECK_VALUE (entries[0].max_correction_height, 50, 70, 7); + CHECK_VALUE (entries[0].kern_value, 70, 80, 8); + CHECK_VALUE (entries[1].kern_value, 80, 90, 9); + hb_position_t italic; + count = 0; + g_assert_cmpuint (hb_ot_math_get_glyph_assembly (font, 1, HB_DIRECTION_BTT, + 0, &count, NULL, &italic), ==, 1); + CHECK_VALUE (italic, 40, 100, 10); +#undef CHECK_VALUE + g_assert_cmpint (hb_ot_math_get_constant (font, HB_OT_MATH_CONSTANT_ACCENT_BASE_HEIGHT), ==, 500); + hb_font_set_ppem (font, 10, 10); + g_assert_cmpint (hb_ot_math_get_constant (font, HB_OT_MATH_CONSTANT_FLATTENED_ACCENT_BASE_HEIGHT), ==, 700); + hb_font_destroy (font); +} + +static void +check_full (hb_face_t *face, float weight, float width, hb_bool_t only_a) +{ + check_math (face, weight, width, only_a); + hb_blob_t *blob = hb_face_reference_table (face, HB_TAG ('f', 'v', 'a', 'r')); + g_assert_cmpuint (hb_blob_get_length (blob), ==, 0); + hb_blob_destroy (blob); + blob = hb_face_reference_table (face, HB_TAG ('G', 'D', 'E', 'F')); + g_assert_cmpuint (hb_blob_get_length (blob), ==, 0); + hb_blob_destroy (blob); +} + +static void +test_subset_math_variations (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/math-variations.ttf"); + const hb_tag_t tags[] = {HB_TAG ('w', 'g', 'h', 't'), HB_TAG ('w', 'd', 't', 'h')}; + const float locations[] = {0.f, .5f, 1.f}; + for (unsigned only_a = 0; only_a < 2; only_a++) + { + hb_face_t *subset = subset_math (face, NULL, NULL, 0, only_a); + for (unsigned i = 0; i < 3; i++) + { + hb_font_t *font = hb_font_create (subset); + hb_ot_font_set_funcs (font); + hb_variation_t vars[] = {{tags[0], locations[i]}, {tags[1], 0.f}}; + hb_font_set_variations (font, vars, 2); + g_assert_cmpint (hb_ot_math_get_constant (font, HB_OT_MATH_CONSTANT_MATH_LEADING), + ==, 100 + (int) (10 * locations[i])); + hb_font_destroy (font); + hb_face_t *partial = subset_math (face, tags, &locations[i], 1, only_a); + for (unsigned j = 0; j < 3; j++) + { + const float values[] = {locations[i], locations[j]}; + check_math (partial, values[0], values[1], only_a); + check_math (face, values[0], values[1], false); + hb_face_t *full = subset_math (face, tags, values, 2, only_a); + check_full (full, values[0], values[1], only_a); + hb_face_t *composed = subset_math (partial, tags + 1, values + 1, 1, only_a); + check_full (composed, values[0], values[1], only_a); + hb_subset_test_check (full, composed, HB_TAG ('M', 'A', 'T', 'H')); + hb_face_destroy (composed); + hb_face_t *width_only = subset_math (face, tags + 1, values + 1, 1, only_a); + composed = subset_math (width_only, tags, values, 1, only_a); + check_full (composed, values[0], values[1], only_a); + hb_subset_test_check (full, composed, HB_TAG ('M', 'A', 'T', 'H')); + hb_face_destroy (composed); + hb_face_destroy (width_only); + hb_face_destroy (full); + } + hb_face_destroy (partial); + } + hb_face_destroy (subset); + } + hb_face_destroy (face); +} + +int +main (int argc, char **argv) +{ + hb_test_init (&argc, &argv); + hb_test_add (test_subset_math_variations); + return hb_test_run (); +}