[pango/pango2: 79/168] Make font metrics copy/free




commit b165dbef61e1508b91f26565219c3e9d5c8dc524
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Jun 11 17:53:10 2022 -0400

    Make font metrics copy/free
    
    Having this struct ref/unref was a bit of an odd
    fit, and we were not taking advantage of it anywhere.

 pango/pango-context.c      | 35 ++++++++++++++---------------------
 pango/pango-font-metrics.c | 35 ++++++++++-------------------------
 pango/pango-font-metrics.h |  6 +++---
 pango/pango-font.c         |  2 +-
 pango/pango-fontset.c      | 22 ++++++----------------
 pango/pango-line-breaker.c |  2 +-
 pango/pango-line.c         |  2 +-
 pango/pango-renderer.c     |  2 +-
 pango/pango-run.c          |  2 +-
 tests/test-font.c          |  4 ++--
 utils/pango-list.c         |  2 +-
 11 files changed, 41 insertions(+), 73 deletions(-)
---
diff --git a/pango/pango-context.c b/pango/pango-context.c
index 90d48aef..34b19469 100644
--- a/pango/pango-context.c
+++ b/pango/pango-context.c
@@ -126,7 +126,7 @@ pango_context_finalize (GObject *object)
     pango_matrix_free (context->matrix);
 
   if (context->metrics)
-    pango_font_metrics_unref (context->metrics);
+    pango_font_metrics_free (context->metrics);
 
   G_OBJECT_CLASS (pango_context_parent_class)->finalize (object);
 }
@@ -554,31 +554,21 @@ get_first_metrics_foreach (PangoFontset  *fontset,
                            PangoFont     *font,
                            gpointer       data)
 {
-  PangoFontMetrics *fontset_metrics = data;
+  PangoFontMetrics **fontset_metrics = data;
   PangoLanguage *language = pango_fontset_get_language (fontset);
-  PangoFontMetrics *font_metrics = pango_font_get_metrics (font, language);
-  guint save_ref_count;
 
-  /* Initialize the fontset metrics to metrics of the first font in the
-   * fontset; saving the refcount and restoring it is a bit of hack but avoids
-   * having to update this code for each metrics addition.
-   */
-  save_ref_count = fontset_metrics->ref_count;
-  *fontset_metrics = *font_metrics;
-  fontset_metrics->ref_count = save_ref_count;
+  *fontset_metrics = pango_font_get_metrics (font, language);
 
-  pango_font_metrics_unref (font_metrics);
-
-  return TRUE;                  /* Stops iteration */
+  return TRUE; /* Stops iteration */
 }
 
 static PangoFontMetrics *
 get_base_metrics (PangoFontset *fontset)
 {
-  PangoFontMetrics *metrics = pango_font_metrics_new ();
+  PangoFontMetrics *metrics = NULL;
 
   /* Initialize the metrics from the first font in the fontset */
-  pango_fontset_foreach (fontset, get_first_metrics_foreach, metrics);
+  pango_fontset_foreach (fontset, get_first_metrics_foreach, &metrics);
 
   return metrics;
 }
@@ -615,7 +605,7 @@ update_metrics_from_items (PangoFontMetrics *metrics,
           metrics->ascent = MAX (metrics->ascent, raw_metrics->ascent);
           metrics->descent = MAX (metrics->descent, raw_metrics->descent);
           metrics->height = MAX (metrics->height, raw_metrics->height);
-          pango_font_metrics_unref (raw_metrics);
+          pango_font_metrics_free (raw_metrics);
         }
 
       pango_shape (text + item->offset, item->length,
@@ -658,7 +648,7 @@ update_metrics_from_items (PangoFontMetrics *metrics,
  * for the individual families.
  *
  * Return value: a `PangoFontMetrics` object. The caller must call
- *   [method@Pango.FontMetrics.unref] when finished using the object.
+ *   [method Pango FontMetrics free] when finished using the object.
  */
 PangoFontMetrics *
 pango_context_get_metrics (PangoContext               *context,
@@ -682,7 +672,7 @@ pango_context_get_metrics (PangoContext               *context,
   if (desc == context->font_desc &&
       language == context->language &&
       context->metrics != NULL)
-    return pango_font_metrics_ref (context->metrics);
+    return pango_font_metrics_copy (context->metrics);
 
   current_fonts = pango_font_map_load_fontset (context->font_map, context, desc, language);
   metrics = get_base_metrics (current_fonts);
@@ -703,7 +693,10 @@ pango_context_get_metrics (PangoContext               *context,
 
   if (desc == context->font_desc &&
       language == context->language)
-    context->metrics = pango_font_metrics_ref (metrics);
+    {
+      pango_font_metrics_free (context->metrics);
+      context->metrics = pango_font_metrics_copy (metrics);
+    }
 
   return metrics;
 }
@@ -715,7 +708,7 @@ context_changed (PangoContext *context)
   if (context->serial == 0)
     context->serial++;
 
-  g_clear_pointer (&context->metrics, pango_font_metrics_unref);
+  g_clear_pointer (&context->metrics, pango_font_metrics_free);
 }
 
 /**
diff --git a/pango/pango-font-metrics.c b/pango/pango-font-metrics.c
index f5c22129..21a7c735 100644
--- a/pango/pango-font-metrics.c
+++ b/pango/pango-font-metrics.c
@@ -25,8 +25,8 @@
 
 
 G_DEFINE_BOXED_TYPE (PangoFontMetrics, pango_font_metrics,
-                     pango_font_metrics_ref,
-                     pango_font_metrics_unref);
+                     pango_font_metrics_copy,
+                     pango_font_metrics_free);
 
 /**
  * pango_font_metrics_new:
@@ -37,54 +37,39 @@ G_DEFINE_BOXED_TYPE (PangoFontMetrics, pango_font_metrics,
  * no public way to set the fields of the structure.
  *
  * Return value: a newly-created `PangoFontMetrics` structure
- *   with a reference count of 1.
  */
 PangoFontMetrics *
 pango_font_metrics_new (void)
 {
   PangoFontMetrics *metrics = g_slice_new0 (PangoFontMetrics);
-  metrics->ref_count = 1;
 
   return metrics;
 }
 
 /**
- * pango_font_metrics_ref:
+ * pango_font_metrics_copy:
  * @metrics: (nullable): a `PangoFontMetrics` structure, may be %NULL
  *
- * Increase the reference count of a font metrics structure by one.
+ * Create a copy of @metrics.
  *
  * Return value: (nullable): @metrics
  */
 PangoFontMetrics *
-pango_font_metrics_ref (PangoFontMetrics *metrics)
+pango_font_metrics_copy (PangoFontMetrics *metrics)
 {
-  if (metrics == NULL)
-    return NULL;
-
-  g_atomic_int_inc ((int *) &metrics->ref_count);
-
-  return metrics;
+  return g_slice_dup (PangoFontMetrics, metrics);
 }
 
 /**
- * pango_font_metrics_unref:
+ * pango_font_metrics_free:
  * @metrics: (nullable): a `PangoFontMetrics` structure, may be %NULL
  *
- * Decrease the reference count of a font metrics structure by one.
- *
- * If the result is zero, frees the structure and any associated memory.
+ * Free the @metrics.
  */
 void
-pango_font_metrics_unref (PangoFontMetrics *metrics)
+pango_font_metrics_free (PangoFontMetrics *metrics)
 {
-  if (metrics == NULL)
-    return;
-
-  g_return_if_fail (metrics->ref_count > 0 );
-
-  if (g_atomic_int_dec_and_test ((int *) &metrics->ref_count))
-    g_slice_free (PangoFontMetrics, metrics);
+  g_slice_free (PangoFontMetrics, metrics);
 }
 
 /**
diff --git a/pango/pango-font-metrics.h b/pango/pango-font-metrics.h
index d046f2d1..ff919e02 100644
--- a/pango/pango-font-metrics.h
+++ b/pango/pango-font-metrics.h
@@ -51,9 +51,9 @@ typedef struct _PangoFontMetrics PangoFontMetrics;
 PANGO_AVAILABLE_IN_ALL
 GType             pango_font_metrics_get_type                    (void) G_GNUC_CONST;
 PANGO_AVAILABLE_IN_ALL
-PangoFontMetrics *pango_font_metrics_ref                         (PangoFontMetrics *metrics);
+PangoFontMetrics *pango_font_metrics_copy                        (PangoFontMetrics *metrics);
 PANGO_AVAILABLE_IN_ALL
-void              pango_font_metrics_unref                       (PangoFontMetrics *metrics);
+void              pango_font_metrics_free                        (PangoFontMetrics *metrics);
 PANGO_AVAILABLE_IN_ALL
 int               pango_font_metrics_get_ascent                  (PangoFontMetrics *metrics) G_GNUC_PURE;
 PANGO_AVAILABLE_IN_ALL
@@ -73,6 +73,6 @@ int               pango_font_metrics_get_strikethrough_position  (PangoFontMetri
 PANGO_AVAILABLE_IN_ALL
 int               pango_font_metrics_get_strikethrough_thickness (PangoFontMetrics *metrics) G_GNUC_PURE;
 
-G_DEFINE_AUTOPTR_CLEANUP_FUNC(PangoFontMetrics, pango_font_metrics_unref)
+G_DEFINE_AUTOPTR_CLEANUP_FUNC(PangoFontMetrics, pango_font_metrics_free)
 
 G_END_DECLS
diff --git a/pango/pango-font.c b/pango/pango-font.c
index 95f8a3ae..24d6add4 100644
--- a/pango/pango-font.c
+++ b/pango/pango-font.c
@@ -294,7 +294,7 @@ pango_font_get_glyph_extents  (PangoFont      *font,
  * output variables and returns.
  *
  * Return value: a `PangoFontMetrics` object. The caller must call
- *   [method@Pango.FontMetrics.unref] when finished using the object.
+ *   [method Pango FontMetrics free] when finished using the object.
  */
 PangoFontMetrics *
 pango_font_get_metrics (PangoFont     *font,
diff --git a/pango/pango-fontset.c b/pango/pango-fontset.c
index fd5af91d..d528ad8d 100644
--- a/pango/pango-fontset.c
+++ b/pango/pango-fontset.c
@@ -135,22 +135,12 @@ get_first_metrics_foreach (PangoFontset  *fontset,
                            PangoFont     *font,
                            gpointer       data)
 {
-  PangoFontMetrics *fontset_metrics = data;
+  PangoFontMetrics **fontset_metrics = data;
   PangoLanguage *language = pango_fontset_get_language (fontset);
-  PangoFontMetrics *font_metrics = pango_font_get_metrics (font, language);
-  guint save_ref_count;
 
-  /* Initialize the fontset metrics to metrics of the first font in the
-   * fontset; saving the refcount and restoring it is a bit of hack but avoids
-   * having to update this code for each metrics addition.
-   */
-  save_ref_count = fontset_metrics->ref_count;
-  *fontset_metrics = *font_metrics;
-  fontset_metrics->ref_count = save_ref_count;
+  *fontset_metrics = pango_font_get_metrics (font, language);
 
-  pango_font_metrics_unref (font_metrics);
-
-  return TRUE;                  /* Stops iteration */
+  return TRUE; /* Stops iteration */
 }
 
 static PangoFontMetrics *
@@ -168,11 +158,11 @@ pango_fontset_real_get_metrics (PangoFontset  *fontset)
   sample_str = pango_language_get_sample_string (language);
 
   count = 0;
-  metrics = pango_font_metrics_new ();
+  metrics = NULL;
   fonts_seen = g_hash_table_new_full (NULL, NULL, g_object_unref, NULL);
 
   /* Initialize the metrics from the first font in the fontset */
-  pango_fontset_foreach (fontset, get_first_metrics_foreach, metrics);
+  pango_fontset_foreach (fontset, get_first_metrics_foreach, &metrics);
 
   p = sample_str;
   while (*p)
@@ -201,7 +191,7 @@ pango_fontset_real_get_metrics (PangoFontset  *fontset)
                   metrics->approximate_digit_width += raw_metrics->approximate_digit_width;
                 }
               count++;
-              pango_font_metrics_unref (raw_metrics);
+              pango_font_metrics_free (raw_metrics);
             }
           else
             g_object_unref (font);
diff --git a/pango/pango-line-breaker.c b/pango/pango-line-breaker.c
index b9db0125..ccec0ffe 100644
--- a/pango/pango-line-breaker.c
+++ b/pango/pango-line-breaker.c
@@ -1324,7 +1324,7 @@ process_item (PangoLineBreaker *self,
    */
   metrics = pango_font_get_metrics (item->analysis.font, item->analysis.language);
   safe_distance = pango_font_metrics_get_approximate_char_width (metrics) * 3;
-  pango_font_metrics_unref (metrics);
+  pango_font_metrics_free (metrics);
 
   if (processing_new_item)
     {
diff --git a/pango/pango-line.c b/pango/pango-line.c
index f9382ea5..775a25ba 100644
--- a/pango/pango-line.c
+++ b/pango/pango-line.c
@@ -511,7 +511,7 @@ pango_line_get_empty_extents (PangoLine        *line,
                 logical_rect->height += (leading - leading / 2);
             }
 
-          pango_font_metrics_unref (metrics);
+          pango_font_metrics_free (metrics);
         }
 
       g_object_unref (font);
diff --git a/pango/pango-renderer.c b/pango/pango-renderer.c
index f3318b96..4ef9b873 100644
--- a/pango/pango-renderer.c
+++ b/pango/pango-renderer.c
@@ -679,7 +679,7 @@ pango_renderer_draw_runs (PangoRenderer *renderer,
                                x + x_off, y - y_off,
                                ink, logical, glyphs->num_glyphs);
 
-          pango_font_metrics_unref (metrics);
+          pango_font_metrics_free (metrics);
         }
 
       if (renderer->underline == PANGO_LINE_STYLE_NONE &&
diff --git a/pango/pango-run.c b/pango/pango-run.c
index 5f04be8a..17cb237e 100644
--- a/pango/pango-run.c
+++ b/pango/pango-run.c
@@ -200,5 +200,5 @@ pango_run_get_extents (PangoRun         *run,
     }
 
   if (metrics)
-    pango_font_metrics_unref (metrics);
+    pango_font_metrics_free (metrics);
 }
diff --git a/tests/test-font.c b/tests/test-font.c
index 72809f57..956c9580 100644
--- a/tests/test-font.c
+++ b/tests/test-font.c
@@ -185,7 +185,7 @@ test_metrics (void)
   g_test_message ("\tstrikethrough thickness: %d",
                   pango_font_metrics_get_strikethrough_thickness (metrics));
 
-  pango_font_metrics_unref (metrics);
+  pango_font_metrics_free (metrics);
   g_free (str);
   pango_font_description_free (desc);
 }
@@ -497,7 +497,7 @@ test_font_metrics (void)
   g_assert_cmpint (pango_font_metrics_get_approximate_char_width (metrics), ==, PANGO_SCALE * 
PANGO_UNKNOWN_GLYPH_WIDTH);
   g_assert_cmpint (pango_font_metrics_get_approximate_digit_width (metrics), ==, PANGO_SCALE * 
PANGO_UNKNOWN_GLYPH_WIDTH);
 
-  pango_font_metrics_unref (metrics);
+  pango_font_metrics_free (metrics);
 }
 
 static void
diff --git a/utils/pango-list.c b/utils/pango-list.c
index e32cdfc5..9d69a82c 100644
--- a/utils/pango-list.c
+++ b/utils/pango-list.c
@@ -204,7 +204,7 @@ main (int    argc,
                        pango_font_metrics_get_underline_thickness (metrics),
                        pango_font_metrics_get_strikethrough_position (metrics),
                        pango_font_metrics_get_strikethrough_thickness (metrics));
-              pango_font_metrics_unref (metrics);
+              pango_font_metrics_free (metrics);
             }
 
           if (opt_variations)


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