mirror of
https://github.com/gwenhael-le-moine/sway-patched-tray-menu.git
synced 2024-12-27 21:58:16 +01:00
10ef118e09
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.
117 lines
2.8 KiB
C
117 lines
2.8 KiB
C
#include <cairo/cairo.h>
|
|
#include <pango/pangocairo.h>
|
|
#include <stdarg.h>
|
|
#include <stdbool.h>
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "log.h"
|
|
#include "stringop.h"
|
|
|
|
size_t escape_markup_text(const char *src, char *dest) {
|
|
size_t length = 0;
|
|
if (dest) {
|
|
dest[0] = '\0';
|
|
}
|
|
|
|
while (src[0]) {
|
|
switch (src[0]) {
|
|
case '&':
|
|
length += 5;
|
|
lenient_strcat(dest, "&");
|
|
break;
|
|
case '<':
|
|
length += 4;
|
|
lenient_strcat(dest, "<");
|
|
break;
|
|
case '>':
|
|
length += 4;
|
|
lenient_strcat(dest, ">");
|
|
break;
|
|
case '\'':
|
|
length += 6;
|
|
lenient_strcat(dest, "'");
|
|
break;
|
|
case '"':
|
|
length += 6;
|
|
lenient_strcat(dest, """);
|
|
break;
|
|
default:
|
|
if (dest) {
|
|
dest[length] = *src;
|
|
dest[length + 1] = '\0';
|
|
}
|
|
length += 1;
|
|
}
|
|
src++;
|
|
}
|
|
return length;
|
|
}
|
|
|
|
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font,
|
|
const char *text, double scale, bool markup) {
|
|
PangoLayout *layout = pango_cairo_create_layout(cairo);
|
|
PangoAttrList *attrs;
|
|
if (markup) {
|
|
GError *error = NULL;
|
|
if (pango_parse_markup(text, -1, 0, &attrs, NULL, NULL, &error)) {
|
|
pango_layout_set_markup(layout, text, -1);
|
|
} else {
|
|
wlr_log(WLR_ERROR, "pango_parse_markup '%s' -> error %s", text,
|
|
error->message);
|
|
g_error_free(error);
|
|
markup = false; // fallback to plain text
|
|
}
|
|
}
|
|
if (!markup) {
|
|
attrs = pango_attr_list_new();
|
|
pango_layout_set_text(layout, text, -1);
|
|
}
|
|
|
|
pango_attr_list_insert(attrs, pango_attr_scale_new(scale));
|
|
PangoFontDescription *desc = pango_font_description_from_string(font);
|
|
pango_layout_set_font_description(layout, desc);
|
|
pango_layout_set_single_paragraph_mode(layout, 1);
|
|
pango_layout_set_attributes(layout, attrs);
|
|
pango_attr_list_unref(attrs);
|
|
pango_font_description_free(desc);
|
|
return layout;
|
|
}
|
|
|
|
void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
|
|
int *baseline, double scale, bool markup, const char *fmt, ...) {
|
|
static char buf[2048];
|
|
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
if (vsnprintf(buf, 2048, fmt, args) >= 2048) {
|
|
strcpy(buf, "[buffer overflow]");
|
|
}
|
|
va_end(args);
|
|
|
|
PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup);
|
|
pango_cairo_update_layout(cairo, layout);
|
|
pango_layout_get_pixel_size(layout, width, height);
|
|
if (baseline) {
|
|
*baseline = pango_layout_get_baseline(layout) / PANGO_SCALE;
|
|
}
|
|
g_object_unref(layout);
|
|
}
|
|
|
|
void pango_printf(cairo_t *cairo, const char *font,
|
|
double scale, bool markup, const char *fmt, ...) {
|
|
static char buf[2048];
|
|
|
|
va_list args;
|
|
va_start(args, fmt);
|
|
if (vsnprintf(buf, 2048, fmt, args) >= 2048) {
|
|
strcpy(buf, "[buffer overflow]");
|
|
}
|
|
va_end(args);
|
|
|
|
PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup);
|
|
pango_cairo_update_layout(cairo, layout);
|
|
pango_cairo_show_layout(cairo, layout);
|
|
g_object_unref(layout);
|
|
}
|