From b3a943bd9db4cabfa0de7ae9436d596217d5adfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Am=C3=A9rico?= Date: Sun, 3 Mar 2019 15:17:08 -0300 Subject: [PATCH] Add fontCache option --- lib/font/embedded.js | 7 ++++++- tests/unit/font.spec.js | 36 ++++++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 tests/unit/font.spec.js diff --git a/lib/font/embedded.js b/lib/font/embedded.js index a920188..09892c5 100644 --- a/lib/font/embedded.js +++ b/lib/font/embedded.js @@ -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; diff --git a/tests/unit/font.spec.js b/tests/unit/font.spec.js new file mode 100644 index 0000000..79f20f7 --- /dev/null +++ b/tests/unit/font.spec.js @@ -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); + }); +});