Skip to content

Commit

Permalink
Add \n support for image.print #865 (#1265)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Feb 23, 2024
1 parent 9edebb3 commit a659e6a
Show file tree
Hide file tree
Showing 7 changed files with 2,506 additions and 3,047 deletions.
2 changes: 1 addition & 1 deletion README.md
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
@@ -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
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
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
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);
});
});

0 comments on commit a659e6a

Please sign in to comment.