[pango/line-height: 2/8] Add line height to font metrics



commit d75d9598e8b0a704c7bbfbbc61877386b6a86327
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu Jul 4 22:06:06 2019 +0000

    Add line height to font metrics
    
    Add a getter for the line height of a font.

 pango/fonts.c              | 23 ++++++++++++++++++
 pango/pango-font-private.h |  1 +
 pango/pango-font.h         |  2 ++
 pango/pangocairo-font.c    | 38 ++++++++++++++++++-----------
 pango/pangofc-font.c       | 59 ++++++++++++++++++++++++++++++----------------
 5 files changed, 89 insertions(+), 34 deletions(-)
---
diff --git a/pango/fonts.c b/pango/fonts.c
index 6a7778d3..f273cd26 100644
--- a/pango/fonts.c
+++ b/pango/fonts.c
@@ -1793,6 +1793,7 @@ pango_font_get_metrics (PangoFont        *font,
 
       metrics->ascent = PANGO_SCALE * PANGO_UNKNOWN_GLYPH_HEIGHT;
       metrics->descent = 0;
+      metrics->height = 0;
       metrics->approximate_char_width = PANGO_SCALE * PANGO_UNKNOWN_GLYPH_WIDTH;
       metrics->approximate_digit_width = PANGO_SCALE * PANGO_UNKNOWN_GLYPH_WIDTH;
       metrics->underline_position = -PANGO_SCALE;
@@ -1939,6 +1940,28 @@ pango_font_metrics_get_descent (PangoFontMetrics *metrics)
   return metrics->descent;
 }
 
+/**
+ * pango_font_metrics_get_height:
+ * @metrics: a #PangoFontMetrics structure
+ *
+ * Gets the line height from a font metrics structure. The
+ * line height is the distance between successive baselines
+ * in wrapped text.
+ *
+ * If the line height is not available, 0 is returned.
+ *
+ * Return value: the height, in Pango units
+ *
+ * Since: 1.44
+ */
+int
+pango_font_metrics_get_height (PangoFontMetrics *metrics)
+{
+  g_return_val_if_fail (metrics != NULL, 0);
+
+  return metrics->height;
+}
+
 /**
  * pango_font_metrics_get_approximate_char_width:
  * @metrics: a #PangoFontMetrics structure
diff --git a/pango/pango-font-private.h b/pango/pango-font-private.h
index 13b05254..44d19dee 100644
--- a/pango/pango-font-private.h
+++ b/pango/pango-font-private.h
@@ -41,6 +41,7 @@ struct _PangoFontMetrics
 
   int ascent;
   int descent;
+  int height;
   int approximate_char_width;
   int approximate_digit_width;
   int underline_position;
diff --git a/pango/pango-font.h b/pango/pango-font.h
index 60501216..ceaf9643 100644
--- a/pango/pango-font.h
+++ b/pango/pango-font.h
@@ -335,6 +335,8 @@ PANGO_AVAILABLE_IN_ALL
 int               pango_font_metrics_get_ascent                  (PangoFontMetrics *metrics) G_GNUC_PURE;
 PANGO_AVAILABLE_IN_ALL
 int               pango_font_metrics_get_descent                 (PangoFontMetrics *metrics) G_GNUC_PURE;
+PANGO_AVAILABLE_IN_1_44
+int               pango_font_metrics_get_height                  (PangoFontMetrics *metrics) G_GNUC_PURE;
 PANGO_AVAILABLE_IN_ALL
 int               pango_font_metrics_get_approximate_char_width  (PangoFontMetrics *metrics) G_GNUC_PURE;
 PANGO_AVAILABLE_IN_ALL
diff --git a/pango/pangocairo-font.c b/pango/pangocairo-font.c
index cc2e7bee..7915be6f 100644
--- a/pango/pangocairo-font.c
+++ b/pango/pangocairo-font.c
@@ -224,6 +224,7 @@ _pango_cairo_font_get_metrics (PangoFont     *font,
   PangoCairoFontPrivate *cf_priv = PANGO_CAIRO_FONT_PRIVATE (font);
   PangoCairoFontMetricsInfo *info = NULL; /* Quiet gcc */
   GSList *tmp_list;
+  static int in_get_metrics;
 
   const char *sample_str = pango_language_get_sample_string (language);
 
@@ -293,35 +294,43 @@ _pango_cairo_font_get_metrics (PangoFont     *font,
 
          info->metrics->ascent *= xscale;
          info->metrics->descent *= xscale;
+         info->metrics->height *= xscale;
          info->metrics->underline_position *= xscale;
          info->metrics->underline_thickness *= xscale;
          info->metrics->strikethrough_position *= xscale;
          info->metrics->strikethrough_thickness *= xscale;
        }
 
-
       /* Set the matrix on the context so we don't have to adjust the derived
        * metrics. */
       pango_context_set_matrix (context, &pango_matrix);
 
-      /* 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);
+      /* Ugly. We need to prevent recursion when we call into
+       * PangoLayout to determine approximate char width.
+       */
+      if (!in_get_metrics)
+        {
+          in_get_metrics = 1;
 
-      pango_layout_set_text (layout, sample_str, -1);
-      pango_layout_get_extents (layout, NULL, &extents);
+          /* 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);
 
-      sample_str_width = pango_utf8_strwidth (sample_str);
-      g_assert (sample_str_width > 0);
-      info->metrics->approximate_char_width = extents.width / sample_str_width;
+          pango_layout_set_text (layout, sample_str, -1);
+          pango_layout_get_extents (layout, NULL, &extents);
 
-      pango_layout_set_text (layout, "0123456789", -1);
-      info->metrics->approximate_digit_width = max_glyph_width (layout);
+          sample_str_width = pango_utf8_strwidth (sample_str);
+          g_assert (sample_str_width > 0);
+          info->metrics->approximate_char_width = extents.width / sample_str_width;
 
-      g_object_unref (layout);
+          pango_layout_set_text (layout, "0123456789", -1);
+          info->metrics->approximate_digit_width = max_glyph_width (layout);
 
+          g_object_unref (layout);
+          in_get_metrics = 0;
+        }
 
       /* We may actually reuse ascent/descent we got from cairo here.  that's
        * in cf_priv->font_extents.
@@ -355,6 +364,7 @@ _pango_cairo_font_get_metrics (PangoFont     *font,
       g_object_unref (fontmap);
     }
 
+out:
   return pango_font_metrics_ref (info->metrics);
 }
 
diff --git a/pango/pangofc-font.c b/pango/pangofc-font.c
index 7fd7db0f..79fb07c1 100644
--- a/pango/pangofc-font.c
+++ b/pango/pangofc-font.c
@@ -390,6 +390,7 @@ get_face_metrics (PangoFcFont      *fcfont,
     {
       metrics->descent = 0;
       metrics->ascent = PANGO_SCALE * PANGO_UNKNOWN_GLYPH_HEIGHT;
+      metrics->height = metrics->ascent;
       metrics->underline_thickness = PANGO_SCALE;
       metrics->underline_position = - PANGO_SCALE;
       metrics->strikethrough_thickness = PANGO_SCALE;
@@ -422,24 +423,32 @@ get_face_metrics (PangoFcFont      *fcfont,
       vector.y = face->size->metrics.ascender;
       FT_Vector_Transform (&vector, &ft_matrix);
       metrics->ascent = PANGO_UNITS_26_6 (vector.y);
+
+      vector.x = 0;
+      vector.y = face->size->metrics.height;
+      FT_Vector_Transform (&vector, &ft_matrix);
+      metrics->height = PANGO_UNITS_26_6 (vector.y);
     }
   else if (fcfont->is_hinted ||
           (face->face_flags & FT_FACE_FLAG_SCALABLE) == 0)
     {
       metrics->descent = - PANGO_UNITS_26_6 (face->size->metrics.descender);
       metrics->ascent = PANGO_UNITS_26_6 (face->size->metrics.ascender);
+      metrics->height = PANGO_UNITS_26_6 (face->size->metrics.height);
     }
   else
     {
-      FT_Fixed ascender, descender;
+      FT_Fixed ascender, descender, height;
 
       descender = FT_MulFix (face->descender, face->size->metrics.y_scale);
       metrics->descent = - PANGO_UNITS_26_6 (descender);
 
       ascender = FT_MulFix (face->ascender, face->size->metrics.y_scale);
       metrics->ascent = PANGO_UNITS_26_6 (ascender);
-    }
 
+      height = FT_MulFix (face->height, face->size->metrics.y_scale);
+      metrics->height = PANGO_UNITS_26_6 (height);
+    }
 
   metrics->underline_thickness = 0;
   metrics->underline_position = 0;
@@ -545,6 +554,7 @@ pango_fc_font_get_metrics (PangoFont     *font,
   PangoFcFont *fcfont = PANGO_FC_FONT (font);
   PangoFcMetricsInfo *info = NULL; /* Quiet gcc */
   GSList *tmp_list;
+  static int in_get_metrics;
 
   const char *sample_str = pango_language_get_sample_string (language);
 
@@ -570,6 +580,9 @@ pango_fc_font_get_metrics (PangoFont     *font,
 
       info = g_slice_new0 (PangoFcMetricsInfo);
 
+      /* Note: we need to add info to the list before calling
+       * into PangoLayout below, to prevent recursion
+       */
       fcfont->metrics_by_lang = g_slist_prepend (fcfont->metrics_by_lang,
                                                 info);
 
@@ -580,29 +593,35 @@ pango_fc_font_get_metrics (PangoFont     *font,
 
       info->metrics = pango_fc_font_create_base_metrics_for_context (fcfont, context);
 
-      { /* Compute derived metrics */
-       PangoLayout *layout;
-       PangoRectangle extents;
-       const char *sample_str = pango_language_get_sample_string (language);
-       PangoFontDescription *desc = pango_font_describe_with_absolute_size (font);
-       gulong sample_str_width;
+      if (!in_get_metrics)
+        {
+          in_get_metrics = 1;
 
-        layout = pango_layout_new (context);
-       pango_layout_set_font_description (layout, desc);
-       pango_font_description_free (desc);
+          /* Compute derived metrics */
+          PangoLayout *layout;
+          PangoRectangle extents;
+          const char *sample_str = pango_language_get_sample_string (language);
+          PangoFontDescription *desc = pango_font_describe_with_absolute_size (font);
+          gulong sample_str_width;
 
-       pango_layout_set_text (layout, sample_str, -1);
-       pango_layout_get_extents (layout, NULL, &extents);
+          layout = pango_layout_new (context);
+          pango_layout_set_font_description (layout, desc);
+          pango_font_description_free (desc);
 
-       sample_str_width = pango_utf8_strwidth (sample_str);
-       g_assert (sample_str_width > 0);
-       info->metrics->approximate_char_width = extents.width / sample_str_width;
+          pango_layout_set_text (layout, sample_str, -1);
+          pango_layout_get_extents (layout, NULL, &extents);
 
-       pango_layout_set_text (layout, "0123456789", -1);
-       info->metrics->approximate_digit_width = max_glyph_width (layout);
+          sample_str_width = pango_utf8_strwidth (sample_str);
+          g_assert (sample_str_width > 0);
+          info->metrics->approximate_char_width = extents.width / sample_str_width;
 
-       g_object_unref (layout);
-      }
+          pango_layout_set_text (layout, "0123456789", -1);
+          info->metrics->approximate_digit_width = max_glyph_width (layout);
+
+          g_object_unref (layout);
+
+          in_get_metrics = 0;
+        }
 
       g_object_unref (context);
       g_object_unref (fontmap);


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