From 46bf94ef0e1ab39d1d7d328871f3a93ae1ad52ee Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 26 Feb 2026 21:04:18 -0300 Subject: [PATCH] [util] Centralize foreground/background parsing in view options --- util/helper-cairo.hh | 15 ++++++++------- util/raster-output.hh | 32 ++++++++------------------------ util/vector-output.hh | 36 ++++++++---------------------------- util/view-options.hh | 31 +++++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+), 59 deletions(-) diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index 1354d0627..84797e760 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -540,11 +540,14 @@ helper_cairo_create_context (double w, double h, unsigned int fr, fg, fb, fa, br, bg, bb, ba; - const char *color; - br = bg = bb = ba = 255; - color = view_opts->back ? view_opts->back : DEFAULT_BACK; - parse_color (color, br, bg, bb, ba); - fr = fg = fb = 0; fa = 255; + br = view_opts->background_color.r; + bg = view_opts->background_color.g; + bb = view_opts->background_color.b; + ba = view_opts->background_color.a; + fr = view_opts->foreground_color.r; + fg = view_opts->foreground_color.g; + fb = view_opts->foreground_color.b; + fa = view_opts->foreground_color.a; bool foreground_has_color = false; bool foreground_has_alpha = false; bool stroke_has_color = false; @@ -572,8 +575,6 @@ helper_cairo_create_context (double w, double h, } else { - color = view_opts->fore ? view_opts->fore : DEFAULT_FORE; - parse_color (color, fr, fg, fb, fa); foreground_has_color = fr != fg || fg != fb; foreground_has_alpha = fa != 255; } diff --git a/util/raster-output.hh b/util/raster-output.hh index ac623bcf5..2fc8e942f 100644 --- a/util/raster-output.hh +++ b/util/raster-output.hh @@ -71,30 +71,14 @@ struct raster_output_t : output_options_t, view_options_t hb_ot_color_has_layers (face) || hb_ot_color_has_svg (face); - /* Parse foreground / background colors */ - { - unsigned r, g, b, a; - const char *fg_spec = fore ? fore : DEFAULT_FORE; - if (foreground_use_palette && foreground_palette && foreground_palette->len) - { - auto &c = g_array_index (foreground_palette, rgba_color_t, 0); - r = c.r; g = c.g; b = c.b; a = c.a; - fg_color = HB_COLOR ((uint8_t) b, (uint8_t) g, (uint8_t) r, (uint8_t) a); - } - else if (parse_color (fg_spec, r, g, b, a)) - fg_color = HB_COLOR ((uint8_t) b, (uint8_t) g, (uint8_t) r, (uint8_t) a); - } - { - const char *bg_spec = back ? back : DEFAULT_BACK; - unsigned r, g, b, a; - if (parse_color (bg_spec, r, g, b, a)) - { - bg_r = (uint8_t) r; - bg_g = (uint8_t) g; - bg_b = (uint8_t) b; - bg_a = (uint8_t) a; - } - } + fg_color = HB_COLOR ((uint8_t) foreground_color.b, + (uint8_t) foreground_color.g, + (uint8_t) foreground_color.r, + (uint8_t) foreground_color.a); + bg_r = (uint8_t) background_color.r; + bg_g = (uint8_t) background_color.g; + bg_b = (uint8_t) background_color.b; + bg_a = (uint8_t) background_color.a; rdr = hb_raster_draw_create_or_fail (); if (has_color) diff --git a/util/vector-output.hh b/util/vector-output.hh index fe78e6760..f5592f57d 100644 --- a/util/vector-output.hh +++ b/util/vector-output.hh @@ -80,33 +80,14 @@ struct vector_output_t : output_options_t<>, view_options_t return; } - 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 (fore, r, g, b, a)) - return; - foreground = HB_COLOR (b, g, r, a); - } - - has_background = false; - if (back && *back) - { - unsigned 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", back); - return; - } - background = HB_COLOR (b, g, r, a); - has_background = true; - } + foreground = HB_COLOR (foreground_color.b, + foreground_color.g, + foreground_color.r, + foreground_color.a); + background = HB_COLOR (background_color.b, + background_color.g, + background_color.r, + background_color.a); load_custom_palette_overrides_from_view (); } @@ -1049,7 +1030,6 @@ struct vector_output_t : output_options_t<>, view_options_t int precision = 2; 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; hb_font_t *font = nullptr; hb_font_t *upem_font = nullptr; diff --git a/util/view-options.hh b/util/view-options.hh index 2ad0de633..728566d0f 100644 --- a/util/view-options.hh +++ b/util/view-options.hh @@ -100,6 +100,24 @@ struct view_options_t if (!parse_custom_palette_entries (error)) return; + if (!parse_color (DEFAULT_BACK, + background_color.r, background_color.g, + background_color.b, background_color.a)) + fail (false, "Failed parsing default background color `%s`", DEFAULT_BACK); + has_background = false; + if (back && *back) + { + if (!parse_color (back, + background_color.r, background_color.g, + background_color.b, background_color.a)) + { + g_set_error (error, G_OPTION_ERROR, G_OPTION_ERROR_BAD_VALUE, + "Invalid background color `%s`", back); + return; + } + has_background = true; + } + foreground_use_palette = false; if (foreground_palette) { @@ -111,6 +129,11 @@ struct view_options_t if (!foreground) return; + if (!parse_color (DEFAULT_FORE, + foreground_color.r, foreground_color.g, + foreground_color.b, foreground_color.a)) + fail (false, "Failed parsing default foreground color `%s`", DEFAULT_FORE); + const bool use_rainbow = foreground_use_rainbow || 0 == g_ascii_strcasecmp (foreground, "rainbow") || @@ -137,6 +160,9 @@ struct view_options_t fail (false, "Failed parsing foreground color `%s`", *entry); g_array_append_val (foreground_palette, color); } + if (foreground_palette->len) + foreground_color = + g_array_index (foreground_palette, rgba_color_t, 0); foreground_use_palette = true; if (!stroke_specified) @@ -150,6 +176,7 @@ struct view_options_t rgba_color_t color = {0, 0, 0, 255}; if (!parse_color (foreground, color.r, color.g, color.b, color.a)) fail (false, "Failed parsing foreground color `%s`", foreground); + foreground_color = color; return; } @@ -175,6 +202,7 @@ struct view_options_t return; } + foreground_color = g_array_index (foreground_palette, rgba_color_t, 0); foreground_use_palette = true; } @@ -198,6 +226,9 @@ struct view_options_t hb_bool_t stroke_enabled = false; rgba_color_t stroke_color = {0, 0, 0, 255}; double stroke_width = DEFAULT_STROKE_WIDTH; + rgba_color_t foreground_color = {0, 0, 0, 255}; + rgba_color_t background_color = {255, 255, 255, 255}; + hb_bool_t has_background = false; double line_space = 0; bool have_font_extents = false; struct font_extents_t {