[vector] Document and wire custom palette paint APIs

This commit is contained in:
Behdad Esfahbod committed 2026-02-26 17:13:03 -03:00
1 parent 1e857b2e5f
commit 781bcead34
2 files changed
+85 -2

No files matched your search

+9 -1
View File
@@ -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
*/
+76 -1
View File
@@ -3,13 +3,86 @@
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
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++)