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);
}