Skip to content

Commit

Permalink
Print Perf Enhancement (#616)
Browse files Browse the repository at this point in the history
* faster lint

* easier to understand

* add deps

* lint

* crop no longer needed
  • Loading branch information
hipstersmoothie authored Sep 17, 2018
1 parent 8d6ecf8 commit 558e4fe
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 25 deletions.
5 changes: 3 additions & 2 deletions packages/plugin-print/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@
"load-bmfont": "^1.4.0"
},
"peerDependencies": {
"@jimp/custom": ">=0.3.5"
"@jimp/custom": ">=0.3.5",
"@jimp/plugin-blit": ">=0.3.5"
},
"devDependencies": {
"@jimp/custom": "^0.5.0",
"@jimp/plugin-crop": "^0.5.0",
"@jimp/plugin-blit": "^0.5.0",
"@jimp/test-utils": "^0.5.0"
},
"publishConfig": {
Expand Down
44 changes: 23 additions & 21 deletions packages/plugin-print/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@ function xOffsetBasedOnAlignment(constants, font, line, maxWidth, alignment) {

function drawCharacter(image, font, x, y, char) {
if (char.width > 0 && char.height > 0) {
let imageChar = char.image;

if (!imageChar) {
imageChar = font.pages[char.page]
.cloneQuiet()
.crop(char.x, char.y, char.width, char.height);
char.image = imageChar;
}

return image.composite(imageChar, x + char.xoffset, y + char.yoffset);
const characterPage = font.pages[char.page];

image.blit(
characterPage,
x + char.xoffset,
y + char.yoffset,
char.x,
char.y,
char.width,
char.height
);
}

return image;
Expand All @@ -49,9 +50,10 @@ function printText(font, x, y, text, defaultCharWidth) {

drawCharacter(this, font, x, y, fontChar || {});

x +=
(fontKerning && fontKerning[text[i + 1]] ? fontKerning[text[i + 1]] : 0) +
(fontChar.xadvance || defaultCharWidth);
const kerning =
fontKerning && fontKerning[text[i + 1]] ? fontKerning[text[i + 1]] : 0;

x += kerning + (fontChar.xadvance || defaultCharWidth);
}
}

Expand Down Expand Up @@ -290,18 +292,18 @@ export default () => ({

lines.forEach(line => {
const lineString = line.join(' ');
const alignmentWidth = xOffsetBasedOnAlignment(
this.constructor,
font,
lineString,
maxWidth,
alignmentX
);

printText.call(
this,
font,
x +
xOffsetBasedOnAlignment(
this.constructor,
font,
lineString,
maxWidth,
alignmentX
),
x + alignmentWidth,
y,
lineString,
defaultCharWidth
Expand Down
Binary file modified packages/plugin-print/test/images/SANS_16_BLACK-positioned.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/plugin-print/test/images/print-number.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified packages/plugin-print/test/images/unknown-char-test.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 4 additions & 2 deletions packages/plugin-print/test/print.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

import { Jimp, getTestDir, hasOwnProp } from '@jimp/test-utils';
import configure from '@jimp/custom';
import crop from '@jimp/plugin-crop';
import blit from '@jimp/plugin-blit';

import print from '../src';

const jimp = configure({ plugins: [print, crop] }, Jimp);
const jimp = configure({ plugins: [print, blit] }, Jimp);

async function createTextImage(
width,
Expand Down Expand Up @@ -110,6 +110,7 @@ describe('Write text over image', function() {
);
const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {
text: 'This is only a test.',

maxWidth: 100
});

Expand All @@ -122,6 +123,7 @@ describe('Write text over image', function() {
);
const textImage = await createTextImage(320, 240, Jimp.FONT_SANS_16_BLACK, {
text: { text: 'This is only a test.' },

maxWidth: 100
});

Expand Down

0 comments on commit 558e4fe

Please sign in to comment.