Instruction set reference
HIPE_OP_MEASURE_TEXT
Measures how much space some text would take up if it were the content of an element, without adding anything to the document. This is useful for fitting text to a space (for example choosing a font size or where to break a label), finding the width of a character in a monospace font, or placing things alongside text.
- location: The element whose style the text is measured in.
- arg[0]: The text to measure.
- arg[1]: Optional. A font size in CSS pixels (e.g. "18" or "12.5") to measure the text at, instead of the element's own font size. The line height is scaled to match.
The display server responds with a HIPE_OP_TEXT_METRICS instruction.
Notes
The text is measured as if it were placed directly inside the element as its only content: in the element's font (family, size, weight and style, including inherited values), with its letter and word spacing, tab size, text-transform and white-space rules applied. For example, in an element that collapses white space, a run of spaces counts as one space, and spaces at the start and end of a line don't count.
The text is not wrapped: its width is the width of the widest line, however narrow the element is. If the element preserves newlines (e.g. a pre element, or white-space:pre-wrap), each newline in arg[0] starts a new line; otherwise newlines count as spaces. To fit text into a given width, measure different line breaks and font sizes and choose the one that fits.
The element doesn't have to be displayed: an element styled display:none can still be used to measure text.
