diff options
-rw-r--r-- | common/pango.c | 53 | ||||
-rw-r--r-- | include/pango.h | 14 | ||||
-rw-r--r-- | sway/tree/view.c | 9 |
3 files changed, 23 insertions, 53 deletions
diff --git a/common/pango.c b/common/pango.c index ea71ac4a..5afd72d8 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 | ||
@@ -78,7 +57,7 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, | |||
78 | char *buf; | 57 | char *buf; |
79 | GError *error = NULL; | 58 | GError *error = NULL; |
80 | if (pango_parse_markup(text, -1, 0, &attrs, &buf, NULL, &error)) { | 59 | if (pango_parse_markup(text, -1, 0, &attrs, &buf, NULL, &error)) { |
81 | pango_layout_set_markup(layout, buf, -1); | 60 | pango_layout_set_text(layout, buf, -1); |
82 | free(buf); | 61 | free(buf); |
83 | } else { | 62 | } else { |
84 | wlr_log(WLR_ERROR, "pango_parse_markup '%s' -> error %s", text, | 63 | wlr_log(WLR_ERROR, "pango_parse_markup '%s' -> error %s", text, |
diff --git a/include/pango.h b/include/pango.h index 09a535a5..6ab83c16 100644 --- a/include/pango.h +++ b/include/pango.h | |||
@@ -6,17 +6,13 @@ | |||
6 | #include <cairo/cairo.h> | 6 | #include <cairo/cairo.h> |
7 | #include <pango/pangocairo.h> | 7 | #include <pango/pangocairo.h> |
8 | 8 | ||
9 | /* Utility function which escape characters a & < > ' ". | 9 | /** |
10 | * Utility function which escape characters a & < > ' ". | ||
10 | * | 11 | * |
11 | * If the dest parameter is NULL, then the function returns the length of | 12 | * The function returns the length of the escaped string, optionally writing the |
12 | * of the escaped src string. The dest_length doesn't matter. | 13 | * escaped string to dest if provided. |
13 | * | ||
14 | * If the dest parameter is not NULL then the fuction escapes the src string | ||
15 | * an puts the escaped string in dest and returns the lenght of the escaped string. | ||
16 | * The dest_length parameter is the size of dest array. If the size of dest is not | ||
17 | * enough, then the function returns -1. | ||
18 | */ | 14 | */ |
19 | int escape_markup_text(const char *src, char *dest, int dest_length); | 15 | size_t escape_markup_text(const char *src, char *dest); |
20 | PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, | 16 | PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, |
21 | const char *text, double scale, bool markup); | 17 | const char *text, double scale, bool markup); |
22 | void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, | 18 | void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, |
diff --git a/sway/tree/view.c b/sway/tree/view.c index 4398f518..f61f5c84 100644 --- a/sway/tree/view.c +++ b/sway/tree/view.c | |||
@@ -785,14 +785,9 @@ static size_t parse_title_format(struct sway_view *view, char *buffer) { | |||
785 | } | 785 | } |
786 | 786 | ||
787 | static char *escape_title(char *buffer) { | 787 | static char *escape_title(char *buffer) { |
788 | int length = escape_markup_text(buffer, NULL, 0); | 788 | size_t length = escape_markup_text(buffer, NULL); |
789 | char *escaped_title = calloc(length + 1, sizeof(char)); | 789 | char *escaped_title = calloc(length + 1, sizeof(char)); |
790 | int result = escape_markup_text(buffer, escaped_title, length); | 790 | escape_markup_text(buffer, escaped_title); |
791 | if (result != length) { | ||
792 | wlr_log(WLR_ERROR, "Could not escape title: %s", buffer); | ||
793 | free(escaped_title); | ||
794 | return buffer; | ||
795 | } | ||
796 | free(buffer); | 791 | free(buffer); |
797 | return escaped_title; | 792 | return escaped_title; |
798 | } | 793 | } |