diff --git a/pango/fonts.c b/pango/fonts.c index b46aef027525aa553501ccedf20fdc0ff425833b..ebf1b3d1a6ad1480d939ef95e3aeb8596522b21a 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 1b85c386ab8b23f71b856535f8c5e5fd8783bc75..461ece878753b9a5cf7d75d1c6a20ee8d27189f3 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 5a48e78222f4c5fb3ee9f6fa5631d20900906f42..12cc4f500ae6b0d2e967a4a4d635db3dff1f6f48 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 @@ -62,7 +63,6 @@ struct _PangoFcFontPrivate { PangoFcDecoder *decoder; PangoFcFontKey *key; - PangoFcCmapCache *cmap_cache; }; static gboolean pango_fc_font_real_has_char (PangoFcFont *font, @@ -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", @@ -169,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); } @@ -380,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 (fcfont)); + 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 = extents.descender * fc_matrix->yy; + metrics->ascent = extents.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.descender; + 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 * @@ -637,50 +546,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 (PANGO_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; + hb_font_get_nominal_glyph (hb_font, wc, &glyph); - PANGO_FC_FONT_UNLOCK_FACE (font); - } - else - index = 0; - - entry->ch = wc; - entry->glyph = index; - } - - return entry->glyph; + return glyph; } /** @@ -830,61 +701,6 @@ void pango_fc_font_kern_glyphs (PangoFcFont *font, PangoGlyphString *glyphs) { - FT_Face face; - 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); } /** @@ -949,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 @@ -995,75 +794,187 @@ 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; } } +} + +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; + } - PANGO_FC_FONT_UNLOCK_FACE (fcfont); + *hb_variations = var; + *n_variations = n; } +static hb_font_t * +pango_fc_font_create_hb_font (PangoFont *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; + + 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; +} diff --git a/pango/pangofc-fontmap.c b/pango/pangofc-fontmap.c index 7d75f6f9c0f96c21ba74a72f74b80fea62f87f90..4695a21d81a581d6d76a012fc36d81105913a693 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: @@ -163,7 +164,8 @@ struct _PangoFcFontFaceData /* Data */ FcPattern *pattern; /* Referenced pattern that owns filename */ PangoCoverage *coverage; - PangoFcCmapCache *cmap_cache; + + hb_face_t *hb_face; }; struct _PangoFcFace @@ -259,7 +261,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 */ @@ -292,8 +296,7 @@ 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); } @@ -1930,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) @@ -2689,3 +2645,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 bff288b68e87386a5557360a9c5e469774a81dac..afa402df9989a680a5c3d3595409b01d4e857afd 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: * diff --git a/pango/pangofc-private.h b/pango/pangofc-private.h index ee048719fd9d15f86324cd40b54889326fa7dada..f3987044e1e42f8a8b235dd354f4f59453f55fc2 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); diff --git a/pango/pangofc-shape.c b/pango/pangofc-shape.c index 53269d73f753e85509ae0184bca9a3d62a09cdd6..6313208a726b950b7d96467b0a9a8c4deca160aa 100644 --- a/pango/pangofc-shape.c +++ b/pango/pangofc-shape.c @@ -68,249 +68,34 @@ 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) +pango_font_get_features (PangoFont *font, + hb_feature_t *features, + unsigned int len, + unsigned int *num_features) { - 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) + if (PANGO_IS_FC_FONT (font)) { - char *end = strchr (p, ','); - if (hb_variation_from_string (p, end ? end - p: -1, &var[n])) - n++; - p = end ? end + 1 : NULL; + /* 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)++; + } + } } - - *hb_variations = var; - *n_variations = n; } - void _pango_fc_shape (PangoFont *font, const char *item_text, @@ -320,11 +105,6 @@ _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 +116,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); @@ -415,57 +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. */ - 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); @@ -489,7 +200,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. */ @@ -507,41 +218,61 @@ _pango_fc_shape (PangoFont *font, hb_position++; } - if (fc_font->is_hinted) - { - if (context.x_scale == 1.0 && context.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 (context.vertical) - { - /* 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), context.x_scale, x_scale_inv) -#define HINT_Y(value) HINT ((value), context.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); - } +#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); + } #undef HINT_Y #undef HINT_X #undef HINT - } - } + } + } + } release_buffer (hb_buffer, free_buffer); - hb_font_destroy (hb_font); - hb_face_destroy (hb_face); - pango_fc_font_unlock_face (fc_font); }