[cairo] how to get text size ?

Lawrence D'Oliveiro ldo at geek-central.gen.nz
Sat Jul 9 23:16:41 UTC 2016


On Sat, 9 Jul 2016 14:48:12 +0200, Enrico Weigelt, metux IT consult
wrote:

> But I'm still struggling w/ the exact meaning of y_bearing ...

There are 6 fields in a cairo_text_extents_t
<https://www.cairographics.org/manual/cairo-cairo-scaled-font-t.html#cairo-text-extents-t>:
the first 4 define the bounding box of the text, relative to the initial
drawing position. To construct the rectangle equivalent 
<https://www.cairographics.org/manual/cairo-cairo-t.html#cairo-rectangle-t>,
do it like this:

    cairo_text_extents_t text_extents;
    cairo_rectangle_t text_bounds;

    cairo_text_extents(ctx, "some text", &text_extents);
    text_bounds.x = text_extents.x_bearing;
    text_bounds.y = text_extents.y_bearing;
    text_bounds.width = text_extents.width;
    text_bounds.height = text_extents.height;

The other 2 text_extents fields (x_advance, y_advance) define the
offset from the initial drawing position to the final drawing position.


More information about the cairo mailing list