What is going on here?
pango_font_description_set_family(font.get(),"Serif");
pango_font_description_set_absolute_size(font.get(), 16 * PANGO_SCALE);
int w;
int h;
pango_layout_set_font_description (layout.get(),font.get());
pango_layout_set_width(layout.get(),960*PANGO_SCALE/4);
pango_layout_set_text(layout.get(),"Lorem ipsum dolor sit amet, consctetur adipiscing elit. Nulla vel enim est. Phasellus quis lacinia urna.", -1);
//I want right alignment for this layout
pango_layout_set_alignment(layout.get(),PANGO_ALIGN_RIGHT);
pango_layout_get_pixel_size(layout.get(),&w,&h);
cairo_set_source_rgb (cr.get(), 0.0, 0.0, 0.0);
//Move draw point to the right edge, taking the size of the layout into account
cairo_move_to (cr.get(), 960 - 16 - w,16);
pango_cairo_show_layout(cr.get(), layout.get());
//Draw test rectangle around the text.
cairo_rectangle(cr.get(),960-16-w,16,w,h);
cairo_stroke(cr.get());
cairo_surface_write_to_png(surf.get(),"test.png");
As seen in the picture, the text is positioned to the right of the desired position. How can I locate the text correctly? cairo_move_to
is clearly not the right choice here, or is there any known bugs affecting the behaviour of PANGO_ALIGN_CENTER
, and PANGO_ALIGN_RIGHT
. PANGO_ALIGN_LEFT
works as expected.
The problem is that pango may not render the text at its correct offset. To fix the problem, use pango_layout_get_pixel_extents
and query the "ink" rectangle. Notice that it may have a non-zero x and y component. Subtracting these offsets from the desired location results in the correct result.