aboutsummaryrefslogtreecommitdiff
path: root/common
diff options
context:
space:
mode:
authorSimon Ser <contact@emersion.fr>2023-02-28 16:43:05 +0100
committerSimon Ser <contact@emersion.fr>2023-04-14 18:34:54 +0200
commit08c1946d71039e583696842c3558b337aede1cbf (patch)
tree3873db2edfb31146bd6cd17dae63f068aef34f05 /common
parentac8962eb626a8c715241ae91646f2d1bfcb61533 (diff)
Use format_str() throughout
Diffstat (limited to 'common')
-rw-r--r--common/pango.c20
1 files changed, 4 insertions, 16 deletions
diff --git a/common/pango.c b/common/pango.c
index e04bf80f..288569b3 100644
--- a/common/pango.c
+++ b/common/pango.c
@@ -84,18 +84,11 @@ void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width,
int *baseline, double scale, bool markup, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
- // Add one since vsnprintf excludes null terminator.
- int length = vsnprintf(NULL, 0, fmt, args) + 1;
+ char *buf = vformat_str(fmt, args);
va_end(args);
-
- char *buf = malloc(length);
if (buf == NULL) {
- sway_log(SWAY_ERROR, "Failed to allocate memory");
return;
}
- va_start(args, fmt);
- vsnprintf(buf, length, fmt, args);
- va_end(args);
PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup);
pango_cairo_update_layout(cairo, layout);
@@ -104,6 +97,7 @@ void get_text_size(cairo_t *cairo, const PangoFontDescription *desc, int *width,
*baseline = pango_layout_get_baseline(layout) / PANGO_SCALE;
}
g_object_unref(layout);
+
free(buf);
}
@@ -125,18 +119,11 @@ void render_text(cairo_t *cairo, const PangoFontDescription *desc,
double scale, bool markup, const char *fmt, ...) {
va_list args;
va_start(args, fmt);
- // Add one since vsnprintf excludes null terminator.
- int length = vsnprintf(NULL, 0, fmt, args) + 1;
+ char *buf = vformat_str(fmt, args);
va_end(args);
-
- char *buf = malloc(length);
if (buf == NULL) {
- sway_log(SWAY_ERROR, "Failed to allocate memory");
return;
}
- va_start(args, fmt);
- vsnprintf(buf, length, fmt, args);
- va_end(args);
PangoLayout *layout = get_pango_layout(cairo, desc, buf, scale, markup);
cairo_font_options_t *fo = cairo_font_options_create();
@@ -146,5 +133,6 @@ void render_text(cairo_t *cairo, const PangoFontDescription *desc,
pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout);
g_object_unref(layout);
+
free(buf);
}