Skip to content

Commit

Permalink
feat: Add newline support
Browse files Browse the repository at this point in the history
Fixes #3
  • Loading branch information
bostrom committed Nov 19, 2016
1 parent 21349e2 commit f50d0b7
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 8 deletions.
4 changes: 2 additions & 2 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = {
"max-params": "off",
"max-statements": "off",
"max-statements-per-line": "error",
"multiline-ternary": "error",
"multiline-ternary": "off",
"new-cap": "error",
"new-parens": "error",
"newline-after-var": "off",
Expand Down Expand Up @@ -162,7 +162,7 @@ module.exports = {
"no-sync": "off",
"no-tabs": "error",
"no-template-curly-in-string": "error",
"no-ternary": "error",
"no-ternary": "off",
"no-throw-literal": "error",
"no-trailing-spaces": "error",
"no-undef-init": "error",
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ Copy that line into the address field of your browser and you should see the gen

The image generator will adjust the height of the image automatically to fit all text, while the width of the image is fixed to the specified width.

Line breaks can be forced with `\n`.

## Configuring

The ```generate``` function takes an optional second parameter containing configuration options for the image. All configuraion parameters are optional. The available options are as follows.
Expand Down
30 changes: 24 additions & 6 deletions lib/text-to-image.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,24 +62,42 @@ function createTextData(text, maxWidth, fontSize, lineHeight, bgColor, textColor
textContext.textBaseline = 'top';

// split the text into words
var words = text.split(' ');
var words = text.split(' '),
wordCount = words.length;

// the start of the first line
var line = '';
var line = '',
addNewLines = [];

for (var n = 0; n < wordCount; n++) {
var word = words[n];

if (/\n/.test(words[n])) {
var parts = words[n].split('\n');
// use the first word before the newline(s)
word = parts.shift();
// mark the next word as beginning with newline
addNewLines.push(n + 1);
// return the rest of the parts to the words array at the same index
words.splice(n + 1, 0, parts.join('\n'));
wordCount += 1;
}

for (var n = 0; n < words.length; n++) {
// append one word to the line and see
// if its width exceeds the maxWidth
var testLine = line + words[n] + ' ';
var testLine = line + word + ' ';
var testLineWidth = textContext.measureText(testLine).width;

if (testLineWidth > maxWidth && n > 0) {
// if the line is marked as starting with a newline
// OR if the line is too long, add a newline
if (addNewLines.indexOf(n) > -1 || testLineWidth > maxWidth && n > 0) {
// if the line exceeded the width with one additional word
// just paint the line without the word
textContext.fillText(line, textX, textY);

// start a new line with the last word
line = words[n] + ' ';
// and add the following (if this word was a newline word)
line = word + ' ';

// move the pen down
textY += lineHeight;
Expand Down
38 changes: 38 additions & 0 deletions test/text-to-image.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,44 @@ describe("the text-to-image generator", function () {
});
});

it("should create a new lines when a \\n occurrs", function () {
return Promise.all([
imageGenerator.generate('Hello world', {
debug: true
}),
imageGenerator.generate('Hello world\nhello again', {
debug: true
})
]).then(function () {
var images = glob.sync(path.join(process.cwd(), '*.png'));
// expect(images).to.have.lengthOf(2);
var dimensions1 = sizeOf(images[0]);
var dimensions2 = sizeOf(images[1]);
expect(dimensions1.height).to.be.above(0);
expect(dimensions1.height).to.be.below(dimensions2.height);
expect(dimensions1.width).to.equal(dimensions2.width);
});
});

it("should create a new lines when a multiple \\n occurrs", function () {
return Promise.all([
imageGenerator.generate('Hello world\nhello again', {
debug: true
}),
imageGenerator.generate('Hello world\n\n\nhello again', {
debug: true
})
]).then(function () {
var images = glob.sync(path.join(process.cwd(), '*.png'));
// expect(images).to.have.lengthOf(2);
var dimensions1 = sizeOf(images[0]);
var dimensions2 = sizeOf(images[1]);
expect(dimensions1.height).to.be.above(0);
expect(dimensions1.height).to.be.below(dimensions2.height);
expect(dimensions1.width).to.equal(dimensions2.width);
});
});

it("should default to a 400 px wide image", function () {
return Promise.all([
imageGenerator.generate('Lorem ipsum dolor sit amet.', {
Expand Down

0 comments on commit f50d0b7

Please sign in to comment.