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 1/2] 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); + }); +}); From dae45a44ae6122f2c38c5642ac06c62c78496890 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luiz=20Am=C3=A9rico?= Date: Sun, 3 Mar 2019 19:50:14 -0300 Subject: [PATCH 2/2] Rename fontCache to fontLayoutCache --- lib/font/embedded.js | 2 +- tests/unit/font.spec.js | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/font/embedded.js b/lib/font/embedded.js index 09892c5..a185d2d 100644 --- a/lib/font/embedded.js +++ b/lib/font/embedded.js @@ -23,7 +23,7 @@ class EmbeddedFont extends PDFFont { this.lineGap = this.font.lineGap * this.scale; this.bbox = this.font.bbox; - if (document.options.fontCache !== false) { + if (document.options.fontLayoutCache !== false) { this.layoutCache = Object.create(null); } } diff --git a/tests/unit/font.spec.js b/tests/unit/font.spec.js index 79f20f7..a6b5ee6 100644 --- a/tests/unit/font.spec.js +++ b/tests/unit/font.spec.js @@ -2,7 +2,7 @@ const PDFFontFactory = require('../../lib/font_factory').default; const PDFDocument = require('../../lib/document').default; describe('EmbeddedFont', () => { - test('no fontCache option', () => { + test('no fontLayoutCache option', () => { const document = new PDFDocument(); const font = PDFFontFactory.open( document, @@ -18,8 +18,8 @@ describe('EmbeddedFont', () => { expect(runSpy).toBeCalledTimes(1); }); - test('fontCache = false', () => { - const document = new PDFDocument({ fontCache: false }); + test('fontLayoutCache = false', () => { + const document = new PDFDocument({ fontLayoutCache: false }); const font = PDFFontFactory.open( document, 'tests/fonts/Roboto-Regular.ttf'