summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Clasen <mclasen@redhat.com>2021-08-08 14:44:57 +0000
committerMatthias Clasen <mclasen@redhat.com>2021-08-08 14:44:57 +0000
commit4d4d9e02892f6ed8496af5e2c6067d650eff3fd5 (patch)
tree8baccfcbf663d7431c6cab69f42f4fd3712b3f8d
parent1f262e416a2d33265539e423ab5de28895379adf (diff)
parent59d38cde20425696543d262c5329b14056915614 (diff)
downloadpango-4d4d9e02892f6ed8496af5e2c6067d650eff3fd5.tar.gz
Merge branch 'markup-sizes' into 'main'
markup: Allow specifying size in pt or px Closes #67 and #23 See merge request GNOME/pango!391
-rw-r--r--docs/pango_markup.md19
-rw-r--r--pango/pango-markup.c96
-rw-r--r--tests/markups/fail-11.expected2
-rw-r--r--tests/markups/fail-19.expected1
-rw-r--r--tests/markups/fail-19.markup1
-rw-r--r--tests/markups/valid-20.expected14
-rw-r--r--tests/markups/valid-20.markup1
-rw-r--r--tests/markups/valid-21.expected14
-rw-r--r--tests/markups/valid-21.markup1
-rw-r--r--tests/markups/valid-22.expected14
-rw-r--r--tests/markups/valid-22.markup1
-rw-r--r--tests/markups/valid-23.expected18
-rw-r--r--tests/markups/valid-23.markup1
13 files changed, 150 insertions, 33 deletions
diff --git a/docs/pango_markup.md b/docs/pango_markup.md
index 8291dc3c..c82de9d5 100644
--- a/docs/pango_markup.md
+++ b/docs/pango_markup.md
@@ -77,11 +77,15 @@ 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 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 or as percentages was added in
+ Pango 1.50.
font_style
style
@@ -144,8 +148,9 @@ overline_color
name such as 'red'. Overline support was added in Pango 1.46/
rise
-: Vertical displacement, in Pango units. Can be negative for subscript, positive
- for superscript.
+: Vertical displacement, in Pango units or in points (e.g. '5pt'). Can be
+ negative for subscript, positive for superscript.
+ Support for specifying rise in points was added in Pango 1.50.
strikethrough
: 'true' or 'false' whether to strike through the text.
diff --git a/pango/pango-markup.c b/pango/pango-markup.c
index 2828eab3..5394c772 100644
--- a/pango/pango-markup.c
+++ b/pango/pango-markup.c
@@ -861,10 +861,28 @@ big_parse_func (MarkupData *md G_GNUC_UNUSED,
}
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);
@@ -1109,6 +1134,35 @@ span_parse_flags (const char *attr_name,
}
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,
const gchar **names,
@@ -1280,27 +1334,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)
@@ -1573,8 +1614,17 @@ span_parse_func (MarkupData *md G_GNUC_UNUSED,
{
gint n = 0;
- if (!span_parse_int ("rise", rise, &n, line_number, error))
- goto error;
+ if (!parse_length (rise, &n))
+ {
+ g_set_error (error,
+ G_MARKUP_ERROR,
+ G_MARKUP_ERROR_INVALID_CONTENT,
+ _("Value of 'rise' attribute on <span> tag on line %d "
+ "could not be parsed; should be an integer, or a "
+ "string such as '5.5pt', not '%s'"),
+ line_number, rise);
+ goto error;
+ }
add_attribute (tag, pango_attr_rise_new (n));
}
diff --git a/tests/markups/fail-11.expected b/tests/markups/fail-11.expected
index 96cac741..4332ad5d 100644
--- a/tests/markups/fail-11.expected
+++ b/tests/markups/fail-11.expected
@@ -1 +1 @@
-ERROR: Value of 'rise' attribute on <span> tag on line 1 could not be parsed; should be an integer, not 'sky' \ No newline at end of file
+ERROR: Value of 'rise' attribute on <span> tag on line 1 could not be parsed; should be an integer, or a string such as '5.5pt', not 'sky' \ No newline at end of file
diff --git a/tests/markups/fail-19.expected b/tests/markups/fail-19.expected
deleted file mode 100644
index 2627110c..00000000
--- a/tests/markups/fail-19.expected
+++ /dev/null
@@ -1 +0,0 @@
-ERROR: Value of 'size' attribute on <span> tag on line 1 could not be parsed; should be an integer no more than 2147483647, or a string such as 'small', not '20px' \ No newline at end of file
diff --git a/tests/markups/fail-19.markup b/tests/markups/fail-19.markup
deleted file mode 100644
index 726022e1..00000000
--- a/tests/markups/fail-19.markup
+++ /dev/null
@@ -1 +0,0 @@
-<span size="20px">test</span>
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..9dc37dc1
--- /dev/null
+++ b/tests/markups/valid-21.expected
@@ -0,0 +1,14 @@
+test
+
+
+---
+
+range 0 4
+[0,4]rise=2560
+range 4 2147483647
+
+
+---
+
+[0:4] (null) Normal
+[4:2147483647] (null) Normal
diff --git a/tests/markups/valid-21.markup b/tests/markups/valid-21.markup
new file mode 100644
index 00000000..b448051b
--- /dev/null
+++ b/tests/markups/valid-21.markup
@@ -0,0 +1 @@
+<span rise="2.5pt">test</span>
diff --git a/tests/markups/valid-22.expected b/tests/markups/valid-22.expected
new file mode 100644
index 00000000..194c1960
--- /dev/null
+++ b/tests/markups/valid-22.expected
@@ -0,0 +1,14 @@
+test
+
+
+---
+
+range 0 4
+[0,4]rise=-5000
+range 4 2147483647
+
+
+---
+
+[0:4] (null) Normal
+[4:2147483647] (null) Normal
diff --git a/tests/markups/valid-22.markup b/tests/markups/valid-22.markup
new file mode 100644
index 00000000..dd828041
--- /dev/null
+++ b/tests/markups/valid-22.markup
@@ -0,0 +1 @@
+<span rise="-5000">test</span>
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 @@
+<span font="Cantarell 11">test <span size="200%">test</span></span>