[pango/simple-fontmap: 9/16] Add PangoSimpleFontmap




commit a92a53c8bcfa0c6b264d63e3826e02ab9000e00b
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Oct 29 19:08:02 2021 -0400

    Add PangoSimpleFontmap
    
    This is a very basic fontmap implementation which
    lets you add individual faces. It is meant for
    testing purposes only and does not do any caching.

 pango/meson.build                    |   3 +
 pango/pango-hbfont.c                 |   3 +-
 pango/pango-simple-fontmap-private.h |  46 ++++
 pango/pango-simple-fontmap.c         | 454 +++++++++++++++++++++++++++++++++++
 pango/pango-simple-fontmap.h         |  54 +++++
 pango/pango.h                        |   1 +
 6 files changed, 560 insertions(+), 1 deletion(-)
---
diff --git a/pango/meson.build b/pango/meson.build
index 71f77931..dfc997ef 100644
--- a/pango/meson.build
+++ b/pango/meson.build
@@ -33,6 +33,8 @@ pango_sources = [
   'pango-hbface.c',
   'pango-simple-family.c',
   'pango-hbfont.c',
+  'pango-simple-family.c',
+  'pango-simple-fontmap.c',
 ]
 
 pango_headers = [
@@ -64,6 +66,7 @@ pango_headers = [
   'pango-utils.h',
   'pango-hbface.h',
   'pango-hbfont.h',
+  'pango-simple-fontmap.h',
 ]
 
 pango_installed_headers = pango_headers + [ 'pango-version-macros.h' ]
diff --git a/pango/pango-hbfont.c b/pango/pango-hbfont.c
index a0c25616..9301799e 100644
--- a/pango/pango-hbfont.c
+++ b/pango/pango-hbfont.c
@@ -689,7 +689,8 @@ pango_hb_font_new (PangoHbFace       *face,
 
   self->size = size;
   self->variations = g_strdup (variations);
-  self->gravity = gravity;
+  if (gravity != PANGO_GRAVITY_AUTO)
+    self->gravity = gravity;
   if (matrix)
     self->matrix = *matrix;
 
diff --git a/pango/pango-simple-fontmap-private.h b/pango/pango-simple-fontmap-private.h
new file mode 100644
index 00000000..8732d6cb
--- /dev/null
+++ b/pango/pango-simple-fontmap-private.h
@@ -0,0 +1,46 @@
+/* Pango
+ *
+ * Copyright (C) 2021 Red Hat Software
+ *
+ * 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-simple-fontmap.h>
+
+G_BEGIN_DECLS
+
+struct _PangoSimpleFontMap
+{
+  PangoFontMap parent_instance;
+
+  GPtrArray *families;
+  GHashTable *fontsets;
+
+  double dpi;
+};
+
+struct _PangoSimpleFontMapClass
+{
+  PangoFontMapClass parent_class;
+
+  void (* populate) (PangoSimpleFontMap *self);
+
+  gpointer reserved[10];
+};
+
+G_END_DECLS
diff --git a/pango/pango-simple-fontmap.c b/pango/pango-simple-fontmap.c
new file mode 100644
index 00000000..43be564d
--- /dev/null
+++ b/pango/pango-simple-fontmap.c
@@ -0,0 +1,454 @@
+/* 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 "pango-simple-fontmap-private.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 "pango-fontset-private.h"
+
+#include <hb-ot.h>
+
+
+/* {{{ GListModel implementation */
+
+static GType
+pango_simple_font_map_get_item_type (GListModel *list)
+{
+  return PANGO_TYPE_FONT_FAMILY;
+}
+
+static guint
+pango_simple_font_map_get_n_items (GListModel *list)
+{
+  PangoSimpleFontMap *self = PANGO_SIMPLE_FONT_MAP (list);
+
+  return self->families->len;
+}
+
+static gpointer
+pango_simple_font_map_get_item (GListModel *list,
+                                guint       position)
+{
+  PangoSimpleFontMap *self = PANGO_SIMPLE_FONT_MAP (list);
+
+  if (position < self->families->len)
+    return g_object_ref (g_ptr_array_index (self->families, position));
+
+  return NULL;
+}
+
+static void
+pango_simple_font_map_list_model_init (GListModelInterface *iface)
+{
+  iface->get_item_type = pango_simple_font_map_get_item_type;
+  iface->get_n_items = pango_simple_font_map_get_n_items;
+  iface->get_item = pango_simple_font_map_get_item;
+}
+
+/* }}} */
+/* {{{ Fontset keys */
+
+typedef struct {
+  PangoFontDescription *description;
+  PangoLanguage *language;
+} PangoFontsetKey;
+
+static void
+pango_fontset_key_init (PangoFontsetKey            *key,
+                        const PangoFontDescription *description,
+                        PangoLanguage              *language)
+{
+  key->description = (PangoFontDescription *)description;
+  key->language = language;
+}
+
+static gboolean
+pango_fontset_key_equal (const PangoFontsetKey *key_a,
+                         const PangoFontsetKey *key_b)
+{
+  if (key_a->language == key_b->language &&
+      pango_font_description_equal (key_a->description, key_b->description))
+    return TRUE;
+  else
+    return FALSE;
+}
+
+#define FNV1_32_INIT ((guint32)0x811c9dc5)
+
+static guint
+pango_fontset_key_hash (const PangoFontsetKey *key)
+{
+  guint32 hash = FNV1_32_INIT;
+
+  return (hash ^
+          GPOINTER_TO_UINT (key->language) ^
+          pango_font_description_hash (key->description));
+}
+
+static void
+pango_fontset_key_free (PangoFontsetKey *key)
+{
+  pango_font_description_free (key->description);
+  g_slice_free (PangoFontsetKey, key);
+}
+
+static PangoFontsetKey *
+pango_fontset_key_copy (const PangoFontsetKey *old)
+{
+  PangoFontsetKey *key = g_slice_new (PangoFontsetKey);
+
+  key->language = old->language;
+  key->description = pango_font_description_copy (old->description);
+
+  return key;
+}
+
+/* }}} */
+/* {{{ PangoFontMap implementation */
+
+G_DEFINE_TYPE_WITH_CODE (PangoSimpleFontMap, pango_simple_font_map, PANGO_TYPE_FONT_MAP,
+                         G_IMPLEMENT_INTERFACE (G_TYPE_LIST_MODEL, pango_simple_font_map_list_model_init))
+
+
+static void
+pango_simple_font_map_init (PangoSimpleFontMap *self)
+{
+  self->families = g_ptr_array_new_with_free_func (g_object_unref);
+  self->fontsets = g_hash_table_new_full ((GHashFunc) pango_fontset_key_hash,
+                                          (GEqualFunc) pango_fontset_key_equal,
+                                          (GDestroyNotify) pango_fontset_key_free,
+                                          (GDestroyNotify) g_object_unref);
+  self->dpi = 96.;
+}
+
+static void
+pango_simple_font_map_finalize (GObject *object)
+{
+  PangoSimpleFontMap *self = PANGO_SIMPLE_FONT_MAP (object);
+
+  g_ptr_array_unref (self->families);
+  g_hash_table_unref (self->fontsets);
+
+  G_OBJECT_CLASS (pango_simple_font_map_parent_class)->finalize (object);
+}
+
+#define DONTCARE 2
+
+static PangoSimpleFamily *
+find_family (PangoSimpleFontMap *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;
+}
+
+/* Load a font from the first matching family */
+static PangoFont *
+pango_simple_font_map_load_font (PangoFontMap               *map,
+                                 PangoContext               *context,
+                                 const PangoFontDescription *description)
+{
+  PangoSimpleFontMap *self = PANGO_SIMPLE_FONT_MAP (map);
+  PangoFontsetSimple *fontset;
+  PangoLanguage *language;
+  PangoFont *font = NULL;
+
+  if (self->families->len == 0)
+    return NULL;
+
+  if (context)
+    language = pango_context_get_language (context);
+  else
+    language = NULL;
+
+  fontset = PANGO_FONTSET_SIMPLE (pango_font_map_load_fontset (map, context, description, language));
+  if (fontset)
+    {
+      if (fontset->fonts->len > 0)
+        font = g_object_ref (g_ptr_array_index (fontset->fonts, 0));
+      g_object_unref (fontset);
+    }
+
+  return font;
+}
+
+/* Add one font for each family we find */
+static PangoFontset *
+pango_simple_font_map_load_fontset (PangoFontMap               *map,
+                                    PangoContext               *context,
+                                    const PangoFontDescription *description,
+                                    PangoLanguage              *language)
+{
+  PangoSimpleFontMap *self = PANGO_SIMPLE_FONT_MAP (map);
+  PangoFontsetKey key;
+  PangoFontsetSimple *fontset;
+  int size;
+  const char *variations;
+  PangoGravity gravity;
+  const PangoMatrix *matrix;
+  const char *family_name;
+  char **families;
+  PangoFontDescription *copy;
+
+  if (!language)
+    language = pango_context_get_language (context);
+
+  pango_fontset_key_init (&key, description, language);
+  fontset = g_hash_table_lookup (self->fontsets, &key);
+  if (fontset)
+    return g_object_ref (PANGO_FONTSET (fontset));
+
+  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 = PANGO_HB_FACE (pango_simple_family_find_face (family, copy, language));
+      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);
+
+  if (pango_fontset_simple_size (fontset) == 0)
+    {
+      PangoSimpleFamily *family;
+      PangoHbFace *face;
+      PangoHbFont *font;
+
+      family = g_ptr_array_index (self->families, 0);
+      face = g_ptr_array_index (family->faces, 0);
+
+      font = pango_hb_font_new (face, size, variations, gravity, matrix);
+      pango_fontset_simple_append (fontset, PANGO_FONT (font));
+    }
+
+  g_hash_table_insert (self->fontsets, pango_fontset_key_copy (&key), fontset);
+
+  return g_object_ref (PANGO_FONTSET (fontset));
+}
+
+static void
+pango_simple_font_map_list_families (PangoFontMap      *map,
+                                     PangoFontFamily ***families,
+                                     int               *n_families)
+{
+  PangoSimpleFontMap *self = PANGO_SIMPLE_FONT_MAP (map);
+
+  if (n_families)
+    *n_families = self->families->len;
+
+  if (families)
+    *families = g_memdup2 (self->families->pdata, self->families->len * sizeof (PangoFontFamily *));
+}
+
+static PangoFontFamily *
+pango_simple_font_map_get_family (PangoFontMap *map,
+                                  const char   *name)
+{
+  PangoSimpleFontMap *self = PANGO_SIMPLE_FONT_MAP (map);
+
+  return PANGO_FONT_FAMILY (find_family (self, name, DONTCARE));
+}
+
+static void
+pango_simple_font_map_populate (PangoSimpleFontMap *self)
+{
+}
+
+static void
+pango_simple_font_map_class_init (PangoSimpleFontMapClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  PangoFontMapClass *fontmap_class = PANGO_FONT_MAP_CLASS (class);
+
+  object_class->finalize = pango_simple_font_map_finalize;
+
+  fontmap_class->load_font = pango_simple_font_map_load_font;
+  fontmap_class->load_fontset = pango_simple_font_map_load_fontset;
+  fontmap_class->list_families = pango_simple_font_map_list_families;
+  fontmap_class->get_family = pango_simple_font_map_get_family;
+
+  class->populate = pango_simple_font_map_populate;
+}
+
+/* }}} */
+/* {{{ Public API */
+
+/**
+ * pango_simple_font_map_new:
+ *
+ * Creates a new `PangoSimpleFontmMap`.
+ *
+ * Returns: A newly created `PangoSimpleFontMap
+ */
+PangoSimpleFontMap *
+pango_simple_font_map_new (void)
+{
+  return g_object_new (PANGO_TYPE_SIMPLE_FONT_MAP, NULL);
+}
+
+/**
+ * pango_simple_font_map_add_face:
+ * @self: a `PangoSimpleFontMap`
+ * @face: a `PangoHbFace`
+ *
+ * Adds @face to the `PangoSimpleFontMap`.
+ *
+ * This is most useful for creating transformed
+ * faces or aliases. See [method@Pango.HbFace.new_transformed]
+ * or [method@Pango.HbFace.new_alias].
+ */
+void
+pango_simple_font_map_add_face (PangoSimpleFontMap *self,
+                                PangoHbFace        *face)
+{
+  PangoFontMap *map = PANGO_FONT_MAP (self);
+  const char *family_name;
+  PangoSimpleFamily *family;
+
+  family_name = pango_font_description_get_family (face->description);
+  family = PANGO_SIMPLE_FAMILY (pango_font_map_get_family (map, family_name));
+  if (!family)
+    {
+      int position;
+
+      position = self->families->len;
+
+      family = pango_simple_family_new (map,
+                                        family_name,
+                                        pango_hb_face_is_monospace (face),
+                                        pango_hb_face_is_variable (face));
+      g_ptr_array_add (self->families, family);
+
+      g_list_model_items_changed (G_LIST_MODEL (self), position, 0, 1);
+    }
+
+  pango_simple_family_add_face (family, PANGO_FONT_FACE (face));
+}
+
+/**
+ * pango_simple_font_map_add_file:
+ * @file: font filename
+ *
+ * Creates a new `PangoHbFace` and adds it.
+ */
+void
+pango_simple_font_map_add_file (PangoSimpleFontMap *self,
+                                const char         *file)
+{
+  PangoHbFace *face;
+
+  face = pango_hb_face_new_from_file (file, 0, -1, NULL, NULL);
+  pango_simple_font_map_add_face (self, face);
+  g_object_unref (face);
+}
+
+/**
+ * pango_simple_font_map_set_resolution:
+ * @self: a `PangoSimpleFontMap`
+ * @dpi: the new resolution, in "dots per inch"
+ *
+ * Sets the resolution for the fontmap.
+ *
+ * This is a scale factor between points specified in a
+ * `PangoFontDescription` and Cairo units. The default value
+ * is 96, meaning that a 10 point font will be 13 units high.
+ * (10 * 96. / 72. = 13.3).
+ */
+void
+pango_simple_font_map_set_resolution (PangoSimpleFontMap *self,
+                                      double              dpi)
+{
+  self->dpi = dpi;
+
+  pango_simple_font_map_cache_clear (self);
+}
+
+void
+pango_simple_font_map_cache_clear (PangoSimpleFontMap *self)
+{
+  int removed, added;
+
+  removed = self->families->len;
+
+  PANGO_SIMPLE_FONT_MAP_GET_CLASS (self)->populate (self);
+
+  added = self->families->len;
+
+  g_list_model_items_changed (G_LIST_MODEL (self), 0, removed, added);
+
+  pango_font_map_changed (PANGO_FONT_MAP (self));
+}
+
+/* }}} */
+
+/* vim:set foldmethod=marker expandtab: */
diff --git a/pango/pango-simple-fontmap.h b/pango/pango-simple-fontmap.h
new file mode 100644
index 00000000..0cf75a8c
--- /dev/null
+++ b/pango/pango-simple-fontmap.h
@@ -0,0 +1,54 @@
+/* Pango
+ *
+ * Copyright (C) 2021 Matthias Clasen
+ *
+ * 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-types.h>
+#include <pango/pango-font.h>
+#include <pango/pango-fontmap.h>
+#include <pango/pango-hbface.h>
+#include <hb.h>
+
+G_BEGIN_DECLS
+
+#define PANGO_TYPE_SIMPLE_FONT_MAP   (pango_simple_font_map_get_type ())
+
+PANGO_AVAILABLE_IN_1_50
+PANGO_DECLARE_INTERNAL_TYPE (PangoSimpleFontMap, pango_simple_font_map, PANGO, SIMPLE_FONT_MAP, PangoFontMap)
+
+PANGO_AVAILABLE_IN_1_50
+PangoSimpleFontMap *    pango_simple_font_map_new               (void);
+
+PANGO_AVAILABLE_IN_1_50
+void                    pango_simple_font_map_add_file          (PangoSimpleFontMap *self,
+                                                                 const char         *file);
+
+PANGO_AVAILABLE_IN_1_50
+void                    pango_simple_font_map_add_face          (PangoSimpleFontMap *self,
+                                                                 PangoHbFace        *face);
+
+PANGO_AVAILABLE_IN_1_50
+void                    pango_simple_font_map_set_resolution    (PangoSimpleFontMap *self,
+                                                                 double              dpi);
+
+PANGO_AVAILABLE_IN_1_50
+void                    pango_simple_font_map_cache_clear       (PangoSimpleFontMap *self);
+
+G_END_DECLS
diff --git a/pango/pango.h b/pango/pango.h
index b6eb0051..4b7a91dd 100644
--- a/pango/pango.h
+++ b/pango/pango.h
@@ -47,6 +47,7 @@
 #include <pango/pango-markup.h>
 #include <pango/pango-renderer.h>
 #include <pango/pango-script.h>
+#include <pango/pango-simple-fontmap.h>
 #include <pango/pango-tabs.h>
 #include <pango/pango-types.h>
 #include <pango/pango-utils.h>


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