From 9e4251e815c25f1ccabca63160288f80556f5f21 Mon Sep 17 00:00:00 2001 From: Andrew Lisowski Date: Fri, 3 Feb 2023 20:37:42 -0800 Subject: [PATCH] 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 --- packages/plugin-print/src/index.js | 32 +----------------- packages/plugin-print/src/measure-text.js | 40 ++++++++++++++++------- packages/plugin-print/test/print.test.js | 11 +++++++ 3 files changed, 40 insertions(+), 43 deletions(-) diff --git a/packages/plugin-print/src/index.js b/packages/plugin-print/src/index.js index df71548b9..baf8a5c49 100644 --- a/packages/plugin-print/src/index.js +++ b/packages/plugin-print/src/index.js @@ -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) { @@ -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); diff --git a/packages/plugin-print/src/measure-text.js b/packages/plugin-print/src/measure-text.js index a86dfbe3b..510b57f20 100644 --- a/packages/plugin-print/src/measure-text.js +++ b/packages/plugin-print/src/measure-text.js @@ -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; } diff --git a/packages/plugin-print/test/print.test.js b/packages/plugin-print/test/print.test.js index b0292a3ce..fab5b7bcf 100644 --- a/packages/plugin-print/test/print.test.js +++ b/packages/plugin-print/test/print.test.js @@ -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); + }); });