Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add \n support for image.print #865 #1265

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
src="https://s3.amazonaws.com/pix.iemoji.com/images/emoji/apple/ios-11/256/crayon.png">
<h1>Jimp</h1>
<p>JavaScript Image Manipulation Program</p>
<p>An image processing library for Node written entirely in JavaScript, with zero native dependencies.</p>
<p>An image processing library for Node written entirely in JavaScript, with zero native dependencies</p>
</div>

<!-- START doctoc generated TOC please keep comment here to allow auto update -->
Expand Down
6 changes: 3 additions & 3 deletions lerna.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"lerna": "2.11.0",
"lerna": "^7.1.4",
"npmClient": "yarn",
"registry": "https://registry.npmjs.org/",
"useWorkspaces": true,
"version": "0.22.10"
"version": "0.22.10",
"packages": ["packages/*"]
}
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@
"postinstall": "patch-package"
},
"devDependencies": {
"@auto-it/all-contributors": "^10.38.4",
"@auto-it/first-time-contributor": "^10.38.4",
"@auto-it/magic-zero": "^10.38.4",
"@auto-it/protected-branch": "^10.38.4",
"@auto-it/all-contributors": "^11.0.5",
"@auto-it/first-time-contributor": "^11.0.5",
"@auto-it/magic-zero": "^11.0.5",
"@auto-it/protected-branch": "^11.0.5",
"@babel/cli": "^7.20.7",
"@babel/core": "^7.20.12",
"@babel/preset-env": "^7.20.2",
Expand All @@ -40,7 +40,7 @@
"@typescript-eslint/eslint-plugin": "^5.50.0",
"@typescript-eslint/parser": "^5.50.0",
"all-contributors-cli": "^6.24.0",
"auto": "^10.38.4",
"auto": "^11.0.5",
"babel-plugin-add-module-exports": "^1.0.4",
"babel-plugin-source-map-support": "^2.2.0",
"babel-plugin-transform-inline-environment-variables": "^0.4.3",
Expand All @@ -59,7 +59,7 @@
"karma-firefox-launcher": "^1.2.0",
"karma-mocha": "^1.3.0",
"karma-mocha-reporter": "^2.2.5",
"lerna": "^3.16.4",
"lerna": "^7.1.4",
"lint-staged": "^9.2.5",
"mocha": "^6.2.0",
"nyc": "^14.1.1",
Expand Down
9 changes: 4 additions & 5 deletions packages/plugin-print/src/measure-text.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ 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;
Expand All @@ -25,20 +26,18 @@ export function splitLines(font, text, maxWidth) {
const line = [...currentLine, word].join(" ");
const length = measureText(font, line);

if (length <= maxWidth) {
if (length <= maxWidth && !word.includes("\n")) {
if (length > longestLine) {
longestLine = length;
}

currentLine.push(word);
} else {
lines.push(currentLine);
currentLine = [word];
currentLine = [word.replace("\n", "")];
}
});

lines.push(currentLine);

return {
lines,
longestLine,
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions packages/plugin-print/test/print.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -294,4 +294,18 @@ 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);
});
});