Skip to content

Commit 9e4251e

Browse files
use a consistent text layouting algorithm (#1133)
* bug: measureText() is not consistent with measureTextHeight() * use one layouting algorithm consistently * plugin-print v0.16.2 --------- Co-authored-by: Ilia Pozdnyakov <[email protected]>
1 parent bb5433d commit 9e4251e

File tree

3 files changed

+40
-43
lines changed

3 files changed

+40
-43
lines changed

packages/plugin-print/src/index.js

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import Path from "path";
22
import bMFont from "load-bmfont";
33
import { isNodePattern, throwError } from "@jimp/utils";
4-
import { measureText, measureTextHeight } from "./measure-text";
4+
import { measureText, measureTextHeight, splitLines } from "./measure-text";
55

66
function xOffsetBasedOnAlignment(constants, font, line, maxWidth, alignment) {
77
if (alignment === constants.HORIZONTAL_ALIGN_LEFT) {
@@ -57,36 +57,6 @@ function printText(font, x, y, text, defaultCharWidth) {
5757
}
5858
}
5959

60-
function splitLines(font, text, maxWidth) {
61-
const words = text.split(" ");
62-
const lines = [];
63-
let currentLine = [];
64-
let longestLine = 0;
65-
66-
words.forEach((word) => {
67-
const line = [...currentLine, word].join(" ");
68-
const length = measureText(font, line);
69-
70-
if (length <= maxWidth) {
71-
if (length > longestLine) {
72-
longestLine = length;
73-
}
74-
75-
currentLine.push(word);
76-
} else {
77-
lines.push(currentLine);
78-
currentLine = [word];
79-
}
80-
});
81-
82-
lines.push(currentLine);
83-
84-
return {
85-
lines,
86-
longestLine,
87-
};
88-
}
89-
9060
function loadPages(Jimp, dir, pages) {
9161
const newPages = pages.map((page) => {
9262
return Jimp.read(dir + "/" + page);

packages/plugin-print/src/measure-text.js

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,38 @@ export function measureText(font, text) {
1515
return x;
1616
}
1717

18-
export function measureTextHeight(font, text, maxWidth) {
18+
export function splitLines(font, text, maxWidth) {
1919
const words = text.split(" ");
20-
let line = "";
21-
let textTotalHeight = font.common.lineHeight;
20+
const lines = [];
21+
let currentLine = [];
22+
let longestLine = 0;
23+
24+
words.forEach((word) => {
25+
const line = [...currentLine, word].join(" ");
26+
const length = measureText(font, line);
2227

23-
for (let n = 0; n < words.length; n++) {
24-
const testLine = line + words[n] + " ";
25-
const testWidth = measureText(font, testLine);
28+
if (length <= maxWidth) {
29+
if (length > longestLine) {
30+
longestLine = length;
31+
}
2632

27-
if (testWidth > maxWidth && n > 0) {
28-
textTotalHeight += font.common.lineHeight;
29-
line = words[n] + " ";
33+
currentLine.push(word);
3034
} else {
31-
line = testLine;
35+
lines.push(currentLine);
36+
currentLine = [word];
3237
}
33-
}
38+
});
39+
40+
lines.push(currentLine);
41+
42+
return {
43+
lines,
44+
longestLine,
45+
};
46+
}
47+
48+
export function measureTextHeight(font, text, maxWidth) {
49+
const { lines } = splitLines(font, text, maxWidth);
3450

35-
return textTotalHeight;
51+
return lines.length * font.common.lineHeight;
3652
}

packages/plugin-print/test/print.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,4 +281,15 @@ describe("Write text over image", function () {
281281

282282
expectedImage.bitmap.data.should.be.deepEqual(image.bitmap.data);
283283
});
284+
285+
it("measureText is consistent with measureTextWidth", async () => {
286+
const font = await jimp.loadFont(Jimp.FONT_SANS_16_BLACK);
287+
288+
const text = "n n n";
289+
const width = jimp.measureText(font, text);
290+
const height = jimp.measureTextHeight(font, text, width);
291+
const lineHeight = jimp.measureTextHeight(font, text, Infinity);
292+
293+
height.should.be.deepEqual(lineHeight);
294+
});
284295
});

0 commit comments

Comments
 (0)