mirror of
https://github.com/foliojs/pdfkit.git
synced 2026-02-01 16:56:57 +00:00
Replace soft hyphen with visible hyphen if line break demands it (#1488)
* Replace soft hyphen with visible hyphen if line break demands it * Add changelog entry for #457 * Add soft hyphen support in readme
This commit is contained in:
parent
2d5b4160ad
commit
1f70b450a2
@ -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
|
||||
|
||||
|
||||
@ -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
|
||||
|
||||
@ -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();
|
||||
@ -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;
|
||||
@ -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);
|
||||
@ -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,
|
||||
|
||||
Binary file not shown.
|
After Width: | Height: | Size: 48 KiB |
@ -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');
|
||||
@ -31,7 +45,7 @@ describe('text', function() {
|
||||
strike: true
|
||||
});
|
||||
doc.text('Strike', 100, 160, {
|
||||
underline:true,
|
||||
underline: true,
|
||||
strike: true
|
||||
});
|
||||
});
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user