[pango/pango2: 113/115] Store cairo_font_options in the context




commit fb4fb2629f509f130d2a42ac971e455a38b4696d
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Jun 9 00:03:14 2022 -0400

    Store cairo_font_options in the context
    
    Now that everything lives in the same shared
    object, this is easy.

 pango/pango-context-private.h |  11 +++
 pango/pangocairo-context.c    | 189 ++++++++++--------------------------------
 2 files changed, 55 insertions(+), 145 deletions(-)
---
diff --git a/pango/pango-context-private.h b/pango/pango-context-private.h
index 69c2287b..d9d07712 100644
--- a/pango/pango-context-private.h
+++ b/pango/pango-context-private.h
@@ -21,6 +21,9 @@
 
 #include <pango/pango-context.h>
 
+#ifdef HAVE_CAIRO
+#include <cairo.h>
+#endif
 
 struct _PangoContext
 {
@@ -44,4 +47,12 @@ struct _PangoContext
   PangoFontMetrics *metrics;
 
   gboolean round_glyph_positions;
+
+#ifdef HAVE_CAIRO
+  gboolean set_options_explicit;
+
+  cairo_font_options_t *set_options;
+  cairo_font_options_t *surface_options;
+  cairo_font_options_t *merged_options;
+#endif
 };
diff --git a/pango/pangocairo-context.c b/pango/pangocairo-context.c
index e9a3a908..8de11a7f 100644
--- a/pango/pangocairo-context.c
+++ b/pango/pangocairo-context.c
@@ -24,67 +24,26 @@
 #include "pangocairo.h"
 #include "pangocairo-private.h"
 #include "pango-impl-utils.h"
+#include "pango-context-private.h"
 
 #include <string.h>
 
-typedef struct _PangoCairoContextInfo PangoCairoContextInfo;
-
-struct _PangoCairoContextInfo
-{
-  gboolean set_options_explicit;
-
-  cairo_font_options_t *set_options;
-  cairo_font_options_t *surface_options;
-  cairo_font_options_t *merged_options;
-};
-
-static void
-free_context_info (PangoCairoContextInfo *info)
-{
-  if (info->set_options)
-    cairo_font_options_destroy (info->set_options);
-  if (info->surface_options)
-    cairo_font_options_destroy (info->surface_options);
-  if (info->merged_options)
-    cairo_font_options_destroy (info->merged_options);
-
-  g_slice_free (PangoCairoContextInfo, info);
-}
-
-static PangoCairoContextInfo *
-get_context_info (PangoContext *context,
-                  gboolean      create)
-{
-  static GQuark context_info_quark; /* MT-safe */
-  PangoCairoContextInfo *info;
-
-  if (G_UNLIKELY (!context_info_quark))
-    context_info_quark = g_quark_from_static_string ("pango-cairo-context-info");
-
-retry:
-  info = g_object_get_qdata (G_OBJECT (context), context_info_quark);
-
-  if (G_UNLIKELY (!info) && create)
-    {
-      info = g_slice_new0 (PangoCairoContextInfo);
-
-      if (!g_object_replace_qdata (G_OBJECT (context), context_info_quark, NULL,
-                                   info, (GDestroyNotify)free_context_info,
-                                   NULL))
-        {
-          free_context_info (info);
-          goto retry;
-        }
-    }
-
-  return info;
-}
-
-static void
-_pango_cairo_update_context (cairo_t      *cr,
-                             PangoContext *context)
+/**
+ * pango_cairo_update_context:
+ * @cr: a Cairo context
+ * @context: a `PangoContext`, from a pangocairo font map
+ *
+ * Updates a `PangoContext` previously created for use with Cairo to
+ * match the current transformation and target surface of a Cairo
+ * context.
+ *
+ * If any layouts have been created for the context, it's necessary
+ * to call [method@Pango.Layout.context_changed] on those layouts.
+ */
+void
+pango_cairo_update_context (cairo_t      *cr,
+                            PangoContext *context)
 {
-  PangoCairoContextInfo *info;
   cairo_matrix_t cairo_matrix;
   cairo_surface_t *target;
   PangoMatrix pango_matrix;
@@ -93,22 +52,23 @@ _pango_cairo_update_context (cairo_t      *cr,
   cairo_font_options_t *old_merged_options;
   gboolean changed = FALSE;
 
-  info = get_context_info (context, TRUE);
+  g_return_if_fail (cr != NULL);
+  g_return_if_fail (PANGO_IS_CONTEXT (context));
 
   target = cairo_get_target (cr);
 
-  if (!info->surface_options)
-    info->surface_options = cairo_font_options_create ();
-  cairo_surface_get_font_options (target, info->surface_options);
-  if (!info->set_options_explicit)
+  if (!context->surface_options)
+    context->surface_options = cairo_font_options_create ();
+  cairo_surface_get_font_options (target, context->surface_options);
+  if (!context->set_options_explicit)
   {
-    if (!info->set_options)
-      info->set_options = cairo_font_options_create ();
-    cairo_get_font_options (cr, info->set_options);
+    if (!context->set_options)
+      context->set_options = cairo_font_options_create ();
+    cairo_get_font_options (cr, context->set_options);
   }
 
-  old_merged_options = info->merged_options;
-  info->merged_options = NULL;
+  old_merged_options = context->merged_options;
+  context->merged_options = NULL;
 
   merged_options = _pango_cairo_context_get_merged_font_options (context);
 
@@ -135,7 +95,8 @@ _pango_cairo_update_context (cairo_t      *cr,
     current_matrix = &identity_matrix;
 
   /* layout is matrix-independent if metrics-hinting is off.
-   * also ignore matrix translation offsets */
+   * also ignore matrix translation offsets
+   */
   if ((cairo_font_options_get_hint_metrics (merged_options) != CAIRO_HINT_METRICS_OFF) &&
       (0 != memcmp (&pango_matrix, current_matrix, sizeof (PangoMatrix))))
     changed = TRUE;
@@ -146,28 +107,6 @@ _pango_cairo_update_context (cairo_t      *cr,
     pango_context_changed (context);
 }
 
-/**
- * pango_cairo_update_context:
- * @cr: a Cairo context
- * @context: a `PangoContext`, from a pangocairo font map
- *
- * Updates a `PangoContext` previously created for use with Cairo to
- * match the current transformation and target surface of a Cairo
- * context.
- *
- * If any layouts have been created for the context, it's necessary
- * to call [method@Pango.Layout.context_changed] on those layouts.
- */
-void
-pango_cairo_update_context (cairo_t      *cr,
-                            PangoContext *context)
-{
-  g_return_if_fail (cr != NULL);
-  g_return_if_fail (PANGO_IS_CONTEXT (context));
-
-  _pango_cairo_update_context (cr, context);
-}
-
 /**
  * pango_cairo_context_set_font_options:
  * @context: a `PangoContext`, from a pangocairo font map
@@ -183,40 +122,36 @@ void
 pango_cairo_context_set_font_options (PangoContext               *context,
                                       const cairo_font_options_t *options)
 {
-  PangoCairoContextInfo *info;
-
   g_return_if_fail (PANGO_IS_CONTEXT (context));
 
-  info = get_context_info (context, TRUE);
-
-  if (!info->set_options && !options)
+  if (!context->set_options && !options)
     return;
 
-  if (info->set_options && options &&
-      cairo_font_options_equal (info->set_options, options))
+  if (context->set_options && options &&
+      cairo_font_options_equal (context->set_options, options))
     return;
 
-  if (info->set_options || options)
+  if (context->set_options || options)
     pango_context_changed (context);
 
- if (info->set_options)
-    cairo_font_options_destroy (info->set_options);
+ if (context->set_options)
+    cairo_font_options_destroy (context->set_options);
 
   if (options)
   {
-    info->set_options = cairo_font_options_copy (options);
-    info->set_options_explicit = TRUE;
+    context->set_options = cairo_font_options_copy (options);
+    context->set_options_explicit = TRUE;
   }
   else
   {
-    info->set_options = NULL;
-    info->set_options_explicit = FALSE;
+    context->set_options = NULL;
+    context->set_options_explicit = FALSE;
   }
 
-  if (info->merged_options)
+  if (context->merged_options)
     {
-      cairo_font_options_destroy (info->merged_options);
-      info->merged_options = NULL;
+      cairo_font_options_destroy (context->merged_options);
+      context->merged_options = NULL;
     }
 }
 
@@ -237,45 +172,9 @@ pango_cairo_context_set_font_options (PangoContext               *context,
 const cairo_font_options_t *
 pango_cairo_context_get_font_options (PangoContext *context)
 {
-  PangoCairoContextInfo *info;
-
   g_return_val_if_fail (PANGO_IS_CONTEXT (context), NULL);
 
-  info = get_context_info (context, FALSE);
-
-  if (info)
-    return info->set_options;
-  else
-    return NULL;
-}
-
-/**
- * _pango_cairo_context_merge_font_options:
- * @context: a `PangoContext`
- * @options: a `cairo_font_options_t`
- *
- * Merge together options from the target surface and explicitly set
- * on the context.
- *
- * Return value: the combined set of font options. This value is owned
- *   by the context and must not be modified or freed.
- */
-const cairo_font_options_t *
-_pango_cairo_context_get_merged_font_options (PangoContext *context)
-{
-  PangoCairoContextInfo *info = get_context_info (context, TRUE);
-
-  if (!info->merged_options)
-    {
-      info->merged_options = cairo_font_options_create ();
-
-      if (info->surface_options)
-        cairo_font_options_merge (info->merged_options, info->surface_options);
-      if (info->set_options)
-        cairo_font_options_merge (info->merged_options, info->set_options);
-    }
-
-  return info->merged_options;
+  return context->set_options;
 }
 
 /**
@@ -326,5 +225,5 @@ pango_cairo_update_layout (cairo_t     *cr,
   g_return_if_fail (cr != NULL);
   g_return_if_fail (PANGO_IS_LAYOUT (layout));
 
-  _pango_cairo_update_context (cr, pango_layout_get_context (layout));
+  pango_cairo_update_context (cr, pango_layout_get_context (layout));
 }


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