[pango/line-height-attribute: 1/2] Add a line-height attribute




commit d0d7b8e6a528984a417d18e90a4d19cc4176339e
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Aug 7 10:37:07 2021 -0400

    Add a line-height attribute
    
    This will be used to grow the logical extents of
    runs in a way that is compatible with CSS semantics.

 docs/pango_markup.md     |  4 ++++
 pango/pango-attributes.c | 20 ++++++++++++++++++++
 pango/pango-attributes.h |  4 ++++
 pango/pango-layout.c     |  3 +++
 pango/pango-markup.c     | 35 +++++++++++++++++++++++++++++++++++
 5 files changed, 66 insertions(+)
---
diff --git a/docs/pango_markup.md b/docs/pango_markup.md
index 8291dc3c..67ce9d6b 100644
--- a/docs/pango_markup.md
+++ b/docs/pango_markup.md
@@ -185,6 +185,10 @@ allow_breaks
 : 'true' or 'false' to indicate whether breaking lines is allowed. Available
   since Pango 1.44.
 
+line_height
+: A factor that is used to scale up the logical extents of runs. Available since
+  Pango 1.50.
+
 ## Convenience Tags
 
 `<b>`
diff --git a/pango/pango-attributes.c b/pango/pango-attributes.c
index 1a4a9443..11176913 100644
--- a/pango/pango-attributes.c
+++ b/pango/pango-attributes.c
@@ -1341,6 +1341,25 @@ pango_attr_overline_color_new (guint16 red,
   return pango_attr_color_new (&klass, red, green, blue);
 }
 
+/**
+ * pango_attr_line_height_new:
+ * @factor: the scaling factor to apply to the logical height
+ *
+ * Since: 1.50
+ */
+PangoAttribute *
+pango_attr_line_height_new (double factor)
+{
+  static const PangoAttrClass klass = {
+    PANGO_ATTR_LINE_HEIGHT,
+    pango_attr_float_copy,
+    pango_attr_float_destroy,
+    pango_attr_float_equal
+  };
+
+  return pango_attr_float_new (&klass, factor);
+}
+
 /*
  * Attribute List
  */
@@ -2559,6 +2578,7 @@ pango_attribute_as_float (PangoAttribute *attr)
   switch (attr->klass->type)
     {
     case PANGO_ATTR_SCALE:
+    case PANGO_ATTR_LINE_HEIGHT:
       return (PangoAttrFloat *)attr;
 
     default:
diff --git a/pango/pango-attributes.h b/pango/pango-attributes.h
index 6f18718e..420b12ea 100644
--- a/pango/pango-attributes.h
+++ b/pango/pango-attributes.h
@@ -201,6 +201,7 @@ typedef enum
   PANGO_ATTR_INSERT_HYPHENS,   /* PangoAttrInt */
   PANGO_ATTR_OVERLINE,         /* PangoAttrInt */
   PANGO_ATTR_OVERLINE_COLOR,   /* PangoAttrColor */
+  PANGO_ATTR_LINE_HEIGHT,      /* PangoAttrFloat */
 } PangoAttrType;
 
 /**
@@ -590,6 +591,9 @@ PangoAttribute *pango_attr_overline_color_new   (guint16       red,
                                                 guint16       green,
                                                 guint16       blue);
 
+PANGO_AVAILABLE_IN_1_50
+PangoAttribute *pango_attr_line_height_new (double factor);
+
 /**
  * PangoShowFlags:
  * @PANGO_SHOW_NONE: No special treatment for invisible characters
diff --git a/pango/pango-layout.c b/pango/pango-layout.c
index 520782c5..326999c6 100644
--- a/pango/pango-layout.c
+++ b/pango/pango-layout.c
@@ -632,6 +632,9 @@ pango_layout_get_spacing (PangoLayout *layout)
  *
  * If @factor is zero (the default), spacing is applied as before.
  *
+ * Note: for semantics that are closer to the CSS line-height
+ * property, see [func@Pango.attr_line_height_new].
+ *
  * Since: 1.44
  */
 void
diff --git a/pango/pango-markup.c b/pango/pango-markup.c
index 2828eab3..3e460b3c 100644
--- a/pango/pango-markup.c
+++ b/pango/pango-markup.c
@@ -951,6 +951,29 @@ span_parse_int (const char *attr_name,
   return TRUE;
 }
 
+static gboolean
+span_parse_float (const char  *attr_name,
+                  const char  *attr_val,
+                  double      *val,
+                  int          line_number,
+                  GError     **error)
+{
+  *val = g_ascii_strtod (attr_val, NULL);
+  if (errno != 0)
+    {
+      g_set_error (error,
+                   G_MARKUP_ERROR,
+                   G_MARKUP_ERROR_INVALID_CONTENT,
+                   _("Value of '%s' attribute on <span> tag "
+                     "on line %d could not be parsed; "
+                     "should be a number, not '%s'"),
+                   attr_name, line_number, attr_val);
+      return FALSE;
+    }
+
+  return TRUE;
+}
+
 static gboolean
 span_parse_boolean (const char *attr_name,
                    const char *attr_val,
@@ -1146,6 +1169,7 @@ span_parse_func     (MarkupData            *md G_GNUC_UNUSED,
   const char *allow_breaks = NULL;
   const char *insert_hyphens = NULL;
   const char *show = NULL;
+  const char *line_height = NULL;
 
   g_markup_parse_context_get_position (context,
                                       &line_number, &char_number);
@@ -1224,6 +1248,7 @@ span_parse_func     (MarkupData            *md G_GNUC_UNUSED,
       case 'l':
        CHECK_ATTRIBUTE (lang);
        CHECK_ATTRIBUTE (letter_spacing);
+        CHECK_ATTRIBUTE (line_height);
        break;
       case 'o':
        CHECK_ATTRIBUTE (overline);
@@ -1589,6 +1614,16 @@ span_parse_func     (MarkupData            *md G_GNUC_UNUSED,
       add_attribute (tag, pango_attr_letter_spacing_new (n));
     }
 
+  if (G_UNLIKELY (line_height))
+    {
+      double f = 0;
+
+      if (!span_parse_float ("line_height", line_height, &f, line_number, error))
+        goto error;
+
+      add_attribute (tag, pango_attr_line_height_new (f));
+    }
+
   if (G_UNLIKELY (lang))
     {
       add_attribute (tag,


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