Add fontCache option

This commit is contained in:
Luiz Américo 2019-03-03 15:17:08 -03:00
parent ffaec1636e
commit b3a943bd9d
2 changed files with 42 additions and 1 deletions

View File

@ -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.fontCache !== false) {
this.layoutCache = Object.create(null);
}
}
layoutRun(text, features) {
@ -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;

36
tests/unit/font.spec.js Normal file
View File

@ -0,0 +1,36 @@
const PDFFontFactory = require('../../lib/font_factory').default;
const PDFDocument = require('../../lib/document').default;
describe('EmbeddedFont', () => {
test('no fontCache 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('fontCache = false', () => {
const document = new PDFDocument({ fontCache: 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);
});
});