Skip to content
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
7 changes: 6 additions & 1 deletion lib/font/embedded.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ class EmbeddedFont extends PDFFont {
this.lineGap = this.font.lineGap * this.scale;
this.bbox = this.font.bbox;

this.layoutCache = Object.create(null);
if (document.options.fontLayoutCache !== false) {
this.layoutCache = Object.create(null);
}
}

layoutRun(text, features) {
Expand All @@ -43,6 +45,9 @@ class EmbeddedFont extends PDFFont {
}

layoutCached(text) {
if (!this.layoutCache) {
return this.layoutRun(text);
}
let cached;
if ((cached = this.layoutCache[text])) {
return cached;
Expand Down
36 changes: 36 additions & 0 deletions tests/unit/font.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const PDFFontFactory = require('../../lib/font_factory').default;
const PDFDocument = require('../../lib/document').default;

describe('EmbeddedFont', () => {
test('no fontLayoutCache option', () => {
const document = new PDFDocument();
const font = PDFFontFactory.open(
document,
'tests/fonts/Roboto-Regular.ttf'
);
const runSpy = jest.spyOn(font, 'layoutRun');

font.layout('test');
font.layout('test');
font.layout('test');
font.layout('test');

expect(runSpy).toBeCalledTimes(1);
});

test('fontLayoutCache = false', () => {
const document = new PDFDocument({ fontLayoutCache: false });
const font = PDFFontFactory.open(
document,
'tests/fonts/Roboto-Regular.ttf'
);
const runSpy = jest.spyOn(font, 'layoutRun');

font.layout('test');
font.layout('test');
font.layout('test');
font.layout('test');

expect(runSpy).toBeCalledTimes(4);
});
});