From e933f71b0ddc9508ca603b89d3a95ad2e8d362d0 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 16 Apr 2026 14:07:38 -0600 Subject: [PATCH] [cairo][util/hb-view] Honor --draw / --paint via cairo color mode MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit hb-cairo's render_color_glyph drops _or_fail in favor of the synthesizing hb_font_paint_glyph, so the paint callback handles every glyph (color or mono) through the paint pipeline instead of returning NOT_IMPLEMENTED on non-color glyphs and forcing cairo to fall through to the outline path. Hb-view's helper-cairo then picks paint vs draw the same way the other utils do — --paint wins over --draw, otherwise auto-detect via the font's color tables — and expresses "draw" by setting CAIRO_COLOR_MODE_NO_COLOR on the font options (which makes cairo skip render_color_glyph entirely, falling through to render_glyph for the cheaper outline path). No new public API. Co-Authored-By: Claude Opus 4.6 (1M context) --- src/hb-cairo.cc | 8 ++++++-- util/helper-cairo.hh | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/hb-cairo.cc b/src/hb-cairo.cc index 23ebd6bfc..c93206a38 100644 --- a/src/hb-cairo.cc +++ b/src/hb-cairo.cc @@ -645,8 +645,12 @@ hb_cairo_render_color_glyph (cairo_scaled_font_t *scaled_font, c.cr = cr; c.color_cache = (hb_map_t *) cairo_scaled_font_get_user_data (scaled_font, &color_cache_key); - if (!hb_font_paint_glyph_or_fail (font, glyph, hb_cairo_paint_get_funcs (), &c, palette, color)) - return CAIRO_STATUS_USER_FONT_NOT_IMPLEMENTED; + /* Synthesizing variant: mono glyphs render here too via the + * push_clip_glyph + foreground-fill fallback inside + * hb_font_paint_glyph. Callers that want the cheaper + * outline path for mono fonts set CAIRO_COLOR_MODE_NO_COLOR + * on the font options instead. */ + hb_font_paint_glyph (font, glyph, hb_cairo_paint_get_funcs (), &c, palette, color); return CAIRO_STATUS_SUCCESS; } diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index 07502e594..93472bca1 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -37,6 +37,7 @@ #include #include #include +#include #include "helper-cairo-ansi.hh" #ifdef CAIRO_HAS_SVG_SURFACE @@ -119,6 +120,24 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts, font_options = cairo_font_options_create (); cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE); cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF); +#if CAIRO_VERSION >= CAIRO_VERSION_ENCODE(1,18,0) + /* Pick draw vs paint mode. --paint wins over --draw; + * otherwise auto-detect: route mono fonts through the + * cheaper outline path (NO_COLOR) and color fonts through + * the paint pipeline (cairo's default color mode). */ + hb_face_t *hb_face = hb_font_get_face (font); + bool font_has_color = hb_ot_color_has_paint (hb_face) || + hb_ot_color_has_layers (hb_face) || +#ifndef HB_NO_SVG + hb_ot_color_has_svg (hb_face) || +#endif + hb_ot_color_has_png (hb_face); + bool use_paint = view_opts->force_paint ? true + : view_opts->force_draw ? false + : font_has_color; + if (!use_paint) + cairo_font_options_set_color_mode (font_options, CAIRO_COLOR_MODE_NO_COLOR); +#endif #ifdef CAIRO_COLOR_PALETTE_DEFAULT cairo_font_options_set_color_palette (font_options, view_opts->palette); #endif