Add hb_font_get_glyph_name() and hb_font_get_glyph_from_name()

This commit is contained in:
Behdad Esfahbod 2012-05-27 11:29:21 -04:00
parent bc145658bd
commit bce095524b
7 changed files with 142 additions and 40 deletions

1
TODO
View File

@ -57,6 +57,7 @@ API additions
=============
- Buffer (de)serialize API ala hb-shape?
- Move feature parsing from util into the library
- Add hb-cairo glue

View File

@ -50,6 +50,8 @@
HB_FONT_FUNC_IMPLEMENT (glyph_v_kerning) \
HB_FONT_FUNC_IMPLEMENT (glyph_extents) \
HB_FONT_FUNC_IMPLEMENT (glyph_contour_point) \
HB_FONT_FUNC_IMPLEMENT (glyph_name) \
HB_FONT_FUNC_IMPLEMENT (glyph_from_name) \
/* ^--- Add new callbacks here */
struct _hb_font_funcs_t {

View File

@ -44,7 +44,7 @@
*/
static hb_bool_t
hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
hb_font_get_glyph_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_codepoint_t unicode,
hb_codepoint_t variation_selector,
@ -59,7 +59,7 @@ hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
}
static hb_position_t
hb_font_get_glyph_h_advance_nil (hb_font_t *font HB_UNUSED,
hb_font_get_glyph_h_advance_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_codepoint_t glyph,
void *user_data HB_UNUSED)
@ -71,7 +71,7 @@ hb_font_get_glyph_h_advance_nil (hb_font_t *font HB_UNUSED,
}
static hb_position_t
hb_font_get_glyph_v_advance_nil (hb_font_t *font HB_UNUSED,
hb_font_get_glyph_v_advance_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_codepoint_t glyph,
void *user_data HB_UNUSED)
@ -83,7 +83,7 @@ hb_font_get_glyph_v_advance_nil (hb_font_t *font HB_UNUSED,
}
static hb_bool_t
hb_font_get_glyph_h_origin_nil (hb_font_t *font HB_UNUSED,
hb_font_get_glyph_h_origin_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_codepoint_t glyph,
hb_position_t *x,
@ -91,9 +91,7 @@ hb_font_get_glyph_h_origin_nil (hb_font_t *font HB_UNUSED,
void *user_data HB_UNUSED)
{
if (font->parent) {
hb_bool_t ret = hb_font_get_glyph_h_origin (font->parent,
glyph,
x, y);
hb_bool_t ret = hb_font_get_glyph_h_origin (font->parent, glyph, x, y);
if (ret)
font->parent_scale_position (x, y);
return ret;
@ -104,7 +102,7 @@ hb_font_get_glyph_h_origin_nil (hb_font_t *font HB_UNUSED,
}
static hb_bool_t
hb_font_get_glyph_v_origin_nil (hb_font_t *font HB_UNUSED,
hb_font_get_glyph_v_origin_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_codepoint_t glyph,
hb_position_t *x,
@ -112,9 +110,7 @@ hb_font_get_glyph_v_origin_nil (hb_font_t *font HB_UNUSED,
void *user_data HB_UNUSED)
{
if (font->parent) {
hb_bool_t ret = hb_font_get_glyph_v_origin (font->parent,
glyph,
x, y);
hb_bool_t ret = hb_font_get_glyph_v_origin (font->parent, glyph, x, y);
if (ret)
font->parent_scale_position (x, y);
return ret;
@ -125,7 +121,7 @@ hb_font_get_glyph_v_origin_nil (hb_font_t *font HB_UNUSED,
}
static hb_position_t
hb_font_get_glyph_h_kerning_nil (hb_font_t *font HB_UNUSED,
hb_font_get_glyph_h_kerning_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_codepoint_t left_glyph,
hb_codepoint_t right_glyph,
@ -138,7 +134,7 @@ hb_font_get_glyph_h_kerning_nil (hb_font_t *font HB_UNUSED,
}
static hb_position_t
hb_font_get_glyph_v_kerning_nil (hb_font_t *font HB_UNUSED,
hb_font_get_glyph_v_kerning_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_codepoint_t top_glyph,
hb_codepoint_t bottom_glyph,
@ -151,7 +147,7 @@ hb_font_get_glyph_v_kerning_nil (hb_font_t *font HB_UNUSED,
}
static hb_bool_t
hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
hb_font_get_glyph_extents_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_codepoint_t glyph,
hb_glyph_extents_t *extents,
@ -173,7 +169,7 @@ hb_font_get_glyph_extents_nil (hb_font_t *font HB_UNUSED,
}
static hb_bool_t
hb_font_get_glyph_contour_point_nil (hb_font_t *font HB_UNUSED,
hb_font_get_glyph_contour_point_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_codepoint_t glyph,
unsigned int point_index,
@ -182,9 +178,7 @@ hb_font_get_glyph_contour_point_nil (hb_font_t *font HB_UNUSED,
void *user_data HB_UNUSED)
{
if (font->parent) {
hb_bool_t ret = hb_font_get_glyph_contour_point (font->parent,
glyph, point_index,
x, y);
hb_bool_t ret = hb_font_get_glyph_contour_point (font->parent, glyph, point_index, x, y);
if (ret)
font->parent_scale_position (x, y);
return ret;
@ -194,6 +188,34 @@ hb_font_get_glyph_contour_point_nil (hb_font_t *font HB_UNUSED,
return FALSE;
}
static hb_bool_t
hb_font_get_glyph_name_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_codepoint_t glyph,
char *name, unsigned int size,
void *user_data HB_UNUSED)
{
if (font->parent)
return hb_font_get_glyph_name (font->parent, glyph, name, size);
snprintf (name, size, "gid%u", glyph);
return FALSE;
}
static hb_bool_t
hb_font_get_glyph_from_name_nil (hb_font_t *font,
void *font_data HB_UNUSED,
const char *name, int len, /* -1 means nul-terminated */
hb_codepoint_t *glyph,
void *user_data HB_UNUSED)
{
if (font->parent)
return hb_font_get_glyph_from_name (font->parent, name, len, glyph);
*glyph = 0;
return FALSE;
}
static hb_font_funcs_t _hb_font_funcs_nil = {
HB_OBJECT_HEADER_STATIC,
@ -405,6 +427,28 @@ hb_font_get_glyph_contour_point (hb_font_t *font,
font->klass->user_data.glyph_contour_point);
}
hb_bool_t
hb_font_get_glyph_name (hb_font_t *font,
hb_codepoint_t glyph,
char *name, unsigned int size)
{
return font->klass->get.glyph_name (font, font->user_data,
glyph,
name, size,
font->klass->user_data.glyph_name);
}
hb_bool_t
hb_font_get_glyph_from_name (hb_font_t *font,
const char *name, int len, /* -1 means nul-terminated */
hb_codepoint_t *glyph)
{
return font->klass->get.glyph_from_name (font, font->user_data,
name, len,
glyph,
font->klass->user_data.glyph_from_name);
}
/* A bit higher-level, and with fallback */

View File

@ -192,6 +192,16 @@ typedef hb_bool_t (*hb_font_get_glyph_contour_point_func_t) (hb_font_t *font, vo
void *user_data);
typedef hb_bool_t (*hb_font_get_glyph_name_func_t) (hb_font_t *font, void *font_data,
hb_codepoint_t glyph,
char *name, unsigned int size,
void *user_data);
typedef hb_bool_t (*hb_font_get_glyph_from_name_func_t) (hb_font_t *font, void *font_data,
const char *name, int len, /* -1 means nul-terminated */
hb_codepoint_t *glyph,
void *user_data);
/* func setters */
void
@ -235,6 +245,15 @@ hb_font_funcs_set_glyph_contour_point_func (hb_font_funcs_t *ffuncs,
hb_font_get_glyph_contour_point_func_t func,
void *user_data, hb_destroy_func_t destroy);
void
hb_font_funcs_set_glyph_name_func (hb_font_funcs_t *ffuncs,
hb_font_get_glyph_name_func_t glyph_func,
void *user_data, hb_destroy_func_t destroy);
void
hb_font_funcs_set_glyph_from_name_func (hb_font_funcs_t *ffuncs,
hb_font_get_glyph_from_name_func_t glyph_func,
void *user_data, hb_destroy_func_t destroy);
/* func dispatch */
@ -276,6 +295,15 @@ hb_font_get_glyph_contour_point (hb_font_t *font,
hb_codepoint_t glyph, unsigned int point_index,
hb_position_t *x, hb_position_t *y);
hb_bool_t
hb_font_get_glyph_name (hb_font_t *font,
hb_codepoint_t glyph,
char *name, unsigned int size);
hb_bool_t
hb_font_get_glyph_from_name (hb_font_t *font,
const char *name, int len, /* -1 means nul-terminated */
hb_codepoint_t *glyph);
/* high-level funcs, with fallback */

View File

@ -231,21 +231,55 @@ hb_ft_get_glyph_contour_point (hb_font_t *font HB_UNUSED,
return TRUE;
}
static hb_bool_t
hb_ft_get_glyph_name (hb_font_t *font,
void *font_data,
hb_codepoint_t glyph,
char *name, unsigned int size,
void *user_data HB_UNUSED)
{
FT_Face ft_face = (FT_Face) font_data;
hb_bool_t ret = !FT_Get_Glyph_Name (ft_face, glyph, name, size);
if (!ret)
snprintf (name, size, "gid%u", glyph);
return ret;
}
static hb_bool_t
hb_ft_get_glyph_from_name (hb_font_t *font,
void *font_data,
const char *name, int len, /* -1 means nul-terminated */
hb_codepoint_t *glyph,
void *user_data HB_UNUSED)
{
FT_Face ft_face = (FT_Face) font_data;
if (len < 0)
*glyph = FT_Get_Name_Index (ft_face, (FT_String *) name);
else {
/* Make a nul-terminated version. */
char buf[128];
len = MIN (len, (int) sizeof (buf) - 1);
strncpy (buf, name, len);
buf[len] = '\0';
*glyph = FT_Get_Name_Index (ft_face, buf);
}
return *glyph != 0;
}
static hb_font_funcs_t ft_ffuncs = {
HB_OBJECT_HEADER_STATIC,
TRUE, /* immutable */
{
hb_ft_get_glyph,
hb_ft_get_glyph_h_advance,
hb_ft_get_glyph_v_advance,
hb_ft_get_glyph_h_origin,
hb_ft_get_glyph_v_origin,
hb_ft_get_glyph_h_kerning,
hb_ft_get_glyph_v_kerning,
hb_ft_get_glyph_extents,
hb_ft_get_glyph_contour_point,
#define HB_FONT_FUNC_IMPLEMENT(name) hb_ft_get_##name,
HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_FONT_FUNC_IMPLEMENT
}
};

View File

@ -78,13 +78,10 @@ struct shape_closure_consumer_t : option_group_t
first = false;
else
printf (" ");
/* TODO refactor this */
char glyph_name[30];
char glyph_name[32];
if (show_glyph_names) {
if (!FT_Get_Glyph_Name (ft_face, i, glyph_name, sizeof (glyph_name)))
printf ("%s", glyph_name);
else
printf ("gid%u", i);
hb_font_get_glyph_name (font, i, glyph_name, sizeof (glyph_name));
printf ("%s", glyph_name);
} else
printf ("%u", i);
}

View File

@ -725,8 +725,6 @@ format_options_t::serialize_glyphs (hb_buffer_t *buffer,
hb_bool_t utf8_clusters,
GString *gs)
{
FT_Face ft_face = show_glyph_names ? hb_ft_font_get_face (font) : NULL;
unsigned int num_glyphs = hb_buffer_get_length (buffer);
hb_glyph_info_t *info = hb_buffer_get_glyph_infos (buffer, NULL);
hb_glyph_position_t *pos = hb_buffer_get_glyph_positions (buffer, NULL);
@ -737,12 +735,10 @@ format_options_t::serialize_glyphs (hb_buffer_t *buffer,
if (i)
g_string_append_c (gs, '|');
char glyph_name[30];
char glyph_name[32];
if (show_glyph_names) {
if (!FT_Get_Glyph_Name (ft_face, info->codepoint, glyph_name, sizeof (glyph_name)))
g_string_append_printf (gs, "%s", glyph_name);
else
g_string_append_printf (gs, "gid%u", info->codepoint);
hb_font_get_glyph_name (font, info->codepoint, glyph_name, sizeof (glyph_name));
g_string_append_printf (gs, "%s", glyph_name);
} else
g_string_append_printf (gs, "%u", info->codepoint);