From ab37ade7e46ac00152113c275dd8cd7fc9d1a11c Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 18 Jan 2023 23:29:37 -0500 Subject: [PATCH 01/27] Hook up custom palettes for cairo --- src/hb-cairo.cc | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index a0b4f3bf2..67abd527b 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -306,6 +306,41 @@ hb_cairo_paint_sweep_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, _hb_cairo_paint_sweep_gradient (cr, color_line, x0, y0, start_angle, end_angle); } +static hb_color_t +hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, + void *paint_data, + unsigned int color_index, + void *user_data HB_UNUSED) +{ + cairo_t *cr = (cairo_t *) paint_data; + +#ifdef CAIRO_COLOR_PALETTE_CUSTOM + cairo_font_options_t *options; + double red, green, blue, alpha; + + options = cairo_font_options_create (); + cairo_get_font_options (cr, options); + if (cairo_font_options_get_custom_palette_color (options, color_index, + &red, &green, &blue, &alpha)) + { + cairo_font_options_destroy (options); + return HB_COLOR (255 * blue, 255 * green, 255 * red, 255 * alpha); + } + cairo_font_options_destroy (options); +#endif + + // Fall back to the default palette + cairo_scaled_font_t *scaled_font = cairo_get_scaled_font (cr); + hb_font_t *font = hb_cairo_scaled_font_get_font (scaled_font); + hb_face_t *face = hb_font_get_face (font); + hb_color_t color; + unsigned int len = 1; + + hb_ot_color_palette_get_colors (face, 0, color_index, &len, &color); + + return color; +} + static inline void free_static_cairo_paint_funcs (); static struct hb_cairo_paint_funcs_lazy_loader_t : hb_paint_funcs_lazy_loader_t @@ -326,6 +361,7 @@ static struct hb_cairo_paint_funcs_lazy_loader_t : hb_paint_funcs_lazy_loader_t< hb_paint_funcs_set_linear_gradient_func (funcs, hb_cairo_paint_linear_gradient, nullptr, nullptr); hb_paint_funcs_set_radial_gradient_func (funcs, hb_cairo_paint_radial_gradient, nullptr, nullptr); hb_paint_funcs_set_sweep_gradient_func (funcs, hb_cairo_paint_sweep_gradient, nullptr, nullptr); + hb_paint_funcs_set_custom_palette_color_func (funcs, hb_cairo_paint_custom_palette_color, nullptr, nullptr); hb_paint_funcs_make_immutable (funcs); From c41892a01229404d4d0c31b8056fd7b72ac3a58a Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Wed, 18 Jan 2023 23:45:53 -0500 Subject: [PATCH 02/27] hb-view: Add a --custom-palette option --- util/font-options.hh | 3 +++ util/helper-cairo.hh | 12 ++++++++++++ 2 files changed, 15 insertions(+) diff --git a/util/font-options.hh b/util/font-options.hh index b1a0169c6..5f99a37ef 100644 --- a/util/font-options.hh +++ b/util/font-options.hh @@ -49,6 +49,7 @@ struct font_options_t : face_options_t #endif g_free (font_funcs); hb_font_destroy (font); + free (custom_palette); } void add_options (option_parser_t *parser); @@ -70,6 +71,7 @@ struct font_options_t : face_options_t char *font_funcs = nullptr; int ft_load_flags = 2; unsigned int palette = 0; + char *custom_palette = nullptr; unsigned int named_instance = HB_FONT_NO_VAR_NAMED_INSTANCE; hb_font_t *font = nullptr; @@ -290,6 +292,7 @@ font_options_t::add_options (option_parser_t *parser) {"font-slant", 0, 0, G_OPTION_ARG_DOUBLE, &this->slant, "Set synthetic slant (default: 0)", "slant ratio; eg. 0.2"}, {"font-palette", 0, 0, G_OPTION_ARG_INT, &this->palette, "Set font palette (default: 0)", "index"}, + {"custom-palette", 0, 0, G_OPTION_ARG_STRING, &this->custom_palette, "Custom palette", "palette"}, {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, text, "impl"}, {"sub-font", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &this->sub_font, "Create a sub-font (default: false)", "boolean"}, diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index c0ce5955f..114d79e86 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -125,6 +125,18 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts) #ifdef CAIRO_COLOR_PALETTE_DEFAULT cairo_font_options_set_color_palette (font_options, font_opts->palette); #endif +#ifdef CAIRO_COLOR_PALETTE_CUSTOM + if (font_opts->custom_palette) + { + char **entries = g_strsplit (font_opts->custom_palette, ",", -1); + for (unsigned int i = 0; entries[i]; i++) + { + unsigned int idx, fr, fg, fb, fa; + if (sscanf (entries[i], "%u:%2x%2x%2x%2x", &idx, &fr, &fg, &fb, &fa) == 5) + cairo_font_options_set_custom_palette_color (font_options, idx, fr / 255., fg / 255., fb / 255., fa / 255.); + } + } +#endif cairo_scaled_font_t *scaled_font = cairo_scaled_font_create (cairo_face, &font_matrix, From 0bff5704912fb99789ca7e09d3fafb640c2ccfed Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 18 Jan 2023 22:33:32 -0700 Subject: [PATCH 03/27] [hb-view] Use custom palette if any set --- util/helper-cairo.hh | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index 114d79e86..c2a2eda77 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -122,12 +122,11 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts) font_options = cairo_font_options_create (); cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE); cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF); -#ifdef CAIRO_COLOR_PALETTE_DEFAULT - cairo_font_options_set_color_palette (font_options, font_opts->palette); -#endif + unsigned palette_index = font_opts->palette; #ifdef CAIRO_COLOR_PALETTE_CUSTOM if (font_opts->custom_palette) { + palette_index = HB_PAINT_PALETTE_INDEX_CUSTOM; char **entries = g_strsplit (font_opts->custom_palette, ",", -1); for (unsigned int i = 0; entries[i]; i++) { @@ -137,6 +136,9 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts) } } #endif +#ifdef CAIRO_COLOR_PALETTE_DEFAULT + cairo_font_options_set_color_palette (font_options, palette_index); +#endif cairo_scaled_font_t *scaled_font = cairo_scaled_font_create (cairo_face, &font_matrix, From 4fe6ece425c19e8fd63e346179de5bd14415d732 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 18 Jan 2023 22:33:56 -0700 Subject: [PATCH 04/27] [cairo] Don't fallback to CPAL if cairo doesn't support custom palette --- src/hb-cairo.cc | 11 +---------- 1 file changed, 1 insertion(+), 10 deletions(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 67abd527b..074bb79be 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -329,16 +329,7 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, cairo_font_options_destroy (options); #endif - // Fall back to the default palette - cairo_scaled_font_t *scaled_font = cairo_get_scaled_font (cr); - hb_font_t *font = hb_cairo_scaled_font_get_font (scaled_font); - hb_face_t *face = hb_font_get_face (font); - hb_color_t color; - unsigned int len = 1; - - hb_ot_color_palette_get_colors (face, 0, color_index, &len, &color); - - return color; + return HB_COLOR (0, 0, 0, 0); } static inline void free_static_cairo_paint_funcs (); From 5847ec24ff65d7f7c59d105b2cca86ac235ac7eb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 18 Jan 2023 22:37:54 -0700 Subject: [PATCH 05/27] Fix bots --- util/helper-cairo.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index c2a2eda77..4d6468f2b 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -122,6 +122,7 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts) font_options = cairo_font_options_create (); cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE); cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF); +#ifdef CAIRO_COLOR_PALETTE_DEFAULT unsigned palette_index = font_opts->palette; #ifdef CAIRO_COLOR_PALETTE_CUSTOM if (font_opts->custom_palette) @@ -136,7 +137,6 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts) } } #endif -#ifdef CAIRO_COLOR_PALETTE_DEFAULT cairo_font_options_set_color_palette (font_options, palette_index); #endif From 52b78d526b522f95897bfcb4d9652ea328fbdb6a Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 18 Jan 2023 23:06:08 -0700 Subject: [PATCH 06/27] [hb-view] Fix leak --- util/helper-cairo.hh | 1 + 1 file changed, 1 insertion(+) diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index 4d6468f2b..d25372240 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -135,6 +135,7 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts) if (sscanf (entries[i], "%u:%2x%2x%2x%2x", &idx, &fr, &fg, &fb, &fa) == 5) cairo_font_options_set_custom_palette_color (font_options, idx, fr / 255., fg / 255., fb / 255., fa / 255.); } + g_strfreev (entries); } #endif cairo_font_options_set_color_palette (font_options, palette_index); From 253b4cecae0729330ec04fab93972db86ee1b203 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 18 Jan 2023 23:28:25 -0700 Subject: [PATCH 07/27] [hb-view] Simplify palette format --- util/font-options.hh | 2 +- util/helper-cairo.hh | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/util/font-options.hh b/util/font-options.hh index 5f99a37ef..5d569968c 100644 --- a/util/font-options.hh +++ b/util/font-options.hh @@ -292,7 +292,7 @@ font_options_t::add_options (option_parser_t *parser) {"font-slant", 0, 0, G_OPTION_ARG_DOUBLE, &this->slant, "Set synthetic slant (default: 0)", "slant ratio; eg. 0.2"}, {"font-palette", 0, 0, G_OPTION_ARG_INT, &this->palette, "Set font palette (default: 0)", "index"}, - {"custom-palette", 0, 0, G_OPTION_ARG_STRING, &this->custom_palette, "Custom palette", "palette"}, + {"custom-palette", 0, 0, G_OPTION_ARG_STRING, &this->custom_palette, "Custom palette", "comma-separated colors"}, {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, text, "impl"}, {"sub-font", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &this->sub_font, "Create a sub-font (default: false)", "boolean"}, diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index d25372240..293077a6e 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -131,9 +131,9 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts) char **entries = g_strsplit (font_opts->custom_palette, ",", -1); for (unsigned int i = 0; entries[i]; i++) { - unsigned int idx, fr, fg, fb, fa; - if (sscanf (entries[i], "%u:%2x%2x%2x%2x", &idx, &fr, &fg, &fb, &fa) == 5) - cairo_font_options_set_custom_palette_color (font_options, idx, fr / 255., fg / 255., fb / 255., fa / 255.); + unsigned int fr, fg, fb, fa; + if (sscanf (entries[i], "%2x%2x%2x%2x", &fr, &fg, &fb, &fa) == 4) + cairo_font_options_set_custom_palette_color (font_options, i, fr / 255., fg / 255., fb / 255., fa / 255.); } g_strfreev (entries); } From e998cec1d94f32ac44ff0dca42941b28a4fdd546 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 18 Jan 2023 23:33:21 -0700 Subject: [PATCH 08/27] [hb-view] Move palette options to --help-view --- util/font-options.hh | 5 ----- util/helper-cairo.hh | 9 +++++---- util/view-cairo.hh | 3 ++- util/view-options.hh | 5 +++++ 4 files changed, 12 insertions(+), 10 deletions(-) diff --git a/util/font-options.hh b/util/font-options.hh index 5d569968c..efc5a1a8b 100644 --- a/util/font-options.hh +++ b/util/font-options.hh @@ -49,7 +49,6 @@ struct font_options_t : face_options_t #endif g_free (font_funcs); hb_font_destroy (font); - free (custom_palette); } void add_options (option_parser_t *parser); @@ -70,8 +69,6 @@ struct font_options_t : face_options_t mutable double font_size_y = DEFAULT_FONT_SIZE; char *font_funcs = nullptr; int ft_load_flags = 2; - unsigned int palette = 0; - char *custom_palette = nullptr; unsigned int named_instance = HB_FONT_NO_VAR_NAMED_INSTANCE; hb_font_t *font = nullptr; @@ -291,8 +288,6 @@ font_options_t::add_options (option_parser_t *parser) G_OPTION_ARG_DOUBLE, &this->ptem, "Set font point-size (default: 0; disabled)", "point-size"}, {"font-slant", 0, 0, G_OPTION_ARG_DOUBLE, &this->slant, "Set synthetic slant (default: 0)", "slant ratio; eg. 0.2"}, - {"font-palette", 0, 0, G_OPTION_ARG_INT, &this->palette, "Set font palette (default: 0)", "index"}, - {"custom-palette", 0, 0, G_OPTION_ARG_STRING, &this->custom_palette, "Custom palette", "comma-separated colors"}, {"font-funcs", 0, 0, G_OPTION_ARG_STRING, &this->font_funcs, text, "impl"}, {"sub-font", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &this->sub_font, "Create a sub-font (default: false)", "boolean"}, diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index 293077a6e..737e70b77 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -88,7 +88,8 @@ helper_cairo_use_hb_draw (const font_options_t *font_opts) } static inline cairo_scaled_font_t * -helper_cairo_create_scaled_font (const font_options_t *font_opts) +helper_cairo_create_scaled_font (const font_options_t *font_opts, + const view_options_t *view_opts) { hb_font_t *font = font_opts->font; bool use_hb_draw = true; @@ -123,12 +124,12 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts) cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE); cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF); #ifdef CAIRO_COLOR_PALETTE_DEFAULT - unsigned palette_index = font_opts->palette; + unsigned palette_index = view_opts->palette; #ifdef CAIRO_COLOR_PALETTE_CUSTOM - if (font_opts->custom_palette) + if (view_opts->custom_palette) { palette_index = HB_PAINT_PALETTE_INDEX_CUSTOM; - char **entries = g_strsplit (font_opts->custom_palette, ",", -1); + char **entries = g_strsplit (view_opts->custom_palette, ",", -1); for (unsigned int i = 0; entries[i]; i++) { unsigned int fr, fg, fb, fa; diff --git a/util/view-cairo.hh b/util/view-cairo.hh index f30905d0d..fbd5e5d9b 100644 --- a/util/view-cairo.hh +++ b/util/view-cairo.hh @@ -131,7 +131,8 @@ view_cairo_t::render (const font_options_t *font_opts) w = MAX (w, x_sign * x_advance); } - cairo_scaled_font_t *scaled_font = helper_cairo_create_scaled_font (font_opts); + cairo_scaled_font_t *scaled_font = helper_cairo_create_scaled_font (font_opts, + this); /* See if font needs color. */ cairo_content_t content = CAIRO_CONTENT_ALPHA; diff --git a/util/view-options.hh b/util/view-options.hh index 322009a1e..731c133a2 100644 --- a/util/view-options.hh +++ b/util/view-options.hh @@ -39,6 +39,7 @@ struct view_options_t { g_free (fore); g_free (back); + g_free (custom_palette); } void add_options (option_parser_t *parser); @@ -46,6 +47,8 @@ struct view_options_t hb_bool_t annotate = false; char *fore = nullptr; char *back = nullptr; + unsigned int palette = 0; + char *custom_palette = nullptr; double line_space = 0; bool have_font_extents = false; struct font_extents_t { @@ -108,6 +111,8 @@ view_options_t::add_options (option_parser_t *parser) {"annotate", 0, 0, G_OPTION_ARG_NONE, &this->annotate, "Annotate output rendering", nullptr}, {"background", 0, 0, G_OPTION_ARG_STRING, &this->back, "Set background color (default: " DEFAULT_BACK ")", "rrggbb/rrggbbaa"}, {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: " DEFAULT_FORE ")", "rrggbb/rrggbbaa"}, + {"font-palette", 0, 0, G_OPTION_ARG_INT, &this->palette, "Set font palette (default: 0)", "index"}, + {"custom-palette", 0, 0, G_OPTION_ARG_STRING, &this->custom_palette, "Custom palette", "comma-separated colors"}, {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"}, {"font-extents", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_font_extents, "Set font ascent/descent/line-gap (default: auto)","one to three numbers"}, {"margin", 0, 0, G_OPTION_ARG_CALLBACK, (gpointer) &parse_margin, "Margin around output (default: " G_STRINGIFY(DEFAULT_MARGIN) ")","one to four numbers"}, From b81db8d3d82af7a369a2b26ec35dcec81060a965 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Thu, 19 Jan 2023 08:19:04 -0500 Subject: [PATCH 09/27] Avoid a compiler warning --- src/hb-cairo.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 074bb79be..15f804a73 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -312,9 +312,9 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, unsigned int color_index, void *user_data HB_UNUSED) { +#ifdef CAIRO_COLOR_PALETTE_CUSTOM cairo_t *cr = (cairo_t *) paint_data; -#ifdef CAIRO_COLOR_PALETTE_CUSTOM cairo_font_options_t *options; double red, green, blue, alpha; From dc4af478d14ca5d0270e317a87d60dfba111381d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 11:11:02 -0700 Subject: [PATCH 10/27] [hb-view] Default background to white when parsing --- util/helper-cairo.hh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index 737e70b77..fe4367aac 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -508,7 +508,7 @@ helper_cairo_create_context (double w, double h, unsigned int fr, fg, fb, fa, br, bg, bb, ba; const char *color; - br = bg = bb = 0; ba = 255; + br = bg = bb = ba = 255; color = view_opts->back ? view_opts->back : DEFAULT_BACK; sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba); fr = fg = fb = 0; fa = 255; From 03e2e586423ada331ae433db7dea705a8b6ad3fe Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 11:24:35 -0700 Subject: [PATCH 11/27] [hb-view] Improve color parsing Now supports 3, 4, 6, 8 digit colors. --- util/helper-cairo.hh | 13 +++++++------ util/options.hh | 40 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index fe4367aac..a066d6807 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -130,11 +130,12 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts, { palette_index = HB_PAINT_PALETTE_INDEX_CUSTOM; char **entries = g_strsplit (view_opts->custom_palette, ",", -1); - for (unsigned int i = 0; entries[i]; i++) + for (unsigned i = 0; entries[i]; i++) { - unsigned int fr, fg, fb, fa; - if (sscanf (entries[i], "%2x%2x%2x%2x", &fr, &fg, &fb, &fa) == 4) - cairo_font_options_set_custom_palette_color (font_options, i, fr / 255., fg / 255., fb / 255., fa / 255.); + unsigned fr, fg, fb, fa; + fr = fg = fb = fa = 0; + parse_color (entries[i], fr, fg,fb, fa); + cairo_font_options_set_custom_palette_color (font_options, i, fr / 255., fg / 255., fb / 255., fa / 255.); } g_strfreev (entries); } @@ -510,10 +511,10 @@ helper_cairo_create_context (double w, double h, const char *color; br = bg = bb = ba = 255; color = view_opts->back ? view_opts->back : DEFAULT_BACK; - sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &br, &bg, &bb, &ba); + parse_color (color, br, bg, bb, ba); fr = fg = fb = 0; fa = 255; color = view_opts->fore ? view_opts->fore : DEFAULT_FORE; - sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa); + parse_color (color, fr, fg, fb, fa); if (content == CAIRO_CONTENT_ALPHA) { diff --git a/util/options.hh b/util/options.hh index 4336a1dcd..efce20c3a 100644 --- a/util/options.hh +++ b/util/options.hh @@ -242,4 +242,44 @@ __inline float scalbnf (float x, int exp) } #endif +static inline bool +parse_color (const char *s, + unsigned &r, + unsigned &g, + unsigned &b, + unsigned &a) +{ + bool ret = false; + + while (*s == ' ') s++; + if (*s == '#') s++; + + unsigned sr, sg, sb, sa; + sa = 255; + if (sscanf (s, "%2x%2x%2x%2x", &sr, &sg, &sb, &sa) <= 2) + { + if (sscanf (s, "%1x%1x%1x%1x", &sr, &sg, &sb, &sa) >= 3) + { + sr *= 17; + sg *= 17; + sb *= 17; + sa *= 17; + ret = true; + } + } + else + ret = true; + + if (ret) + { + r = sr; + g = sg; + b = sb; + a = sa; + } + + return ret; +} + + #endif From 638e0ed4fdd06a6215f2d7c74786b6436074d564 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 13:01:22 -0700 Subject: [PATCH 12/27] [paint] Overlay custom-palette on top of chosen palette Got to agree this is more ergonomic. --- src/OT/Color/COLR/COLR.hh | 4 +--- src/hb-cairo.cc | 8 +++++--- src/hb-font.cc | 3 +-- src/hb-ft-colr.hh | 7 +++---- src/hb-paint.cc | 12 +++++++----- src/hb-paint.h | 25 ++++++++----------------- src/hb-paint.hh | 6 ++++-- util/helper-cairo.hh | 10 ++++------ 8 files changed, 33 insertions(+), 42 deletions(-) diff --git a/src/OT/Color/COLR/COLR.hh b/src/OT/Color/COLR/COLR.hh index 140928406..6a2fedd41 100644 --- a/src/OT/Color/COLR/COLR.hh +++ b/src/OT/Color/COLR/COLR.hh @@ -97,15 +97,13 @@ public: if (color_index != 0xffff) { - if (palette_index != HB_PAINT_PALETTE_INDEX_CUSTOM) + if (!funcs->custom_palette_color (data, color_index, &color)) { unsigned int clen = 1; hb_face_t *face = hb_font_get_face (font); hb_ot_color_palette_get_colors (face, palette_index, color_index, &clen, &color); } - else - color = funcs->custom_palette_color (data, color_index); *is_foreground = false; } diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 15f804a73..c760311a5 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -306,10 +306,11 @@ hb_cairo_paint_sweep_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, _hb_cairo_paint_sweep_gradient (cr, color_line, x0, y0, start_angle, end_angle); } -static hb_color_t +static hb_bool_t hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, void *paint_data, unsigned int color_index, + hb_color_t *color, void *user_data HB_UNUSED) { #ifdef CAIRO_COLOR_PALETTE_CUSTOM @@ -324,12 +325,13 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, &red, &green, &blue, &alpha)) { cairo_font_options_destroy (options); - return HB_COLOR (255 * blue, 255 * green, 255 * red, 255 * alpha); + *color = HB_COLOR (255 * blue, 255 * green, 255 * red, 255 * alpha); + return true; } cairo_font_options_destroy (options); #endif - return HB_COLOR (0, 0, 0, 0); + return false; } static inline void free_static_cairo_paint_funcs (); diff --git a/src/hb-font.cc b/src/hb-font.cc index ccd93f3d5..f2127c2b1 100644 --- a/src/hb-font.cc +++ b/src/hb-font.cc @@ -1440,8 +1440,7 @@ hb_font_draw_glyph (hb_font_t *font, * * If the font has color palettes (see hb_ot_color_has_palettes()), * then @palette_index selects the palette to use. If the font only - * has one palette, this will be 0. Use %HB_PAINT_PALETTE_INDEX_CUSTOM - * for custom palette. + * has one palette, this will be 0. * * Since: REPLACEME */ diff --git a/src/hb-ft-colr.hh b/src/hb-ft-colr.hh index 73f046385..b3457933c 100644 --- a/src/hb-ft-colr.hh +++ b/src/hb-ft-colr.hh @@ -156,9 +156,9 @@ _hb_ft_color_line_get_color_stops (hb_color_line_t *color_line, (hb_color_get_alpha (c->foreground) * stop.color.alpha) >> 14); else { - if (c->palette_index == HB_PAINT_PALETTE_INDEX_CUSTOM) + hb_color_t color; + if (c->funcs->custom_palette_color (c->data, stop.color.palette_index, &color)) { - hb_color_t color = c->funcs->custom_palette_color (c->data, stop.color.palette_index); color_stops->color = HB_COLOR (hb_color_get_blue (color), hb_color_get_green (color), hb_color_get_red (color), @@ -237,9 +237,8 @@ _hb_ft_paint (hb_ft_paint_context_t *c, (hb_color_get_alpha (c->foreground) * paint.u.solid.color.alpha) >> 14); else { - if (c->palette_index == HB_PAINT_PALETTE_INDEX_CUSTOM) + if (c->funcs->custom_palette_color (c->data, paint.u.solid.color.palette_index, &color)) { - color = c->funcs->custom_palette_color (c->data, paint.u.solid.color.palette_index); color = HB_COLOR (hb_color_get_blue (color), hb_color_get_green (color), hb_color_get_red (color), diff --git a/src/hb-paint.cc b/src/hb-paint.cc index 21229c49e..9fcff5f79 100644 --- a/src/hb-paint.cc +++ b/src/hb-paint.cc @@ -117,10 +117,11 @@ hb_paint_pop_group_nil (hb_paint_funcs_t *funcs, void *paint_data, hb_paint_composite_mode_t mode, void *user_data) {} -static hb_color_t +static hb_bool_t hb_paint_custom_palette_color_nil (hb_paint_funcs_t *funcs, void *paint_data, unsigned int color_index, - void *user_data) { return HB_COLOR(0,0,0,0); } + hb_color_t *color, + void *user_data) { return false; } static bool _hb_paint_funcs_set_preamble (hb_paint_funcs_t *funcs, @@ -690,11 +691,12 @@ hb_paint_pop_group (hb_paint_funcs_t *funcs, void *paint_data, * * Since: REPLACEME */ -hb_color_t +hb_bool_t hb_paint_custom_palette_color (hb_paint_funcs_t *funcs, void *paint_data, - unsigned int color_index) + unsigned int color_index, + hb_color_t *color) { - return funcs->custom_palette_color (paint_data, color_index); + return funcs->custom_palette_color (paint_data, color_index, color); } #endif diff --git a/src/hb-paint.h b/src/hb-paint.h index d4d831e57..5f63b0a4b 100644 --- a/src/hb-paint.h +++ b/src/hb-paint.h @@ -34,17 +34,6 @@ HB_BEGIN_DECLS -/** - * HB_PAINT_PALETTE_INDEX_CUSTOM - * - * A palette index signifying that custom colors are in use. - * Such colors are fetched from the client using the - * custom-palette-color callback of the paint functions. - * - * Since: REPLACEME - **/ -#define HB_PAINT_PALETTE_INDEX_CUSTOM 0xFFFFFFFF - /** * hb_paint_funcs_t: * @@ -680,10 +669,11 @@ typedef void (*hb_paint_pop_group_func_t) (hb_paint_funcs_t *funcs, * Return value: the color * Since: REPLACEME */ -typedef hb_color_t (*hb_paint_custom_palette_color_func_t) (hb_paint_funcs_t *funcs, - void *paint_data, - unsigned int color_index, - void *user_data); +typedef hb_bool_t (*hb_paint_custom_palette_color_func_t) (hb_paint_funcs_t *funcs, + void *paint_data, + unsigned int color_index, + hb_color_t *color, + void *user_data); /** @@ -974,9 +964,10 @@ HB_EXTERN void hb_paint_pop_group (hb_paint_funcs_t *funcs, void *paint_data, hb_paint_composite_mode_t mode); -HB_EXTERN hb_color_t +HB_EXTERN hb_bool_t hb_paint_custom_palette_color (hb_paint_funcs_t *funcs, void *paint_data, - unsigned int color_index); + unsigned int color_index, + hb_color_t *color); HB_END_DECLS diff --git a/src/hb-paint.hh b/src/hb-paint.hh index b31a0cfc0..c4789cf9b 100644 --- a/src/hb-paint.hh +++ b/src/hb-paint.hh @@ -138,10 +138,12 @@ struct hb_paint_funcs_t { func.pop_group (this, paint_data, mode, !user_data ? nullptr : user_data->pop_group); } - hb_color_t custom_palette_color (void *paint_data, - unsigned int color_index) + bool custom_palette_color (void *paint_data, + unsigned int color_index, + hb_color_t *color) { return func.custom_palette_color (this, paint_data, color_index, + color, !user_data ? nullptr : user_data->custom_palette_color); } diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index a066d6807..ca7f469c1 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -124,24 +124,22 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts, cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE); cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF); #ifdef CAIRO_COLOR_PALETTE_DEFAULT - unsigned palette_index = view_opts->palette; + cairo_font_options_set_color_palette (font_options, view_opts->palette); +#endif #ifdef CAIRO_COLOR_PALETTE_CUSTOM if (view_opts->custom_palette) { - palette_index = HB_PAINT_PALETTE_INDEX_CUSTOM; char **entries = g_strsplit (view_opts->custom_palette, ",", -1); for (unsigned i = 0; entries[i]; i++) { unsigned fr, fg, fb, fa; fr = fg = fb = fa = 0; - parse_color (entries[i], fr, fg,fb, fa); - cairo_font_options_set_custom_palette_color (font_options, i, fr / 255., fg / 255., fb / 255., fa / 255.); + if (parse_color (entries[i], fr, fg,fb, fa)) + cairo_font_options_set_custom_palette_color (font_options, i, fr / 255., fg / 255., fb / 255., fa / 255.); } g_strfreev (entries); } #endif - cairo_font_options_set_color_palette (font_options, palette_index); -#endif cairo_scaled_font_t *scaled_font = cairo_scaled_font_create (cairo_face, &font_matrix, From f21b15dcc318aa62d256443be3ccec7953a64242 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 13:38:22 -0700 Subject: [PATCH 13/27] [hb-view] Update to alternative cairo custom-palette API --- meson.build | 1 + src/hb-cairo.cc | 5 +++-- util/helper-cairo.hh | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/meson.build b/meson.build index 947c371da..b22db3988 100644 --- a/meson.build +++ b/meson.build @@ -214,6 +214,7 @@ if cairo_dep.found() else check_funcs += [['cairo_user_font_face_set_render_color_glyph_func', {'deps': cairo_dep}]] endif + check_funcs += [['cairo_font_options_get_custom_palette_color', {'deps': cairo_dep}]] endif if cairo_ft_dep.found() diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index c760311a5..9bcd447af 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -313,7 +313,7 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, hb_color_t *color, void *user_data HB_UNUSED) { -#ifdef CAIRO_COLOR_PALETTE_CUSTOM +#ifdef HAVE_CAIRO_FONT_OPTIONS_GET_CUSTOM_PALETTE_COLOR cairo_t *cr = (cairo_t *) paint_data; cairo_font_options_t *options; @@ -321,7 +321,8 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, options = cairo_font_options_create (); cairo_get_font_options (cr, options); - if (cairo_font_options_get_custom_palette_color (options, color_index, + if (CAIRO_STATUS_SUCCESS == + cairo_font_options_get_custom_palette_color (options, color_index, &red, &green, &blue, &alpha)) { cairo_font_options_destroy (options); diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index ca7f469c1..aa3905ce1 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -126,7 +126,7 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts, #ifdef CAIRO_COLOR_PALETTE_DEFAULT cairo_font_options_set_color_palette (font_options, view_opts->palette); #endif -#ifdef CAIRO_COLOR_PALETTE_CUSTOM +#ifdef HAVE_CAIRO_FONT_OPTIONS_GET_CUSTOM_PALETTE_COLOR if (view_opts->custom_palette) { char **entries = g_strsplit (view_opts->custom_palette, ",", -1); From eb00088bcfea640d2d1d591d08cdcdd01d5acf91 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 13:55:13 -0700 Subject: [PATCH 14/27] [paint] Docs --- docs/harfbuzz-sections.txt | 2 -- src/hb-paint.cc | 3 ++- src/hb-paint.h | 4 +++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index 6e2aab4d3..0e5959bec 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -225,8 +225,6 @@ hb_draw_state_t
hb-paint -HB_PAINT_PALETTE_INDEX_CUSTOM - hb_paint_funcs_t hb_paint_funcs_create hb_paint_funcs_get_empty diff --git a/src/hb-paint.cc b/src/hb-paint.cc index 9fcff5f79..00dce7285 100644 --- a/src/hb-paint.cc +++ b/src/hb-paint.cc @@ -684,10 +684,11 @@ hb_paint_pop_group (hb_paint_funcs_t *funcs, void *paint_data, * @funcs: paint functions * @paint_data: associated data passed by the caller * @color_index: color index + * @color: (out): fetched color * * Gets the custom palette color for @color_index. * - * Return value: the custom color + * Return value: `true` if found, `false` otherwise * * Since: REPLACEME */ diff --git a/src/hb-paint.h b/src/hb-paint.h index 5f63b0a4b..a9eb31e78 100644 --- a/src/hb-paint.h +++ b/src/hb-paint.h @@ -661,12 +661,14 @@ typedef void (*hb_paint_pop_group_func_t) (hb_paint_funcs_t *funcs, * @funcs: paint functions object * @paint_data: The data accompanying the paint functions in hb_font_paint_glyph() * @color_index: the color index + * @color: (out): fetched color * @user_data: User data pointer passed to hb_paint_funcs_set_pop_group_func() * * A virtual method for the #hb_paint_funcs_t to fetch a color from the custom * color palette. * - * Return value: the color + * Return value: `true` if found, `false` otherwise + * * Since: REPLACEME */ typedef hb_bool_t (*hb_paint_custom_palette_color_func_t) (hb_paint_funcs_t *funcs, From 10def9b3df1241eec912b94ba82d43cd8f93caa4 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 14:06:18 -0700 Subject: [PATCH 15/27] meson fix --- meson.build | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/meson.build b/meson.build index b22db3988..f28fb9bc1 100644 --- a/meson.build +++ b/meson.build @@ -213,8 +213,8 @@ if cairo_dep.found() conf.set('HAVE_CAIRO_USER_FONT_FACE_SET_RENDER_COLOR_GLYPH_FUNC', 1) else check_funcs += [['cairo_user_font_face_set_render_color_glyph_func', {'deps': cairo_dep}]] + check_funcs += [['cairo_font_options_get_custom_palette_color', {'deps': cairo_dep}]] endif - check_funcs += [['cairo_font_options_get_custom_palette_color', {'deps': cairo_dep}]] endif if cairo_ft_dep.found() From cc9b55c79469b93583f21f8a45cd3cb0759aa789 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 14:57:57 -0700 Subject: [PATCH 16/27] [hb-cairo] Add a color cache --- src/hb-cairo.cc | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 9bcd447af..8428a4ba7 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -306,6 +306,8 @@ hb_cairo_paint_sweep_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, _hb_cairo_paint_sweep_gradient (cr, color_line, x0, y0, start_angle, end_angle); } +static const cairo_user_data_key_t color_cache_key = {0}; + static hb_bool_t hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, void *paint_data, @@ -316,6 +318,16 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, #ifdef HAVE_CAIRO_FONT_OPTIONS_GET_CUSTOM_PALETTE_COLOR cairo_t *cr = (cairo_t *) paint_data; +#define DEADBEEF HB_TAG(0xDE,0xAD,0xBE,0xEF) + + hb_map_t *color_cache = (hb_map_t *) cairo_get_user_data (cr, &color_cache_key); + hb_codepoint_t *c; + if (likely (color_cache && color_cache->has (color_index, &c))) + { + *color = *c; + return true; + } + cairo_font_options_t *options; double red, green, blue, alpha; @@ -327,11 +339,18 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, { cairo_font_options_destroy (options); *color = HB_COLOR (255 * blue, 255 * green, 255 * red, 255 * alpha); + + if (likely (color_cache && *color != DEADBEEF)) + color_cache->set (color_index, *color); + return true; } cairo_font_options_destroy (options); #endif + if (likely (color_cache)) + color_cache->set (color_index, DEADBEEF); + return false; } @@ -556,8 +575,13 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, hb_font_get_scale (font, &x_scale, &y_scale); cairo_scale (cr, +1./x_scale, -1./y_scale); + hb_map_t color_cache; + cairo_set_user_data (cr, &color_cache_key, &color_cache, nullptr); + hb_font_paint_glyph (font, glyph, hb_cairo_paint_get_funcs (), cr, palette, color); + cairo_set_user_data (cr, &color_cache_key, nullptr, nullptr); + return CAIRO_STATUS_SUCCESS; } From beba43eebe235dac402a66ffd58fa29a9689fe15 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 15:02:24 -0700 Subject: [PATCH 17/27] [hb-cairo] Fix color cache on not-found --- src/hb-cairo.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 8428a4ba7..1045925d8 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -324,6 +324,8 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, hb_codepoint_t *c; if (likely (color_cache && color_cache->has (color_index, &c))) { + if (*c == DEADBEEF) + return false; *color = *c; return true; } From 4f19c3b3be07b75235684b969677725fe50494f3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 15:15:52 -0700 Subject: [PATCH 18/27] [hb-cairo] Move color-cache to scaled-font --- src/hb-cairo.cc | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 1045925d8..5c5e862d7 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -308,6 +308,12 @@ hb_cairo_paint_sweep_gradient (hb_paint_funcs_t *pfuncs HB_UNUSED, static const cairo_user_data_key_t color_cache_key = {0}; +static void +_hb_cairo_destroy_map (void *p) +{ + hb_map_destroy ((hb_map_t *) p); +} + static hb_bool_t hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, void *paint_data, @@ -490,6 +496,9 @@ hb_cairo_init_scaled_font (cairo_scaled_font_t *scaled_font, extents->descent = (double) -hb_extents.descender / y_scale; extents->height = extents->ascent + extents->descent; + hb_map_t *color_cache = hb_map_create (); + cairo_scaled_font_set_user_data (scaled_font, &color_cache_key, color_cache, _hb_cairo_destroy_map); + return CAIRO_STATUS_SUCCESS; } @@ -577,8 +586,8 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, hb_font_get_scale (font, &x_scale, &y_scale); cairo_scale (cr, +1./x_scale, -1./y_scale); - hb_map_t color_cache; - cairo_set_user_data (cr, &color_cache_key, &color_cache, nullptr); + void *color_cache = cairo_scaled_font_get_user_data (scaled_font, &color_cache_key); + cairo_set_user_data (cr, &color_cache_key, color_cache, nullptr); hb_font_paint_glyph (font, glyph, hb_cairo_paint_get_funcs (), cr, palette, color); From 574d9344dccdd7c79a02070dac48bf825c8095de Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 15:22:50 -0700 Subject: [PATCH 19/27] [hb-cairo] Fix build with old cairo --- src/hb-cairo.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 5c5e862d7..a6ad292be 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -496,8 +496,10 @@ hb_cairo_init_scaled_font (cairo_scaled_font_t *scaled_font, extents->descent = (double) -hb_extents.descender / y_scale; extents->height = extents->ascent + extents->descent; +#ifdef HAVE_CAIRO_USER_FONT_FACE_SET_RENDER_COLOR_GLYPH_FUNC hb_map_t *color_cache = hb_map_create (); cairo_scaled_font_set_user_data (scaled_font, &color_cache_key, color_cache, _hb_cairo_destroy_map); +#endif return CAIRO_STATUS_SUCCESS; } From 68a73e436a37851465b1b8b59e3b7a2c552d28f2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 15:30:07 -0700 Subject: [PATCH 20/27] [hb-cairo] Macro hygiene --- src/hb-cairo.cc | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index a6ad292be..a9fef059a 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -324,13 +324,13 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, #ifdef HAVE_CAIRO_FONT_OPTIONS_GET_CUSTOM_PALETTE_COLOR cairo_t *cr = (cairo_t *) paint_data; -#define DEADBEEF HB_TAG(0xDE,0xAD,0xBE,0xEF) +#define HB_DEADBEEF HB_TAG(0xDE,0xAD,0xBE,0xEF) hb_map_t *color_cache = (hb_map_t *) cairo_get_user_data (cr, &color_cache_key); hb_codepoint_t *c; if (likely (color_cache && color_cache->has (color_index, &c))) { - if (*c == DEADBEEF) + if (*c == HB_DEADBEEF) return false; *color = *c; return true; @@ -348,7 +348,7 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, cairo_font_options_destroy (options); *color = HB_COLOR (255 * blue, 255 * green, 255 * red, 255 * alpha); - if (likely (color_cache && *color != DEADBEEF)) + if (likely (color_cache && *color != HB_DEADBEEF)) color_cache->set (color_index, *color); return true; @@ -357,7 +357,9 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, #endif if (likely (color_cache)) - color_cache->set (color_index, DEADBEEF); + color_cache->set (color_index, HB_DEADBEEF); + +#undef HB_DEADBEEF return false; } From 876675e090e2b55fdb5f3e8b187022184145b2f3 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 15:31:43 -0700 Subject: [PATCH 21/27] [hb-cairo] Macro shuffling --- src/hb-cairo.cc | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index a9fef059a..e649e0691 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -321,11 +321,11 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, hb_color_t *color, void *user_data HB_UNUSED) { +#define HB_DEADBEEF HB_TAG(0xDE,0xAD,0xBE,0xEF) + #ifdef HAVE_CAIRO_FONT_OPTIONS_GET_CUSTOM_PALETTE_COLOR cairo_t *cr = (cairo_t *) paint_data; -#define HB_DEADBEEF HB_TAG(0xDE,0xAD,0xBE,0xEF) - hb_map_t *color_cache = (hb_map_t *) cairo_get_user_data (cr, &color_cache_key); hb_codepoint_t *c; if (likely (color_cache && color_cache->has (color_index, &c))) From 4759932bcfb5af5f576868cc96dfe2755361fe9d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 15:33:38 -0700 Subject: [PATCH 22/27] [hb-cairo] Round colors --- src/hb-cairo.cc | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index e649e0691..027664bcf 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -346,7 +346,10 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, &red, &green, &blue, &alpha)) { cairo_font_options_destroy (options); - *color = HB_COLOR (255 * blue, 255 * green, 255 * red, 255 * alpha); + *color = HB_COLOR (round (255 * blue), + round (255 * green), + round (255 * red), + round (255 * alpha)); if (likely (color_cache && *color != HB_DEADBEEF)) color_cache->set (color_index, *color); From ab7c91442536086f0baebe2d419827bb9e4cce06 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 15:43:01 -0700 Subject: [PATCH 23/27] [hb-cairo] Macro shuffle again --- src/hb-cairo.cc | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 027664bcf..f16b7c3c2 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -321,11 +321,11 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, hb_color_t *color, void *user_data HB_UNUSED) { -#define HB_DEADBEEF HB_TAG(0xDE,0xAD,0xBE,0xEF) - #ifdef HAVE_CAIRO_FONT_OPTIONS_GET_CUSTOM_PALETTE_COLOR cairo_t *cr = (cairo_t *) paint_data; +#define HB_DEADBEEF HB_TAG(0xDE,0xAD,0xBE,0xEF) + hb_map_t *color_cache = (hb_map_t *) cairo_get_user_data (cr, &color_cache_key); hb_codepoint_t *c; if (likely (color_cache && color_cache->has (color_index, &c))) @@ -357,13 +357,14 @@ hb_cairo_paint_custom_palette_color (hb_paint_funcs_t *funcs, return true; } cairo_font_options_destroy (options); -#endif if (likely (color_cache)) color_cache->set (color_index, HB_DEADBEEF); #undef HB_DEADBEEF +#endif + return false; } From 61719a835089ea2c2cda36702f630c9343b029c7 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 15:52:09 -0700 Subject: [PATCH 24/27] [hb-view] Support specifying color indices again --- src/hb-number.cc | 1 - util/helper-cairo.hh | 16 ++++++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/hb-number.cc b/src/hb-number.cc index 6e4f3f7eb..c52b284e1 100644 --- a/src/hb-number.cc +++ b/src/hb-number.cc @@ -24,7 +24,6 @@ */ #include "hb.hh" -#include "hb-machinery.hh" #include "hb-number.hh" #include "hb-number-parser.hh" diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index aa3905ce1..c62413150 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -130,12 +130,24 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts, if (view_opts->custom_palette) { char **entries = g_strsplit (view_opts->custom_palette, ",", -1); + unsigned idx = 0; for (unsigned i = 0; entries[i]; i++) { + const char *p = strchr (entries[i], '='); + if (!p) + p = entries[i]; + else + { + sscanf (entries[i], "%u", &idx); + p++; + } + unsigned fr, fg, fb, fa; fr = fg = fb = fa = 0; - if (parse_color (entries[i], fr, fg,fb, fa)) - cairo_font_options_set_custom_palette_color (font_options, i, fr / 255., fg / 255., fb / 255., fa / 255.); + if (parse_color (p, fr, fg,fb, fa)) + cairo_font_options_set_custom_palette_color (font_options, idx, fr / 255., fg / 255., fb / 255., fa / 255.); + + idx++; } g_strfreev (entries); } From f70f7194de5f24625d12d40cf639a7a0e7ef48b9 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 16:30:59 -0700 Subject: [PATCH 25/27] [hb-cairo] Remove unused prototype --- src/hb-cairo.cc | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index f16b7c3c2..5193d1eb0 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -409,12 +409,6 @@ hb_cairo_paint_get_funcs () { return static_cairo_paint_funcs.get_unconst (); } - -static cairo_status_t -hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, - unsigned long glyph, - cairo_t *cr, - cairo_text_extents_t *extents); #endif static const cairo_user_data_key_t hb_cairo_face_user_data_key = {0}; From 76b059cadb805df3df860be6a130ab5480cb8846 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 16:41:47 -0700 Subject: [PATCH 26/27] [hb-cairo] Simplify foreground color fetching --- src/hb-cairo.cc | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 5193d1eb0..71818c0c7 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -577,12 +577,9 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, hb_color_t color = HB_COLOR (0, 0, 0, 255); cairo_pattern_t *pattern = cairo_get_source (cr); - if (cairo_pattern_get_type (pattern) == CAIRO_PATTERN_TYPE_SOLID) - { - double r, g, b, a; - cairo_pattern_get_rgba (pattern, &r, &g, &b, &a); + double r, g, b, a; + if (cairo_pattern_get_rgba (pattern, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS) color = HB_COLOR ((int)(b * 255.), (int)(g * 255.), (int) (r * 255.), (int)(a * 255.)); - } hb_position_t x_scale, y_scale; hb_font_get_scale (font, &x_scale, &y_scale); From 7f59bed528e75e5336ace1d9cdbee20932e3e211 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Fri, 20 Jan 2023 16:42:55 -0700 Subject: [PATCH 27/27] [hb-cairo] Round foreground color --- src/hb-cairo.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 71818c0c7..6a5c6b430 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -579,7 +579,7 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, cairo_pattern_t *pattern = cairo_get_source (cr); double r, g, b, a; if (cairo_pattern_get_rgba (pattern, &r, &g, &b, &a) == CAIRO_STATUS_SUCCESS) - color = HB_COLOR ((int)(b * 255.), (int)(g * 255.), (int) (r * 255.), (int)(a * 255.)); + color = HB_COLOR (round (b * 255.), round (g * 255.), round (r * 255.), round (a * 255.)); hb_position_t x_scale, y_scale; hb_font_get_scale (font, &x_scale, &y_scale);