Text and fonts
Draw text with bitmap or TrueType fonts, measure it, wrap it and align it.
print draws text with two kinds of font:
- Bitmap fonts from
loadFont. Each character is a small picture, copied onto your image wherever the text needs it. Jimp comes with Open Sans in several sizes. A bitmap font has a fixed size, a fixed color and only the characters it was made with. - TrueType, OpenType and WOFF fonts from
loadTTF. The characters are drawn from their outlines, antialiased, at any size, in any color or gradient, with an optional outline. Bring your own.ttf,.otfor.wofffile.
Both are pure JavaScript, so there’s nothing native to install and the result looks the same everywhere. Wrapping, alignment and measuring work the same way for both.
Draw text on the image with a bitmap or TrueType font.
loadFont()Load a bitmap font (.fnt) to use with print.
loadTTF()Load a TrueType, OpenType or WOFF font to print at any size.
measureText()Measure the width of a string in a font.
measureTextHeight()Measure the height of wrapped text in a font.
Load a font and print
Load a font with loadFont, then draw with print.
The fonts that come with Jimp are named exports of jimp/fonts:
import { Jimp, loadFont } from "jimp";import { SANS_32_BLACK } from "jimp/fonts";
const image = new Jimp({ width: 400, height: 120, color: 0xffffffff });const font = await loadFont(SANS_32_BLACK);
image.print({ font, x: 10, y: 10, text: "Hello, world" });
await image.write("hello.png");loadFont is asynchronous because it reads the font file and its image pages. Load each font once and reuse it for every image and every print call.
The bundled fonts
Jimp includes Open Sans in these sizes. The number is the font size in pixels; the line height is a little larger.
| Size | Black | White | Characters |
|---|---|---|---|
| 8px | SANS_8_BLACK |
SANS_8_WHITE |
ASCII, Latin-1 and € |
| 10px | SANS_10_BLACK |
ASCII only | |
| 12px | SANS_12_BLACK |
ASCII only | |
| 14px | SANS_14_BLACK |
ASCII only | |
| 16px | SANS_16_BLACK |
SANS_16_WHITE |
ASCII, Latin-1 and € |
| 32px | SANS_32_BLACK |
SANS_32_WHITE |
ASCII, Latin-1 and € |
| 64px | SANS_64_BLACK |
SANS_64_WHITE |
ASCII, Latin-1 and € |
| 128px | SANS_128_BLACK |
SANS_128_WHITE |
ASCII, Latin-1 and € |
“ASCII” means the printable ASCII characters. “Latin-1” covers accented letters such as é, ñ and ü, and symbols such as £, © and °. The fonts with Latin-1 don’t have a glyph for the backtick (`).
Fonts in the browser
The jimp/fonts constants are absolute paths into the installed package on disk, built with Node’s path and url modules.
They can’t be used in the browser, from a CDN or in a browser bundle.
Instead, serve the .fnt file and its .png pages from your site, and pass the URL to loadFont:
import { Jimp, loadFont } from "jimp";
const font = await loadFont("/fonts/open-sans-32-black/open-sans-32-black.fnt");The bundled fonts are in the dist/fonts/open-sans/ folder of the @jimp/plugin-print package, under the Apache License 2.0.
This site does exactly that for the playground below. See also Using in Browser.
Try it
Print on top of other edits. Click print to add text, then click the image to move it.
image.print({ font, x: 20, y: 20, text: { text: "Hello, Jimp", alignmentX: HorizontalAlign.CENTER, alignmentY: VerticalAlign.BOTTOM }, maxWidth: 320, maxHeight: 230 })Docs →Draw text with a bitmap font or a TrueType font, aligned in a box from x, y to the image's edges. Turn on the text box to set its size and wrap lines.
import { Jimp } from "jimp"; const image = await Jimp.read("dice.png"); // Click a method above to add it here await image.write("output.png");
TrueType, OpenType and WOFF fonts
Load a font file with loadTTF from jimp/load-ttf, then pass it to print with a size in pixels:
import { Jimp } from "jimp";import { loadTTF } from "jimp/load-ttf";
const font = await loadTTF("fonts/Inter-Regular.otf");const image = new Jimp({ width: 400, height: 120, color: "white" });
image.print({ font, size: 48, x: 10, y: 10, text: "Hello, world", color: "#e63946", stroke: "#1d3557", strokeWidth: 2,});color and stroke take a hex number, a CSS color, { r, g, b, a } or a gradient; see print for every option.
The font’s kerning and standard ligatures are applied. Like loadFont, loadTTF takes a path or URL, and also the font’s bytes in a Buffer or ArrayBuffer.
In the browser, pass a URL: the .ttf, .otf or .woff file is loaded with fetch.
loadTTF is in its own entry point, jimp/load-ttf, rather than in jimp.
It parses fonts with opentype.js, which adds about 68 KB (gzipped) to a browser bundle, so importing it separately keeps it out of apps that only use bitmap fonts.
Limitations
- No WOFF2. Decoding it needs a Brotli decompressor. Convert a
.woff2file first, for example with fonttools:fonttools ttLib.woff2 decompress font.woff2. - No font collections.
.ttcfiles don’t load; extract the font you need into a.ttf. - No fallback fonts. Characters the font doesn’t have are drawn with its
.notdefglyph, usually a box. Use a font that covers all of your text. - Left to right only. Characters are placed left to right in the order they’re written, with no bidirectional reordering, so right-to-left scripts such as Arabic and Hebrew aren’t supported. Scripts that need complex shaping, such as Devanagari, aren’t either.
- No hinting. Small text can look softer than in a browser. Each line’s baseline is rounded to a whole pixel so horizontal edges stay sharp.
Wrapping and alignment
print lays the text out in a box whose top left corner is at x, y.
maxWidth and maxHeight set the size of the box. Without them, the box reaches the right and bottom edges of the image.
Give print a maxWidth and it wraps the text at spaces so that no line is wider. Words move to the next line whole; only a word wider than maxWidth is split.
Without maxWidth, text only breaks at line breaks. Every \n, \r\n or \r starts a new line, and blank lines are kept, so "Title\n\nBody" leaves an empty line between the two.
To align the text, pass an object as text with alignmentX from HorizontalAlign and alignmentY from VerticalAlign:
import { Jimp, loadFont, HorizontalAlign, VerticalAlign } from "jimp";import { SANS_32_WHITE } from "jimp/fonts";
const image = await Jimp.read("photo.jpg");const font = await loadFont(SANS_32_WHITE);
// A caption centered at the bottom, with a 20px marginimage.print({ font, x: 20, y: 20, text: { text: "Summer 2026", alignmentX: HorizontalAlign.CENTER, alignmentY: VerticalAlign.BOTTOM, }, maxWidth: image.width - 40, maxHeight: image.height - 40,});To center text on the whole image, start the box at 0, 0 and leave out maxWidth and maxHeight.
| Constant | Aligns text |
|---|---|
HorizontalAlign.LEFT |
to the left edge of the box (default) |
HorizontalAlign.CENTER |
centered in the box |
HorizontalAlign.RIGHT |
to the right edge of the box |
VerticalAlign.TOP |
to the top of the box (default) |
VerticalAlign.MIDDLE |
centered vertically |
VerticalAlign.BOTTOM |
to the bottom of the box |
maxHeight positions the text but doesn’t cut it off: lines that don’t fit continue below the box.
These rules are the same for both kinds of font. With a loadTTF font, set the distance between lines with lineHeight; with a bitmap font, it’s the font’s line height.
Measuring text
measureText returns the width of the widest line, and measureTextHeight returns the height of the text, wrapped at maxWidth if you pass one.
Both use the same spacing and line breaking as print, so you can size things before drawing:
import { Jimp, loadFont, measureText, measureTextHeight } from "jimp";import { SANS_32_BLACK } from "jimp/fonts";
const font = await loadFont(SANS_32_BLACK);const text = "Sale";
// A badge that fits the text with 10px of padding on each sideconst badge = new Jimp({ width: measureText(font, text) + 20, height: measureTextHeight(font, text) + 20, color: 0xffd83aff,});
badge.print({ font, x: 10, y: 10, text });With a loadTTF font, pass the size, lineHeight and letterSpacing you print with as the last argument, for example measureText(font, text, { size: 32 }) and measureTextHeight(font, text, maxWidth, { size: 32 }).
print’s cb option also reports where the text ended, which helps when you stack paragraphs. See print.
Coloring text
With a loadTTF font, pass color (and stroke for an outline) to print.
With a bitmap font, a glyph is copied from the font’s image as it is, so the text color is part of the font.
To print in another color, print a white font onto a transparent layer, change the color of the layer’s pixels while keeping their alpha, and composite the layer onto the image:
import { Jimp, loadFont, measureText, measureTextHeight } from "jimp";import { SANS_64_WHITE } from "jimp/fonts";
const image = new Jimp({ width: 400, height: 120, color: 0xffffffff });const font = await loadFont(SANS_64_WHITE);const text = "Jimp";
const layer = new Jimp({ width: measureText(font, text), height: measureTextHeight(font, text), color: 0x00000000,});layer.print({ font, x: 0, y: 0, text });
// Keep each pixel's alpha, replace its color with #ff4b4blayer.scan((x, y, idx) => { layer.bitmap.data[idx] = 0xff; layer.bitmap.data[idx + 1] = 0x4b; layer.bitmap.data[idx + 2] = 0x4b;});
image.composite(layer, 20, 20);The layer is also a good place for effects: blur a dark copy of it for a drop shadow, or rotate it before compositing. If you always need the same color, it’s simpler to generate a font in that color (see below).
Characters a font doesn’t have
With a bitmap font, print draws ? for any character the font has no glyph for, and leaves whitespace without a glyph blank.
With a loadTTF font, it draws the font’s .notdef glyph, usually an empty box.
So if Chinese, Japanese, Korean, Cyrillic or emoji text prints as ??? or boxes, the font doesn’t include those characters, and Jimp doesn’t fall back to another font.
The fix is a font that does include them. The simplest is a TrueType or OpenType font that covers the text, such as Noto Sans JP for Japanese, loaded with loadTTF:
import { loadTTF } from "jimp/load-ttf";
const font = await loadTTF("fonts/NotoSansJP-Regular.ttf");
image.print({ font, size: 32, x: 10, y: 10, text: "こんにちは世界" });Or make a bitmap font with those characters, as below.
Making your own fonts
Fonts are in the BMFont format: a .fnt file that describes each glyph, and one or more .png pages that hold the glyph images.
Text, XML, JSON and binary .fnt files all work, but the pages must be PNG images.
If you’d rather not ship a font file and parse it at runtime, or want the exact look of a pre-rendered font, any BMFont generator can make a bitmap font from a TrueType or OpenType font. For example, with fontbm and Noto Sans JP:
# chars.txt holds the characters to include, e.g. こんにちは世界fontbm --font-file NotoSansJP-Regular.ttf --output noto-sans-jp \ --font-size 32 --chars 32-126 --chars-file chars.txt --color 0,0,0Then load the .fnt file, with its .png pages in the same directory:
const font = await loadFont("fonts/noto-sans-jp.fnt");
image.print({ font, x: 10, y: 10, text: "こんにちは世界" });- Export PNG pages. AngelCode’s BMFont writes TGA pages by default; set its texture format to
png. A TGA page makesloadFontthrowloadFont: font page "…/font_0.tga" is a TGA image; Jimp fonts need PNG pages. …. - Set the color and size when generating. Jimp doesn’t scale or tint glyphs.
- Use plain bitmaps. Signed distance field (SDF or MSDF) fonts are meant to be rendered by a shader and won’t look right.
- Include only what you need. CJK fonts have thousands of glyphs; limiting the characters keeps the pages small.
- Characters outside the Basic Multilingual Plane such as emoji work too, as long as the font has glyphs for them.
- Check the license of the font you convert. Many fonts allow embedding; some don’t.
See also
print,loadFont,loadTTF,measureTextandmeasureTextHeight- Compositing: layer text and images
- Constants and enums:
HorizontalAlignandVerticalAlign - Using in Browser
- Migrate to v1:
Jimp.loadFontandJimp.FONT_*in v1