diff --git a/src/gen-def.py b/src/gen-def.py index e751f524e..f6b98bfd2 100755 --- a/src/gen-def.py +++ b/src/gen-def.py @@ -23,6 +23,7 @@ if '--experimental-api' not in sys.argv: hb_subset_input_pin_axis_location hb_subset_input_pin_axis_to_default hb_subset_preprocess +hb_subset_input_override_name_table """.splitlines () symbols = [x for x in symbols if x not in experimental_symbols] symbols = "\n".join (symbols) diff --git a/src/hb-ot-name-table.hh b/src/hb-ot-name-table.hh index 1f2131ffc..b63559101 100644 --- a/src/hb-ot-name-table.hh +++ b/src/hb-ot-name-table.hh @@ -30,10 +30,55 @@ #include "hb-open-type.hh" #include "hb-ot-name-language.hh" #include "hb-aat-layout.hh" +#include "hb-utf.hh" namespace OT { +template +inline unsigned int +hb_ot_name_convert_utf (hb_bytes_t bytes, + unsigned int *text_size /* IN/OUT */, + typename out_utf_t::codepoint_t *text /* OUT */) +{ + unsigned int src_len = bytes.length / sizeof (typename in_utf_t::codepoint_t); + const typename in_utf_t::codepoint_t *src = (const typename in_utf_t::codepoint_t *) bytes.arrayZ; + const typename in_utf_t::codepoint_t *src_end = src + src_len; + + typename out_utf_t::codepoint_t *dst = text; + + hb_codepoint_t unicode; + const hb_codepoint_t replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT; + + if (text_size && *text_size) + { + (*text_size)--; /* Same room for NUL-termination. */ + const typename out_utf_t::codepoint_t *dst_end = text + *text_size; + + while (src < src_end && dst < dst_end) + { + const typename in_utf_t::codepoint_t *src_next = in_utf_t::next (src, src_end, &unicode, replacement); + typename out_utf_t::codepoint_t *dst_next = out_utf_t::encode (dst, dst_end, unicode); + if (dst_next == dst) + break; /* Out-of-room. */ + + dst = dst_next; + src = src_next; + } + + *text_size = dst - text; + *dst = 0; /* NUL-terminate. */ + } + + /* Accumulate length of rest. */ + unsigned int dst_len = dst - text; + while (src < src_end) + { + src = in_utf_t::next (src, src_end, &unicode, replacement); + dst_len += out_utf_t::encode_len (unicode); + } + return dst_len; +} #define entry_score var.u16[0] #define entry_index var.u16[1] @@ -97,12 +142,39 @@ struct NameRecord return UNSUPPORTED; } - NameRecord* copy (hb_serialize_context_t *c, const void *base) const + NameRecord* copy (hb_serialize_context_t *c, const void *base, + const hb_hashmap_t *name_table_overrides) const { TRACE_SERIALIZE (this); + auto snap = c->snapshot (); auto *out = c->embed (this); if (unlikely (!out)) return_trace (nullptr); - out->offset.serialize_copy (c, offset, base, 0, hb_serialize_context_t::Tail, length); + if (name_table_overrides->has (nameID)) { + hb_bytes_t name_bytes = name_table_overrides->get (nameID); + char *name_str_utf16_be = (char *) hb_calloc ((name_bytes.length + 1) * 4, 1); + unsigned text_size = hb_ot_name_convert_utf (name_bytes, nullptr, + (hb_utf16_be_t::codepoint_t *) name_str_utf16_be); + + text_size++; // needs to consider NULL terminator for use in hb_ot_name_convert_utf() + hb_ot_name_convert_utf (name_bytes, &text_size, + (hb_utf16_be_t::codepoint_t *) name_str_utf16_be); + + unsigned encoded_byte_len = text_size * hb_utf16_be_t::codepoint_t::static_size; + if (!encoded_byte_len || !c->check_assign (out->length, encoded_byte_len, HB_SERIALIZE_ERROR_INT_OVERFLOW)) { + c->revert (snap); + hb_free (name_str_utf16_be); + return_trace (nullptr); + } + + hb_bytes_t utf16_be_bytes (name_str_utf16_be, encoded_byte_len); + out->offset = 0; + c->push (); + utf16_be_bytes.copy (c); + c->add_link (out->offset, c->pop_pack (), hb_serialize_context_t::Tail, 0); + hb_free (name_str_utf16_be); + } else { + out->offset.serialize_copy (c, offset, base, 0, hb_serialize_context_t::Tail, length); + } return_trace (out); } @@ -216,7 +288,8 @@ struct name hb_requires (hb_is_source_of (Iterator, const NameRecord &))> bool serialize (hb_serialize_context_t *c, Iterator it, - const void *src_string_pool) + const void *src_string_pool, + const hb_hashmap_t *name_table_overrides) { TRACE_SERIALIZE (this); @@ -238,7 +311,7 @@ struct name records.qsort (); - c->copy_all (records, src_string_pool); + c->copy_all (records, src_string_pool, name_table_overrides); hb_free (records.arrayZ); @@ -267,7 +340,7 @@ struct name }) ; - name_prime->serialize (c->serializer, it, std::addressof (this + stringOffset)); + name_prime->serialize (c->serializer, it, std::addressof (this + stringOffset), c->plan->name_table_overrides); return_trace (name_prime->count); } diff --git a/src/hb-ot-name.cc b/src/hb-ot-name.cc index 3051828ae..6adf1e8fb 100644 --- a/src/hb-ot-name.cc +++ b/src/hb-ot-name.cc @@ -64,52 +64,6 @@ hb_ot_name_list_names (hb_face_t *face, return (const hb_ot_name_entry_t *) name.names; } - -template -static inline unsigned int -hb_ot_name_convert_utf (hb_bytes_t bytes, - unsigned int *text_size /* IN/OUT */, - typename out_utf_t::codepoint_t *text /* OUT */) -{ - unsigned int src_len = bytes.length / sizeof (typename in_utf_t::codepoint_t); - const typename in_utf_t::codepoint_t *src = (const typename in_utf_t::codepoint_t *) bytes.arrayZ; - const typename in_utf_t::codepoint_t *src_end = src + src_len; - - typename out_utf_t::codepoint_t *dst = text; - - hb_codepoint_t unicode; - const hb_codepoint_t replacement = HB_BUFFER_REPLACEMENT_CODEPOINT_DEFAULT; - - if (text_size && *text_size) - { - (*text_size)--; /* Save room for NUL-termination. */ - const typename out_utf_t::codepoint_t *dst_end = text + *text_size; - - while (src < src_end && dst < dst_end) - { - const typename in_utf_t::codepoint_t *src_next = in_utf_t::next (src, src_end, &unicode, replacement); - typename out_utf_t::codepoint_t *dst_next = out_utf_t::encode (dst, dst_end, unicode); - if (dst_next == dst) - break; /* Out-of-room. */ - - dst = dst_next; - src = src_next; - } - - *text_size = dst - text; - *dst = 0; /* NUL-terminate. */ - } - - /* Accumulate length of rest. */ - unsigned int dst_len = dst - text; - while (src < src_end) - { - src = in_utf_t::next (src, src_end, &unicode, replacement); - dst_len += out_utf_t::encode_len (unicode); - } - return dst_len; -} - template static inline unsigned int hb_ot_name_get_utf (hb_face_t *face, @@ -130,10 +84,10 @@ hb_ot_name_get_utf (hb_face_t *face, hb_bytes_t bytes = name.get_name (idx); if (width == 2) /* UTF16-BE */ - return hb_ot_name_convert_utf (bytes, text_size, text); + return OT::hb_ot_name_convert_utf (bytes, text_size, text); if (width == 1) /* ASCII */ - return hb_ot_name_convert_utf (bytes, text_size, text); + return OT::hb_ot_name_convert_utf (bytes, text_size, text); } if (text_size) @@ -227,5 +181,4 @@ hb_ot_name_get_utf32 (hb_face_t *face, return hb_ot_name_get_utf (face, name_id, language, text_size, text); } - #endif diff --git a/src/hb-subset-input.cc b/src/hb-subset-input.cc index fd250104b..a13129ce2 100644 --- a/src/hb-subset-input.cc +++ b/src/hb-subset-input.cc @@ -49,8 +49,9 @@ hb_subset_input_create_or_fail (void) set = hb_set_create (); input->axes_location = hb_hashmap_create (); + input->name_table_overrides = hb_hashmap_create (); - if (!input->axes_location || input->in_error ()) + if (!input->axes_location || !input->name_table_overrides || input->in_error ()) { hb_subset_input_destroy (input); return nullptr; @@ -248,6 +249,14 @@ hb_subset_input_destroy (hb_subset_input_t *input) hb_hashmap_destroy (input->axes_location); + if (input->name_table_overrides) + { + for (auto _ : input->name_table_overrides->values ()) + _.fini (); + } + + hb_hashmap_destroy (input->name_table_overrides); + hb_free (input); } @@ -478,4 +487,43 @@ hb_subset_preprocess (hb_face_t *source) return new_source; } + +/** + * hb_subset_input_override_name_table: + * @input: a #hb_subset_input_t object. + * @name_id: name_id of a nameRecord + * @name_str: pointer to name string new value or null to indicate should remove + * @str_len: the size of @name_str, or -1 if it is `NULL`-terminated + * + * Override the name string of a nameRecord with specified name_id + * Since: EXPERIMENTAL + **/ +HB_EXTERN void +hb_subset_input_override_name_table (hb_subset_input_t *input, + hb_ot_name_id_t name_id, + const char *name_str, + int str_len /* -1 means nul-terminated */) +{ + if (!name_str) + { + hb_set_del (hb_subset_input_set(input, HB_SUBSET_SETS_NAME_ID), name_id); + return; + } + + if (str_len == -1) + str_len = strlen (name_str); + + if (!str_len) + { + hb_set_del (hb_subset_input_set(input, HB_SUBSET_SETS_NAME_ID), name_id); + return; + } + + char *override_name = (char *) hb_malloc (str_len); + if (unlikely (!override_name)) return; + + strncpy (override_name, name_str, str_len); + input->name_table_overrides->set (name_id, hb_bytes_t (override_name, str_len)); +} + #endif diff --git a/src/hb-subset-input.hh b/src/hb-subset-input.hh index dabb4918f..8e8cf5402 100644 --- a/src/hb-subset-input.hh +++ b/src/hb-subset-input.hh @@ -61,6 +61,7 @@ struct hb_subset_input_t unsigned flags; bool attach_accelerator_data = false; hb_hashmap_t *axes_location; + hb_hashmap_t *name_table_overrides; inline unsigned num_sets () const { @@ -80,7 +81,7 @@ struct hb_subset_input_t return true; } - return axes_location->in_error (); + return axes_location->in_error () || name_table_overrides->in_error (); } }; diff --git a/src/hb-subset-plan.cc b/src/hb-subset-plan.cc index 9cf7c9e43..81fc56a3f 100644 --- a/src/hb-subset-plan.cc +++ b/src/hb-subset-plan.cc @@ -848,6 +848,29 @@ hb_subset_plan_create_or_fail (hb_face_t *face, plan->check_success (plan->vmtx_map = hb_hashmap_create> ()); plan->check_success (plan->hmtx_map = hb_hashmap_create> ()); + plan->check_success (plan->name_table_overrides = hb_hashmap_create ()); + if (plan->name_table_overrides && input->name_table_overrides) + { + for (auto _ : *input->name_table_overrides) + { + unsigned name_id = _.first; + hb_bytes_t name_bytes = _.second; + unsigned len = name_bytes.length; + + char *name_str = (char *) hb_malloc (len); + if (unlikely (!plan->check_success (name_str))) + { + for (auto bytes : plan->name_table_overrides->values ()) + bytes.fini (); + break; + } + + strncpy (name_str, name_bytes.arrayZ, len); + plan->name_table_overrides->set (name_id, hb_bytes_t (name_str, len)); + } + } + + void* accel = hb_face_get_user_data(face, hb_subset_accelerator_t::user_data_key()); plan->attach_accelerator_data = input->attach_accelerator_data; diff --git a/src/hb-subset-plan.hh b/src/hb-subset-plan.hh index 15fabba9c..2e1997a18 100644 --- a/src/hb-subset-plan.hh +++ b/src/hb-subset-plan.hh @@ -87,6 +87,13 @@ struct hb_subset_plan_t hb_hashmap_destroy (vmtx_map); hb_hashmap_destroy (layout_variation_idx_delta_map); + if (name_table_overrides) + { + for (auto _ : name_table_overrides->values ()) + _.fini (); + } + hb_hashmap_destroy (name_table_overrides); + if (user_axes_location) { hb_object_destroy (user_axes_location); @@ -191,6 +198,10 @@ struct hb_subset_plan_t //vmtx metrics map: new gid->(advance, lsb) hb_hashmap_t> *vmtx_map; + // name table overrides map: name_id->name string new value or None + // to indicate should remove + hb_hashmap_t *name_table_overrides; + const hb_subset_accelerator_t* accelerator; public: diff --git a/src/hb-subset.h b/src/hb-subset.h index 6a2c5f611..7ea965924 100644 --- a/src/hb-subset.h +++ b/src/hb-subset.h @@ -28,6 +28,7 @@ #define HB_SUBSET_H #include "hb.h" +#include "hb-ot.h" HB_BEGIN_DECLS @@ -184,6 +185,12 @@ hb_subset_input_pin_axis_location (hb_subset_input_t *input, HB_EXTERN hb_face_t * hb_subset_preprocess (hb_face_t *source); +HB_EXTERN void +hb_subset_input_override_name_table (hb_subset_input_t *input, + hb_ot_name_id_t name_id, + const char *name_str, + int str_len); + #endif HB_EXTERN hb_face_t * diff --git a/test/api/fonts/nameID.override.expected.ttf b/test/api/fonts/nameID.override.expected.ttf new file mode 100644 index 000000000..14172bbca Binary files /dev/null and b/test/api/fonts/nameID.override.expected.ttf differ diff --git a/test/api/test-subset-nameids.c b/test/api/test-subset-nameids.c index b58a86c91..5874aaad1 100644 --- a/test/api/test-subset-nameids.c +++ b/test/api/test-subset-nameids.c @@ -67,6 +67,38 @@ test_subset_nameids_with_dup_strs (void) hb_face_destroy (face_expected); } +#ifdef HB_EXPERIMENTAL_API +static void +test_subset_name_overrides (void) +{ + hb_face_t *face_origin = hb_test_open_font_file ("fonts/nameID.origin.ttf"); + hb_face_t *face_expected = hb_test_open_font_file ("fonts/nameID.override.expected.ttf"); + + char str1[] = "Roboto Test"; + char str2[] = "Bold"; + char str6[] = "Roboto-Bold"; + + hb_set_t *name_ids = hb_set_create(); + hb_face_t *face_subset; + hb_set_add_range (name_ids, 0, 15); + + hb_subset_input_t *subset_input = hb_subset_test_create_input_from_nameids (name_ids); + hb_subset_input_override_name_table (subset_input, 1, str1, -1); + hb_subset_input_override_name_table (subset_input, 2, str2, 4); + hb_subset_input_override_name_table (subset_input, 6, str6, -1); + hb_subset_input_override_name_table (subset_input, 14, NULL, -1); + + face_subset = hb_subset_test_create_subset (face_origin, subset_input); + hb_set_destroy (name_ids); + + hb_subset_test_check (face_expected, face_subset, HB_TAG ('n','a','m','e')); + + hb_face_destroy (face_subset); + hb_face_destroy (face_origin); + hb_face_destroy (face_expected); +} +#endif + int main (int argc, char **argv) { @@ -74,6 +106,9 @@ main (int argc, char **argv) hb_test_add (test_subset_nameids); hb_test_add (test_subset_nameids_with_dup_strs); +#ifdef HB_EXPERIMENTAL_API + hb_test_add (test_subset_name_overrides); +#endif return hb_test_run(); }