From c042c008396d720e36ab476ef7a558525982ede5 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 2 Oct 2026 17:17:10 -0600 Subject: [PATCH] [VARC] Preserve precision when accumulating 32-bit deltas (#6314) Packed VARC deltas can use the full signed 32-bit range. Converting them to float before scaling and accumulation loses small residuals when large contributions cancel, affecting conditions, transforms, and component axis values. Accumulate MultiItemVariationStore deltas in double and keep condition and axis values at that precision until comparison or rounding. Leave region scalars, drawing transforms, and ordinary variation stores unchanged. Add a generated regression font covering all three consumers. Tests: meson test -C build (281 passed, 1 skipped); C++11 syntax checks with default, HB_NO_VAR, and HB_OPTIMIZE_SIZE configurations. Assisted-by: OpenAI Codex --- src/OT/Var/VARC/VARC.cc | 10 +- src/OT/Var/VARC/VARC.hh | 4 +- src/hb-open-type.hh | 11 +- src/hb-ot-layout-common.hh | 20 ++-- .../fonts/generate-varc-delta-precision.py | 100 ++++++++++++++++++ test/api/fonts/varc-delta-precision.ttf | Bin 0 -> 1124 bytes test/api/test-draw-varc.c | 21 ++++ 7 files changed, 144 insertions(+), 22 deletions(-) create mode 100644 test/api/fonts/generate-varc-delta-precision.py create mode 100644 test/api/fonts/varc-delta-precision.ttf diff --git a/src/OT/Var/VARC/VARC.cc b/src/OT/Var/VARC/VARC.cc index 722356639..b2b8c4321 100644 --- a/src/OT/Var/VARC/VARC.cc +++ b/src/OT/Var/VARC/VARC.cc @@ -32,7 +32,7 @@ bool VarComponent::decompile_record (const VARC &varc, hb_ubytes_t total_record, hb_vector_t *axis_indices, - hb_vector_t *axis_values, + hb_vector_t *axis_values, record_t *decoded, int64_t *budget) { @@ -403,18 +403,18 @@ VarComponent::get_path_at (const hb_varc_context_t &c, return hb_ubytes_t (); coord_setter_t coord_setter (axisIndices ? component_coords : hb_array ()); for (unsigned i = 0; i < axisIndices.length; i++) - coord_setter[axisIndices[i]] = hb_clamp_to (roundf (axisValues[i])); + coord_setter[axisIndices[i]] = hb_clamp_to (round (axisValues[i])); if (axisIndices) component_coords = coord_setter.get_coords (); // Apply transform variations if any if (transformVarIdx != VarIdx::NO_VARIATION && coords) { - float transformValues[9]; + double transformValues[9]; unsigned numTransformValues = 0; #define PROCESS_TRANSFORM_COMPONENT(shift, type, flag, name) \ if (flags & (unsigned) flags_t::flag) \ - transformValues[numTransformValues++] = transform.name; + transformValues[numTransformValues++] = (double) transform.name; VARC_PROCESS_TRANSFORM_COMPONENTS; #undef PROCESS_TRANSFORM_COMPONENT if (unlikely (!hb_budget_spend (c.budget, numTransformValues, @@ -424,7 +424,7 @@ VarComponent::get_path_at (const hb_varc_context_t &c, numTransformValues = 0; #define PROCESS_TRANSFORM_COMPONENT(shift, type, flag, name) \ if (flags & (unsigned) flags_t::flag) \ - transform.name = transformValues[numTransformValues++]; + transform.name = (float) transformValues[numTransformValues++]; VARC_PROCESS_TRANSFORM_COMPONENTS; #undef PROCESS_TRANSFORM_COMPONENT } diff --git a/src/OT/Var/VARC/VARC.hh b/src/OT/Var/VARC/VARC.hh index 20866ddaf..42b905c0c 100644 --- a/src/OT/Var/VARC/VARC.hh +++ b/src/OT/Var/VARC/VARC.hh @@ -26,7 +26,7 @@ namespace OT { struct hb_varc_scratch_t { hb_vector_t axisIndices; - hb_vector_t axisValues; + hb_vector_t axisValues; hb_glyf_scratch_t glyf_scratch; }; @@ -101,7 +101,7 @@ struct VarComponent HB_INTERNAL static bool decompile_record (const VARC &varc, hb_ubytes_t record, hb_vector_t *axis_indices, - hb_vector_t *axis_values, + hb_vector_t *axis_values, record_t *decoded /* OUT */, int64_t *budget = nullptr); }; diff --git a/src/hb-open-type.hh b/src/hb-open-type.hh index 1115e41db..b6dc5e1fa 100644 --- a/src/hb-open-type.hh +++ b/src/hb-open-type.hh @@ -2008,11 +2008,11 @@ struct TupleValues } private: - template - void _add_to (hb_array_t out, float scale = 1.0f) + template + void _add_to (hb_array_t out, T scale = T (1)) { unsigned n = out.length; - float *arrayZ = out.arrayZ; + T *arrayZ = out.arrayZ; for (unsigned i = 0; i < n;) { @@ -2080,7 +2080,8 @@ struct TupleValues } public: - void add_to (hb_array_t out, float scale = 1.0f) + template + void add_to (hb_array_t out, float scale = 1.0f) { #ifndef HB_OPTIMIZE_SIZE // The following branch is supposed to speed things up by avoiding @@ -2090,7 +2091,7 @@ struct TupleValues _add_to (out); else #endif - _add_to (out, scale); + _add_to (out, (T) scale); } }; }; diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index 7a725e39e..874080d49 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -3404,7 +3404,7 @@ struct MultiVarData void get_delta (unsigned int inner, const int *coords, unsigned int coord_count, const SparseVarRegionList ®ions, - hb_array_t out, + hb_array_t out, hb_scalar_cache_t *cache = nullptr) const { auto &deltaSets = get_delta_sets (); @@ -3827,7 +3827,7 @@ struct MultiItemVariationStore private: void get_delta (unsigned int outer, unsigned int inner, const int *coords, unsigned int coord_count, - hb_array_t out, + hb_array_t out, hb_scalar_cache_t *cache = nullptr) const { #ifdef HB_NO_VAR @@ -3847,7 +3847,7 @@ struct MultiItemVariationStore public: void get_delta (unsigned int index, const int *coords, unsigned int coord_count, - hb_array_t out, + hb_array_t out, hb_scalar_cache_t *cache = nullptr) const { unsigned int outer = index >> 16; @@ -3856,7 +3856,7 @@ struct MultiItemVariationStore } void get_delta (unsigned int index, hb_array_t coords, - hb_array_t out, + hb_array_t out, hb_scalar_cache_t *cache = nullptr) const { return get_delta (index, @@ -4203,14 +4203,14 @@ struct MultiItemVarStoreInstancer operator bool () const { return varStore && bool (coords); } - float operator[] (uint32_t varIdx) const + double operator[] (uint32_t varIdx) const { - float v = 0; + double v = 0; (*this) (hb_array (&v, 1), varIdx); return v; } - void operator() (hb_array_t out, uint32_t varIdx, unsigned short offset = 0) const + void operator() (hb_array_t out, uint32_t varIdx, unsigned short offset = 0) const { if (coords && varIdx != VarIdx::NO_VARIATION) { @@ -4221,7 +4221,7 @@ struct MultiItemVarStoreInstancer } else for (unsigned i = 0; i < out.length; i++) - out.arrayZ[i] = 0.f; + out.arrayZ[i] = 0.; } const MultiItemVariationStore *varStore; @@ -4407,8 +4407,8 @@ struct ConditionValue bool evaluate (const int *coords, unsigned int coord_len, Instancer *instancer) const { - float value = defaultValue; - value += (*instancer)[varIdx]; + double value = defaultValue; + value += (double) (*instancer)[varIdx]; return value > 0; } diff --git a/test/api/fonts/generate-varc-delta-precision.py b/test/api/fonts/generate-varc-delta-precision.py new file mode 100644 index 000000000..d67a9c66f --- /dev/null +++ b/test/api/fonts/generate-varc-delta-precision.py @@ -0,0 +1,100 @@ +"""Generate the VARC delta-precision regression font using current FontTools.""" + +import sys + +from fontTools.fontBuilder import FontBuilder +from fontTools.pens.ttGlyphPen import TTGlyphPen +from fontTools.ttLib import newTable +from fontTools.ttLib.tables import otTables as ot +from fontTools.ttLib.tables.TupleVariation import TupleVariation +from fontTools.varLib.builder import ( + buildMultiVarData, + buildMultiVarStore, + buildSparseVarRegionList, +) + + +def build(): + order = [".notdef", "leaf", "conditional", "translated", "axes"] + fb = FontBuilder(1000) + fb.setupGlyphOrder(order) + fb.setupCharacterMap({65: "conditional", 66: "translated", 67: "axes"}) + glyphs = {name: TTGlyphPen(None).glyph() for name in order} + pen = TTGlyphPen(None) + pen.moveTo((100, 0)) + pen.lineTo((200, 0)) + pen.lineTo((100, 100)) + pen.closePath() + glyphs["leaf"] = pen.glyph() + fb.setupGlyf(glyphs) + fb.setupHorizontalMetrics( + {name: (500, 100 if name == "leaf" else 0) for name in order} + ) + fb.setupHorizontalHeader(ascent=800, descent=-200) + fb.setupNameTable({"familyName": "VARC delta precision", "styleName": "Regular"}) + fb.setupOS2() + fb.setupPost() + fb.setupFvar([("TEST", 0, 0, 1, "Test"), ("LEAF", 0, 0, 1, "Leaf")], []) + fb.setupGvar( + { + "leaf": [ + TupleVariation( + {"LEAF": (0, 1, 1)}, [(16384, 0)] * 3 + [(0, 0)] * 4 + ) + ] + } + ) + + varc = ot.VARC() + varc.Version = 0x10000 + varc.Coverage = ot.Coverage() + varc.Coverage.glyphs = order[2:] + data = buildMultiVarData([0, 1], []) + data.Item = [ + [16777217, -16777216], # condition: +1 at TEST=1 + [2147483647, 0, 0, -2147483583, 0, 0], # translation: +64 + [16777217, -16777216], # axis override: +1 in F2DOT14 units + ] + varc.MultiVarStore = buildMultiVarStore( + buildSparseVarRegionList([{"TEST": (0, 1, 1)}] * 2, ["TEST", "LEAF"]), + [data], + ) + condition = ot.ConditionTable() + condition.Format, condition.DefaultValue, condition.VarIdx = 2, 0, 0 + varc.ConditionList = ot.ConditionList() + varc.ConditionList.ConditionTable = [condition] + varc.AxisIndicesList = ot.AxisIndicesList() + varc.AxisIndicesList.Item = [(1,)] + varc.VarCompositeGlyphs = ot.VarCompositeGlyphs() + varc.VarCompositeGlyphs.VarCompositeGlyph = [] + for name in order[2:]: + component = ot.VarComponent() + component.glyphName = "leaf" + if name == "axes": + component.axisIndicesIndex = 0 + component.axisValues = (0.0,) + component.axisValuesVarIndex = 2 + else: + component.flags = int( + ot.VarComponentFlags.HAVE_TRANSLATE_X + | ot.VarComponentFlags.HAVE_SCALE_X + | ot.VarComponentFlags.HAVE_SCALE_Y + ) + component.transform.translateX = 400 + component.transform.scaleX = component.transform.scaleY = 2 + if name == "conditional": + component.conditionIndex = 0 + else: + component.transformVarIndex = 1 + glyph = ot.VarCompositeGlyph() + glyph.components = [component] + varc.VarCompositeGlyphs.VarCompositeGlyph.append(glyph) + fb.font["VARC"] = newTable("VARC") + fb.font["VARC"].table = varc + return fb.font + + +if __name__ == "__main__": + font = build() + for path in sys.argv[1:]: + font.save(path) diff --git a/test/api/fonts/varc-delta-precision.ttf b/test/api/fonts/varc-delta-precision.ttf new file mode 100644 index 0000000000000000000000000000000000000000..54b34c316e69c954bdf06b9bfaf5eac38ab733c5 GIT binary patch literal 1124 zcmah|OKTHR6#mZ4w5Dx*fFTxqbWpJ_3W^oUqF`dE8!Jj1in`HjGD!oI2brlvU6@jE zp__tG3NCb|`>qN_XhET_`~$kINO2=na3k@%H`7=_@m$WG^PSf{=bk$d0DIAbjqBH@ z&Ya7nG7kZ{LG7LC%w%>s`Dzug$B0+*6;-G2BJFR9&lm5h#>*FrZ)o2pPL=%o#i#F= z-T{_FoGj6v{g%E9I3vUtW;|6mTZ}JJ{%9~2_Lg;v`4Yq@XDVUS(kDY!xW)ZiULnZ^ z`Jr(|HS5@e9^w?STU8bB)6ZiEdEOD?5A|9QZoQt#5q~AV3f&udb@=)6?fqllVcTD* z!RO7*X7^iQ^)5DuZCF}MVvOy!d4{kr8KG5Y+D&3KJ&fsCb|QcK*s?7PtX1E{|LBkG z7fB4rAp6fSw^JEddqnzto1BO$O7L8l*x}OIv+XwbOuOA)-_^?iI!jEZ4erb{Iyzh+ z(VY}SG4dDYHaElF;uI1cJ(D{)9LYj?NLS$WQF9t6BH6+|T zwj5DF-wA#d8|lthclI~ZFUt~J9O-duXVw1}>$!r!oH+_ytgKkzD`) literal 0 HcmV?d00001 diff --git a/test/api/test-draw-varc.c b/test/api/test-draw-varc.c index c0e8cbbd1..b2bca72a9 100644 --- a/test/api/test-draw-varc.c +++ b/test/api/test-draw-varc.c @@ -360,6 +360,26 @@ test_hb_draw_varc_short_cff2 (void) hb_face_destroy (face); } +static void +test_hb_draw_varc_delta_precision (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/varc-delta-precision.ttf"); + hb_font_t *font = hb_font_create (face); + hb_face_destroy (face); + hb_variation_t variation = { HB_TAG ('T','E','S','T'), 1.f }; + hb_font_set_variations (font, &variation, 1); + + const float first_moves[] = {600.f, 664.f, 101.f}; + for (unsigned i = 0; i < G_N_ELEMENTS (first_moves); i++) + { + draw_data_t draw_data = {0}; + hb_font_draw_glyph (font, i + 2, funcs, &draw_data); + g_assert_cmpuint (draw_data.move_to_count, ==, 1); + g_assert_cmpfloat (draw_data.first_move_x, ==, first_moves[i]); + } + hb_font_destroy (font); +} + static void test_hb_draw_varc_budget (void) { @@ -418,6 +438,7 @@ main (int argc, char **argv) hb_test_add (test_hb_draw_varc_conditional); hb_test_add (test_hb_draw_varc_static_gvar); hb_test_add (test_hb_draw_varc_short_cff2); + hb_test_add (test_hb_draw_varc_delta_precision); hb_test_add (test_hb_draw_varc_budget); #endif unsigned result = hb_test_run ();