From 59d38cde20425696543d262c5329b14056915614 Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Sun, 8 Aug 2021 01:40:08 -0400 Subject: 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 | 6 ++++-- pango/pango-markup.c | 27 ++++++++++++++++++++++++++- tests/markups/valid-23.expected | 18 ++++++++++++++++++ tests/markups/valid-23.markup | 1 + 4 files changed, 49 insertions(+), 3 deletions(-) create mode 100644 tests/markups/valid-23.expected create mode 100644 tests/markups/valid-23.markup diff --git a/docs/pango_markup.md b/docs/pango_markup.md index 408bead9..c82de9d5 100644 --- a/docs/pango_markup.md +++ b/docs/pango_markup.md @@ -79,11 +79,13 @@ font_size size : 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'. + 'xx-large', or a percentage (e.g. '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' or size='12.5pt'. - Support for specifying font sizes in points was added in Pango 1.50. + Support for specifying font sizes in points or as percentages was added in + Pango 1.50. font_style style diff --git a/pango/pango-markup.c b/pango/pango-markup.c index a4bea3e5..5394c772 100644 --- a/pango/pango-markup.c +++ b/pango/pango-markup.c @@ -860,11 +860,29 @@ big_parse_func (MarkupData *md G_GNUC_UNUSED, return TRUE; } +static gboolean +parse_percentage (const char *input, + double *val) +{ + double v; + char *end; + + v = g_ascii_strtod (input, &end); + if (errno == 0 && strcmp (end, "%") == 0 && v > 0) + { + *val = v; + return TRUE; + } + + return FALSE; +} + static gboolean parse_absolute_size (OpenTag *tag, - const char *size) + const char *size) { SizeLevel level = Medium; + double val; double factor; if (strcmp (size, "xx-small") == 0) @@ -881,6 +899,11 @@ parse_absolute_size (OpenTag *tag, level = XLarge; else if (strcmp (size, "xx-large") == 0) level = XXLarge; + else if (parse_percentage (size, &val)) + { + factor = val / 100.0; + goto done; + } else return FALSE; @@ -888,6 +911,8 @@ parse_absolute_size (OpenTag *tag, * but not to sizes created by any other tags */ factor = scale_factor (level, 1.0); + +done: add_attribute (tag, pango_attr_scale_new (factor)); if (tag) open_tag_set_absolute_font_scale (tag, factor); diff --git a/tests/markups/valid-23.expected b/tests/markups/valid-23.expected new file mode 100644 index 00000000..43621301 --- /dev/null +++ b/tests/markups/valid-23.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-23.markup b/tests/markups/valid-23.markup new file mode 100644 index 00000000..601fa7ee --- /dev/null +++ b/tests/markups/valid-23.markup @@ -0,0 +1 @@ +test test -- cgit v1.2.1