Skip to content

Commit

Permalink
Update load-bmfont add test that we can load font from URL (jimp-dev#589
Browse files Browse the repository at this point in the history
)
  • Loading branch information
hipstersmoothie authored and edi9999 committed Sep 2, 2018
1 parent c09d130 commit 7d0bfc4
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
4 changes: 2 additions & 2 deletions packages/jimp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
42 changes: 14 additions & 28 deletions packages/jimp/test/print.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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);
});
});
2 changes: 1 addition & 1 deletion packages/plugin-print/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
13 changes: 13 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 7d0bfc4

Please sign in to comment.