[pango/pango2: 54/168] Apply font options




commit 84710b7284244de1eba4c21387c2e06dc2d3b361
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Jun 9 00:47:43 2022 -0400

    Apply font options
    
    Arrange for font options to be passed through
    from the PangoContext to where we create a
    cairo_scaled_font_t.

 pango/meson.build                    |  1 +
 pango/pango-font-private.h           |  6 ++++
 pango/pango-fontmap.c                | 23 +++++++++++--
 pango/pango-fontset-cached-private.h |  8 ++++-
 pango/pango-fontset-cached.c         | 46 ++++++++++++++++++++++----
 pango/pangocairo-context.c           |  3 +-
 pango/pangocairo-font.c              | 63 ++++++++++++++++++++++++++++++++++--
 pango/pangocairo-font.h              | 34 +++++++++++++++++++
 pango/pangocairo-private.h           |  4 +--
 pango/pangocairo.h                   |  1 +
 10 files changed, 172 insertions(+), 17 deletions(-)
---
diff --git a/pango/meson.build b/pango/meson.build
index 862816d7..eab49200 100644
--- a/pango/meson.build
+++ b/pango/meson.build
@@ -102,6 +102,7 @@ if cairo_dep.found()
   pango_headers += [
     'pangocairo.h',
     'pangocairo-context.h',
+    'pangocairo-font.h',
     'pangocairo-render.h',
   ]
 
diff --git a/pango/pango-font-private.h b/pango/pango-font-private.h
index c6739820..0c42d5a6 100644
--- a/pango/pango-font-private.h
+++ b/pango/pango-font-private.h
@@ -25,12 +25,18 @@
 
 #include <glib-object.h>
 
+#ifdef HAVE_CAIRO
+#include <cairo.h>
+#endif
 
 struct _PangoFont
 {
   GObject parent_instance;
 
   hb_font_t *hb_font;
+#ifdef HAVE_CAIRO
+  cairo_font_options_t *options;
+#endif
 };
 
 typedef struct _PangoFontClass       PangoFontClass;
diff --git a/pango/pango-fontmap.c b/pango/pango-fontmap.c
index 6d68d07d..8eebcce3 100644
--- a/pango/pango-fontmap.c
+++ b/pango/pango-fontmap.c
@@ -36,18 +36,23 @@
 #include "pango-trace-private.h"
 #include "pango-context.h"
 
-#if defined (HAVE_CORE_TEXT)
+#ifdef HAVE_CORE_TEXT
 #include "pangocoretext-fontmap.h"
 #endif
 
-#if defined (HAVE_DIRECT_WRITE)
+#ifdef HAVE_DIRECT_WRITE
 #include "pangodwrite-fontmap.h"
 #endif
 
-#if defined (HAVE_FONTCONFIG)
+#ifdef HAVE_FONTCONFIG
 #include "pangofc-fontmap.h"
 #endif
 
+#ifdef HAVE_CAIRO
+#include <cairo.h>
+#include <pangocairo-private.h>
+#endif
+
 #include <hb-ot.h>
 
 
@@ -135,6 +140,9 @@ pango_fontset_cached_hash (const PangoFontsetCached *fontset)
 
   return (hash ^
           GPOINTER_TO_UINT (fontset->language) ^
+#ifdef HAVE_CAIRO
+          cairo_font_options_hash (fontset->font_options) ^
+#endif
           pango_font_description_hash (fontset->description));
 }
 
@@ -143,6 +151,9 @@ pango_fontset_cached_equal (const PangoFontsetCached *a,
                             const PangoFontsetCached *b)
 {
   return a->language == b->language &&
+#ifdef HAVE_CAIRO
+         cairo_font_options_equal (a->font_options, b->font_options) &&
+#endif
          pango_font_description_equal (a->description, b->description);
 }
 
@@ -417,6 +428,9 @@ pango_font_map_default_load_fontset (PangoFontMap               *self,
 
   lookup.language = language;
   lookup.description = (PangoFontDescription *)description;
+#ifdef HAVE_CAIRO
+  lookup.font_options = (cairo_font_options_t *)pango_cairo_context_get_merged_font_options (context);
+#endif
   fontset = g_hash_table_lookup (self->fontsets, &lookup);
 
   if (fontset)
@@ -425,6 +439,9 @@ pango_font_map_default_load_fontset (PangoFontMap               *self,
   matrix = pango_context_get_matrix (context);
 
   fontset = pango_fontset_cached_new (description, language, self->dpi, matrix);
+#ifdef HAVE_CAIRO
+  fontset->font_options = cairo_font_options_copy (pango_cairo_context_get_merged_font_options (context));
+#endif
 
   if (self->families->len == 0)
     {
diff --git a/pango/pango-fontset-cached-private.h b/pango/pango-fontset-cached-private.h
index ec8e7617..ccf28ceb 100644
--- a/pango/pango-fontset-cached-private.h
+++ b/pango/pango-fontset-cached-private.h
@@ -24,6 +24,9 @@
 #include <pango/pango-generic-family.h>
 #include <glib-object.h>
 
+#ifdef HAVE_CAIRO
+#include <cairo.h>
+#endif
 
 typedef struct _PangoFontsetCached PangoFontsetCached;
 typedef struct _PangoFontsetCachedClass  PangoFontsetCachedClass;
@@ -39,6 +42,10 @@ struct _PangoFontsetCached
   const PangoMatrix *matrix;
   GList cache_link;
   GHashTable *cache;
+
+#ifdef HAVE_CAIRO
+  cairo_font_options_t *font_options;
+#endif
 };
 
 struct _PangoFontsetCachedClass
@@ -60,4 +67,3 @@ void                 pango_fontset_cached_add_family     (PangoFontsetCached
                                                           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
index 28194948..06d7a502 100644
--- a/pango/pango-fontset-cached.c
+++ b/pango/pango-fontset-cached.c
@@ -27,6 +27,9 @@
 #include "pango-font-face-private.h"
 #include "pango-generic-family-private.h"
 
+#ifdef HAVE_CAIRO
+#include "pangocairo-font.h"
+#endif
 
 G_DEFINE_TYPE (PangoFontsetCached, pango_fontset_cached, PANGO_TYPE_FONTSET);
 
@@ -36,6 +39,9 @@ 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;
+#ifdef HAVE_CAIRO
+  fontset->font_options = NULL;
+#endif
 }
 
 static void
@@ -46,6 +52,10 @@ pango_fontset_cached_finalize (GObject *object)
   g_ptr_array_free (self->items, TRUE);
   g_hash_table_unref (self->cache);
   pango_font_description_free (self->description);
+#ifdef HAVE_CAIRO
+  if (self->font_options)
+    cairo_font_options_destroy (self->font_options);
+#endif
 
   G_OBJECT_CLASS (pango_fontset_cached_parent_class)->finalize (object);
 }
@@ -104,10 +114,15 @@ pango_fontset_cached_get_font (PangoFontset *fontset,
                 }
 
               if (!retval)
-                retval = pango_font_face_create_font (face,
-                                                      self->description,
-                                                      self->dpi,
-                                                      self->matrix);
+                {
+                  retval = pango_font_face_create_font (face,
+                                                        self->description,
+                                                        self->dpi,
+                                                        self->matrix);
+#ifdef HAVE_CAIRO
+                  pango_cairo_font_set_font_options (retval, self->font_options);
+#endif
+                }
               break;
             }
         }
@@ -130,8 +145,21 @@ pango_fontset_cached_get_first_font (PangoFontsetCached *self)
     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);
+      PangoFontFace *face;
+      PangoFont *font;
+
+      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);
+#ifdef HAVE_CAIRO
+      pango_cairo_font_set_font_options (font, self->font_options);
+#endif
+      return font;
     }
 
   return NULL;
@@ -185,6 +213,9 @@ pango_fontset_cached_foreach (PangoFontset            *fontset,
         {
           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);
+#ifdef HAVE_CAIRO
+          pango_cairo_font_set_font_options (font, self->font_options);
+#endif
         }
 
       if ((*func) (fontset, font, data))
@@ -237,6 +268,9 @@ pango_fontset_cached_add_face (PangoFontsetCached *self,
                                       self->description,
                                       self->dpi,
                                       self->matrix);
+#ifdef HAVE_CAIRO
+  pango_cairo_font_set_font_options (font, self->font_options);
+#endif
   g_ptr_array_add (self->items, font);
 }
 
diff --git a/pango/pangocairo-context.c b/pango/pangocairo-context.c
index daa122ff..98eef290 100644
--- a/pango/pangocairo-context.c
+++ b/pango/pangocairo-context.c
@@ -185,8 +185,7 @@ pango_cairo_context_get_merged_font_options (PangoContext *context)
       context->merged_options = cairo_font_options_create ();
 
       if (context->surface_options)
-        cairo_font_options_merge (context->merged_options, context->surface_options)
-;
+        cairo_font_options_merge (context->merged_options, context->surface_options);
       if (context->set_options)
         cairo_font_options_merge (context->merged_options, context->set_options);
     }
diff --git a/pango/pangocairo-font.c b/pango/pangocairo-font.c
index faa1fda4..865dfe22 100644
--- a/pango/pangocairo-font.c
+++ b/pango/pangocairo-font.c
@@ -33,6 +33,7 @@
 #include "pango-hbface-private.h"
 #include "pango-userfont-private.h"
 #include "pango-userface-private.h"
+#include "pango-font-private.h"
 
 #pragma GCC diagnostic push
 #pragma GCC diagnostic ignored "-Wundef"
@@ -578,9 +579,15 @@ _pango_font_get_cairo_font_private (PangoFont *font)
                           x_scale * size / (double)PANGO_SCALE,
                           y_scale * size / (double)PANGO_SCALE);
 
-      font_options = cairo_font_options_create ();
-      cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
-      cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
+      font_options = (cairo_font_options_t *)pango_cairo_font_get_font_options (font);
+      if (font_options)
+        font_options = cairo_font_options_copy (font_options);
+      else
+        {
+          font_options = cairo_font_options_create ();
+          cairo_font_options_set_hint_style (font_options, CAIRO_HINT_STYLE_NONE);
+          cairo_font_options_set_hint_metrics (font_options, CAIRO_HINT_METRICS_OFF);
+        }
 
       cf_priv = g_new0 (PangoCairoFontPrivate, 1);
       _pango_cairo_font_private_initialize (cf_priv,
@@ -661,3 +668,53 @@ _pango_cairo_font_private_finalize (PangoCairoFontPrivate *cf_priv)
   _pango_cairo_font_hex_box_info_destroy (cf_priv->hbi);
   cf_priv->hbi = NULL;
 }
+
+/**
+ * pango_cairo_font_set_font_options:
+ * @font: a `PangoFont`
+ * @options: (nullable): a `cairo_font_options_t`, or %NULL to unset
+ *   any previously set options. A copy is made.
+ *
+ * Sets the font options used when rendering text with this font.
+ *
+ * This is rarely needed. Fonts usually get font options from the
+ * `PangoContext` in which they are loaded.
+ */
+void
+pango_cairo_font_set_font_options (PangoFont                  *font,
+                                   const cairo_font_options_t *options)
+{
+  g_return_if_fail (PANGO_IS_FONT (font));
+
+  if (!font->options && !options)
+    return;
+
+  if (font->options && options &&
+      cairo_font_options_equal (font->options, options))
+    return;
+
+  if (font->options)
+    cairo_font_options_destroy (font->options);
+
+  if (options)
+    font->options = cairo_font_options_copy (options);
+  else
+    font->options = NULL;
+}
+
+/**
+ * pango_cairo_font_get_font_options:
+ * @font: a `PangoFont`
+ *
+ * Gets font options for the font.
+ *
+ * Returns: (transfer none): font options that are
+ *   applied when rendering text with this font
+ */
+const cairo_font_options_t *
+pango_cairo_font_get_font_options (PangoFont *font)
+{
+  g_return_val_if_fail (PANGO_IS_FONT (font), NULL);
+
+  return font->options;
+}
diff --git a/pango/pangocairo-font.h b/pango/pangocairo-font.h
new file mode 100644
index 00000000..a8a7a1e9
--- /dev/null
+++ b/pango/pangocairo-font.h
@@ -0,0 +1,34 @@
+/*
+ * Copyright (C) 1999, 2004 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.h>
+#include <cairo.h>
+
+G_BEGIN_DECLS
+
+PANGO_AVAILABLE_IN_ALL
+void                    pango_cairo_font_set_font_options       (PangoFont                      *font,
+                                                                 const cairo_font_options_t     *options);
+PANGO_AVAILABLE_IN_ALL
+const cairo_font_options_t *
+                        pango_cairo_font_get_font_options       (PangoFont                      *font);
+
+G_END_DECLS
diff --git a/pango/pangocairo-private.h b/pango/pangocairo-private.h
index 23e3c49f..0f1ce314 100644
--- a/pango/pangocairo-private.h
+++ b/pango/pangocairo-private.h
@@ -61,5 +61,5 @@ typedef struct _PangoCairoRenderer PangoCairoRenderer;
 _PANGO_EXTERN
 GType pango_cairo_renderer_get_type    (void) G_GNUC_CONST;
 
-
-const cairo_font_options_t *pango_cairo_context_get_merged_font_options (PangoContext *context);
+const cairo_font_options_t *
+         pango_cairo_context_get_merged_font_options (PangoContext *context);
diff --git a/pango/pangocairo.h b/pango/pangocairo.h
index a2658f82..1ffa4bbd 100644
--- a/pango/pangocairo.h
+++ b/pango/pangocairo.h
@@ -20,4 +20,5 @@
 #pragma once
 
 #include <pango/pangocairo-context.h>
+#include <pango/pangocairo-font.h>
 #include <pango/pangocairo-render.h>


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