[cairo] Add constructor from hb_face_t

This commit is contained in:
Behdad Esfahbod 2022-12-25 19:01:28 -07:00
parent 2e897cc90b
commit cf001f6ec7
3 changed files with 67 additions and 16 deletions

View File

@ -351,6 +351,7 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font,
cairo_text_extents_t *extents); cairo_text_extents_t *extents);
#endif #endif
static const cairo_user_data_key_t hb_cairo_face_user_data_key = {0};
static const cairo_user_data_key_t hb_cairo_font_user_data_key = {0}; static const cairo_user_data_key_t hb_cairo_font_user_data_key = {0};
static cairo_status_t static cairo_status_t
@ -361,6 +362,21 @@ hb_cairo_init_scaled_font (cairo_scaled_font_t *scaled_font,
hb_font_t *font = (hb_font_t *) cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font), hb_font_t *font = (hb_font_t *) cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
&hb_cairo_font_user_data_key); &hb_cairo_font_user_data_key);
if (!font)
{
hb_face_t *face = (hb_face_t *) cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font),
&hb_cairo_face_user_data_key);
font = hb_font_create (face);
// TODO Set variations
// TODO Set (what?) scale and slant
}
cairo_scaled_font_set_user_data (scaled_font,
&hb_cairo_font_user_data_key,
(void *) hb_font_reference (font),
(cairo_destroy_func_t) hb_font_destroy);
hb_position_t x_scale, y_scale; hb_position_t x_scale, y_scale;
hb_font_get_scale (font, &x_scale, &y_scale); hb_font_get_scale (font, &x_scale, &y_scale);
@ -380,8 +396,8 @@ hb_cairo_render_glyph (cairo_scaled_font_t *scaled_font,
cairo_t *cr, cairo_t *cr,
cairo_text_extents_t *extents) cairo_text_extents_t *extents)
{ {
hb_font_t *font = (hb_font_t *) cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font), hb_font_t *font = (hb_font_t *) cairo_scaled_font_get_user_data (scaled_font,
&hb_cairo_font_user_data_key); &hb_cairo_font_user_data_key);
hb_position_t x_scale, y_scale; hb_position_t x_scale, y_scale;
hb_font_get_scale (font, &x_scale, &y_scale); hb_font_get_scale (font, &x_scale, &y_scale);
@ -402,8 +418,8 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font,
cairo_t *cr, cairo_t *cr,
cairo_text_extents_t *extents) cairo_text_extents_t *extents)
{ {
hb_font_t *font = (hb_font_t *) cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font), hb_font_t *font = (hb_font_t *) cairo_scaled_font_get_user_data (scaled_font,
&hb_cairo_font_user_data_key); &hb_cairo_font_user_data_key);
unsigned int palette = 0; unsigned int palette = 0;
#ifdef CAIRO_COLOR_PALETTE_DEFAULT #ifdef CAIRO_COLOR_PALETTE_DEFAULT
@ -434,7 +450,7 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font,
#endif #endif
static cairo_font_face_t * static cairo_font_face_t *
user_font_face_create (hb_font_t *font) user_font_face_create (hb_face_t *face)
{ {
cairo_font_face_t *cairo_face; cairo_font_face_t *cairo_face;
@ -442,21 +458,34 @@ user_font_face_create (hb_font_t *font)
cairo_user_font_face_set_init_func (cairo_face, hb_cairo_init_scaled_font); cairo_user_font_face_set_init_func (cairo_face, hb_cairo_init_scaled_font);
cairo_user_font_face_set_render_glyph_func (cairo_face, hb_cairo_render_glyph); cairo_user_font_face_set_render_glyph_func (cairo_face, hb_cairo_render_glyph);
#ifdef HAVE_CAIRO_USER_FONT_FACE_SET_RENDER_COLOR_GLYPH_FUNC #ifdef HAVE_CAIRO_USER_FONT_FACE_SET_RENDER_COLOR_GLYPH_FUNC
hb_face_t *face = hb_font_get_face (font);
if (hb_ot_color_has_png (face) || hb_ot_color_has_layers (face) || hb_ot_color_has_paint (face)) if (hb_ot_color_has_png (face) || hb_ot_color_has_layers (face) || hb_ot_color_has_paint (face))
cairo_user_font_face_set_render_color_glyph_func (cairo_face, hb_cairo_render_color_glyph); cairo_user_font_face_set_render_color_glyph_func (cairo_face, hb_cairo_render_color_glyph);
#endif #endif
cairo_font_face_set_user_data (cairo_face, cairo_font_face_set_user_data (cairo_face,
&hb_cairo_font_user_data_key, &hb_cairo_face_user_data_key,
(void *) hb_font_reference (font), (void *) hb_face_reference (face),
(cairo_destroy_func_t) hb_font_destroy); (cairo_destroy_func_t) hb_face_destroy);
return cairo_face; return cairo_face;
} }
cairo_font_face_t *
hb_cairo_font_face_create_for_face (hb_face_t *face)
{
hb_face_make_immutable (face);
return user_font_face_create (face);
}
hb_face_t *
hb_cairo_font_face_get_face (cairo_font_face_t *font_face)
{
return (hb_face_t *) cairo_font_face_get_user_data (font_face, &hb_cairo_face_user_data_key);
}
/** /**
* hb_cairo_font_face_create: * hb_cairo_font_face_create_for_font:
* @font: a `hb_font_t` * @font: a `hb_font_t`
* *
* Creates a `cairo_font_face_t` for rendering text according * Creates a `cairo_font_face_t` for rendering text according
@ -469,20 +498,27 @@ user_font_face_create (hb_font_t *font)
* Since: REPLACEME * Since: REPLACEME
*/ */
cairo_font_face_t * cairo_font_face_t *
hb_cairo_font_face_create (hb_font_t *font) hb_cairo_font_face_create_for_font (hb_font_t *font)
{ {
hb_font_make_immutable (font); hb_font_make_immutable (font);
return user_font_face_create (font); auto *cr_face = user_font_face_create (font->face);
cairo_font_face_set_user_data (cr_face,
&hb_cairo_font_user_data_key,
(void *) hb_font_reference (font),
(cairo_destroy_func_t) hb_font_destroy);
return cr_face;
} }
/** /**
* hb_cairo_font_face_get_font: * hb_cairo_font_face_get_font:
* @font_face: a `cairo_font_face_t` * @font_face: a `cairo_font_face_t`
* *
* Gets the `hb_font_t` that @font_face was created for. * Gets the `hb_font_t` that @font_face was created from.
* *
* Returns: (nullable): the `hb_font_t` that @font_face was created for * Returns: (nullable): the `hb_font_t` that @font_face was created from
* *
* Since: REPLACEME * Since: REPLACEME
*/ */
@ -492,6 +528,12 @@ hb_cairo_font_face_get_font (cairo_font_face_t *font_face)
return (hb_font_t *) cairo_font_face_get_user_data (font_face, &hb_cairo_font_user_data_key); return (hb_font_t *) cairo_font_face_get_user_data (font_face, &hb_cairo_font_user_data_key);
} }
hb_font_t *
hb_cairo_scaled_font_get_font (cairo_scaled_font_t *scaled_font)
{
return (hb_font_t *) cairo_scaled_font_get_user_data (scaled_font, &hb_cairo_font_user_data_key);
}
/** /**
* hb_cairo_glyphs_from_buffer: * hb_cairo_glyphs_from_buffer:
* @buffer: a `hb_buffer_t` containing glyphs * @buffer: a `hb_buffer_t` containing glyphs

View File

@ -34,11 +34,20 @@
HB_BEGIN_DECLS HB_BEGIN_DECLS
HB_EXTERN cairo_font_face_t * HB_EXTERN cairo_font_face_t *
hb_cairo_font_face_create (hb_font_t *font); hb_cairo_font_face_create_for_face (hb_face_t *face);
HB_EXTERN hb_face_t *
hb_cairo_font_face_get_face (cairo_font_face_t *font_face);
HB_EXTERN cairo_font_face_t *
hb_cairo_font_face_create_for_font (hb_font_t *font);
HB_EXTERN hb_font_t * HB_EXTERN hb_font_t *
hb_cairo_font_face_get_font (cairo_font_face_t *font_face); hb_cairo_font_face_get_font (cairo_font_face_t *font_face);
HB_EXTERN hb_font_t *
hb_cairo_scaled_font_get_font (cairo_scaled_font_t *scaled_font);
HB_EXTERN void HB_EXTERN void
hb_cairo_glyphs_from_buffer (hb_buffer_t *buffer, hb_cairo_glyphs_from_buffer (hb_buffer_t *buffer,
const char *text, const char *text,

View File

@ -100,7 +100,7 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts)
cairo_font_face_t *cairo_face; cairo_font_face_t *cairo_face;
if (use_hb_draw) if (use_hb_draw)
cairo_face = hb_cairo_font_face_create (font); cairo_face = hb_cairo_font_face_create_for_font (font);
#ifdef HAVE_CAIRO_FT #ifdef HAVE_CAIRO_FT
else else
cairo_face = helper_cairo_create_ft_font_face (font_opts); cairo_face = helper_cairo_create_ft_font_face (font_opts);