[pango/simple-fontmap: 8/9] Implement a new fontconfig fontmap




commit 28704c750876ba4a96797c5d4d74bbe180f8f1e5
Author: Matthias Clasen <mclasen redhat com>
Date:   Mon Nov 1 22:04:14 2021 -0400

    Implement a new fontconfig fontmap
    
    This is a new fontmap implementation that
    is populated from fontconfig data, but uses
    PangoSimpleFamily, PangoHbFace and PangoHbFont.
    
    After the initial population of the fontmap,
    no fontconfig data of apis are used.

 pango/meson.build        |   1 +
 pango/pangofc-fontmap2.c | 913 +++++++++++++++++++++++++++++++++++++++++++++++
 pango/pangofc-fontmap2.h |  48 +++
 3 files changed, 962 insertions(+)
---
diff --git a/pango/meson.build b/pango/meson.build
index b8cad795..105a5c6e 100644
--- a/pango/meson.build
+++ b/pango/meson.build
@@ -208,6 +208,7 @@ if build_pangoft2
   pangofc_public_sources = [
     'pangofc-font.c',
     'pangofc-fontmap.c',
+    'pangofc-fontmap2.c',
     'pangofc-decoder.c',
     'pango-trace.c',
   ]
diff --git a/pango/pangofc-fontmap2.c b/pango/pangofc-fontmap2.c
new file mode 100644
index 00000000..cb354bee
--- /dev/null
+++ b/pango/pangofc-fontmap2.c
@@ -0,0 +1,913 @@
+/* Pango
+ *
+ * Copyright (C) 2021 Red Hat, Inc.
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "config.h"
+#include <math.h>
+
+#include <gio/gio.h>
+
+#include "pangofc-fontmap2.h"
+#include "pango-simple-family-private.h"
+#include "pango-hbface-private.h"
+#include "pango-hbfont-private.h"
+#include "pango-context.h"
+#include "pango-impl-utils.h"
+
+#include <fontconfig/fontconfig.h>
+#include <hb-ot.h>
+
+struct _PangoFcFontMap2
+{
+  PangoFontMap parent_instance;
+
+  FcConfig *config;
+
+  GPtrArray *families;
+
+  double dpi;
+};
+
+struct _PangoFcFontMap2Class
+{
+  PangoFontMapClass parent_class;
+};
+
+static void ensure_families (PangoFcFontMap2 *self);
+
+/* {{{ GListModel implementation */
+
+static GType
+pango_fc_font_map2_get_item_type (GListModel *list)
+{
+  return PANGO_TYPE_FONT_FAMILY;
+}
+
+static guint
+pango_fc_font_map2_get_n_items (GListModel *list)
+{
+  PangoFcFontMap2 *self = PANGO_FC_FONT_MAP2 (list);
+
+  ensure_families (self);
+
+  return self->families->len;
+}
+
+static gpointer
+pango_fc_font_map2_get_item (GListModel *list,
+                             guint       position)
+{
+  PangoFcFontMap2 *self = PANGO_FC_FONT_MAP2 (list);
+
+  ensure_families (self);
+
+  if (position < self->families->len)
+    return g_object_ref (g_ptr_array_index (self->families, position));
+
+  return NULL;
+}
+
+static void
+pango_fc_font_map2_list_model_init (GListModelInterface *iface)
+{
+  iface->get_item_type = pango_fc_font_map2_get_item_type;
+  iface->get_n_items = pango_fc_font_map2_get_n_items;
+  iface->get_item = pango_fc_font_map2_get_item;
+}
+
+/* }}} */
+/* {{{ Utilities */
+
+#define DONTCARE 2
+
+static PangoSimpleFamily *
+find_family (PangoFcFontMap2 *self,
+             const char      *family_name,
+             gboolean         variable)
+{
+  for (int i = 0; i < self->families->len; i++)
+    {
+      PangoSimpleFamily *family = g_ptr_array_index (self->families, i);
+
+      if (g_ascii_strcasecmp (family->name, family_name) == 0 &&
+          (variable == DONTCARE || variable == family->variable))
+        return family;
+    }
+
+  return NULL;
+}
+
+static PangoHbFace *
+find_face (PangoSimpleFamily    *family,
+           PangoFontDescription *desc)
+{
+  PangoHbFace *face = NULL;
+  PangoFontDescription *best = NULL;
+
+  for (int i = 0; i < family->faces->len; i++)
+    {
+      PangoHbFace *face2 = g_ptr_array_index (family->faces, i);
+
+      if (pango_font_description_better_match (desc, best, face2->description))
+        {
+          face = face2;
+          best = face2->description;
+        }
+    }
+
+  return face;
+}
+
+/* }}} */
+/* {{{ Fontconfig utilities */
+
+static gboolean
+pango_fc_is_supported_font_format (FcPattern* pattern)
+{
+  FcResult res;
+  const char *fontformat;
+  const char *file;
+
+  /* Harfbuzz loads woff fonts, but we don't get any glyphs */
+  res = FcPatternGetString (pattern, FC_FILE, 0, (FcChar8 **)(void*)&file);
+  if (res == FcResultMatch &&
+      (g_str_has_suffix (file, ".woff") || g_str_has_suffix (file, ".woff2")))
+    return FALSE;
+
+  res = FcPatternGetString (pattern, FC_FONTFORMAT, 0, (FcChar8 **)(void*)&fontformat);
+  if (res != FcResultMatch)
+    return FALSE;
+
+  /* Harfbuzz supports only SFNT fonts */
+
+  /* FIXME: "CFF" is used for both CFF in OpenType and bare CFF files, but
+   * HarfBuzz does not support the later and FontConfig does not seem
+   * to have a way to tell them apart.
+   */
+  if (g_ascii_strcasecmp (fontformat, "TrueType") == 0 ||
+      g_ascii_strcasecmp (fontformat, "CFF") == 0)
+    return TRUE;
+
+  return FALSE;
+}
+
+static PangoStyle
+pango_fc_convert_slant_to_pango (int fc_style)
+{
+  switch (fc_style)
+    {
+    case FC_SLANT_ROMAN:
+      return PANGO_STYLE_NORMAL;
+    case FC_SLANT_ITALIC:
+      return PANGO_STYLE_ITALIC;
+    case FC_SLANT_OBLIQUE:
+      return PANGO_STYLE_OBLIQUE;
+    default:
+      return PANGO_STYLE_NORMAL;
+    }
+}
+
+static PangoStretch
+pango_fc_convert_width_to_pango (int fc_stretch)
+{
+  switch (fc_stretch)
+    {
+    case FC_WIDTH_NORMAL:
+      return PANGO_STRETCH_NORMAL;
+    case FC_WIDTH_ULTRACONDENSED:
+      return PANGO_STRETCH_ULTRA_CONDENSED;
+    case FC_WIDTH_EXTRACONDENSED:
+      return PANGO_STRETCH_EXTRA_CONDENSED;
+    case FC_WIDTH_CONDENSED:
+      return PANGO_STRETCH_CONDENSED;
+    case FC_WIDTH_SEMICONDENSED:
+      return PANGO_STRETCH_SEMI_CONDENSED;
+    case FC_WIDTH_SEMIEXPANDED:
+      return PANGO_STRETCH_SEMI_EXPANDED;
+    case FC_WIDTH_EXPANDED:
+      return PANGO_STRETCH_EXPANDED;
+    case FC_WIDTH_EXTRAEXPANDED:
+      return PANGO_STRETCH_EXTRA_EXPANDED;
+    case FC_WIDTH_ULTRAEXPANDED:
+      return PANGO_STRETCH_ULTRA_EXPANDED;
+    default:
+      return PANGO_STRETCH_NORMAL;
+    }
+}
+
+static PangoWeight
+pango_fc_convert_weight_to_pango (double fc_weight)
+{
+  return FcWeightToOpenTypeDouble (fc_weight);
+}
+
+#define PANGO_FC_GRAVITY "gravity"
+
+static PangoFontDescription *
+pango_font_description_from_pattern (FcPattern *pattern,
+                                     gboolean   include_size)
+{
+  PangoFontDescription *desc;
+  PangoStyle style;
+  PangoWeight weight;
+  PangoStretch stretch;
+  double size;
+  PangoGravity gravity;
+  PangoVariant variant;
+  gboolean all_caps;
+
+  FcChar8 *s;
+  int i;
+  double d;
+  FcResult res;
+
+  desc = pango_font_description_new ();
+
+  res = FcPatternGetString (pattern, FC_FAMILY, 0, (FcChar8 **) &s);
+  g_assert (res == FcResultMatch);
+
+  pango_font_description_set_family (desc, (gchar *)s);
+
+  if (FcPatternGetInteger (pattern, FC_SLANT, 0, &i) == FcResultMatch)
+    style = pango_fc_convert_slant_to_pango (i);
+  else
+    style = PANGO_STYLE_NORMAL;
+
+  pango_font_description_set_style (desc, style);
+
+  if (FcPatternGetDouble (pattern, FC_WEIGHT, 0, &d) == FcResultMatch)
+    weight = pango_fc_convert_weight_to_pango (d);
+  else
+    weight = PANGO_WEIGHT_NORMAL;
+
+  pango_font_description_set_weight (desc, weight);
+
+  if (FcPatternGetInteger (pattern, FC_WIDTH, 0, &i) == FcResultMatch)
+    stretch = pango_fc_convert_width_to_pango (i);
+  else
+    stretch = PANGO_STRETCH_NORMAL;
+
+  pango_font_description_set_stretch (desc, stretch);
+
+  variant = PANGO_VARIANT_NORMAL;
+  all_caps = FALSE;
+
+  for (int i = 0; i < 32; i++)
+    {
+      const char *s;
+
+      if (FcPatternGetString (pattern, FC_FONT_FEATURES, i, (FcChar8 **)&s) == FcResultMatch)
+        {
+          if (strcmp (s, "smcp=1") == 0)
+            {
+              if (all_caps)
+                variant = PANGO_VARIANT_ALL_SMALL_CAPS;
+              else
+                variant = PANGO_VARIANT_SMALL_CAPS;
+            }
+          else if (strcmp (s, "c2sc=1") == 0)
+            {
+              if (variant == PANGO_VARIANT_SMALL_CAPS)
+                variant = PANGO_VARIANT_ALL_SMALL_CAPS;
+              else
+                all_caps = TRUE;
+            }
+          else if (strcmp (s, "pcap=1") == 0)
+            {
+              if (all_caps)
+                variant = PANGO_VARIANT_ALL_PETITE_CAPS;
+              else
+                variant = PANGO_VARIANT_PETITE_CAPS;
+            }
+          else if (strcmp (s, "c2pc=1") == 0)
+            {
+              if (variant == PANGO_VARIANT_PETITE_CAPS)
+                variant = PANGO_VARIANT_ALL_PETITE_CAPS;
+              else
+                all_caps = TRUE;
+            }
+          else if (strcmp (s, "unic=1") == 0)
+            {
+              variant = PANGO_VARIANT_UNICASE;
+            }
+          else if (strcmp (s, "titl=1") == 0)
+            {
+              variant = PANGO_VARIANT_TITLE_CAPS;
+            }
+        }
+      else
+        break;
+    }
+
+  pango_font_description_set_variant (desc, variant);
+
+  if (include_size && FcPatternGetDouble (pattern, FC_SIZE, 0, &size) == FcResultMatch)
+    {
+      FcMatrix *fc_matrix;
+      double scale_factor = 1;
+      volatile double scaled_size;
+
+      if (FcPatternGetMatrix (pattern, FC_MATRIX, 0, &fc_matrix) == FcResultMatch)
+        {
+          PangoMatrix mat = PANGO_MATRIX_INIT;
+
+          mat.xx = fc_matrix->xx;
+          mat.xy = fc_matrix->xy;
+          mat.yx = fc_matrix->yx;
+          mat.yy = fc_matrix->yy;
+
+          scale_factor = pango_matrix_get_font_scale_factor (&mat);
+        }
+
+      /* We need to use a local variable to ensure that the compiler won't
+       * implicitly cast it to integer while the result is kept in registers,
+       * leading to a wrong approximation in i386 (with 387 FPU)
+       */
+      scaled_size = scale_factor * size * PANGO_SCALE;
+      pango_font_description_set_size (desc, scaled_size);
+    }
+
+  /* gravity is a bit different.  we don't want to set it if it was not set on the pattern */
+  if (FcPatternGetString (pattern, PANGO_FC_GRAVITY, 0, (FcChar8 **)&s) == FcResultMatch)
+    {
+      GEnumClass *class = g_type_class_ref (PANGO_TYPE_GRAVITY);
+      GEnumValue *value = g_enum_get_value_by_nick (class, (char *)s);
+      gravity = value->value;
+      pango_font_description_set_gravity (desc, gravity);
+      g_type_class_unref (class);
+    }
+
+  if (FcPatternGetString (pattern, FC_FONT_VARIATIONS, 0, (FcChar8 **)&s) == FcResultMatch)
+    {
+      if (s && *s)
+        pango_font_description_set_variations (desc, (char *)s);
+    }
+
+  return desc;
+}
+
+static const char *
+style_name_from_pattern (FcPattern *pattern)
+{
+  const char *font_style;
+  int weight, slant;
+
+  if (FcPatternGetString (pattern, FC_STYLE, 0, (FcChar8 **)(void*)&font_style) == FcResultMatch)
+    return font_style;
+
+  if (FcPatternGetInteger(pattern, FC_WEIGHT, 0, &weight) != FcResultMatch)
+    weight = FC_WEIGHT_MEDIUM;
+
+  if (FcPatternGetInteger(pattern, FC_SLANT, 0, &slant) != FcResultMatch)
+    slant = FC_SLANT_ROMAN;
+
+  if (weight <= FC_WEIGHT_MEDIUM)
+    {
+      if (slant == FC_SLANT_ROMAN)
+        return "Regular";
+      else
+        return "Italic";
+    }
+  else
+    {
+      if (slant == FC_SLANT_ROMAN)
+        return "Bold";
+      else
+        return "Bold Italic";
+    }
+
+  return "Normal";
+}
+
+static gboolean
+get_font_matrix_from_pattern (FcPattern   *pattern,
+                              PangoMatrix *matrix)
+{
+  FcMatrix fc_matrix, *fc_matrix_val;
+  int i;
+
+  FcMatrixInit (&fc_matrix);
+  for (i = 0; FcPatternGetMatrix (pattern, FC_MATRIX, i, &fc_matrix_val) == FcResultMatch; i++)
+    FcMatrixMultiply (&fc_matrix, &fc_matrix, fc_matrix_val);
+
+  if (i > 0)
+    {
+      matrix->xx = fc_matrix.xx;
+      matrix->yx = fc_matrix.yx;
+      matrix->xy = - fc_matrix.xy;
+      matrix->yy = - fc_matrix.yy;
+
+      return TRUE;
+    }
+
+  return FALSE;
+}
+
+static PangoLanguage **
+get_languages_from_pattern (FcPattern *pattern,
+                            gsize     *n_languages)
+{
+  GPtrArray *langs;
+  FcLangSet *langset;
+  FcStrSet *strset;
+  FcStrList *list;
+  FcChar8 *s;
+
+  if (FcPatternGetLangSet (pattern, FC_LANG, 0, &langset) != FcResultMatch)
+    {
+      *n_languages = 0;
+      return NULL;
+    }
+
+  langs = g_ptr_array_new ();
+
+  strset = FcLangSetGetLangs (langset);
+  list = FcStrListCreate (strset);
+
+  FcStrListFirst (list);
+  while ((s = FcStrListNext (list)))
+    {
+      PangoLanguage *l = pango_language_from_string ((const char *)s);
+      g_ptr_array_add (langs, l);
+    }
+
+  FcStrListDone (list);
+  FcStrSetDestroy (strset);
+
+  g_ptr_array_add (langs, NULL);
+
+  *n_languages = langs->len;
+
+  return (PangoLanguage **) g_ptr_array_free (langs, FALSE);
+}
+
+/* }}} */
+/* {{{ PangoFontMap implementation */
+
+G_DEFINE_TYPE_WITH_CODE (PangoFcFontMap2, pango_fc_font_map2, PANGO_TYPE_FONT_MAP,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, pango_fc_font_map2_list_model_init))
+
+static void
+pango_fc_font_map2_init (PangoFcFontMap2 *self)
+{
+  self->families = g_ptr_array_new_with_free_func (g_object_unref);
+  self->dpi = 96.;
+}
+
+static void
+pango_fc_font_map2_finalize (GObject *object)
+{
+  PangoFcFontMap2 *self = PANGO_FC_FONT_MAP2 (object);
+
+  g_ptr_array_unref (self->families);
+  if (self->config)
+    FcConfigDestroy (self->config);
+
+  G_OBJECT_CLASS (pango_fc_font_map2_parent_class)->finalize (object);
+}
+
+static void
+ensure_families (PangoFcFontMap2 *self)
+{
+  if (self->families->len > 0)
+    return;
+
+  FcObjectSet *os = FcObjectSetBuild (FC_FAMILY,
+                                      FC_SPACING,
+                                      FC_STYLE,
+                                      FC_WEIGHT,
+                                      FC_WIDTH,
+                                      FC_SLANT,
+                                      FC_VARIABLE,
+                                      FC_FONTFORMAT,
+                                      FC_FILE,
+                                      FC_INDEX,
+                                      NULL);
+  FcPattern *pat;
+  FcFontSet *fontset;
+  int k;
+
+  pat = FcPatternCreate ();
+  fontset = FcFontList (self->config, pat, os);
+  FcPatternDestroy (pat);
+
+  for (k = 0; k < fontset->nfont; k++)
+    {
+      const char *family_name, *file;
+      int index;
+      int instance_id;
+      PangoSimpleFamily *family;
+      PangoFontDescription *desc;
+      const char *name;
+      PangoHbFace *face;
+      int spacing;
+      gboolean monospace;
+      gboolean variable;
+      PangoMatrix font_matrix;
+      PangoLanguage **languages;
+      gsize n_languages;
+
+      if (!pango_fc_is_supported_font_format (fontset->fonts[k]))
+        continue;
+
+      if (FcPatternGetString (fontset->fonts[k], FC_FAMILY, 0, (FcChar8 **)(void*)&family_name) != 
FcResultMatch)
+        continue;
+      if (FcPatternGetString (fontset->fonts[k], FC_FILE, 0, (FcChar8 **)(void*)&file) != FcResultMatch)
+        continue;
+      if (FcPatternGetInteger (fontset->fonts[k], FC_INDEX, 0, &index) != FcResultMatch)
+        index = 0;
+
+      instance_id = (index >> 16) - 1;
+      index = index & 0xffff;
+
+      if (FcPatternGetInteger (fontset->fonts[k], FC_SPACING, 0, &spacing) != FcResultMatch)
+        spacing = FC_PROPORTIONAL;
+      monospace = spacing == FC_MONO;
+
+      name = style_name_from_pattern (fontset->fonts[k]);
+      desc = pango_font_description_from_pattern (fontset->fonts[k], FALSE);
+
+      face = pango_hb_face_new_from_file (desc, name, file, index, instance_id);
+
+      variable = pango_hb_face_is_variable (face);
+      monospace = pango_hb_face_is_monospace (face);
+
+      family = find_family (self, family_name, variable);
+      if (!family)
+        {
+          family = pango_simple_family_new (PANGO_FONT_MAP (self), family_name, monospace, variable);
+          g_ptr_array_add (self->families, family);
+        }
+
+      if (get_font_matrix_from_pattern (fontset->fonts[k], &font_matrix))
+        pango_hb_face_set_matrix (face, &font_matrix);
+
+      languages = get_languages_from_pattern (fontset->fonts[k], &n_languages);
+      pango_hb_face_set_languages (face, languages, n_languages);
+      g_free (languages);
+
+      pango_simple_family_add_face (family, PANGO_FONT_FACE (face));
+
+      g_object_unref (face);
+      pango_font_description_free (desc);
+    }
+  FcFontSetDestroy (fontset);
+
+  /* Add alias families */
+  const char *alias_names[] = {
+    "monospace", "sans", "serif", "cursive", "fantasy" "system-ui"
+  };
+
+  for (int i = 0; i < G_N_ELEMENTS (alias_names); i++)
+    {
+      FcPattern *pattern;
+      FcPattern *ret;
+      FcResult res;
+      PangoSimpleFamily *alias_family;
+      const char *family_name;
+
+      alias_family = find_family (self, alias_names[i], FALSE);
+      if (alias_family)
+        continue;
+
+      pattern = FcPatternCreate ();
+      FcPatternAddString (pattern, FC_FAMILY, (FcChar8 *) alias_names[i]);
+
+      FcConfigSubstitute (self->config, pattern, FcMatchPattern);
+      FcDefaultSubstitute (pattern);
+
+      ret = FcFontMatch (self->config, pattern, &res);
+      if (ret && FcPatternGetString (ret, FC_FAMILY, 0, (FcChar8 **)(void*)&family_name) == FcResultMatch)
+       {
+         PangoSimpleFamily *family;
+
+         family = find_family (self, family_name, DONTCARE);
+         if (!family)
+           {
+             g_warning ("Ignoring alias family %s", alias_names[i]);
+             continue;
+           }
+
+          alias_family = pango_simple_family_new (PANGO_FONT_MAP (self),
+                                                  alias_names[i],
+                                                  family->monospace,
+                                                  FALSE);
+          g_ptr_array_add (self->families, alias_family);
+
+          for (int j = 0; j < g_list_model_get_n_items (G_LIST_MODEL (family)); j++)
+            {
+              PangoFontFace *face;
+              PangoFontFace *alias_face;
+              char *fullname;
+
+              face = g_list_model_get_item (G_LIST_MODEL (family), j);
+              fullname = g_strdup_printf ("%s %s", alias_names[i], pango_font_face_get_face_name (face));
+              alias_face = PANGO_FONT_FACE (pango_hb_face_new_alias (PANGO_HB_FACE (face), fullname));
+              pango_simple_family_add_face (alias_family, alias_face);
+              g_object_unref (alias_face);
+              g_free (fullname);
+              g_object_unref (face);
+            }
+       }
+      FcPatternDestroy (ret);
+      FcPatternDestroy (pattern);
+    }
+
+  FcObjectSetDestroy (os);
+
+  /* Synthesize missing Italics */
+  for (int i = 0; i < self->families->len; i++)
+    {
+      PangoSimpleFamily *family = g_ptr_array_index (self->families, i);
+      PangoHbFace *regular_face = NULL;
+      PangoHbFace *bold_face = NULL;
+      int regular_weight = 0;
+      int bold_weight = 1000;
+      gboolean has_italic = FALSE;
+      gboolean has_bold_italic = FALSE;
+
+      for (int j = 0; j < g_list_model_get_n_items (G_LIST_MODEL (family)); j++)
+        {
+          PangoFontFace *face = g_list_model_get_item (G_LIST_MODEL (family), j);
+          PangoFontDescription *desc;
+          PangoWeight weight;
+          PangoStyle style;
+
+          desc = pango_font_face_describe (face);
+          weight = pango_font_description_get_weight (desc);
+          style = pango_font_description_get_style (desc);
+          pango_font_description_free (desc);
+
+          if (style == PANGO_STYLE_NORMAL)
+            {
+              if (abs (weight - PANGO_WEIGHT_NORMAL) < abs (regular_weight - PANGO_WEIGHT_NORMAL))
+                {
+                  regular_weight = weight;
+                  regular_face = PANGO_HB_FACE (face);
+                }
+              if (abs (weight - PANGO_WEIGHT_BOLD) < abs (bold_weight - PANGO_WEIGHT_BOLD))
+                {
+                  bold_weight = weight;
+                  bold_face = PANGO_HB_FACE (face);
+                }
+            }
+          else
+            {
+              if (weight < PANGO_WEIGHT_SEMIBOLD)
+                has_italic = TRUE;
+              else
+                has_bold_italic = TRUE;
+            }
+
+          g_object_unref (face);
+        }
+
+      if (regular_face && !has_italic)
+        {
+          PangoHbFace *face = pango_hb_face_new_transformed (regular_face,
+                                                             "Italic",
+                                                             &(PangoMatrix) { 1, 0.2, 0, 1, 0, 0 });
+          pango_simple_family_add_face (family, PANGO_FONT_FACE (face));
+          g_object_unref (face);
+        }
+
+      if (bold_face && !has_bold_italic)
+        {
+          PangoHbFace *face = pango_hb_face_new_transformed (bold_face,
+                                                             "Bold Italic",
+                                                             &(PangoMatrix) { 1, 0.2, 0, 1, 0, 0 });
+          pango_simple_family_add_face (family, PANGO_FONT_FACE (face));
+          g_object_unref (face);
+        }
+    }
+}
+
+static PangoFont *
+pango_fc_font_map2_load_font (PangoFontMap               *map,
+                              PangoContext               *context,
+                              const PangoFontDescription *desc)
+{
+  PangoFcFontMap2 *self = PANGO_FC_FONT_MAP2 (map);
+  int size;
+  const char *variations;
+  PangoGravity gravity;
+  const PangoMatrix *matrix;
+  const char *family_name;
+  char **families;
+  PangoFontDescription *copy;
+  PangoSimpleFamily *family;
+  PangoHbFace *face;
+
+  if (self->families->len == 0)
+    return NULL;
+
+  if (pango_font_description_get_size_is_absolute (desc))
+    size = pango_font_description_get_size (desc);
+  else
+    size = pango_font_description_get_size (desc) * self->dpi / 72.;
+
+  variations = pango_font_description_get_variations (desc);
+
+  gravity = pango_font_description_get_gravity (desc);
+  matrix = pango_context_get_matrix (context);
+
+  family_name = pango_font_description_get_family (desc);
+  families = g_strsplit (family_name, ",", 0);
+
+  /* Unset gravity since PangoHbFace has no gravity */
+  copy = pango_font_description_copy_static (desc);
+  pango_font_description_unset_fields (copy, PANGO_FONT_MASK_GRAVITY);
+
+  family = NULL;
+  for (int i = 0; families[i]; i++)
+    {
+      family = find_family (self, families[i], variations ? TRUE : DONTCARE);
+      if (!family && variations)
+        family = find_family (self, families[i], DONTCARE);
+      if (family)
+        break;
+    }
+
+  if (!family)
+    family = g_ptr_array_index (self->families, 0);
+
+  face = find_face (family, copy);
+
+  if (!face)
+    {
+      char *s = pango_font_description_to_string (desc);
+      g_warning ("No match for pattern '%s', falling back to default face\n", s);
+      g_free (s);
+
+      face = g_ptr_array_index (family->faces, 0);
+    }
+
+  g_strfreev (families);
+
+  pango_font_description_free (copy);
+
+  return PANGO_FONT (pango_hb_font_new (face, size, variations, gravity, matrix));
+}
+
+/* Add one font for each family we find */
+static PangoFontset *
+pango_fc_font_map2_load_fontset (PangoFontMap               *map,
+                                 PangoContext               *context,
+                                 const PangoFontDescription *description,
+                                 PangoLanguage              *language)
+{
+  PangoFcFontMap2 *self = PANGO_FC_FONT_MAP2 (map);
+  PangoFontsetSimple *fontset;
+  int size;
+  const char *variations;
+  PangoGravity gravity;
+  const PangoMatrix *matrix;
+  const char *family_name;
+  char **families;
+  PangoFontDescription *copy;
+
+  fontset = pango_fontset_simple_new (language);
+
+  if (self->families->len == 0)
+    return PANGO_FONTSET (fontset);
+
+  if (pango_font_description_get_size_is_absolute (description))
+    size = pango_font_description_get_size (description);
+  else
+    size = pango_font_description_get_size (description) * self->dpi / 72.;
+
+  variations = pango_font_description_get_variations (description);
+
+  gravity = pango_font_description_get_gravity (description);
+  matrix = pango_context_get_matrix (context);
+
+  family_name = pango_font_description_get_family (description);
+  families = g_strsplit (family_name, ",", 0);
+
+  /* Unset gravity since PangoHbFace has no gravity */
+  copy = pango_font_description_copy_static (description);
+  pango_font_description_unset_fields (copy, PANGO_FONT_MASK_GRAVITY);
+
+  for (int i = 0; families[i]; i++)
+    {
+      PangoSimpleFamily *family;
+      PangoHbFace *face;
+      PangoHbFont *font;
+
+      family = find_family (self, families[i], variations ? TRUE : DONTCARE);
+      if (!family && variations)
+        family = find_family (self, families[i], DONTCARE);
+      if (!family)
+        continue;
+
+      face = find_face (family, copy);
+      if (!face)
+        continue;
+
+      font = pango_hb_font_new (face, size, variations, gravity, matrix);
+
+      pango_fontset_simple_append (fontset, PANGO_FONT (font));
+    }
+
+  g_strfreev (families);
+
+  pango_font_description_free (copy);
+
+  return PANGO_FONTSET (fontset);
+}
+
+static void
+pango_fc_font_map2_list_families (PangoFontMap      *map,
+                                  PangoFontFamily ***families,
+                                  int               *n_families)
+{
+  PangoFcFontMap2 *self = PANGO_FC_FONT_MAP2 (map);
+
+  ensure_families (self);
+
+  if (n_families)
+    *n_families = self->families->len;
+
+  if (families)
+    *families = g_memdup2 (self->families->pdata, self->families->len * sizeof (PangoFontFamily *));
+}
+
+static PangoFontFamily *
+pango_fc_font_map2_get_family (PangoFontMap *map,
+                               const char   *name)
+{
+  PangoFcFontMap2 *self = PANGO_FC_FONT_MAP2 (map);
+
+  ensure_families (self);
+
+  return PANGO_FONT_FAMILY (find_family (self, name, DONTCARE));
+}
+
+static void
+pango_fc_font_map2_class_init (PangoFcFontMap2Class *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  PangoFontMapClass *map_class = PANGO_FONT_MAP_CLASS (class);
+
+  FcInit ();
+
+  object_class->finalize = pango_fc_font_map2_finalize;
+
+  map_class->load_font = pango_fc_font_map2_load_font;
+  map_class->load_fontset = pango_fc_font_map2_load_fontset;
+  map_class->list_families = pango_fc_font_map2_list_families;
+  map_class->get_family = pango_fc_font_map2_get_family;
+}
+
+/* }}} */
+/* {{{ Public API */
+
+PangoFcFontMap2 *
+pango_fc_font_map2_new (void)
+{
+  return g_object_new (PANGO_TYPE_FC_FONT_MAP2, NULL);
+}
+
+void
+pango_fc_font_map2_set_config (PangoFcFontMap2 *self,
+                               FcConfig        *config)
+{
+  if (self->config)
+    FcConfigDestroy (self->config);
+
+  self->config = config;
+
+  if (self->config)
+    FcConfigReference (self->config);
+
+  g_ptr_array_set_size (self->families, 0);
+}
+
+void
+pango_fc_font_map2_set_resolution (PangoFcFontMap2 *self,
+                                   double           dpi)
+{
+  self->dpi = dpi;
+
+  pango_font_map_changed (PANGO_FONT_MAP (self));
+}
+
+/* }}} */
+
+/* vim:set foldmethod=marker expandtab: */
diff --git a/pango/pangofc-fontmap2.h b/pango/pangofc-fontmap2.h
new file mode 100644
index 00000000..4ee59901
--- /dev/null
+++ b/pango/pangofc-fontmap2.h
@@ -0,0 +1,48 @@
+/* Pango
+ * pangofc-fontmap2.h: Base fontmap type for fontconfig-based backends
+ *
+ * Copyright (C) 2021 Red Hat, Inc
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.         See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#pragma once
+
+#include <pango/pango.h>
+#include <fontconfig/fontconfig.h>
+#include <pango/pangofc-decoder.h>
+#include <pango/pangofc-font.h>
+#include <hb.h>
+
+G_BEGIN_DECLS
+
+#define PANGO_TYPE_FC_FONT_MAP2      (pango_fc_font_map2_get_type ())
+
+PANGO_AVAILABLE_IN_1_50
+G_DECLARE_FINAL_TYPE (PangoFcFontMap2, pango_fc_font_map2, PANGO, FC_FONT_MAP2, PangoFontMap)
+
+PANGO_AVAILABLE_IN_1_50
+PangoFcFontMap2 *       pango_fc_font_map2_new                  (void);
+
+PANGO_AVAILABLE_IN_1_50
+void                    pango_fc_font_map2_set_config           (PangoFcFontMap2 *self,
+                                                                 FcConfig        *config);
+
+PANGO_AVAILABLE_IN_1_50
+void                    pango_fc_font_map2_set_resolution       (PangoFcFontMap2 *self,
+                                                                 double           dpi);
+
+G_END_DECLS


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]