From 1bf17d729584dabb9bfebf13bf223216d2ee618e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sat, 10 Oct 2026 04:04:32 -0600 Subject: [PATCH] [subset] Allow instancing axes unrelated to VARC (#6265) Replace the blanket VARC instancing rejection with validation of the axes referenced by retained components, variation regions, and nested conditions. Remap those indices when unrelated axes are removed, while keeping passthrough VARC incompatible with instancing. Preserve component-internal variation data during avar2 instancing by avoiding culling based on font-level reachable ranges. Add drawing comparisons and rejection tests with reproducible fixtures, including hidden component axes and non-default unrelated-axis pins. Serialize sparse-region axis references using the revised VARC format. Regenerate the fixtures without changing their logical VARC contents or other table bytes, apart from the head checksum adjustment. Validation: meson test -C build (283 passed); subset suite (130 passed); C++11 compile checks with HB_NO_VAR and HB_NO_VAR_COMPOSITES. Assisted-by: Codex --- NEWS | 3 + src/OT/Var/VARC/VARC-subset.hh | 49 ++++-- src/hb-ot-layout-common.hh | 70 +++++++-- src/hb-subset-plan-var.cc | 30 ++-- src/hb-subset-plan.cc | 1 + test/api/fonts/varc-unrelated-axis-avar2.ttf | Bin 0 -> 3172 bytes test/api/fonts/varc-unrelated-axis.ttf | Bin 0 -> 3000 bytes test/api/generate-varc-instancing-fonts.py | 68 ++++++++ test/api/test-subset-varc.c | 154 +++++++++++++++++-- 9 files changed, 327 insertions(+), 48 deletions(-) create mode 100644 test/api/fonts/varc-unrelated-axis-avar2.ttf create mode 100644 test/api/fonts/varc-unrelated-axis.ttf create mode 100644 test/api/generate-varc-instancing-fonts.py diff --git a/NEWS b/NEWS index a84abe4a1..b189f2c71 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,6 @@ +- Support instancing axes not referenced by retained `VARC` data. + + Overview of changes leading to 14.6.0 Monday, October 5, 2026 ===================================== diff --git a/src/OT/Var/VARC/VARC-subset.hh b/src/OT/Var/VARC/VARC-subset.hh index 2e259ed4a..f5886776c 100644 --- a/src/OT/Var/VARC/VARC-subset.hh +++ b/src/OT/Var/VARC/VARC-subset.hh @@ -97,6 +97,18 @@ struct varc_subset_plan_t bool create_maps () { + const auto *plan = subset_context->plan; + if (!plan->user_axes_location.is_empty ()) + { + /* Instancing axes referenced by VARC is not supported. Keep this + * restriction for avar2 as well; component coordinate overrides + * and their variation data need special handling. */ + axis_map = &axes_index_map; + for (auto _ : plan->axes_index_map) + if (!plan->user_axes_location.has (plan->axes_old_index_tag_map.get (_.first))) + axes_index_map.set (_.first, _.second); + if (unlikely (axes_index_map.in_error ())) return false; + } condition_map.add_set (&condition_indices); axis_indices_map.add_set (&axis_indices); return !condition_map.in_error () && !axis_indices_map.in_error () && @@ -146,11 +158,31 @@ struct varc_subset_plan_t bool select_axis_indices () { if (unlikely (!selected_axis_indices.alloc_exact ( - axis_indices_map.get_population ()))) + axis_indices_map.get_population ()) || + (axis_map && !axis_indices_data.resize ( + axis_indices_map.get_population ())))) return false; for (unsigned i = 0; i < axis_indices_map.get_population (); i++) { hb_ubytes_t bytes = source_axis_indices[axis_indices_map.backward (i)]; + if (axis_map) + { + hb_vector_t indices; + const HBUINT8 *p = (const HBUINT8 *) bytes.arrayZ; + if (unlikely (!TupleValues::decompile (p, indices, p + bytes.length, true))) + return false; + for (int &index : indices) + { + if (unlikely (!axis_map->has (index))) return false; + index = axis_map->get (index); + } + auto &data = axis_indices_data[i]; + /* One control byte and at most four data bytes per value. */ + unsigned size = hb_unsigned_mul_saturate (indices.length, 5u); + if (unlikely (size == UINT_MAX || !data.resize_dirty (size))) return false; + data.shrink (TupleValues::compile_unsafe (indices.as_array (), data.arrayZ)); + bytes = data.as_array (); + } axis_indices_data_size = hb_unsigned_add_saturate (axis_indices_data_size, bytes.length); selected_axis_indices.push (bytes); @@ -184,7 +216,10 @@ struct varc_subset_plan_t hb_vector_t records; hb_vector_t selected_axis_indices; + hb_vector_t> axis_indices_data; unsigned axis_indices_data_size = 0; + hb_map_t axes_index_map; + const hb_map_t *axis_map = nullptr; private: bool auxiliary_indices_unchanged () const @@ -332,12 +367,6 @@ VARC::subset (hb_subset_context_t *c) const return false; }; - /* VARC instancing is not supported yet. In particular, copying the - * auxiliary lists while fvar axes are removed would leave stale axis - * indices in the table. */ - if (unlikely (!c->plan->user_axes_location.is_empty ())) - return_trace (fail ()); - const Coverage &source_coverage = this+coverage; const CFF2Index &source_records = this+glyphRecords; const ConditionList &source_conditions = this+conditionList; @@ -373,7 +402,8 @@ VARC::subset (hb_subset_context_t *c) const !out->conditionList.serialize_serialize (c->serializer, &source_conditions, subset_plan.condition_map, - subset_plan.varidx_map)) || + subset_plan.varidx_map, + subset_plan.axis_map)) || (subset_plan.axis_indices_map.get_population () && !out->axisIndicesList.serialize_serialize (c->serializer, subset_plan.selected_axis_indices.iter (), @@ -381,7 +411,8 @@ VARC::subset (hb_subset_context_t *c) const (subset_plan.var_indices.get_population () && !out->varStore.serialize_serialize (c->serializer, &source_var_store, - subset_plan.var_inner_maps.as_array ())) || + subset_plan.var_inner_maps.as_array (), + subset_plan.axis_map)) || !out->glyphRecords.serialize_serialize (c->serializer, subset_plan.records.iter (), &subset_plan.data_size))) diff --git a/src/hb-ot-layout-common.hh b/src/hb-ot-layout-common.hh index e11739fb9..241fb87b8 100644 --- a/src/hb-ot-layout-common.hh +++ b/src/hb-ot-layout-common.hh @@ -2602,10 +2602,25 @@ struct SparseVarRegionAxis return_trace (c->check_struct (this)); } - bool serialize (hb_serialize_context_t *c) const + bool serialize (hb_serialize_context_t *c, + const SparseVarRegionAxis *src, + const hb_map_t *axis_map) { TRACE_SERIALIZE (this); - return_trace (c->embed (this)); + auto *out = c->embed (src); + if (unlikely (!out)) return_trace (false); + if (axis_map) + { + if (unlikely (!axis_map->has (out->axisIndex))) + { + c->err (HB_SERIALIZE_ERROR_OTHER); + return_trace (false); + } + if (unlikely (!c->check_assign (out->axisIndex, axis_map->get (out->axisIndex), + HB_SERIALIZE_ERROR_INT_OVERFLOW))) + return_trace (false); + } + return_trace (true); } public: @@ -2933,12 +2948,14 @@ struct SparseVariationRegion } bool serialize (hb_serialize_context_t *c, - const SparseVariationRegion *src) + const SparseVariationRegion *src, + const hb_map_t *axis_map) { TRACE_SERIALIZE (this); if (unlikely (!axes.serialize (c, (unsigned) src->axes.len))) return_trace (false); for (unsigned i = 0; i < axes.len; i++) - if (unlikely (!axes[i].serialize_copy (c, src->axes[i], src))) + if (unlikely (src->axes[i].is_null () || + !axes[i].serialize_serialize (c, &(src+src->axes[i]), axis_map))) return_trace (false); return_trace (true); } @@ -2986,7 +3003,8 @@ struct SparseVarRegionList bool serialize (hb_serialize_context_t *c, const SparseVarRegionList *src, - const hb_inc_bimap_t ®ion_map) + const hb_inc_bimap_t ®ion_map, + const hb_map_t *axis_map) { TRACE_SERIALIZE (this); if (unlikely (!c->extend_min (this))) return_trace (false); @@ -2998,7 +3016,8 @@ struct SparseVarRegionList unsigned old_index = region_map.backward (i); if (unlikely (old_index >= src->regions.len || !regions[i].serialize_serialize (c, - &(src+src->regions[old_index])))) + &(src+src->regions[old_index]), + axis_map))) return_trace (false); } return_trace (true); @@ -3926,7 +3945,8 @@ struct MultiItemVariationStore bool serialize (hb_serialize_context_t *c, const MultiItemVariationStore *src, - const hb_array_t &inner_maps) + const hb_array_t &inner_maps, + const hb_map_t *axis_map) { TRACE_SERIALIZE (this); if (unlikely (!c->extend_min (this) || @@ -3947,7 +3967,7 @@ struct MultiItemVariationStore hb_inc_bimap_t region_map; region_map.add_set (®ion_indices); if (unlikely (region_indices.in_error () || region_map.in_error () || - !regions.serialize_serialize (c, &src_regions, region_map))) + !regions.serialize_serialize (c, &src_regions, region_map, axis_map))) return_trace (false); dataSets.len = set_count; @@ -4739,7 +4759,8 @@ struct Condition bool serialize (hb_serialize_context_t *c, const Condition *src, - const hb_map_t &varidx_map); + const hb_map_t &varidx_map, + const hb_map_t *axis_map = nullptr); private: lookup_condition_subset_result_t subset_lookup_condition_impl ( @@ -4779,7 +4800,8 @@ struct ConditionList bool serialize (hb_serialize_context_t *c, const ConditionList *src, const hb_inc_bimap_t &condition_map, - const hb_map_t &varidx_map) + const hb_map_t &varidx_map, + const hb_map_t *axis_map) { TRACE_SERIALIZE (this); if (unlikely (!c->extend_min (this))) return_trace (false); @@ -4790,7 +4812,7 @@ struct ConditionList unsigned old_index = condition_map.backward (i); if (unlikely (old_index >= src->conditions.len || !conditions[i].serialize_serialize ( - c, &(src+src->conditions[old_index]), varidx_map))) + c, &(src+src->conditions[old_index]), varidx_map, axis_map))) return_trace (false); } return_trace (true); @@ -4851,7 +4873,8 @@ Condition::collect_var_indices (hb_set_t *var_indices, unsigned depth, inline bool Condition::serialize (hb_serialize_context_t *c, const Condition *src, - const hb_map_t &varidx_map) + const hb_map_t &varidx_map, + const hb_map_t *axis_map) { TRACE_SERIALIZE (this); switch (src->u.format.v) @@ -4866,7 +4889,22 @@ Condition::serialize (hb_serialize_context_t *c, return_trace (true); } case 1: - return_trace (bool (c->embed (&src->u.format1))); + { + auto *out = c->embed (&src->u.format1); + if (unlikely (!out)) return_trace (false); + if (axis_map) + { + if (unlikely (!axis_map->has (out->axisIndex))) + { + c->err (HB_SERIALIZE_ERROR_OTHER); + return_trace (false); + } + if (unlikely (!c->check_assign (out->axisIndex, axis_map->get (out->axisIndex), + HB_SERIALIZE_ERROR_INT_OVERFLOW))) + return_trace (false); + } + return_trace (true); + } case 2: { auto *out = c->embed (&src->u.format2); @@ -4888,7 +4926,7 @@ Condition::serialize (hb_serialize_context_t *c, for (unsigned i = 0; i < out->conditions.len; i++) if (unlikely (!out->conditions[i].serialize_serialize ( c, &(&src->u.format3+src->u.format3.conditions[i]), - varidx_map))) + varidx_map, axis_map))) return_trace (false); return_trace (true); } @@ -4902,7 +4940,7 @@ Condition::serialize (hb_serialize_context_t *c, for (unsigned i = 0; i < out->conditions.len; i++) if (unlikely (!out->conditions[i].serialize_serialize ( c, &(&src->u.format4+src->u.format4.conditions[i]), - varidx_map))) + varidx_map, axis_map))) return_trace (false); return_trace (true); } @@ -4912,7 +4950,7 @@ Condition::serialize (hb_serialize_context_t *c, if (unlikely (!out || !out->condition.serialize_serialize ( c, &(&src->u.format5+src->u.format5.condition), - varidx_map))) + varidx_map, axis_map))) return_trace (false); return_trace (true); } diff --git a/src/hb-subset-plan-var.cc b/src/hb-subset-plan-var.cc index c341b931e..3619af1ca 100644 --- a/src/hb-subset-plan-var.cc +++ b/src/hb-subset-plan-var.cc @@ -583,22 +583,28 @@ normalize_axes_location (hb_face_t *face, hb_subset_plan_t *plan) * (its 'pinned' path flattens ALL blends; lift this once * https://github.com/harfbuzz/harfbuzz/pull/4710 lands, porting * fontTools' instantiateCFF2 from - * https://github.com/fonttools/fonttools/pull/3506), and VARC passes - * through verbatim with explicit fvar axis indices that renumbering - * would desynchronize. Such axes stay in fvar as ordinary hidden - * pins. */ - bool detect_self_contained = true; - for (hb_tag_t table_tag : { HB_TAG ('C','F','F','2'), HB_TAG ('V','A','R','C') }) - { - hb_blob_t *blob = hb_face_reference_table (face, table_tag); - if (hb_blob_get_length (blob)) - detect_self_contained = false; - hb_blob_destroy (blob); - } + * https://github.com/fonttools/fonttools/pull/3506). Such axes stay + * in fvar as ordinary hidden pins. VARC remaps its axis indices and + * rejects instancing of axes it references. */ + hb_blob_t *cff2_blob = hb_face_reference_table (face, HB_TAG ('C','F','F','2')); + bool detect_self_contained = !hb_blob_get_length (cff2_blob); + hb_blob_destroy (cff2_blob); if (!_compute_avar2_reachable_ranges (plan, axes, avar_table, detect_self_contained)) return false; +#ifndef HB_NO_VAR_COMPOSITES + /* VARC overrides can reach coordinates outside the font-level + * ranges. Do not cull variation data using those ranges. */ + if (!plan->drop_tables.has (HB_TAG ('V','A','R','C'))) + { + hb_blob_t *varc_blob = hb_face_reference_table (face, HB_TAG ('V','A','R','C')); + if (hb_blob_get_length (varc_blob)) + plan->avar2_reachable_ranges.reset (); + hb_blob_destroy (varc_blob); + } +#endif + /* Keep all axes in fvar (pinned ones as hidden), EXCEPT self-contained * pinned axes, which are removed entirely. */ plan->axes_index_map.reset (); diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 9dcf55310..e34e13be8 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -718,6 +718,7 @@ hb_subset_plan_t::hb_subset_plan_t (hb_face_t *face, #ifndef HB_NO_VAR_COMPOSITES if (!user_axes_location.is_empty () && !input->sets.drop_tables->has (OT::VARC::tableTag) && + input->sets.no_subset_tables->has (OT::VARC::tableTag) && face->table.VARC->has_data ()) { check_success (false); diff --git a/test/api/fonts/varc-unrelated-axis-avar2.ttf b/test/api/fonts/varc-unrelated-axis-avar2.ttf new file mode 100644 index 0000000000000000000000000000000000000000..f5821ba0fc3813b51c13e7258b995d7abdf4b66a GIT binary patch literal 3172 zcmc&$eQX@X6@N3cyJuexpU+-G9J`KGv zb|FnGb2;zvHkX(q9yX|l0 z_TrDU{kOAzZ+`FhX5M_g86Pnsx`u2L^wo{+U+etS)~A*biBm-Ei;4RaLsU&wMEY{H z+Sju!F-VbTzYgC3?AH6>f9&s5C*k*CPi`Lg?tPE`_T&BV{|@$^&G5fc_RMa1La_6}|z9>F?L#3AV?xi&~X{>;oh(fD~1a$Q8((q^ zv!<=B{@VH*OvBJZ`d?Vv8$-DY{^Ip_|1tlsgALaW{rZCko7Y8#UwU$&hB$vZSh1iG zLbbFth?ZCbfAzqD|xcLgJ$jUH3cW1<^T53OwA`%c1lYyjUF;1KXlatF0iFeTWP z0_MQC7BDt1LhS)&w3u!Sux!&4U;(@}z#8yyfOX*S1z67B32+EFHPo|ZD-yeBQ*Zy~ zzL5=sBmF&zf!q6c^)?mCYE;dT7Dy|kjfODb77TPhB^2I7z0^;esgFjG+=I~lXpx`+ zx*cd2yiN1I)q&L<)Ga~X8q{s_Bvjr@@Lx(Ho0h;U@A7ky_aO50Wai1!Fx2iwlOa7Kg!BNMzxYZDq|=CmBvYD##qhqLs@&*VLYWBY(i|(7ITR?cFm< zk)(AwZd>ee+p@^9^3U5=$zIzs!j8?dhey#v0+kZQVDdV$5K#VUvn=z_4qh~u zQzg!rzKCnOp+Q0=U^K=ewX5(xaNEWr@fNKm!8+dF!5=<$G>NN}rKZIQY8|$1tM*gw z&#cA0Xisna@FrVmv?bqMxV1!K5*~SoP+05#*;4a>k{cyop)l?u(W=)F4IX0r(DtlgEuV^(Qt75ni(fR&@+d?8<&4VTU$K_OA@F^RV2T`TUR+TkJD_Co* zN-8f9iD@2hDN3nEdlqA7yrNZC;4Ro*AgC}WTU8dcr*=}AL`!W2tU!CJhN`!VNVWHh z*1Y(0#;vsHMlH4`s=U&Zj?c1J(5&|gMbop8s4RR*y7Y#oW=GtyX>GM@@Uznw%fIkVr<0vyC)h)*ob51Ox~G#dmSk_TPaHOUs4M?` z?n3T|@=JCV>fHPJN7$oXjxCmac4hgPrbWUR-*-Ah2mb;4ve@DKev+n>baw}<;a?IA z?OHc>*Nkc5>T5rG!Aar0V(xe-9*?h>nLK#V*`outt&F-U&INZT##ubRSA1~#Le4cz zV)>b~8HB)P@pq#uca!8h%rr;Eb>lUazbpv9Smb|3{p#BqOKFi5MCD@U+O*a4l literal 0 HcmV?d00001 diff --git a/test/api/fonts/varc-unrelated-axis.ttf b/test/api/fonts/varc-unrelated-axis.ttf new file mode 100644 index 0000000000000000000000000000000000000000..6fd4c04aac695a3127183ba05687226d4e78e164 GIT binary patch literal 3000 zcmaJ@eQ;D)6+idh_ug)nZ8n>{QXnxT4bW8SG$dKHx-u^x!(<#PGuFbiI>ntdkU)@- z#ER&QZ}Z*~n6*6QcvO9|{gfA{WN z@_~A9&bhzyJLjJJ{WipiC`>j9vNm;mrRy&{o?b#ECW+V=dUqyP^rDZG`g1U0@;$U*}5eXzeGZumEP~I{JkyP(;N0=y(`{tg)5q13gVJQ zK_AiJeGdApcZE#hs!SzcNpr;nsmkwD?s+{%jwGItCNdCz2H<{(jFU4oq2=sM(x?Hg zh6?*x=3#fFmYWQIw$xKStwHSu#W3r)L=XQXMTl-$yJGFySgi5p#@kH8&;t73SnRdo zLN$Nk)(8Gv{P&TjTZVu4{tsH$heuv~a}#hOH0w28lb`0$Di8`xdz zi+74|VeI?;5fGzCRraXpLDr+In)t!i^;L!Y_>d131CF{8zFw~u8^ zua5=r4j*g4BRMu(z+JRMwzr zg|tE1Au$@pfZH+9LzGZ>GxgB`ZKHk~#pND?9zaBb2I(H4z3{fot809#)oS^yJNxr(vkw4J+LVpUhYVSq!OD>|&*ts9PKYTP=}= zDr05KR418^=q8BHSfpn8XIeUnuN4p5omw}GvIG0aD4eve#chi{Zd(>PR`HN+1rOMk z5prynKR$+@a=JkhNK`=-g~=PpLPGhc&9W?mICv2*r%IeLeG%7mLxTi@U^K?U^{eA> z*)|qQWUM2>I^WvOA36DE5>F{lEsK%VI&Rxm{U_X;S%-Jgk=^vc9k$SDd$F}N8Tryf zxbH-(yYIdGKKV{nQTxiMYvY%@7joam{a_E;Bt`UWcts}KH8irf)DgPWm*JAMEa=NY zty#bcaIFhC4P4s-P6yY%fHT0w7H|PIehBAIvaCVpD92---HonaPxa-&zZ7 zX-X~w`R~QLf&PDtZL(ic6Xw{AZmaEieKTdN87CLTrHjn>m)sT(`$irtQ7ulhw&YVb zs0>7vrM7CUIawvFBzjM-$at?@6a-+3RT5d!3@`Sx8hFx+z*?nr1GS z%VtlU`q+)+KAOICjZE{z3949jTT81W?%lku);0Kr=_^&A|C-aqF0#|?VOGU+EBPjU4SR9x7Ze{8-jxcd0GBSNsuswA-=8l25OyN@-d+bmcv#Q*`q0voDF= zp64ZLI!O<7vO4}n!4TKJsi!Wbg=%m9@OdYVZvu121Mzr#<;>)fBhG#uFt#e)@l(!uQsDdbT&KqiUST64|k;qB||q_mUU7AdlRZ*hmRGR2NU^z5J literal 0 HcmV?d00001 diff --git a/test/api/generate-varc-instancing-fonts.py b/test/api/generate-varc-instancing-fonts.py new file mode 100644 index 000000000..915b5a3e2 --- /dev/null +++ b/test/api/generate-varc-instancing-fonts.py @@ -0,0 +1,68 @@ +"""Regenerate the fixtures using FontTools with the revised VARC format.""" + +from copy import deepcopy +from pathlib import Path + +from fontTools.ttLib import TTFont, newTable +from fontTools.ttLib.tables import otTables +from fontTools.ttLib.tables._f_v_a_r import Axis +from fontTools.ttLib.tables.TupleVariation import TupleVariation + + +def condition(format, **fields): + result = otTables.ConditionTable() + result.Format = format + result.__dict__.update(fields) + return result + + +directory = Path(__file__).parent / "fonts" +font = TTFont(directory / "varc-ac01-conditional.ttf", recalcTimestamp=False) +# Load gvar before changing the axis order. +variations = font["gvar"].variations +for tag in reversed(("DUMY", "COND")): + axis = Axis() + axis.axisTag = tag + axis.minValue, axis.defaultValue, axis.maxValue = -1, 0, 1 + axis.axisNameID = font["fvar"].axes[0].axisNameID + font["fvar"].axes.insert(0, axis) + +varc = font["VARC"].table +varc.AxisIndicesList.Item = [[i + 2 for i in indices] + for indices in varc.AxisIndicesList.Item] +for region in varc.MultiVarStore.SparseVarRegionList.Region: + for axis in region.SparseVarRegionAxis: + axis.AxisIndex += 2 + +# COND is used only by nested conditions; DUMY is absent from VARC. +weight_condition = varc.ConditionList.ConditionTable[0] +weight_condition.AxisIndex += 2 +low = condition(1, AxisIndex=1, FilterRangeMinValue=-1, FilterRangeMaxValue=0) +middle = condition(1, AxisIndex=1, FilterRangeMinValue=0, FilterRangeMaxValue=0.25) +either = condition(4, ConditionTable=[low, middle]) +negated = condition(5, ConditionTable=either) +varc.ConditionList.ConditionTable[0] = condition( + 3, ConditionTable=[weight_condition, negated]) + +# Give the unrelated axis a visible effect in an underlying outline. +name = "glyph00003" +points = len(font["glyf"][name].getCoordinates(font["glyf"])[0]) +variations[name].append(TupleVariation( + {"DUMY": (0, 1, 1)}, [(20, 0)] * points + [(0, 0)] * 4)) +# This tent is reached by a VARC override, but not by the font-level +# default of the private axis. Font-level avar2 culling must preserve it. +variations[name].append(TupleVariation( + {"0000": (-1, -0.5, -0.25)}, [(0, 20)] * points + [(0, 0)] * 4)) +font.save(directory / "varc-unrelated-axis.ttf") + +font = deepcopy(font) +for axis in font["fvar"].axes: + if axis.axisTag.startswith("000"): + axis.flags = 1 # Hidden component axes. +avar = font["avar"] = newTable("avar") +avar.majorVersion, avar.minorVersion = 2, 0 +avar.segments = {a.axisTag: {-1: -1, 0: 0, 1: 1} for a in font["fvar"].axes} +avar.table = otTables.avar() +avar.table.VarIdxMap = None +avar.table.VarStore = None +font.save(directory / "varc-unrelated-axis-avar2.ttf") diff --git a/test/api/test-subset-varc.c b/test/api/test-subset-varc.c index b31d2e94a..46dd9560b 100644 --- a/test/api/test-subset-varc.c +++ b/test/api/test-subset-varc.c @@ -25,6 +25,8 @@ #include "hb-test.h" #include "hb-subset-test.h" +#include + static void draw_move_to (hb_draw_funcs_t *funcs HB_UNUSED, void *data, hb_draw_state_t *state HB_UNUSED, float x, float y, @@ -94,6 +96,28 @@ static char * draw_unicode (hb_face_t *face, hb_codepoint_t unicode) { return draw_unicode_at (face, unicode, NULL, 0); } +static void +assert_paths_close (const char *a, const char *b) +{ + /* Baking gvar deltas changes floating-point summation order. */ + while (*a && *b) + { + char *a_end, *b_end; + double x = g_ascii_strtod (a, &a_end); + double y = g_ascii_strtod (b, &b_end); + if (a_end != a || b_end != b) + { + g_assert_true (a_end != a && b_end != b); + g_assert_cmpfloat (fabs (x - y), <, 0.001); + a = a_end; + b = b_end; + } + else + g_assert_cmpint (*a++, ==, *b++); + } + g_assert_cmpint (*a, ==, *b); +} + static void assert_has_varc (hb_face_t *face, hb_bool_t expected) { @@ -270,19 +294,119 @@ test_subset_varc_retain_gids (void) static void test_subset_varc_fails_when_instancing (void) +{ + const char *fonts[] = { + "fonts/varc-unrelated-axis.ttf", + "fonts/varc-unrelated-axis-avar2.ttf", + }; + const hb_tag_t tags[] = { + HB_TAG ('w','g','h','t'), /* MultiVarStore */ + HB_TAG ('o','p','s','z'), /* MultiVarStore */ + HB_TAG ('0','0','0','0'), /* Component coordinates */ + HB_TAG ('C','O','N','D'), /* Nested conditions only */ + }; + for (unsigned f = 0; f < G_N_ELEMENTS (fonts); f++) + { + hb_face_t *face = hb_test_open_font_file (fonts[f]); + for (unsigned i = 0; i < G_N_ELEMENTS (tags); i++) + for (unsigned restrict_range = 0; restrict_range < 2; restrict_range++) + { + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add (hb_subset_input_unicode_set (input), 0xAC01); + hb_ot_var_axis_info_t axis; + g_assert_true (hb_ot_var_find_axis_info (face, tags[i], &axis)); + if (restrict_range) + g_assert_true (hb_subset_input_set_axis_range ( + input, face, tags[i], axis.min_value, + (axis.default_value + axis.max_value) / 2, axis.default_value)); + else + g_assert_true (hb_subset_input_pin_axis_to_default (input, face, tags[i])); + + g_assert_null (hb_subset_or_fail (face, input)); + hb_subset_input_destroy (input); + } + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add (hb_subset_input_unicode_set (input), 0xAC01); + g_assert_true (hb_subset_input_pin_all_axes_to_default (input, face)); + g_assert_null (hb_subset_or_fail (face, input)); + hb_subset_input_destroy (input); + hb_face_destroy (face); + } +} + +static void +test_subset_varc_instance_unrelated_axis (void) +{ + const char *fonts[] = { + "fonts/varc-unrelated-axis.ttf", + "fonts/varc-unrelated-axis-avar2.ttf", + }; + hb_tag_t dummy = HB_TAG ('D','U','M','Y'); + for (unsigned f = 0; f < G_N_ELEMENTS (fonts); f++) + { + hb_face_t *face = hb_test_open_font_file (fonts[f]); + for (unsigned mode = 0; mode < 3; mode++) + { + hb_bool_t restrict_range = mode == 1; + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add (hb_subset_input_unicode_set (input), 0xAC01); + if (mode == 2) + hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_RETAIN_GIDS); + if (restrict_range) + g_assert_true (hb_subset_input_set_axis_range (input, face, dummy, 0, 1, 0.5f)); + else + g_assert_true (hb_subset_input_pin_axis_location (input, face, dummy, 0.5f)); + hb_face_t *subset = hb_subset_test_create_subset (face, input); + assert_has_varc (subset, true); + g_assert_cmpuint (hb_ot_var_get_axis_count (subset), ==, + hb_ot_var_get_axis_count (face) - !restrict_range); + hb_ot_var_axis_info_t axis; + g_assert_cmpint (hb_ot_var_find_axis_info (subset, dummy, &axis), ==, restrict_range); + if (restrict_range) + { + g_assert_cmpfloat (axis.min_value, ==, 0); + g_assert_cmpfloat (axis.default_value, ==, 0.5f); + g_assert_cmpfloat (axis.max_value, ==, 1); + } + g_assert_true (hb_ot_var_find_axis_info (subset, HB_TAG ('w','g','h','t'), &axis)); + g_assert_cmpuint (axis.axis_index, ==, restrict_range ? 2 : 1); + + for (unsigned sample = 0; sample < 8; sample++) + { + hb_variation_t variations[] = { + {dummy, restrict_range && (sample & 1) ? 1.f : 0.5f}, + {HB_TAG ('w','g','h','t'), sample & 2 ? 840.3f : 356.5f}, + {HB_TAG ('o','p','s','z'), sample & 1 ? 1.f : 0.f}, + {HB_TAG ('C','O','N','D'), sample & 4 ? 1.f : 0.f}, + }; + char *source_path = draw_unicode_at (face, 0xAC01, variations, + G_N_ELEMENTS (variations)); + char *subset_path = draw_unicode_at (subset, 0xAC01, variations, + G_N_ELEMENTS (variations)); + g_assert_cmpstr (source_path, !=, ""); + assert_paths_close (subset_path, source_path); + g_free (subset_path); + g_free (source_path); + } + hb_face_destroy (subset); + } + hb_face_destroy (face); + } +} + +static void +test_subset_varc_instance_without_varc_glyphs (void) { hb_face_t *face = hb_test_open_font_file ("fonts/varc-ac00-ac01.ttf"); - hb_set_t *unicodes = hb_set_create (); - hb_set_add (unicodes, 0xAC00); - hb_subset_input_t *input = hb_subset_test_create_input (unicodes); - hb_set_destroy (unicodes); - - g_assert_true (hb_subset_input_pin_axis_location (input, face, - HB_TAG ('w','g','h','t'), 400)); - hb_face_t *subset = hb_subset_or_fail (face, input); - g_assert_null (subset); - - hb_subset_input_destroy (input); + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add (hb_subset_input_glyph_set (input), 0); + g_assert_true (hb_subset_input_pin_axis_to_default ( + input, face, HB_TAG ('w','g','h','t'))); + hb_face_t *subset = hb_subset_test_create_subset (face, input); + assert_has_varc (subset, false); + g_assert_cmpuint (hb_ot_var_get_axis_count (subset), ==, + hb_ot_var_get_axis_count (face) - 1); + hb_face_destroy (subset); hb_face_destroy (face); } @@ -333,6 +457,12 @@ test_subset_varc_passthrough_requires_retained_gids (void) assert_has_varc (subset, true); hb_face_destroy (subset); + input = create_varc_no_subset_input (true); + /* Even an unrelated pin cannot renumber axes in a passthrough table. */ + g_assert_true (hb_subset_input_pin_axis_to_default ( + input, face, HB_TAG ('0','0','0','5'))); + g_assert_null (hb_subset_or_fail (face, input)); + hb_subset_input_destroy (input); hb_face_destroy (face); } @@ -347,6 +477,8 @@ main (int argc, char **argv) hb_test_add (test_subset_varc_no_variation_index); hb_test_add (test_subset_varc_retain_gids); hb_test_add (test_subset_varc_fails_when_instancing); + hb_test_add (test_subset_varc_instance_unrelated_axis); + hb_test_add (test_subset_varc_instance_without_varc_glyphs); hb_test_add (test_subset_varc_can_be_explicitly_dropped_when_instancing); hb_test_add (test_subset_varc_passthrough_requires_retained_gids); return hb_test_run ();