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

loadFont()

Load a bitmap font (.fnt) to use with print.

From @jimp/plugin-print

loadFont loads a bitmap font in the BMFont format so you can draw text with print and measure it with measureText. Pass it one of the fonts bundled in jimp/fonts, or the path or URL of your own .fnt file. To print with a TrueType, OpenType or WOFF font at any size and color, use loadTTF instead.

Signature

loadFont.d.ts
import { loadFont } from "jimp";
loadFont(file: string): Promise<BmFont>

loadFont is a function, not an image method. It resolves to a font object (BmFont) holding the glyph metrics and the font’s image pages.

Parameters

Parameter Type Default Description
file string required Path to a .fnt file, a file: URL, or an http:, https:, data: or blob: URL. Its .png pages must sit next to it.

Example

example.ts
import { Jimp, loadFont } from "jimp";
import { SANS_32_BLACK } from "jimp/fonts";
const font = await loadFont(SANS_32_BLACK);
const image = new Jimp({ width: 320, height: 80, color: 0xffffffff });
image.print({ font, x: 10, y: 10, text: "Hello, world" });
await image.write("hello.png");

Bundled fonts

jimp/fonts exports the path of each bundled Open Sans font, in black and in white:

Size Black White Line height
8px SANS_8_BLACK SANS_8_WHITE 9px
10px SANS_10_BLACK 14px
12px SANS_12_BLACK 17px
14px SANS_14_BLACK 20px
16px SANS_16_BLACK SANS_16_WHITE 18px
32px SANS_32_BLACK SANS_32_WHITE 36px
64px SANS_64_BLACK SANS_64_WHITE 72px
128px SANS_128_BLACK SANS_128_WHITE 143px

The 8, 16, 32, 64 and 128 px fonts cover printable ASCII except the backtick (`), plus Latin-1 (accented letters such as é and symbols such as £) and €. The 10, 12 and 14 px fonts cover printable ASCII only. print draws any other character as ?.

The constants are absolute file paths inside the installed package, so they only work in Node.js.

Your own font

Pass the path to the .fnt file. Its pages are read from the same directory, and they must be PNG images.

example.ts
const font = await loadFont("fonts/my-font/my-font.fnt");

See Making your own fonts for how to generate one.

In the browser

jimp/fonts can’t be used in the browser, because its constants are paths on the server’s disk. Host the .fnt and .png files with the rest of your site (the bundled ones are in the dist/fonts/open-sans/ folder of the @jimp/plugin-print package) and pass their URL:

main.ts
import { loadFont } from "jimp";
const font = await loadFont("/fonts/open-sans-32-black/open-sans-32-black.fnt");

In the browser and in web workers, loadFont always loads with fetch, so the usual CORS rules apply to fonts on another origin.

Notes

  • How the file is loaded. In Node.js, a path or file: URL is read from disk, pages included, without fetch. http:, https:, data: and blob: URLs are loaded with the global fetch, and the pages are fetched relative to the font’s URL.
  • Formats. Text, XML, JSON and binary BMFont files all work. The bundled fonts are XML. The pages must be PNGs; AngelCode’s BMFont exports TGA pages by default, so set its texture format to png.
  • Load once, print many times. The font object can be reused for any number of images, so load it once rather than on every call.
  • Pages stay in the font’s directory. A page name that points outside the font’s directory, such as ../secret.png, throws instead of being loaded.
  • Changed in 2.0: a missing file, the font’s contents, an HTTP error response and a TGA page used to fail with unrelated messages such as Failed to parse URL from …, Unquoted attribute value or Mime type image/x-icon does not support decoding. They now get the errors below. file: URLs, which used to fail, are read from disk.

Errors

  • loadFont expects a path or URL to a font file, but received undefined.: no string was passed. This is often v0 code such as loadFont(Jimp.FONT_SANS_32_BLACK); import the font from jimp/fonts instead.
  • loadFont expects a path or URL to a font file, but received the contents of one: …: the .fnt file’s text was passed instead of its path.
  • loadFont: font file not found: "fonts/missing.fnt".: the path doesn’t exist. Relative paths are resolved from the current working directory.
  • loadFont: font page not found: "…/my-font.png".: the .fnt file was found, but a page it lists isn’t next to it.
  • loadFont: font page "fonts/font_0.tga" is a TGA image; Jimp fonts need PNG pages. In BMFont, set the texture format to png when exporting.: a page isn’t a PNG. A page named .png that holds another format gets … isn't a PNG image; …. The decoder’s error is on error.cause.
  • loadFont: couldn't load "https://…/font.fnt": HTTP 404 Not Found: the server answered the font’s URL with an error status. A page URL that fails gets Jimp.read’s error, such as HTTP Status 404 for url ….
  • loadFont: couldn't fetch "https://…/font.fnt": fetch failed: the request itself failed (the end of the message is the fetch error, which varies by runtime), for example because the host can’t be reached or, in the browser, because of CORS. The fetch error is on error.cause.
  • loadFont: can't load "…" because this runtime has no global fetch.: a URL was passed in a runtime without fetch. Pass a file path instead.
  • Font page is outside of the font directory: ../secret.png: a page name in the font points outside the font’s directory.