From 6bd549e8df66cc450b40cf26ef56f0ae5d8f37de Mon Sep 17 00:00:00 2001 From: Adrian Johnson Date: Fri, 17 Jun 2022 21:54:30 +0930 Subject: [PATCH] Add color mode option --- src/cairo-font-options.c | 50 ++++++++++++++++++++++++++++++++++++++- src/cairo-ft-font.c | 12 +++++++++- src/cairo-surface.c | 4 +++- src/cairo-types-private.h | 1 + src/cairo.h | 30 +++++++++++++++++++++++ 5 files changed, 94 insertions(+), 3 deletions(-) diff --git a/src/cairo-font-options.c b/src/cairo-font-options.c index 73a1c6b75..30d695894 100644 --- a/src/cairo-font-options.c +++ b/src/cairo-font-options.c @@ -57,6 +57,7 @@ static const cairo_font_options_t _cairo_font_options_nil = { CAIRO_HINT_METRICS_DEFAULT, CAIRO_ROUND_GLYPH_POS_DEFAULT, NULL, /* variations */ + CAIRO_COLOR_MODE_DEFAULT, CAIRO_COLOR_PALETTE_DEFAULT }; @@ -76,6 +77,7 @@ _cairo_font_options_init_default (cairo_font_options_t *options) options->hint_metrics = CAIRO_HINT_METRICS_DEFAULT; options->round_glyph_positions = CAIRO_ROUND_GLYPH_POS_DEFAULT; options->variations = NULL; + options->color_mode = CAIRO_COLOR_MODE_DEFAULT; options->palette_index = CAIRO_COLOR_PALETTE_DEFAULT; } @@ -90,6 +92,7 @@ _cairo_font_options_init_copy (cairo_font_options_t *options, options->hint_metrics = other->hint_metrics; options->round_glyph_positions = other->round_glyph_positions; options->variations = other->variations ? strdup (other->variations) : NULL; + options->color_mode = other->color_mode; options->palette_index = other->palette_index; } @@ -259,6 +262,8 @@ cairo_font_options_merge (cairo_font_options_t *options, } } + if (other->color_mode != CAIRO_COLOR_MODE_DEFAULT) + options->color_mode = other->color_mode; if (other->palette_index != CAIRO_COLOR_PALETTE_DEFAULT) options->palette_index = other->palette_index; } @@ -298,6 +303,7 @@ cairo_font_options_equal (const cairo_font_options_t *options, ((options->variations == NULL && other->variations == NULL) || (options->variations != NULL && other->variations != NULL && strcmp (options->variations, other->variations) == 0)) && + options->color_mode == other->color_mode && options->palette_index == other->palette_index); } slim_hidden_def (cairo_font_options_equal); @@ -333,7 +339,8 @@ cairo_font_options_hash (const cairo_font_options_t *options) (options->subpixel_order << 4) | (options->lcd_filter << 8) | (options->hint_style << 12) | - (options->hint_metrics << 16)) ^ hash; + (options->hint_metrics << 16) | + (options->color_mode << 20)) ^ hash; } slim_hidden_def (cairo_font_options_hash); @@ -631,6 +638,47 @@ cairo_font_options_get_variations (cairo_font_options_t *options) return options->variations; } +/** + * cairo_font_options_set_color_mode: + * @options: a #cairo_font_options_t + * @font_color: the new color mode + * + * Sets the color mode for the font options object. This controls + * whether color fonts are to be rendered in color or as outlines. + * See the documentation for #cairo_color_mode_t for full details. + * + * Since: 1.18 + **/ +cairo_public void +cairo_font_options_set_color_mode (cairo_font_options_t *options, + cairo_color_mode_t color_mode) +{ + if (cairo_font_options_status (options)) + return; + + options->color_mode = color_mode; +} + +/** + * cairo_font_options_get_color_mode: + * @options: a #cairo_font_options_t + * + * Gets the color mode for the font options object. + * See the documentation for #cairo_color_mode_t for full details. + * + * Return value: the color mode for the font options object + * + * Since: 1.18 + **/ +cairo_public cairo_color_mode_t +cairo_font_options_get_color_mode (const cairo_font_options_t *options) +{ + if (cairo_font_options_status ((cairo_font_options_t *) options)) + return CAIRO_COLOR_MODE_DEFAULT; + + return options->color_mode; +} + /** * cairo_font_options_set_color_palette: * @options: a #cairo_font_options_t diff --git a/src/cairo-ft-font.c b/src/cairo-ft-font.c index 41a9a8321..cdb02ff65 100644 --- a/src/cairo-ft-font.c +++ b/src/cairo-ft-font.c @@ -2664,10 +2664,20 @@ _cairo_ft_scaled_glyph_init (void *abstract_font, cairo_bool_t hint_metrics = scaled_font->base.options.hint_metrics != CAIRO_HINT_METRICS_OFF; + /* The font metrics for color glyphs should be the same as the + * outline glyphs. But just in case there aren't, request the + * color or outline metrics based on the font option and if + * the font has color. + */ + int color_flag = 0; +#ifdef FT_LOAD_COLOR + if (unscaled->have_color && scaled_font->base.options.color_mode != CAIRO_COLOR_MODE_NO_COLOR) + color_flag = FT_LOAD_COLOR; +#endif status = _cairo_ft_scaled_glyph_load_glyph (scaled_font, scaled_glyph, face, - load_flags, + load_flags | color_flag, !hint_metrics, vertical_layout); if (unlikely (status)) diff --git a/src/cairo-surface.c b/src/cairo-surface.c index 0bc0b8c7d..105f4bff1 100644 --- a/src/cairo-surface.c +++ b/src/cairo-surface.c @@ -2901,7 +2901,9 @@ _cairo_surface_show_text_glyphs (cairo_surface_t *surface, if (unlikely (status)) return status; - if (_cairo_scaled_font_has_color_glyphs (scaled_font)) { + if (_cairo_scaled_font_has_color_glyphs (scaled_font) && + scaled_font->options.color_mode != CAIRO_COLOR_MODE_NO_COLOR) + { utf8_copy = malloc (sizeof (char) * utf8_len); memcpy (utf8_copy, utf8, sizeof (char) * utf8_len); utf8 = utf8_copy; diff --git a/src/cairo-types-private.h b/src/cairo-types-private.h index f01afcf2d..a9980f3b9 100644 --- a/src/cairo-types-private.h +++ b/src/cairo-types-private.h @@ -195,6 +195,7 @@ struct _cairo_font_options { cairo_hint_metrics_t hint_metrics; cairo_round_glyph_positions_t round_glyph_positions; char *variations; + cairo_color_mode_t color_mode; unsigned int palette_index; }; diff --git a/src/cairo.h b/src/cairo.h index aec16eaed..82e2c69d8 100644 --- a/src/cairo.h +++ b/src/cairo.h @@ -1369,6 +1369,29 @@ typedef enum _cairo_hint_metrics { CAIRO_HINT_METRICS_ON } cairo_hint_metrics_t; +/** + * cairo_color_mode_t: + * @CAIRO_COLOR_MODE_DEFAULT: Use the default color mode for + * font backend and target device, since 1.18. + * @CAIRO_COLOR_MODE_NO_COLOR: Disable rendering color glyphs. Glyphs are + * always rendered as outline glyphs, since 1.18. + * @CAIRO_COLOR_MODE_COLOR: Enable rendering color glyphs. If the font + * contains a color presentation for a glyph, and when supported by + * the font backend, the glyph will be rendered in color, since 1.18. + * + * Specifies if color fonts are to be rendered using the the color + * glyphs or outline glyphs. Glyphs that do not have a color + * presentation, and non-color fonts are not affected by this font + * option. + * + * Since: 1.18 + **/ +typedef enum _cairo_color_mode { + CAIRO_COLOR_MODE_DEFAULT, + CAIRO_COLOR_MODE_NO_COLOR, + CAIRO_COLOR_MODE_COLOR +} cairo_color_mode_t; + /** * cairo_font_options_t: * @@ -1448,6 +1471,13 @@ cairo_font_options_set_variations (cairo_font_options_t *options, #define CAIRO_COLOR_PALETTE_DEFAULT 0 +cairo_public void +cairo_font_options_set_color_mode (cairo_font_options_t *options, + cairo_color_mode_t color_mode); + +cairo_public cairo_color_mode_t +cairo_font_options_get_color_mode (const cairo_font_options_t *options); + cairo_public unsigned int cairo_font_options_get_color_palette (const cairo_font_options_t *options);