From 4668a05006e6c4797df19651489b4589817e1e01 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 23 Oct 2018 20:51:53 -0700 Subject: [PATCH] [name] Hook things up Accept Mac Latin name entries as ASCII as well. --- src/hb-ot-name-table.hh | 24 +++++++++++++----- src/hb-ot-name.cc | 8 ++++-- src/hb-utf.hh | 55 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 8 deletions(-) diff --git a/src/hb-ot-name-table.hh b/src/hb-ot-name-table.hh index f2eb49681..26906671d 100644 --- a/src/hb-ot-name-table.hh +++ b/src/hb-ot-name-table.hh @@ -84,7 +84,8 @@ struct NameRecord /* Symbol. */ if (p == 3 && e == 0) return 8; - /* TODO Apple MacRoman (0:1). */ + /* We treat all Mac Latin names as ASCII only. */ + if (p == 1 && e == 0) return 10; /* 10 is magic number :| */ return UNSUPPORTED; } @@ -198,11 +199,15 @@ struct name } this->names.qsort (_hb_ot_name_entry_cmp); - /* Walk and pick best only for each name_id,language pair... */ + /* Walk and pick best only for each name_id,language pair, + * while dropping unsupported encodings. */ unsigned int j = 0; - for (unsigned int i = 1; i < this->names.len; i++) + for (unsigned int i = 0; i < this->names.len; i++) { - if (this->names[i - 1].name_id == this->names[i].name_id && + if (this->names[i].entry_score == UNSUPPORTED) + continue; + if (i && + this->names[i - 1].name_id == this->names[i].name_id && this->names[i - 1].language == this->names[i].language) continue; this->names[j++] = this->names[i]; @@ -217,7 +222,8 @@ struct name } inline int get_index (hb_name_id_t name_id, - hb_language_t language) const + hb_language_t language, + unsigned int *width=nullptr) const { const hb_ot_name_entry_t key = {name_id, {0}, language}; const hb_ot_name_entry_t *entry = (const hb_ot_name_entry_t *) @@ -226,7 +232,13 @@ struct name this->names.len, sizeof (key), _hb_ot_name_entry_cmp_key); - return entry ? entry->entry_index : -1; + if (!entry) + return -1; + + if (width) + *width = entry->entry_score < 10 ? 2 : 1; + + return entry->entry_index; } private: diff --git a/src/hb-ot-name.cc b/src/hb-ot-name.cc index 626e0637a..56f89335b 100644 --- a/src/hb-ot-name.cc +++ b/src/hb-ot-name.cc @@ -106,13 +106,17 @@ hb_ot_name_get_utf (hb_face_t *face, { const OT::name_accelerator_t &name = _get_name (face); - int idx = name.get_index (name_id, language); + unsigned int width; + int idx = name.get_index (name_id, language, &width); if (idx != -1) { hb_bytes_t bytes = name.table->get_name (idx); - if (true /*UTF16-BE*/) + if (width == 2) /* UTF16-BE */ return hb_ot_name_convert_utf (&bytes, text_size, text); + + if (width == 1) /* ASCII */ + return hb_ot_name_convert_utf (&bytes, text_size, text); } if (text_size) diff --git a/src/hb-utf.hh b/src/hb-utf.hh index 5e2faebb5..f78d54995 100644 --- a/src/hb-utf.hh +++ b/src/hb-utf.hh @@ -398,4 +398,59 @@ struct hb_latin1_t } }; + +struct hb_ascii_t +{ + typedef uint8_t codepoint_t; + + static inline const codepoint_t * + next (const codepoint_t *text, + const codepoint_t *end HB_UNUSED, + hb_codepoint_t *unicode, + hb_codepoint_t replacement HB_UNUSED) + { + *unicode = *text++; + if (*unicode >= 0x100) + *unicode = replacement; + return text; + } + + static inline const codepoint_t * + prev (const codepoint_t *text, + const codepoint_t *start HB_UNUSED, + hb_codepoint_t *unicode, + hb_codepoint_t replacement) + { + *unicode = *--text; + if (*unicode >= 0x0080) + *unicode = replacement; + return text; + } + + static inline unsigned int + strlen (const codepoint_t *text) + { + unsigned int l = 0; + while (*text++) l++; + return l; + } + + static inline unsigned int + encode_len (hb_codepoint_t unicode HB_UNUSED) + { + return 1; + } + + static inline codepoint_t * + encode (codepoint_t *text, + const codepoint_t *end HB_UNUSED, + hb_codepoint_t unicode) + { + if (unlikely (unicode >= 0x0080u)) + unicode = '?'; + *text++ = unicode; + return text; + } +}; + #endif /* HB_UTF_HH */