Jump to content

OpenSCAD User Manual/WIP/Text Metrics

From Wikibooks, open books for an open world

textmetrics

[edit | edit source]

textmetrics() returns information about the size and positioning that would be applied to text if laid out as specified by its arguments.

Parameters

The parameters to textmetrics() are exactly the same as the parameters to text().

Return value

The return value from textmetrics() is an object with these properties:

position an [x,y] pair giving the lower left corner of the smallest box that would completely surround the text.

size an [x,y] pair giving the size of that box.

ascent the vertical distance (normally positive) from the baseline of the text to the highest point in the text.

descent the vertical distance (normally zero or negative) from the baseline of the text to the lowest point in the text.

offset an [x,y] pair giving the distance from the origin to the starting point of the baseline of the text. This value is normally [0,0] but can be non-zero when using non-default alignments.

advance an [x,y] pair giving the distance from the starting point for this text to the starting point for a subsequent piece of text. Informally, it says how far the pen should be moved before starting the next piece of text. This value is directly helpful only with default alignments - horizontal text aligned left/baseline and vertical text aligned center/top.

Note: as with any array, you can use an object-like ".x" or ".y" to refer to the two entries in [x,y] pairs. Thus you can refer to either obj.size[0] or obj.size.x to get the X dimension of the text.

Information returned by textmetrics() function.

Sample output (reformatted for readability):

 ECHO: {
   position = [2.2784, -5.7728];
   size = [87.0362, 24.8832];
   ascent = 19.1104;
   descent = -5.7728;
   offset = [0, 0];
   advance = [89.5186, 0];
 }

Example

echo(textmetrics(text="A",size=10));

fontmetrics

[edit | edit source]

fontmetrics() returns information about the specified font and size.

The arguments to fontmetrics() are the size and font name.

Parameters

size the font size font the font name

Return value

The return value from fontmetrics() is an object with these properties:

nominal an object with ascent and descent properties describing the "usual" ascent and descent of the font. For most fonts, this will be the ascent of upper-case letters and the descent of lower-case letters with descenders.

max an object with ascent and descent properties describing the maximum ascent and descent of the font.

interline gives, as a positive number, the designed inter-line spacing for the font.

font an object with members family and style that gives the name and style of the selected font. These may not be the same as the name supplied, if the specified font is not available, or if a generic name like "sans" or "serif" is specified, or if only a style is specified.