swayfx/wayland/pango.c

64 lines
1.6 KiB
C
Raw Normal View History

#include <cairo/cairo.h>
#include <pango/pangocairo.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
2016-04-18 01:35:22 +10:00
#include <stdbool.h>
#include "log.h"
2016-04-18 01:35:22 +10:00
PangoLayout *get_pango_layout(cairo_t *cairo, const char *font, const char *text, bool markup) {
PangoLayout *layout = pango_cairo_create_layout(cairo);
2016-04-18 01:35:22 +10:00
if (markup) {
pango_layout_set_markup(layout, text, -1);
} else {
pango_layout_set_text(layout, text, -1);
}
PangoFontDescription *desc = pango_font_description_from_string(font);
pango_layout_set_font_description(layout, desc);
pango_layout_set_single_paragraph_mode(layout, 1);
pango_font_description_free(desc);
return layout;
}
2016-04-18 01:35:22 +10:00
void get_text_size(cairo_t *cairo, const char *font, int *width, int *height,
bool markup, const char *fmt, ...) {
char *buf = malloc(2048);
va_list args;
va_start(args, fmt);
if (vsnprintf(buf, 2048, fmt, args) >= 2048) {
strcpy(buf, "[buffer overflow]");
}
va_end(args);
2016-04-18 01:35:22 +10:00
PangoLayout *layout = get_pango_layout(cairo, font, buf, markup);
pango_cairo_update_layout(cairo, layout);
pango_layout_get_pixel_size(layout, width, height);
g_object_unref(layout);
free(buf);
}
2016-04-18 01:35:22 +10:00
void pango_printf(cairo_t *cairo, const char *font, bool markup, const char *fmt, ...) {
char *buf = malloc(2048);
va_list args;
va_start(args, fmt);
if (vsnprintf(buf, 2048, fmt, args) >= 2048) {
strcpy(buf, "[buffer overflow]");
}
va_end(args);
2016-04-18 01:35:22 +10:00
PangoLayout *layout = get_pango_layout(cairo, font, buf, markup);
pango_cairo_update_layout(cairo, layout);
pango_cairo_show_layout(cairo, layout);
g_object_unref(layout);
free(buf);
}