From 781bcead344e94c39615d077230f627f736a30e8 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 17:13:03 -0300 Subject: [PATCH] [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++)