0.22.12 • Published 1 month ago

@jimp/plugin-print v0.22.12

Weekly downloads
1,119,532
License
MIT
Repository
github
Last release
1 month ago

Jimp supports basic typography using BMFont format (.fnt) even ones in different languages! Just find a bitmap font that is suitable bitmap fonts.

Online tools are also available to convert TTF fonts to BMFont format. They can be used to create color font or sprite packs.

:star: ttf2fnt

:star: Littera

:star: Hiero

Included Fonts

  • Jimp.FONT_SANS_8_BLACK
  • Jimp.FONT_SANS_10_BLACK
  • Jimp.FONT_SANS_12_BLACK
  • Jimp.FONT_SANS_14_BLACK
  • Jimp.FONT_SANS_16_BLACK
  • Jimp.FONT_SANS_32_BLACK
  • Jimp.FONT_SANS_64_BLACK
  • Jimp.FONT_SANS_128_BLACK
  • Jimp.FONT_SANS_8_WHITE
  • Jimp.FONT_SANS_16_WHITE
  • Jimp.FONT_SANS_32_WHITE
  • Jimp.FONT_SANS_64_WHITE
  • Jimp.FONT_SANS_128_WHITE

loadFont

Loads a bitmap font from a file

  • @param {string} file the file path of a .fnt file
  • @param {function(Error, Jimp)} cb (optional) a function to call when the font is loaded
  • @returns {Promise} a promise
import Jimp from "jimp";

async function main() {
  const font = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK);
}

main();

print

Draws a text on a image on a given boundary

  • @param {Jimp} font a bitmap font loaded from Jimp.loadFont command
  • @param {number} x the x position to start drawing the text
  • @param {number} y the y position to start drawing the text
  • @param {string} text the text to draw (string or object with text, alignmentX, and/or alignmentY)
  • @param {number} maxWidth (optional) the boundary width to draw in
  • @param {number} maxHeight (optional) the boundary height to draw in
  • @param {function(Error, Jimp)} cb (optional) a function to call when the text is written
import Jimp from "jimp";

async function main() {
  const font = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK);
  const image = await Jimp.read(1000, 1000, 0x0000ffff);

  image.print(font, 10, 10, "Hello World!");
}

main();

Alignment

Alignment modes are supported by replacing the str argument with an object containing text, alignmentX and alignmentY. alignmentX defaults to Jimp.HORIZONTAL_ALIGN_LEFT and alignmentY defaults to Jimp.VERTICAL_ALIGN_TOP.

You can align text using the following constants.

Jimp.HORIZONTAL_ALIGN_LEFT;
Jimp.HORIZONTAL_ALIGN_CENTER;
Jimp.HORIZONTAL_ALIGN_RIGHT;

Jimp.VERTICAL_ALIGN_TOP;
Jimp.VERTICAL_ALIGN_MIDDLE;
Jimp.VERTICAL_ALIGN_BOTTOM;

Default align modes for image.print are:

{
    alignmentX: Jimp.HORIZONTAL_ALIGN_LEFT,
    alignmentY: Jimp.VERTICAL_ALIGN_TOP
}
const font = await Jimp.loadFont(pathOrURL);

// prints 'Hello world!' on an image, middle and center-aligned
image.print(
  font,
  x,
  y,
  {
    text: "Hello world!",
    alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
    alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE,
  },
  maxWidth,
  maxHeight
);

Note: although maxWidth and maxHeight parameters are optional to print(), they are needed to correctly align the text using the requested alignment mode.

Staggering Text

If you need to stagger text position along the x or y-axis the print method also returns the final coordinates as an argument to the callback.

const font = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK);

image.print(
  font,
  10,
  10,
  "Hello world that wraps!",
  50,
  (err, image, { x, y }) => {
    image.print(font, x, y + 20, "More text on another line", 50);
  }
);

measureText

Measure how wide a piece of text will be.

import Jimp from "jimp";

async function main() {
  const font = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK);
  const image = await Jimp.read(1000, 1000, 0x0000ffff);

  Jimp.measureText(font, "Hello World!");
}

main();

measureTextHeight

Measure how tall a piece of text will be.

import Jimp from "jimp";

async function main() {
  const font = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK);
  const image = await Jimp.read(1000, 1000, 0x0000ffff);

  Jimp.measureTextHeight(font, "Hello World!", 100);
}

main();
0.22.12

1 month ago

0.22.11

1 month ago

0.22.10

8 months ago

0.22.9

8 months ago

0.22.8

11 months ago

0.22.7

1 year ago

0.22.6

1 year ago

0.20.1

1 year ago

0.20.0

1 year ago

0.17.2

1 year ago

0.17.3

1 year ago

0.17.4

1 year ago

0.17.5

1 year ago

0.17.6

1 year ago

0.17.7

1 year ago

0.17.8

1 year ago

0.17.9

1 year ago

0.17.0

1 year ago

0.17.1

1 year ago

0.17.10

1 year ago

0.21.3

1 year ago

0.21.2

1 year ago

0.21.1

1 year ago

0.21.0

1 year ago

0.18.0

1 year ago

0.22.5

1 year ago

0.22.4

1 year ago

0.22.3

1 year ago

0.22.2

1 year ago

0.22.1

1 year ago

0.22.0

1 year ago

0.16.10

1 year ago

0.16.11

1 year ago

0.16.12

1 year ago

0.16.13

1 year ago

0.16.3

1 year ago

0.16.4

1 year ago

0.16.5

1 year ago

0.16.6

1 year ago

0.16.7

1 year ago

0.16.8

1 year ago

0.16.9

1 year ago

0.20.2

1 year ago

0.19.0

1 year ago

0.16.2

2 years ago

0.16.1

4 years ago

0.15.0

4 years ago

0.16.0

4 years ago

0.14.0

4 years ago

0.13.0

4 years ago

0.12.1

4 years ago

0.12.0

4 years ago

0.11.0

4 years ago

0.10.3

4 years ago

0.10.2

4 years ago

0.10.1

4 years ago

0.10.0

4 years ago

0.9.8

4 years ago

0.9.7

4 years ago

0.9.6

4 years ago

0.9.5

4 years ago

0.9.4

4 years ago

0.9.3

4 years ago

0.9.0

4 years ago

0.9.1

4 years ago

0.8.5

4 years ago

0.8.4

5 years ago

0.8.3

5 years ago

0.8.2

5 years ago

0.8.1

5 years ago

0.8.0

5 years ago

0.7.0

5 years ago

0.6.8

5 years ago

0.6.7

5 years ago

0.6.6

5 years ago

0.6.5

5 years ago

0.6.4

5 years ago

0.6.2

5 years ago

0.6.1

5 years ago

0.6.0

5 years ago

0.5.4

5 years ago

0.5.3

6 years ago

0.5.2

6 years ago

0.5.1

6 years ago

0.5.0

6 years ago

0.4.0

6 years ago

0.3.9

6 years ago

0.3.9-alpha.0

6 years ago

0.3.8

6 years ago

0.3.7

6 years ago

0.3.6

6 years ago

0.3.6-alpha.6

6 years ago

0.3.6-alpha.5

6 years ago