From 79fdf3ac84dd1152cae0c6c5d2c33d39983a43f6 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 16:10:17 -0300 Subject: [PATCH 01/19] [util] Let hb-vector inherit shared view options --- util/vector-output.hh | 75 +++++++++++++------------------------------ 1 file changed, 22 insertions(+), 53 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index 8caf82343..92539b364 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -30,46 +30,19 @@ #include #include "output-options.hh" +#include "view-options.hh" #include "hb-vector.h" #include "hb-ot.h" -struct vector_output_t : output_options_t<> +struct vector_output_t : output_options_t<>, view_options_t { static const bool repeat_shape = false; - struct margin_t { - float t, r, b, l; - }; - - static gboolean - parse_margin (const char *name G_GNUC_UNUSED, - const char *arg, - gpointer data, - GError **error) - { - vector_output_t *opts = (vector_output_t *) data; - margin_t &m = opts->margin; - double t, r, b, l; - if (parse_1to4_doubles (arg, &t, &r, &b, &l)) - { - m.t = (float) t; - m.r = (float) r; - m.b = (float) b; - m.l = (float) l; - return true; - } - g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, - "%s argument should be one to four space-separated numbers", - name); - return false; - } ~vector_output_t () { - g_free (background_str); hb_font_destroy (font); hb_font_destroy (upem_font); - g_free (foreground_str); } void add_options (option_parser_t *parser) @@ -78,17 +51,12 @@ struct vector_output_t : output_options_t<> parser->set_description ("Shows shaped glyph outlines as SVG."); output_options_t::add_options (parser); + view_options_t::add_options (parser); GOptionEntry entries[] = { - {"logical", 0, 0, G_OPTION_ARG_NONE, &this->logical, "Use logical extents for bounding box (default)", nullptr}, - {"ink", 0, 0, G_OPTION_ARG_NONE, &this->ink, "Use ink extents for bounding box", nullptr}, {"flat", 0, 0, G_OPTION_ARG_NONE, &this->flat, "Flatten geometry and disable reuse", nullptr}, {"precision", 0, 0, G_OPTION_ARG_INT, &this->precision, "Decimal precision (default: 2)", "N"}, - {"palette", 0, 0, G_OPTION_ARG_INT, &this->palette, "Color palette index (default: 0)", "N"}, - {"background", 0, 0, G_OPTION_ARG_STRING, &this->background_str,"Background color", "rrggbb[aa]"}, - {"margin", 0, 0, G_OPTION_ARG_CALLBACK,(gpointer) &parse_margin, "Margin around output (default: 0)","one to four numbers"}, - {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->foreground_str,"Foreground color (default: 000000)", "rrggbb[aa]"}, {nullptr} }; parser->add_group (entries, @@ -100,6 +68,10 @@ struct vector_output_t : output_options_t<> void post_parse (GError **error) { + view_options_t::post_parse (error); + if (error && *error) + return; + if (precision < 0) { g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, @@ -107,25 +79,28 @@ struct vector_output_t : output_options_t<> return; } - if (foreground_str) + foreground = HB_COLOR (0, 0, 0, 255); + if (foreground_use_palette && foreground_palette && foreground_palette->len) + { + const rgba_color_t &c = g_array_index (foreground_palette, rgba_color_t, 0); + foreground = HB_COLOR (c.b, c.g, c.r, c.a); + } + else if (fore && *fore) { unsigned r, g, b, a; - if (!parse_color (foreground_str, r, g, b, a)) - { - g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, - "Invalid foreground color: %s", foreground_str); + if (!parse_color (fore, r, g, b, a)) return; - } foreground = HB_COLOR (b, g, r, a); } - if (background_str) + has_background = false; + if (back && *back) { unsigned r, g, b, a; - if (!parse_color (background_str, r, g, b, a)) + if (!parse_color (back, r, g, b, a)) { g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, - "Invalid background color: %s", background_str); + "Invalid background color: %s", back); return; } background = HB_COLOR (b, g, r, a); @@ -230,7 +205,7 @@ struct vector_output_t : output_options_t<> hb_vector_paint_set_scale_factor (paint, 1.f, 1.f); hb_vector_paint_set_extents (paint, &extents); hb_vector_paint_set_foreground (paint, foreground); - hb_vector_paint_set_palette (paint, palette); + hb_vector_paint_set_palette (paint, this->palette); hb_vector_svg_paint_set_precision (paint, precision); hb_vector_svg_paint_set_flat (paint, flat); @@ -518,7 +493,7 @@ struct vector_output_t : output_options_t<> void emit_fill_group_open () { - if (!foreground_str) + if (!fore && !foreground_use_palette) return; unsigned r = hb_color_get_red (foreground); @@ -534,7 +509,7 @@ struct vector_output_t : output_options_t<> void emit_fill_group_close () { - if (!foreground_str) + if (!fore && !foreground_use_palette) return; fputs ("\n", out_fp); } @@ -643,13 +618,7 @@ struct vector_output_t : output_options_t<> } hb_bool_t flat = false; - hb_bool_t logical = false; - hb_bool_t ink = false; int precision = 2; - int palette = 0; - margin_t margin = {0.f, 0.f, 0.f, 0.f}; - char *background_str = nullptr; - char *foreground_str = nullptr; hb_color_t background = HB_COLOR (255, 255, 255, 255); hb_color_t foreground = HB_COLOR (0, 0, 0, 255); hb_bool_t has_background = false; From 8b961af787b570657119e63ecf82bb931310807b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 16:17:48 -0300 Subject: [PATCH 02/19] [util] Support --rainbow foreground cycling in hb-vector paint path --- util/vector-output.hh | 132 ++++++++++++++++++++++++++++++++++++------ 1 file changed, 114 insertions(+), 18 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index 92539b364..b9be49575 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -213,15 +213,18 @@ struct vector_output_t : output_options_t<>, view_options_t bool had_paint = false; hb_vector_extents_mode_t extents_mode = ink ? HB_VECTOR_EXTENTS_MODE_EXPAND : HB_VECTOR_EXTENTS_MODE_NONE; + const bool use_foreground_palette = + foreground_use_palette && foreground_palette && foreground_palette->len; + unsigned palette_glyph_index = 0; hb_direction_t dir = direction; if (dir == HB_DIRECTION_INVALID) dir = HB_DIRECTION_LTR; bool vertical = HB_DIRECTION_IS_VERTICAL (dir); - hb_font_extents_t fext = {0, 0, 0}; - hb_font_get_extents_for_direction (upem_font, dir, &fext); - float step = fabsf ((float) (fext.ascender - fext.descender + fext.line_gap)); + float asc = 0.f, desc = 0.f, gap = 0.f; + get_line_metrics (dir, &asc, &desc, &gap); + float step = fabsf (asc - desc + gap + (float) line_space); if (!(step > 0.f)) step = (float) upem; @@ -235,6 +238,14 @@ struct vector_output_t : output_options_t<>, view_options_t float pen_x = g.x + off_x; float pen_y = g.y + off_y; + if (use_foreground_palette) + { + const rgba_color_t &c = + g_array_index (foreground_palette, rgba_color_t, + palette_glyph_index++ % foreground_palette->len); + hb_vector_paint_set_foreground (paint, HB_COLOR (c.b, c.g, c.r, c.a)); + } + hb_vector_paint_set_transform (paint, 1.f, 0.f, 0.f, 1.f, 0.f, 0.f); if (hb_vector_paint_glyph (paint, upem_font, g.gid, pen_x, pen_y, extents_mode)) @@ -263,6 +274,9 @@ struct vector_output_t : output_options_t<>, view_options_t else if (draw_blob && draw_blob != hb_blob_get_empty ()) write_blob (draw_blob, true); + if (show_extents) + emit_extents_overlay (dir, step); + hb_blob_destroy (draw_blob); hb_blob_destroy (paint_blob); hb_vector_draw_destroy (draw); @@ -315,10 +329,9 @@ struct vector_output_t : output_options_t<>, view_options_t dir = HB_DIRECTION_LTR; bool vertical = HB_DIRECTION_IS_VERTICAL (dir); - hb_font_extents_t fext = {0, 0, 0}; - hb_font_get_extents_for_direction (upem_font, dir, &fext); - - float step = fabsf ((float) (fext.ascender - fext.descender + fext.line_gap)); + float asc = 0.f, desc = 0.f, gap = 0.f; + get_line_metrics (dir, &asc, &desc, &gap); + float step = fabsf (asc - desc + gap + (float) line_space); if (!(step > 0.f)) step = (float) upem; @@ -352,8 +365,8 @@ struct vector_output_t : output_options_t<>, view_options_t if (include_logical) { - float log_min = hb_min ((float) fext.descender, (float) fext.ascender); - float log_max = hb_max ((float) fext.descender, (float) fext.ascender); + float log_min = hb_min (desc, asc); + float log_max = hb_max (desc, asc); if (vertical) { include_point (off_x + log_min, hb_min (0.f, line.advance_y)); @@ -493,23 +506,42 @@ struct vector_output_t : output_options_t<>, view_options_t void emit_fill_group_open () { - if (!fore && !foreground_use_palette) + const bool has_fill_override = fore || foreground_use_palette; + const bool has_stroke = stroke_enabled && stroke_width > 0.; + if (!has_fill_override && !has_stroke) return; - unsigned r = hb_color_get_red (foreground); - unsigned g = hb_color_get_green (foreground); - unsigned b = hb_color_get_blue (foreground); - unsigned a = hb_color_get_alpha (foreground); + fputs ("\n"); } void emit_fill_group_close () { - if (!fore && !foreground_use_palette) + if (!(fore || foreground_use_palette || (stroke_enabled && stroke_width > 0.))) return; fputs ("\n", out_fp); } @@ -617,6 +649,70 @@ struct vector_output_t : output_options_t<>, view_options_t fputs ("/>\n", out_fp); } + void emit_extents_overlay (hb_direction_t dir, float step) + { + const bool vertical = HB_DIRECTION_IS_VERTICAL (dir); + const float dot_r = hb_max (1.f, step * 0.01f); + fputs ("\n", out_fp); + for (unsigned li = 0; li < lines.size (); li++) + { + float off_x = vertical ? -(step * (float) li) : 0.f; + float off_y = vertical ? 0.f : -(step * (float) li); + const line_t &line = lines[li]; + for (const auto &g : line.glyphs) + { + fprintf (out_fp, "\n", + precision + 4, (double) (g.x + off_x), + precision + 4, (double) (-(g.y + off_y)), + precision + 4, (double) dot_r); + } + } + fputs ("\n", out_fp); + + fputs ("\n", out_fp); + for (unsigned li = 0; li < lines.size (); li++) + { + float off_x = vertical ? -(step * (float) li) : 0.f; + float off_y = vertical ? 0.f : -(step * (float) li); + const line_t &line = lines[li]; + for (const auto &g : line.glyphs) + { + hb_glyph_extents_t ge; + if (!hb_font_get_glyph_extents (upem_font, g.gid, &ge)) + continue; + float x = g.x + off_x + ge.x_bearing; + float y = g.y + off_y + ge.y_bearing; + float w = ge.width; + float h = ge.height; + if (!w || !h) + continue; + fprintf (out_fp, "\n", + precision + 4, (double) x, + precision + 4, (double) (-y), + precision + 4, (double) w, + precision + 4, (double) (-h)); + } + } + fputs ("\n", out_fp); + } + + void get_line_metrics (hb_direction_t dir, float *asc, float *desc, float *gap) const + { + if (have_font_extents) + { + if (asc) *asc = (float) font_extents.ascent; + if (desc) *desc = (float) (-font_extents.descent); + if (gap) *gap = (float) font_extents.line_gap; + return; + } + + hb_font_extents_t fext = {0, 0, 0}; + hb_font_get_extents_for_direction (upem_font, dir, &fext); + if (asc) *asc = (float) fext.ascender; + if (desc) *desc = (float) fext.descender; + if (gap) *gap = (float) fext.line_gap; + } + hb_bool_t flat = false; int precision = 2; hb_color_t background = HB_COLOR (255, 255, 255, 255); From 51c599ccb9caa2cb0691f69c12c2d8e5483d051d Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 16:18:56 -0300 Subject: [PATCH 03/19] [vector] Hook --custom-palette in hb-vector paint output --- src/hb-vector-svg-paint.cc | 35 +++++++++++++++++++++++++++++++++++ src/hb-vector.h | 26 ++++++++++++++++++++++++++ util/vector-output.hh | 25 +++++++++++++++++++++++++ 3 files changed, 86 insertions(+) diff --git a/src/hb-vector-svg-paint.cc b/src/hb-vector-svg-paint.cc index a30db1a9a..5e516e7b7 100644 --- a/src/hb-vector-svg-paint.cc +++ b/src/hb-vector-svg-paint.cc @@ -385,6 +385,7 @@ struct hb_vector_paint_t hb_color_t foreground = HB_COLOR (0, 0, 0, 255); int palette = 0; + hb_hashmap_t custom_palette_colors; unsigned precision = 2; bool flat = false; @@ -969,6 +970,7 @@ static void hb_vector_paint_sweep_gradient (hb_paint_funcs_t *, void *, hb_color static void hb_vector_paint_push_group (hb_paint_funcs_t *, void *, void *); static void hb_vector_paint_pop_group (hb_paint_funcs_t *, void *, hb_paint_composite_mode_t, void *); static hb_bool_t hb_vector_paint_color_glyph (hb_paint_funcs_t *, void *, hb_codepoint_t, hb_font_t *, void *); +static hb_bool_t hb_vector_paint_custom_palette_color (hb_paint_funcs_t *, void *, unsigned, hb_color_t *, void *); static inline void free_static_vector_paint_funcs (); static struct hb_vector_paint_funcs_lazy_loader_t @@ -990,6 +992,7 @@ static struct hb_vector_paint_funcs_lazy_loader_t hb_paint_funcs_set_push_group_func (funcs, (hb_paint_push_group_func_t) hb_vector_paint_push_group, nullptr, nullptr); hb_paint_funcs_set_pop_group_func (funcs, (hb_paint_pop_group_func_t) hb_vector_paint_pop_group, nullptr, nullptr); hb_paint_funcs_set_color_glyph_func (funcs, (hb_paint_color_glyph_func_t) hb_vector_paint_color_glyph, nullptr, nullptr); + hb_paint_funcs_set_custom_palette_color_func (funcs, (hb_paint_custom_palette_color_func_t) hb_vector_paint_custom_palette_color, nullptr, nullptr); hb_paint_funcs_make_immutable (funcs); hb_atexit (free_static_vector_paint_funcs); return funcs; @@ -1656,12 +1659,44 @@ hb_vector_paint_set_palette (hb_vector_paint_t *paint, paint->palette = palette; } +void +hb_vector_paint_set_custom_palette_color (hb_vector_paint_t *paint, + unsigned color_index, + hb_color_t color) +{ + paint->custom_palette_colors.set (color_index, color); +} + +void +hb_vector_paint_clear_custom_palette_colors (hb_vector_paint_t *paint) +{ + paint->custom_palette_colors.clear (); +} + hb_paint_funcs_t * hb_vector_paint_get_funcs (void) { return hb_vector_paint_funcs_get (); } +static hb_bool_t +hb_vector_paint_custom_palette_color (hb_paint_funcs_t *pfuncs HB_UNUSED, + void *paint_data, + unsigned color_index, + hb_color_t *color, + void *user_data HB_UNUSED) +{ + hb_vector_paint_t *paint = (hb_vector_paint_t *) paint_data; + if (!paint || !color) + return false; + + hb_color_t *value = nullptr; + if (!paint->custom_palette_colors.has (color_index, &value) || !value) + return false; + *color = *value; + return true; +} + hb_bool_t hb_vector_paint_glyph (hb_vector_paint_t *paint, hb_font_t *font, diff --git a/src/hb-vector.h b/src/hb-vector.h index 0e96dd056..20e58e78e 100644 --- a/src/hb-vector.h +++ b/src/hb-vector.h @@ -524,6 +524,32 @@ HB_EXTERN void hb_vector_paint_set_palette (hb_vector_paint_t *paint, int palette); +/** + * hb_vector_paint_set_custom_palette_color: + * @paint: a paint context. + * @color_index: color index to override. + * @color: replacement color. + * + * Overrides one font palette color entry for subsequent paint operations. + * + * XSince: REPLACEME + */ +HB_EXTERN void +hb_vector_paint_set_custom_palette_color (hb_vector_paint_t *paint, + unsigned color_index, + hb_color_t color); + +/** + * hb_vector_paint_clear_custom_palette_colors: + * @paint: a paint context. + * + * Clears all custom palette color overrides. + * + * XSince: REPLACEME + */ +HB_EXTERN void +hb_vector_paint_clear_custom_palette_colors (hb_vector_paint_t *paint); + /** * hb_vector_paint_get_funcs: * diff --git a/util/vector-output.hh b/util/vector-output.hh index b9be49575..838e3b0f4 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -206,6 +206,7 @@ struct vector_output_t : output_options_t<>, view_options_t hb_vector_paint_set_extents (paint, &extents); hb_vector_paint_set_foreground (paint, foreground); hb_vector_paint_set_palette (paint, this->palette); + apply_custom_palette (paint); hb_vector_svg_paint_set_precision (paint, precision); hb_vector_svg_paint_set_flat (paint, flat); @@ -649,6 +650,30 @@ struct vector_output_t : output_options_t<>, view_options_t fputs ("/>\n", out_fp); } + void apply_custom_palette (hb_vector_paint_t *paint) + { + hb_vector_paint_clear_custom_palette_colors (paint); + if (!custom_palette) + return; + + char **entries = g_strsplit (custom_palette, ",", -1); + for (unsigned idx = 0; entries && entries[idx]; idx++) + { + char *entry = g_strstrip (entries[idx]); + if (!*entry) + continue; + + unsigned r = 0, g = 0, b = 0, a = 255; + if (!parse_color (entry, r, g, b, a)) + { + error ("Invalid --custom-palette entry; expected rrggbb or rrggbbaa"); + continue; + } + hb_vector_paint_set_custom_palette_color (paint, idx, HB_COLOR (b, g, r, a)); + } + g_strfreev (entries); + } + void emit_extents_overlay (hb_direction_t dir, float step) { const bool vertical = HB_DIRECTION_IS_VERTICAL (dir); From 1ed094ac68436e07a319429d20b574a1c9689dcc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 16:24:45 -0300 Subject: [PATCH 04/19] [util] Apply rainbow colors per glyph in hb-vector draw output --- util/vector-output.hh | 86 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 79 insertions(+), 7 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index 838e3b0f4..7f3d93740 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -547,6 +547,71 @@ struct vector_output_t : output_options_t<>, view_options_t fputs ("\n", out_fp); } + void emit_fill_stroke_attrs_for_color (hb_color_t color) + { + unsigned r = hb_color_get_red (color); + unsigned g = hb_color_get_green (color); + unsigned b = hb_color_get_blue (color); + unsigned a = hb_color_get_alpha (color); + fprintf (out_fp, " fill=\"#%02X%02X%02X\"", r, g, b); + if (a != 255) + fprintf (out_fp, " fill-opacity=\"%.3f\"", (double) a / 255.); + + const bool has_stroke = stroke_enabled && stroke_width > 0.; + if (has_stroke) + { + fprintf (out_fp, " stroke=\"#%02X%02X%02X\"", + stroke_color.r, stroke_color.g, stroke_color.b); + if (stroke_color.a != 255) + fprintf (out_fp, " stroke-opacity=\"%.3f\"", (double) stroke_color.a / 255.); + fprintf (out_fp, " stroke-width=\"%.*g\" stroke-linecap=\"round\" stroke-linejoin=\"round\"", + precision + 4, stroke_width); + } + } + + void emit_draw_body_with_palette (slice_t body) + { + if (!foreground_palette || !foreground_palette->len || !body.p || !body.len) + return; + + const char *p = body.p; + const char *end = body.p + body.len; + unsigned palette_i = 0; + while (p < end) + { + const char *line_end = (const char *) memchr (p, '\n', (size_t) (end - p)); + if (!line_end) + line_end = end; + + const char *s = p; + while (s < line_end && (*s == ' ' || *s == '\t' || *s == '\r')) + s++; + bool is_elem = s < line_end && *s == '<' && (s + 1 < line_end && s[1] != '/'); + + if (is_elem) + { + const rgba_color_t &c = + g_array_index (foreground_palette, rgba_color_t, + palette_i++ % foreground_palette->len); + hb_color_t col = HB_COLOR (c.b, c.g, c.r, c.a); + fputs ("", out_fp); + fwrite (p, 1, (size_t) (line_end - p), out_fp); + fputs ("\n", out_fp); + } + else + { + fwrite (p, 1, (size_t) (line_end - p), out_fp); + fputc ('\n', out_fp); + } + + p = line_end; + if (p < end && *p == '\n') + p++; + } + } + void write_blob (hb_blob_t *blob, hb_bool_t is_draw_blob) { @@ -575,13 +640,15 @@ struct vector_output_t : output_options_t<>, view_options_t emit_background_rect (); - if (is_draw_blob) + if (is_draw_blob && foreground_use_palette && foreground_palette && foreground_palette->len) + emit_draw_body_with_palette (body); + else if (is_draw_blob) emit_fill_group_open (); - if (body.len) + if (body.len && !(is_draw_blob && foreground_use_palette && foreground_palette && foreground_palette->len)) fwrite (body.p, 1, body.len, out_fp); - if (is_draw_blob) + if (is_draw_blob && !(foreground_use_palette && foreground_palette && foreground_palette->len)) emit_fill_group_close (); fputs ("\n", out_fp); @@ -621,10 +688,15 @@ struct vector_output_t : output_options_t<>, view_options_t emit_background_rect (); - emit_fill_group_open (); - if (draw_body.len) - fwrite (draw_body.p, 1, draw_body.len, out_fp); - emit_fill_group_close (); + if (foreground_use_palette && foreground_palette && foreground_palette->len) + emit_draw_body_with_palette (draw_body); + else + { + emit_fill_group_open (); + if (draw_body.len) + fwrite (draw_body.p, 1, draw_body.len, out_fp); + emit_fill_group_close (); + } if (paint_body.len) fwrite (paint_body.p, 1, paint_body.len, out_fp); From fec0d08b7625a7a6c01d2de31419740efee3333c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 16:26:54 -0300 Subject: [PATCH 05/19] [util] Hoist shared stroke attrs in hb-vector rainbow draw output --- util/vector-output.hh | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index 7f3d93740..f9b07fa3e 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -528,14 +528,7 @@ struct vector_output_t : output_options_t<>, view_options_t fputs (" fill=\"none\"", out_fp); if (has_stroke) - { - fprintf (out_fp, " stroke=\"#%02X%02X%02X\"", - stroke_color.r, stroke_color.g, stroke_color.b); - if (stroke_color.a != 255) - fprintf (out_fp, " stroke-opacity=\"%.3f\"", (double) stroke_color.a / 255.); - fprintf (out_fp, " stroke-width=\"%.*g\" stroke-linecap=\"round\" stroke-linejoin=\"round\"", - precision + 4, stroke_width); - } + emit_stroke_attrs (); fprintf (out_fp, ">\n"); } @@ -557,16 +550,6 @@ struct vector_output_t : output_options_t<>, view_options_t if (a != 255) fprintf (out_fp, " fill-opacity=\"%.3f\"", (double) a / 255.); - const bool has_stroke = stroke_enabled && stroke_width > 0.; - if (has_stroke) - { - fprintf (out_fp, " stroke=\"#%02X%02X%02X\"", - stroke_color.r, stroke_color.g, stroke_color.b); - if (stroke_color.a != 255) - fprintf (out_fp, " stroke-opacity=\"%.3f\"", (double) stroke_color.a / 255.); - fprintf (out_fp, " stroke-width=\"%.*g\" stroke-linecap=\"round\" stroke-linejoin=\"round\"", - precision + 4, stroke_width); - } } void emit_draw_body_with_palette (slice_t body) @@ -577,6 +560,13 @@ struct vector_output_t : output_options_t<>, view_options_t const char *p = body.p; const char *end = body.p + body.len; unsigned palette_i = 0; + const bool has_stroke = stroke_enabled && stroke_width > 0.; + if (has_stroke) + { + fputs ("\n", out_fp); + } while (p < end) { const char *line_end = (const char *) memchr (p, '\n', (size_t) (end - p)); @@ -610,6 +600,18 @@ struct vector_output_t : output_options_t<>, view_options_t if (p < end && *p == '\n') p++; } + if (has_stroke) + fputs ("\n", out_fp); + } + + void emit_stroke_attrs () + { + fprintf (out_fp, " stroke=\"#%02X%02X%02X\"", + stroke_color.r, stroke_color.g, stroke_color.b); + if (stroke_color.a != 255) + fprintf (out_fp, " stroke-opacity=\"%.3f\"", (double) stroke_color.a / 255.); + fprintf (out_fp, " stroke-width=\"%.*g\" stroke-linecap=\"round\" stroke-linejoin=\"round\"", + precision + 4, stroke_width); } void write_blob (hb_blob_t *blob, From 9b3583f2f69da2482cb2a47e92eaa5d3b41c528f Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 16:30:33 -0300 Subject: [PATCH 06/19] [util] Emit CPAL CSS vars for SVG var(--colorN) in hb-vector --- util/vector-output.hh | 88 ++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 83 insertions(+), 5 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index f9b07fa3e..348d803df 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -106,6 +106,9 @@ struct vector_output_t : output_options_t<>, view_options_t background = HB_COLOR (b, g, r, a); has_background = true; } + + if (!parse_custom_palette_overrides (error)) + return; } template @@ -614,6 +617,58 @@ struct vector_output_t : output_options_t<>, view_options_t precision + 4, stroke_width); } + void emit_palette_css_vars () + { + hb_face_t *face = hb_font_get_face (upem_font ? upem_font : font); + if (!face) + return; + + unsigned palette_count = hb_ot_color_palette_get_count (face); + if (!palette_count && custom_palette_values.empty ()) + return; + + unsigned palette_index = palette >= 0 ? (unsigned) palette : 0u; + if (palette_count && palette_index >= palette_count) + palette_index = 0; + + unsigned color_count = palette_count + ? hb_ot_color_palette_get_colors (face, palette_index, 0, nullptr, nullptr) + : 0u; + unsigned max_colors = hb_max (color_count, (unsigned) custom_palette_values.size ()); + if (!max_colors) + return; + + fputs ("\n", out_fp); + } + void write_blob (hb_blob_t *blob, hb_bool_t is_draw_blob) { @@ -641,6 +696,7 @@ struct vector_output_t : output_options_t<>, view_options_t } emit_background_rect (); + emit_palette_css_vars (); if (is_draw_blob && foreground_use_palette && foreground_palette && foreground_palette->len) emit_draw_body_with_palette (body); @@ -689,6 +745,7 @@ struct vector_output_t : output_options_t<>, view_options_t } emit_background_rect (); + emit_palette_css_vars (); if (foreground_use_palette && foreground_palette && foreground_palette->len) emit_draw_body_with_palette (draw_body); @@ -727,8 +784,17 @@ struct vector_output_t : output_options_t<>, view_options_t void apply_custom_palette (hb_vector_paint_t *paint) { hb_vector_paint_clear_custom_palette_colors (paint); - if (!custom_palette) - return; + for (unsigned idx = 0; idx < custom_palette_values.size (); idx++) + if (idx < custom_palette_has_value.size () && custom_palette_has_value[idx]) + hb_vector_paint_set_custom_palette_color (paint, idx, custom_palette_values[idx]); + } + + bool parse_custom_palette_overrides (GError **error) + { + custom_palette_values.clear (); + custom_palette_has_value.clear (); + if (!custom_palette || !*custom_palette) + return true; char **entries = g_strsplit (custom_palette, ",", -1); for (unsigned idx = 0; entries && entries[idx]; idx++) @@ -740,12 +806,22 @@ struct vector_output_t : output_options_t<>, view_options_t unsigned r = 0, g = 0, b = 0, a = 255; if (!parse_color (entry, r, g, b, a)) { - error ("Invalid --custom-palette entry; expected rrggbb or rrggbbaa"); - continue; + g_strfreev (entries); + g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, + "Invalid --custom-palette entry; expected rrggbb or rrggbbaa"); + return false; } - hb_vector_paint_set_custom_palette_color (paint, idx, HB_COLOR (b, g, r, a)); + + if (custom_palette_values.size () <= idx) + { + custom_palette_values.resize (idx + 1, HB_COLOR (0, 0, 0, 255)); + custom_palette_has_value.resize (idx + 1, false); + } + custom_palette_values[idx] = HB_COLOR (b, g, r, a); + custom_palette_has_value[idx] = true; } g_strfreev (entries); + return true; } void emit_extents_overlay (hb_direction_t dir, float step) @@ -828,6 +904,8 @@ struct vector_output_t : output_options_t<>, view_options_t unsigned upem = 0; unsigned subpixel_bits = 0; hb_vector_extents_t final_extents = {0, 0, 1, 1}; + std::vector custom_palette_values; + std::vector custom_palette_has_value; }; #endif From a4a7f9af84a5cb74cf3ed01cc26e3216dd01ddeb Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 16:32:11 -0300 Subject: [PATCH 07/19] [util] Resolve SVG var(--colorN) to concrete colors in hb-vector output --- util/vector-output.hh | 116 ++++++++++++++++++++++++++++-------------- 1 file changed, 77 insertions(+), 39 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index 348d803df..a18f0f17b 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -617,56 +617,96 @@ struct vector_output_t : output_options_t<>, view_options_t precision + 4, stroke_width); } - void emit_palette_css_vars () + bool lookup_palette_color (unsigned idx, hb_color_t *color) const { + if (idx < custom_palette_has_value.size () && custom_palette_has_value[idx]) + { + *color = custom_palette_values[idx]; + return true; + } + hb_face_t *face = hb_font_get_face (upem_font ? upem_font : font); if (!face) - return; + return false; unsigned palette_count = hb_ot_color_palette_get_count (face); - if (!palette_count && custom_palette_values.empty ()) - return; + if (!palette_count) + return false; unsigned palette_index = palette >= 0 ? (unsigned) palette : 0u; - if (palette_count && palette_index >= palette_count) + if (palette_index >= palette_count) palette_index = 0; - unsigned color_count = palette_count - ? hb_ot_color_palette_get_colors (face, palette_index, 0, nullptr, nullptr) - : 0u; - unsigned max_colors = hb_max (color_count, (unsigned) custom_palette_values.size ()); - if (!max_colors) + unsigned n = 1; + if (!hb_ot_color_palette_get_colors (face, palette_index, idx, &n, color) || !n) + return false; + return true; + } + + void write_slice_resolving_palette_vars (slice_t s) + { + if (!s.p || !s.len) return; - fputs ("\n", out_fp); } void write_blob (hb_blob_t *blob, @@ -691,12 +731,11 @@ struct vector_output_t : output_options_t<>, view_options_t if (defs.len) { fputs ("\n", out_fp); - fwrite (defs.p, 1, defs.len, out_fp); + write_slice_resolving_palette_vars (defs); fputs ("\n", out_fp); } emit_background_rect (); - emit_palette_css_vars (); if (is_draw_blob && foreground_use_palette && foreground_palette && foreground_palette->len) emit_draw_body_with_palette (body); @@ -704,7 +743,7 @@ struct vector_output_t : output_options_t<>, view_options_t emit_fill_group_open (); if (body.len && !(is_draw_blob && foreground_use_palette && foreground_palette && foreground_palette->len)) - fwrite (body.p, 1, body.len, out_fp); + write_slice_resolving_palette_vars (body); if (is_draw_blob && !(foreground_use_palette && foreground_palette && foreground_palette->len)) emit_fill_group_close (); @@ -738,14 +777,13 @@ struct vector_output_t : output_options_t<>, view_options_t { fputs ("\n", out_fp); if (draw_defs.len) - fwrite (draw_defs.p, 1, draw_defs.len, out_fp); + write_slice_resolving_palette_vars (draw_defs); if (paint_defs.len) - fwrite (paint_defs.p, 1, paint_defs.len, out_fp); + write_slice_resolving_palette_vars (paint_defs); fputs ("\n", out_fp); } emit_background_rect (); - emit_palette_css_vars (); if (foreground_use_palette && foreground_palette && foreground_palette->len) emit_draw_body_with_palette (draw_body); @@ -757,7 +795,7 @@ struct vector_output_t : output_options_t<>, view_options_t emit_fill_group_close (); } if (paint_body.len) - fwrite (paint_body.p, 1, paint_body.len, out_fp); + write_slice_resolving_palette_vars (paint_body); fputs ("\n", out_fp); } From 2c4c86275863a5a5dd211441fc246f8e74593474 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 16:52:25 -0300 Subject: [PATCH 08/19] [util] Keep hb-vector default margin at zero --- util/vector-output.hh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/util/vector-output.hh b/util/vector-output.hh index a18f0f17b..91064281f 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -39,6 +39,11 @@ struct vector_output_t : output_options_t<>, view_options_t { static const bool repeat_shape = false; + vector_output_t () + { + margin = {0., 0., 0., 0.}; + } + ~vector_output_t () { hb_font_destroy (font); From 12989d4e6ad56d22893b636eccbf3d21f049e854 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 16:54:28 -0300 Subject: [PATCH 09/19] [util] Align vector/raster defaults with view and update vector baselines --- test/vector/data/expected/basic/cff1.svg | 6 +++--- test/vector/data/expected/basic/cff2-var-cntr.svg | 6 +++--- test/vector/data/expected/basic/cff2-var-wght900.svg | 6 +++--- test/vector/data/expected/basic/default.svg | 2 +- test/vector/data/expected/basic/flat-cff1.svg | 6 +++--- test/vector/data/expected/basic/flat-precision-0.svg | 2 +- test/vector/data/expected/basic/flat-precision-4.svg | 2 +- test/vector/data/expected/basic/flat.svg | 2 +- test/vector/data/expected/basic/glyf-var-wght900.svg | 2 +- test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg | 8 ++++---- test/vector/data/expected/basic/multiline.svg | 2 +- test/vector/data/expected/basic/ttb.svg | 2 +- test/vector/data/expected/basic/upem-default.svg | 2 +- test/vector/data/expected/varc/conditional-default.svg | 2 +- test/vector/data/expected/varc/conditional-wght840p3.svg | 2 +- test/vector/data/expected/varc/default.svg | 4 ++-- test/vector/data/expected/varc/wght840p3.svg | 4 ++-- util/hb-raster.cc | 2 +- util/hb-vector.cc | 2 +- util/vector-output.hh | 5 ----- 20 files changed, 32 insertions(+), 37 deletions(-) diff --git a/test/vector/data/expected/basic/cff1.svg b/test/vector/data/expected/basic/cff1.svg index 059070e59..3cfaea262 100644 --- a/test/vector/data/expected/basic/cff1.svg +++ b/test/vector/data/expected/basic/cff1.svg @@ -1,4 +1,4 @@ - + @@ -7,6 +7,6 @@ - - + + diff --git a/test/vector/data/expected/basic/cff2-var-cntr.svg b/test/vector/data/expected/basic/cff2-var-cntr.svg index 8b9fabcd3..0079c1e47 100644 --- a/test/vector/data/expected/basic/cff2-var-cntr.svg +++ b/test/vector/data/expected/basic/cff2-var-cntr.svg @@ -1,4 +1,4 @@ - + @@ -7,6 +7,6 @@ - - + + diff --git a/test/vector/data/expected/basic/cff2-var-wght900.svg b/test/vector/data/expected/basic/cff2-var-wght900.svg index 8d24be661..d0b58f275 100644 --- a/test/vector/data/expected/basic/cff2-var-wght900.svg +++ b/test/vector/data/expected/basic/cff2-var-wght900.svg @@ -1,10 +1,10 @@ - + - - + + diff --git a/test/vector/data/expected/basic/default.svg b/test/vector/data/expected/basic/default.svg index a80b2ce85..b77a87de2 100644 --- a/test/vector/data/expected/basic/default.svg +++ b/test/vector/data/expected/basic/default.svg @@ -1,4 +1,4 @@ - + diff --git a/test/vector/data/expected/basic/flat-cff1.svg b/test/vector/data/expected/basic/flat-cff1.svg index 209abd225..9b6f42888 100644 --- a/test/vector/data/expected/basic/flat-cff1.svg +++ b/test/vector/data/expected/basic/flat-cff1.svg @@ -1,6 +1,6 @@ - + - - + + diff --git a/test/vector/data/expected/basic/flat-precision-0.svg b/test/vector/data/expected/basic/flat-precision-0.svg index 7c52efcc9..eabb07d73 100644 --- a/test/vector/data/expected/basic/flat-precision-0.svg +++ b/test/vector/data/expected/basic/flat-precision-0.svg @@ -1,4 +1,4 @@ - + diff --git a/test/vector/data/expected/basic/flat-precision-4.svg b/test/vector/data/expected/basic/flat-precision-4.svg index 66e057540..7cc0510a0 100644 --- a/test/vector/data/expected/basic/flat-precision-4.svg +++ b/test/vector/data/expected/basic/flat-precision-4.svg @@ -1,4 +1,4 @@ - + diff --git a/test/vector/data/expected/basic/flat.svg b/test/vector/data/expected/basic/flat.svg index 66e057540..7cc0510a0 100644 --- a/test/vector/data/expected/basic/flat.svg +++ b/test/vector/data/expected/basic/flat.svg @@ -1,4 +1,4 @@ - + diff --git a/test/vector/data/expected/basic/glyf-var-wght900.svg b/test/vector/data/expected/basic/glyf-var-wght900.svg index 43588876d..b0f36f456 100644 --- a/test/vector/data/expected/basic/glyf-var-wght900.svg +++ b/test/vector/data/expected/basic/glyf-var-wght900.svg @@ -1,4 +1,4 @@ - + diff --git a/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg b/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg index b55c4257c..8aa652089 100644 --- a/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg +++ b/test/vector/data/expected/basic/glyf-vvar-ttb-wght900.svg @@ -1,4 +1,4 @@ - + @@ -6,7 +6,7 @@ - - - + + + diff --git a/test/vector/data/expected/basic/multiline.svg b/test/vector/data/expected/basic/multiline.svg index 4e0ce6c52..f58695d27 100644 --- a/test/vector/data/expected/basic/multiline.svg +++ b/test/vector/data/expected/basic/multiline.svg @@ -1,4 +1,4 @@ - + diff --git a/test/vector/data/expected/basic/ttb.svg b/test/vector/data/expected/basic/ttb.svg index 094bf6e4d..4b18afc2f 100644 --- a/test/vector/data/expected/basic/ttb.svg +++ b/test/vector/data/expected/basic/ttb.svg @@ -1,4 +1,4 @@ - + diff --git a/test/vector/data/expected/basic/upem-default.svg b/test/vector/data/expected/basic/upem-default.svg index a80b2ce85..b77a87de2 100644 --- a/test/vector/data/expected/basic/upem-default.svg +++ b/test/vector/data/expected/basic/upem-default.svg @@ -1,4 +1,4 @@ - + diff --git a/test/vector/data/expected/varc/conditional-default.svg b/test/vector/data/expected/varc/conditional-default.svg index 515a25554..0dba2a6c2 100644 --- a/test/vector/data/expected/varc/conditional-default.svg +++ b/test/vector/data/expected/varc/conditional-default.svg @@ -1,4 +1,4 @@ - + diff --git a/test/vector/data/expected/varc/conditional-wght840p3.svg b/test/vector/data/expected/varc/conditional-wght840p3.svg index 11b23332d..d9b86f1a8 100644 --- a/test/vector/data/expected/varc/conditional-wght840p3.svg +++ b/test/vector/data/expected/varc/conditional-wght840p3.svg @@ -1,4 +1,4 @@ - + diff --git a/test/vector/data/expected/varc/default.svg b/test/vector/data/expected/varc/default.svg index 61f69a399..7f7bc048c 100644 --- a/test/vector/data/expected/varc/default.svg +++ b/test/vector/data/expected/varc/default.svg @@ -1,4 +1,4 @@ - + @@ -6,5 +6,5 @@ - + diff --git a/test/vector/data/expected/varc/wght840p3.svg b/test/vector/data/expected/varc/wght840p3.svg index 63c11f933..8aecd4e38 100644 --- a/test/vector/data/expected/varc/wght840p3.svg +++ b/test/vector/data/expected/varc/wght840p3.svg @@ -1,4 +1,4 @@ - + @@ -6,5 +6,5 @@ - + diff --git a/util/hb-raster.cc b/util/hb-raster.cc index 5973fd918..7d70594a9 100644 --- a/util/hb-raster.cc +++ b/util/hb-raster.cc @@ -31,7 +31,7 @@ #include "shape-consumer.hh" #include "text-options.hh" -const unsigned DEFAULT_FONT_SIZE = FONT_SIZE_UPEM; +const unsigned DEFAULT_FONT_SIZE = 256; const unsigned SUBPIXEL_BITS = 6; int diff --git a/util/hb-vector.cc b/util/hb-vector.cc index a042ebc92..059e89c8d 100644 --- a/util/hb-vector.cc +++ b/util/hb-vector.cc @@ -29,7 +29,7 @@ #include "shape-consumer.hh" #include "text-options.hh" -const unsigned DEFAULT_FONT_SIZE = FONT_SIZE_UPEM; +const unsigned DEFAULT_FONT_SIZE = 256; const unsigned SUBPIXEL_BITS = 6; int diff --git a/util/vector-output.hh b/util/vector-output.hh index 91064281f..a18f0f17b 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -39,11 +39,6 @@ struct vector_output_t : output_options_t<>, view_options_t { static const bool repeat_shape = false; - vector_output_t () - { - margin = {0., 0., 0., 0.}; - } - ~vector_output_t () { hb_font_destroy (font); From 412f215d021b66025645a43b81bcfed3dce844bc Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 16:58:01 -0300 Subject: [PATCH 10/19] [util] Harden hb-vector var(--colorN) rewrite parser --- util/vector-output.hh | 91 ++++++++++++++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 15 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index a18f0f17b..132b01519 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -643,6 +643,23 @@ struct vector_output_t : output_options_t<>, view_options_t return true; } + static inline const char * + skip_ascii_ws (const char *p, const char *end) + { + while (p < end && (*p == ' ' || *p == '\t' || *p == '\n' || *p == '\r')) + p++; + return p; + } + + static inline const char * + trim_ascii_ws_right (const char *begin, const char *end) + { + while (end > begin && + (end[-1] == ' ' || end[-1] == '\t' || end[-1] == '\n' || end[-1] == '\r')) + end--; + return end; + } + void write_slice_resolving_palette_vars (slice_t s) { if (!s.p || !s.len) @@ -653,8 +670,8 @@ struct vector_output_t : output_options_t<>, view_options_t while (p < end) { const char *match = nullptr; - for (const char *q = p; q + 11 <= end; q++) - if (memcmp (q, "var(--color", 11) == 0) + for (const char *q = p; q + 4 <= end; q++) + if (memcmp (q, "var(", 4) == 0) { match = q; break; @@ -669,24 +686,67 @@ struct vector_output_t : output_options_t<>, view_options_t if (match > p) fwrite (p, 1, (size_t) (match - p), out_fp); - const char *q = match + 11; + const char *expr_close = nullptr; + unsigned depth = 1; + for (const char *q = match + 4; q < end; q++) + { + if (*q == '(') depth++; + else if (*q == ')') + { + depth--; + if (!depth) + { + expr_close = q; + break; + } + } + } + + if (!expr_close) + { + fwrite (match, 1, (size_t) (end - match), out_fp); + return; + } + + const char *inside = match + 4; + const char *inside_end = expr_close; + const char *q = skip_ascii_ws (inside, inside_end); unsigned idx = 0; bool have_digits = false; - while (q < end && *q >= '0' && *q <= '9') + bool parse_ok = true; + + if (!(q + 7 <= inside_end && memcmp (q, "--color", 7) == 0)) + parse_ok = false; + if (parse_ok) + q += 7; + + while (parse_ok && q < inside_end && *q >= '0' && *q <= '9') { have_digits = true; idx = idx * 10 + (unsigned) (*q - '0'); q++; } + q = skip_ascii_ws (q, inside_end); - const char *expr_end = q; - while (expr_end < end && *expr_end != ')') - expr_end++; - if (expr_end < end) - expr_end++; + bool has_fallback = false; + const char *fallback_begin = nullptr; + const char *fallback_end = nullptr; + if (parse_ok && q < inside_end && *q == ',') + { + has_fallback = true; + q++; + fallback_begin = skip_ascii_ws (q, inside_end); + fallback_end = trim_ascii_ws_right (fallback_begin, inside_end); + q = inside_end; + } + + if (parse_ok && q != inside_end) + parse_ok = false; + + const char *expr_end = expr_close + 1; hb_color_t color; - if (have_digits && expr_end <= end && lookup_palette_color (idx, &color)) + if (parse_ok && have_digits && lookup_palette_color (idx, &color)) { unsigned r = hb_color_get_red (color); unsigned g = hb_color_get_green (color); @@ -697,15 +757,16 @@ struct vector_output_t : output_options_t<>, view_options_t else fprintf (out_fp, "rgba(%u,%u,%u,%.6g)", r, g, b, (double) a / 255.); } + else if (has_fallback && fallback_begin && fallback_end >= fallback_begin) + { + fwrite (fallback_begin, 1, (size_t) (fallback_end - fallback_begin), out_fp); + } else { - if (expr_end <= end) - fwrite (match, 1, (size_t) (expr_end - match), out_fp); - else - fwrite (match, 1, (size_t) (end - match), out_fp); + fwrite (match, 1, (size_t) (expr_end - match), out_fp); } - p = expr_end <= end ? expr_end : end; + p = expr_end; } } From 90bd83dfc2a4e04f1903d2b7b8738b2831e273e2 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 17:00:09 -0300 Subject: [PATCH 11/19] [util] Cache active CPAL colors for hb-vector SVG var rewrite --- util/vector-output.hh | 80 ++++++++++++++++++++++++++++++++----------- 1 file changed, 60 insertions(+), 20 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index 132b01519..6afb2620e 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -25,6 +25,7 @@ #ifndef HB_VECTOR_OUTPUT_HH #define HB_VECTOR_OUTPUT_HH +#include #include #include #include @@ -210,6 +211,7 @@ struct vector_output_t : output_options_t<>, view_options_t hb_vector_paint_set_foreground (paint, foreground); hb_vector_paint_set_palette (paint, this->palette); apply_custom_palette (paint); + init_palette_color_cache (); hb_vector_svg_paint_set_precision (paint, precision); hb_vector_svg_paint_set_flat (paint, flat); @@ -619,27 +621,9 @@ struct vector_output_t : output_options_t<>, view_options_t bool lookup_palette_color (unsigned idx, hb_color_t *color) const { - if (idx < custom_palette_has_value.size () && custom_palette_has_value[idx]) - { - *color = custom_palette_values[idx]; - return true; - } - - hb_face_t *face = hb_font_get_face (upem_font ? upem_font : font); - if (!face) - return false; - - unsigned palette_count = hb_ot_color_palette_get_count (face); - if (!palette_count) - return false; - - unsigned palette_index = palette >= 0 ? (unsigned) palette : 0u; - if (palette_index >= palette_count) - palette_index = 0; - - unsigned n = 1; - if (!hb_ot_color_palette_get_colors (face, palette_index, idx, &n, color) || !n) + if (idx >= palette_cache_has_value.size () || !palette_cache_has_value[idx]) return false; + *color = palette_cache_values[idx]; return true; } @@ -888,6 +872,60 @@ struct vector_output_t : output_options_t<>, view_options_t hb_vector_paint_set_custom_palette_color (paint, idx, custom_palette_values[idx]); } + void init_palette_color_cache () + { + palette_cache_values.clear (); + palette_cache_has_value.clear (); + + hb_face_t *face = hb_font_get_face (upem_font ? upem_font : font); + if (face) + { + unsigned palette_count = hb_ot_color_palette_get_count (face); + if (palette_count) + { + unsigned palette_index = palette >= 0 ? (unsigned) palette : 0u; + if (palette_index >= palette_count) + palette_index = 0; + + unsigned color_count = hb_ot_color_palette_get_colors (face, palette_index, 0, + nullptr, nullptr); + if (color_count) + { + palette_cache_values.resize (color_count, HB_COLOR (0, 0, 0, 255)); + palette_cache_has_value.resize (color_count, false); + unsigned n = color_count; + if (hb_ot_color_palette_get_colors (face, palette_index, 0, &n, + palette_cache_values.data ()) && n) + { + if (n < palette_cache_values.size ()) + palette_cache_values.resize (n); + palette_cache_has_value.resize (palette_cache_values.size (), false); + std::fill (palette_cache_has_value.begin (), + palette_cache_has_value.end (), true); + } + else + { + palette_cache_values.clear (); + palette_cache_has_value.clear (); + } + } + } + } + + for (unsigned idx = 0; idx < custom_palette_values.size (); idx++) + { + if (idx >= custom_palette_has_value.size () || !custom_palette_has_value[idx]) + continue; + if (palette_cache_values.size () <= idx) + { + palette_cache_values.resize (idx + 1, HB_COLOR (0, 0, 0, 255)); + palette_cache_has_value.resize (idx + 1, false); + } + palette_cache_values[idx] = custom_palette_values[idx]; + palette_cache_has_value[idx] = true; + } + } + bool parse_custom_palette_overrides (GError **error) { custom_palette_values.clear (); @@ -1005,6 +1043,8 @@ struct vector_output_t : output_options_t<>, view_options_t hb_vector_extents_t final_extents = {0, 0, 1, 1}; std::vector custom_palette_values; std::vector custom_palette_has_value; + std::vector palette_cache_values; + std::vector palette_cache_has_value; }; #endif From f7a0ca52ff4de0c9c72a2201bea20fa232c50d27 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 17:00:51 -0300 Subject: [PATCH 12/19] [util] Refactor hb-vector palette var rewrite helpers --- util/vector-output.hh | 176 ++++++++++++++++++++++++------------------ 1 file changed, 102 insertions(+), 74 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index 6afb2620e..c4ae3b218 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -644,6 +644,97 @@ struct vector_output_t : output_options_t<>, view_options_t return end; } + struct parsed_var_color_expr_t + { + const char *expr_end = nullptr; + const char *fallback_begin = nullptr; + const char *fallback_end = nullptr; + unsigned idx = 0; + bool parse_ok = false; + bool have_digits = false; + bool has_fallback = false; + }; + + static inline const char * + find_next_var_expr (const char *p, const char *end) + { + for (const char *q = p; q + 4 <= end; q++) + if (memcmp (q, "var(", 4) == 0) + return q; + return nullptr; + } + + static inline const char * + find_matching_paren (const char *inside, const char *end) + { + unsigned depth = 1; + for (const char *q = inside; q < end; q++) + { + if (*q == '(') depth++; + else if (*q == ')') + { + depth--; + if (!depth) + return q; + } + } + return nullptr; + } + + static inline parsed_var_color_expr_t + parse_var_color_expr (const char *match, const char *end) + { + parsed_var_color_expr_t parsed; + const char *expr_close = find_matching_paren (match + 4, end); + if (!expr_close) + return parsed; + + parsed.expr_end = expr_close + 1; + const char *inside = match + 4; + const char *inside_end = expr_close; + const char *q = skip_ascii_ws (inside, inside_end); + parsed.parse_ok = true; + + if (!(q + 7 <= inside_end && memcmp (q, "--color", 7) == 0)) + parsed.parse_ok = false; + if (parsed.parse_ok) + q += 7; + + while (parsed.parse_ok && q < inside_end && *q >= '0' && *q <= '9') + { + parsed.have_digits = true; + parsed.idx = parsed.idx * 10 + (unsigned) (*q - '0'); + q++; + } + q = skip_ascii_ws (q, inside_end); + + if (parsed.parse_ok && q < inside_end && *q == ',') + { + parsed.has_fallback = true; + q++; + parsed.fallback_begin = skip_ascii_ws (q, inside_end); + parsed.fallback_end = trim_ascii_ws_right (parsed.fallback_begin, inside_end); + q = inside_end; + } + + if (parsed.parse_ok && q != inside_end) + parsed.parse_ok = false; + + return parsed; + } + + void emit_css_color (hb_color_t color) + { + unsigned r = hb_color_get_red (color); + unsigned g = hb_color_get_green (color); + unsigned b = hb_color_get_blue (color); + unsigned a = hb_color_get_alpha (color); + if (a == 255) + fprintf (out_fp, "#%02X%02X%02X", r, g, b); + else + fprintf (out_fp, "rgba(%u,%u,%u,%.6g)", r, g, b, (double) a / 255.); + } + void write_slice_resolving_palette_vars (slice_t s) { if (!s.p || !s.len) @@ -653,13 +744,7 @@ struct vector_output_t : output_options_t<>, view_options_t const char *end = s.p + s.len; while (p < end) { - const char *match = nullptr; - for (const char *q = p; q + 4 <= end; q++) - if (memcmp (q, "var(", 4) == 0) - { - match = q; - break; - } + const char *match = find_next_var_expr (p, end); if (!match) { @@ -670,87 +755,30 @@ struct vector_output_t : output_options_t<>, view_options_t if (match > p) fwrite (p, 1, (size_t) (match - p), out_fp); - const char *expr_close = nullptr; - unsigned depth = 1; - for (const char *q = match + 4; q < end; q++) - { - if (*q == '(') depth++; - else if (*q == ')') - { - depth--; - if (!depth) - { - expr_close = q; - break; - } - } - } - - if (!expr_close) + parsed_var_color_expr_t parsed = parse_var_color_expr (match, end); + if (!parsed.expr_end) { fwrite (match, 1, (size_t) (end - match), out_fp); return; } - const char *inside = match + 4; - const char *inside_end = expr_close; - const char *q = skip_ascii_ws (inside, inside_end); - unsigned idx = 0; - bool have_digits = false; - bool parse_ok = true; - - if (!(q + 7 <= inside_end && memcmp (q, "--color", 7) == 0)) - parse_ok = false; - if (parse_ok) - q += 7; - - while (parse_ok && q < inside_end && *q >= '0' && *q <= '9') - { - have_digits = true; - idx = idx * 10 + (unsigned) (*q - '0'); - q++; - } - q = skip_ascii_ws (q, inside_end); - - bool has_fallback = false; - const char *fallback_begin = nullptr; - const char *fallback_end = nullptr; - if (parse_ok && q < inside_end && *q == ',') - { - has_fallback = true; - q++; - fallback_begin = skip_ascii_ws (q, inside_end); - fallback_end = trim_ascii_ws_right (fallback_begin, inside_end); - q = inside_end; - } - - if (parse_ok && q != inside_end) - parse_ok = false; - - const char *expr_end = expr_close + 1; - hb_color_t color; - if (parse_ok && have_digits && lookup_palette_color (idx, &color)) + if (parsed.parse_ok && parsed.have_digits && lookup_palette_color (parsed.idx, &color)) { - unsigned r = hb_color_get_red (color); - unsigned g = hb_color_get_green (color); - unsigned b = hb_color_get_blue (color); - unsigned a = hb_color_get_alpha (color); - if (a == 255) - fprintf (out_fp, "#%02X%02X%02X", r, g, b); - else - fprintf (out_fp, "rgba(%u,%u,%u,%.6g)", r, g, b, (double) a / 255.); + emit_css_color (color); } - else if (has_fallback && fallback_begin && fallback_end >= fallback_begin) + else if (parsed.has_fallback && parsed.fallback_begin && + parsed.fallback_end >= parsed.fallback_begin) { - fwrite (fallback_begin, 1, (size_t) (fallback_end - fallback_begin), out_fp); + fwrite (parsed.fallback_begin, 1, + (size_t) (parsed.fallback_end - parsed.fallback_begin), out_fp); } else { - fwrite (match, 1, (size_t) (expr_end - match), out_fp); + fwrite (match, 1, (size_t) (parsed.expr_end - match), out_fp); } - p = expr_end; + p = parsed.expr_end; } } From 23e2c603f44e76915058a7975da85c04d592c9c0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 17:01:43 -0300 Subject: [PATCH 13/19] [util] Resolve hb-vector CPAL vars with lazy per-index cache --- util/vector-output.hh | 106 ++++++++++++++++++++++-------------------- 1 file changed, 56 insertions(+), 50 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index c4ae3b218..b99c70d4a 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -619,12 +619,44 @@ struct vector_output_t : output_options_t<>, view_options_t precision + 4, stroke_width); } - bool lookup_palette_color (unsigned idx, hb_color_t *color) const + bool lookup_palette_color (unsigned idx, hb_color_t *color) { - if (idx >= palette_cache_has_value.size () || !palette_cache_has_value[idx]) + if (idx < custom_palette_has_value.size () && custom_palette_has_value[idx]) + { + *color = custom_palette_values[idx]; + return true; + } + + if (!palette_lookup_enabled) return false; - *color = palette_cache_values[idx]; - return true; + + if (palette_cache_state.size () <= idx) + { + palette_cache_state.resize (idx + 1, 0); + palette_cache_values.resize (idx + 1, HB_COLOR (0, 0, 0, 255)); + } + + if (palette_cache_state[idx] == 1) + { + *color = palette_cache_values[idx]; + return true; + } + if (palette_cache_state[idx] == 2) + return false; + + hb_color_t fetched = HB_COLOR (0, 0, 0, 255); + unsigned n = 1; + if (hb_ot_color_palette_get_colors (palette_face, palette_lookup_index, + idx, &n, &fetched) && n) + { + palette_cache_values[idx] = fetched; + palette_cache_state[idx] = 1; + *color = fetched; + return true; + } + + palette_cache_state[idx] = 2; + return false; } static inline const char * @@ -903,55 +935,26 @@ struct vector_output_t : output_options_t<>, view_options_t void init_palette_color_cache () { palette_cache_values.clear (); - palette_cache_has_value.clear (); + palette_cache_state.clear (); + palette_face = nullptr; + palette_lookup_index = 0; + palette_lookup_enabled = false; hb_face_t *face = hb_font_get_face (upem_font ? upem_font : font); - if (face) - { - unsigned palette_count = hb_ot_color_palette_get_count (face); - if (palette_count) - { - unsigned palette_index = palette >= 0 ? (unsigned) palette : 0u; - if (palette_index >= palette_count) - palette_index = 0; + if (!face) + return; - unsigned color_count = hb_ot_color_palette_get_colors (face, palette_index, 0, - nullptr, nullptr); - if (color_count) - { - palette_cache_values.resize (color_count, HB_COLOR (0, 0, 0, 255)); - palette_cache_has_value.resize (color_count, false); - unsigned n = color_count; - if (hb_ot_color_palette_get_colors (face, palette_index, 0, &n, - palette_cache_values.data ()) && n) - { - if (n < palette_cache_values.size ()) - palette_cache_values.resize (n); - palette_cache_has_value.resize (palette_cache_values.size (), false); - std::fill (palette_cache_has_value.begin (), - palette_cache_has_value.end (), true); - } - else - { - palette_cache_values.clear (); - palette_cache_has_value.clear (); - } - } - } - } + unsigned palette_count = hb_ot_color_palette_get_count (face); + if (!palette_count) + return; - for (unsigned idx = 0; idx < custom_palette_values.size (); idx++) - { - if (idx >= custom_palette_has_value.size () || !custom_palette_has_value[idx]) - continue; - if (palette_cache_values.size () <= idx) - { - palette_cache_values.resize (idx + 1, HB_COLOR (0, 0, 0, 255)); - palette_cache_has_value.resize (idx + 1, false); - } - palette_cache_values[idx] = custom_palette_values[idx]; - palette_cache_has_value[idx] = true; - } + unsigned palette_index = palette >= 0 ? (unsigned) palette : 0u; + if (palette_index >= palette_count) + palette_index = 0; + + palette_face = face; + palette_lookup_index = palette_index; + palette_lookup_enabled = true; } bool parse_custom_palette_overrides (GError **error) @@ -1072,7 +1075,10 @@ struct vector_output_t : output_options_t<>, view_options_t std::vector custom_palette_values; std::vector custom_palette_has_value; std::vector palette_cache_values; - std::vector palette_cache_has_value; + std::vector palette_cache_state; + hb_face_t *palette_face = nullptr; + unsigned palette_lookup_index = 0; + bool palette_lookup_enabled = false; }; #endif From 1e857b2e5f8c7c07deb7fb3959dc26a482ba8643 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 17:02:37 -0300 Subject: [PATCH 14/19] [util] Make hb-vector palette var cache mutable --- util/vector-output.hh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/util/vector-output.hh b/util/vector-output.hh index b99c70d4a..b6dd7c919 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -619,7 +619,7 @@ struct vector_output_t : output_options_t<>, view_options_t precision + 4, stroke_width); } - bool lookup_palette_color (unsigned idx, hb_color_t *color) + bool lookup_palette_color (unsigned idx, hb_color_t *color) const { if (idx < custom_palette_has_value.size () && custom_palette_has_value[idx]) { @@ -1074,8 +1074,8 @@ struct vector_output_t : output_options_t<>, view_options_t hb_vector_extents_t final_extents = {0, 0, 1, 1}; std::vector custom_palette_values; std::vector custom_palette_has_value; - std::vector palette_cache_values; - std::vector palette_cache_state; + mutable std::vector palette_cache_values; + mutable std::vector palette_cache_state; hb_face_t *palette_face = nullptr; unsigned palette_lookup_index = 0; bool palette_lookup_enabled = false; From 781bcead344e94c39615d077230f627f736a30e8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 17:13:03 -0300 Subject: [PATCH 15/19] [vector] Document and wire custom palette paint APIs --- src/hb-vector.h | 10 +++++- util/hb-vector-svg-all.c | 77 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/src/hb-vector.h b/src/hb-vector.h index 20e58e78e..13c81ba8b 100644 --- a/src/hb-vector.h +++ b/src/hb-vector.h @@ -531,6 +531,11 @@ hb_vector_paint_set_palette (hb_vector_paint_t *paint, * @color: replacement color. * * Overrides one font palette color entry for subsequent paint operations. + * Overrides are keyed by @color_index and persist on @paint until cleared + * (or replaced for the same index). + * + * These overrides are consulted by paint operations that resolve CPAL + * entries, including SVG glyph content using `var(--colorN)`. * * XSince: REPLACEME */ @@ -543,7 +548,10 @@ hb_vector_paint_set_custom_palette_color (hb_vector_paint_t *paint, * hb_vector_paint_clear_custom_palette_colors: * @paint: a paint context. * - * Clears all custom palette color overrides. + * Clears all custom palette color overrides previously set on @paint. + * + * After this call, palette lookups use the selected font palette without + * custom override entries. * * XSince: REPLACEME */ diff --git a/util/hb-vector-svg-all.c b/util/hb-vector-svg-all.c index 4b0b14ae5..908fad302 100644 --- a/util/hb-vector-svg-all.c +++ b/util/hb-vector-svg-all.c @@ -3,13 +3,86 @@ #include #include +#include + +static int +hexval (char c) +{ + if (c >= '0' && c <= '9') return c - '0'; + if (c >= 'a' && c <= 'f') return c - 'a' + 10; + if (c >= 'A' && c <= 'F') return c - 'A' + 10; + return -1; +} + +static hb_bool_t +parse_hex_color (const char *s, hb_color_t *color) +{ + if (!s || !*s || !color) + return 0; + + if (*s == '#') + s++; + + size_t len = strlen (s); + if (len != 6 && len != 8) + return 0; + + int vals[8]; + for (unsigned i = 0; i < len; i++) + { + vals[i] = hexval (s[i]); + if (vals[i] < 0) + return 0; + } + + unsigned r = (unsigned) (vals[0] * 16 + vals[1]); + unsigned g = (unsigned) (vals[2] * 16 + vals[3]); + unsigned b = (unsigned) (vals[4] * 16 + vals[5]); + unsigned a = len == 8 ? (unsigned) (vals[6] * 16 + vals[7]) : 255u; + *color = HB_COLOR (b, g, r, a); + return 1; +} + +static void +apply_custom_palette (hb_vector_paint_t *paint, const char *spec) +{ + hb_vector_paint_clear_custom_palette_colors (paint); + if (!spec || !*spec) + return; + + char *copy = strdup (spec); + if (!copy) + return; + + unsigned idx = 0; + char *saveptr = NULL; + for (char *tok = strtok_r (copy, ",", &saveptr); + tok; + tok = strtok_r (NULL, ",", &saveptr), idx++) + { + while (*tok == ' ' || *tok == '\t') + tok++; + if (!*tok) + continue; + + hb_color_t color; + if (!parse_hex_color (tok, &color)) + { + fprintf (stderr, "Ignoring invalid custom palette color: %s\n", tok); + continue; + } + hb_vector_paint_set_custom_palette_color (paint, idx, color); + } + + free (copy); +} int main (int argc, char **argv) { if (argc < 2) { - fprintf (stderr, "Usage: %s font-file [font-funcs] [wght]\n", argv[0]); + fprintf (stderr, "Usage: %s font-file [font-funcs] [wght] [custom-palette]\n", argv[0]); return 1; } @@ -51,6 +124,8 @@ main (int argc, char **argv) hb_vector_paint_set_palette (paint, 0); hb_vector_paint_set_foreground (paint, HB_COLOR (0, 0, 0, 255)); + if (argc > 4) + apply_custom_palette (paint, argv[4]); unsigned glyph_count = hb_face_get_glyph_count (face); for (unsigned gid = 0; gid < glyph_count; gid++) From 0edadf2aa066c6b7cd30373abc7e1b3282355b21 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 17:14:31 -0300 Subject: [PATCH 16/19] [vector] Revert hb-vector-svg-all custom palette wiring --- util/hb-vector-svg-all.c | 77 +--------------------------------------- 1 file changed, 1 insertion(+), 76 deletions(-) diff --git a/util/hb-vector-svg-all.c b/util/hb-vector-svg-all.c index 908fad302..4b0b14ae5 100644 --- a/util/hb-vector-svg-all.c +++ b/util/hb-vector-svg-all.c @@ -3,86 +3,13 @@ #include #include -#include - -static int -hexval (char c) -{ - if (c >= '0' && c <= '9') return c - '0'; - if (c >= 'a' && c <= 'f') return c - 'a' + 10; - if (c >= 'A' && c <= 'F') return c - 'A' + 10; - return -1; -} - -static hb_bool_t -parse_hex_color (const char *s, hb_color_t *color) -{ - if (!s || !*s || !color) - return 0; - - if (*s == '#') - s++; - - size_t len = strlen (s); - if (len != 6 && len != 8) - return 0; - - int vals[8]; - for (unsigned i = 0; i < len; i++) - { - vals[i] = hexval (s[i]); - if (vals[i] < 0) - return 0; - } - - unsigned r = (unsigned) (vals[0] * 16 + vals[1]); - unsigned g = (unsigned) (vals[2] * 16 + vals[3]); - unsigned b = (unsigned) (vals[4] * 16 + vals[5]); - unsigned a = len == 8 ? (unsigned) (vals[6] * 16 + vals[7]) : 255u; - *color = HB_COLOR (b, g, r, a); - return 1; -} - -static void -apply_custom_palette (hb_vector_paint_t *paint, const char *spec) -{ - hb_vector_paint_clear_custom_palette_colors (paint); - if (!spec || !*spec) - return; - - char *copy = strdup (spec); - if (!copy) - return; - - unsigned idx = 0; - char *saveptr = NULL; - for (char *tok = strtok_r (copy, ",", &saveptr); - tok; - tok = strtok_r (NULL, ",", &saveptr), idx++) - { - while (*tok == ' ' || *tok == '\t') - tok++; - if (!*tok) - continue; - - hb_color_t color; - if (!parse_hex_color (tok, &color)) - { - fprintf (stderr, "Ignoring invalid custom palette color: %s\n", tok); - continue; - } - hb_vector_paint_set_custom_palette_color (paint, idx, color); - } - - free (copy); -} int main (int argc, char **argv) { if (argc < 2) { - fprintf (stderr, "Usage: %s font-file [font-funcs] [wght] [custom-palette]\n", argv[0]); + fprintf (stderr, "Usage: %s font-file [font-funcs] [wght]\n", argv[0]); return 1; } @@ -124,8 +51,6 @@ main (int argc, char **argv) hb_vector_paint_set_palette (paint, 0); hb_vector_paint_set_foreground (paint, HB_COLOR (0, 0, 0, 255)); - if (argc > 4) - apply_custom_palette (paint, argv[4]); unsigned glyph_count = hb_face_get_glyph_count (face); for (unsigned gid = 0; gid < glyph_count; gid++) From 8dc76787fc7684231c9fa090047c7bca83d84f1e Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 17:16:08 -0300 Subject: [PATCH 17/19] [raster] Align custom palette API docs with vector --- src/hb-raster-paint.cc | 18 +++++++++++------- src/hb-raster.h | 25 +++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/src/hb-raster-paint.cc b/src/hb-raster-paint.cc index 96b839e06..67af5b33c 100644 --- a/src/hb-raster-paint.cc +++ b/src/hb-raster-paint.cc @@ -1855,9 +1855,12 @@ hb_raster_paint_set_foreground (hb_raster_paint_t *paint, /** * hb_raster_paint_clear_custom_palette_colors: - * @paint: a paint context + * @paint: a paint context. * - * Clears all custom palette color overrides. + * Clears all custom palette color overrides previously set on @paint. + * + * After this call, palette lookups use the selected font palette without + * custom override entries. * * XSince: REPLACEME **/ @@ -1870,12 +1873,13 @@ hb_raster_paint_clear_custom_palette_colors (hb_raster_paint_t *paint) /** * hb_raster_paint_set_custom_palette_color: - * @paint: a paint context - * @color_index: palette color index to override - * @color: replacement color for @color_index + * @paint: a paint context. + * @color_index: color index to override. + * @color: replacement color. * - * Sets a custom palette color override. During paint operations, - * when the font requests @color_index from CPAL, @color is used. + * Overrides one font palette color entry for subsequent paint operations. + * Overrides are keyed by @color_index and persist on @paint until cleared + * (or replaced for the same index). * * Return value: `true` if the override was set; `false` on allocation failure. * diff --git a/src/hb-raster.h b/src/hb-raster.h index 70afa3f22..8f1ebc707 100644 --- a/src/hb-raster.h +++ b/src/hb-raster.h @@ -278,9 +278,34 @@ HB_EXTERN void hb_raster_paint_set_foreground (hb_raster_paint_t *paint, hb_color_t foreground); +/** + * hb_raster_paint_clear_custom_palette_colors: + * @paint: a paint context. + * + * Clears all custom palette color overrides previously set on @paint. + * + * After this call, palette lookups use the selected font palette without + * custom override entries. + * + * XSince: REPLACEME + */ HB_EXTERN void hb_raster_paint_clear_custom_palette_colors (hb_raster_paint_t *paint); +/** + * hb_raster_paint_set_custom_palette_color: + * @paint: a paint context. + * @color_index: color index to override. + * @color: replacement color. + * + * Overrides one font palette color entry for subsequent paint operations. + * Overrides are keyed by @color_index and persist on @paint until cleared + * (or replaced for the same index). + * + * Return value: `true` if the override was set; `false` on allocation failure. + * + * XSince: REPLACEME + */ HB_EXTERN hb_bool_t hb_raster_paint_set_custom_palette_color (hb_raster_paint_t *paint, unsigned int color_index, From b7e019973c5156eb16556bccb5d66e0a4bf60e50 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 17:17:18 -0300 Subject: [PATCH 18/19] [raster] Keep custom palette docs out of hb-raster.h --- src/hb-raster.h | 25 ------------------------- 1 file changed, 25 deletions(-) diff --git a/src/hb-raster.h b/src/hb-raster.h index 8f1ebc707..70afa3f22 100644 --- a/src/hb-raster.h +++ b/src/hb-raster.h @@ -278,34 +278,9 @@ HB_EXTERN void hb_raster_paint_set_foreground (hb_raster_paint_t *paint, hb_color_t foreground); -/** - * hb_raster_paint_clear_custom_palette_colors: - * @paint: a paint context. - * - * Clears all custom palette color overrides previously set on @paint. - * - * After this call, palette lookups use the selected font palette without - * custom override entries. - * - * XSince: REPLACEME - */ HB_EXTERN void hb_raster_paint_clear_custom_palette_colors (hb_raster_paint_t *paint); -/** - * hb_raster_paint_set_custom_palette_color: - * @paint: a paint context. - * @color_index: color index to override. - * @color: replacement color. - * - * Overrides one font palette color entry for subsequent paint operations. - * Overrides are keyed by @color_index and persist on @paint until cleared - * (or replaced for the same index). - * - * Return value: `true` if the override was set; `false` on allocation failure. - * - * XSince: REPLACEME - */ HB_EXTERN hb_bool_t hb_raster_paint_set_custom_palette_color (hb_raster_paint_t *paint, unsigned int color_index, From 7b733462539ebb8a9eb4f8cb7915ff868bc8eab1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 17:21:48 -0300 Subject: [PATCH 19/19] [docs] Export hb-vector custom palette APIs in sections --- docs/harfbuzz-sections.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/harfbuzz-sections.txt b/docs/harfbuzz-sections.txt index adff1749a..7e8d79d00 100644 --- a/docs/harfbuzz-sections.txt +++ b/docs/harfbuzz-sections.txt @@ -1089,6 +1089,8 @@ hb_vector_paint_get_extents hb_vector_paint_set_glyph_extents hb_vector_paint_set_foreground hb_vector_paint_set_palette +hb_vector_paint_set_custom_palette_color +hb_vector_paint_clear_custom_palette_colors hb_vector_paint_get_funcs hb_vector_paint_glyph hb_vector_svg_paint_set_flat