measureText returns how many pixels wide text is when printed in a font without wrapping: the width of its widest line.
It works with bitmap fonts from loadFont and TrueType, OpenType and WOFF fonts from loadTTF.
Use it to size a canvas or a label before you print, or to pick a font size that fits.
Signature
import { measureText } from "jimp";
measureText(font: BmFont, text: string): numbermeasureText( font: VectorFont, text: string, options: { size: number; lineHeight?: number; letterSpacing?: number },): numbermeasureText is a function, not an image method. It doesn’t draw anything; it returns a width in pixels.
A loadTTF font needs the third argument: the size and letterSpacing you print with.
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. |
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 | As print’s lineHeight. It doesn’t change the width. |
options.letterSpacing |
number |
0 |
As print’s letterSpacing. |
Example
Make a label that is exactly as big as its text, plus some padding:
import { Jimp, loadFont, measureText, measureTextHeight } from "jimp";import { SANS_32_BLACK } from "jimp/fonts";
const font = await loadFont(SANS_32_BLACK);const text = "Sale";
const width = measureText(font, text); // 64const height = measureTextHeight(font, text); // 36
const label = new Jimp({ width: width + 20, height: height + 10, color: 0xffd83aff });label.print({ font, x: 10, y: 5, text });
await label.write("label.png");Pick the largest font that fits
import { Jimp, loadFont, measureText } from "jimp";import { SANS_128_BLACK, SANS_64_BLACK, SANS_32_BLACK, SANS_16_BLACK } from "jimp/fonts";
const image = new Jimp({ width: 300, height: 100, color: 0xffffffff });const text = "Hello, world";
for (const path of [SANS_128_BLACK, SANS_64_BLACK, SANS_32_BLACK, SANS_16_BLACK]) { const font = await loadFont(path);
if (measureText(font, text) <= image.width) { image.print({ font, x: 0, y: 0, text }); break; }}With a TrueType font
Pass the same size and letterSpacing you print with. The width is in fractional pixels:
import { measureText } from "jimp";import { loadTTF } from "jimp/load-ttf";
const font = await loadTTF("fonts/Inter-Regular.otf");
measureText(font, "Hello, world", { size: 32 }); // 180.03125measureText(font, "Sale", { size: 48, letterSpacing: 4 }); // 109.359375Notes
- Widest line. Line breaks (
\n,\r\nor\r) split the text into lines as they do inprint, and the result is the width of the widest one. Empty lines are0wide.measureTextdoesn’t wrap; for the height of wrapped or multi-line text, usemeasureTextHeight. - It matches
print. The width is the sum of each glyph’s advance plus the font’s kerning, the same spacingprintuses. With a bitmap font, characters the font doesn’t have are measured as?, which is howprintdraws them. With aloadTTFfont, they’re measured as the font’s.notdefglyph, and ligatures are applied. - Trailing spaces count.
"Hi "is wider than"Hi", because a space has an advance width. - Added in 2.0: fonts from
loadTTF, with theoptionsargument. Leavingoptionsout with one throws aZodError(Invalid input: expected object, received undefined). - Changed in 2.0: line breaks used to be skipped, so
"Hello\nHello"measured as one line twice as wide. Characters without a glyph used to count as0wide, so text with them measured narrower than it printed.