diff options
author | 2018-04-12 20:19:54 -0400 | |
---|---|---|
committer | 2018-04-12 20:19:54 -0400 | |
commit | cd1b32453a9296c18b28bff71607aeb22987b5cd (patch) | |
tree | c653c6d525b471914c01a9d7ae543f521b6138ed /common/pango.c | |
parent | Merge pull request #1634 from aleksander/master (diff) | |
parent | Fix separator height calculation (diff) | |
download | sway-cd1b32453a9296c18b28bff71607aeb22987b5cd.tar.gz sway-cd1b32453a9296c18b28bff71607aeb22987b5cd.tar.zst sway-cd1b32453a9296c18b28bff71607aeb22987b5cd.zip |
Merge branch 'wlroots'
Diffstat (limited to 'common/pango.c')
-rw-r--r-- | common/pango.c | 72 |
1 files changed, 72 insertions, 0 deletions
diff --git a/common/pango.c b/common/pango.c new file mode 100644 index 00000000..658d2876 --- /dev/null +++ b/common/pango.c | |||
@@ -0,0 +1,72 @@ | |||
1 | #include <cairo/cairo.h> | ||
2 | #include <pango/pangocairo.h> | ||
3 | #include <stdarg.h> | ||
4 | #include <stdbool.h> | ||
5 | #include <stdint.h> | ||
6 | #include <stdio.h> | ||
7 | #include <stdlib.h> | ||
8 | #include <string.h> | ||
9 | #include "log.h" | ||
10 | |||
11 | PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, | ||
12 | const char *text, int32_t scale, bool markup) { | ||
13 | PangoLayout *layout = pango_cairo_create_layout(cairo); | ||
14 | PangoAttrList *attrs; | ||
15 | if (markup) { | ||
16 | char *buf; | ||
17 | GError *error = NULL; | ||
18 | if (!sway_assert(pango_parse_markup( | ||
19 | text, -1, 0, &attrs, &buf, NULL, &error), | ||
20 | "pango_parse_markup '%s' -> error %s", text, | ||
21 | error ? error->message : NULL)) { | ||
22 | return NULL; | ||
23 | } | ||
24 | pango_layout_set_markup(layout, buf, -1); | ||
25 | free(buf); | ||
26 | } else { | ||
27 | attrs = pango_attr_list_new(); | ||
28 | pango_layout_set_text(layout, text, -1); | ||
29 | } | ||
30 | pango_attr_list_insert(attrs, pango_attr_scale_new(scale)); | ||
31 | PangoFontDescription *desc = pango_font_description_from_string(font); | ||
32 | pango_layout_set_font_description(layout, desc); | ||
33 | pango_layout_set_single_paragraph_mode(layout, 1); | ||
34 | pango_layout_set_attributes(layout, attrs); | ||
35 | pango_attr_list_unref(attrs); | ||
36 | pango_font_description_free(desc); | ||
37 | return layout; | ||
38 | } | ||
39 | |||
40 | void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, | ||
41 | int32_t scale, bool markup, const char *fmt, ...) { | ||
42 | static char buf[2048]; | ||
43 | |||
44 | va_list args; | ||
45 | va_start(args, fmt); | ||
46 | if (vsnprintf(buf, 2048, fmt, args) >= 2048) { | ||
47 | strcpy(buf, "[buffer overflow]"); | ||
48 | } | ||
49 | va_end(args); | ||
50 | |||
51 | PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup); | ||
52 | pango_cairo_update_layout(cairo, layout); | ||
53 | pango_layout_get_pixel_size(layout, width, height); | ||
54 | g_object_unref(layout); | ||
55 | } | ||
56 | |||
57 | void pango_printf(cairo_t *cairo, const char *font, | ||
58 | int32_t scale, bool markup, const char *fmt, ...) { | ||
59 | static char buf[2048]; | ||
60 | |||
61 | va_list args; | ||
62 | va_start(args, fmt); | ||
63 | if (vsnprintf(buf, 2048, fmt, args) >= 2048) { | ||
64 | strcpy(buf, "[buffer overflow]"); | ||
65 | } | ||
66 | va_end(args); | ||
67 | |||
68 | PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup); | ||
69 | pango_cairo_update_layout(cairo, layout); | ||
70 | pango_cairo_show_layout(cairo, layout); | ||
71 | g_object_unref(layout); | ||
72 | } | ||