[pango/markup-sizes: 1/2] markup: Allow specifying size in pt or px




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

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

 docs/pango_markup.md            | 12 +++++----
 pango/pango-markup.c            | 59 +++++++++++++++++++++++++++--------------
 tests/markups/fail-19.expected  |  1 -
 tests/markups/fail-19.markup    |  1 -
 tests/markups/valid-20.expected | 14 ++++++++++
 tests/markups/valid-20.markup   |  1 +
 tests/markups/valid-21.expected | 14 ++++++++++
 tests/markups/valid-21.markup   |  1 +
 8 files changed, 76 insertions(+), 27 deletions(-)
---
diff --git a/docs/pango_markup.md b/docs/pango_markup.md
index 8291dc3c..f5b39b1d 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 font size with one of the units 'pt'
+  or 'px, 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' (since 1.50, you can alternatively say
+  size='12.5pt').
 
 font_style
 style
diff --git a/pango/pango-markup.c b/pango/pango-markup.c
index 2828eab3..e20e3473 100644
--- a/pango/pango-markup.c
+++ b/pango/pango-markup.c
@@ -1108,6 +1108,24 @@ span_parse_flags (const char  *attr_name,
   return TRUE;
 }
 
+static gboolean
+parse_float (const char **s,
+             double      *val)
+{
+  double v;
+  char *end;
+
+  v = g_ascii_strtod (*s, &end);
+
+  if (errno != 0)
+    return FALSE;
+
+  *val = v;
+  *s = end;
+
+  return TRUE;
+}
+
 static gboolean
 span_parse_func     (MarkupData            *md G_GNUC_UNUSED,
                     OpenTag               *tag,
@@ -1280,27 +1298,28 @@ span_parse_func     (MarkupData            *md G_GNUC_UNUSED,
 
   if (G_UNLIKELY (size))
     {
-      if (g_ascii_isdigit (*size))
-       {
-         const char *end;
-         gint n;
+      const char *end;
+      int n;
+      double val;
 
-         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;
-           }
-
-         add_attribute (tag, pango_attr_size_new (n));
-         if (tag)
-           open_tag_set_absolute_font_size (tag, n);
-       }
+      if ((end = size, _pango_scan_int (&end, &n)) && *end == '\0' && n > 0)
+        {
+          add_attribute (tag, pango_attr_size_new (n));
+          if (tag)
+            open_tag_set_absolute_font_size (tag, n);
+        }
+      else if ((end = size, parse_float (&end, &val)) &&
+               (strcmp (end, "px") == 0 || strcmp (end, "pt") == 0) && val > 0)
+        {
+          if (strcmp (end, "pt") == 0)
+            n = val * PANGO_SCALE;
+          else if (strcmp (end, "px") == 0)
+            n = val * PANGO_SCALE * 72 / 96;
+
+          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>
diff --git a/tests/markups/valid-21.expected b/tests/markups/valid-21.expected
new file mode 100644
index 00000000..b512e878
--- /dev/null
+++ b/tests/markups/valid-21.expected
@@ -0,0 +1,14 @@
+test
+
+
+---
+
+range 0 4
+[0,4]size=10239
+range 4 2147483647
+
+
+---
+
+[0:4] (null) Normal 9.9990234375
+[4:2147483647] (null) Normal 9.9990234375
diff --git a/tests/markups/valid-21.markup b/tests/markups/valid-21.markup
new file mode 100644
index 00000000..d2fca124
--- /dev/null
+++ b/tests/markups/valid-21.markup
@@ -0,0 +1 @@
+<span size="13.333333px">test</span>


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