From f26e0cd4ed059f905c2f278fbcfa5da679b737b1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 29 Jul 2026 11:29:23 -0600 Subject: [PATCH] [instancer] Fix HVAR partial instancing for map-less advance mappings (#6070) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * [instancer] Don't dedup VarStore rows when not optimizing as_item_varstore dropped duplicate delta rows even with optimize=false, where varidx_map is left empty and callers rely on original VariationIndex values staying valid. HVAR/VVAR subsetting uses that mode when the source table has an implicit (map-less) advance mapping: any two glyphs sharing identical deltas shifted every following row, so the synthesized identity map pointed at wrong rows and past the end of the VarData — advances silently froze or took neighbors' deltas when partially instancing such fonts (e.g. Source Han fonts). Fixes https://github.com/harfbuzz/harfbuzz/issues/6068 * [subset] Drop identity advance maps for map-less HVAR/VVAR sources When the source table uses implicit glyph-id indexing, the subsetter synthesized an advance DeltaSetIndexMap whose content was the identity over every output glyph — equivalent to the implicit indexing it replaced, just bigger. Recognize that case in is_identity () and keep the output map-less, like fontTools does. Sources that carry a real map keep one, preserving their form; truncated maps don't qualify since glyphs past their end share the last value. retain-num-glyphs expected files regenerated: NotoSansJP-VF.subset.otf is map-less, and its redundant identity map is now dropped (verified against fontTools output). * [test/subset] Test partial instancing of map-less HVAR ImplicitHVAR.ttf carries an HVAR with implicit (map-less) advance indexing and deliberately duplicated delta rows, the combination that broke before the row-dedup fix: rows shifted or fell off the end of the rebuilt VarData and advances silently froze. Expected outputs are cross-checked against fontTools varLib.instancer + subset. --- src/hb-ot-var-common.hh | 12 +++++++--- src/hb-ot-var-hvar-table.hh | 21 +++++++++++++++++- ...HVAR.default.41,42,43.wght=200-400-800.ttf | Bin 0 -> 800 bytes ...icitHVAR.default.41,42,43.wght=300-600.ttf | Bin 0 -> 800 bytes ...licitHVAR.default.all.wght=200-400-800.ttf | Bin 0 -> 864 bytes .../ImplicitHVAR.default.all.wght=300-600.ttf | Bin 0 -> 864 bytes ...SansJP-VF.subset.retain-num-glyphs.all.otf | Bin 4024 -> 4016 bytes test/subset/data/fonts/ImplicitHVAR.ttf | Bin 0 -> 968 bytes .../hvar_partial_instance_implicit_map.tests | 13 +++++++++++ test/subset/meson.build | 1 + 10 files changed, 43 insertions(+), 4 deletions(-) create mode 100644 test/subset/data/expected/hvar_partial_instance_implicit_map/ImplicitHVAR.default.41,42,43.wght=200-400-800.ttf create mode 100644 test/subset/data/expected/hvar_partial_instance_implicit_map/ImplicitHVAR.default.41,42,43.wght=300-600.ttf create mode 100644 test/subset/data/expected/hvar_partial_instance_implicit_map/ImplicitHVAR.default.all.wght=200-400-800.ttf create mode 100644 test/subset/data/expected/hvar_partial_instance_implicit_map/ImplicitHVAR.default.all.wght=300-600.ttf create mode 100644 test/subset/data/fonts/ImplicitHVAR.ttf create mode 100644 test/subset/data/tests/hvar_partial_instance_implicit_map.tests diff --git a/src/hb-ot-var-common.hh b/src/hb-ot-var-common.hh index e9ba284ca..2121dd234 100644 --- a/src/hb-ot-var-common.hh +++ b/src/hb-ot-var-common.hh @@ -1964,10 +1964,16 @@ struct item_variations_t if (!front_mapping.set ((major<<16) + minor, &row)) return false; - if (delta_rows_map.has (&row)) - continue; + /* Duplicate rows may only be dropped when optimizing: without a + * varidx_map the caller relies on original VariationIndex values + * staying valid (e.g. HVAR/VVAR with an implicit advance mapping). */ + if (optimize) + { + if (delta_rows_map.has (&row)) + continue; - delta_rows_map.set (&row, 1); + delta_rows_map.set (&row, 1); + } major_rows.push (&row); } diff --git a/src/hb-ot-var-hvar-table.hh b/src/hb-ot-var-hvar-table.hh index f7be19e93..dda30eca8 100644 --- a/src/hb-ot-var-hvar-table.hh +++ b/src/hb-ot-var-hvar-table.hh @@ -49,6 +49,12 @@ struct index_map_subset_plan_t bool bypass_empty = true) { map_count = 0; + full_map_count = plan->new_to_old_gid_list.length ? + plan->new_to_old_gid_list.tail ().first + 1 : 0; + /* Only the advance mapping may be dropped in favor of implicit + * glyph-id indexing (for the others a null offset means no deltas), + * and only when the source was map-less too, preserving its form. */ + can_drop_to_implicit = !bypass_empty && !index_map; max_inners.init (); output_map.init (); @@ -187,11 +193,24 @@ struct index_map_subset_plan_t DeltaSetIndexMap::min_size); } - bool is_identity () const { return get_output_map ().length == 0; } + bool is_identity () const + { + if (!output_map) return true; + /* An advance map whose entries are the identity over every output + * glyph is equivalent to implicit glyph-id indexing; drop it. A + * truncated map doesn't qualify: glyphs past its end share its last + * value, not their own ids. */ + if (!can_drop_to_implicit || map_count != full_map_count) return false; + for (unsigned i = 0; i < map_count; i++) + if (output_map.arrayZ[i] != i) return false; + return true; + } hb_array_t get_output_map () const { return output_map.as_array (); } protected: unsigned int map_count; + unsigned int full_map_count; + bool can_drop_to_implicit; hb_vector_t max_inners; unsigned int outer_bit_count; unsigned int inner_bit_count; diff --git a/test/subset/data/expected/hvar_partial_instance_implicit_map/ImplicitHVAR.default.41,42,43.wght=200-400-800.ttf b/test/subset/data/expected/hvar_partial_instance_implicit_map/ImplicitHVAR.default.41,42,43.wght=200-400-800.ttf new file mode 100644 index 0000000000000000000000000000000000000000..0561b7734b41f354021a0d32087914d25c5d51ab GIT binary patch literal 800 zcmaJf~TzCo22QIL*Ha(-uHcP-g`3}C;(~f!bGvO>3sN3oN2ZyF7kLn{~PC^?`{WpzyaKFWq6x1P3RzaxvBn{jy_Hx7@MmVD0vV}jtJ`ihkvo>)CbgxS#Z|Ie>?+IsZ+A0MMC3$K zbk;pDQ4$w7;fA%I=1E-}mX#($rO46ci0*#wU%Y!f^#`UY?}+Q2+11{z`!s$Lo4PX* zlTsdi%|0%a|<#W3vikOL<+%_@548#0F9pAts{-3nZ zHq(q-MimXz;UUBV{W?CP2^Z^_g^z%K0~O8?B`m>VjA@ws21)$}y3~Ibd9$8P(+C{Q K^r(I(3)}-0IAVDK literal 0 HcmV?d00001 diff --git a/test/subset/data/expected/hvar_partial_instance_implicit_map/ImplicitHVAR.default.all.wght=200-400-800.ttf b/test/subset/data/expected/hvar_partial_instance_implicit_map/ImplicitHVAR.default.all.wght=200-400-800.ttf new file mode 100644 index 0000000000000000000000000000000000000000..97c16721dbc96a277d3c1fe5d93391bbe758a693 GIT binary patch literal 864 zcmah{O-mb56g@MOND5JFh!CWU5rhU8Z5e_MA~XXEanoW1TSU+yO)`Ou4tCTOLW%wY zan)5<{Q+IHP)aGql?&HdDY&teE(Ae0f-{~whB&zCP40X5+;czPyKkTXNTLl5h2nx) zDtPuJp!}sb|7!lJVdl+GfZ8HnDLaY% zEvFJc9dcP;Cth;b-g!JXMM-nf#X>(u@EAFm^fHTTs0r#L>NDyz49K1P5DRw}3-h8& zSTpLlXG{J=<|&ZAiLILL;efq%0Zo@!2Jo-xL7wHMqoJm{-GsgsxtH{oZfUMkHO*7$ z++*F-%>)6F83e)aZoWiOe|^}#ULKsfgr><`;ZJLJvA2i))H7l&>>z~XN&TpZZzN=r z;gkhuvl(RRqg~*SqG4Q47kfZohVi$yiUEELq0P`F<6fhJI%=@tp+vjJw`{<|I%eU( zrCmpv>Y<1QzE267I?S2HdDs7ooUJD>CEdanD1PKP3sSN1sx&iMK0(5Rv8`mUk= j>@uAw^VwgzWBx^%?|$))@uSS4pX)JdD$1Prx-hr_0}gU) literal 0 HcmV?d00001 diff --git a/test/subset/data/expected/hvar_partial_instance_implicit_map/ImplicitHVAR.default.all.wght=300-600.ttf b/test/subset/data/expected/hvar_partial_instance_implicit_map/ImplicitHVAR.default.all.wght=300-600.ttf new file mode 100644 index 0000000000000000000000000000000000000000..35af082a6cfea4651a827ce20178b970c5ee61f4 GIT binary patch literal 864 zcmah{Pe>GD6#soQyRyQhDI_NN- z(jhu^>g1_IK}4W<_Pi$pFG_gmRF@7j{oc5YgO}d!`@Z*kzxU^R-#1VI4D3Khxw7O` z%L`AY0rivK(xb&0+bKC80Ifs3TJzn2vG2(XE;e4f>zy~Px5P!_iDrAd(f#r5G2>5& zcfGp%V!GP>O#Tz`U5}1@LwzA$mUzz(w~QV9r7iKJ?X{YV4LqcMOdasutpEk|(f&=f zUb=oge>N1bzD2weti1|(Zi+IwSYqJ@#xRK@9D12WwY38E7WEGG0c^<3eaIy{izRv4 zC8?QpT(c$rCi4`eZ(+0Pg=&hu_E=0NlfwM#R$QV-I@;QBuba@9B6CS^>1J}3swPim zanJQmHy6i5P8`RFy?lwH9=zKhc|JJ%2f8kAh4aq*vvg1TX}^f|bUMkC`dJa*SW?$W zZdtHt8jMonE%Qgwaa&H8>!)v&@t3xS0e%aK%}|4JPf;Fa0mdbq!frGi!b=|P&P;&F?h&RDIzBfKJZmYd0u(j_! hrkiCxMk`m$MV9#*Rj!yQ%N$3=l+lK>%xPo^gMXWeaBTnp literal 0 HcmV?d00001 diff --git a/test/subset/data/expected/retain-num-glyphs/NotoSansJP-VF.subset.retain-num-glyphs.all.otf b/test/subset/data/expected/retain-num-glyphs/NotoSansJP-VF.subset.retain-num-glyphs.all.otf index 8105e790732587ca00b44bdd4f3105b584f40d57..43e8bcc600f5c390fc2befc147c2441097490f37 100644 GIT binary patch delta 194 zcmdlXzd?S2IOBzh67q&S7#J8FJi;7<7^E1K7#JA80Qp+}!TLsvKd)NGz`*PRIfkj|qhPP|!HBEU~D!x`&a0fk^?RDLFT>V1fC9W+49uknfWQl<)2; ztpFOb1jx_INKH&p;9;u)$~R0rVb6GCG6&;lRS}>R0sswT5Mlt4JPd4LVGx^P@)yQb Sb|ywPgJo%)o2M|T@B#pOQY!uc delta 208 zcmdlWze9e4IOC0p67q%z7#J8FJi;7=kb(Jb2DXc#(Rt<~pL1+Ul?uz(h3#}AE!#2AKBsQ$6R_URL zM-Rn|3gXE>L7}LJ;#tAdo)kQZ^dJa&@sRjUvSt^dIFFh4zWL_m&Ac~(0$>MLq2cn> zO#K`YJ=zB6LT%P;bdFCxCSD+}UYa>!m{s#RL#`6vX!=%wBAyU`B(Ah>Tb-33?jy!O zBQCeSyRGi$*H-|wM10?|t%dPM_a)b#sHYt|hFZ!i?iar7_~CMK6~BqsiATM~riCS3 zjb|WB%^!$7t$aWer1&_`gV= znM_a!OiX94e-}Da{F$hs7MTA*>Pf0(%cQ9qwL~jlE-?q4bB;ssbR5%~aU5^-vL|E@ z9Kcajc&^mXs+D)V6@KT;*vOcntVMyLzU?vHLFPqtt2vJ$?a$7 ze2H{nMDl!j$yf3zCRWxewh<+MnmqPMmAUQojADOptzsCb(luj>n5g@K=QiC?I=W%E zmprR8XS;1DL>)c?cyQ6AhNxkdSJ2?F(Pr~`u+YIAkxT3_BFRDQ*1`@&8HaI%H+qcg xv