From a74b164f176bffd8f7d34618d669c492bff5c25e Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 15 Oct 2018 16:34:01 -0400 Subject: [PATCH 1/8] Add api to get a hb_font_t Add pango_font_get_hb_font, which will make it easier access harfbuzz features. --- pango/fonts.c | 50 ++++++++++++++++++++++++++++++++++++++++- pango/pango-font.h | 6 ++++- pango/pangofc-font.c | 13 ++++++++++- pango/pangofc-fontmap.c | 25 +++++++++++++++++++++ pango/pangofc-fontmap.h | 7 ++++++ 5 files changed, 98 insertions(+), 3 deletions(-) diff --git a/pango/fonts.c b/pango/fonts.c index b46aef027..ebf1b3d1a 100644 --- a/pango/fonts.c +++ b/pango/fonts.c @@ -1617,11 +1617,29 @@ pango_parse_stretch (const char *str, * PangoFont */ -G_DEFINE_ABSTRACT_TYPE (PangoFont, pango_font, G_TYPE_OBJECT) +typedef struct { + hb_font_t *hb_font; +} PangoFontPrivate; + +G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (PangoFont, pango_font, G_TYPE_OBJECT) + +static void +pango_font_finalize (GObject *object) +{ + PangoFont *font = PANGO_FONT (object); + PangoFontPrivate *priv = pango_font_get_instance_private (font); + + hb_font_destroy (priv->hb_font); + + G_OBJECT_CLASS (pango_font_parent_class)->finalize (object); +} static void pango_font_class_init (PangoFontClass *class G_GNUC_UNUSED) { + GObjectClass *object_class = G_OBJECT_CLASS (class); + + object_class->finalize = pango_font_finalize; } static void @@ -1837,6 +1855,36 @@ pango_font_get_font_map (PangoFont *font) return NULL; } +/** + * pango_font_get_hb_font: + * @font: a #PangoFont + * + * Get a hb_font_t object backing this font. + * + * Returns: (transfer none) (nullable): the hb_font_t object backing the + * font, or %NULL if the font does not have one + * + * Since: 1.44 + */ +hb_font_t * +pango_font_get_hb_font (PangoFont *font) +{ + PangoFontPrivate *priv = pango_font_get_instance_private (font); + + if (G_UNLIKELY (!font)) + return NULL; + + if (priv->hb_font) + return priv->hb_font; + else if (PANGO_FONT_GET_CLASS (font)->create_hb_font) + { + priv->hb_font = PANGO_FONT_GET_CLASS (font)->create_hb_font (font); + return priv->hb_font; + } + + return hb_font_get_empty (); +} + G_DEFINE_BOXED_TYPE (PangoFontMetrics, pango_font_metrics, pango_font_metrics_ref, pango_font_metrics_unref); diff --git a/pango/pango-font.h b/pango/pango-font.h index 1b85c386a..461ece878 100644 --- a/pango/pango-font.h +++ b/pango/pango-font.h @@ -26,6 +26,7 @@ #include #include +#include G_BEGIN_DECLS @@ -585,6 +586,9 @@ void pango_font_get_glyph_extents (PangoFont *font, PANGO_AVAILABLE_IN_1_10 PangoFontMap *pango_font_get_font_map (PangoFont *font); +PANGO_AVAILABLE_IN_1_44 +hb_font_t * pango_font_get_hb_font (PangoFont *font); + #ifdef PANGO_ENABLE_BACKEND #define PANGO_FONT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PANGO_TYPE_FONT, PangoFontClass)) @@ -634,11 +638,11 @@ struct _PangoFontClass PangoLanguage *language); PangoFontMap * (*get_font_map) (PangoFont *font); PangoFontDescription *(*describe_absolute) (PangoFont *font); + hb_font_t * (*create_hb_font) (PangoFont *font); /*< private >*/ /* Padding for future expansion */ void (*_pango_reserved1) (void); - void (*_pango_reserved2) (void); }; /* used for very rare and miserable situtations that we cannot even diff --git a/pango/pangofc-font.c b/pango/pangofc-font.c index 5a48e7822..fffc6e2d6 100644 --- a/pango/pangofc-font.c +++ b/pango/pangofc-font.c @@ -89,7 +89,7 @@ static PangoFontMetrics * pango_fc_font_get_metrics (PangoFont *font, static PangoFontMap * pango_fc_font_get_font_map (PangoFont *font); static PangoFontDescription *pango_fc_font_describe (PangoFont *font); static PangoFontDescription *pango_fc_font_describe_absolute (PangoFont *font); - +static hb_font_t * pango_fc_font_create_hb_font (PangoFont *font); #define PANGO_FC_FONT_LOCK_FACE(font) (PANGO_FC_FONT_GET_CLASS (font)->lock_face (font)) #define PANGO_FC_FONT_UNLOCK_FACE(font) (PANGO_FC_FONT_GET_CLASS (font)->unlock_face (font)) @@ -116,6 +116,7 @@ pango_fc_font_class_init (PangoFcFontClass *class) font_class->get_coverage = pango_fc_font_get_coverage; font_class->get_metrics = pango_fc_font_get_metrics; font_class->get_font_map = pango_fc_font_get_font_map; + font_class->create_hb_font = pango_fc_font_create_hb_font; g_object_class_install_property (object_class, PROP_PATTERN, g_param_spec_pointer ("pattern", @@ -1067,3 +1068,13 @@ pango_fc_font_get_raw_extents (PangoFcFont *fcfont, PANGO_FC_FONT_UNLOCK_FACE (fcfont); } +static hb_font_t * +pango_fc_font_create_hb_font (PangoFont *font) +{ + PangoFcFont *fcfont = PANGO_FC_FONT (font); + hb_face_t *hb_face; + + hb_face = pango_fc_font_map_get_hb_face (PANGO_FC_FONT_MAP (fcfont->fontmap), fcfont); + + return hb_font_create (hb_face); +} diff --git a/pango/pangofc-fontmap.c b/pango/pangofc-fontmap.c index 7d75f6f9c..575f5d031 100644 --- a/pango/pangofc-fontmap.c +++ b/pango/pangofc-fontmap.c @@ -50,6 +50,7 @@ #include "pangofc-private.h" #include "pango-impl-utils.h" #include "pango-enum-types.h" +#include /* Overview: @@ -164,6 +165,8 @@ struct _PangoFcFontFaceData FcPattern *pattern; /* Referenced pattern that owns filename */ PangoCoverage *coverage; PangoFcCmapCache *cmap_cache; + + hb_face_t *hb_face; }; struct _PangoFcFace @@ -295,6 +298,8 @@ pango_fc_font_face_data_free (PangoFcFontFaceData *data) if (data->cmap_cache) _pango_fc_cmap_cache_unref (data->cmap_cache); + hb_face_destroy (data->hb_face); + g_slice_free (PangoFcFontFaceData, data); } @@ -2689,3 +2694,23 @@ pango_fc_family_init (PangoFcFamily *fcfamily) { fcfamily->n_faces = -1; } + +hb_face_t * +pango_fc_font_map_get_hb_face (PangoFcFontMap *fcfontmap, + PangoFcFont *fcfont) +{ + PangoFcFontFaceData *data; + + data = pango_fc_font_map_get_font_face_data (fcfontmap, fcfont->font_pattern); + + if (!data->hb_face) + { + hb_blob_t *blob; + + blob = hb_blob_create_from_file (data->filename); + data->hb_face = hb_face_create (blob, data->id); + hb_blob_destroy (blob); + } + + return data->hb_face; +} diff --git a/pango/pangofc-fontmap.h b/pango/pangofc-fontmap.h index bff288b68..afa402df9 100644 --- a/pango/pangofc-fontmap.h +++ b/pango/pangofc-fontmap.h @@ -26,6 +26,7 @@ #include #include #include +#include G_BEGIN_DECLS @@ -187,6 +188,7 @@ struct _PangoFcFontMapClass FcPattern *pattern); PangoFcFont *(*create_font) (PangoFcFontMap *fontmap, PangoFcFontKey *fontkey); + /*< private >*/ /* Padding for future expansion */ @@ -249,6 +251,11 @@ PANGO_AVAILABLE_IN_1_4 PangoFontDescription *pango_fc_font_description_from_pattern (FcPattern *pattern, gboolean include_size); +PANGO_AVAILABLE_IN_1_44 +hb_face_t * pango_fc_font_map_get_hb_face (PangoFcFontMap *fcfontmap, + PangoFcFont *fcfont); + + /** * PANGO_FC_GRAVITY: * -- GitLab From d99bbf617abd3997a67eac10a15ddab7afdf65ba Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 19 Nov 2018 11:47:42 -0500 Subject: [PATCH 2/8] fc: Move font setup code from the shaper Move all the code that sets up the hb_font_t to pango_fc_font_create_hb_font, and use it from the shaper. This is the second step towards taking over font management. Even better, harfbuzz has a ready-made function for this. We can drop a lot of FT_Face-using code this way. We assume unhinted rendering for now, so we can set ppem to 0. --- pango/pangofc-font.c | 127 ++++++++++++++- pango/pangofc-fontmap.c | 4 +- pango/pangofc-shape.c | 334 ++++------------------------------------ 3 files changed, 153 insertions(+), 312 deletions(-) diff --git a/pango/pangofc-font.c b/pango/pangofc-font.c index fffc6e2d6..65b6cb73c 100644 --- a/pango/pangofc-font.c +++ b/pango/pangofc-font.c @@ -46,6 +46,7 @@ #include "pango-layout.h" #include "pango-impl-utils.h" +#include #include #include FT_TRUETYPE_TABLES_H @@ -1068,13 +1069,133 @@ pango_fc_font_get_raw_extents (PangoFcFont *fcfont, PANGO_FC_FONT_UNLOCK_FACE (fcfont); } +extern gpointer get_gravity_class (void); + +static PangoGravity +pango_fc_font_key_get_gravity (PangoFcFontKey *key) +{ + FcPattern *pattern; + PangoGravity gravity = PANGO_GRAVITY_SOUTH; + FcChar8 *s; + + pattern = pango_fc_font_key_get_pattern (key); + if (FcPatternGetString (pattern, PANGO_FC_GRAVITY, 0, (FcChar8 **)&s) == FcResultMatch) + { + GEnumValue *value = g_enum_get_value_by_nick (get_gravity_class (), (char *)s); + gravity = value->value; + } + + return gravity; +} + +static double +get_font_size (PangoFcFontKey *key) +{ + FcPattern *pattern; + double size; + double dpi; + + pattern = pango_fc_font_key_get_pattern (key); + if (FcPatternGetDouble (pattern, FC_PIXEL_SIZE, 0, &size) == FcResultMatch) + return size; + + /* Just in case FC_PIXEL_SIZE got unset between pango_fc_make_pattern() + * and here. That would be very weird. + */ + + if (FcPatternGetDouble (pattern, FC_DPI, 0, &dpi) != FcResultMatch) + dpi = 72; + + if (FcPatternGetDouble (pattern, FC_SIZE, 0, &size) == FcResultMatch) + return size * dpi / 72.; + + /* Whatever */ + return 18.; +} + +static void +parse_variations (const char *variations, + hb_variation_t **hb_variations, + guint *n_variations) +{ + guint n; + hb_variation_t *var; + int i; + const char *p; + + n = 1; + for (i = 0; variations[i]; i++) + { + if (variations[i] == ',') + n++; + } + + var = g_new (hb_variation_t, n); + + p = variations; + n = 0; + while (p && *p) + { + char *end = strchr (p, ','); + if (hb_variation_from_string (p, end ? end - p: -1, &var[n])) + n++; + p = end ? end + 1 : NULL; + } + + *hb_variations = var; + *n_variations = n; +} + static hb_font_t * pango_fc_font_create_hb_font (PangoFont *font) { - PangoFcFont *fcfont = PANGO_FC_FONT (font); + PangoFcFont *fc_font = PANGO_FC_FONT (font); + PangoFcFontKey *key; hb_face_t *hb_face; + hb_font_t *hb_font; + double x_scale_inv, y_scale_inv; + double x_scale, y_scale; + double size; + PangoGravity gravity; - hb_face = pango_fc_font_map_get_hb_face (PANGO_FC_FONT_MAP (fcfont->fontmap), fcfont); + x_scale_inv = y_scale_inv = 1.0; + key = _pango_fc_font_get_font_key (fc_font); + if (key) + { + const PangoMatrix *matrix = pango_fc_font_key_get_matrix (key); + pango_matrix_get_font_scale_factors (matrix, &x_scale_inv, &y_scale_inv); + } + if (PANGO_GRAVITY_IS_IMPROPER (gravity)) + { + x_scale_inv = -x_scale_inv; + y_scale_inv = -y_scale_inv; + } + + x_scale = 1. / x_scale_inv; + y_scale = 1. / y_scale_inv; + + size = get_font_size (key); + gravity = pango_fc_font_key_get_gravity (key); + hb_face = pango_fc_font_map_get_hb_face (PANGO_FC_FONT_MAP (fc_font->fontmap), fc_font); + + hb_font = hb_font_create (hb_face); + hb_ot_font_set_funcs (hb_font); + hb_font_set_scale (hb_font, size * 1024 * x_scale, size * 1024 * y_scale); + hb_font_set_ppem (hb_font, 0, 0); + if (key) + { + const char *variations = pango_fc_font_key_get_variations (key); + if (variations) + { + guint n_variations; + hb_variation_t *hb_variations; + + parse_variations (variations, &hb_variations, &n_variations); + hb_font_set_variations (hb_font, hb_variations, n_variations); + + g_free (hb_variations); + } + } - return hb_font_create (hb_face); + return hb_font; } diff --git a/pango/pangofc-fontmap.c b/pango/pangofc-fontmap.c index 575f5d031..0f80665ef 100644 --- a/pango/pangofc-fontmap.c +++ b/pango/pangofc-fontmap.c @@ -262,7 +262,9 @@ static FcPattern *pango_fc_patterns_get_font_pattern (PangoFcPatterns *pat static FcPattern *uniquify_pattern (PangoFcFontMap *fcfontmap, FcPattern *pattern); -static gpointer +gpointer get_gravity_class (void); + +gpointer get_gravity_class (void) { static GEnumClass *class = NULL; /* MT-safe */ diff --git a/pango/pangofc-shape.c b/pango/pangofc-shape.c index 53269d73f..5efbdeebd 100644 --- a/pango/pangofc-shape.c +++ b/pango/pangofc-shape.c @@ -68,249 +68,6 @@ release_buffer (hb_buffer_t *buffer, gboolean free_buffer) hb_buffer_destroy (buffer); } -typedef struct _PangoFcHbContext { - FT_Face ft_face; - PangoFcFont *fc_font; - gboolean vertical; - double x_scale, y_scale; /* CTM scales. */ -} PangoFcHbContext; - -static hb_bool_t -pango_fc_hb_font_get_nominal_glyph (hb_font_t *font, void *font_data, - hb_codepoint_t unicode, - hb_codepoint_t *glyph, - void *user_data G_GNUC_UNUSED) -{ - PangoFcHbContext *context = (PangoFcHbContext *) font_data; - PangoFcFont *fc_font = context->fc_font; - - *glyph = pango_fc_font_get_glyph (fc_font, unicode); - if (G_LIKELY (*glyph)) - return TRUE; - - *glyph = PANGO_GET_UNKNOWN_GLYPH (unicode); - - /* We draw our own invalid-Unicode shape, so prevent HarfBuzz - * from using REPLACEMENT CHARACTER. */ - if (unicode > 0x10FFFF) - return TRUE; - - return FALSE; -} - -static hb_bool_t -pango_fc_hb_font_get_variation_glyph (hb_font_t *font, - void *font_data, - hb_codepoint_t unicode, - hb_codepoint_t variation_selector, - hb_codepoint_t *glyph, - void *user_data G_GNUC_UNUSED) -{ - PangoFcHbContext *context = (PangoFcHbContext *) font_data; - FT_Face ft_face = context->ft_face; - unsigned int g; - - g = FT_Face_GetCharVariantIndex (ft_face, unicode, variation_selector); - - if (G_UNLIKELY (!g)) - return FALSE; - - *glyph = g; - return TRUE; -} - -static hb_bool_t -pango_fc_hb_font_get_glyph_contour_point (hb_font_t *font, void *font_data, - hb_codepoint_t glyph, unsigned int point_index, - hb_position_t *x, hb_position_t *y, - void *user_data G_GNUC_UNUSED) -{ - return FALSE; -#if 0 - FT_Face ft_face = (FT_Face) font_data; - int load_flags = FT_LOAD_DEFAULT; - - /* TODO: load_flags, embolden, etc */ - - if (HB_UNLIKELY (FT_Load_Glyph (ft_face, glyph, load_flags))) - return FALSE; - - if (HB_UNLIKELY (ft_face->glyph->format != FT_GLYPH_FORMAT_OUTLINE)) - return FALSE; - - if (HB_UNLIKELY (point_index >= (unsigned int) ft_face->glyph->outline.n_points)) - return FALSE; - - *x = ft_face->glyph->outline.points[point_index].x; - *y = ft_face->glyph->outline.points[point_index].y; - - return TRUE; -#endif -} - -static hb_position_t -pango_fc_hb_font_get_glyph_advance (hb_font_t *font, void *font_data, - hb_codepoint_t glyph, - void *user_data G_GNUC_UNUSED) -{ - PangoFcHbContext *context = (PangoFcHbContext *) font_data; - PangoFcFont *fc_font = context->fc_font; - PangoRectangle logical; - - pango_font_get_glyph_extents ((PangoFont *) fc_font, glyph, NULL, &logical); - - return logical.width; -} - -static hb_bool_t -pango_fc_hb_font_get_glyph_extents (hb_font_t *font, void *font_data, - hb_codepoint_t glyph, - hb_glyph_extents_t *extents, - void *user_data G_GNUC_UNUSED) -{ - PangoFcHbContext *context = (PangoFcHbContext *) font_data; - PangoFcFont *fc_font = context->fc_font; - PangoRectangle ink; - - pango_font_get_glyph_extents ((PangoFont *) fc_font, glyph, &ink, NULL); - - if (G_LIKELY (!context->vertical)) { - extents->x_bearing = ink.x; - extents->y_bearing = ink.y; - extents->width = ink.width; - extents->height = ink.height; - } else { - /* XXX */ - extents->x_bearing = ink.x; - extents->y_bearing = ink.y; - extents->width = ink.height; - extents->height = ink.width; - } - - return TRUE; -} - -static hb_bool_t -pango_fc_hb_font_get_glyph_h_origin (hb_font_t *font, void *font_data, - hb_codepoint_t glyph, - hb_position_t *x, hb_position_t *y, - void *user_data G_GNUC_UNUSED) -{ - PangoFcHbContext *context = (PangoFcHbContext *) font_data; - FT_Face ft_face = context->ft_face; - int load_flags = FT_LOAD_DEFAULT; - - if (!context->vertical) return TRUE; - - if (FT_Load_Glyph (ft_face, glyph, load_flags)) - return FALSE; - - /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates - * have a Y growing upward. Hence the extra negations. */ - *x = -PANGO_UNITS_26_6 (ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX); - *y = +PANGO_UNITS_26_6 (ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY)); - - return TRUE; -} - -static hb_bool_t -pango_fc_hb_font_get_glyph_v_origin (hb_font_t *font, void *font_data, - hb_codepoint_t glyph, - hb_position_t *x, hb_position_t *y, - void *user_data G_GNUC_UNUSED) -{ - PangoFcHbContext *context = (PangoFcHbContext *) font_data; - FT_Face ft_face = context->ft_face; - int load_flags = FT_LOAD_DEFAULT; - - /* pangocairo-fc configures font in vertical origin for vertical writing. */ - if (context->vertical) return TRUE; - - if (FT_Load_Glyph (ft_face, glyph, load_flags)) - return FALSE; - - /* Note: FreeType's vertical metrics grows downward while other FreeType coordinates - * have a Y growing upward. Hence the extra negation. */ - *x = PANGO_UNITS_26_6 (ft_face->glyph->metrics.horiBearingX - ft_face->glyph->metrics.vertBearingX); - *y = PANGO_UNITS_26_6 (ft_face->glyph->metrics.horiBearingY - (-ft_face->glyph->metrics.vertBearingY)); - - /* XXX */ - - return TRUE; -} - - -static hb_position_t -pango_fc_hb_font_get_h_kerning (hb_font_t *font, void *font_data, - hb_codepoint_t left_glyph, hb_codepoint_t right_glyph, - void *user_data G_GNUC_UNUSED) -{ - PangoFcHbContext *context = (PangoFcHbContext *) font_data; - FT_Face ft_face = context->ft_face; - FT_Vector kerning; - - if (FT_Get_Kerning (ft_face, left_glyph, right_glyph, FT_KERNING_DEFAULT, &kerning)) - return 0; - - return PANGO_UNITS_26_6 (kerning.x * context->x_scale); -} - -static hb_font_funcs_t * -pango_fc_get_hb_font_funcs (void) -{ - static hb_font_funcs_t *funcs; - - if (G_UNLIKELY (!funcs)) { - funcs = hb_font_funcs_create (); - hb_font_funcs_set_nominal_glyph_func (funcs, pango_fc_hb_font_get_nominal_glyph, NULL, NULL); - hb_font_funcs_set_variation_glyph_func (funcs, pango_fc_hb_font_get_variation_glyph, NULL, NULL); - hb_font_funcs_set_glyph_h_advance_func (funcs, pango_fc_hb_font_get_glyph_advance, NULL, NULL); - hb_font_funcs_set_glyph_v_advance_func (funcs, pango_fc_hb_font_get_glyph_advance, NULL, NULL); - hb_font_funcs_set_glyph_h_origin_func (funcs, pango_fc_hb_font_get_glyph_h_origin, NULL, NULL); - hb_font_funcs_set_glyph_v_origin_func (funcs, pango_fc_hb_font_get_glyph_v_origin, NULL, NULL); - hb_font_funcs_set_glyph_h_kerning_func (funcs, pango_fc_hb_font_get_h_kerning, NULL, NULL); - /* Don't need v_kerning. */ - hb_font_funcs_set_glyph_extents_func (funcs, pango_fc_hb_font_get_glyph_extents, NULL, NULL); - hb_font_funcs_set_glyph_contour_point_func (funcs, pango_fc_hb_font_get_glyph_contour_point, NULL, NULL); - /* Don't need glyph_name / glyph_from_name */ - } - - return funcs; -} - -static void -parse_variations (const char *variations, - hb_variation_t **hb_variations, - guint *n_variations) -{ - guint n; - hb_variation_t *var; - int i; - const char *p; - - n = 1; - for (i = 0; variations[i]; i++) - { - if (variations[i] == ',') - n++; - } - - var = g_new (hb_variation_t, n); - - p = variations; - n = 0; - while (p && *p) - { - char *end = strchr (p, ','); - if (hb_variation_from_string (p, end ? end - p: -1, &var[n])) - n++; - p = end ? end + 1 : NULL; - } - - *hb_variations = var; - *n_variations = n; -} - void _pango_fc_shape (PangoFont *font, const char *item_text, @@ -320,11 +77,7 @@ _pango_fc_shape (PangoFont *font, const char *paragraph_text, unsigned int paragraph_length) { - PangoFcHbContext context; PangoFcFont *fc_font; - PangoFcFontKey *key; - FT_Face ft_face; - hb_face_t *hb_face; hb_font_t *hb_font; hb_buffer_t *hb_buffer; hb_direction_t hb_direction; @@ -336,64 +89,12 @@ _pango_fc_shape (PangoFont *font, unsigned int item_offset = item_text - paragraph_text; hb_feature_t features[32]; unsigned int num_features = 0; - double x_scale_inv, y_scale_inv; PangoGlyphInfo *infos; - const char *variations; g_return_if_fail (font != NULL); g_return_if_fail (analysis != NULL); - fc_font = PANGO_FC_FONT (font); - ft_face = pango_fc_font_lock_face (fc_font); - if (!ft_face) - return; - - /* TODO: Cache hb_font? */ - - x_scale_inv = y_scale_inv = 1.0; - key = _pango_fc_font_get_font_key (fc_font); - if (key) - { - const PangoMatrix *matrix = pango_fc_font_key_get_matrix (key); - pango_matrix_get_font_scale_factors (matrix, &x_scale_inv, &y_scale_inv); - } - if (PANGO_GRAVITY_IS_IMPROPER (analysis->gravity)) - { - x_scale_inv = -x_scale_inv; - y_scale_inv = -y_scale_inv; - } - context.x_scale = 1. / x_scale_inv; - context.y_scale = 1. / y_scale_inv; - context.ft_face = ft_face; - context.fc_font = fc_font; - context.vertical = PANGO_GRAVITY_IS_VERTICAL (analysis->gravity); - hb_face = hb_ft_face_create_cached (ft_face); - hb_font = hb_font_create (hb_face); - hb_font_set_funcs (hb_font, - pango_fc_get_hb_font_funcs (), - &context, - NULL); - hb_font_set_scale (hb_font, - +(((gint64) ft_face->size->metrics.x_scale * ft_face->units_per_EM) >> 12) * context.x_scale, - -(((gint64) ft_face->size->metrics.y_scale * ft_face->units_per_EM) >> 12) * context.y_scale); - hb_font_set_ppem (hb_font, - fc_font->is_hinted ? ft_face->size->metrics.x_ppem : 0, - fc_font->is_hinted ? ft_face->size->metrics.y_ppem : 0); - - if (key) - { - variations = pango_fc_font_key_get_variations (key); - if (variations) - { - guint n_variations; - hb_variation_t *hb_variations; - - parse_variations (variations, &hb_variations, &n_variations); - hb_font_set_variations (hb_font, hb_variations, n_variations); - - g_free (hb_variations); - } - } + hb_font = pango_font_get_hb_font (font); hb_buffer = acquire_buffer (&free_buffer); @@ -416,6 +117,7 @@ _pango_fc_shape (PangoFont *font, hb_buffer_add_utf8 (hb_buffer, paragraph_text, paragraph_length, item_offset, item_length); /* Setup features from fontconfig pattern. */ + fc_font = PANGO_FC_FONT (font); if (fc_font->font_pattern) { char *s; @@ -489,7 +191,7 @@ _pango_fc_shape (PangoFont *font, } hb_position = hb_buffer_get_glyph_positions (hb_buffer, NULL); - if (context.vertical) + if (PANGO_GRAVITY_IS_VERTICAL (analysis->gravity)) for (i = 0; i < num_glyphs; i++) { /* 90 degrees rotation counter-clockwise. */ @@ -509,7 +211,26 @@ _pango_fc_shape (PangoFont *font, if (fc_font->is_hinted) { - if (context.x_scale == 1.0 && context.y_scale == 1.0) + double x_scale_inv, y_scale_inv; + double x_scale, y_scale; + PangoFcFontKey *key; + + x_scale_inv = y_scale_inv = 1.0; + key = _pango_fc_font_get_font_key (fc_font); + if (key) + { + const PangoMatrix *matrix = pango_fc_font_key_get_matrix (key); + pango_matrix_get_font_scale_factors (matrix, &x_scale_inv, &y_scale_inv); + } + if (PANGO_GRAVITY_IS_IMPROPER (analysis->gravity)) + { + x_scale_inv = -x_scale_inv; + y_scale_inv = -y_scale_inv; + } + x_scale = 1. / x_scale_inv; + y_scale = 1. / y_scale_inv; + + if (x_scale == 1.0 && y_scale == 1.0) { for (i = 0; i < num_glyphs; i++) infos[i].geometry.width = PANGO_UNITS_ROUND (infos[i].geometry.width); @@ -517,7 +238,7 @@ _pango_fc_shape (PangoFont *font, else { #if 0 - if (context.vertical) + if (PANGO_GRAVITY_IS_VERTICAL (analysis->gravity)) { /* XXX */ double tmp = x_scale; @@ -526,8 +247,8 @@ _pango_fc_shape (PangoFont *font, } #endif #define HINT(value, scale_inv, scale) (PANGO_UNITS_ROUND ((int) ((value) * scale)) * scale_inv) -#define HINT_X(value) HINT ((value), context.x_scale, x_scale_inv) -#define HINT_Y(value) HINT ((value), context.y_scale, y_scale_inv) +#define HINT_X(value) HINT ((value), x_scale, x_scale_inv) +#define HINT_Y(value) HINT ((value), y_scale, y_scale_inv) for (i = 0; i < num_glyphs; i++) { infos[i].geometry.width = HINT_X (infos[i].geometry.width); @@ -541,7 +262,4 @@ _pango_fc_shape (PangoFont *font, } release_buffer (hb_buffer, free_buffer); - hb_font_destroy (hb_font); - hb_face_destroy (hb_face); - pango_fc_font_unlock_face (fc_font); } -- GitLab From 246f46b9cd390789edf3e1cdc93f1685efce82e6 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Mon, 19 Nov 2018 13:59:26 -0500 Subject: [PATCH 3/8] fc: Make pangofc_shape backend-neutral Use typechecks for the two remaining bits of code that use PangoFcFont. --- pango/pangofc-shape.c | 187 ++++++++++++++++++++++-------------------- 1 file changed, 100 insertions(+), 87 deletions(-) diff --git a/pango/pangofc-shape.c b/pango/pangofc-shape.c index 5efbdeebd..6313208a7 100644 --- a/pango/pangofc-shape.c +++ b/pango/pangofc-shape.c @@ -68,6 +68,34 @@ release_buffer (hb_buffer_t *buffer, gboolean free_buffer) hb_buffer_destroy (buffer); } +static void +pango_font_get_features (PangoFont *font, + hb_feature_t *features, + unsigned int len, + unsigned int *num_features) +{ + if (PANGO_IS_FC_FONT (font)) + { + /* Setup features from fontconfig pattern. */ + PangoFcFont *fc_font = PANGO_FC_FONT (font); + if (fc_font->font_pattern) + { + char *s; + while (*num_features < len && + FcResultMatch == FcPatternGetString (fc_font->font_pattern, + PANGO_FC_FONT_FEATURES, + *num_features, + (FcChar8 **) &s)) + { + gboolean ret = hb_feature_from_string (s, -1, &features[*num_features]); + features[*num_features].start = 0; + features[*num_features].end = (unsigned int) -1; + if (ret) + (*num_features)++; + } + } + } +} void _pango_fc_shape (PangoFont *font, const char *item_text, @@ -77,7 +105,6 @@ _pango_fc_shape (PangoFont *font, const char *paragraph_text, unsigned int paragraph_length) { - PangoFcFont *fc_font; hb_font_t *hb_font; hb_buffer_t *hb_buffer; hb_direction_t hb_direction; @@ -116,58 +143,40 @@ _pango_fc_shape (PangoFont *font, hb_buffer_add_utf8 (hb_buffer, paragraph_text, paragraph_length, item_offset, item_length); - /* Setup features from fontconfig pattern. */ - fc_font = PANGO_FC_FONT (font); - if (fc_font->font_pattern) - { - char *s; - while (num_features < G_N_ELEMENTS (features) && - FcResultMatch == FcPatternGetString (fc_font->font_pattern, - PANGO_FC_FONT_FEATURES, - num_features, - (FcChar8 **) &s)) - { - gboolean ret = hb_feature_from_string (s, -1, &features[num_features]); - features[num_features].start = 0; - features[num_features].end = (unsigned int) -1; - if (ret) - num_features++; - } - } + pango_font_get_features (font, features, G_N_ELEMENTS (features), &num_features); if (analysis->extra_attrs) { GSList *tmp_attrs; for (tmp_attrs = analysis->extra_attrs; tmp_attrs && num_features < G_N_ELEMENTS (features); tmp_attrs = tmp_attrs->next) - { - if (((PangoAttribute *) tmp_attrs->data)->klass->type == PANGO_ATTR_FONT_FEATURES) - { - const PangoAttrFontFeatures *fattr = (const PangoAttrFontFeatures *) tmp_attrs->data; - const gchar *feat; - const gchar *end; + { + if (((PangoAttribute *) tmp_attrs->data)->klass->type == PANGO_ATTR_FONT_FEATURES) + { + const PangoAttrFontFeatures *fattr = (const PangoAttrFontFeatures *) tmp_attrs->data; + const char *feat; + const char *end; int len; - feat = fattr->features; - + feat = fattr->features; while (feat != NULL && num_features < G_N_ELEMENTS (features)) - { - end = strchr (feat, ','); - if (end) - len = end - feat; - else - len = -1; - - if (hb_feature_from_string (feat, len, &features[num_features])) - num_features++; - - if (end == NULL) - break; - - feat = end + 1; - } - } - } + { + end = strchr (feat, ','); + if (end) + len = end - feat; + else + len = -1; + + if (hb_feature_from_string (feat, len, &features[num_features])) + num_features++; + + if (end == NULL) + break; + + feat = end + 1; + } + } + } } hb_shape (hb_font, hb_buffer, features, num_features); @@ -209,57 +218,61 @@ _pango_fc_shape (PangoFont *font, hb_position++; } - if (fc_font->is_hinted) - { - double x_scale_inv, y_scale_inv; - double x_scale, y_scale; - PangoFcFontKey *key; - - x_scale_inv = y_scale_inv = 1.0; - key = _pango_fc_font_get_font_key (fc_font); - if (key) - { - const PangoMatrix *matrix = pango_fc_font_key_get_matrix (key); - pango_matrix_get_font_scale_factors (matrix, &x_scale_inv, &y_scale_inv); - } - if (PANGO_GRAVITY_IS_IMPROPER (analysis->gravity)) - { - x_scale_inv = -x_scale_inv; - y_scale_inv = -y_scale_inv; - } - x_scale = 1. / x_scale_inv; - y_scale = 1. / y_scale_inv; - - if (x_scale == 1.0 && y_scale == 1.0) - { - for (i = 0; i < num_glyphs; i++) - infos[i].geometry.width = PANGO_UNITS_ROUND (infos[i].geometry.width); - } - else - { + if (PANGO_IS_FC_FONT (font)) + { + PangoFcFont *fc_font = PANGO_FC_FONT (font); + if (fc_font->is_hinted) + { + double x_scale_inv, y_scale_inv; + double x_scale, y_scale; + PangoFcFontKey *key; + + x_scale_inv = y_scale_inv = 1.0; + key = _pango_fc_font_get_font_key (fc_font); + if (key) + { + const PangoMatrix *matrix = pango_fc_font_key_get_matrix (key); + pango_matrix_get_font_scale_factors (matrix, &x_scale_inv, &y_scale_inv); + } + if (PANGO_GRAVITY_IS_IMPROPER (analysis->gravity)) + { + x_scale_inv = -x_scale_inv; + y_scale_inv = -y_scale_inv; + } + x_scale = 1. / x_scale_inv; + y_scale = 1. / y_scale_inv; + + if (x_scale == 1.0 && y_scale == 1.0) + { + for (i = 0; i < num_glyphs; i++) + infos[i].geometry.width = PANGO_UNITS_ROUND (infos[i].geometry.width); + } + else + { #if 0 - if (PANGO_GRAVITY_IS_VERTICAL (analysis->gravity)) - { - /* XXX */ - double tmp = x_scale; - x_scale = y_scale; - y_scale = -tmp; - } + if (PANGO_GRAVITY_IS_VERTICAL (analysis->gravity)) + { + /* XXX */ + double tmp = x_scale; + x_scale = y_scale; + y_scale = -tmp; + } #endif #define HINT(value, scale_inv, scale) (PANGO_UNITS_ROUND ((int) ((value) * scale)) * scale_inv) #define HINT_X(value) HINT ((value), x_scale, x_scale_inv) #define HINT_Y(value) HINT ((value), y_scale, y_scale_inv) - for (i = 0; i < num_glyphs; i++) - { - infos[i].geometry.width = HINT_X (infos[i].geometry.width); - infos[i].geometry.x_offset = HINT_X (infos[i].geometry.x_offset); - infos[i].geometry.y_offset = HINT_Y (infos[i].geometry.y_offset); - } + for (i = 0; i < num_glyphs; i++) + { + infos[i].geometry.width = HINT_X (infos[i].geometry.width); + infos[i].geometry.x_offset = HINT_X (infos[i].geometry.x_offset); + infos[i].geometry.y_offset = HINT_Y (infos[i].geometry.y_offset); + } #undef HINT_Y #undef HINT_X #undef HINT - } - } + } + } + } release_buffer (hb_buffer, free_buffer); } -- GitLab From cbfedef9ea5862959c15eb60ceb72b8157fa24b1 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 24 May 2019 16:25:03 +0000 Subject: [PATCH 4/8] fc: Use harfbuzz for glyph lookup We don't need our own caching here. --- pango/pangofc-font.c | 46 ++++---------------------------------------- 1 file changed, 4 insertions(+), 42 deletions(-) diff --git a/pango/pangofc-font.c b/pango/pangofc-font.c index 65b6cb73c..70e68e1f1 100644 --- a/pango/pangofc-font.c +++ b/pango/pangofc-font.c @@ -639,50 +639,12 @@ static guint pango_fc_font_real_get_glyph (PangoFcFont *font, gunichar wc) { - PangoFcFontPrivate *priv = font->priv; - FT_Face face; - FT_UInt index; - - guint idx; - PangoFcCmapCacheEntry *entry; - - - if (G_UNLIKELY (priv->cmap_cache == NULL)) - { - PangoFcFontMap *fontmap = g_weak_ref_get ((GWeakRef *) &font->fontmap); - if (G_UNLIKELY (!fontmap)) - return 0; - - priv->cmap_cache = _pango_fc_font_map_get_cmap_cache (fontmap, font); - - g_object_unref (fontmap); + hb_font_t *hb_font = pango_font_get_hb_font (font); + hb_codepoint_t glyph = PANGO_GET_UNKNOWN_GLYPH (wc); - if (G_UNLIKELY (!priv->cmap_cache)) - return 0; - } - - idx = wc & CMAP_CACHE_MASK; - entry = priv->cmap_cache->entries + idx; - - if (entry->ch != wc) - { - face = PANGO_FC_FONT_LOCK_FACE (font); - if (G_LIKELY (face)) - { - index = FcFreeTypeCharIndex (face, wc); - if (index > (FT_UInt)face->num_glyphs) - index = 0; - - PANGO_FC_FONT_UNLOCK_FACE (font); - } - else - index = 0; - - entry->ch = wc; - entry->glyph = index; - } + hb_font_get_nominal_glyph (hb_font, wc, &glyph); - return entry->glyph; + return glyph; } /** -- GitLab From f6b2e1c18d1c3c58fd58725a01c74fa40b914e2e Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 24 May 2019 16:28:15 +0000 Subject: [PATCH 5/8] fc: Drop the cmap cache This is now unused. --- pango/pangofc-fontmap.c | 51 ----------------------------------------- pango/pangofc-private.h | 23 ------------------- 2 files changed, 74 deletions(-) diff --git a/pango/pangofc-fontmap.c b/pango/pangofc-fontmap.c index 0f80665ef..4695a21d8 100644 --- a/pango/pangofc-fontmap.c +++ b/pango/pangofc-fontmap.c @@ -164,7 +164,6 @@ struct _PangoFcFontFaceData /* Data */ FcPattern *pattern; /* Referenced pattern that owns filename */ PangoCoverage *coverage; - PangoFcCmapCache *cmap_cache; hb_face_t *hb_face; }; @@ -297,9 +296,6 @@ pango_fc_font_face_data_free (PangoFcFontFaceData *data) if (data->coverage) pango_coverage_unref (data->coverage); - if (data->cmap_cache) - _pango_fc_cmap_cache_unref (data->cmap_cache); - hb_face_destroy (data->hb_face); g_slice_free (PangoFcFontFaceData, data); @@ -1937,53 +1933,6 @@ pango_fc_font_map_get_font_face_data (PangoFcFontMap *fcfontmap, return data; } -static PangoFcCmapCache * -_pango_fc_cmap_cache_ref (PangoFcCmapCache *cmap_cache) -{ - g_atomic_int_inc ((int *) &cmap_cache->ref_count); - - return cmap_cache; -} - -void -_pango_fc_cmap_cache_unref (PangoFcCmapCache *cmap_cache) -{ - g_return_if_fail (cmap_cache->ref_count > 0); - - if (g_atomic_int_dec_and_test ((int *) &cmap_cache->ref_count)) - { - g_free (cmap_cache); - } -} - -PangoFcCmapCache * -_pango_fc_font_map_get_cmap_cache (PangoFcFontMap *fcfontmap, - PangoFcFont *fcfont) -{ - PangoFcFontFaceData *data; - - if (G_UNLIKELY (fcfontmap == NULL)) - return NULL; - - if (G_UNLIKELY (!fcfont->font_pattern)) - return NULL; - - data = pango_fc_font_map_get_font_face_data (fcfontmap, fcfont->font_pattern); - if (G_UNLIKELY (!data)) - return NULL; - - if (G_UNLIKELY (data->cmap_cache == NULL)) - { - data->cmap_cache = g_new0 (PangoFcCmapCache, 1); - data->cmap_cache->ref_count = 1; - - /* Make sure all cache entries are invalid initially */ - data->cmap_cache->entries[0].ch = 1; /* char 1 cannot happen in bucket 0 */ - } - - return _pango_fc_cmap_cache_ref (data->cmap_cache); -} - PangoCoverage * _pango_fc_font_map_get_coverage (PangoFcFontMap *fcfontmap, PangoFcFont *fcfont) diff --git a/pango/pangofc-private.h b/pango/pangofc-private.h index ee048719f..f3987044e 100644 --- a/pango/pangofc-private.h +++ b/pango/pangofc-private.h @@ -43,25 +43,6 @@ struct _PangoFcMetricsInfo }; -typedef struct _PangoFcCmapCacheEntry PangoFcCmapCacheEntry; -typedef struct _PangoFcCmapCache PangoFcCmapCache; - -#define CMAP_CACHE_NUM_ENTRIES 256 /* should be power of two */ -#define CMAP_CACHE_MASK (CMAP_CACHE_NUM_ENTRIES - 1) - -struct _PangoFcCmapCacheEntry -{ - gunichar ch; - PangoGlyph glyph; -}; - -struct _PangoFcCmapCache -{ - guint ref_count; - PangoFcCmapCacheEntry entries[CMAP_CACHE_NUM_ENTRIES]; -}; - - #define PANGO_SCALE_26_6 (PANGO_SCALE / (1<<6)) #define PANGO_PIXELS_26_6(d) \ (((d) >= 0) ? \ @@ -79,10 +60,6 @@ PangoCoverage *_pango_fc_font_map_get_coverage (PangoFcFontMap *fcfontmap, PangoFcFont *fcfont); PangoCoverage *_pango_fc_font_map_fc_to_coverage (FcCharSet *charset); -PangoFcCmapCache *_pango_fc_font_map_get_cmap_cache (PangoFcFontMap *fcfontmap, - PangoFcFont *fcfont); -void _pango_fc_cmap_cache_unref (PangoFcCmapCache *cmap_cache); - PangoFcDecoder *_pango_fc_font_get_decoder (PangoFcFont *font); void _pango_fc_font_set_decoder (PangoFcFont *font, PangoFcDecoder *decoder); -- GitLab From 092d865269c263b0969001e3c141926eb0aae919 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 24 May 2019 16:53:28 +0000 Subject: [PATCH 6/8] fc: Replace get_face_metrics with harfbuzz This is incomplete; harfbuzz does not have underline and strikethrough metrics currently. See https://github.com/harfbuzz/harfbuzz/pull/1432 --- pango/pangofc-font.c | 116 ++++--------------------------------------- 1 file changed, 11 insertions(+), 105 deletions(-) diff --git a/pango/pangofc-font.c b/pango/pangofc-font.c index 70e68e1f1..da90c89e7 100644 --- a/pango/pangofc-font.c +++ b/pango/pangofc-font.c @@ -63,7 +63,6 @@ struct _PangoFcFontPrivate { PangoFcDecoder *decoder; PangoFcFontKey *key; - PangoFcCmapCache *cmap_cache; }; static gboolean pango_fc_font_real_has_char (PangoFcFont *font, @@ -171,9 +170,6 @@ pango_fc_font_finalize (GObject *object) if (priv->decoder) _pango_fc_font_set_decoder (fcfont, NULL); - if (priv->cmap_cache) - _pango_fc_cmap_cache_unref (priv->cmap_cache); - G_OBJECT_CLASS (pango_fc_font_parent_class)->finalize (object); } @@ -382,126 +378,37 @@ static void get_face_metrics (PangoFcFont *fcfont, PangoFontMetrics *metrics) { - FT_Face face = PANGO_FC_FONT_LOCK_FACE (fcfont); + hb_font_t *hb_font = pango_font_get_hb_font (PANGO_FONT (font)); + hb_font_extents_t extents; + FcMatrix *fc_matrix; - FT_Matrix ft_matrix; - TT_OS2 *os2; gboolean have_transform = FALSE; - if (G_UNLIKELY (!face)) - { - metrics->descent = 0; - metrics->ascent = PANGO_SCALE * PANGO_UNKNOWN_GLYPH_HEIGHT; - metrics->underline_thickness = PANGO_SCALE; - metrics->underline_position = - PANGO_SCALE; - metrics->strikethrough_thickness = PANGO_SCALE; - metrics->strikethrough_position = PANGO_SCALE * (PANGO_UNKNOWN_GLYPH_HEIGHT/2); - return; - } + hb_font_get_extents_for_direction (hb_font, HB_DIRECTION_LTR, &extents); if (FcPatternGetMatrix (fcfont->font_pattern, FC_MATRIX, 0, &fc_matrix) == FcResultMatch) { - ft_matrix.xx = 0x10000L * fc_matrix->xx; - ft_matrix.yy = 0x10000L * fc_matrix->yy; - ft_matrix.xy = 0x10000L * fc_matrix->xy; - ft_matrix.yx = 0x10000L * fc_matrix->yx; - - have_transform = (ft_matrix.xx != 0x10000 || ft_matrix.xy != 0 || - ft_matrix.yx != 0 || ft_matrix.yy != 0x10000); + have_transform = (fc_matrix->xx != 1 || fc_matrix->xy != 0 || + fc_matrix->yx != 0 || fc_matrix->yy != 1); } if (have_transform) { - FT_Vector vector; - - vector.x = 0; - vector.y = face->size->metrics.descender; - FT_Vector_Transform (&vector, &ft_matrix); - metrics->descent = - PANGO_UNITS_26_6 (vector.y); - - vector.x = 0; - vector.y = face->size->metrics.ascender; - FT_Vector_Transform (&vector, &ft_matrix); - metrics->ascent = PANGO_UNITS_26_6 (vector.y); - } - else if (fcfont->is_hinted || - (face->face_flags & FT_FACE_FLAG_SCALABLE) == 0) - { - metrics->descent = - PANGO_UNITS_26_6 (face->size->metrics.descender); - metrics->ascent = PANGO_UNITS_26_6 (face->size->metrics.ascender); + metrics->descent = extends.descender * fc_matrix->yy; + metrics->ascent = extens.ascender * fc_matrix->yy; } else { - FT_Fixed ascender, descender; - - descender = FT_MulFix (face->descender, face->size->metrics.y_scale); - metrics->descent = - PANGO_UNITS_26_6 (descender); - - ascender = FT_MulFix (face->ascender, face->size->metrics.y_scale); - metrics->ascent = PANGO_UNITS_26_6 (ascender); + metrics->descent = - extents.decender; + metrics->ascent = extents.ascender; } metrics->underline_thickness = 0; metrics->underline_position = 0; - - { - FT_Fixed ft_thickness, ft_position; - - ft_thickness = FT_MulFix (face->underline_thickness, face->size->metrics.y_scale); - metrics->underline_thickness = PANGO_UNITS_26_6 (ft_thickness); - - ft_position = FT_MulFix (face->underline_position, face->size->metrics.y_scale); - metrics->underline_position = PANGO_UNITS_26_6 (ft_position); - } - - if (metrics->underline_thickness == 0 || metrics->underline_position == 0) - { - metrics->underline_thickness = (PANGO_SCALE * face->size->metrics.y_ppem) / 14; - metrics->underline_position = - metrics->underline_thickness; - } - - metrics->strikethrough_thickness = 0; metrics->strikethrough_position = 0; - - os2 = FT_Get_Sfnt_Table (face, ft_sfnt_os2); - if (os2 && os2->version != 0xFFFF) - { - FT_Fixed ft_thickness, ft_position; - - ft_thickness = FT_MulFix (os2->yStrikeoutSize, face->size->metrics.y_scale); - metrics->strikethrough_thickness = PANGO_UNITS_26_6 (ft_thickness); - - ft_position = FT_MulFix (os2->yStrikeoutPosition, face->size->metrics.y_scale); - metrics->strikethrough_position = PANGO_UNITS_26_6 (ft_position); - } - - if (metrics->strikethrough_thickness == 0 || metrics->strikethrough_position == 0) - { - metrics->strikethrough_thickness = metrics->underline_thickness; - metrics->strikethrough_position = (PANGO_SCALE * face->size->metrics.y_ppem) / 4; - } - - - /* If hinting is on for this font, quantize the underline and strikethrough position - * to integer values. - */ - if (fcfont->is_hinted) - { - pango_quantize_line_geometry (&metrics->underline_thickness, - &metrics->underline_position); - pango_quantize_line_geometry (&metrics->strikethrough_thickness, - &metrics->strikethrough_position); - - /* Quantizing may have pushed underline_position to 0. Not good */ - if (metrics->underline_position == 0) - metrics->underline_position = - metrics->underline_thickness; - } - - - PANGO_FC_FONT_UNLOCK_FACE (fcfont); } PangoFontMetrics * @@ -639,7 +546,7 @@ static guint pango_fc_font_real_get_glyph (PangoFcFont *font, gunichar wc) { - hb_font_t *hb_font = pango_font_get_hb_font (font); + hb_font_t *hb_font = pango_font_get_hb_font (PANGO_FONT (font)); hb_codepoint_t glyph = PANGO_GET_UNKNOWN_GLYPH (wc); hb_font_get_nominal_glyph (hb_font, wc, &glyph); @@ -794,7 +701,6 @@ void pango_fc_font_kern_glyphs (PangoFcFont *font, PangoGlyphString *glyphs) { - FT_Face face; FT_Error error; FT_Vector kerning; int i; -- GitLab From f07f28a32dc8688111a469577f12f8e408bdf43f Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 24 May 2019 16:54:34 +0000 Subject: [PATCH 7/8] fc: Make pango_fc_font_kern_glyphs empty This function has long been deprecated; and it is is using freetype. So drop the implementation. --- pango/pangofc-font.c | 54 -------------------------------------------- 1 file changed, 54 deletions(-) diff --git a/pango/pangofc-font.c b/pango/pangofc-font.c index da90c89e7..d0b118046 100644 --- a/pango/pangofc-font.c +++ b/pango/pangofc-font.c @@ -701,60 +701,6 @@ void pango_fc_font_kern_glyphs (PangoFcFont *font, PangoGlyphString *glyphs) { - FT_Error error; - FT_Vector kerning; - int i; - gboolean hinting = font->is_hinted; - gboolean scale = FALSE; - double xscale = 1; - PangoFcFontKey *key; - - g_return_if_fail (PANGO_IS_FC_FONT (font)); - g_return_if_fail (glyphs != NULL); - - face = PANGO_FC_FONT_LOCK_FACE (font); - if (G_UNLIKELY (!face)) - return; - - if (!FT_HAS_KERNING (face)) - { - PANGO_FC_FONT_UNLOCK_FACE (font); - return; - } - - key = _pango_fc_font_get_font_key (font); - if (key) { - const PangoMatrix *matrix = pango_fc_font_key_get_matrix (key); - PangoMatrix identity = PANGO_MATRIX_INIT; - if (G_UNLIKELY (matrix && 0 != memcmp (&identity, matrix, 2 * sizeof (double)))) - { - scale = TRUE; - pango_matrix_get_font_scale_factors (matrix, &xscale, NULL); - if (xscale) xscale = 1 / xscale; - } - } - - for (i = 1; i < glyphs->num_glyphs; ++i) - { - error = FT_Get_Kerning (face, - glyphs->glyphs[i-1].glyph, - glyphs->glyphs[i].glyph, - ft_kerning_default, - &kerning); - - if (error == FT_Err_Ok) { - int adjustment = PANGO_UNITS_26_6 (kerning.x); - - if (hinting) - adjustment = PANGO_UNITS_ROUND (adjustment); - if (G_UNLIKELY (scale)) - adjustment *= xscale; - - glyphs->glyphs[i-1].geometry.width += adjustment; - } - } - - PANGO_FC_FONT_UNLOCK_FACE (font); } /** -- GitLab From 375e3cf46d18f6acefb451142adfd92ef2ac84c1 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Fri, 24 May 2019 17:15:19 +0000 Subject: [PATCH 8/8] fc: Use harfbuzz for glyph extents Harfbuzz has the api, no need to use freetype. --- pango/pangofc-font.c | 99 ++++++++++++++------------------------------ 1 file changed, 32 insertions(+), 67 deletions(-) diff --git a/pango/pangofc-font.c b/pango/pangofc-font.c index d0b118046..12cc4f500 100644 --- a/pango/pangofc-font.c +++ b/pango/pangofc-font.c @@ -378,7 +378,7 @@ static void get_face_metrics (PangoFcFont *fcfont, PangoFontMetrics *metrics) { - hb_font_t *hb_font = pango_font_get_hb_font (PANGO_FONT (font)); + hb_font_t *hb_font = pango_font_get_hb_font (PANGO_FONT (fcfont)); hb_font_extents_t extents; FcMatrix *fc_matrix; @@ -395,12 +395,12 @@ get_face_metrics (PangoFcFont *fcfont, if (have_transform) { - metrics->descent = extends.descender * fc_matrix->yy; - metrics->ascent = extens.ascender * fc_matrix->yy; + metrics->descent = extents.descender * fc_matrix->yy; + metrics->ascent = extents.ascender * fc_matrix->yy; } else { - metrics->descent = - extents.decender; + metrics->descent = - extents.descender; metrics->ascent = extents.ascender; } @@ -765,23 +765,6 @@ _pango_fc_font_set_font_key (PangoFcFont *fcfont, priv->key = key; } -static FT_Glyph_Metrics * -get_per_char (FT_Face face, - FT_Int32 load_flags, - PangoGlyph glyph) -{ - FT_Error error; - FT_Glyph_Metrics *result; - - error = FT_Load_Glyph (face, glyph, load_flags); - if (error == FT_Err_Ok) - result = &face->glyph->metrics; - else - result = NULL; - - return result; -} - /** * pango_fc_font_get_raw_extents: * @fcfont: a #PangoFcFont @@ -811,76 +794,58 @@ pango_fc_font_get_raw_extents (PangoFcFont *fcfont, PangoRectangle *ink_rect, PangoRectangle *logical_rect) { - FT_Glyph_Metrics *gm; - FT_Face face; - g_return_if_fail (PANGO_IS_FC_FONT (fcfont)); - face = PANGO_FC_FONT_LOCK_FACE (fcfont); - if (G_UNLIKELY (!face)) - { - /* Get generic unknown-glyph extents. */ - pango_font_get_glyph_extents (NULL, glyph, ink_rect, logical_rect); - return; - } - if (glyph == PANGO_GLYPH_EMPTY) - gm = NULL; - else - gm = get_per_char (face, load_flags, glyph); - - if (gm) { if (ink_rect) { - ink_rect->x = PANGO_UNITS_26_6 (gm->horiBearingX); - ink_rect->width = PANGO_UNITS_26_6 (gm->width); - ink_rect->y = -PANGO_UNITS_26_6 (gm->horiBearingY); - ink_rect->height = PANGO_UNITS_26_6 (gm->height); + ink_rect->x = 0; + ink_rect->width = 0; + ink_rect->y = 0; + ink_rect->height = 0; } if (logical_rect) { logical_rect->x = 0; - logical_rect->width = PANGO_UNITS_26_6 (gm->horiAdvance); - if (fcfont->is_hinted || - (face->face_flags & FT_FACE_FLAG_SCALABLE) == 0) - { - logical_rect->y = - PANGO_UNITS_26_6 (face->size->metrics.ascender); - logical_rect->height = PANGO_UNITS_26_6 (face->size->metrics.ascender - face->size->metrics.descender); - } - else - { - FT_Fixed ascender, descender; - - ascender = FT_MulFix (face->ascender, face->size->metrics.y_scale); - descender = FT_MulFix (face->descender, face->size->metrics.y_scale); - - logical_rect->y = - PANGO_UNITS_26_6 (ascender); - logical_rect->height = PANGO_UNITS_26_6 (ascender - descender); - } + logical_rect->width = 0; + logical_rect->y = 0; + logical_rect->height = 0; } } else { + hb_font_t *hb_font = pango_font_get_hb_font (PANGO_FONT (fcfont)); + hb_glyph_extents_t extents; + hb_font_extents_t font_extents; + + hb_font_get_glyph_extents (hb_font, glyph, &extents); + hb_font_get_extents_for_direction (hb_font, HB_DIRECTION_LTR, &font_extents); + if (ink_rect) { - ink_rect->x = 0; - ink_rect->width = 0; - ink_rect->y = 0; - ink_rect->height = 0; + ink_rect->x = extents.x_bearing; + ink_rect->width = extents.width; + ink_rect->y = -extents.y_bearing; + ink_rect->height = extents.height; } if (logical_rect) { + hb_position_t x, y; + + hb_font_get_glyph_advance_for_direction (hb_font, + glyph, + HB_DIRECTION_LTR, + &x, &y); + logical_rect->x = 0; - logical_rect->width = 0; - logical_rect->y = 0; - logical_rect->height = 0; + logical_rect->width = x; + logical_rect->y = - font_extents.ascender; + logical_rect->height = font_extents.ascender - font_extents.descender; } } - - PANGO_FC_FONT_UNLOCK_FACE (fcfont); } extern gpointer get_gravity_class (void); -- GitLab