I have problems with Pango Cairo word wrap. Below is some demo code. I set the width of the layout in the same way as the red rectangle, so I expect it to be wrapped in a red rectangle. Be that as it may, it simply puts one word on each line, as if the width were set very small. If I use pango.WRAP_WORD_CHAR, I get only one character per line.
What am I doing wrong? How to make the layout wrap the specified width?
EDIT If I set the width to 100000, the words will wrap correctly. This means that set_width arguments and constructors use different units. Any ideas?
import sys
import cairo
import pango
import pangocairo
SIZE = 200
HALF = 100
QUARTER = 50
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, SIZE, SIZE)
context = cairo.Context(surface)
context.set_source_rgb(1, 0, 0)
context.rectangle(QUARTER, QUARTER, HALF, HALF)
context.fill()
context.set_source_rgb(1, 1, 0)
context.translate(QUARTER, QUARTER)
pangocairo_context = pangocairo.CairoContext(context)
layout = pangocairo_context.create_layout()
layout.set_width(HALF)
layout.set_alignment(pango.ALIGN_LEFT)
layout.set_wrap(pango.WRAP_WORD)
layout.set_font_description(pango.FontDescription("Arial 10"))
layout.set_text("The Quick Brown Fox Jumps Over The Piqued Gymnast")
pangocairo_context.update_layout(layout)
pangocairo_context.show_layout(layout)
context.show_page()
with file("test.png", "w") as op:
surface.write_to_png(op)

source
share