From 9dd782aff8b59b4143da07557d16a8aa5b70148b Mon Sep 17 00:00:00 2001 From: juurr00 Date: Wed, 1 Nov 2023 14:58:19 +0100 Subject: [PATCH] Add \n support for image.print(). This fixes #865 --- packages/plugin-print/src/measure-text.js | 14 +++++++------- .../plugin-print/test/images/with-newlines.png | Bin 0 -> 2355 bytes packages/plugin-print/test/print.test.js | 13 +++++++++++++ 3 files changed, 20 insertions(+), 7 deletions(-) create mode 100644 packages/plugin-print/test/images/with-newlines.png diff --git a/packages/plugin-print/src/measure-text.js b/packages/plugin-print/src/measure-text.js index 510b57f20..c112db53b 100644 --- a/packages/plugin-print/src/measure-text.js +++ b/packages/plugin-print/src/measure-text.js @@ -16,29 +16,29 @@ export function measureText(font, text) { } export function splitLines(font, text, maxWidth) { - const words = text.split(" "); + const words = text.replace(/[\r\n]+/g, " \n").split(" "); + const lines = []; let currentLine = []; let longestLine = 0; - words.forEach((word) => { const line = [...currentLine, word].join(" "); const length = measureText(font, line); - if (length <= maxWidth) { + const withinMaxWidth = length <= maxWidth; + const containsNewline = word.includes("\n"); + + if (withinMaxWidth && !containsNewline) { if (length > longestLine) { longestLine = length; } - currentLine.push(word); } else { lines.push(currentLine); - currentLine = [word]; + currentLine = [word.replace("\n", "")]; } }); - lines.push(currentLine); - return { lines, longestLine, diff --git a/packages/plugin-print/test/images/with-newlines.png b/packages/plugin-print/test/images/with-newlines.png new file mode 100644 index 0000000000000000000000000000000000000000..8da2d115a3d653ff8cc951cae8334e86c43b0c34 GIT binary patch literal 2355 zcmeHJ`Bzid8bzxREyxsX!lXWk3{ACQL}Y;sHNgNu5Uz_@X%R_i3ZYa86&YMYD4G{S z%a91fAXEun+gO7lGlbC)ODd^NQ>KFCWzh%-DGd-S@5J_Xt@ix`?}zupz27>+J?Go| z+uvUIN>pU1#diDcCMG5p#A8QKfS3fH=Ll2q{?$&h$;1S)PCObMdw$crq%P|zb!X2~ zcbdiUmuJqL`~ivAZRyJ1oDx@cFqKt*=-W3=?kp}+>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bP-!$UJc5E;Ak25tDd*6+vWfzGbQ zUdd4DuGa4Eo zx@_a7`keG!Yk!`@^mS}FsPn~J#?Y7N@Vi+CL*zS3bD(|zy=xJDgBFIk)YNpqRH;Z(C3k{Of4;%l^rC$tsn}y)r;_}At1Eon@?SdpfC9X!SbBK&9q#~T+5A& zMrOtr8&bMnHv{V^m)lQR^4dUMpJiZrM#ekr&wtC8Q=95w&&lqKa(Up!qs8uxk;J1}qq6^v5K1sf@nBJg2%s~ni@~A$b z5G#wr?gT3M3+a#l+<5xE_v+yYVa&MhzwP!v8vct1|9_J2@xMyQi`P@f=Zl;KaW>#* O$b^WGJSqxFDEK%05@rto literal 0 HcmV?d00001 diff --git a/packages/plugin-print/test/print.test.js b/packages/plugin-print/test/print.test.js index ed05e30ef..71766af0e 100644 --- a/packages/plugin-print/test/print.test.js +++ b/packages/plugin-print/test/print.test.js @@ -294,4 +294,17 @@ describe("Write text over image", function () { expect(height).toEqual(lineHeight); }); + + it("text with newlines, default alignment", async () => { + const expectedImage = await Jimp.read( + getTestDir(__dirname) + "/images/with-newlines.png" + ); + + const textImage = await createTextImage(100, 240, Jimp.FONT_SANS_16_BLACK, { + text: "This \nis only \na \ntest.", + maxWidth: 300, + }); + + expect(textImage.bitmap.data).toEqual(expectedImage.bitmap.data); + }); });