Skip to content

Commit e1d4d0f

Browse files
committed
Merge branch 'font-extents'
Fixes #165
2 parents 70b33ed + 808d3fc commit e1d4d0f

File tree

9 files changed

+399
-21
lines changed

9 files changed

+399
-21
lines changed

src/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ HBSOURCES = \
4848
hb-ot-hmtx-table.hh \
4949
hb-ot-maxp-table.hh \
5050
hb-ot-name-table.hh \
51+
hb-ot-os2-table.hh \
5152
hb-ot-tag.cc \
5253
hb-private.hh \
5354
hb-set-private.hh \

src/hb-font-private.hh

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
*/
4343

4444
#define HB_FONT_FUNCS_IMPLEMENT_CALLBACKS \
45+
HB_FONT_FUNC_IMPLEMENT (font_h_extents) \
46+
HB_FONT_FUNC_IMPLEMENT (font_v_extents) \
4547
HB_FONT_FUNC_IMPLEMENT (glyph) \
4648
HB_FONT_FUNC_IMPLEMENT (glyph_h_advance) \
4749
HB_FONT_FUNC_IMPLEMENT (glyph_v_advance) \
@@ -160,6 +162,21 @@ struct hb_font_t {
160162
HB_FONT_FUNCS_IMPLEMENT_CALLBACKS
161163
#undef HB_FONT_FUNC_IMPLEMENT
162164

165+
inline hb_bool_t get_font_h_extents (hb_font_extents_t *extents)
166+
{
167+
memset (extents, 0, sizeof (*extents));
168+
return klass->get.f.font_h_extents (this, user_data,
169+
extents,
170+
klass->user_data.font_h_extents);
171+
}
172+
inline hb_bool_t get_font_v_extents (hb_font_extents_t *extents)
173+
{
174+
memset (extents, 0, sizeof (*extents));
175+
return klass->get.f.font_v_extents (this, user_data,
176+
extents,
177+
klass->user_data.font_v_extents);
178+
}
179+
163180
inline bool has_glyph (hb_codepoint_t unicode)
164181
{
165182
hb_codepoint_t glyph;
@@ -265,6 +282,26 @@ struct hb_font_t {
265282

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

285+
inline void get_extents_for_direction (hb_direction_t direction,
286+
hb_font_extents_t *extents)
287+
{
288+
if (likely (HB_DIRECTION_IS_HORIZONTAL (direction))) {
289+
if (!get_font_h_extents (extents))
290+
{
291+
extents->ascender = y_scale * .8;
292+
extents->descender = y_scale - extents->ascender;
293+
extents->line_gap = 0;
294+
}
295+
} else {
296+
if (!get_font_v_extents (extents))
297+
{
298+
extents->ascender = x_scale / 2;
299+
extents->descender = x_scale - extents->ascender;
300+
extents->line_gap = 0;
301+
}
302+
}
303+
}
304+
268305
inline void get_glyph_advance_for_direction (hb_codepoint_t glyph,
269306
hb_direction_t direction,
270307
hb_position_t *x, hb_position_t *y)
@@ -284,7 +321,7 @@ struct hb_font_t {
284321
{
285322
*x = get_glyph_h_advance (glyph) / 2;
286323

287-
/* TODO use font_metrics.ascent */
324+
/* TODO use font_extents.ascender */
288325
*y = y_scale;
289326
}
290327

src/hb-font.cc

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,54 @@
4444
* hb_font_funcs_t
4545
*/
4646

47+
static hb_bool_t
48+
hb_font_get_font_h_extents_nil (hb_font_t *font,
49+
void *font_data HB_UNUSED,
50+
hb_font_extents_t *metrics,
51+
void *user_data HB_UNUSED)
52+
{
53+
memset (metrics, 0, sizeof (*metrics));
54+
return false;
55+
}
56+
static hb_bool_t
57+
hb_font_get_font_h_extents_parent (hb_font_t *font,
58+
void *font_data HB_UNUSED,
59+
hb_font_extents_t *metrics,
60+
void *user_data HB_UNUSED)
61+
{
62+
hb_bool_t ret = font->parent->get_font_h_extents (metrics);
63+
if (ret) {
64+
metrics->ascender = font->parent_scale_y_distance (metrics->ascender);
65+
metrics->descender = font->parent_scale_y_distance (metrics->descender);
66+
metrics->line_gap = font->parent_scale_y_distance (metrics->line_gap);
67+
}
68+
return ret;
69+
}
70+
71+
static hb_bool_t
72+
hb_font_get_font_v_extents_nil (hb_font_t *font,
73+
void *font_data HB_UNUSED,
74+
hb_font_extents_t *metrics,
75+
void *user_data HB_UNUSED)
76+
{
77+
memset (metrics, 0, sizeof (*metrics));
78+
return false;
79+
}
80+
static hb_bool_t
81+
hb_font_get_font_v_extents_parent (hb_font_t *font,
82+
void *font_data HB_UNUSED,
83+
hb_font_extents_t *metrics,
84+
void *user_data HB_UNUSED)
85+
{
86+
hb_bool_t ret = font->parent->get_font_v_extents (metrics);
87+
if (ret) {
88+
metrics->ascender = font->parent_scale_x_distance (metrics->ascender);
89+
metrics->descender = font->parent_scale_x_distance (metrics->descender);
90+
metrics->line_gap = font->parent_scale_x_distance (metrics->line_gap);
91+
}
92+
return ret;
93+
}
94+
4795
static hb_bool_t
4896
hb_font_get_glyph_nil (hb_font_t *font HB_UNUSED,
4997
void *font_data HB_UNUSED,
@@ -280,7 +328,6 @@ hb_font_get_glyph_from_name_parent (hb_font_t *font,
280328
return font->parent->get_glyph_from_name (name, len, glyph);
281329
}
282330

283-
284331
static const hb_font_funcs_t _hb_font_funcs_nil = {
285332
HB_OBJECT_HEADER_STATIC,
286333

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

522569
/* Public getters */
523570

571+
/**
572+
* hb_font_get_h_extents:
573+
* @font: a font.
574+
* @extents: (out):
575+
*
576+
*
577+
*
578+
* Return value:
579+
*
580+
* Since: 1.1.2
581+
**/
582+
hb_bool_t
583+
hb_font_get_h_extents (hb_font_t *font,
584+
hb_font_extents_t *extents)
585+
{
586+
return font->get_font_h_extents (extents);
587+
}
588+
589+
/**
590+
* hb_font_get_v_extents:
591+
* @font: a font.
592+
* @extents: (out):
593+
*
594+
*
595+
*
596+
* Return value:
597+
*
598+
* Since: 1.1.2
599+
**/
600+
hb_bool_t
601+
hb_font_get_v_extents (hb_font_t *font,
602+
hb_font_extents_t *extents)
603+
{
604+
return font->get_font_v_extents (extents);
605+
}
606+
524607
/**
525608
* hb_font_get_glyph:
526609
* @font: a font.
@@ -745,6 +828,23 @@ hb_font_get_glyph_from_name (hb_font_t *font,
745828

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

831+
/**
832+
* hb_font_get_extents_for_direction:
833+
* @font: a font.
834+
* @direction:
835+
* @extents:
836+
*
837+
*
838+
*
839+
* Since: 1.1.2
840+
**/
841+
void
842+
hb_font_get_extents_for_direction (hb_font_t *font,
843+
hb_direction_t direction,
844+
hb_font_extents_t *extents)
845+
{
846+
return font->get_extents_for_direction (direction, extents);
847+
}
748848
/**
749849
* hb_font_get_glyph_advance_for_direction:
750850
* @font: a font.

src/hb-font.h

Lines changed: 59 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,15 @@ HB_EXTERN hb_bool_t
7878
hb_font_funcs_is_immutable (hb_font_funcs_t *ffuncs);
7979

8080

81-
/* glyph extents */
81+
/* font and glyph extents */
82+
83+
/* Note that typically ascender is positive and descender negative in coordinate systems that grow up. */
84+
typedef struct hb_font_extents_t
85+
{
86+
hb_position_t ascender; /* typographic ascender. */
87+
hb_position_t descender; /* typographic descender. */
88+
hb_position_t line_gap; /* suggested line spacing gap. */
89+
} hb_font_extents_t;
8290

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

92-
93100
/* func types */
94101

102+
typedef hb_bool_t (*hb_font_get_font_extents_func_t) (hb_font_t *font, void *font_data,
103+
hb_font_extents_t *metrics,
104+
void *user_data);
105+
typedef hb_font_get_font_extents_func_t hb_font_get_font_h_extents_func_t;
106+
typedef hb_font_get_font_extents_func_t hb_font_get_font_v_extents_func_t;
107+
108+
95109
typedef hb_bool_t (*hb_font_get_glyph_func_t) (hb_font_t *font, void *font_data,
96110
hb_codepoint_t unicode, hb_codepoint_t variation_selector,
97111
hb_codepoint_t *glyph,
@@ -140,6 +154,38 @@ typedef hb_bool_t (*hb_font_get_glyph_from_name_func_t) (hb_font_t *font, void *
140154

141155
/* func setters */
142156

157+
/**
158+
* hb_font_funcs_set_font_h_extents_func:
159+
* @ffuncs: font functions.
160+
* @func: (closure user_data) (destroy destroy) (scope notified):
161+
* @user_data:
162+
* @destroy:
163+
*
164+
*
165+
*
166+
* Since: 1.1.2
167+
**/
168+
HB_EXTERN void
169+
hb_font_funcs_set_font_h_extents_func (hb_font_funcs_t *ffuncs,
170+
hb_font_get_font_h_extents_func_t func,
171+
void *user_data, hb_destroy_func_t destroy);
172+
173+
/**
174+
* hb_font_funcs_set_font_v_extents_func:
175+
* @ffuncs: font functions.
176+
* @func: (closure user_data) (destroy destroy) (scope notified):
177+
* @user_data:
178+
* @destroy:
179+
*
180+
*
181+
*
182+
* Since: 1.1.2
183+
**/
184+
HB_EXTERN void
185+
hb_font_funcs_set_font_v_extents_func (hb_font_funcs_t *ffuncs,
186+
hb_font_get_font_v_extents_func_t func,
187+
void *user_data, hb_destroy_func_t destroy);
188+
143189
/**
144190
* hb_font_funcs_set_glyph_func:
145191
* @ffuncs: font functions.
@@ -316,9 +362,15 @@ hb_font_funcs_set_glyph_from_name_func (hb_font_funcs_t *ffuncs,
316362
hb_font_get_glyph_from_name_func_t func,
317363
void *user_data, hb_destroy_func_t destroy);
318364

319-
320365
/* func dispatch */
321366

367+
HB_EXTERN hb_bool_t
368+
hb_font_get_h_extents (hb_font_t *font,
369+
hb_font_extents_t *extents);
370+
HB_EXTERN hb_bool_t
371+
hb_font_get_v_extents (hb_font_t *font,
372+
hb_font_extents_t *extents);
373+
322374
HB_EXTERN hb_bool_t
323375
hb_font_get_glyph (hb_font_t *font,
324376
hb_codepoint_t unicode, hb_codepoint_t variation_selector,
@@ -369,6 +421,10 @@ hb_font_get_glyph_from_name (hb_font_t *font,
369421

370422
/* high-level funcs, with fallback */
371423

424+
HB_EXTERN void
425+
hb_font_get_extents_for_direction (hb_font_t *font,
426+
hb_direction_t direction,
427+
hb_font_extents_t *extents);
372428
HB_EXTERN void
373429
hb_font_get_glyph_advance_for_direction (hb_font_t *font,
374430
hb_codepoint_t glyph,

src/hb-ft.cc

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,25 @@ hb_ft_get_glyph_from_name (hb_font_t *font HB_UNUSED,
366366
return *glyph != 0;
367367
}
368368

369+
static hb_bool_t
370+
hb_ft_get_font_h_extents (hb_font_t *font HB_UNUSED,
371+
void *font_data,
372+
hb_font_extents_t *metrics,
373+
void *user_data HB_UNUSED)
374+
{
375+
const hb_ft_font_t *ft_font = (const hb_ft_font_t *) font_data;
376+
FT_Face ft_face = ft_font->ft_face;
377+
metrics->ascender = ft_face->size->metrics.ascender;
378+
metrics->descender = ft_face->size->metrics.descender;
379+
metrics->line_gap = ft_face->size->metrics.height - (ft_face->size->metrics.ascender - ft_face->size->metrics.descender);
380+
if (font->y_scale < 0)
381+
{
382+
metrics->ascender = -metrics->ascender;
383+
metrics->descender = -metrics->descender;
384+
metrics->line_gap = -metrics->line_gap;
385+
}
386+
return true;
387+
}
369388

370389
static hb_font_funcs_t *static_ft_funcs = NULL;
371390

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

409+
hb_font_funcs_set_font_h_extents_func (funcs, hb_ft_get_font_h_extents, NULL, NULL);
410+
//hb_font_funcs_set_font_v_extents_func (funcs, hb_ft_get_font_v_extents, NULL, NULL);
390411
hb_font_funcs_set_glyph_func (funcs, hb_ft_get_glyph, NULL, NULL);
391412
hb_font_funcs_set_glyph_h_advance_func (funcs, hb_ft_get_glyph_h_advance, NULL, NULL);
392413
hb_font_funcs_set_glyph_v_advance_func (funcs, hb_ft_get_glyph_v_advance, NULL, NULL);

0 commit comments

Comments
 (0)