[pango] [cairo] Refactor common code in create_metrics_for_context()



commit 34e05035af0ce854df1cc2f77c0b11dbc1a3cb36
Author: Behdad Esfahbod <behdad behdad org>
Date:   Tue May 4 11:55:27 2010 -0400

    [cairo] Refactor common code in create_metrics_for_context()

 pango/pangocairo-atsuifont.c |   31 +++++--------------------
 pango/pangocairo-fcfont.c    |    8 +++---
 pango/pangocairo-font.c      |   48 ++++++++++++++++++++++++++++++++++++++-
 pango/pangocairo-private.h   |    4 +-
 pango/pangocairo-win32font.c |   31 +++++--------------------
 pango/pangofc-font.c         |   51 ++---------------------------------------
 pango/pangofc-private.h      |    4 +-
 7 files changed, 70 insertions(+), 107 deletions(-)
---
diff --git a/pango/pangocairo-atsuifont.c b/pango/pangocairo-atsuifont.c
index 3abafff..1c1ba55 100644
--- a/pango/pangocairo-atsuifont.c
+++ b/pango/pangocairo-atsuifont.c
@@ -46,15 +46,15 @@ struct _PangoCairoATSUIFontClass
 
 
 
-static cairo_font_face_t *pango_cairo_atsui_font_create_font_face           (PangoCairoFont *font);
-static PangoFontMetrics  *pango_cairo_atsui_font_create_metrics_for_context (PangoCairoFont *font,
-                                                                            PangoContext    *context);
+static cairo_font_face_t *pango_cairo_atsui_font_create_font_face                (PangoCairoFont *font);
+static PangoFontMetrics  *pango_cairo_atsui_font_create_base_metrics_for_context (PangoCairoFont *font,
+										  PangoContext    *context);
 
 static void
 cairo_font_iface_init (PangoCairoFontIface *iface)
 {
   iface->create_font_face = pango_cairo_atsui_font_create_font_face;
-  iface->create_metrics_for_context = pango_cairo_atsui_font_create_metrics_for_context;
+  iface->create_base_metrics_for_context = pango_cairo_atsui_font_create_base_metrics_for_context;
   iface->cf_priv_offset = G_STRUCT_OFFSET (PangoCairoATSUIFont, cf_priv);
 }
 
@@ -113,19 +113,14 @@ max_glyph_width (PangoLayout *layout)
 }
 
 static PangoFontMetrics *
-pango_cairo_atsui_font_create_metrics_for_context (PangoCairoFont *font,
-                                                   PangoContext   *context)
+pango_cairo_atsui_font_create_base_metrics_for_context (PangoCairoFont *font,
+							PangoContext   *context)
 {
   PangoCairoATSUIFont *cafont = (PangoCairoATSUIFont *) font;
   PangoATSUIFont *afont = (PangoATSUIFont *) font;
   ATSFontRef ats_font;
   ATSFontMetrics ats_metrics;
   PangoFontMetrics *metrics;
-  PangoFontDescription *font_desc;
-  PangoLayout *layout;
-  PangoRectangle extents;
-  PangoLanguage *language = pango_context_get_language (context);
-  const char *sample_str = pango_language_get_sample_string (language);
 
   metrics = pango_font_metrics_new ();
 
@@ -141,20 +136,6 @@ pango_cairo_atsui_font_create_metrics_for_context (PangoCairoFont *font,
   metrics->strikethrough_position = metrics->ascent / 3;
   metrics->strikethrough_thickness = ats_metrics.underlineThickness * cafont->size * PANGO_SCALE;
 
-  layout = pango_layout_new (context);
-  font_desc = pango_font_describe_with_absolute_size ((PangoFont *) font);
-  pango_layout_set_font_description (layout, font_desc);
-  pango_layout_set_text (layout, sample_str, -1);
-  pango_layout_get_extents (layout, NULL, &extents);
-
-  metrics->approximate_char_width = extents.width / pango_utf8_strwidth (sample_str);
-
-  pango_layout_set_text (layout, "0123456789", -1);
-  metrics->approximate_digit_width = max_glyph_width (layout);
-
-  pango_font_description_free (font_desc);
-  g_object_unref (layout);
-
   return metrics;
 }
 
diff --git a/pango/pangocairo-fcfont.c b/pango/pangocairo-fcfont.c
index 8401ed8..1393ce8 100644
--- a/pango/pangocairo-fcfont.c
+++ b/pango/pangocairo-fcfont.c
@@ -64,19 +64,19 @@ pango_cairo_fc_font_create_font_face (PangoCairoFont *cfont)
 }
 
 static PangoFontMetrics *
-pango_cairo_fc_font_create_metrics_for_context (PangoCairoFont *cfont,
-					        PangoContext   *context)
+pango_cairo_fc_font_create_base_metrics_for_context (PangoCairoFont *cfont,
+						     PangoContext   *context)
 {
   PangoFcFont *fcfont = (PangoFcFont *) (cfont);
 
-  return pango_fc_font_create_metrics_for_context (fcfont, context);
+  return pango_fc_font_create_base_metrics_for_context (fcfont, context);
 }
 
 static void
 cairo_font_iface_init (PangoCairoFontIface *iface)
 {
   iface->create_font_face = pango_cairo_fc_font_create_font_face;
-  iface->create_metrics_for_context = pango_cairo_fc_font_create_metrics_for_context;
+  iface->create_base_metrics_for_context = pango_cairo_fc_font_create_base_metrics_for_context;
   iface->cf_priv_offset = G_STRUCT_OFFSET (PangoCairoFcFont, cf_priv);
 }
 
diff --git a/pango/pangocairo-font.c b/pango/pangocairo-font.c
index 35da71c..ace2a57 100644
--- a/pango/pangocairo-font.c
+++ b/pango/pangocairo-font.c
@@ -206,6 +206,30 @@ _pango_cairo_font_install (PangoFont *font,
 }
 
 
+static int
+max_glyph_width (PangoLayout *layout)
+{
+  int max_width = 0;
+  GSList *l, *r;
+
+  for (l = pango_layout_get_lines_readonly (layout); l; l = l->next)
+    {
+      PangoLayoutLine *line = l->data;
+
+      for (r = line->runs; r; r = r->next)
+	{
+	  PangoGlyphString *glyphs = ((PangoGlyphItem *)r->data)->glyphs;
+	  int i;
+
+	  for (i = 0; i < glyphs->num_glyphs; i++)
+	    if (glyphs->glyphs[i].geometry.width > max_width)
+	      max_width = glyphs->glyphs[i].geometry.width;
+	}
+    }
+
+  return max_width;
+}
+
 typedef struct _PangoCairoFontMetricsInfo
 {
   const char       *sample_str;
@@ -239,6 +263,10 @@ _pango_cairo_font_get_metrics (PangoFont     *font,
       PangoFontMap *fontmap;
       PangoContext *context;
       cairo_font_options_t *font_options;
+      PangoLayout *layout;
+      PangoRectangle extents;
+      PangoFontDescription *desc;
+
       int height, shift;
 
       /* XXX this is racy.  need a ref'ing getter... */
@@ -260,7 +288,25 @@ _pango_cairo_font_get_metrics (PangoFont     *font,
       pango_cairo_context_set_font_options (context, font_options);
       cairo_font_options_destroy (font_options);
 
-      info->metrics = (* PANGO_CAIRO_FONT_GET_IFACE (font)->create_metrics_for_context) (cfont, context);
+      info->metrics = (* PANGO_CAIRO_FONT_GET_IFACE (font)->create_base_metrics_for_context) (cfont, context);
+
+      /* Update approximate_*_width now */
+
+      layout = pango_layout_new (context);
+      desc = pango_font_describe_with_absolute_size (font);
+      pango_layout_set_font_description (layout, desc);
+      pango_font_description_free (desc);
+
+      pango_layout_set_text (layout, sample_str, -1);
+      pango_layout_get_extents (layout, NULL, &extents);
+
+      info->metrics->approximate_char_width = extents.width / pango_utf8_strwidth (sample_str);
+
+      pango_layout_set_text (layout, "0123456789", -1);
+      info->metrics->approximate_digit_width = max_glyph_width (layout);
+
+      g_object_unref (layout);
+
 
       /* We may actually reuse ascent/descent we got from cairo here.  that's
        * in cf_priv->font_extents.
diff --git a/pango/pangocairo-private.h b/pango/pangocairo-private.h
index 3a4a9d0..4e64f25 100644
--- a/pango/pangocairo-private.h
+++ b/pango/pangocairo-private.h
@@ -95,8 +95,8 @@ struct _PangoCairoFontIface
   GTypeInterface g_iface;
 
   cairo_font_face_t *(*create_font_face) (PangoCairoFont *cfont);
-  PangoFontMetrics *(*create_metrics_for_context) (PangoCairoFont *cfont,
-						   PangoContext   *context);
+  PangoFontMetrics *(*create_base_metrics_for_context) (PangoCairoFont *cfont,
+							PangoContext   *context);
 
   gssize cf_priv_offset;
 };
diff --git a/pango/pangocairo-win32font.c b/pango/pangocairo-win32font.c
index 3493d58..59f7c0f 100644
--- a/pango/pangocairo-win32font.c
+++ b/pango/pangocairo-win32font.c
@@ -54,16 +54,16 @@ struct _PangoCairoWin32FontClass
 
 GType pango_cairo_win32_font_get_type (void);
 
-static cairo_font_face_t *pango_cairo_win32_font_create_font_face           (PangoCairoFont *font);
-static PangoFontMetrics  *pango_cairo_win32_font_create_metrics_for_context (PangoCairoFont *font,
-									     PangoContext    *context);
+static cairo_font_face_t *pango_cairo_win32_font_create_font_face                (PangoCairoFont *font);
+static PangoFontMetrics  *pango_cairo_win32_font_create_base_metrics_for_context (PangoCairoFont *font,
+										  PangoContext    *context);
 
 
 static void
 cairo_font_iface_init (PangoCairoFontIface *iface)
 {
   iface->create_font_face = pango_cairo_win32_font_create_font_face;
-  iface->create_metrics_for_context = pango_cairo_win32_font_create_metrics_for_context;
+  iface->create_base_metrics_for_context = pango_cairo_win32_font_create_base_metrics_for_context;
   iface->cf_priv_offset = G_STRUCT_OFFSET (PangoCairoWin32Font, cf_priv);
 }
 
@@ -104,15 +104,10 @@ max_glyph_width (PangoLayout *layout)
 }
 
 static PangoFontMetrics *
-pango_cairo_win32_font_create_metrics_for_context (PangoCairoFont *font,
-                                                   PangoContext   *context)
+pango_cairo_win32_font_create_base_metrics_for_context (PangoCairoFont *font,
+							PangoContext   *context)
 {
   PangoFontMetrics *metrics;
-  PangoFontDescription *font_desc;
-  PangoLayout *layout;
-  PangoRectangle extents;
-  PangoLanguage *language = pango_context_get_language (context);
-  const char *sample_str = pango_language_get_sample_string (language);
   cairo_scaled_font_t *scaled_font;
   cairo_font_extents_t font_extents;
   double height;
@@ -144,20 +139,6 @@ pango_cairo_win32_font_create_metrics_for_context (PangoCairoFont *font,
   if (metrics->underline_position == 0)
     metrics->underline_position = - metrics->underline_thickness;
 
-  layout = pango_layout_new (context);
-  font_desc = pango_font_describe_with_absolute_size ((PangoFont *) font);
-  pango_layout_set_font_description (layout, font_desc);
-  pango_layout_set_text (layout, sample_str, -1);
-  pango_layout_get_extents (layout, NULL, &extents);
-
-  metrics->approximate_char_width = extents.width / pango_utf8_strwidth (sample_str);
-
-  pango_layout_set_text (layout, "0123456789", -1);
-  metrics->approximate_digit_width = max_glyph_width (layout);
-
-  pango_font_description_free (font_desc);
-  g_object_unref (layout);
-
   return metrics;
 }
 
diff --git a/pango/pangofc-font.c b/pango/pangofc-font.c
index ad234be..7c38ab7 100644
--- a/pango/pangofc-font.c
+++ b/pango/pangofc-font.c
@@ -479,60 +479,15 @@ get_face_metrics (PangoFcFont      *fcfont,
   PANGO_FC_FONT_UNLOCK_FACE (fcfont);
 }
 
-static int
-max_glyph_width (PangoLayout *layout)
-{
-  int max_width = 0;
-  GSList *l, *r;
-
-  for (l = pango_layout_get_lines_readonly (layout); l; l = l->next)
-    {
-      PangoLayoutLine *line = l->data;
-
-      for (r = line->runs; r; r = r->next)
-	{
-	  PangoGlyphString *glyphs = ((PangoGlyphItem *)r->data)->glyphs;
-	  int i;
-
-	  for (i = 0; i < glyphs->num_glyphs; i++)
-	    if (glyphs->glyphs[i].geometry.width > max_width)
-	      max_width = glyphs->glyphs[i].geometry.width;
-	}
-    }
-
-  return max_width;
-}
-
 PangoFontMetrics *
-pango_fc_font_create_metrics_for_context (PangoFcFont   *fcfont,
-					  PangoContext  *context)
+pango_fc_font_create_base_metrics_for_context (PangoFcFont   *fcfont,
+					       PangoContext  *context)
 {
   PangoFontMetrics *metrics;
-  PangoLayout *layout;
-  PangoRectangle extents;
-  PangoLanguage *language = pango_context_get_language (context);
-  const char *sample_str = pango_language_get_sample_string (language);
-  PangoFontDescription *desc = pango_font_describe_with_absolute_size (PANGO_FONT (fcfont));
-
   metrics = pango_font_metrics_new ();
 
   get_face_metrics (fcfont, metrics);
 
-  layout = pango_layout_new (context);
-  pango_layout_set_font_description (layout, desc);
-  pango_font_description_free (desc);
-
-  pango_layout_set_text (layout, sample_str, -1);
-  pango_layout_get_extents (layout, NULL, &extents);
-
-  metrics->approximate_char_width =
-    extents.width / pango_utf8_strwidth (sample_str);
-
-  pango_layout_set_text (layout, "0123456789", -1);
-  metrics->approximate_digit_width = max_glyph_width (layout);
-
-  g_object_unref (layout);
-
   return metrics;
 }
 
@@ -581,7 +536,7 @@ pango_fc_font_get_metrics (PangoFont     *font,
       context = pango_font_map_create_context (fontmap);
       pango_context_set_language (context, language);
 
-      info->metrics = pango_fc_font_create_metrics_for_context (fcfont, context);
+      info->metrics = pango_fc_font_create_base_metrics_for_context (fcfont, context);
 
       g_object_unref (context);
       g_object_unref (fontmap);
diff --git a/pango/pangofc-private.h b/pango/pangofc-private.h
index e7c08bf..f200c8c 100644
--- a/pango/pangofc-private.h
+++ b/pango/pangofc-private.h
@@ -90,8 +90,8 @@ void            pango_fc_font_get_raw_extents    (PangoFcFont    *font,
 						  PangoRectangle *ink_rect,
 						  PangoRectangle *logical_rect);
 
-PangoFontMetrics *pango_fc_font_create_metrics_for_context (PangoFcFont   *font,
-							    PangoContext  *context);
+PangoFontMetrics *pango_fc_font_create_base_metrics_for_context (PangoFcFont   *font,
+								 PangoContext  *context);
 
 
 



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