From d9e166f74c3ba3128c9ef3ccd8d7799e67f14eab Mon Sep 17 00:00:00 2001 From: Khaled Hosny Date: Wed, 18 Oct 2017 20:49:16 +0200 Subject: [PATCH] [ot-font] Implement hb_ot_get_glyph_from_name --- src/hb-ot-font.cc | 22 ++++++++++++++++++- src/hb-ot-post-table.hh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/hb-ot-font.cc b/src/hb-ot-font.cc index 62cef9b..dff27bf 100644 --- a/src/hb-ot-font.cc +++ b/src/hb-ot-font.cc @@ -327,6 +327,15 @@ struct hb_ot_face_post_accelerator_t return this->post->get_glyph_name (glyph, name, size, this->post_len); } + + inline bool get_glyph_from_name (const char *name, int len, + hb_codepoint_t *glyph) const + { + if (unlikely (!name) || unlikely(!len)) + return false; + + return this->post->get_glyph_from_name (name, len, glyph, this->post_len); + } }; typedef bool (*hb_cmap_get_glyph_func_t) (const void *obj, @@ -578,6 +587,17 @@ hb_ot_get_glyph_name (hb_font_t *font HB_UNUSED, } static hb_bool_t +hb_ot_get_glyph_from_name (hb_font_t *font HB_UNUSED, + void *font_data, + const char *name, int len, + hb_codepoint_t *glyph, + void *user_data HB_UNUSED) +{ + const hb_ot_font_t *ot_font = (const hb_ot_font_t *) font_data; + return ot_font->post->get_glyph_from_name (name, len, glyph); +} + +static hb_bool_t hb_ot_get_font_h_extents (hb_font_t *font HB_UNUSED, void *font_data, hb_font_extents_t *metrics, @@ -638,7 +658,7 @@ retry: hb_font_funcs_set_glyph_extents_func (funcs, hb_ot_get_glyph_extents, nullptr, nullptr); //hb_font_funcs_set_glyph_contour_point_func (funcs, hb_ot_get_glyph_contour_point, nullptr, nullptr); TODO hb_font_funcs_set_glyph_name_func (funcs, hb_ot_get_glyph_name, nullptr, nullptr); - //hb_font_funcs_set_glyph_from_name_func (funcs, hb_ot_get_glyph_from_name, nullptr, nullptr); TODO + hb_font_funcs_set_glyph_from_name_func (funcs, hb_ot_get_glyph_from_name, nullptr, nullptr); hb_font_funcs_make_immutable (funcs); diff --git a/src/hb-ot-post-table.hh b/src/hb-ot-post-table.hh index 3240ec6..c0d25f2 100644 --- a/src/hb-ot-post-table.hh +++ b/src/hb-ot-post-table.hh @@ -173,6 +173,64 @@ struct post return false; } + inline bool get_glyph_from_name (const char *name, int len, + hb_codepoint_t *glyph, + unsigned int blob_len) const + { + if (len < 0) + len = strlen (name); + + if (version.to_int () == 0x00010000) + { + for (int i = 0; i < NUM_FORMAT1_NAMES; i++) + { + if (strncmp (name, format1_names[i], len) == 0) + { + *glyph = i; + return true; + } + } + return false; + } + + if (version.to_int () == 0x00020000) + { + const postV2Tail &v2 = StructAfter(*this); + unsigned int offset = min_size + v2.min_size + 2 * v2.numberOfGlyphs; + char* data = (char*) this + offset; + + for (hb_codepoint_t gid = 0; gid < v2.numberOfGlyphs; gid++) + { + unsigned int index = v2.glyphNameIndex[gid]; + if (index >= NUM_FORMAT1_NAMES) + { + for (unsigned int i = 0; data < (char*) this + blob_len; i++) + { + unsigned int name_length = data[0]; + unsigned int remaining = (char*) this + blob_len - data - 1; + name_length = MIN (name_length, remaining); + if (name_length == len && strncmp (name, data + 1, len) == 0) + { + *glyph = gid; + return true; + } + data += name_length + 1; + } + return false; + } + else if (strncmp (name, format1_names[index], len) == 0) + { + *glyph = gid; + return true; + } + } + + return false; + } + + return false; + } + public: FixedVersion<>version; /* 0x00010000 for version 1.0 * 0x00020000 for version 2.0 -- 2.7.4