[util] Parse custom palette once in view-options and reuse

This commit is contained in:
Behdad Esfahbod committed 2026-02-26 21:00:21 -03:00
1 parent 648c75b112
commit 164d58c8d7
4 files changed
+114 -63

No files matched your search

+10 -20
View File
@@ -122,29 +122,19 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts,
cairo_font_options_set_color_palette (font_options, view_opts->palette);
#endif
#ifdef HAVE_CAIRO_FONT_OPTIONS_GET_CUSTOM_PALETTE_COLOR
if (view_opts->custom_palette)
if (view_opts->custom_palette_entries)
{
char **entries = g_strsplit (view_opts->custom_palette, ",", -1);
unsigned idx = 0;
for (unsigned i = 0; entries[i]; i++)
for (unsigned i = 0; i < view_opts->custom_palette_entries->len; 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 (p, fr, fg,fb, fa))
cairo_font_options_set_custom_palette_color (font_options, idx, fr / 255., fg / 255., fb / 255., fa / 255.);
idx++;
auto &entry =
g_array_index (view_opts->custom_palette_entries,
typename view_options_t::custom_palette_entry_t, i);
cairo_font_options_set_custom_palette_color (font_options, entry.index,
entry.color.r / 255.,
entry.color.g / 255.,
entry.color.b / 255.,
entry.color.a / 255.);
}
g_strfreev (entries);
}
#endif
+10 -20
View File
@@ -102,29 +102,19 @@ struct raster_output_t : output_options_t<true>, view_options_t
pnt = hb_raster_paint_create_or_fail ();
hb_raster_paint_set_foreground (pnt, fg_color);
hb_raster_paint_clear_custom_palette_colors (pnt);
if (custom_palette)
if (custom_palette_entries)
{
char **entries = g_strsplit (custom_palette, ",", -1);
unsigned idx = 0;
for (unsigned i = 0; entries[i]; i++)
for (unsigned i = 0; i < custom_palette_entries->len; i++)
{
char *entry = g_strstrip (entries[i]);
const char *p = strchr (entry, '=');
if (!p)
p = entry;
else
{
sscanf (entry, "%u", &idx);
p++;
}
unsigned r = 0, g = 0, b = 0, a = 0;
if (parse_color (p, r, g, b, a))
hb_raster_paint_set_custom_palette_color (pnt, idx,
HB_COLOR ((uint8_t) b, (uint8_t) g, (uint8_t) r, (uint8_t) a));
idx++;
auto &entry =
g_array_index (custom_palette_entries,
typename view_options_t::custom_palette_entry_t, i);
hb_raster_paint_set_custom_palette_color (pnt, entry.index,
HB_COLOR ((uint8_t) entry.color.b,
(uint8_t) entry.color.g,
(uint8_t) entry.color.r,
(uint8_t) entry.color.a));
}
g_strfreev (entries);
}
}
}
+12 -23
View File
@@ -108,8 +108,7 @@ struct vector_output_t : output_options_t<>, view_options_t
has_background = true;
}
if (!parse_custom_palette_overrides (error))
return;
load_custom_palette_overrides_from_view ();
}
template <typename app_t>
@@ -957,39 +956,29 @@ struct vector_output_t : output_options_t<>, view_options_t
palette_lookup_enabled = true;
}
bool parse_custom_palette_overrides (GError **error)
void load_custom_palette_overrides_from_view ()
{
custom_palette_values.clear ();
custom_palette_has_value.clear ();
if (!custom_palette || !*custom_palette)
return true;
if (!custom_palette_entries)
return;
char **entries = g_strsplit (custom_palette, ",", -1);
for (unsigned idx = 0; entries && entries[idx]; idx++)
for (unsigned i = 0; i < custom_palette_entries->len; i++)
{
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))
{
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;
}
const custom_palette_entry_t &entry =
g_array_index (custom_palette_entries, custom_palette_entry_t, i);
unsigned idx = entry.index;
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_values[idx] = HB_COLOR (entry.color.b,
entry.color.g,
entry.color.r,
entry.color.a);
custom_palette_has_value[idx] = true;
}
g_strfreev (entries);
return true;
}
void emit_extents_overlay (hb_direction_t dir, float step)
+82
View File
@@ -45,6 +45,8 @@ struct view_options_t
g_free (custom_palette);
if (foreground_palette)
g_array_unref (foreground_palette);
if (custom_palette_entries)
g_array_unref (custom_palette_entries);
}
void add_options (option_parser_t *parser);
@@ -95,6 +97,9 @@ struct view_options_t
g_free (spec);
}
if (!parse_custom_palette_entries (error))
return;
foreground_use_palette = false;
if (foreground_palette)
{
@@ -176,12 +181,17 @@ struct view_options_t
struct rgba_color_t {
unsigned r, g, b, a;
};
struct custom_palette_entry_t {
unsigned index;
rgba_color_t color;
};
char *fore = nullptr;
char *stroke = nullptr;
char *back = nullptr;
unsigned int palette = 0;
char *custom_palette = nullptr;
GArray *custom_palette_entries = nullptr;
GArray *foreground_palette = nullptr;
hb_bool_t foreground_use_palette = false;
hb_bool_t foreground_use_rainbow = false;
@@ -199,6 +209,78 @@ struct view_options_t
hb_bool_t logical = false;
hb_bool_t ink = false;
hb_bool_t show_extents = false;
bool parse_custom_palette_entries (GError **error)
{
if (custom_palette_entries)
{
g_array_unref (custom_palette_entries);
custom_palette_entries = nullptr;
}
if (!custom_palette || !*custom_palette)
return true;
custom_palette_entries = g_array_new (false, false, sizeof (custom_palette_entry_t));
char **entries = g_strsplit (custom_palette, ",", -1);
unsigned next_index = 0;
for (unsigned i = 0; entries && entries[i]; i++)
{
char *entry = g_strstrip (entries[i]);
if (!*entry)
continue;
unsigned idx = next_index;
char *color_spec = entry;
char *eq = strchr (entry, '=');
if (eq)
{
*eq = '\0';
char *idx_spec = g_strstrip (entry);
color_spec = g_strstrip (eq + 1);
if (!*idx_spec || !*color_spec)
{
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Invalid --custom-palette entry");
g_strfreev (entries);
return false;
}
char *end = nullptr;
errno = 0;
unsigned long parsed = strtoul (idx_spec, &end, 10);
if (errno || !end || *g_strstrip (end))
{
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Invalid --custom-palette index `%s`", idx_spec);
g_strfreev (entries);
return false;
}
idx = (unsigned) parsed;
next_index = idx + 1;
}
rgba_color_t color = {0, 0, 0, 255};
if (!parse_color (color_spec, color.r, color.g, color.b, color.a))
{
g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE,
"Invalid --custom-palette color `%s`", color_spec);
g_strfreev (entries);
return false;
}
custom_palette_entry_t parsed = {idx, color};
g_array_append_val (custom_palette_entries, parsed);
if (!eq)
next_index++;
}
g_strfreev (entries);
if (!custom_palette_entries->len)
{
g_array_unref (custom_palette_entries);
custom_palette_entries = nullptr;
}
return true;
}
};