From 5c5172fcd29077b25191ddceb3efb524b30b277a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 11 Oct 2026 00:38:02 -0600 Subject: [PATCH] [subset] Preserve zero-contour glyph instructions when instancing (#6369) Zero-contour glyphs can contain instructions for phantom-point metrics. Non-default instancing discarded them, while the empty-glyph branch also bypassed ordinary hint removal. Copy these instructions into owned compiled storage, and remove them when hinting is dropped. Keep discarded .notdef outlines empty. Add generated data and regressions for ordinary subsets, default pins, partial and full instances, hint removal, and phantom-point advances. The configured Meson suite passes: 253 tests and one skipped. Assisted-by: Codex --- src/OT/glyf/Glyph.hh | 32 ++++++++++++++-- test/api/fonts/empty-hinted.ttf | Bin 0 -> 1052 bytes test/api/fonts/generate-empty-hinted.py | 48 ++++++++++++++++++++++++ test/api/test-subset-glyf.c | 48 ++++++++++++++++++++++++ 4 files changed, 124 insertions(+), 4 deletions(-) create mode 100644 test/api/fonts/empty-hinted.ttf create mode 100644 test/api/fonts/generate-empty-hinted.py diff --git a/src/OT/glyf/Glyph.hh b/src/OT/glyf/Glyph.hh index d6510812d..04ac4c4ca 100644 --- a/src/OT/glyf/Glyph.hh +++ b/src/OT/glyf/Glyph.hh @@ -55,7 +55,10 @@ struct Glyph switch (type) { case COMPOSITE: CompositeGlyph (*header, bytes, extended).drop_hints (); return; case SIMPLE: SimpleGlyph (*header, bytes).drop_hints (); return; - case EMPTY: return; + case EMPTY: + if (bytes.length >= GlyphHeader::static_size) + SimpleGlyph (*header, bytes).drop_hints (); + return; } } @@ -73,7 +76,13 @@ struct Glyph switch (type) { case COMPOSITE: CompositeGlyph (*header, bytes, extended).drop_hints_bytes (dest_start); return; case SIMPLE: SimpleGlyph (*header, bytes).drop_hints_bytes (dest_start, dest_end); return; - case EMPTY: return; + case EMPTY: + dest_start = bytes; + dest_end = hb_bytes_t (); + if (bytes.length >= GlyphHeader::static_size && + SimpleGlyph (*header, bytes).has_instructions_length ()) + SimpleGlyph (*header, bytes).drop_hints_bytes (dest_start, dest_end); + return; } } @@ -281,10 +290,25 @@ struct Glyph return false; break; case EMPTY: - /* set empty bytes for empty glyph - * do not use source glyph's pointers */ dest_start = hb_bytes_t (); dest_end = hb_bytes_t (); + /* Zero-contour glyphs may carry instructions for phantom points. + * Copy them into owned memory like the other compiled glyphs. */ + if (!(plan->flags & HB_SUBSET_FLAGS_NO_HINTING) && + bytes.length >= GlyphHeader::static_size && + header->numberOfContours == 0) + { + SimpleGlyph simple (*header, bytes); + unsigned length = simple.instructions_length (); + if (length) + { + length = simple.length (length); + char *p = (char *) hb_malloc (length); + if (unlikely (!p)) return false; + hb_memcpy (p, bytes.arrayZ, length); + dest_start = hb_bytes_t (p, length); + } + } break; } } diff --git a/test/api/fonts/empty-hinted.ttf b/test/api/fonts/empty-hinted.ttf new file mode 100644 index 0000000000000000000000000000000000000000..c67330a6d5ad786190f6ebe694120275cb79e9be GIT binary patch literal 1052 zcmah|O>5Lp6g@95O=qO|fki45OhKg!@uN^FIABqS+Lcll<4(iObP`Br!c4|?pxB68 z7uB^J!G*YxL5jE#+`8~Lh|-M<5ejwXBA%D`Yz9HSki2`(x!*5=05E|R5{ox(otvxT z>MkIz6TMrHycpZ4&>qsRtlsmw-#>rK0J%+jp%o5RH{b8PCBH}ebc_6zsdSV4H`-_Y zrq{T=`}`^Ok9;or1nk%13H@!_C;cc{&m$pT&@P3Yx`z^ul2`Gi$Xk!$5P_Jb?Y6zB zx%2Zed25#-JwqpXeOM>oSF^3s3hTU3~Z>fonR` zu&S+UPMU5eSyS!D;VIY|GiAIZeP&;>XG9Ch_q%V(p_s|%=Syvcxi8EdF$<#CLA)~2 z0#megCJW=Vv@UTBCyZ{7_&5blM+fuTM+oldlIq+p6FHAXqg$*M8(rcI?i<}6@o@^A zIVbQ^`!MGLys1WUGH{o|LF~KB?OxLD*OQ>r?yWRi{m|>?bk2^FwPw)rlUmS7d{hy! zg9HP(SVG7%hEIJNZS;tD(MO$UfKHCApotc-5FWWAx`_Q-WQ~ylV|`|;QETw@xm|U$ z{}OfUlHWUr$6= 2) + g_assert_true (hb_subset_input_pin_axis_location (input, face, + HB_TAG ('w','d','t','h'), 0.f)); + hb_face_t *subset = hb_subset_test_create_subset (face, input); + hb_blob_t *blob = hb_face_reference_table (subset, HB_TAG ('g','l','y','f')); + unsigned length; + const unsigned char *data = (const unsigned char *) hb_blob_get_data (blob, &length); + if (!no_hinting) + { + static const unsigned char instructions[] = {0xb0, 1, 0x21}; + g_assert_cmpuint (length, >=, 15); + g_assert_cmpuint (data[0], ==, 0); + g_assert_cmpuint (data[1], ==, 0); + g_assert_cmpuint (data[10], ==, 0); + g_assert_cmpuint (data[11], ==, sizeof (instructions)); + g_assert_cmpmem (data + 12, sizeof (instructions), instructions, sizeof (instructions)); + } + else if (length >= 12) + { + g_assert_cmpuint (data[10], ==, 0); + g_assert_cmpuint (data[11], ==, 0); + } + hb_blob_destroy (blob); + hb_font_t *font = hb_font_create (subset); + hb_ot_font_set_funcs (font); + g_assert_cmpint (hb_font_get_glyph_h_advance (font, 1), ==, + instance == 1 || instance == 2 ? 550 : 500); + hb_font_destroy (font); + hb_face_destroy (subset); + } + hb_face_destroy (face); +} + int main (int argc, char **argv) { @@ -553,6 +600,7 @@ main (int argc, char **argv) hb_test_add (test_subset_glyf_without_gsub); hb_test_add (test_subset_glyf_retain_gids); hb_test_add (test_subset_glyf_retain_gids_truncates); + hb_test_add (test_subset_glyf_zero_contour_instructions); #ifdef HB_EXPERIMENTAL_API hb_test_add (test_subset_glyf_iftb_requirements);