diff options
Diffstat (limited to 'common/pango.c')
-rw-r--r-- | common/pango.c | 46 |
1 files changed, 23 insertions, 23 deletions
diff --git a/common/pango.c b/common/pango.c index dbc369dc..288569b3 100644 --- a/common/pango.c +++ b/common/pango.c | |||
@@ -50,7 +50,7 @@ size_t escape_markup_text(const char *src, char *dest) { | |||
50 | return length; | 50 | return length; |
51 | } | 51 | } |
52 | 52 | ||
53 | PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, | 53 | PangoLayout *get_pango_layout(cairo_t *cairo, const PangoFontDescription *desc, |
54 | const char *text, double scale, bool markup) { | 54 | const char *text, double scale, bool markup) { |
55 | PangoLayout *layout = pango_cairo_create_layout(cairo); | 55 | PangoLayout *layout = pango_cairo_create_layout(cairo); |
56 | PangoAttrList *attrs; | 56 | PangoAttrList *attrs; |
@@ -73,60 +73,59 @@ PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, | |||
73 | } | 73 | } |
74 | 74 | ||
75 | pango_attr_list_insert(attrs, pango_attr_scale_new(scale)); | 75 | pango_attr_list_insert(attrs, pango_attr_scale_new(scale)); |
76 | PangoFontDescription *desc = pango_font_description_from_string(font); | ||
77 | pango_layout_set_font_description(layout, desc); | 76 | pango_layout_set_font_description(layout, desc); |
78 | pango_layout_set_single_paragraph_mode(layout, 1); | 77 | pango_layout_set_single_paragraph_mode(layout, 1); |
79 | pango_layout_set_attributes(layout, attrs); | 78 | pango_layout_set_attributes(layout, attrs); |
80 | pango_attr_list_unref(attrs); | 79 | pango_attr_list_unref(attrs); |
81 | pango_font_description_free(desc); | ||
82 | return layout; | 80 | return layout; |
83 | } | 81 | } |
84 | 82 | ||
85 | void get_text_size(cairo_t *cairo, const char *font, int *width, int *height, | 83 | void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width, int *height, |
86 | int *baseline, double scale, bool markup, const char *fmt, ...) { | 84 | int *baseline, double scale, bool markup, const char *fmt, ...) { |
87 | va_list args; | 85 | va_list args; |
88 | va_start(args, fmt); | 86 | va_start(args, fmt); |
89 | // Add one since vsnprintf excludes null terminator. | 87 | char *buf = vformat_str(fmt, args); |
90 | int length = vsnprintf(NULL, 0, fmt, args) + 1; | ||
91 | va_end(args); | 88 | va_end(args); |
92 | |||
93 | char *buf = malloc(length); | ||
94 | if (buf == NULL) { | 89 | if (buf == NULL) { |
95 | sway_log(SWAY_ERROR, "Failed to allocate memory"); | ||
96 | return; | 90 | return; |
97 | } | 91 | } |
98 | va_start(args, fmt); | ||
99 | vsnprintf(buf, length, fmt, args); | ||
100 | va_end(args); | ||
101 | 92 | ||
102 | PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup); | 93 | PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup); |
103 | pango_cairo_update_layout(cairo, layout); | 94 | pango_cairo_update_layout(cairo, layout); |
104 | pango_layout_get_pixel_size(layout, width, height); | 95 | pango_layout_get_pixel_size(layout, width, height); |
105 | if (baseline) { | 96 | if (baseline) { |
106 | *baseline = pango_layout_get_baseline(layout) / PANGO_SCALE; | 97 | *baseline = pango_layout_get_baseline(layout) / PANGO_SCALE; |
107 | } | 98 | } |
108 | g_object_unref(layout); | 99 | g_object_unref(layout); |
100 | |||
109 | free(buf); | 101 | free(buf); |
110 | } | 102 | } |
111 | 103 | ||
112 | void pango_printf(cairo_t *cairo, const char *font, | 104 | void get_text_metrics(const PangoFontDescription *description, int *height, int *baseline) { |
105 | cairo_t *cairo = cairo_create(NULL); | ||
106 | PangoContext *pango = pango_cairo_create_context(cairo); | ||
107 | // When passing NULL as a language, pango uses the current locale. | ||
108 | PangoFontMetrics *metrics = pango_context_get_metrics(pango, description, NULL); | ||
109 | |||
110 | *baseline = pango_font_metrics_get_ascent(metrics) / PANGO_SCALE; | ||
111 | *height = *baseline + pango_font_metrics_get_descent(metrics) / PANGO_SCALE; | ||
112 | |||
113 | pango_font_metrics_unref(metrics); | ||
114 | g_object_unref(pango); | ||
115 | cairo_destroy(cairo); | ||
116 | } | ||
117 | |||
118 | void render_text(cairo_t *cairo, const PangoFontDescription *desc, | ||
113 | double scale, bool markup, const char *fmt, ...) { | 119 | double scale, bool markup, const char *fmt, ...) { |
114 | va_list args; | 120 | va_list args; |
115 | va_start(args, fmt); | 121 | va_start(args, fmt); |
116 | // Add one since vsnprintf excludes null terminator. | 122 | char *buf = vformat_str(fmt, args); |
117 | int length = vsnprintf(NULL, 0, fmt, args) + 1; | ||
118 | va_end(args); | 123 | va_end(args); |
119 | |||
120 | char *buf = malloc(length); | ||
121 | if (buf == NULL) { | 124 | if (buf == NULL) { |
122 | sway_log(SWAY_ERROR, "Failed to allocate memory"); | ||
123 | return; | 125 | return; |
124 | } | 126 | } |
125 | va_start(args, fmt); | ||
126 | vsnprintf(buf, length, fmt, args); | ||
127 | va_end(args); | ||
128 | 127 | ||
129 | PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup); | 128 | PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup); |
130 | cairo_font_options_t *fo = cairo_font_options_create(); | 129 | cairo_font_options_t *fo = cairo_font_options_create(); |
131 | cairo_get_font_options(cairo, fo); | 130 | cairo_get_font_options(cairo, fo); |
132 | pango_cairo_context_set_font_options(pango_layout_get_context(layout), fo); | 131 | pango_cairo_context_set_font_options(pango_layout_get_context(layout), fo); |
@@ -134,5 +133,6 @@ void pango_printf(cairo_t *cairo, const char *font, | |||
134 | pango_cairo_update_layout(cairo, layout); | 133 | pango_cairo_update_layout(cairo, layout); |
135 | pango_cairo_show_layout(cairo, layout); | 134 | pango_cairo_show_layout(cairo, layout); |
136 | g_object_unref(layout); | 135 | g_object_unref(layout); |
136 | |||
137 | free(buf); | 137 | free(buf); |
138 | } | 138 | } |