Skip to content

Instantly share code, notes, and snippets.

@ammgws
Last active October 16, 2021 04:50
Show Gist options
  • Save ammgws/67bb2211eda58b262f46a6021a5b2a5f to your computer and use it in GitHub Desktop.
Save ammgws/67bb2211eda58b262f46a6021a5b2a5f to your computer and use it in GitHub Desktop.
Pango pixel width output test
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include <stdio.h>
#include <cairo.h>
#include <pango/pangocairo.h>
int main (int argc, char **argv)
{
if (argc != 3)
{
return 1;
}
char *text = argv[1];
char *font = argv[2];
cairo_surface_t *target = cairo_recording_surface_create(CAIRO_CONTENT_COLOR_ALPHA, NULL);
cairo_t *cairo = cairo_create (target);
PangoLayout *layout = pango_cairo_create_layout(cairo);
PangoFontDescription *desc = pango_font_description_from_string(font);
pango_layout_set_font_description(layout, desc);
pango_layout_set_text(layout, text, -1);
int text_width, text_height;
pango_cairo_update_layout(cairo, layout);
pango_layout_get_pixel_size(layout, &text_width, &text_height);
char *pango_font_used = pango_font_description_to_string(desc);
printf("Width of '%s' is %d\nUsing font: %s", text, text_width, pango_font_used);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment