diff --git a/src/hb-subset-cff1.cc b/src/hb-subset-cff1.cc index ba71e4cc8..4c2da6b40 100644 --- a/src/hb-subset-cff1.cc +++ b/src/hb-subset-cff1.cc @@ -529,7 +529,7 @@ struct cff1_subset_plan if (it->first == 0) it++; auto _ = *it; bool not_is_cid = !acc.is_CID (); - bool skip = !not_is_cid && glyph_to_sid_map; + bool skip = !not_is_cid && glyph_to_sid_map && !identity_charset; if (not_is_cid) sidmap.alloc (num_glyphs); for (hb_codepoint_t glyph = 1; glyph < num_glyphs; glyph++) @@ -545,12 +545,18 @@ struct cff1_subset_plan /* Retain the SID for the old missing glyph ID */ old_glyph = glyph; } - unsigned sid = glyph_to_sid_map ? - glyph_to_sid_map->arrayZ[old_glyph].code : - acc.glyph_to_sid (old_glyph, &glyph_to_sid_cache); + unsigned sid; + if (identity_charset) + sid = glyph; + else + { + sid = glyph_to_sid_map ? + glyph_to_sid_map->arrayZ[old_glyph].code : + acc.glyph_to_sid (old_glyph, &glyph_to_sid_cache); - if (not_is_cid) - sid = sidmap.add (sid); + if (not_is_cid) + sid = sidmap.add (sid); + } if (sid != last_sid + 1) subset_charset_ranges.push (code_pair_t {sid, glyph}); @@ -621,6 +627,7 @@ struct cff1_subset_plan orig_fdcount = acc.fdCount; drop_hints = plan->flags & HB_SUBSET_FLAGS_NO_HINTING; desubroutinize = plan->flags & HB_SUBSET_FLAGS_DESUBROUTINIZE; + identity_charset = (plan->flags & HB_SUBSET_FLAGS_CFF_IDENTITY_CHARSET) && acc.is_CID (); #ifdef HB_EXPERIMENTAL_API min_charstrings_off_size = (plan->flags & HB_SUBSET_FLAGS_IFTB_REQUIREMENTS) ? 4 : 0; @@ -628,7 +635,7 @@ struct cff1_subset_plan min_charstrings_off_size = 0; #endif - subset_charset = !acc.is_predef_charset (); + subset_charset = !acc.is_predef_charset () || identity_charset; if (!subset_charset) /* check whether the subset renumbers any glyph IDs */ for (const auto &_ : plan->new_to_old_gid_list) @@ -786,6 +793,7 @@ struct cff1_subset_plan unsigned int topDictModSIDs[name_dict_values_t::ValCount]; bool desubroutinize = false; + bool identity_charset = false; unsigned min_charstrings_off_size = 0; }; diff --git a/src/hb-subset.h b/src/hb-subset.h index 6f623273d..705e5524f 100644 --- a/src/hb-subset.h +++ b/src/hb-subset.h @@ -87,6 +87,9 @@ typedef struct hb_subset_plan_t hb_subset_plan_t; * @HB_SUBSET_FLAGS_DOWNGRADE_CFF2: If set and instantiating a variable font * with all axes pinned, convert the output CFF2 table to CFF1. This enables * compatibility with older renderers that don't support CFF2. Since: 13.0.0 + * @HB_SUBSET_FLAGS_CFF_IDENTITY_CHARSET: If set and subsetting a CID-keyed CFF + * font, the output CFF charset will use sequential identity CIDs (CID = new + * GID) rather than preserving the original CIDs. XSince: REPLACEME * * List of boolean properties that can be configured on the subset input. * @@ -111,6 +114,7 @@ typedef enum { /*< flags >*/ HB_SUBSET_FLAGS_RETAIN_NUM_GLYPHS = 0x00002000u, #endif HB_SUBSET_FLAGS_DOWNGRADE_CFF2 = 0x00004000u, + HB_SUBSET_FLAGS_CFF_IDENTITY_CHARSET = 0x00008000u, } hb_subset_flags_t; /** diff --git a/test/api/fonts/SourceHanSans-Regular.41,4C2E.identity.otf b/test/api/fonts/SourceHanSans-Regular.41,4C2E.identity.otf new file mode 100644 index 000000000..ddb4cc6de Binary files /dev/null and b/test/api/fonts/SourceHanSans-Regular.41,4C2E.identity.otf differ diff --git a/test/api/test-subset-cff1.c b/test/api/test-subset-cff1.c index f6a79a9c8..88c79bbb9 100644 --- a/test/api/test-subset-cff1.c +++ b/test/api/test-subset-cff1.c @@ -336,6 +336,54 @@ test_subset_cff1_j_retaingids (void) hb_face_destroy (face_41_4c2e); } +static void +test_subset_cff1_identity_charset (void) +{ + /* The flag has no effect on a name-keyed font, so this is the same output + * as test_subset_cff1(). */ + hb_face_t *face_abc = hb_test_open_font_file ("fonts/SourceSansPro-Regular.abc.otf"); + hb_face_t *face_ac = hb_test_open_font_file ("fonts/SourceSansPro-Regular.ac.otf"); + + hb_set_t *codepoints = hb_set_create (); + hb_subset_input_t *input; + hb_face_t *face_abc_subset; + hb_set_add (codepoints, 'a'); + hb_set_add (codepoints, 'c'); + input = hb_subset_test_create_input (codepoints); + hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_CFF_IDENTITY_CHARSET); + face_abc_subset = hb_subset_test_create_subset (face_abc, input); + hb_set_destroy (codepoints); + + hb_subset_test_check (face_ac, face_abc_subset, HB_TAG ('C','F','F',' ')); + + hb_face_destroy (face_abc_subset); + hb_face_destroy (face_abc); + hb_face_destroy (face_ac); +} + +static void +test_subset_cff1_j_identity_charset (void) +{ + hb_face_t *face_41_3041_4c2e = hb_test_open_font_file ("fonts/SourceHanSans-Regular.41,3041,4C2E.otf"); + hb_face_t *face_41_4c2e = hb_test_open_font_file ("fonts/SourceHanSans-Regular.41,4C2E.identity.otf"); + + hb_set_t *codepoints = hb_set_create (); + hb_subset_input_t *input; + hb_face_t *face_41_3041_4c2e_subset; + hb_set_add (codepoints, 0x41); + hb_set_add (codepoints, 0x4C2E); + input = hb_subset_test_create_input (codepoints); + hb_subset_input_set_flags (input, HB_SUBSET_FLAGS_CFF_IDENTITY_CHARSET); + face_41_3041_4c2e_subset = hb_subset_test_create_subset (face_41_3041_4c2e, input); + hb_set_destroy (codepoints); + + hb_subset_test_check (face_41_4c2e, face_41_3041_4c2e_subset, HB_TAG ('C','F','F',' ')); + + hb_face_destroy (face_41_3041_4c2e_subset); + hb_face_destroy (face_41_3041_4c2e); + hb_face_destroy (face_41_4c2e); +} + #ifdef HB_EXPERIMENTAL_API static void test_subset_cff1_iftb_requirements (void) @@ -383,6 +431,8 @@ main (int argc, char **argv) hb_test_add (test_subset_cff1_dotsection); hb_test_add (test_subset_cff1_retaingids); hb_test_add (test_subset_cff1_j_retaingids); + hb_test_add (test_subset_cff1_identity_charset); + hb_test_add (test_subset_cff1_j_identity_charset); #ifdef HB_EXPERIMENTAL_API hb_test_add (test_subset_cff1_iftb_requirements); diff --git a/util/hb-subset.cc b/util/hb-subset.cc index 09fd73ef6..f2f41fdf4 100644 --- a/util/hb-subset.cc +++ b/util/hb-subset.cc @@ -981,6 +981,7 @@ subset_main_t::add_options () {"retain-gids", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag, "If set don't renumber glyph ids in the subset.", nullptr}, {"desubroutinize", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag, "Remove CFF/CFF2 use of subroutines", nullptr}, {"downgrade-cff2", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag, "Convert instantiated variable fonts from CFF2 to CFF1", nullptr}, + {"cff-identity-charset", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag, "Emit identity CFF charset (CID = new GID) for CID-keyed CFF", nullptr}, {"name-legacy", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag, "Keep legacy (non-Unicode) 'name' table entries", nullptr}, {"set-overlaps-flag", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag, "Set the overlaps flag on each glyph.", nullptr}, {"notdef-outline", 0, G_OPTION_FLAG_NO_ARG, G_OPTION_ARG_CALLBACK, (gpointer) &set_flag, "Keep the outline of \'.notdef\' glyph", nullptr},