[pango/markup-sizes: 1/3] markup: Allow specifying size in pt




commit 90b289bba8492e8de2373a23e3023bba70df69bc
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Aug 8 01:22:14 2021 -0400

    markup: Allow specifying size in pt
    
    Accept values like 12.5pt, in addition to
    the other ways of specifying font size in
    markup.
    
    Fixes: #67

 docs/pango_markup.md            | 12 +++++----
 pango/pango-markup.c            | 56 ++++++++++++++++++++++++++---------------
 tests/markups/fail-19.expected  |  1 -
 tests/markups/fail-19.markup    |  1 -
 tests/markups/valid-20.expected | 14 +++++++++++
 tests/markups/valid-20.markup   |  1 +
 6 files changed, 58 insertions(+), 27 deletions(-)
---
diff --git a/docs/pango_markup.md b/docs/pango_markup.md
index 8291dc3c..5f72dc98 100644
--- a/docs/pango_markup.md
+++ b/docs/pango_markup.md
@@ -77,11 +77,13 @@ face
 
 font_size
 size
-: Font size in 1024ths of a point, or one of the absolute sizes 'xx-small',
-  'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large', or one of the
-  relative sizes 'smaller' or 'larger'. If you want to specify a absolute size,
-  it's usually easier to take advantage of the ability to specify a partial font
-  description using 'font'; you can use font='12.5' rather than size='12800'.
+: Font size in 1024ths of a point, or in points (e.g. '12.5pt'), or one of the
+  absolute sizes 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large',
+  'xx-large', or one of the relative sizes 'smaller' or 'larger'.
+  If you want to specify a absolute size, it's usually easier to take advantage
+  of the ability to specify a partial font description using 'font'; you can use
+  font='12.5' rather than size='12800' or size='12.5pt'.
+  Support for specifying font sizes in points was added in Pango 1.50.
 
 font_style
 style
diff --git a/pango/pango-markup.c b/pango/pango-markup.c
index 2828eab3..24de1fc6 100644
--- a/pango/pango-markup.c
+++ b/pango/pango-markup.c
@@ -1108,6 +1108,35 @@ span_parse_flags (const char  *attr_name,
   return TRUE;
 }
 
+static gboolean
+parse_length (const char *attr_val,
+              int        *result)
+{
+  const char *attr;
+  int n;
+
+  attr = attr_val;
+  if (_pango_scan_int (&attr, &n) && *attr == '\0')
+    {
+      *result = n;
+      return TRUE;
+    }
+  else
+    {
+      double val;
+      char *end;
+
+      val = g_ascii_strtod (attr_val, &end);
+      if (errno == 0 && strcmp (end, "pt") == 0)
+        {
+          *result = val * PANGO_SCALE;
+          return TRUE;
+        }
+    }
+
+  return FALSE;
+}
+
 static gboolean
 span_parse_func     (MarkupData            *md G_GNUC_UNUSED,
                     OpenTag               *tag,
@@ -1280,27 +1309,14 @@ span_parse_func     (MarkupData            *md G_GNUC_UNUSED,
 
   if (G_UNLIKELY (size))
     {
-      if (g_ascii_isdigit (*size))
-       {
-         const char *end;
-         gint n;
-
-         if ((end = size, !_pango_scan_int (&end, &n)) || *end != '\0' || n < 0)
-           {
-             g_set_error (error,
-                          G_MARKUP_ERROR,
-                          G_MARKUP_ERROR_INVALID_CONTENT,
-                          _("Value of 'size' attribute on <span> tag on line %d "
-                            "could not be parsed; should be an integer no more than %d,"
-                            " or a string such as 'small', not '%s'"),
-                          line_number, INT_MAX, size);
-             goto error;
-           }
+      int n;
 
-         add_attribute (tag, pango_attr_size_new (n));
-         if (tag)
-           open_tag_set_absolute_font_size (tag, n);
-       }
+      if (parse_length (size, &n) && n > 0)
+        {
+          add_attribute (tag, pango_attr_size_new (n));
+          if (tag)
+            open_tag_set_absolute_font_size (tag, n);
+        }
       else if (strcmp (size, "smaller") == 0)
        {
          if (tag)
diff --git a/tests/markups/valid-20.expected b/tests/markups/valid-20.expected
new file mode 100644
index 00000000..bd3ac65f
--- /dev/null
+++ b/tests/markups/valid-20.expected
@@ -0,0 +1,14 @@
+test
+
+
+---
+
+range 0 4
+[0,4]size=20480
+range 4 2147483647
+
+
+---
+
+[0:4] (null) Normal 20
+[4:2147483647] (null) Normal 20
diff --git a/tests/markups/valid-20.markup b/tests/markups/valid-20.markup
new file mode 100644
index 00000000..424b4c9c
--- /dev/null
+++ b/tests/markups/valid-20.markup
@@ -0,0 +1 @@
+<span size="20pt">test</span>


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