Skip to content

Commit a659e6a

Browse files
kozmozjuurr00hipstersmoothie
authored
Add \n support for image.print #865 (#1265)
* Add \n support for image.print(). This fixes #865 * Add \n support for image.print(). This fixes #865 - more condensed code. * force build * update auto --------- Co-authored-by: juurr00 <juurr00@juurlink.org> Co-authored-by: Andrew Lisowski <lisowski54@gmail.com>
1 parent 9edebb3 commit a659e6a

7 files changed

Lines changed: 2506 additions & 3047 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
src="https://s3.amazonaws.com/pix.iemoji.com/images/emoji/apple/ios-11/256/crayon.png">
44
<h1>Jimp</h1>
55
<p>JavaScript Image Manipulation Program</p>
6-
<p>An image processing library for Node written entirely in JavaScript, with zero native dependencies.</p>
6+
<p>An image processing library for Node written entirely in JavaScript, with zero native dependencies</p>
77
</div>
88

99
<!-- START doctoc generated TOC please keep comment here to allow auto update -->

lerna.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"lerna": "2.11.0",
2+
"lerna": "^7.1.4",
33
"npmClient": "yarn",
44
"registry": "https://registry.npmjs.org/",
5-
"useWorkspaces": true,
6-
"version": "0.22.10"
5+
"version": "0.22.10",
6+
"packages": ["packages/*"]
77
}

package.json

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,10 @@
2727
"postinstall": "patch-package"
2828
},
2929
"devDependencies": {
30-
"@auto-it/all-contributors": "^10.38.4",
31-
"@auto-it/first-time-contributor": "^10.38.4",
32-
"@auto-it/magic-zero": "^10.38.4",
33-
"@auto-it/protected-branch": "^10.38.4",
30+
"@auto-it/all-contributors": "^11.0.5",
31+
"@auto-it/first-time-contributor": "^11.0.5",
32+
"@auto-it/magic-zero": "^11.0.5",
33+
"@auto-it/protected-branch": "^11.0.5",
3434
"@babel/cli": "^7.20.7",
3535
"@babel/core": "^7.20.12",
3636
"@babel/preset-env": "^7.20.2",
@@ -40,7 +40,7 @@
4040
"@typescript-eslint/eslint-plugin": "^5.50.0",
4141
"@typescript-eslint/parser": "^5.50.0",
4242
"all-contributors-cli": "^6.24.0",
43-
"auto": "^10.38.4",
43+
"auto": "^11.0.5",
4444
"babel-plugin-add-module-exports": "^1.0.4",
4545
"babel-plugin-source-map-support": "^2.2.0",
4646
"babel-plugin-transform-inline-environment-variables": "^0.4.3",
@@ -59,7 +59,7 @@
5959
"karma-firefox-launcher": "^1.2.0",
6060
"karma-mocha": "^1.3.0",
6161
"karma-mocha-reporter": "^2.2.5",
62-
"lerna": "^3.16.4",
62+
"lerna": "^7.1.4",
6363
"lint-staged": "^9.2.5",
6464
"mocha": "^6.2.0",
6565
"nyc": "^14.1.1",

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ export function measureText(font, text) {
1616
}
1717

1818
export function splitLines(font, text, maxWidth) {
19-
const words = text.split(" ");
19+
const words = text.replace(/[\r\n]+/g, " \n").split(" ");
20+
2021
const lines = [];
2122
let currentLine = [];
2223
let longestLine = 0;
@@ -25,20 +26,18 @@ export function splitLines(font, text, maxWidth) {
2526
const line = [...currentLine, word].join(" ");
2627
const length = measureText(font, line);
2728

28-
if (length <= maxWidth) {
29+
if (length <= maxWidth && !word.includes("\n")) {
2930
if (length > longestLine) {
3031
longestLine = length;
3132
}
3233

3334
currentLine.push(word);
3435
} else {
3536
lines.push(currentLine);
36-
currentLine = [word];
37+
currentLine = [word.replace("\n", "")];
3738
}
3839
});
39-
4040
lines.push(currentLine);
41-
4241
return {
4342
lines,
4443
longestLine,
2.3 KB
Loading

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,4 +294,18 @@ describe("Write text over image", function () {
294294

295295
expect(height).toEqual(lineHeight);
296296
});
297+
298+
it("text with newlines, default alignment", async () => {
299+
const expectedImage = await Jimp.read(
300+
getTestDir(__dirname) + "/images/with-newlines.png"
301+
);
302+
303+
const textImage = await createTextImage(100, 240, Jimp.FONT_SANS_16_BLACK, {
304+
text: "This \nis only \na \ntest.",
305+
306+
maxWidth: 300,
307+
});
308+
309+
expect(textImage.bitmap.data).toEqual(expectedImage.bitmap.data);
310+
});
297311
});

0 commit comments

Comments
 (0)