print draws text onto the image with a bitmap font from loadFont, or a TrueType, OpenType or WOFF font from loadTTF.
The text is laid out in a box that starts at x, y: set maxWidth to wrap lines, and align the text inside the box.
Bitmap fonts print at the size and in the color they were made with; fonts from loadTTF print at any size, in any color or gradient, with an optional outline.
Signature
image.print(options: { font: BmFont | VectorFont; x: number; y: number; text: | string | number | { text: string | number; alignmentX?: HorizontalAlign; alignmentY?: VerticalAlign; }; maxWidth?: number; maxHeight?: number; cb?: (end: { x: number; y: number }) => void;
// Only with a VectorFont from loadTTF size: number; color?: Paint; stroke?: Paint; strokeWidth?: number; lineHeight?: number; letterSpacing?: number;}): thisprint draws on the image in place and returns it, so you can chain more calls after it.
BmFont is the font object that loadFont resolves to, and VectorFont the one loadTTF resolves to.
size is required with a VectorFont, and the options below it can only be used with one.
A Paint is a color or a gradient, the same as the drawing methods take.
Options
| Option | Type | Default | Description |
|---|---|---|---|
font |
BmFont | VectorFont |
required | A font loaded with loadFont or loadTTF. |
x |
number |
required | Left edge of the text box. |
y |
number |
required | Top edge of the text box. |
text |
string | number | object |
required | The text to draw. Pass { text, alignmentX, alignmentY } to align it. |
text.alignmentX |
HorizontalAlign |
LEFT |
HorizontalAlign.LEFT, CENTER or RIGHT: where each line sits across the width of the box. |
text.alignmentY |
VerticalAlign |
TOP |
VerticalAlign.TOP, MIDDLE or BOTTOM: where the block of lines sits in the height of the box. |
maxWidth |
number |
to the right edge | Width of the text box. Lines wider than this wrap. Without it the box reaches the right edge of the image, and the text only breaks at line breaks. |
maxHeight |
number |
to the bottom edge | Height of the text box, used only for alignmentY. Without it the box reaches the bottom edge of the image. It doesn’t clip. |
cb |
function |
none | Called after drawing with { x, y }: x plus the width of the widest line, and the bottom of the last line. this is the image. |
These options only work with a font from loadTTF. With a bitmap font they throw.
| Option | Type | Default | Description |
|---|---|---|---|
size |
number |
required | Font size in pixels: the size of the font’s em square, like CSS font-size. |
color |
Paint |
black, or none with a stroke |
What to fill the glyphs with: a hex number (0xRRGGBBAA), a CSS color, { r, g, b, a }, or a linear or radial gradient. |
stroke |
Paint |
none | What to outline the glyphs with, a color or gradient like color. With a stroke and no color the text is outline only. |
strokeWidth |
number |
1 |
Width of the outline in pixels, centered on the glyphs’ edges, with round corners. |
lineHeight |
number |
the font’s line height | Height of each line in pixels. The glyphs are centered vertically in it, like CSS. |
letterSpacing |
number |
0 |
Extra space between characters in pixels. Can be negative. Turns off optional ligatures such as fi, like CSS. |
Example
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");Center text on the image
Without maxWidth and maxHeight, the box reaches from x, y to the right and bottom edges of the image.
Start at 0, 0 to align the text on the whole image:
import { Jimp, loadFont, HorizontalAlign, VerticalAlign } from "jimp";import { SANS_32_BLACK } from "jimp/fonts";
const image = new Jimp({ width: 400, height: 200, color: 0xffffffff });const font = await loadFont(SANS_32_BLACK);
image.print({ font, x: 0, y: 0, text: { text: "Centered", alignmentX: HorizontalAlign.CENTER, alignmentY: VerticalAlign.MIDDLE, },});Wrap and align text in a box
Set maxWidth to wrap the text at spaces, and maxHeight to give the box a height to align in.
Here the caption sits at the bottom of the photo with a 20px margin:
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);
image.print({ font, x: 20, y: 20, text: { text: "The quick brown fox jumps over the lazy dog", alignmentX: HorizontalAlign.CENTER, alignmentY: VerticalAlign.BOTTOM, }, maxWidth: image.width - 40, maxHeight: image.height - 40,});Find where the text ended
cb receives the point just past the text: x is the starting x plus the width of the widest line, and y is the bottom of the last line.
Use it to place the next element below the text.
let bottom = 0;
image.print({ font, x: 10, y: 10, text: "First paragraph", cb: ({ y }) => { bottom = y; },});
image.print({ font, x: 10, y: bottom + 8, text: "Second paragraph" });To know the size before drawing, use measureText and measureTextHeight.
TrueType, OpenType and WOFF fonts
Load a .ttf, .otf or .woff file with loadTTF from jimp/load-ttf, and pass it to print with a size.
The glyphs are drawn from their outlines, antialiased, with the font’s kerning and ligatures:
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: 100, color: "white" });
image.print({ font, size: 32, x: 10, y: 10, text: "Hello, world", color: "#1d3557" });
await image.write("hello.png");Alignment, maxWidth, maxHeight and cb work the same as with bitmap fonts.
Gradients and outlines
color and stroke take a color or a gradient. Gradient coordinates are in image pixels, so this one runs from the top of the text (y1: 20) to its bottom (y2: 90):
import { HorizontalAlign, Jimp, VerticalAlign } from "jimp";import { loadTTF } from "jimp/load-ttf";
const font = await loadTTF("fonts/Inter-Regular.otf");const image = new Jimp({ width: 320, height: 110, color: "white" });
image.print({ font, size: 80, x: 0, y: 0, text: { text: "Jimp", alignmentX: HorizontalAlign.CENTER, alignmentY: VerticalAlign.MIDDLE, }, color: { type: "linear", x1: 0, y1: 20, x2: 0, y2: 90, stops: [ { offset: 0, color: "#ffd166" }, { offset: 1, color: "#e63946" }, ], }, stroke: "#1d3557", strokeWidth: 3,});A radial gradient is { type: "radial", cx, cy, r, stops }.
Pass a stroke without a color to draw only the outline:
image.print({ font, size: 64, x: 10, y: 10, text: "Outline", stroke: "black", strokeWidth: 2 });Line height and letter spacing
lineHeight sets the distance between lines in pixels, and letterSpacing adds space between characters:
image.print({ font, size: 24, x: 10, y: 10, text: "Spaced out\nand tall", lineHeight: 40, letterSpacing: 2,});Pass the same size, lineHeight and letterSpacing to measureText and measureTextHeight to measure the text before you draw it.
Try it
Pick a font, some text and an alignment, and click the image to move the box’s top left corner. Turn on the text box to set maxWidth and maxHeight.
The loadTTF fonts are TrueType and WOFF files, with size, color and stroke controls.
The code shows the jimp/fonts constant you’d use in Node.js for the bitmap fonts; this page loads the same fonts by URL.
The TrueType and WOFF fonts here only have printable ASCII characters, so others print as a box.
image.print({ font, x: 20, y: 20, text: { text: "Hello, Jimp", alignmentX: HorizontalAlign.CENTER, alignmentY: VerticalAlign.BOTTOM }, maxWidth: 320, maxHeight: 230 })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"); // Change an option to run it await image.write("output.png");
Notes
- Line breaks. Every
\n,\r\nor\rstarts a new line, so"a\n\nb"prints three lines with an empty one in the middle. Leading and trailing line breaks add empty lines too. Lines arefont.common.lineHeightpixels apart (36 for the 32px fonts). - Wrapping. With
maxWidth, lines break at spaces and words move to the next line whole. Only a word wider thanmaxWidthon its own is split between characters. The spaces where a line wraps are dropped; other spaces, including runs of spaces and leading spaces, are printed as they are. maxHeightdoesn’t clip. Text that doesn’t fit runs past the bottom of the box. Measure first withmeasureTextHeightif it has to fit.- Text can overflow the box. Without
maxWidth, a long line isn’t wrapped: left-aligned text runs past the right edge of the image, and centered or right-aligned text can start left ofx. - Missing characters. With a bitmap font, a character the font has no glyph for is drawn as
?, and whitespace with no glyph is left blank. The 8, 16, 32, 64 and 128 px bundled fonts cover printable ASCII (except`), Latin-1 letters and symbols such aséand£, and€. The 10, 12 and 14 px fonts cover printable ASCII only. With aloadTTFfont, missing characters are drawn with the font’s.notdefglyph, usually a box. There’s no fallback to another font: for other scripts, use a TrueType font that covers them, or make your own bitmap font. - Bitmap text color comes from the font. Glyphs are copied from the font’s image pages as they are, so a
_BLACKfont prints black and a_WHITEfont prints white. See Coloring text for other colors, or use aloadTTFfont andcolor. - Vector text.
loadTTFfonts are placed at fractional x positions, and each line’s baseline is rounded to a whole pixel to keep horizontal edges sharp. There’s no hinting. CSS colors incolorandstrokeare converted to the image’s color space; an invalid one throwsInvalid color "…"before anything is drawn. Text is laid out left to right only. See the limitations. - Validation.
x,y,maxWidthandmaxHeightmust be numbers, andalignmentXandalignmentYmust be enum values. Anything else throws aZodError, for exampleInvalid input: expected number, received undefinedat pathx. A missingfontthrowsCannot read properties of undefined (reading 'chars'). With aloadTTFfont, a missingsizefails validation at pathsize, and negativesize,lineHeightorstrokeWidthvalues throw. - Vector options with a bitmap font throw, for example
print: size, color can only be used with fonts loaded with loadTTF. Bitmap fonts from loadFont are drawn at the size and in the color they were made with. - Added in 2.0: TrueType, OpenType and WOFF fonts from
loadTTF, and thesize,color,stroke,strokeWidth,lineHeightandletterSpacingoptions. - v0 signature. In v0 the call was
image.print(font, x, y, text, maxWidth, maxHeight, cb). Passing the font first in v1 fails validation becausexandyare missing. See Migrate to v1. - Changed in 2.0:
alignmentXwithoutmaxWidthused to throw (Invalid input: expected number, received Infinity). It now aligns the text within the image, to the right ofx. - Changed in 2.0:
alignmentYwithoutmaxHeightused to be ignored. It now aligns the text within the image, belowy, soBOTTOMmoves the text to the bottom edge of the image. - Changed in 2.0: blank lines used to be collapsed, so
"a\n\nb"printed two lines. Every line break now starts a line, and vertical alignment andcbcount the empty lines. - Changed in 2.0: characters outside the Basic Multilingual Plane, such as emoji, print when the font has them. They used to print as
??. - Changed in 2.0: a word that fit on a line of its own but not after the text already on the line could be split mid-word. It now moves to the next line whole.
cb’sxalso used to miss the widest line when that line started after a wrap or a line break.