[pango/simple-fontmap: 7/15] Add PangoSimpleFontmap




commit d9b242b5b8550c92ceccd7e804c7968b7c1d72a6
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.

 pango/meson.build           |   1 +
 pango/pango-hbfont.c        |   3 +-
 pango/pango-simplefontmap.  |   0
 pango/pango-simplefontmap.c | 287 ++++++++++++++++++++++++++++++++++++++++++++
 pango/pango-simplefontmap.h |  51 ++++++++
 pango/pango.h               |   1 +
 6 files changed, 342 insertions(+), 1 deletion(-)
---
diff --git a/pango/meson.build b/pango/meson.build
index 10a62903..89ab3e4f 100644
--- a/pango/meson.build
+++ b/pango/meson.build
@@ -30,6 +30,7 @@ pango_sources = [
   'pango-hbface.c',
   'pango-hbfont.c',
   'pango-simple-family.c',
+  'pango-simplefontmap.c',
 ]
 
 pango_headers = [
diff --git a/pango/pango-hbfont.c b/pango/pango-hbfont.c
index 864f71ed..76adbc01 100644
--- a/pango/pango-hbfont.c
+++ b/pango/pango-hbfont.c
@@ -661,7 +661,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-simplefontmap. b/pango/pango-simplefontmap.
new file mode 100644
index 00000000..e69de29b
diff --git a/pango/pango-simplefontmap.c b/pango/pango-simplefontmap.c
new file mode 100644
index 00000000..fc084c0e
--- /dev/null
+++ b/pango/pango-simplefontmap.c
@@ -0,0 +1,287 @@
+/* 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-simplefontmap.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 <hb-ot.h>
+
+/* {{{ PangoFontMap implementation */
+
+struct _PangoSimpleFontMap
+{
+  PangoFontMap parent_instance;
+
+  GPtrArray *families;
+
+  double dpi;
+};
+
+struct _PangoSimpleFontMapClass
+{
+  PangoFontMapClass parent_class;
+};
+
+G_DEFINE_TYPE (PangoSimpleFontMap, pango_simple_font_map, PANGO_TYPE_FONT_MAP)
+
+static void
+pango_simple_font_map_init (PangoSimpleFontMap *self)
+{
+  self->families = g_ptr_array_new_with_free_func (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_OBJECT_CLASS (pango_simple_font_map_parent_class)->finalize (object);
+}
+
+static PangoFont *
+pango_simple_font_map_load_font (PangoFontMap               *map,
+                                 PangoContext               *context,
+                                 const PangoFontDescription *desc)
+{
+  PangoSimpleFontMap *self = PANGO_SIMPLE_FONT_MAP (map);
+  PangoFontDescription *best = NULL;
+  PangoHbFace *face = NULL;
+  PangoHbFace *first = NULL;
+  int size;
+  const char *variations;
+  PangoGravity gravity;
+  const PangoMatrix *matrix;
+  PangoFontDescription *copy = pango_font_description_copy_static (desc);
+  const char *family_name = pango_font_description_get_family (desc);
+
+  pango_font_description_unset_fields (copy, PANGO_FONT_MASK_SIZE |
+                                             PANGO_FONT_MASK_VARIATIONS |
+                                             PANGO_FONT_MASK_GRAVITY);
+
+  for (int pass = 0; pass <= 1; pass++)
+    {
+      for (int i = 0; i < self->families->len; i++)
+        {
+          PangoSimpleFamily *family = g_ptr_array_index (self->families, i);
+
+          if (pass > 0 || g_ascii_strcasecmp (family->name, family_name) == 0)
+            {
+              for (int j = 0; j < family->faces->len; j++)
+                {
+                  PangoHbFace *face2 = g_ptr_array_index (family->faces, j);
+
+                  if (!first)
+                    first = face2;
+
+                  if (pango_font_description_better_match (copy, best, face2->description))
+                    {
+                      face = face2;
+                      best = face2->description;
+                    }
+                }
+            }
+        }
+    }
+
+  pango_font_description_free (copy);
+
+  if (face == NULL)
+    {
+      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 = first;
+    }
+
+  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);
+
+  return PANGO_FONT (pango_hb_font_new (face, size, variations, gravity, matrix));
+}
+
+static PangoFontset *
+pango_simple_font_map_load_fontset (PangoFontMap               *map,
+                                    PangoContext               *context,
+                                    const PangoFontDescription *description,
+                                    PangoLanguage              *language)
+{
+  PangoFontsetSimple *fontset;
+
+  // TODO: Do the FcFontSort thing
+  fontset = pango_fontset_simple_new (language);
+  pango_fontset_simple_append (fontset, pango_font_map_load_font (map, context, description));
+
+  return 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);
+
+  for (int i = 0; i < self->families->len; i++)
+    {
+      PangoSimpleFamily *family = g_ptr_array_index (self->families, i);
+
+      if (strcmp (name, family->name) == 0)
+        return PANGO_FONT_FAMILY (family);
+    }
+
+  return NULL;
+}
+
+static void
+pango_simple_font_map_class_init (PangoSimpleFontMapClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  PangoFontMapClass *map_class = PANGO_FONT_MAP_CLASS (class);
+
+  object_class->finalize = pango_simple_font_map_finalize;
+
+  map_class->load_font = pango_simple_font_map_load_font;
+  map_class->load_fontset = pango_simple_font_map_load_fontset;
+  map_class->list_families = pango_simple_font_map_list_families;
+  map_class->get_family = pango_simple_font_map_get_family;
+}
+
+/* }}} */
+/* {{{ 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);
+  PangoSimpleFamily *family;
+
+  family = PANGO_SIMPLE_FAMILY (pango_font_map_get_family (map, face->family));
+  if (!family)
+    {
+      family = pango_simple_family_new (face->family,
+                                        pango_hb_face_is_monospace (face),
+                                        pango_hb_face_is_variable (face));
+      g_ptr_array_add (self->families, family);
+    }
+
+  pango_hb_face_set_font_map (face, map);
+  pango_simple_family_add_face (family, PANGO_FONT_FACE (face));
+}
+
+/**
+ * pango_simple_font_map_add_file:
+ * @file: font filename
+ * @index: face index
+ *
+ * Creates a new `PangoHbFace` and adds it.
+ */
+void
+pango_simple_font_map_add_file (PangoSimpleFontMap *self,
+                                const char         *file,
+                                unsigned int        index)
+{
+  PangoHbFace *face;
+
+  face = pango_hb_face_new_from_file (file, index);
+  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_font_map_changed (PANGO_FONT_MAP (self));
+}
+
+/* }}} */
+
+/* vim:set foldmethod=marker expandtab: */
diff --git a/pango/pango-simplefontmap.h b/pango/pango-simplefontmap.h
new file mode 100644
index 00000000..a4924fd8
--- /dev/null
+++ b/pango/pango-simplefontmap.h
@@ -0,0 +1,51 @@
+/* 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-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
+G_DECLARE_FINAL_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,
+                                                         unsigned int          index);
+
+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);
+
+G_END_DECLS
diff --git a/pango/pango.h b/pango/pango.h
index 8dc86b13..cf594e55 100644
--- a/pango/pango.h
+++ b/pango/pango.h
@@ -45,6 +45,7 @@
 #include <pango/pango-markup.h>
 #include <pango/pango-renderer.h>
 #include <pango/pango-script.h>
+#include <pango/pango-simplefontmap.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]