How to get Pango Cairo to word wrap properly?

cairo, pango, pangocairo, pycairo

Solution

I found the answer. The value must be multiplied by `pango.SCALE`. This doesn't seem to be mentioned in the documentation for the function in the C API.

Problem

I'm having problems getting Pango Cairo to word wrap. Below is some demo code. I am setting the layout's width to the same as the red rectangle, so I would expect it to wrap to the red rectangle. As it is, it is simply putting one word on each line, as though the width was set very small. If I use pango.WRAP_WORD_CHAR, I only get one character to the line. What am I doing wrong? How do I get the layout to wrap to the width I specified? EDIT If I set the width to 100000, the words wrap correctly. This implies that the set_width and the construction arguments are using different units. Any ideas? ``` # -*- coding: utf-8 -*- 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) ```

Original source