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

Replace soft hyphen with visible hyphen if line break demands it #1488

Merged
merged 3 commits into from
Jan 2, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Add subset for PDF/UA
- Fix for line breaks in list items (#1486)
- Fix for soft hyphen not being replaced by visible hyphen if necessary (#457)

### [v0.14.0] - 2023-11-09

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ Installation uses the [npm](http://npmjs.org/) package manager. Just type the fo
- Transformations
- Linear and radial gradients
- Text
- Line wrapping
- Line wrapping (with soft hyphen recognition)
- Text alignments
- Bulleted lists
- Font embedding
Expand Down
20 changes: 18 additions & 2 deletions lib/line_wrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import { EventEmitter } from 'events';
import LineBreaker from 'linebreak';

const SOFT_HYPHEN = '\u00AD';
const HYPHEN = '-';

class LineWrapper extends EventEmitter {
constructor(document, options) {
super();
Expand Down Expand Up @@ -73,6 +76,13 @@ class LineWrapper extends EventEmitter {
);
}

canFit(word, w) {
if (word[word.length - 1] != SOFT_HYPHEN) {
return w <= this.spaceLeft;
}
return w + this.wordWidth(HYPHEN) <= this.spaceLeft;
}

eachWord(text, fn) {
// setup a unicode line breaker
let bk;
Expand Down Expand Up @@ -199,13 +209,13 @@ class LineWrapper extends EventEmitter {
this.spaceLeft = this.lineWidth;
}

if (w <= this.spaceLeft) {
if (this.canFit(word, w)) {
buffer += word;
textWidth += w;
wc++;
}

if (bk.required || w > this.spaceLeft) {
if (bk.required || !this.canFit(word, w)) {
// if the user specified a max height and an ellipsis, and is about to pass the
// max height and max columns after the next line, append the ellipsis
const lh = this.document.currentLineHeight(true);
Expand Down Expand Up @@ -246,6 +256,12 @@ class LineWrapper extends EventEmitter {
this.emit('lastLine', options, this);
}

// Previous entry is a soft hyphen - add visible hyphen.
if (buffer[buffer.length - 1] == SOFT_HYPHEN) {
buffer = buffer.slice(0, -1) + HYPHEN;
this.spaceLeft -= this.wordWidth(HYPHEN);
}

emitLine();

// if we've reached the edge of the page,
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion tests/visual/text.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,20 @@ describe('text', function() {
});
});

test('soft hyphen', function() {
return runDocTest(function(doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
doc.text(
'Text with soft hyphen - Lorem ipsum dolor sit amet, consectetur adipiscing elit. Lo ip\u00ADsum',
{ align: 'justify' }
);
doc.text(
'Text with soft hyphen on the edge - ttttestttestttestttestttestttestttestttestttestttestttes\u00ADtt\u00ADt',
{ align: 'justify' }
);
});
});

test('decoration', function() {
return runDocTest(function(doc) {
doc.font('tests/fonts/Roboto-Regular.ttf');
Expand All @@ -31,7 +45,7 @@ describe('text', function() {
strike: true
});
doc.text('Strike', 100, 160, {
underline:true,
underline: true,
strike: true
});
});
Expand Down
Loading