Skip to content

Instantly share code, notes, and snippets.

@ynkdir
Created March 1, 2011 12:46
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ynkdir/849071 to your computer and use it in GitHub Desktop.
Save ynkdir/849071 to your computer and use it in GitHub Desktop.
pango-font-metrics-test.c
/*
* $ cc `pkg-config pangocairo --cflags --libs` pango-font-metrics-test.c
*
* $ LC_ALL=C ./a.out
* lang=default ascent=13.000000 descent=4.000000
* lang=C ascent=13.000000 descent=4.000000
* lang=ja-jp ascent=13.000000 descent=4.000000
*
* $ LC_ALL=en_US.UTF-8 ./a.out
* lang=default ascent=13.000000 descent=4.000000
* lang=C ascent=13.000000 descent=4.000000
* lang=ja-jp ascent=13.000000 descent=4.000000
*
* $ LC_ALL=ja_JP.UTF-8 ./a.out
* lang=default ascent=12.000000 descent=2.000000
* lang=C ascent=13.000000 descent=4.000000
* lang=ja-jp ascent=12.000000 descent=2.000000
*/
#include <stdio.h>
#include <string.h>
#include <locale.h>
#include <pango/pangocairo.h>
cairo_t *cr;
cairo_surface_t *surface;
void test(const char *font, const char *lang)
{
PangoContext *context;
PangoLanguage *language;
PangoLayout *layout;
PangoFontDescription *desc;
PangoFontMetrics *metrics;
double ascent, descent;
layout = pango_cairo_create_layout(cr);
context = pango_layout_get_context(layout);
if (strcmp(lang, "default") == 0) {
language = pango_language_get_default();
} else {
language = pango_language_from_string(lang);
}
desc = pango_font_description_from_string(font);
pango_layout_set_font_description(layout, desc);
metrics = pango_context_get_metrics(context, desc, language);
ascent = (double)pango_font_metrics_get_ascent(metrics) / PANGO_SCALE;
descent = (double)pango_font_metrics_get_descent(metrics) / PANGO_SCALE;
printf("lang=%s ascent=%f descent=%f\n", lang, ascent, descent);
pango_font_description_free(desc);
g_object_unref(layout);
}
int main()
{
setlocale(LC_ALL, "");
surface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 100, 100);
cr = cairo_create(surface);
test("Monospace 10", "default");
test("Monospace 10", "C");
test("Monospace 10", "ja-jp");
cairo_destroy(cr);
cairo_surface_destroy(surface);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment