Skip to content

Commit

Permalink
If char is unknown white space char. use a default width for text pri…
Browse files Browse the repository at this point in the history
…nting (#550)
  • Loading branch information
hipstersmoothie authored and edi9999 committed Aug 17, 2018
1 parent 7c12320 commit 8b43c15
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions src/image-manipulation/text.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,21 +67,27 @@ function drawCharacter(image, font, x, y, char) {
return image;
}

function printText(font, x, y, text) {
function printText(font, x, y, text, defaultCharWidth) {
for (let i = 0; i < text.length; i++) {
let char;

if (font.chars[text[i]]) {
char = text[i];
} else if (/\s/.test(text[i])) {
char = '';
} else {
char = '?';
}

drawCharacter(this, font, x, y, font.chars[char]);
const fontChar = font.chars[char] || {};
const fontKerning = font.kernings[char];

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

x +=
(font.kernings[char] && font.kernings[char][text[i + 1]]
? font.kernings[char][text[i + 1]]
: 0) + (font.chars[char].xadvance || 0);
(fontKerning && fontKerning[text[i + 1]]
? fontKerning[text[i + 1]]
: 0) + (fontChar.xadvance || defaultCharWidth);
}
}

Expand Down Expand Up @@ -165,6 +171,7 @@ export function print(font, x, y, text, maxWidth, maxHeight, cb) {

const words = text.split(' ');
let line = '';
const defaultCharWidth = font.chars[0].xadvance;

for (let n = 0; n < words.length; n++) {
const testLine = line + words[n] + ' ';
Expand All @@ -189,7 +196,8 @@ export function print(font, x, y, text, maxWidth, maxHeight, cb) {
font,
x + xOffsetBasedOnAlignment(font, line, maxWidth, alignmentX),
y,
line
line,
defaultCharWidth
);

if (isNodePattern(cb)) {
Expand Down

0 comments on commit 8b43c15

Please sign in to comment.