From 7d0bfc4bae6dd7cb7f489de47bd73d02bf844259 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Sun, 2 Sep 2018 06:23:37 -0700 Subject: [PATCH] Update load-bmfont add test that we can load font from URL (#589) --- packages/jimp/README.md | 4 +-- packages/jimp/test/print.test.js | 42 ++++++++++-------------------- packages/plugin-print/package.json | 2 +- yarn.lock | 13 +++++++++ 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/packages/jimp/README.md b/packages/jimp/README.md index 7198810ae..3c5dbc07b 100644 --- a/packages/jimp/README.md +++ b/packages/jimp/README.md @@ -265,7 +265,7 @@ image.composite(srcImage, 100, 0, Jimp.BLEND_MULTIPLY, 0.5, 0.9); Jimp supports basic typography using BMFont format (.fnt) even ones in different languages! Just find a bitmap font that is suitable [bitmap fonts](https://en.wikipedia.org/wiki/Bitmap_fonts): ```js -Jimp.loadFont(path).then(font => { +Jimp.loadFont(pathOrURL).then(font => { // load font from .fnt file image.print(font, x, y, message); // print a message on an image. message can be a any type image.print(font, x, y, message, maxWidth); // print a message on an image with text wrapped at maxWidth @@ -275,7 +275,7 @@ Jimp.loadFont(path).then(font => { 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`. ```js -Jimp.loadFont(path).then(font => { +Jimp.loadFont(pathOrURL).then(font => { image.print( font, x, diff --git a/packages/jimp/test/print.test.js b/packages/jimp/test/print.test.js index 946025c39..8013073de 100644 --- a/packages/jimp/test/print.test.js +++ b/packages/jimp/test/print.test.js @@ -62,6 +62,20 @@ describe('Write text over image', function() { .bitmap.data.should.be.deepEqual(expectedImg.bitmap.data); }); + it('Jimp loads font from URL', async () => { + const font = await Jimp.loadFont( + 'https://raw.githubusercontent.com/oliver-moran/jimp/master/packages/plugin-print/fonts/open-sans/open-sans-16-black/open-sans-16-black.fnt' + ); + const expected = + getTestDir() + '/samples/text-samples/SANS_16_BLACK-positioned.png'; + const expectedImg = await Jimp.read(expected); + const image = await Jimp.create('300', '100', 0xff8800ff); + + image + .print(font, 150, 50, 'This is only a test.', 100) + .bitmap.data.should.be.deepEqual(expectedImg.bitmap.data); + }); + it('Jimp renders ? for unknown characters', async () => { const font = await Jimp.loadFont(Jimp.FONT_SANS_16_BLACK); @@ -227,32 +241,4 @@ describe('Write text over image', function() { results[0].bitmap.data.should.be.deepEqual(results[1].bitmap.data); }); }); - - it('exposes print y position in cb', async () => { - const expectedImage = await Jimp.read( - getTestDir() + '/samples/text-samples/spacing.png' - ); - - const loadedFont = await Jimp.loadFont(Jimp.FONT_SANS_16_BLACK); - const image = await Jimp.create(500, 500, 0xffffffff); - - image.print( - loadedFont, - 0, - 0, - 'One two three four fix six seven eight nine ten eleven twelve', - 250, - (err, image, { x, y }) => { - image.print( - loadedFont, - x, - y + 50, - 'thirteen fourteen fifteen sixteen seventeen eighteen nineteen twenty', - 250 - ); - } - ); - - expectedImage.bitmap.data.should.be.deepEqual(image.bitmap.data); - }); }); diff --git a/packages/plugin-print/package.json b/packages/plugin-print/package.json index 4444d7510..4ac45061c 100644 --- a/packages/plugin-print/package.json +++ b/packages/plugin-print/package.json @@ -18,7 +18,7 @@ "dependencies": { "@jimp/utils": "^0.3.9", "core-js": "^2.5.7", - "load-bmfont": "^1.3.1" + "load-bmfont": "^1.4.0" }, "peerDependencies": { "@jimp/custom": ">=0.3.5" diff --git a/yarn.lock b/yarn.lock index 75aca27a0..b2b5fb38d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5317,6 +5317,19 @@ load-bmfont@^1.3.1: xhr "^2.0.1" xtend "^4.0.0" +load-bmfont@^1.4.0: + version "1.4.0" + resolved "https://registry.yarnpkg.com/load-bmfont/-/load-bmfont-1.4.0.tgz#75f17070b14a8c785fe7f5bee2e6fd4f98093b6b" + dependencies: + buffer-equal "0.0.1" + mime "^1.3.4" + parse-bmfont-ascii "^1.0.3" + parse-bmfont-binary "^1.0.5" + parse-bmfont-xml "^1.1.4" + phin "^2.9.1" + xhr "^2.0.1" + xtend "^4.0.0" + load-json-file@^1.0.0: version "1.1.0" resolved "https://registry.yarnpkg.com/load-json-file/-/load-json-file-1.1.0.tgz#956905708d58b4bab4c2261b04f59f31c99374c0"