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 000000000..c67330a6d Binary files /dev/null and b/test/api/fonts/empty-hinted.ttf differ diff --git a/test/api/fonts/generate-empty-hinted.py b/test/api/fonts/generate-empty-hinted.py new file mode 100644 index 000000000..07a05cb40 --- /dev/null +++ b/test/api/fonts/generate-empty-hinted.py @@ -0,0 +1,48 @@ +"""Generate a zero-contour glyph with instructions and varying advance.""" + +from pathlib import Path +from fontTools.fontBuilder import FontBuilder +from fontTools.pens.ttGlyphPen import TTGlyphPen +from fontTools.ttLib.tables.ttProgram import Program +from fontTools.ttLib.tables.TupleVariation import TupleVariation + +root = Path(__file__).parent +fb = FontBuilder(1000, isTTF=True) +names = [".notdef", "space", "a"] +fb.setupGlyphOrder(names) +fb.setupCharacterMap({32: "space", 97: "a"}) +glyphs = {} +for name in names: + p = TTGlyphPen(None) + if name == "a": + p.moveTo((0, 0)) + p.lineTo((400, 0)) + p.lineTo((400, 700)) + p.closePath() + glyphs[name] = p.glyph() +program = Program() +program.fromAssembly(["PUSHB[ ]", "1", "POP[ ]"]) +glyphs["space"].program = program +fb.setupGlyf(glyphs) +fb.setupHorizontalMetrics({g: (500, 0) for g in names}) +fb.setupHorizontalHeader(ascent=800, descent=-200) +fb.setupNameTable( + { + "familyName": "Empty Glyph Instructions", + "styleName": "Regular", + "uniqueFontIdentifier": "EmptyGlyphInstructions", + "fullName": "Empty Glyph Instructions", + "psName": "EmptyGlyphInstructions", + } +) +fb.setupOS2(sTypoAscender=800, sTypoDescender=-200, usWinAscent=800, usWinDescent=200) +fb.setupPost() +fb.setupFvar([("wght", 0, 0, 1, "Weight"), ("wdth", 0, 0, 1, "Width")], []) +fb.setupGvar( + {"space": [TupleVariation({"wght": (0, 1, 1)}, [(0, 0), (50, 0), (0, 0), (0, 0)])]} +) +fb.font["maxp"].maxSizeOfInstructions = 3 +fb.font["maxp"].maxStackElements = 1 +fb.font.recalcTimestamp = False +fb.font["head"].created = fb.font["head"].modified = 2082844800 +fb.save(root / "empty-hinted.ttf") diff --git a/test/api/test-subset-glyf.c b/test/api/test-subset-glyf.c index 373b11154..ffdd05b48 100644 --- a/test/api/test-subset-glyf.c +++ b/test/api/test-subset-glyf.c @@ -532,6 +532,53 @@ test_subset_glyf_iftb_requirements (void) // TODO(grieger): test for long loca generation. +static void +test_subset_glyf_zero_contour_instructions (void) +{ + hb_face_t *face = hb_test_open_font_file ("fonts/empty-hinted.ttf"); + /* No instance, partial instance, full instance, and pinning at default. */ + for (unsigned instance = 0; instance < 4; instance++) + for (unsigned no_hinting = 0; no_hinting < 2; no_hinting++) + { + hb_subset_input_t *input = hb_subset_input_create_or_fail (); + hb_set_add (hb_subset_input_glyph_set (input), 1); + hb_subset_input_set_flags (input, no_hinting ? HB_SUBSET_FLAGS_NO_HINTING : HB_SUBSET_FLAGS_DEFAULT); + if (instance) + g_assert_true (hb_subset_input_pin_axis_location (input, face, + HB_TAG ('w','g','h','t'), instance == 3 ? 0.f : 1.f)); + if (instance >= 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);