diff --git a/src/hb-ot-cff-common.hh b/src/hb-ot-cff-common.hh index 04222eaf3..28b227f97 100644 --- a/src/hb-ot-cff-common.hh +++ b/src/hb-ot-cff-common.hh @@ -280,7 +280,7 @@ struct FDSelect3_4 { auto *range = hb_bsearch (glyph, &ranges[0], nRanges () - 1, sizeof (ranges[0]), _cmp_range); unsigned fd = range ? range->fd : ranges[nRanges () - 1].fd; - hb_codepoint_t end = range ? range[1].first : ranges[nRanges () - 1].first; + hb_codepoint_t end = range ? range[1].first : sentinel (); return {fd, end}; } diff --git a/src/hb-ot-cff2-table.hh b/src/hb-ot-cff2-table.hh index 18440c51e..6c418bdc6 100644 --- a/src/hb-ot-cff2-table.hh +++ b/src/hb-ot-cff2-table.hh @@ -82,6 +82,19 @@ struct CFF2FDSelect default:return 0; } } + /* Returns pair of fd and one after last glyph in range. */ + hb_pair_t get_fd_range (hb_codepoint_t glyph) const + { + if (this == &Null (CFF2FDSelect)) return {0, 1}; + + switch (format) + { + case 0: hb_barrier (); return u.format0.get_fd_range (glyph); + case 3: hb_barrier (); return u.format3.get_fd_range (glyph); + case 4: hb_barrier (); return u.format4.get_fd_range (glyph); + default:return {0, 1}; + } + } bool sanitize (hb_sanitize_context_t *c, unsigned int fdcount, unsigned int charstring_count = HB_CODEPOINT_INVALID) const diff --git a/src/hb-subset-cff-common.cc b/src/hb-subset-cff-common.cc index 57206c608..108bba974 100644 --- a/src/hb-subset-cff-common.cc +++ b/src/hb-subset-cff-common.cc @@ -29,6 +29,7 @@ #ifndef HB_NO_SUBSET_CFF #include "hb-ot-cff-common.hh" +#include "hb-ot-cff1-table.hh" #include "hb-ot-cff2-table.hh" #include "hb-subset-cff-common.hh" @@ -45,10 +46,11 @@ using namespace CFF; * along with a font index remapping table */ +template bool hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, unsigned int fdCount, - const FDSelect &src, /* IN */ + const FDSELECT &src, /* IN */ unsigned int src_num_glyphs, unsigned int &subset_fd_count /* OUT */, unsigned int &subset_fdselect_size /* OUT */, @@ -160,6 +162,28 @@ hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, return true; } +template bool +hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, + unsigned int fdCount, + const CFF1FDSelect &src, /* IN */ + unsigned int src_num_glyphs, + unsigned int &subset_fd_count /* OUT */, + unsigned int &subset_fdselect_size /* OUT */, + unsigned int &subset_fdselect_format /* OUT */, + hb_vector_t &fdselect_ranges /* OUT */, + hb_inc_bimap_t &fdmap /* OUT */); + +template bool +hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, + unsigned int fdCount, + const CFF2FDSelect &src, /* IN */ + unsigned int src_num_glyphs, + unsigned int &subset_fd_count /* OUT */, + unsigned int &subset_fdselect_size /* OUT */, + unsigned int &subset_fdselect_format /* OUT */, + hb_vector_t &fdselect_ranges /* OUT */, + hb_inc_bimap_t &fdmap /* OUT */); + template static inline bool serialize_fdselect_3_4 (hb_serialize_context_t *c, diff --git a/src/hb-subset-cff-common.hh b/src/hb-subset-cff-common.hh index 5f355a2f9..5cbd2774e 100644 --- a/src/hb-subset-cff-common.hh +++ b/src/hb-subset-cff-common.hh @@ -1382,10 +1382,11 @@ struct subr_subsetter_t } /* namespace CFF */ +template HB_INTERNAL bool hb_plan_subset_cff_fdselect (const hb_subset_plan_t *plan, unsigned int fdCount, - const CFF::FDSelect &src, /* IN */ + const FDSELECT &src, /* IN */ unsigned int src_num_glyphs, unsigned int &subset_fd_count /* OUT */, unsigned int &subset_fdselect_size /* OUT */, diff --git a/src/hb-subset-cff1.cc b/src/hb-subset-cff1.cc index d4213383c..96b5c9a62 100644 --- a/src/hb-subset-cff1.cc +++ b/src/hb-subset-cff1.cc @@ -479,6 +479,9 @@ struct cff1_subset_plan { hb_codepoint_t sid = acc.glyph_to_sid (old_glyph, &glyph_to_sid_cache); encoding->get_supplement_codes (sid, supp_codes); + /* Supplement entries name the glyph by SID; emit the renumbered one + * (plan_subset_charset () already added all glyph names to sidmap). */ + sid = sidmap[sid]; for (unsigned int i = 0; i < supp_codes.length; i++) subset_enc_supp_codes.push (code_pair_t {supp_codes[i], sid}); } diff --git a/src/hb-subset-cff2.cc b/src/hb-subset-cff2.cc index 33218fc66..876384fa2 100644 --- a/src/hb-subset-cff2.cc +++ b/src/hb-subset-cff2.cc @@ -1330,7 +1330,7 @@ struct cff2_subset_plan { if (unlikely (!hb_plan_subset_cff_fdselect (plan, orig_fdcount, - *(const FDSelect *)acc.fdSelect, + *acc.fdSelect, acc.num_glyphs, subset_fdcount, subset_fdselect_size, @@ -1645,6 +1645,10 @@ serialize_cff2_to_cff1 (hb_serialize_context_t *c, // CFF2 makes it optional when there's only one FD if (acc.fdSelect != &Null (CFF2FDSelect)) { + // CFF1 has no FDSelect format 4; more than 256 FDs cannot be represented + if (unlikely (plan.subset_fdselect_format == 4)) + return_trace (false); + c->push (); if (likely (hb_serialize_cff_fdselect (c, plan.num_glyphs, *(const FDSelect *)acc.fdSelect, diff --git a/src/test-cff.cc b/src/test-cff.cc index 16e5e90fc..95089e3ec 100644 --- a/src/test-cff.cc +++ b/src/test-cff.cc @@ -150,6 +150,8 @@ test_fd_select_sentinel () select->sentinel () = 4; hb_always_assert (select->sanitize (&c, 1, 3)); hb_always_assert (select->sanitize (&c, 1)); + /* The last range ends at the sentinel. */ + hb_always_assert (select->get_fd_range (2).second == 4); select->sentinel () = 2; hb_always_assert (!select->sanitize (&c, 1, 3)); select->sentinel () = 5; diff --git a/test/api/fonts/cff1_encoding_supplement.42,43.otf b/test/api/fonts/cff1_encoding_supplement.42,43.otf new file mode 100644 index 000000000..fa3889e3f Binary files /dev/null and b/test/api/fonts/cff1_encoding_supplement.42,43.otf differ diff --git a/test/api/fonts/cff1_encoding_supplement.otf b/test/api/fonts/cff1_encoding_supplement.otf new file mode 100644 index 000000000..b81854993 Binary files /dev/null and b/test/api/fonts/cff1_encoding_supplement.otf differ diff --git a/test/api/test-subset-cff1.c b/test/api/test-subset-cff1.c index 0d7156320..6d836e152 100644 --- a/test/api/test-subset-cff1.c +++ b/test/api/test-subset-cff1.c @@ -316,6 +316,28 @@ test_subset_cff1_seac_precheck (void) } } +static void +test_subset_cff1_encoding_supplement (void) +{ + /* Dropping "first" renumbers the custom glyph-name SIDs; the Encoding + * supplement (code 98 -> "second") must follow them. */ + hb_face_t *face = hb_test_open_font_file ("fonts/cff1_encoding_supplement.otf"); + hb_face_t *face_subset = hb_test_open_font_file ("fonts/cff1_encoding_supplement.42,43.otf"); + hb_face_t *face_test; + + hb_set_t *codepoints = hb_set_create (); + hb_set_add (codepoints, 0x42); /* B */ + hb_set_add (codepoints, 0x43); /* C */ + face_test = hb_subset_test_create_subset (face, hb_subset_test_create_input (codepoints)); + hb_set_destroy (codepoints); + + hb_subset_test_check (face_subset, face_test, HB_TAG ('C','F','F',' ')); + + hb_face_destroy (face_test); + hb_face_destroy (face_subset); + hb_face_destroy (face); +} + static void test_subset_cff1_dotsection (void) { @@ -646,6 +668,7 @@ main (int argc, char **argv) hb_test_add (test_subset_cff1_seac); hb_test_add (test_subset_cff1_cid_seac); hb_test_add (test_subset_cff1_seac_precheck); + hb_test_add (test_subset_cff1_encoding_supplement); hb_test_add (test_subset_cff1_dotsection); hb_test_add (test_subset_cff1_retaingids); hb_test_add (test_subset_cff1_j_retaingids); diff --git a/test/api/test-subset-cff2.c b/test/api/test-subset-cff2.c index 1b6a3cd2e..57da4b343 100644 --- a/test/api/test-subset-cff2.c +++ b/test/api/test-subset-cff2.c @@ -161,6 +161,79 @@ test_subset_cff2_retaingids (void) hb_face_destroy (face_ac); } +static void +test_subset_cff2_fdselect_format4 (void) +{ + /* Three glyphs and two Font DICTs: glyphs 0-1 use FD 0, glyph 2 uses FD 1 and + * draws one line through FD 1's local subr 0. The two tables differ only in + * the FDSelect encoding: format 3, or the CFF2-only format 4. */ + static const char maxp_data[] = {0, 0, 0x50, 0, 0, 3}; + static const char cff2_fdselect3_data[] = { + 0x02, 0, 0x05, 0, 0x14, /* CFF2 header */ + 0x1D, 0, 0, 0, 0x1D, 0x11, 0x1D, 0, 0, 0, 0x6D, 0x0C, 0x24, 0x1D, 0, 0, 0, 0x62, 0x0C, 0x25, /* Top DICT: CharStrings, FDArray, FDSelect */ + 0, 0, 0, 0, /* Global Subrs INDEX (empty) */ + 0, 0, 0, 0x03, 0x01, 0x01, 0x16, 0x2B, 0x3D, /* CharStrings INDEX */ + 0x1C, 0, 0, 0x1C, 0, 0, 0x15, 0x1C, 0, 0x0A, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0, 0x0A, 0x05, /* .notdef */ + 0x1C, 0, 0x32, 0x1C, 0, 0, 0x15, 0x1C, 0x01, (char) 0xC2, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0x02, (char) 0xBC, 0x05, /* glyph 1 */ + 0x1C, 0, 0x64, 0x1C, (char) 0xFF, 0x38, 0x15, 0x1C, (char) 0xFF, (char) 0x95, 0x0A, 0x1C, 0, 0, 0x1C, 0x01, 0x2C, 0x05, /* glyph 2: calls FD 1's local subr 0 */ + 0x03, 0, 0x02, 0, 0, 0, 0, 0x02, 0x01, 0, 0x03, /* FDSelect format 3: glyphs 0-1 -> FD 0, glyph 2 -> FD 1 */ + 0, 0, 0, 0x02, 0x01, 0x01, 0x0A, 0x13, /* FDArray INDEX */ + 0x1C, 0, 0x04, 0x1D, 0, 0, 0, (char) 0x87, 0x12, /* Font DICT 0 */ + 0x1C, 0, 0x0A, 0x1D, 0, 0, 0, (char) 0x8B, 0x12, /* Font DICT 1 */ + 0x1C, 0, 0x32, 0x0A, /* Private DICT 0 */ + 0x1C, 0, 0x50, 0x0A, 0x1D, 0, 0, 0, 0x0A, 0x13, /* Private DICT 1: Subrs */ + 0, 0, 0, 0x01, 0x01, 0x01, 0x08, 0x1C, 0, 0x64, 0x1C, 0, 0x64, 0x05, /* FD 1 local Subrs INDEX */ + }; + static const char cff2_fdselect4_data[] = { + 0x02, 0, 0x05, 0, 0x14, /* CFF2 header */ + 0x1D, 0, 0, 0, 0x1D, 0x11, 0x1D, 0, 0, 0, 0x77, 0x0C, 0x24, 0x1D, 0, 0, 0, 0x62, 0x0C, 0x25, /* Top DICT: CharStrings, FDArray, FDSelect */ + 0, 0, 0, 0, /* Global Subrs INDEX (empty) */ + 0, 0, 0, 0x03, 0x01, 0x01, 0x16, 0x2B, 0x3D, /* CharStrings INDEX */ + 0x1C, 0, 0, 0x1C, 0, 0, 0x15, 0x1C, 0, 0x0A, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0, 0x0A, 0x05, /* .notdef */ + 0x1C, 0, 0x32, 0x1C, 0, 0, 0x15, 0x1C, 0x01, (char) 0xC2, 0x1C, 0, 0, 0x05, 0x1C, 0, 0, 0x1C, 0x02, (char) 0xBC, 0x05, /* glyph 1 */ + 0x1C, 0, 0x64, 0x1C, (char) 0xFF, 0x38, 0x15, 0x1C, (char) 0xFF, (char) 0x95, 0x0A, 0x1C, 0, 0, 0x1C, 0x01, 0x2C, 0x05, /* glyph 2: calls FD 1's local subr 0 */ + 0x04, 0, 0, 0, 0x02, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x02, 0, 0x01, 0, 0, 0, 0x03, /* FDSelect format 4: glyphs 0-1 -> FD 0, glyph 2 -> FD 1 */ + 0, 0, 0, 0x02, 0x01, 0x01, 0x0A, 0x13, /* FDArray INDEX */ + 0x1C, 0, 0x04, 0x1D, 0, 0, 0, (char) 0x91, 0x12, /* Font DICT 0 */ + 0x1C, 0, 0x0A, 0x1D, 0, 0, 0, (char) 0x95, 0x12, /* Font DICT 1 */ + 0x1C, 0, 0x32, 0x0A, /* Private DICT 0 */ + 0x1C, 0, 0x50, 0x0A, 0x1D, 0, 0, 0, 0x0A, 0x13, /* Private DICT 1: Subrs */ + 0, 0, 0, 0x01, 0x01, 0x01, 0x08, 0x1C, 0, 0x64, 0x1C, 0, 0x64, 0x05, /* FD 1 local Subrs INDEX */ + }; + + hb_face_t *face_fdselect3 = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face_fdselect3, "maxp", maxp_data); + HB_FACE_ADD_TABLE (face_fdselect3, "CFF2", cff2_fdselect3_data); + + hb_face_t *face_fdselect4 = hb_face_builder_create (); + HB_FACE_ADD_TABLE (face_fdselect4, "maxp", maxp_data); + HB_FACE_ADD_TABLE (face_fdselect4, "CFF2", cff2_fdselect4_data); + + hb_set_t *glyphs = hb_set_create (); + hb_face_t *face_fdselect3_subset, *face_fdselect4_subset; + hb_set_add_range (glyphs, 0, 2); + face_fdselect3_subset = hb_subset_test_create_subset (face_fdselect3, hb_subset_test_create_input_from_glyphs (glyphs)); + face_fdselect4_subset = hb_subset_test_create_subset (face_fdselect4, hb_subset_test_create_input_from_glyphs (glyphs)); + hb_set_destroy (glyphs); + + /* Both retain FD 1; the format-4 source must not have been planned as FD 0 only. */ + hb_subset_test_check (face_fdselect3_subset, face_fdselect4_subset, HB_TAG ('C','F','F','2')); + + hb_font_t *font = hb_font_create (face_fdselect4_subset); + hb_glyph_extents_t extents; + g_assert_true (hb_font_get_glyph_extents (font, 2, &extents)); + g_assert_cmpint (extents.x_bearing, ==, 100); + g_assert_cmpint (extents.y_bearing, ==, 200); + g_assert_cmpint (extents.width, ==, 100); + g_assert_cmpint (extents.height, ==, -400); + hb_font_destroy (font); + + hb_face_destroy (face_fdselect4_subset); + hb_face_destroy (face_fdselect3_subset); + hb_face_destroy (face_fdselect4); + hb_face_destroy (face_fdselect3); +} + #ifdef HB_EXPERIMENTAL_API static void test_subset_cff2_iftb_requirements (void) @@ -469,6 +542,7 @@ main (int argc, char **argv) hb_test_add (test_subset_cff2_desubr); hb_test_add (test_subset_cff2_desubr_strip_hints); hb_test_add (test_subset_cff2_retaingids); + hb_test_add (test_subset_cff2_fdselect_format4); #ifdef HB_EXPERIMENTAL_API hb_test_add (test_subset_cff2_iftb_requirements);