mirror of
https://github.com/foliojs/pdfkit.git
synced 2025-12-08 20:15:54 +00:00
Add fontCache option
This commit is contained in:
parent
ffaec1636e
commit
b3a943bd9d
@ -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
36
tests/unit/font.spec.js
Normal 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);
|
||||
});
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user