From 6c0ebd02c99e7536975ba7194832a1f33abd7faf Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 5 Nov 2015 11:37:48 -0800 Subject: [PATCH] [util] If font has color, generate PNG with color --- util/helper-cairo.cc | 33 ++++++++++++++++++++++++++------- util/helper-cairo.hh | 6 +++++- util/view-cairo.cc | 7 ++++++- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/util/helper-cairo.cc b/util/helper-cairo.cc index 8960df946..50e22ab0e 100644 --- a/util/helper-cairo.cc +++ b/util/helper-cairo.cc @@ -128,6 +128,22 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts) return scaled_font; } +bool +helper_cairo_scaled_font_has_color (cairo_scaled_font_t *scaled_font) +{ + bool ret = false; +#ifdef FT_HAS_COLOR + FT_Face ft_face = cairo_ft_scaled_font_lock_face (scaled_font); + if (ft_face) + { + if (FT_HAS_COLOR (ft_face)) + ret = true; + cairo_ft_scaled_font_unlock_face (scaled_font); + } +#endif + return ret; +} + struct finalize_closure_t { void (*callback)(finalize_closure_t *); @@ -295,7 +311,8 @@ const char *helper_cairo_supported_formats[] = cairo_t * helper_cairo_create_context (double w, double h, view_options_t *view_opts, - output_options_t *out_opts) + output_options_t *out_opts, + cairo_content_t content) { cairo_surface_t *(*constructor) (cairo_write_func_t write_func, void *closure, @@ -357,12 +374,14 @@ helper_cairo_create_context (double w, double h, color = view_opts->fore ? view_opts->fore : DEFAULT_FORE; sscanf (color + (*color=='#'), "%2x%2x%2x%2x", &fr, &fg, &fb, &fa); - cairo_content_t content; - if (!view_opts->annotate && ba == 255 && br == bg && bg == bb && fr == fg && fg == fb) - content = CAIRO_CONTENT_ALPHA; - else if (ba == 255) - content = CAIRO_CONTENT_COLOR; - else + if (content == CAIRO_CONTENT_ALPHA) + { + if (view_opts->annotate || + br != bg || bg != bb || + fr != fg || fg != fb) + content = CAIRO_CONTENT_COLOR; + } + if (ba != 255) content = CAIRO_CONTENT_COLOR_ALPHA; cairo_surface_t *surface; diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index ed55a452d..27b6eb3dc 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -35,12 +35,16 @@ cairo_scaled_font_t * helper_cairo_create_scaled_font (const font_options_t *font_opts); +bool +helper_cairo_scaled_font_has_color (cairo_scaled_font_t *scaled_font); + extern const char *helper_cairo_supported_formats[]; cairo_t * helper_cairo_create_context (double w, double h, view_options_t *view_opts, - output_options_t *out_opts); + output_options_t *out_opts, + cairo_content_t content); void helper_cairo_destroy_context (cairo_t *cr); diff --git a/util/view-cairo.cc b/util/view-cairo.cc index 41b8d6e94..bdb97bfc7 100644 --- a/util/view-cairo.cc +++ b/util/view-cairo.cc @@ -64,10 +64,15 @@ view_cairo_t::render (const font_options_t *font_opts) w = MAX (w, x_sign * x_advance); } + /* See if font needs color. */ + cairo_content_t content = CAIRO_CONTENT_ALPHA; + if (helper_cairo_scaled_font_has_color (scaled_font)) + content = CAIRO_CONTENT_COLOR; + /* Create surface. */ cairo_t *cr = helper_cairo_create_context (w + view_options.margin.l + view_options.margin.r, h + view_options.margin.t + view_options.margin.b, - &view_options, &output_options); + &view_options, &output_options, content); cairo_set_scaled_font (cr, scaled_font); /* Setup coordinate system. */