Skip to content

Commit

Permalink
Merge branch 'font-extents'
Browse files Browse the repository at this point in the history
Fixes #165
  • Loading branch information
behdad committed Dec 10, 2015
2 parents 70b33ed + 808d3fc commit e1d4d0f
Show file tree
Hide file tree
Showing 9 changed files with 399 additions and 21 deletions.
1 change: 1 addition & 0 deletions src/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ HBSOURCES = \
hb-ot-hmtx-table.hh \
hb-ot-maxp-table.hh \
hb-ot-name-table.hh \
hb-ot-os2-table.hh \
hb-ot-tag.cc \
hb-private.hh \
hb-set-private.hh \
Expand Down
39 changes: 38 additions & 1 deletion src/hb-font-private.hh
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
*/

#define HB_FONT_FUNCS_IMPLEMENT_CALLBACKS \
HB_FONT_FUNC_IMPLEMENT (font_h_extents) \
HB_FONT_FUNC_IMPLEMENT (font_v_extents) \
HB_FONT_FUNC_IMPLEMENT (glyph) \
HB_FONT_FUNC_IMPLEMENT (glyph_h_advance) \
HB_FONT_FUNC_IMPLEMENT (glyph_v_advance) \
Expand Down Expand Up @@ -160,6 +162,21 @@ struct hb_font_t {
HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
#undef HB_FONT_FUNC_IMPLEMENT

inline hb_bool_t get_font_h_extents (hb_font_extents_t *extents)
{
memset (extents, 0, sizeof (*extents));
return klass->get.f.font_h_extents (this, user_data,
extents,
klass->user_data.font_h_extents);
}
inline hb_bool_t get_font_v_extents (hb_font_extents_t *extents)
{
memset (extents, 0, sizeof (*extents));
return klass->get.f.font_v_extents (this, user_data,
extents,
klass->user_data.font_v_extents);
}

inline bool has_glyph (hb_codepoint_t unicode)
{
hb_codepoint_t glyph;
Expand Down Expand Up @@ -265,6 +282,26 @@ struct hb_font_t {

/* A bit higher-level, and with fallback */

inline void get_extents_for_direction (hb_direction_t direction,
hb_font_extents_t *extents)
{
if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
if (!get_font_h_extents (extents))
{
extents->ascender = y_scale * .8;
extents->descender = y_scale - extents->ascender;
extents->line_gap = 0;
}
} else {
if (!get_font_v_extents (extents))
{
extents->ascender = x_scale / 2;
extents->descender = x_scale - extents->ascender;
extents->line_gap = 0;
}
}
}

inline void get_glyph_advance_for_direction (hb_codepoint_t glyph,
hb_direction_t direction,
hb_position_t *x, hb_position_t *y)
Expand All @@ -284,7 +321,7 @@ struct hb_font_t {
{
*x = get_glyph_h_advance (glyph) / 2;

/* TODO use font_metrics.ascent */
/* TODO use font_extents.ascender */
*y = y_scale;
}

Expand Down
102 changes: 101 additions & 1 deletion src/hb-font.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,54 @@
* hb_font_funcs_t
*/

static hb_bool_t
hb_font_get_font_h_extents_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_font_extents_t *metrics,
void *user_data HB_UNUSED)
{
memset (metrics, 0, sizeof (*metrics));
return false;
}
static hb_bool_t
hb_font_get_font_h_extents_parent (hb_font_t *font,
void *font_data HB_UNUSED,
hb_font_extents_t *metrics,
void *user_data HB_UNUSED)
{
hb_bool_t ret = font->parent->get_font_h_extents (metrics);
if (ret) {
metrics->ascender = font->parent_scale_y_distance (metrics->ascender);
metrics->descender = font->parent_scale_y_distance (metrics->descender);
metrics->line_gap = font->parent_scale_y_distance (metrics->line_gap);
}
return ret;
}

static hb_bool_t
hb_font_get_font_v_extents_nil (hb_font_t *font,
void *font_data HB_UNUSED,
hb_font_extents_t *metrics,
void *user_data HB_UNUSED)
{
memset (metrics, 0, sizeof (*metrics));
return false;
}
static hb_bool_t
hb_font_get_font_v_extents_parent (hb_font_t *font,
void *font_data HB_UNUSED,
hb_font_extents_t *metrics,
void *user_data HB_UNUSED)
{
hb_bool_t ret = font->parent->get_font_v_extents (metrics);
if (ret) {
metrics->ascender = font->parent_scale_x_distance (metrics->ascender);
metrics->descender = font->parent_scale_x_distance (metrics->descender);
metrics->line_gap = font->parent_scale_x_distance (metrics->line_gap);
}
return ret;
}

static hb_bool_t
hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
void *font_data HB_UNUSED,
Expand Down Expand Up @@ -280,7 +328,6 @@ hb_font_get_glyph_from_name_parent (hb_font_t *font,
return font->parent->get_glyph_from_name (name, len, glyph);
}


static const hb_font_funcs_t _hb_font_funcs_nil = {
HB_OBJECT_HEADER_STATIC,

Expand Down Expand Up @@ -521,6 +568,42 @@ hb_font_t::has_func (unsigned int i)

/* Public getters */

/**
* hb_font_get_h_extents:
* @font: a font.
* @extents: (out):
*
*
*
* Return value:
*
* Since: 1.1.2
**/
hb_bool_t
hb_font_get_h_extents (hb_font_t *font,
hb_font_extents_t *extents)
{
return font->get_font_h_extents (extents);
}

/**
* hb_font_get_v_extents:
* @font: a font.
* @extents: (out):
*
*
*
* Return value:
*

This comment has been minimized.

Copy link
@KonstantinRitt

KonstantinRitt Dec 16, 2015

Contributor

actually, 1.1.2 was already released.
1.1.3

* Since: 1.1.2
**/
hb_bool_t
hb_font_get_v_extents (hb_font_t *font,
hb_font_extents_t *extents)
{
return font->get_font_v_extents (extents);
}

/**
* hb_font_get_glyph:
* @font: a font.
Expand Down Expand Up @@ -745,6 +828,23 @@ hb_font_get_glyph_from_name (hb_font_t *font,

/* A bit higher-level, and with fallback */

/**
* hb_font_get_extents_for_direction:
* @font: a font.
* @direction:
* @extents:
*
*
*

This comment has been minimized.

Copy link
@KonstantinRitt

KonstantinRitt Dec 16, 2015

Contributor

ditto

* Since: 1.1.2
**/
void
hb_font_get_extents_for_direction (hb_font_t *font,
hb_direction_t direction,
hb_font_extents_t *extents)
{
return font->get_extents_for_direction (direction, extents);
}
/**
* hb_font_get_glyph_advance_for_direction:
* @font: a font.
Expand Down
62 changes: 59 additions & 3 deletions src/hb-font.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,15 @@ HB_EXTERN hb_bool_t
hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs);


/* glyph extents */
/* font and glyph extents */

/* Note that typically ascender is positive and descender negative in coordinate systems that grow up. */
typedef struct hb_font_extents_t
{
hb_position_t ascender; /* typographic ascender. */
hb_position_t descender; /* typographic descender. */
hb_position_t line_gap; /* suggested line spacing gap. */
} hb_font_extents_t;

This comment has been minimized.

Copy link
@KonstantinRitt

KonstantinRitt Dec 16, 2015

Contributor

Wouldn't sxHeight, xAvgCharWidth and subscript/superscript/strikeout positions be helpful data as well?

This comment has been minimized.

Copy link
@behdad

behdad Jan 6, 2016

Author Member

I'd say italicAngle is the only sensible other thing to add here. xHeight and capHeight are very ill-defined for anything other than Latin/Greek/Cyrillic. xAvgCharWidth is even more ill-defined. What's the correct number for it for a CJK font that includes Latin as well?

We can add the other values in a hb_font_metrics_t opaque structure later, or just add functions for them instead of public structs, since this went into a release already...


/* Note that height is negative in coordinate systems that grow up. */
typedef struct hb_glyph_extents_t
Expand All @@ -89,9 +97,15 @@ typedef struct hb_glyph_extents_t
hb_position_t height; /* distance from top to bottom side. */
} hb_glyph_extents_t;


/* func types */

typedef hb_bool_t (*hb_font_get_font_extents_func_t) (hb_font_t *font, void *font_data,
hb_font_extents_t *metrics,
void *user_data);
typedef hb_font_get_font_extents_func_t hb_font_get_font_h_extents_func_t;
typedef hb_font_get_font_extents_func_t hb_font_get_font_v_extents_func_t;


typedef hb_bool_t (*hb_font_get_glyph_func_t) (hb_font_t *font, void *font_data,
hb_codepoint_t unicode, hb_codepoint_t variation_selector,
hb_codepoint_t *glyph,
Expand Down Expand Up @@ -140,6 +154,38 @@ typedef hb_bool_t (*hb_font_get_glyph_from_name_func_t) (hb_font_t *font, void *

/* func setters */

/**
* hb_font_funcs_set_font_h_extents_func:
* @ffuncs: font functions.
* @func: (closure user_data) (destroy destroy) (scope notified):
* @user_data:
* @destroy:
*
*
*

This comment has been minimized.

Copy link
@KonstantinRitt

KonstantinRitt Dec 16, 2015

Contributor

ditto

* Since: 1.1.2
**/
HB_EXTERN void
hb_font_funcs_set_font_h_extents_func (hb_font_funcs_t *ffuncs,
hb_font_get_font_h_extents_func_t func,
void *user_data, hb_destroy_func_t destroy);

/**
* hb_font_funcs_set_font_v_extents_func:
* @ffuncs: font functions.
* @func: (closure user_data) (destroy destroy) (scope notified):
* @user_data:
* @destroy:
*
*
*

This comment has been minimized.

Copy link
@KonstantinRitt

KonstantinRitt Dec 16, 2015

Contributor

ditto

* Since: 1.1.2
**/
HB_EXTERN void
hb_font_funcs_set_font_v_extents_func (hb_font_funcs_t *ffuncs,
hb_font_get_font_v_extents_func_t func,
void *user_data, hb_destroy_func_t destroy);

/**
* hb_font_funcs_set_glyph_func:
* @ffuncs: font functions.
Expand Down Expand Up @@ -316,9 +362,15 @@ hb_font_funcs_set_glyph_from_name_func (hb_font_funcs_t *ffuncs,
hb_font_get_glyph_from_name_func_t func,
void *user_data, hb_destroy_func_t destroy);


/* func dispatch */

HB_EXTERN hb_bool_t
hb_font_get_h_extents (hb_font_t *font,
hb_font_extents_t *extents);
HB_EXTERN hb_bool_t
hb_font_get_v_extents (hb_font_t *font,
hb_font_extents_t *extents);

HB_EXTERN hb_bool_t
hb_font_get_glyph (hb_font_t *font,
hb_codepoint_t unicode, hb_codepoint_t variation_selector,
Expand Down Expand Up @@ -369,6 +421,10 @@ hb_font_get_glyph_from_name (hb_font_t *font,

/* high-level funcs, with fallback */

HB_EXTERN void
hb_font_get_extents_for_direction (hb_font_t *font,
hb_direction_t direction,
hb_font_extents_t *extents);
HB_EXTERN void
hb_font_get_glyph_advance_for_direction (hb_font_t *font,
hb_codepoint_t glyph,
Expand Down
21 changes: 21 additions & 0 deletions src/hb-ft.cc
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,25 @@ hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
return *glyph != 0;
}

static hb_bool_t
hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
void *font_data,
hb_font_extents_t *metrics,
void *user_data HB_UNUSED)
{
const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
FT_Face ft_face = ft_font->ft_face;
metrics->ascender = ft_face->size->metrics.ascender;
metrics->descender = ft_face->size->metrics.descender;
metrics->line_gap = ft_face->size->metrics.height - (ft_face->size->metrics.ascender - ft_face->size->metrics.descender);
if (font->y_scale < 0)
{
metrics->ascender = -metrics->ascender;
metrics->descender = -metrics->descender;
metrics->line_gap = -metrics->line_gap;
}
return true;
}

static hb_font_funcs_t *static_ft_funcs = NULL;

Expand All @@ -387,6 +406,8 @@ _hb_ft_font_set_funcs (hb_font_t *font, FT_Face ft_face, bool unref)
{
funcs = hb_font_funcs_create ();

hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, NULL, NULL);
//hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, NULL, NULL);
hb_font_funcs_set_glyph_func (funcs, hb_ft_get_glyph, NULL, NULL);
hb_font_funcs_set_glyph_h_advance_func (funcs, hb_ft_get_glyph_h_advance, NULL, NULL);
hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ft_get_glyph_v_advance, NULL, NULL);
Expand Down
Loading

0 comments on commit e1d4d0f

Please sign in to comment.