How to use the loadFont function from jimp

Find comprehensive JavaScript jimp.loadFont code examples handpicked from public code repositorys.

jimp.loadFont loads a font file from disk or a URL and returns a Promise that resolves to a Jimp Font object.

1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
var newMsg='<@'+msg.author.id+'> used `!text`'
if (!creditsDisabled){
  chargeCredits(msg.author.id,0.05)
  newMsg+=', it cost :coin:`0.05`/`'+creditsRemaining(msg.author.id)+'`'
}
jimp.loadFont(jimp.FONT_SANS_32_BLACK).then(font => {
  //var newTxtWidth=jimp.measureText(jimp.FONT_SANS_32_BLACK, newTxt)
  //var newTxtHeight=jimp.measureTextHeight(jimp.FONT_SANS_32_BLACK, newTxt, 100)
  var txtObj={text:newTxt,alignmentX:jimp.HORIZONTAL_ALIGN_CENTER,alignmentY:jimp.VERTICAL_ALIGN_TOP,background:0xffffff}
  switch(c){
fork icon22
star icon101
watch icon3

202
203
204
205
206
207
208
209
210
211
212
let gmt = new Date(0).getTime() - new Date('1 Januari 2021').getTime()
const weton = ['Pahing', 'Pon','Wage','Kliwon','Legi'][Math.floor(((d * 1) + gmt) / 84600000) % 5]
const week = d.toLocaleDateString(locale, { weekday: 'long' })


async function ppwa(conn, m) { 
let font = await jimp.loadFont('./name.fnt'), 
mask = await jimp.read('https://i.imgur.com/552kzaW.png'), 
welcome = await jimp.read(randomkontol()),  
avatar = await jimp.read(await conn.profilePictureUrl(m.sender, 'image').catch(() => 'https://telegra.ph/file/24fa902ead26340f3df2c.png')), 
status = (await conn.fetchStatus(m.sender).catch(console.log) || {}).status?.slice(0, 30) || 'Not Detected' 
fork icon0
star icon1
watch icon1

+ 9 other calls in file

How does jimp.loadFont work?

jimp.loadFont is a method provided by the Jimp library that allows you to load a font file from a local path or a URL. When you call this method, it returns a Promise that will either resolve with the loaded Jimp Font object or reject with an error if there was a problem loading the font. The Jimp Font object can then be used with various Jimp methods to add text to images with the specified font. The jimp.loadFont method can accept a variety of font file formats, including TrueType (.ttf), OpenType (.otf), and Bitmap (.bmp) fonts. You can also pass in additional options to customize how the font is loaded, such as the font size and the number of characters to pre-cache. Once you have loaded the font, you can use the Jimp.loadFont method to create an instance of a Jimp Font object that can be passed to various Jimp methods for rendering text with the font.

359
360
361
362
363
364
365
366
367
368
const placeholder = new Jimp(w, h, '#121117');
const textW = 1920;
const textH = 140;
const textImage = new Jimp(textW, textH, '#121117');

await Jimp.loadFont(Jimp.FONT_SANS_128_WHITE)
    .then((font) => {
        textImage.print(font, 0, 0, {
            text: `${w}x${h}`,
            alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
fork icon0
star icon0
watch icon1

+ 9 other calls in file

1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
    });
} else if (co["overlay"].m) {
    const url = co["overlay"].m[1];
    const overlay = co["overlay"].m[2];
    processImage(url, attachments, groupInfo, (img, filename) => {
        jimp.loadFont(jimp.FONT_SANS_32_BLACK, (err, font) => {
            if (!err) {
                const width = img.bitmap.width; // Image width
                const height = img.bitmap.height; // Image height
                const textDims = measureText(font, overlay); // Get text dimensions (x,y)
fork icon0
star icon0
watch icon1

+ 28 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
const Jimp = require("jimp");

Jimp.loadFont("./my-font.ttf")
  .then((font) => {
    const image = new Jimp(300, 150, "white");
    image.print(font, 50, 50, "Hello, world!");
    image.write("output.png");
  })
  .catch((error) => {
    console.error(error);
  });

In this example, we first load a TrueType font file named "my-font.ttf" using jimp.loadFont. We then create a new Jimp image with a white background and dimensions of 300x150 pixels. Next, we use the image.print method to print the text "Hello, world!" onto the image using the loaded font. The font argument is passed in as the first parameter to the image.print method, indicating that we want to use the loaded font to render the text. The (50, 50) coordinates specify where the text should be rendered on the image. Finally, we call the image.write method to save the resulting image to a file named "output.png". If there was an error loading the font file, the Promise returned by jimp.loadFont would be rejected and the error would be logged to the console.

65
66
67
68
69
70
71
72
73
74
}          
Promise.all(jimps).then((data) => {
    return Promise.all(jimps);
}).then((data) => {                 
    jimp.loadFont('fonts/font.fnt').then((font) => { 
        jimp.loadFont(jimp.FONT_SANS_64_WHITE).then((font2) => {
            var width = data[0].bitmap.width;

            var x1 = width*0.13;
            var x2 = width*0.33;
fork icon0
star icon0
watch icon1

+ 25 other calls in file

649
650
651
652
653
654
655
656
657
658

const welcomeBanner = await Jimp.loadFont(Jimp.FONT_SANS_64_BLACK);
background.print(welcomeBanner, 165, 30, 'Welcome');
const groupName = await Jimp.loadFont(Jimp.FONT_SANS_32_WHITE);
background.print(groupName, 190, 120, gcName);
const userName = await Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
background.print(userName, 200, 260, fullName);
const memberCount = await Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
background.print(memberCount, 250, 290, call.participantIDs.length - 1 + 'th Member of this group');
await background.writeAsync('welcomeImage.jpg');
fork icon0
star icon0
watch icon1

+ 31 other calls in file

709
710
711
712
713
714
715
716
717
718

async function prepareImages() {
	let pantsBase = await Jimp.read(pantsBelow)
	let pantsTextureFile = await Jimp.read(pantsTexture)
	let pantsOverlay = await Jimp.read(pantsLogoCanvas)
	let font = await Jimp.loadFont(__dirname+"/fonts/rowdies.fnt")
	let blankImage = new Jimp(3000, 500)
	await blankImage.print(font, 10, 10, text)
	await blankImage.autocrop()
	await blankImage.scaleToFit(500,15)
fork icon0
star icon0
watch icon0

+ 5 other calls in file

3
4
5
6
7
8
9
10
11
12
13
14


const addTextWatermarkToImage = async function(inputFile, outputFile, text) {


  try {
    const image = await Jimp.read(inputFile);
    const font = await Jimp.loadFont(Jimp.FONT_SANS_32_BLACK);
    const textData = {
      text,
      alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER,
      alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE,
fork icon0
star icon0
watch icon0