Skip to content
docs
GitHub 4.5M /week v2.2.0
Text

measureText()

Measure the width of a string in a font.

From @jimp/plugin-print

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

measureText.d.ts
import { measureText } from "jimp";
measureText(font: BmFont, text: string): number
measureText(
font: VectorFont,
text: string,
options: { size: number; lineHeight?: number; letterSpacing?: number },
): number

measureText 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:

example.ts
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); // 64
const 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

example.ts
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:

example.ts
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.03125
measureText(font, "Sale", { size: 48, letterSpacing: 4 }); // 109.359375

Notes

  • Widest line. Line breaks (\n, \r\n or \r) split the text into lines as they do in print, and the result is the width of the widest one. Empty lines are 0 wide. measureText doesn’t wrap; for the height of wrapped or multi-line text, use measureTextHeight.
  • It matches print. The width is the sum of each glyph’s advance plus the font’s kerning, the same spacing print uses. With a bitmap font, characters the font doesn’t have are measured as ?, which is how print draws them. With a loadTTF font, they’re measured as the font’s .notdef glyph, 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 the options argument. Leaving options out with one throws a ZodError (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 as 0 wide, so text with them measured narrower than it printed.