diff options
author | Ryan Dwyer <ryandwyer1@gmail.com> | 2018-09-21 21:27:36 +1000 |
---|---|---|
committer | Ryan Dwyer <ryandwyer1@gmail.com> | 2018-09-22 18:33:28 +1000 |
commit | 10ef118e09435a6fa7815a40829126490d9a7d67 (patch) | |
tree | 91a73019ddb8482d90cf79e3f5cdf2c1f1a8c789 /common | |
parent | Merge pull request #2678 from RyanDwyer/reconfigure-on-reposition (diff) | |
download | sway-10ef118e09435a6fa7815a40829126490d9a7d67.tar.gz sway-10ef118e09435a6fa7815a40829126490d9a7d67.tar.zst sway-10ef118e09435a6fa7815a40829126490d9a7d67.zip |
Fix pango escaping and refactor escape_markup_text
Fixes #2674.
The cause of the issue was in get_pango_layout. When we call
pango_parse_markup, `text` is the escaped string, and the unescaped
string is then computed and written to `buf`. We were then passing the
unescaped string to pango_layout_set_markup, but this function needs the
escaped string. `buf` is not needed and has been removed.
The other part of this PR refactors escape_markup_text to remove the
dest_length argument and removes the -1 return value on error. It now
assumes that you've allocated dest to the correct length.
Diffstat (limited to 'common')
-rw-r--r-- | common/pango.c | 57 |
1 files changed, 17 insertions, 40 deletions
diff --git a/common/pango.c b/common/pango.c index ea71ac4a..dd27991b 100644 --- a/common/pango.c +++ b/common/pango.c | |||
@@ -7,66 +7,45 @@ | |||
7 | #include <stdlib.h> | 7 | #include <stdlib.h> |
8 | #include <string.h> | 8 | #include <string.h> |
9 | #include "log.h" | 9 | #include "log.h" |
10 | #include "stringop.h" | ||
10 | 11 | ||
11 | int escape_markup_text(const char *src, char *dest, int dest_length) { | 12 | size_t escape_markup_text(const char *src, char *dest) { |
12 | int length = 0; | 13 | size_t length = 0; |
14 | if (dest) { | ||
15 | dest[0] = '\0'; | ||
16 | } | ||
13 | 17 | ||
14 | while (src[0]) { | 18 | while (src[0]) { |
15 | switch (src[0]) { | 19 | switch (src[0]) { |
16 | case '&': | 20 | case '&': |
17 | length += 5; | 21 | length += 5; |
18 | if (dest && dest_length - length >= 0) { | 22 | lenient_strcat(dest, "&"); |
19 | dest += sprintf(dest, "%s", "&"); | ||
20 | } else { | ||
21 | dest_length = -1; | ||
22 | } | ||
23 | break; | 23 | break; |
24 | case '<': | 24 | case '<': |
25 | length += 4; | 25 | length += 4; |
26 | if (dest && dest_length - length >= 0) { | 26 | lenient_strcat(dest, "<"); |
27 | dest += sprintf(dest, "%s", "<"); | ||
28 | } else { | ||
29 | dest_length = -1; | ||
30 | } | ||
31 | break; | 27 | break; |
32 | case '>': | 28 | case '>': |
33 | length += 4; | 29 | length += 4; |
34 | if (dest && dest_length - length >= 0) { | 30 | lenient_strcat(dest, ">"); |
35 | dest += sprintf(dest, "%s", ">"); | ||
36 | } else { | ||
37 | dest_length = -1; | ||
38 | } | ||
39 | break; | 31 | break; |
40 | case '\'': | 32 | case '\'': |
41 | length += 6; | 33 | length += 6; |
42 | if (dest && dest_length - length >= 0) { | 34 | lenient_strcat(dest, "'"); |
43 | dest += sprintf(dest, "%s", "'"); | ||
44 | } else { | ||
45 | dest_length = -1; | ||
46 | } | ||
47 | break; | 35 | break; |
48 | case '"': | 36 | case '"': |
49 | length += 6; | 37 | length += 6; |
50 | if (dest && dest_length - length >= 0) { | 38 | lenient_strcat(dest, """); |
51 | dest += sprintf(dest, "%s", """); | ||
52 | } else { | ||
53 | dest_length = -1; | ||
54 | } | ||
55 | break; | 39 | break; |
56 | default: | 40 | default: |
57 | length += 1; | 41 | if (dest) { |
58 | if (dest && dest_length - length >= 0) { | 42 | dest[length] = *src; |
59 | *(dest++) = *src; | 43 | dest[length + 1] = '\0'; |
60 | } else { | ||
61 | dest_length = -1; | ||
62 | } | 44 | } |
45 | length += 1; | ||
63 | } | 46 | } |
64 | src++; | 47 | src++; |
65 | } | 48 | } |
66 | // if we could not fit the escaped string in dest, return -1 | ||
67 | if (dest && dest_length == -1) { | ||
68 | return -1; | ||
69 | } | ||
70 | return length; | 49 | return length; |
71 | } | 50 | } |
72 | 51 | ||
@@ -75,11 +54,9 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, | |||
75 | PangoLayout *layout = pango_cairo_create_layout(cairo); | 54 | PangoLayout *layout = pango_cairo_create_layout(cairo); |
76 | PangoAttrList *attrs; | 55 | PangoAttrList *attrs; |
77 | if (markup) { | 56 | if (markup) { |
78 | char *buf; | ||
79 | GError *error = NULL; | 57 | GError *error = NULL; |
80 | if (pango_parse_markup(text, -1, 0, &attrs, &buf, NULL, &error)) { | 58 | if (pango_parse_markup(text, -1, 0, &attrs, NULL, NULL, &error)) { |
81 | pango_layout_set_markup(layout, buf, -1); | 59 | pango_layout_set_markup(layout, text, -1); |
82 | free(buf); | ||
83 | } else { | 60 | } else { |
84 | wlr_log(WLR_ERROR, "pango_parse_markup '%s' -> error %s", text, | 61 | wlr_log(WLR_ERROR, "pango_parse_markup '%s' -> error %s", text, |
85 | error->message); | 62 | error->message); |