Skip to content

Commit

Permalink
use a consistent text layouting algorithm (#1133)
Browse files Browse the repository at this point in the history
* bug: measureText() is not consistent with measureTextHeight()

* use one layouting algorithm consistently

* plugin-print v0.16.2

---------

Co-authored-by: Ilia Pozdnyakov <ilia.pozdnyakov@ya.ru>
  • Loading branch information
hipstersmoothie and iliazeus authored Feb 4, 2023
1 parent bb5433d commit 9e4251e
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 43 deletions.
32 changes: 1 addition & 31 deletions packages/plugin-print/src/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Path from "path";
import bMFont from "load-bmfont";
import { isNodePattern, throwError } from "@jimp/utils";
import { measureText, measureTextHeight } from "./measure-text";
import { measureText, measureTextHeight, splitLines } from "./measure-text";

function xOffsetBasedOnAlignment(constants, font, line, maxWidth, alignment) {
if (alignment === constants.HORIZONTAL_ALIGN_LEFT) {
Expand Down Expand Up @@ -57,36 +57,6 @@ function printText(font, x, y, text, defaultCharWidth) {
}
}

function splitLines(font, text, maxWidth) {
const words = text.split(" ");
const lines = [];
let currentLine = [];
let longestLine = 0;

words.forEach((word) => {
const line = [...currentLine, word].join(" ");
const length = measureText(font, line);

if (length <= maxWidth) {
if (length > longestLine) {
longestLine = length;
}

currentLine.push(word);
} else {
lines.push(currentLine);
currentLine = [word];
}
});

lines.push(currentLine);

return {
lines,
longestLine,
};
}

function loadPages(Jimp, dir, pages) {
const newPages = pages.map((page) => {
return Jimp.read(dir + "/" + page);
Expand Down
40 changes: 28 additions & 12 deletions packages/plugin-print/src/measure-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,22 +15,38 @@ export function measureText(font, text) {
return x;
}

export function measureTextHeight(font, text, maxWidth) {
export function splitLines(font, text, maxWidth) {
const words = text.split(" ");
let line = "";
let textTotalHeight = font.common.lineHeight;
const lines = [];
let currentLine = [];
let longestLine = 0;

words.forEach((word) => {
const line = [...currentLine, word].join(" ");
const length = measureText(font, line);

for (let n = 0; n < words.length; n++) {
const testLine = line + words[n] + " ";
const testWidth = measureText(font, testLine);
if (length <= maxWidth) {
if (length > longestLine) {
longestLine = length;
}

if (testWidth > maxWidth && n > 0) {
textTotalHeight += font.common.lineHeight;
line = words[n] + " ";
currentLine.push(word);
} else {
line = testLine;
lines.push(currentLine);
currentLine = [word];
}
}
});

lines.push(currentLine);

return {
lines,
longestLine,
};
}

export function measureTextHeight(font, text, maxWidth) {
const { lines } = splitLines(font, text, maxWidth);

return textTotalHeight;
return lines.length * font.common.lineHeight;
}
11 changes: 11 additions & 0 deletions packages/plugin-print/test/print.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -281,4 +281,15 @@ describe("Write text over image", function () {

expectedImage.bitmap.data.should.be.deepEqual(image.bitmap.data);
});

it("measureText is consistent with measureTextWidth", async () => {
const font = await jimp.loadFont(Jimp.FONT_SANS_16_BLACK);

const text = "n n n";
const width = jimp.measureText(font, text);
const height = jimp.measureTextHeight(font, text, width);
const lineHeight = jimp.measureTextHeight(font, text, Infinity);

height.should.be.deepEqual(lineHeight);
});
});

0 comments on commit 9e4251e

Please sign in to comment.