[pango/pango2: 47/168] Split off PangoFontSetCached




commit 2f4e6a05526783a9943ad7358651c339a7690db0
Author: Matthias Clasen <mclasen redhat com>
Date:   Wed Jun 8 17:43:18 2022 -0400

    Split off PangoFontSetCached

 pango/meson.build                    |   1 +
 pango/pango-fontset-cached-private.h |  63 +++++++++
 pango/pango-fontset-cached.c         | 256 +++++++++++++++++++++++++++++++++++
 pango/pango-hbfontmap.c              | 249 +---------------------------------
 4 files changed, 323 insertions(+), 246 deletions(-)
---
diff --git a/pango/meson.build b/pango/meson.build
index 9aa1c6f5..f65ef89d 100644
--- a/pango/meson.build
+++ b/pango/meson.build
@@ -18,6 +18,7 @@ pango_sources = [
   'pango-font-metrics.c',
   'pango-fontmap.c',
   'pango-fontset.c',
+  'pango-fontset-cached.c',
   'pango-fontset-simple.c',
   'pango-glyph-item.c',
   'pango-gravity.c',
diff --git a/pango/pango-fontset-cached-private.h b/pango/pango-fontset-cached-private.h
new file mode 100644
index 00000000..ec8e7617
--- /dev/null
+++ b/pango/pango-fontset-cached-private.h
@@ -0,0 +1,63 @@
+/*
+ * Copyright (C) 2021 Red Hat, Inc.
+ *
+ * SPDX-License-Identifier: LGPL-2.1-or-later
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, see <http://www.gnu.org/licenses/>.
+ */
+
+#pragma once
+
+#include <pango/pango-types.h>
+#include <pango/pango-fontset-private.h>
+#include <pango/pango-generic-family.h>
+#include <glib-object.h>
+
+
+typedef struct _PangoFontsetCached PangoFontsetCached;
+typedef struct _PangoFontsetCachedClass  PangoFontsetCachedClass;
+
+struct _PangoFontsetCached
+{
+  PangoFontset parent_instance;
+
+  GPtrArray *items; /* contains PangoHbFont or PangoGenericFamily */
+  PangoLanguage *language;
+  PangoFontDescription *description;
+  float dpi;
+  const PangoMatrix *matrix;
+  GList cache_link;
+  GHashTable *cache;
+};
+
+struct _PangoFontsetCachedClass
+{
+  PangoFontsetClass parent_class;
+};
+
+
+GType                pango_fontset_cached_get_type       (void) G_GNUC_CONST;
+
+PangoFontsetCached * pango_fontset_cached_new            (const PangoFontDescription *description,
+                                                          PangoLanguage              *language,
+                                                          float                       dpi,
+                                                          const PangoMatrix          *matrix);
+
+void                 pango_fontset_cached_add_face       (PangoFontsetCached         *self,
+                                                          PangoFontFace              *face);
+void                 pango_fontset_cached_add_family     (PangoFontsetCached         *self,
+                                                          PangoGenericFamily         *family);
+int                  pango_fontset_cached_size           (PangoFontsetCached         *self);
+PangoFont *          pango_fontset_cached_get_first_font (PangoFontsetCached         *self);
+
diff --git a/pango/pango-fontset-cached.c b/pango/pango-fontset-cached.c
new file mode 100644
index 00000000..28194948
--- /dev/null
+++ b/pango/pango-fontset-cached.c
@@ -0,0 +1,256 @@
+/* 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.
+ */
+
+#include "config.h"
+#include <math.h>
+
+#include <gio/gio.h>
+
+#include "pango-fontset-cached-private.h"
+#include "pango-font-face-private.h"
+#include "pango-generic-family-private.h"
+
+
+G_DEFINE_TYPE (PangoFontsetCached, pango_fontset_cached, PANGO_TYPE_FONTSET);
+
+static void
+pango_fontset_cached_init (PangoFontsetCached *fontset)
+{
+  fontset->items = g_ptr_array_new_with_free_func (g_object_unref);
+  fontset->cache = g_hash_table_new_full (NULL, NULL, NULL, g_object_unref);
+  fontset->language = NULL;
+}
+
+static void
+pango_fontset_cached_finalize (GObject *object)
+{
+  PangoFontsetCached *self = (PangoFontsetCached *)object;
+
+  g_ptr_array_free (self->items, TRUE);
+  g_hash_table_unref (self->cache);
+  pango_font_description_free (self->description);
+
+  G_OBJECT_CLASS (pango_fontset_cached_parent_class)->finalize (object);
+}
+
+static PangoFont *
+pango_fontset_cached_get_font (PangoFontset *fontset,
+                               guint         wc)
+{
+  PangoFontsetCached *self = (PangoFontsetCached *)fontset;
+  PangoFont *retval;
+  int i;
+
+  retval = g_hash_table_lookup (self->cache, GUINT_TO_POINTER (wc));
+  if (retval)
+    return g_object_ref (retval);
+
+  for (i = 0; i < self->items->len; i++)
+    {
+      gpointer item = g_ptr_array_index (self->items, i);
+
+      if (PANGO_IS_FONT (item))
+        {
+          PangoFont *font = PANGO_FONT (item);
+          if (pango_font_has_char (font, wc))
+            {
+              retval = g_object_ref (font);
+              break;
+            }
+        }
+      else if (PANGO_IS_GENERIC_FAMILY (item))
+        {
+          PangoGenericFamily *family = PANGO_GENERIC_FAMILY (item);
+          PangoFontFace *face;
+
+          /* Here is where we implement delayed picking for generic families.
+           * If a face does not cover the character and its family is generic,
+           * choose a different face from the same family and create a font to use.
+           */
+          face = pango_generic_family_find_face (family,
+                                                 self->description,
+                                                 self->language,
+                                                 wc);
+          if (face)
+            {
+              GHashTableIter iter;
+              PangoFont *font;
+
+              g_hash_table_iter_init (&iter, self->cache);
+              while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&font))
+                {
+                  if (pango_font_get_face (font) == face)
+                    {
+                      retval = g_object_ref (font);
+                      break;
+                    }
+                }
+
+              if (!retval)
+                retval = pango_font_face_create_font (face,
+                                                      self->description,
+                                                      self->dpi,
+                                                      self->matrix);
+              break;
+            }
+        }
+    }
+
+  if (retval)
+    g_hash_table_insert (self->cache, GUINT_TO_POINTER (wc), g_object_ref (retval));
+
+  return retval;
+}
+
+PangoFont *
+pango_fontset_cached_get_first_font (PangoFontsetCached *self)
+{
+  gpointer item;
+
+  item = g_ptr_array_index (self->items, 0);
+
+  if (PANGO_IS_FONT (item))
+    return g_object_ref (PANGO_FONT (item));
+  else if (PANGO_IS_GENERIC_FAMILY (item))
+    {
+      PangoFontFace *face = pango_generic_family_find_face (PANGO_GENERIC_FAMILY (item), self->description, 
self->language, 0);
+      return pango_font_face_create_font (face, self->description, self->dpi, self->matrix);
+    }
+
+  return NULL;
+}
+
+static PangoFontMetrics *
+pango_fontset_cached_get_metrics (PangoFontset *fontset)
+{
+  PangoFontsetCached *self = (PangoFontsetCached *)fontset;
+
+  if (self->items->len == 1)
+    {
+      PangoFont *font = pango_fontset_cached_get_first_font (self);
+      PangoFontMetrics *ret;
+
+      ret = pango_font_get_metrics (font, self->language);
+      g_object_unref (font);
+
+      return ret;
+    }
+
+  return PANGO_FONTSET_CLASS (pango_fontset_cached_parent_class)->get_metrics (fontset);
+}
+
+static PangoLanguage *
+pango_fontset_cached_get_language (PangoFontset *fontset)
+{
+  PangoFontsetCached *self = (PangoFontsetCached *)fontset;
+
+  return self->language;
+}
+
+static void
+pango_fontset_cached_foreach (PangoFontset            *fontset,
+                              PangoFontsetForeachFunc  func,
+                              gpointer                 data)
+{
+  PangoFontsetCached *self = (PangoFontsetCached *)fontset;
+  unsigned int i;
+
+  for (i = 0; i < self->items->len; i++)
+    {
+      gpointer item = g_ptr_array_index (self->items, i);
+      PangoFont *font = NULL;
+
+      if (PANGO_IS_FONT (item))
+        {
+          font = g_object_ref (PANGO_FONT (item));
+        }
+      else if (PANGO_IS_GENERIC_FAMILY (item))
+        {
+          PangoFontFace *face = pango_generic_family_find_face (PANGO_GENERIC_FAMILY (item), 
self->description, self->language, 0);
+          font = pango_font_face_create_font (face, self->description, self->dpi, self->matrix);
+        }
+
+      if ((*func) (fontset, font, data))
+        {
+          g_object_unref (font);
+          return;
+        }
+      g_object_unref (font);
+    }
+}
+
+static void
+pango_fontset_cached_class_init (PangoFontsetCachedClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  PangoFontsetClass *fontset_class = PANGO_FONTSET_CLASS (class);
+
+  object_class->finalize = pango_fontset_cached_finalize;
+
+  fontset_class->get_font = pango_fontset_cached_get_font;
+  fontset_class->get_metrics = pango_fontset_cached_get_metrics;
+  fontset_class->get_language = pango_fontset_cached_get_language;
+  fontset_class->foreach = pango_fontset_cached_foreach;
+}
+
+PangoFontsetCached *
+pango_fontset_cached_new (const PangoFontDescription *description,
+                          PangoLanguage              *language,
+                          float                       dpi,
+                          const PangoMatrix          *matrix)
+{
+  PangoFontsetCached *self;
+
+  self = g_object_new (pango_fontset_cached_get_type (), NULL);
+  self->language = language;
+  self->description = pango_font_description_copy (description);
+  self->dpi = dpi;
+  self->matrix = matrix;
+
+  return self;
+}
+
+void
+pango_fontset_cached_add_face (PangoFontsetCached *self,
+                               PangoFontFace      *face)
+{
+  PangoFont *font;
+
+  font = pango_font_face_create_font (face,
+                                      self->description,
+                                      self->dpi,
+                                      self->matrix);
+  g_ptr_array_add (self->items, font);
+}
+
+void
+pango_fontset_cached_add_family (PangoFontsetCached *self,
+                                 PangoGenericFamily *family)
+{
+  g_ptr_array_add (self->items, g_object_ref (family));
+}
+
+int
+pango_fontset_cached_size (PangoFontsetCached *self)
+{
+  return self->items->len;
+}
+
+/* vim:set foldmethod=marker expandtab: */
diff --git a/pango/pango-hbfontmap.c b/pango/pango-hbfontmap.c
index 99b5ecdb..eef10b68 100644
--- a/pango/pango-hbfontmap.c
+++ b/pango/pango-hbfontmap.c
@@ -28,7 +28,7 @@
 #include "pango-generic-family-private.h"
 #include "pango-hbface-private.h"
 #include "pango-hbfont-private.h"
-#include "pango-fontset-private.h"
+#include "pango-fontset-cached-private.h"
 #include "pango-userface-private.h"
 #include "pango-userfont-private.h"
 #include "pango-fontset.h"
@@ -73,8 +73,6 @@
 
 /* The number of fontsets we keep in the fontset cache */
 
-#define FONTSET_CACHE_SIZE 256
-
 
 /* {{{ GListModel implementation */
 
@@ -113,250 +111,9 @@ pango_hb_font_map_list_model_init (GListModelInterface *iface)
 }
 
 /* }}} */
-/* {{{ Fontset implementation */
-
-typedef struct _PangoFontsetCached PangoFontsetCached;
-
-struct _PangoFontsetCached
-{
-  PangoFontset parent_instance;
-
-  GPtrArray *items; /* contains PangoHbFont or PangoGenericFamily */
-  PangoLanguage *language;
-  PangoFontDescription *description;
-  float dpi;
-  const PangoMatrix *matrix;
-  GList cache_link;
-  GHashTable *cache;
-};
-
-typedef PangoFontsetClass PangoFontsetCachedClass;
-
-GType pango_fontset_cached_get_type (void) G_GNUC_CONST;
-
-G_DEFINE_TYPE (PangoFontsetCached, pango_fontset_cached, PANGO_TYPE_FONTSET);
-
-static void
-pango_fontset_cached_init (PangoFontsetCached *fontset)
-{
-  fontset->items = g_ptr_array_new_with_free_func (g_object_unref);
-  fontset->cache = g_hash_table_new_full (NULL, NULL, NULL, g_object_unref);
-  fontset->language = NULL;
-}
-
-static void
-pango_fontset_cached_finalize (GObject *object)
-{
-  PangoFontsetCached *self = (PangoFontsetCached *)object;
-
-  g_ptr_array_free (self->items, TRUE);
-  g_hash_table_unref (self->cache);
-  pango_font_description_free (self->description);
-
-  G_OBJECT_CLASS (pango_fontset_cached_parent_class)->finalize (object);
-}
-
-static PangoFont *
-pango_fontset_cached_get_font (PangoFontset *fontset,
-                               guint         wc)
-{
-  PangoFontsetCached *self = (PangoFontsetCached *)fontset;
-  PangoFont *retval;
-  int i;
-
-  retval = g_hash_table_lookup (self->cache, GUINT_TO_POINTER (wc));
-  if (retval)
-    return g_object_ref (retval);
-
-  for (i = 0; i < self->items->len; i++)
-    {
-      gpointer item = g_ptr_array_index (self->items, i);
-
-      if (PANGO_IS_FONT (item))
-        {
-          PangoFont *font = PANGO_FONT (item);
-          if (pango_font_has_char (font, wc))
-            {
-              retval = g_object_ref (font);
-              break;
-            }
-        }
-      else if (PANGO_IS_GENERIC_FAMILY (item))
-        {
-          PangoGenericFamily *family = PANGO_GENERIC_FAMILY (item);
-          PangoFontFace *face;
-
-          /* Here is where we implement delayed picking for generic families.
-           * If a face does not cover the character and its family is generic,
-           * choose a different face from the same family and create a font to use.
-           */
-          face = pango_generic_family_find_face (family,
-                                                 self->description,
-                                                 self->language,
-                                                 wc);
-          if (face)
-            {
-              GHashTableIter iter;
-              PangoFont *font;
-
-              g_hash_table_iter_init (&iter, self->cache);
-              while (g_hash_table_iter_next (&iter, NULL, (gpointer *)&font))
-                {
-                  if (pango_font_get_face (font) == face)
-                    {
-                      retval = g_object_ref (font);
-                      break;
-                    }
-                }
-
-              if (!retval)
-                retval = pango_font_face_create_font (face,
-                                                      self->description,
-                                                      self->dpi,
-                                                      self->matrix);
-              break;
-            }
-        }
-    }
-
-  if (retval)
-    g_hash_table_insert (self->cache, GUINT_TO_POINTER (wc), g_object_ref (retval));
-
-  return retval;
-}
-
-static PangoFont *
-pango_fontset_cached_get_first_font (PangoFontsetCached *self)
-{
-  gpointer item;
-
-  item = g_ptr_array_index (self->items, 0);
-
-  if (PANGO_IS_FONT (item))
-    return g_object_ref (PANGO_FONT (item));
-  else if (PANGO_IS_GENERIC_FAMILY (item))
-    {
-      PangoFontFace *face = pango_generic_family_find_face (PANGO_GENERIC_FAMILY (item), self->description, 
self->language, 0);
-      return pango_font_face_create_font (face, self->description, self->dpi, self->matrix);
-    }
-
-  return NULL;
-}
-
-static PangoFontMetrics *
-pango_fontset_cached_get_metrics (PangoFontset *fontset)
-{
-  PangoFontsetCached *self = (PangoFontsetCached *)fontset;
-
-  if (self->items->len == 1)
-    {
-      PangoFont *font = pango_fontset_cached_get_first_font (self);
-      PangoFontMetrics *ret;
-
-      ret = pango_font_get_metrics (font, self->language);
-      g_object_unref (font);
-
-      return ret;
-    }
-
-  return PANGO_FONTSET_CLASS (pango_fontset_cached_parent_class)->get_metrics (fontset);
-}
-
-static PangoLanguage *
-pango_fontset_cached_get_language (PangoFontset *fontset)
-{
-  PangoFontsetCached *self = (PangoFontsetCached *)fontset;
-
-  return self->language;
-}
-
-static void
-pango_fontset_cached_foreach (PangoFontset            *fontset,
-                              PangoFontsetForeachFunc  func,
-                              gpointer                 data)
-{
-  PangoFontsetCached *self = (PangoFontsetCached *)fontset;
-  unsigned int i;
-
-  for (i = 0; i < self->items->len; i++)
-    {
-      gpointer item = g_ptr_array_index (self->items, i);
-      PangoFont *font = NULL;
-
-      if (PANGO_IS_FONT (item))
-        {
-          font = g_object_ref (PANGO_FONT (item));
-        }
-      else if (PANGO_IS_GENERIC_FAMILY (item))
-        {
-          PangoFontFace *face = pango_generic_family_find_face (PANGO_GENERIC_FAMILY (item), 
self->description, self->language, 0);
-          font = pango_font_face_create_font (face, self->description, self->dpi, self->matrix);
-        }
-
-      if ((*func) (fontset, font, data))
-        {
-          g_object_unref (font);
-          return;
-        }
-      g_object_unref (font);
-    }
-}
-
-static void
-pango_fontset_cached_class_init (PangoFontsetCachedClass *class)
-{
-  GObjectClass *object_class = G_OBJECT_CLASS (class);
-  PangoFontsetClass *fontset_class = PANGO_FONTSET_CLASS (class);
-
-  object_class->finalize = pango_fontset_cached_finalize;
-
-  fontset_class->get_font = pango_fontset_cached_get_font;
-  fontset_class->get_metrics = pango_fontset_cached_get_metrics;
-  fontset_class->get_language = pango_fontset_cached_get_language;
-  fontset_class->foreach = pango_fontset_cached_foreach;
-}
-
-static PangoFontsetCached *
-pango_fontset_cached_new (const PangoFontDescription *description,
-                          PangoLanguage              *language,
-                          float                       dpi,
-                          const PangoMatrix          *matrix)
-{
-  PangoFontsetCached *self;
-
-  self = g_object_new (pango_fontset_cached_get_type (), NULL);
-  self->language = language;
-  self->description = pango_font_description_copy (description);
-  self->dpi = dpi;
-  self->matrix = matrix;
-
-  return self;
-}
-
-static void
-pango_fontset_cached_add_face (PangoFontsetCached *self,
-                               PangoFontFace      *face)
-{
-  g_ptr_array_add (self->items,
-                   pango_font_face_create_font (face,
-                                                self->description,
-                                                self->dpi,
-                                                self->matrix));
-}
-
-static void
-pango_fontset_cached_add_family (PangoFontsetCached *self,
-                                 PangoGenericFamily *family)
-{
-  g_ptr_array_add (self->items, g_object_ref (family));
-}
-
-static int
-pango_fontset_cached_size (PangoFontsetCached *self)
-{
-  return self->items->len;
-}
+/* {{{ Fontset caching */
 
+#define FONTSET_CACHE_SIZE 256
 #define FNV1_32_INIT ((guint32)0x811c9dc5)
 
 static guint


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