Name
ccontext:TextExtents -- get text extents
Synopsis
t = ccontext:TextExtents(s$)
Function
Gets the extents for a string of text. The extents describe a user-space rectangle that encloses the "inked" portion of the text, (as it would be drawn by ccontext:ShowText()). Additionally, the XAdvance and YAdvance values indicate the amount by which the current point would be advanced by ccontext:ShowText().

This function returns a table describing the text extents. The table contains the following fields:

XBearing
The horizontal distance from the origin to the leftmost part of the glyphs as drawn. Positive if the glyphs lie entirely to the right of the origin.
YBearing
The vertical distance from the origin to the topmost part of the glyphs as drawn. Positive only if the glyphs lie completely below the origin; will usually be negative.
Width
Width of the glyphs as drawn.
Height
Height of the glyphs as drawn.
XAdvance
Distance to advance in the X direction after drawing these glyphs.
YAdvance
Distance to advance in the Y direction after drawing these glyphs. Will typically be zero except for vertical text layout as found in East-Asian languages.

Note that whitespace characters do not directly contribute to the size of the rectangle (Width and Height). They do contribute indirectly by changing the position of non-whitespace characters. In particular, trailing whitespace characters are likely to not affect the size of the rectangle, though they will affect the XAdvance and YAdvance values.

Also note that because text extents are in user-space coordinates, they are mostly, but not entirely, independent of the current transformation matrix. If you call ccontext:Scale() with scaling coefficients of 2.0 on each axis, the text will be drawn twice as big, but the reported text extents will not be doubled. They will change slightly due to hinting (so you can't assume that metrics are independent of the transformation matrix), but otherwise will remain unchanged.

Inputs
s$
string of text
Results
t
table containing the text extents (see above)

Show TOC