From ff4d329ef1954276e0afc74e60e64372b271be6c Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Sun, 8 Feb 2026 20:18:50 -0700 Subject: [PATCH] [hb-view] Add --stroke= --- util/helper-cairo.hh | 16 ++++++++--- util/view-cairo.hh | 18 +++++++++++++ util/view-options.hh | 64 +++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 94 insertions(+), 4 deletions(-) diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index c6b715c15..d290ad583 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -557,6 +557,8 @@ helper_cairo_create_context (double w, double h, fr = fg = fb = 0; fa = 255; bool foreground_has_color = false; bool foreground_has_alpha = false; + bool stroke_has_color = false; + bool stroke_has_alpha = false; using rgba_color_t = typename view_options_t::rgba_color_t; if (view_opts->foreground_use_palette && @@ -586,14 +588,22 @@ helper_cairo_create_context (double w, double h, foreground_has_alpha = fa != 255; } + if (view_opts->stroke_enabled) + { + auto &stroke = view_opts->stroke_color; + stroke_has_color = stroke.r != stroke.g || stroke.g != stroke.b; + stroke_has_alpha = stroke.a != 255; + } + if (content == CAIRO_CONTENT_ALPHA) { if (view_opts->show_extents || - br != bg || bg != bb || - foreground_has_color) + br != bg || bg != bb || + foreground_has_color || + stroke_has_color) content = CAIRO_CONTENT_COLOR; } - if (ba != 255 || foreground_has_alpha) + if (ba != 255 || foreground_has_alpha || stroke_has_alpha) content = CAIRO_CONTENT_COLOR_ALPHA; cairo_surface_t *surface; diff --git a/util/view-cairo.hh b/util/view-cairo.hh index 9e0684e17..f450a63e6 100644 --- a/util/view-cairo.hh +++ b/util/view-cairo.hh @@ -95,6 +95,7 @@ view_cairo_t::draw_lines (cairo_t *cr, hb_font_t *font, double leading, int vert { const bool use_foreground_palette = foreground_use_palette && foreground_palette && foreground_palette->len; + const bool use_stroke = stroke_enabled && stroke_width > 0; cairo_translate (cr, +vert * leading, -horiz * leading); for (unsigned int i = 0; i < lines->len; i++) @@ -167,6 +168,23 @@ view_cairo_t::draw_lines (cairo_t *cr, hb_font_t *font, double leading, int vert #endif cairo_show_glyphs (cr, l.glyphs, l.num_glyphs); } + + if (use_stroke && l.num_glyphs) + { + cairo_save (cr); + cairo_set_source_rgba (cr, + stroke_color.r / 255., + stroke_color.g / 255., + stroke_color.b / 255., + stroke_color.a / 255.); + cairo_set_line_width (cr, stroke_width); + cairo_set_line_cap (cr, CAIRO_LINE_CAP_ROUND); + cairo_set_line_join (cr, CAIRO_LINE_JOIN_ROUND); + cairo_new_path (cr); + cairo_glyph_path (cr, l.glyphs, l.num_glyphs); + cairo_stroke (cr); + cairo_restore (cr); + } } } diff --git a/util/view-options.hh b/util/view-options.hh index 335e07c07..843ab3af4 100644 --- a/util/view-options.hh +++ b/util/view-options.hh @@ -32,12 +32,15 @@ #define DEFAULT_MARGIN 16 #define DEFAULT_FORE "#000000" #define DEFAULT_BACK "#FFFFFF" +#define DEFAULT_STROKE "#888" +#define DEFAULT_STROKE_WIDTH 1 struct view_options_t { ~view_options_t () { g_free (fore); + g_free (stroke); g_free (back); g_free (custom_palette); if (foreground_palette) @@ -52,12 +55,16 @@ struct view_options_t }; char *fore = nullptr; + char *stroke = nullptr; char *back = nullptr; unsigned int palette = 0; char *custom_palette = nullptr; GArray *foreground_palette = nullptr; hb_bool_t foreground_use_palette = false; hb_bool_t foreground_use_rainbow = false; + hb_bool_t stroke_enabled = false; + rgba_color_t stroke_color = {0, 0, 0, 255}; + double stroke_width = DEFAULT_STROKE_WIDTH; double line_space = 0; bool have_font_extents = false; struct font_extents_t { @@ -126,6 +133,7 @@ view_options_t::add_options (option_parser_t *parser) {"foreground", 0, 0, G_OPTION_ARG_STRING, &this->fore, "Set foreground color (default: " DEFAULT_FORE ")", "rrggbb/rrggbbaa[,...]"}, {"rainbow", 0, 0, G_OPTION_ARG_NONE, &this->foreground_use_rainbow, "Rotate glyph foreground colors through a default palette", nullptr}, {"lgbtq", 0, G_OPTION_FLAG_HIDDEN, G_OPTION_ARG_NONE, &this->foreground_use_rainbow, "Hidden alias for --rainbow", nullptr}, + {"stroke", 0, 0, G_OPTION_ARG_STRING, &this->stroke, "Stroke glyph outlines; `--stroke=` enables defaults", "rrggbb/rrggbbaa[+width]"}, {"font-palette", 0, 0, G_OPTION_ARG_INT, &this->palette, "Set font palette (default: 0)", "index"}, {"custom-palette", 0, 0, G_OPTION_ARG_STRING, &this->custom_palette, "Custom palette", "comma-separated colors"}, {"line-space", 0, 0, G_OPTION_ARG_DOUBLE, &this->line_space, "Set space between lines (default: 0)", "units"}, @@ -146,6 +154,51 @@ view_options_t::add_options (option_parser_t *parser) inline void view_options_t::setup_foreground () { + stroke_enabled = false; + stroke_width = DEFAULT_STROKE_WIDTH; + if (!parse_color (DEFAULT_STROKE, stroke_color.r, stroke_color.g, stroke_color.b, stroke_color.a)) + fail (false, "Failed parsing default stroke color `%s`", DEFAULT_STROKE); + + const bool stroke_specified = stroke != nullptr; + if (stroke_specified) + { + stroke_enabled = true; + char *spec = g_strdup (stroke); + char *s = g_strstrip (spec); + if (*s) + { + char *plus = strchr (s, '+'); + if (plus && strchr (plus + 1, '+')) + fail (false, "Failed parsing stroke spec `%s`", stroke); + + char *color_spec = s; + char *width_spec = nullptr; + if (plus) + { + *plus = '\0'; + width_spec = g_strstrip (plus + 1); + } + color_spec = g_strstrip (color_spec); + + if (*color_spec) + { + if (!parse_color (color_spec, stroke_color.r, stroke_color.g, stroke_color.b, stroke_color.a)) + fail (false, "Failed parsing stroke color `%s`", color_spec); + } + + if (width_spec && *width_spec) + { + char *end = nullptr; + errno = 0; + double w = g_ascii_strtod (width_spec, &end); + if (!end || errno || *g_strstrip (end) || w <= 0) + fail (false, "Failed parsing stroke width `%s`", width_spec); + stroke_width = w; + } + } + g_free (spec); + } + foreground_use_palette = false; if (foreground_palette) { @@ -157,7 +210,12 @@ view_options_t::setup_foreground () if (!foreground) return; - if (foreground_use_rainbow) + const bool use_rainbow = + foreground_use_rainbow || + 0 == g_ascii_strcasecmp (foreground, "rainbow") || + 0 == g_ascii_strcasecmp (foreground, "lgbtq"); + + if (use_rainbow) { static const char *rainbow[] = { @@ -179,6 +237,10 @@ view_options_t::setup_foreground () g_array_append_val (foreground_palette, color); } foreground_use_palette = true; + + if (!stroke_specified) + stroke_enabled = true; + return; }