Search⌘ K
AI Features

Text Extents

Explore how to calculate and interpret text extents in Pycairo, including the bounding rectangle and offsets. Understand how text positioning works relative to the baseline and how to use these metrics for precise text layout in vector graphics.

We'll cover the following...

What are text extents?

The font size gives us control of the nominal size of the font. The text extents tell us the exact size of a particular text string, when rendered with the current font. To be precise, they pinpoint the rectangle around the area that will be marked when we paint the text.

The Context and text_extents functions return a Pycairo TextExtents object (which behaves much like a tuple). Here is an example of this:

xbearing, ybearing, width, height, dx, dy = ctx.text_extents('Xyz')

The text_extents function accepts a string. It calculates the extents of the string and returns the TextExtents object, which we unpack into 6 variables. Here is an example of this:

In the diagram above, the red dot represents the point (x, y) ...