measureTextHeight returns how many pixels tall text is when print wraps it at a given width.
Use it to make room for a caption, or to check that text fits in a box before printing it.
It works with bitmap fonts from loadFont and TrueType, OpenType and WOFF fonts from loadTTF.
Signature
import { measureTextHeight } from "jimp";
measureTextHeight(font: BmFont, text: string, maxWidth?: number): numbermeasureTextHeight( font: VectorFont, text: string, maxWidth: number | undefined, options: { size: number; lineHeight?: number; letterSpacing?: number },): numbermeasureTextHeight is a function, not an image method. It returns the number of lines times the line height.
A loadTTF font needs the fourth argument: the size, lineHeight and letterSpacing you print with. Pass undefined as maxWidth to only break at line breaks.
Parameters
| Parameter | Type | Default | Description |
|---|---|---|---|
font |
BmFont | VectorFont |
required | A font from loadFont or loadTTF. |
text |
string |
required | The text to measure. \n, \r\n and \r start a new line. |
maxWidth |
number |
Infinity |
The width to wrap at, as print’s maxWidth. Leave it out to only break at line breaks. |
options.size |
number |
required with a loadTTF font |
The font size in pixels, as print’s size. |
options.lineHeight |
number |
the font’s line height | The height of each line in pixels, as print’s lineHeight. |
options.letterSpacing |
number |
0 |
As print’s letterSpacing. It changes where lines wrap. |
Example
Add a caption strip under a photo that’s tall enough for the wrapped text:
import { Jimp, loadFont, measureTextHeight } from "jimp";import { SANS_32_BLACK } from "jimp/fonts";
const photo = await Jimp.read("photo.jpg");const font = await loadFont(SANS_32_BLACK);
const caption = "The quick brown fox jumps over the lazy dog";const padding = 16;const textWidth = photo.width - padding * 2;const textHeight = measureTextHeight(font, caption, textWidth);
const card = new Jimp({ width: photo.width, height: photo.height + textHeight + padding * 2, color: 0xffffffff,});
card.composite(photo, 0, 0);card.print({ font, x: padding, y: photo.height + padding, text: caption, maxWidth: textWidth,});
await card.write("card.jpg");With SANS_32_BLACK (36px line height) that caption wrapped at 200px takes four lines, so measureTextHeight(font, caption, 200) returns 144.
With a TrueType font
import { measureTextHeight } from "jimp";import { loadTTF } from "jimp/load-ttf";
const font = await loadTTF("fonts/Inter-Regular.otf");const text = "The quick brown fox jumps over the lazy dog";
measureTextHeight(font, text, 200, { size: 32 }); // 154.875: four lines of 38.72pxmeasureTextHeight(font, text, 200, { size: 32, lineHeight: 40 }); // 160Notes
- Same wrapping as
print. The lines are split exactly asprintsplits them for the samemaxWidth: at line breaks, at spaces, and between characters for words wider thanmaxWidth. - Empty lines count. Every line break starts a line, so
"a\n\nb"is three lines tall, and leading or trailing line breaks add a line each. Text with no characters is one line. - Line height. With a bitmap font, each line counts as
font.common.lineHeightpixels, which includes the space between lines. With aloadTTFfont it’slineHeight, or by default the font’s own line height (its ascender to its descender plus its line gap) atsize. - For the width, use
measureText, which returns the width of the widest line. - Added in 2.0: fonts from
loadTTF, with theoptionsargument. - Changed in 2.0:
maxWidthis optional. Leaving it out in plain JavaScript used to count an empty line plus one line per word, someasureTextHeight(font, "Hello")returned72with a 36px font instead of36. - Changed in 2.0: blank lines used to be collapsed, so
"a\n\nb"measured two lines. Empty lines now count, matching whatprintdraws.