Cairo + Pango 布局非左对齐

Cairo + Pango layout non-left alignment

这是怎么回事?

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");

如图所示,文本位于所需位置的右侧。如何正确定位文本? cairo_move_to 显然不是正确的选择,或者是否存在影响 PANGO_ALIGN_CENTERPANGO_ALIGN_RIGHT 行为的已知错误。 PANGO_ALIGN_LEFT 按预期工作。

问题是 pango 可能无法以正确的偏移量呈现文本。要解决此问题,请使用 pango_layout_get_pixel_extents 并查询 "ink" 矩形。请注意,它可能有一个 non-zero x 和 y 分量。从所需位置减去这些偏移量会得到正确的结果。