[post] Minor; towards implementing get_glyph_from_name()

This commit is contained in:
Behdad Esfahbod 2017-10-30 09:46:36 -06:00
parent 977679f229
commit 0f8b5aa1bc
1 changed files with 31 additions and 25 deletions

View File

@ -110,50 +110,56 @@ struct post
index_to_offset.finish (); index_to_offset.finish ();
} }
inline bool get_glyph_name (hb_codepoint_t glyph, struct str_t
char *buf, unsigned int buf_len) const {
inline str_t (void) : bytes (nullptr), len (0) {}
inline str_t (const char *bytes_, unsigned int len_) : bytes (bytes_), len (len_) {}
const char *bytes;
unsigned int len;
};
inline str_t _find_glyph_name (hb_codepoint_t glyph) const
{ {
if (version == 0x00010000) if (version == 0x00010000)
{ {
if (glyph >= NUM_FORMAT1_NAMES) if (glyph >= NUM_FORMAT1_NAMES)
return false; return str_t ();
if (!buf_len) return str_t (format1_names (glyph), strlen (format1_names (glyph)));
return true;
strncpy (buf, format1_names (glyph), buf_len);
buf[buf_len - 1] = '\0';
return true;
} }
if (version != 0x00020000) if (version != 0x00020000 || glyph >= glyphNameIndex->len)
return false; return str_t ();
if (glyph >= glyphNameIndex->len)
return false;
unsigned int index = glyphNameIndex->array[glyph]; unsigned int index = glyphNameIndex->array[glyph];
if (index < NUM_FORMAT1_NAMES) if (index < NUM_FORMAT1_NAMES)
{ return str_t (format1_names (index), strlen (format1_names (index)));
if (!buf_len)
return true;
strncpy (buf, format1_names (index), buf_len);
buf[buf_len - 1] = '\0';
return true;
}
index -= NUM_FORMAT1_NAMES; index -= NUM_FORMAT1_NAMES;
if (index >= index_to_offset.len) if (index >= index_to_offset.len)
return false; return str_t ();
unsigned int offset = index_to_offset.array[index]; unsigned int offset = index_to_offset.array[index];
const uint8_t *data = pool + offset; const uint8_t *data = pool + offset;
unsigned int name_length = *data; unsigned int name_length = *data;
data++; data++;
if (unlikely (!name_length || buf_len <= name_length)) return str_t ((const char *) data, name_length);
return false; }
memcpy (buf, data, name_length);
buf[name_length] = '\0'; inline bool get_glyph_name (hb_codepoint_t glyph,
char *buf, unsigned int buf_len) const
{
str_t s = _find_glyph_name (glyph);
if (!s.len)
return false;
if (!buf_len)
return true;
if (buf_len <= s.len) /* What to do with truncation? Returning false for now. */
return false;
strncpy (buf, s.bytes, s.len);
buf[s.len] = '\0';
return true; return true;
} }