gsk: Add a way to get a (un)hinted font

Add a function to change the cairo font options of a font to
to the given values while keeping everything else the same.

We use pango api for this if available.

Note that this is not a fully general api, but tailored to the
needs of GSK. We don't allow setting hint-metrics (because it
only influences layout, not rendering) or subpixel-mode (since
we don't have component alpha available).
This commit is contained in:
Matthias Clasen 2024-02-20 18:20:30 -05:00
parent 16a476fe22
commit 0127217e10
2 changed files with 61 additions and 0 deletions

View File

@ -94,3 +94,59 @@ gsk_get_scaled_font (PangoFont *font,
return g_object_ref (font2);
#endif
}
/*< private >
* gsk_get_hinted_font:
* @font: a `PangoFont`
* @hint_style: hint style to use
* @antialias: antialiasing to use
*
* Returns a font that is just like @font, but uses the
* given hinting options for its glyphs and metrics.
*
* Returns: (transfer full): the modified `PangoFont`
*/
PangoFont *
gsk_get_hinted_font (PangoFont *font,
cairo_hint_style_t hint_style,
cairo_antialias_t antialias)
{
cairo_font_options_t *options;
cairo_scaled_font_t *sf;
static PangoContext *context = NULL;
PangoFontDescription *desc G_GNUC_UNUSED;
options = cairo_font_options_create ();
sf = pango_cairo_font_get_scaled_font (PANGO_CAIRO_FONT (font));
cairo_scaled_font_get_font_options (sf, options);
if (cairo_font_options_get_hint_style (options) == hint_style &&
cairo_font_options_get_antialias (options) == antialias &&
cairo_font_options_get_hint_metrics (options) == CAIRO_HINT_METRICS_OFF &&
cairo_font_options_get_subpixel_order (options) == CAIRO_SUBPIXEL_ORDER_DEFAULT)
{
cairo_font_options_destroy (options);
return g_object_ref (font);
}
cairo_font_options_set_hint_style (options, hint_style);
cairo_font_options_set_antialias (options, antialias);
cairo_font_options_set_hint_metrics (options, CAIRO_HINT_METRICS_OFF);
cairo_font_options_set_subpixel_order (options, CAIRO_SUBPIXEL_ORDER_DEFAULT);
if (!context)
context = pango_context_new ();
pango_cairo_context_set_font_options (context, options);
cairo_font_options_destroy (options);
#if PANGO_VERSION_CHECK (1, 52, 0)
return pango_font_map_reload_font (pango_font_get_font_map (font), font, 1.0, context, NULL);
#else
desc = pango_font_describe (font);
font = pango_font_map_load_font (pango_font_get_font_map (font), context, desc);
pango_font_description_free (desc);
return font;
#endif
}

View File

@ -2,6 +2,7 @@
#include <glib.h>
#include <pango/pango.h>
#include <cairo.h>
G_BEGIN_DECLS
@ -10,5 +11,9 @@ void gsk_ensure_resources (void);
PangoFont *gsk_get_scaled_font (PangoFont *font,
float scale);
PangoFont *gsk_get_hinted_font (PangoFont *font,
cairo_hint_style_t hint_style,
cairo_antialias_t antialias);
G_END_DECLS