From 5d2df1208a58e537cb16e3b8009135dcf4d9393b Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Thu, 3 Feb 2022 17:18:54 -0600 Subject: [PATCH] [util] Use hb-draw to render alternatively to cairo-ft Is automatically enabled if cairo is recent enough, and font-funcs are not 'ft'. Uses cairo-user-font backend internally. --- util/Makefile.sources | 2 + util/helper-cairo-ft.hh | 120 ++++++++++++++++++++++++++++++++ util/helper-cairo-user.hh | 142 ++++++++++++++++++++++++++++++++++++++ util/helper-cairo.hh | 86 ++++------------------- 4 files changed, 279 insertions(+), 71 deletions(-) create mode 100644 util/helper-cairo-ft.hh create mode 100644 util/helper-cairo-user.hh diff --git a/util/Makefile.sources b/util/Makefile.sources index df3ad4a41..257e21171 100644 --- a/util/Makefile.sources +++ b/util/Makefile.sources @@ -4,6 +4,8 @@ HB_VIEW_sources = \ font-options.hh \ hb-view.cc \ helper-cairo-ansi.hh \ + helper-cairo-ft.hh \ + helper-cairo-user.hh \ helper-cairo.hh \ main-font-text.hh \ options.hh \ diff --git a/util/helper-cairo-ft.hh b/util/helper-cairo-ft.hh new file mode 100644 index 000000000..f22c558f8 --- /dev/null +++ b/util/helper-cairo-ft.hh @@ -0,0 +1,120 @@ +/* + * Copyright © 2022 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#ifndef HELPER_CAIRO_FT_HH +#define HELPER_CAIRO_FT_HH + +#include "font-options.hh" + +#include +#include +#include FT_MULTIPLE_MASTERS_H + +static FT_Library ft_library; + +#ifdef HAVE_ATEXIT +static inline +void free_ft_library () +{ + FT_Done_FreeType (ft_library); +} +#endif + +static inline cairo_font_face_t * +helper_cairo_create_ft_font_face (const font_options_t *font_opts) +{ + cairo_font_face_t *cairo_face; + /* We cannot use the FT_Face from hb_font_t, as doing so will confuse hb_font_t because + * cairo will reset the face size. As such, create new face... + * TODO Perhaps add API to hb-ft to encapsulate this code. */ + FT_Face ft_face = nullptr;//hb_ft_font_get_face (font); + if (!ft_face) + { + if (!ft_library) + { + FT_Init_FreeType (&ft_library); +#ifdef HAVE_ATEXIT + atexit (free_ft_library); +#endif + } + + unsigned int blob_length; + const char *blob_data = hb_blob_get_data (font_opts->blob, &blob_length); + + if (FT_New_Memory_Face (ft_library, + (const FT_Byte *) blob_data, + blob_length, + font_opts->face_index, + &ft_face)) + fail (false, "FT_New_Memory_Face fail"); + } + if (!ft_face) + { + /* This allows us to get some boxes at least... */ + cairo_face = cairo_toy_font_face_create ("@cairo:sans", + CAIRO_FONT_SLANT_NORMAL, + CAIRO_FONT_WEIGHT_NORMAL); + } + else + { +#ifdef HAVE_FT_SET_VAR_BLEND_COORDINATES + unsigned int num_coords; + const int *coords = hb_font_get_var_coords_normalized (font_opts->font, &num_coords); + if (num_coords) + { + FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed)); + if (ft_coords) + { + for (unsigned int i = 0; i < num_coords; i++) + ft_coords[i] = coords[i] << 2; + FT_Set_Var_Blend_Coordinates (ft_face, num_coords, ft_coords); + free (ft_coords); + } + } +#endif + + cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, font_opts->ft_load_flags); + } + return cairo_face; +} + +static inline bool +helper_cairo_ft_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; +} + +#endif diff --git a/util/helper-cairo-user.hh b/util/helper-cairo-user.hh new file mode 100644 index 000000000..c1f8c466e --- /dev/null +++ b/util/helper-cairo-user.hh @@ -0,0 +1,142 @@ +/* + * Copyright © 2022 Google, Inc. + * + * This is part of HarfBuzz, a text shaping library. + * + * Permission is hereby granted, without written agreement and without + * license or royalty fees, to use, copy, modify, and distribute this + * software and its documentation for any purpose, provided that the + * above copyright notice and the following two paragraphs appear in + * all copies of this software. + * + * IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE TO ANY PARTY FOR + * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES + * ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN + * IF THE COPYRIGHT HOLDER HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH + * DAMAGE. + * + * THE COPYRIGHT HOLDER SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, + * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND + * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS + * ON AN "AS IS" BASIS, AND THE COPYRIGHT HOLDER HAS NO OBLIGATION TO + * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS. + * + * Google Author(s): Behdad Esfahbod + */ + +#ifndef HELPER_CAIRO_USER_HH +#define HELPER_CAIRO_USER_HH + +#include "font-options.hh" + +#include +#include + +static void +move_to (hb_draw_funcs_t *dfuncs, + cairo_t *cr, + hb_draw_state_t *st, + float to_x, float to_y, + void *) +{ + cairo_move_to (cr, to_x, to_y); +} + +static void +line_to (hb_draw_funcs_t *dfuncs, + cairo_t *cr, + hb_draw_state_t *st, + float to_x, float to_y, + void *) +{ + cairo_line_to (cr, to_x, to_y); +} + +static void +cubic_to (hb_draw_funcs_t *dfuncs, + cairo_t *cr, + hb_draw_state_t *st, + float control1_x, float control1_y, + float control2_x, float control2_y, + float to_x, float to_y, + void *) +{ + cairo_curve_to (cr, control1_x, control1_y, control2_x, control2_y, to_x, to_y); +} + +static void +close_path (hb_draw_funcs_t *dfuncs, + cairo_t *cr, + hb_draw_state_t *st, + void *) +{ + cairo_close_path (cr); +} + + +static hb_draw_funcs_t * +get_cairo_draw_funcs () +{ + static hb_draw_funcs_t *funcs; + + if (!funcs) + { + funcs = hb_draw_funcs_create (); + hb_draw_funcs_set_move_to_func (funcs, (hb_draw_move_to_func_t) move_to, nullptr, nullptr); + hb_draw_funcs_set_line_to_func (funcs, (hb_draw_line_to_func_t) line_to, nullptr, nullptr); + hb_draw_funcs_set_cubic_to_func (funcs, (hb_draw_cubic_to_func_t) cubic_to, nullptr, nullptr); + hb_draw_funcs_set_close_path_func (funcs, (hb_draw_close_path_func_t) close_path, nullptr, nullptr); + } + + return funcs; +} + +static const cairo_user_data_key_t _hb_font_cairo_user_data_key = {0}; + +static cairo_status_t +render_glyph (cairo_scaled_font_t *scaled_font, + unsigned long glyph, + cairo_t *cr, + cairo_text_extents_t *extents) +{ + hb_font_t *font = (hb_font_t *) (cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font), + &_hb_font_cairo_user_data_key)); + + hb_position_t x_scale, y_scale; + hb_font_get_scale (font, &x_scale, &y_scale); + cairo_scale (cr, +1./x_scale, -1./y_scale); + + hb_font_get_glyph_shape (font, glyph, get_cairo_draw_funcs (), cr); + cairo_fill (cr); + + return CAIRO_STATUS_SUCCESS; +} + +static inline cairo_font_face_t * +helper_cairo_create_user_font_face (const font_options_t *font_opts) +{ + cairo_font_face_t *cairo_face = cairo_user_font_face_create (); + cairo_user_font_face_set_render_glyph_func (cairo_face, render_glyph); + cairo_font_face_set_user_data (cairo_face, + &_hb_font_cairo_user_data_key, + hb_font_reference (font_opts->font), + (cairo_destroy_func_t) hb_font_destroy); + return cairo_face; +} + +static inline bool +helper_cairo_user_font_face_has_data (cairo_font_face_t *font_face) +{ + return cairo_font_face_get_user_data (font_face, &_hb_font_cairo_user_data_key); +} + +static inline bool +helper_cairo_user_scaled_font_has_color (cairo_scaled_font_t *scaled_font) +{ + /* Ignoring SVG for now, since we cannot render it. */ + hb_font_t *font = (hb_font_t *) (cairo_font_face_get_user_data (cairo_scaled_font_get_font_face (scaled_font), + &_hb_font_cairo_user_data_key)); + return hb_ot_color_has_png (hb_font_get_face (font)); +} + +#endif diff --git a/util/helper-cairo.hh b/util/helper-cairo.hh index b8d461224..b42913e37 100644 --- a/util/helper-cairo.hh +++ b/util/helper-cairo.hh @@ -29,10 +29,11 @@ #include "view-options.hh" #include "output-options.hh" +#include "helper-cairo-ft.hh" +#include "helper-cairo-user.hh" -#include -#include -#include FT_MULTIPLE_MASTERS_H +#include +#include #include "helper-cairo-ansi.hh" #ifdef CAIRO_HAS_SVG_SURFACE @@ -65,16 +66,12 @@ _cairo_eps_surface_create_for_stream (cairo_write_func_t write_func, # endif #endif - -static FT_Library ft_library; - -#ifdef HAVE_ATEXIT -static inline -void free_ft_library () +static inline bool +helper_cairo_use_hb_draw (const font_options_t *font_opts) { - FT_Done_FreeType (ft_library); + return cairo_version () >= CAIRO_VERSION_ENCODE (1, 17, 5) && + (font_opts->font_funcs && strcmp (font_opts->font_funcs, "ft")); } -#endif static inline cairo_scaled_font_t * helper_cairo_create_scaled_font (const font_options_t *font_opts) @@ -82,57 +79,11 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts) hb_font_t *font = hb_font_reference (font_opts->font); cairo_font_face_t *cairo_face; - /* We cannot use the FT_Face from hb_font_t, as doing so will confuse hb_font_t because - * cairo will reset the face size. As such, create new face... - * TODO Perhaps add API to hb-ft to encapsulate this code. */ - FT_Face ft_face = nullptr;//hb_ft_font_get_face (font); - if (!ft_face) - { - if (!ft_library) - { - FT_Init_FreeType (&ft_library); -#ifdef HAVE_ATEXIT - atexit (free_ft_library); -#endif - } - - unsigned int blob_length; - const char *blob_data = hb_blob_get_data (font_opts->blob, &blob_length); - - if (FT_New_Memory_Face (ft_library, - (const FT_Byte *) blob_data, - blob_length, - font_opts->face_index, - &ft_face)) - fail (false, "FT_New_Memory_Face fail"); - } - if (!ft_face) - { - /* This allows us to get some boxes at least... */ - cairo_face = cairo_toy_font_face_create ("@cairo:sans", - CAIRO_FONT_SLANT_NORMAL, - CAIRO_FONT_WEIGHT_NORMAL); - } + if (helper_cairo_use_hb_draw (font_opts)) + cairo_face = helper_cairo_create_user_font_face (font_opts); else - { -#ifdef HAVE_FT_SET_VAR_BLEND_COORDINATES - unsigned int num_coords; - const int *coords = hb_font_get_var_coords_normalized (font, &num_coords); - if (num_coords) - { - FT_Fixed *ft_coords = (FT_Fixed *) calloc (num_coords, sizeof (FT_Fixed)); - if (ft_coords) - { - for (unsigned int i = 0; i < num_coords; i++) - ft_coords[i] = coords[i] << 2; - FT_Set_Var_Blend_Coordinates (ft_face, num_coords, ft_coords); - free (ft_coords); - } - } -#endif + cairo_face = helper_cairo_create_ft_font_face (font_opts); - cairo_face = cairo_ft_font_face_create_for_ft_face (ft_face, font_opts->ft_load_flags); - } cairo_matrix_t ctm, font_matrix; cairo_font_options_t *font_options; @@ -165,17 +116,10 @@ helper_cairo_create_scaled_font (const font_options_t *font_opts) static inline 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; + if (helper_cairo_user_font_face_has_data (cairo_scaled_font_get_font_face (scaled_font))) + return helper_cairo_user_scaled_font_has_color (scaled_font); + else + return helper_cairo_ft_scaled_font_has_color (scaled_font); }