HiI'm having a problem with pango_layout_get_extents. It seems to me that the results are wrong when used on a layout with center or right alignment, while pango_layout_iter_get_line_extents returns the correct rectangle for every line.
In attachment I'm sending a small test program, and also the resulting png image (done with cairo).
Compiled withgcc -o testPangoLayouts `pkg-config --cflags --libs pangocairo` testPangoLayouts.c
Am I missing something? Or is this a bug? I'm using pango 1.20.1 and cairo 1.6.0. Thanks Oscar Lazzarino
#include <pango-1.0/pango/pango.h> #include <cairo/cairo.h> #include <string.h> void drawRects(cairo_t *pCairoContext, PangoLayout *pLayout) { PangoRectangle pLayoutRect; pango_layout_get_extents(pLayout, NULL, &pLayoutRect); cairo_set_source_rgba(pCairoContext, 0.0, 1.0, 0.0, 1.0); cairo_rectangle(pCairoContext, pLayoutRect.x / PANGO_SCALE, pLayoutRect.y / PANGO_SCALE, pLayoutRect.width / PANGO_SCALE, pLayoutRect.height / PANGO_SCALE); cairo_fill(pCairoContext); cairo_set_source_rgba(pCairoContext, 1.0, 1.0, 0.0, 0.5); PangoLayoutIter *lLayoutIter = pango_layout_get_iter(pLayout); do { PangoRectangle lLineRect; pango_layout_iter_get_line_extents(lLayoutIter, NULL, &lLineRect); cairo_rectangle(pCairoContext, lLineRect.x / PANGO_SCALE, lLineRect.y / PANGO_SCALE, lLineRect.width / PANGO_SCALE, lLineRect.height / PANGO_SCALE); cairo_fill(pCairoContext); } while (pango_layout_iter_next_line(lLayoutIter)); } int main() { cairo_surface_t *lCairoSurface = cairo_image_surface_create(CAIRO_FORMAT_ARGB32, 800, 200); cairo_t *lCairoContext = cairo_create(lCairoSurface); PangoFontDescription *lFontDescr = pango_font_description_new(); pango_font_description_set_family(lFontDescr, "Sans"); pango_font_description_set_size(lFontDescr, 10 * PANGO_SCALE); PangoLayout *lLayout = pango_cairo_create_layout(lCairoContext); pango_layout_set_font_description(lLayout, lFontDescr); pango_layout_set_text(lLayout, "The quick brown fox jumps over the lazy dog", strlen("The quick brown fox jumps over the lazy dog")); pango_layout_set_width(lLayout, 100 * PANGO_SCALE); // White bg cairo_set_source_rgba(lCairoContext, 1.0, 1.0, 1.0, 1.0); cairo_paint(lCairoContext); cairo_save(lCairoContext); { pango_layout_set_alignment(lLayout, PANGO_ALIGN_LEFT); cairo_translate(lCairoContext, 100, 100); drawRects(lCairoContext, lLayout); cairo_set_source_rgba(lCairoContext, 0.0, 0.0, 0.0, 1.0); pango_cairo_show_layout(lCairoContext, lLayout); } cairo_restore(lCairoContext); cairo_save(lCairoContext); { pango_layout_set_alignment(lLayout, PANGO_ALIGN_CENTER); cairo_translate(lCairoContext, 300, 100); drawRects(lCairoContext, lLayout); cairo_set_source_rgba(lCairoContext, 0.0, 0.0, 0.0, 1.0); pango_cairo_show_layout(lCairoContext, lLayout); } cairo_restore(lCairoContext); cairo_save(lCairoContext); { pango_layout_set_alignment(lLayout, PANGO_ALIGN_RIGHT); cairo_translate(lCairoContext, 500, 100); drawRects(lCairoContext, lLayout); cairo_set_source_rgba(lCairoContext, 0.0, 0.0, 0.0, 1.0); pango_cairo_show_layout(lCairoContext, lLayout); } cairo_restore(lCairoContext); cairo_surface_write_to_png(lCairoSurface, "test.png"); return 0; }
Attachment:
test.png
Description: PNG image