2017-02-14 94 views
0

這是怎麼回事?開羅+ Pango佈局非左對齊

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按預期工作。

sample render

回答

0

的問題是,攀高可能無法呈現在其正確的偏移量的文本。要解決該問題,請使用pango_layout_get_pixel_extents並查詢「墨跡」矩形。注意它可能有一個非零的x和y分量。從所需的位置減去這些偏移會得到正確的結果。