[subset] add an experimental API that can override name strings for specified name_id

This commit is contained in:
Qunxin Liu 2022-10-28 10:44:48 -07:00 committed by Garret Rieger
parent f53ef69d59
commit 6314aa7da4
10 changed files with 208 additions and 56 deletions

View File

@ -23,6 +23,7 @@ if '--experimental-api' not in sys.argv:
hb_subset_input_pin_axis_location hb_subset_input_pin_axis_location
hb_subset_input_pin_axis_to_default hb_subset_input_pin_axis_to_default
hb_subset_preprocess hb_subset_preprocess
hb_subset_input_override_name_table
""".splitlines () """.splitlines ()
symbols = [x for x in symbols if x not in experimental_symbols] symbols = [x for x in symbols if x not in experimental_symbols]
symbols = "\n".join (symbols) symbols = "\n".join (symbols)

View File

@ -30,10 +30,55 @@
#include "hb-open-type.hh" #include "hb-open-type.hh"
#include "hb-ot-name-language.hh" #include "hb-ot-name-language.hh"
#include "hb-aat-layout.hh" #include "hb-aat-layout.hh"
#include "hb-utf.hh"
namespace OT { namespace OT {
template <typename in_utf_t, typename out_utf_t>
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_score var.u16[0]
#define entry_index var.u16[1] #define entry_index var.u16[1]
@ -97,12 +142,39 @@ struct NameRecord
return UNSUPPORTED; 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<unsigned, hb_bytes_t> *name_table_overrides) const
{ {
TRACE_SERIALIZE (this); TRACE_SERIALIZE (this);
auto snap = c->snapshot ();
auto *out = c->embed (this); auto *out = c->embed (this);
if (unlikely (!out)) return_trace (nullptr); 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<hb_utf8_t, hb_utf16_be_t> (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<hb_utf8_t, hb_utf16_be_t> (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); return_trace (out);
} }
@ -216,7 +288,8 @@ struct name
hb_requires (hb_is_source_of (Iterator, const NameRecord &))> hb_requires (hb_is_source_of (Iterator, const NameRecord &))>
bool serialize (hb_serialize_context_t *c, bool serialize (hb_serialize_context_t *c,
Iterator it, Iterator it,
const void *src_string_pool) const void *src_string_pool,
const hb_hashmap_t<unsigned, hb_bytes_t> *name_table_overrides)
{ {
TRACE_SERIALIZE (this); TRACE_SERIALIZE (this);
@ -238,7 +311,7 @@ struct name
records.qsort (); records.qsort ();
c->copy_all (records, src_string_pool); c->copy_all (records, src_string_pool, name_table_overrides);
hb_free (records.arrayZ); 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); return_trace (name_prime->count);
} }

View File

@ -64,52 +64,6 @@ hb_ot_name_list_names (hb_face_t *face,
return (const hb_ot_name_entry_t *) name.names; return (const hb_ot_name_entry_t *) name.names;
} }
template <typename in_utf_t, typename out_utf_t>
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 <typename utf_t> template <typename utf_t>
static inline unsigned int static inline unsigned int
hb_ot_name_get_utf (hb_face_t *face, 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); hb_bytes_t bytes = name.get_name (idx);
if (width == 2) /* UTF16-BE */ if (width == 2) /* UTF16-BE */
return hb_ot_name_convert_utf<hb_utf16_be_t, utf_t> (bytes, text_size, text); return OT::hb_ot_name_convert_utf<hb_utf16_be_t, utf_t> (bytes, text_size, text);
if (width == 1) /* ASCII */ if (width == 1) /* ASCII */
return hb_ot_name_convert_utf<hb_ascii_t, utf_t> (bytes, text_size, text); return OT::hb_ot_name_convert_utf<hb_ascii_t, utf_t> (bytes, text_size, text);
} }
if (text_size) if (text_size)
@ -227,5 +181,4 @@ hb_ot_name_get_utf32 (hb_face_t *face,
return hb_ot_name_get_utf<hb_utf32_t> (face, name_id, language, text_size, text); return hb_ot_name_get_utf<hb_utf32_t> (face, name_id, language, text_size, text);
} }
#endif #endif

View File

@ -49,8 +49,9 @@ hb_subset_input_create_or_fail (void)
set = hb_set_create (); set = hb_set_create ();
input->axes_location = hb_hashmap_create<hb_tag_t, float> (); input->axes_location = hb_hashmap_create<hb_tag_t, float> ();
input->name_table_overrides = hb_hashmap_create<unsigned, hb_bytes_t> ();
if (!input->axes_location || input->in_error ()) if (!input->axes_location || !input->name_table_overrides || input->in_error ())
{ {
hb_subset_input_destroy (input); hb_subset_input_destroy (input);
return nullptr; return nullptr;
@ -248,6 +249,14 @@ hb_subset_input_destroy (hb_subset_input_t *input)
hb_hashmap_destroy (input->axes_location); 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); hb_free (input);
} }
@ -478,4 +487,43 @@ hb_subset_preprocess (hb_face_t *source)
return new_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 #endif

View File

@ -61,6 +61,7 @@ struct hb_subset_input_t
unsigned flags; unsigned flags;
bool attach_accelerator_data = false; bool attach_accelerator_data = false;
hb_hashmap_t<hb_tag_t, float> *axes_location; hb_hashmap_t<hb_tag_t, float> *axes_location;
hb_hashmap_t<unsigned, hb_bytes_t> *name_table_overrides;
inline unsigned num_sets () const inline unsigned num_sets () const
{ {
@ -80,7 +81,7 @@ struct hb_subset_input_t
return true; return true;
} }
return axes_location->in_error (); return axes_location->in_error () || name_table_overrides->in_error ();
} }
}; };

View File

@ -848,6 +848,29 @@ hb_subset_plan_create_or_fail (hb_face_t *face,
plan->check_success (plan->vmtx_map = hb_hashmap_create<unsigned, hb_pair_t<unsigned, int>> ()); plan->check_success (plan->vmtx_map = hb_hashmap_create<unsigned, hb_pair_t<unsigned, int>> ());
plan->check_success (plan->hmtx_map = hb_hashmap_create<unsigned, hb_pair_t<unsigned, int>> ()); plan->check_success (plan->hmtx_map = hb_hashmap_create<unsigned, hb_pair_t<unsigned, int>> ());
plan->check_success (plan->name_table_overrides = hb_hashmap_create<unsigned, hb_bytes_t> ());
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()); void* accel = hb_face_get_user_data(face, hb_subset_accelerator_t::user_data_key());
plan->attach_accelerator_data = input->attach_accelerator_data; plan->attach_accelerator_data = input->attach_accelerator_data;

View File

@ -87,6 +87,13 @@ struct hb_subset_plan_t
hb_hashmap_destroy (vmtx_map); hb_hashmap_destroy (vmtx_map);
hb_hashmap_destroy (layout_variation_idx_delta_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) if (user_axes_location)
{ {
hb_object_destroy (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) //vmtx metrics map: new gid->(advance, lsb)
hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *vmtx_map; hb_hashmap_t<unsigned, hb_pair_t<unsigned, int>> *vmtx_map;
// name table overrides map: name_id->name string new value or None
// to indicate should remove
hb_hashmap_t<unsigned, hb_bytes_t> *name_table_overrides;
const hb_subset_accelerator_t* accelerator; const hb_subset_accelerator_t* accelerator;
public: public:

View File

@ -28,6 +28,7 @@
#define HB_SUBSET_H #define HB_SUBSET_H
#include "hb.h" #include "hb.h"
#include "hb-ot.h"
HB_BEGIN_DECLS HB_BEGIN_DECLS
@ -184,6 +185,12 @@ hb_subset_input_pin_axis_location (hb_subset_input_t *input,
HB_EXTERN hb_face_t * HB_EXTERN hb_face_t *
hb_subset_preprocess (hb_face_t *source); 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 #endif
HB_EXTERN hb_face_t * HB_EXTERN hb_face_t *

Binary file not shown.

View File

@ -67,6 +67,38 @@ test_subset_nameids_with_dup_strs (void)
hb_face_destroy (face_expected); 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 int
main (int argc, char **argv) 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);
hb_test_add (test_subset_nameids_with_dup_strs); 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(); return hb_test_run();
} }