Path: blob/master/node_modules/@jimp/plugin-print/test/print.test.js
1126 views
/* eslint key-spacing: ["error", { "align": "value" }] */12import { Jimp, getTestDir, hasOwnProp } from '@jimp/test-utils';3import configure from '@jimp/custom';4import blit from '@jimp/plugin-blit';56import print from '../src';78const jimp = configure({ plugins: [print, blit] }, Jimp);910async function createTextImage(11width,12height,13font,14{ x = 0, y = 0, text, maxWidth, maxHeight }15) {16const loadedFont = await jimp.loadFont(font);17const image = await Jimp.create(width, height, 0xffffffff);1819return image.print(loadedFont, x, y, text, maxWidth, maxHeight);20}2122describe('Write text over image', function() {23this.timeout(30000);2425const fontDefs = {26SANS_8_BLACK: { w: 28, h: 28, bg: 0xffffffff },2728SANS_16_BLACK: { w: 54, h: 54, bg: 0xffffffff },29SANS_32_BLACK: { w: 114, h: 114, bg: 0xffffffff },30SANS_64_BLACK: { w: 220, h: 220, bg: 0xffffffff },3132SANS_8_WHITE: { w: 28, h: 28, bg: 0x000000ff },3334SANS_16_WHITE: { w: 54, h: 54, bg: 0x000000ff },35SANS_32_WHITE: { w: 114, h: 114, bg: 0x000000ff },36SANS_64_WHITE: { w: 220, h: 220, bg: 0x000000ff }37};3839for (const fontName in fontDefs)40if (hasOwnProp(fontDefs, fontName))41((fontName, conf) => {42it('Jimp preset ' + fontName + ' bitmap font', async () => {43const font = await jimp.loadFont(Jimp['FONT_' + fontName]);44const expected =45getTestDir(__dirname) + '/images/' + fontName + '.png';4647const expectedImg = await Jimp.read(expected);48const image = await Jimp.create(conf.w, conf.h, conf.bg);4950image51.print(font, 0, 0, 'This is only a test.', image.bitmap.width)52.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);53});54})(fontName, fontDefs[fontName]);5556it('Jimp preset SANS_16_BLACK bitmap font positioned', async () => {57const font = await jimp.loadFont(Jimp.FONT_SANS_16_BLACK);58const expected =59getTestDir(__dirname) + '/images/SANS_16_BLACK-positioned.png';60const expectedImg = await Jimp.read(expected);61const image = await Jimp.create('300', '100', 0xff8800ff);6263image64.print(font, 150, 50, 'This is only a test.', 100)65.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);66});6768it('Jimp loads font from URL', async () => {69const font = await Jimp.loadFont(70'https://raw.githubusercontent.com/oliver-moran/jimp/master/packages/plugin-print/fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt'71);72const expected =73getTestDir(__dirname) + '/images/SANS_16_BLACK-positioned.png';74const expectedImg = await Jimp.read(expected);75const image = await Jimp.create('300', '100', 0xff8800ff);7677image78.print(font, 150, 50, 'This is only a test.', 100)79.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);80});8182it('Jimp renders ? for unknown characters', async () => {83const font = await jimp.loadFont(Jimp.FONT_SANS_16_BLACK);8485const expected = getTestDir(__dirname) + '/images/unknown-char-test.png';86const expectedImg = await Jimp.read(expected);87const image = await Jimp.read('300', '100', 0xff8800ff);8889image90.print(font, 0, 0, 'ツ ツ ツ', 100)91.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);92});9394it('Jimp can print numbers too', async () => {95const font = await Jimp.loadFont(Jimp.FONT_SANS_16_BLACK);9697const expected = getTestDir(__dirname) + '/images/print-number.png';98const expectedImg = await Jimp.read(expected);99const image = await Jimp.read('300', '100', 0xff8800ff);100101image102.print(font, 0, 0, 12345678, 100)103.bitmap.data.should.be.deepEqual(expectedImg.bitmap.data);104});105106it('left-align text by default', async () => {107const expectedImage = await Jimp.read(108getTestDir(__dirname) + '/images/left-aligned.png'109);110const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {111text: 'This is only a test.',112113maxWidth: 100114});115116expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);117});118119it('left-align text by default when passing object', async () => {120const expectedImage = await Jimp.read(121getTestDir(__dirname) + '/images/left-aligned.png'122);123const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {124text: { text: 'This is only a test.' },125126maxWidth: 100127});128129expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);130});131132it('left-align text when passing object with alignmentX', async () => {133const expectedImage = await Jimp.read(134getTestDir(__dirname) + '/images/left-aligned.png'135);136const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {137text: {138text: 'This is only a test.',139140alignmentX: Jimp.HORIZONTAL_ALIGN_LEFT141},142maxWidth: 100143});144145expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);146});147148it('center-align text when passing object with alignmentX', async () => {149const expectedImage = await Jimp.read(150getTestDir(__dirname) + '/images/center-aligned.png'151);152const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {153text: {154text: 'This is only a test.',155156alignmentX: Jimp.HORIZONTAL_ALIGN_CENTER157},158maxWidth: 100159});160161expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);162});163164it('right-align text when passing object with alignmentX', async () => {165const expectedImage = await Jimp.read(166getTestDir(__dirname) + '/images/right-aligned.png'167);168const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {169text: {170text: 'This is only a test.',171172alignmentX: Jimp.HORIZONTAL_ALIGN_RIGHT173},174maxWidth: 100175});176177expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);178});179180it('middle-align text when passing object with alignmentY', async () => {181const expectedImage = await Jimp.read(182getTestDir(__dirname) + '/images/middle-aligned.png'183);184const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {185text: {186text: 'This is only a test.',187188alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE189},190maxWidth: 100,191192maxHeight: 240193});194195expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);196});197198it('middle-align text when passing object with alignmentY can offset y', async () => {199const expectedImage = await Jimp.read(200getTestDir(__dirname) + '/images/middle-aligned-y.png'201);202const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {203y: 50,204205text: {206text: 'This is only a test.',207208alignmentY: Jimp.VERTICAL_ALIGN_MIDDLE209},210maxWidth: 100,211212maxHeight: 240213});214215expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);216});217218it('bottom-align text when passing object with alignmentY', async () => {219const expectedImage = await Jimp.read(220getTestDir(__dirname) + '/images/bottom-aligned.png'221);222const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {223text: {224text: 'This is only a test.',225226alignmentY: Jimp.VERTICAL_ALIGN_BOTTOM227},228maxWidth: 100,229230maxHeight: 240231});232233expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);234});235236it('bottom-align text when passing object with alignmentY offset y', async () => {237const expectedImage = await Jimp.read(238getTestDir(__dirname) + '/images/bottom-aligned-y.png'239);240const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {241y: 100,242243text: {244text: 'This is only a test.',245246alignmentY: Jimp.VERTICAL_ALIGN_BOTTOM247},248maxWidth: 100,249250maxHeight: 100251});252253expectedImage.bitmap.data.should.be.deepEqual(textImage.bitmap.data);254});255256it('exposes print y position in cb', async () => {257const expectedImage = await Jimp.read(258getTestDir(__dirname) + '/images/spacing.png'259);260261const loadedFont = await jimp.loadFont(Jimp.FONT_SANS_16_BLACK);262const image = await Jimp.create(500, 500, 0xffffffff);263264image.print(265loadedFont,2660,2670,268'One two three four fix six seven eight nine ten eleven twelve',269250,270(err, image, { x, y }) => {271image.print(272loadedFont,273x,274y + 50,275'thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty',276250277);278}279);280281expectedImage.bitmap.data.should.be.deepEqual(image.bitmap.data);282});283});284285286