diff --git a/meson.build b/meson.build index 32965e2..f9d8196 100644 --- a/meson.build +++ b/meson.build @@ -132,7 +132,6 @@ sources = [ 'log.c', 'loop.c', 'main.c', - 'pango.c', 'password.c', 'pool-buffer.c', 'render.c', diff --git a/pango.c b/pango.c deleted file mode 100644 index 8e80015..0000000 --- a/pango.c +++ /dev/null @@ -1,145 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include "cairo.h" -#include "log.h" - -static const char overflow[] = "[buffer overflow]"; -static const int max_chars = 16384; - -static char *lenient_strcat(char *dest, const char *src) { - if (dest && src) { - return strcat(dest, src); - } - return dest; -} - -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) { - char *buf; - GError *error = NULL; - if (pango_parse_markup(text, -1, 0, &attrs, &buf, NULL, &error)) { - pango_layout_set_text(layout, buf, -1); - free(buf); - } else { - swaylock_log(LOG_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, ...) { - char *buf = malloc(max_chars); - if (buf == NULL) { - swaylock_log(LOG_ERROR, "Failed to allocate memory"); - return; - } - - va_list args; - va_start(args, fmt); - if (vsnprintf(buf, max_chars, fmt, args) >= max_chars) { - strcpy(&buf[sizeof(buf) - sizeof(overflow)], 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); - - free(buf); -} - -void pango_printf(cairo_t *cairo, const char *font, - double scale, bool markup, const char *fmt, ...) { - char *buf = malloc(max_chars); - if (buf == NULL) { - swaylock_log(LOG_ERROR, "Failed to allocate memory"); - return; - } - - va_list args; - va_start(args, fmt); - if (vsnprintf(buf, max_chars, fmt, args) >= max_chars) { - strcpy(&buf[sizeof(buf) - sizeof(overflow)], overflow); - } - va_end(args); - - PangoLayout *layout = get_pango_layout(cairo, font, buf, scale, markup); - cairo_font_options_t *fo = cairo_font_options_create(); - cairo_get_font_options(cairo, fo); - pango_cairo_context_set_font_options(pango_layout_get_context(layout), fo); - cairo_font_options_destroy(fo); - pango_cairo_update_layout(cairo, layout); - pango_cairo_show_layout(cairo, layout); - g_object_unref(layout); - - free(buf); -}