[pango/markup-sizes: 2/2] markup: Allow specifying size as percentage




commit 0595c5d427ee9ab2f8eef069fd4a458bf1876f12
Author: Matthias Clasen <mclasen redhat com>
Date:   Sun Aug 8 01:40:08 2021 -0400

    markup: Allow specifying size as percentage
    
    Accept values like 200%, in addition to other
    ways of specifying sizes in markup.
    
    Fixes: #23

 docs/pango_markup.md            |  5 +++--
 pango/pango-markup.c            | 31 ++++++++++++++++++++++---------
 tests/markups/valid-22.expected | 18 ++++++++++++++++++
 tests/markups/valid-22.markup   |  1 +
 4 files changed, 44 insertions(+), 11 deletions(-)
---
diff --git a/docs/pango_markup.md b/docs/pango_markup.md
index f5b39b1d..ee86ec9f 100644
--- a/docs/pango_markup.md
+++ b/docs/pango_markup.md
@@ -78,8 +78,9 @@ face
 font_size
 size
 : 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'.
+  or 'px', or one of the absolute sizes 'xx-small', 'x-small', 'small', 'medium',
+  'large', 'x-large', 'xx-large', or a percentage like '200%', 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
diff --git a/pango/pango-markup.c b/pango/pango-markup.c
index e20e3473..669dde67 100644
--- a/pango/pango-markup.c
+++ b/pango/pango-markup.c
@@ -1309,16 +1309,29 @@ span_parse_func     (MarkupData            *md G_GNUC_UNUSED,
             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)
+               (strcmp (end, "px") == 0 ||
+                strcmp (end, "pt") == 0 ||
+                strcmp (end, "%") == 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);
+          if (strcmp (end, "%") == 0)
+            {
+              double factor = val / 100.0;
+
+              add_attribute (tag, pango_attr_scale_new (factor));
+              if (tag)
+                open_tag_set_absolute_font_scale (tag, factor);
+            }
+          else
+            {
+              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)
        {
diff --git a/tests/markups/valid-22.expected b/tests/markups/valid-22.expected
new file mode 100644
index 00000000..43621301
--- /dev/null
+++ b/tests/markups/valid-22.expected
@@ -0,0 +1,18 @@
+test test
+
+
+---
+
+range 0 5
+[0,9]font-desc=Cantarell 11
+range 5 9
+[0,9]font-desc=Cantarell 11
+[5,9]scale=2.000000
+range 9 2147483647
+
+
+---
+
+[0:5] (null) Cantarell 11
+[5:9] (null) Cantarell 22
+[9:2147483647] (null) Cantarell 22
diff --git a/tests/markups/valid-22.markup b/tests/markups/valid-22.markup
new file mode 100644
index 00000000..601fa7ee
--- /dev/null
+++ b/tests/markups/valid-22.markup
@@ -0,0 +1 @@
+<span font="Cantarell 11">test <span size="200%">test</span></span>


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