diff --git a/lib/font/embedded.js b/lib/font/embedded.js index 0eddfe0e..7224d0d8 100644 --- a/lib/font/embedded.js +++ b/lib/font/embedded.js @@ -1,3 +1,4 @@ +import LRUCache from 'lru-cache'; import PDFFont from '../font'; const toHex = function(num) { @@ -24,7 +25,9 @@ class EmbeddedFont extends PDFFont { this.bbox = this.font.bbox; if (document.options.fontLayoutCache !== false) { - this.layoutCache = Object.create(null); + this.layoutCache = new LRUCache({ + max: document.options.fontLayoutMaxCacheSize || 1000 + }); } } @@ -49,12 +52,12 @@ class EmbeddedFont extends PDFFont { return this.layoutRun(text); } let cached; - if ((cached = this.layoutCache[text])) { + if ((cached = this.layoutCache.get(text))) { return cached; } const run = this.layoutRun(text); - this.layoutCache[text] = run; + this.layoutCache.set(text, run); return run; } diff --git a/package.json b/package.json index 0bbbf212..4f956548 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "fontkit": "^1.8.1", "jpeg-exif": "^1.1.4", "linebreak": "^1.0.2", + "lru-cache": "^7.0.0", "png-js": "^1.0.0" }, "scripts": { @@ -89,4 +90,4 @@ "/tests/unit/setupTests.js" ] } -} \ No newline at end of file +} diff --git a/yarn.lock b/yarn.lock index 273331f5..6c750215 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5030,6 +5030,11 @@ lru-cache@^6.0.0: dependencies: yallist "^4.0.0" +lru-cache@^7.0.0: + version "7.18.3" + resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.18.3.tgz#f793896e0fd0e954a59dfdd82f0773808df6aa89" + integrity sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA== + magic-string@0.25.1: version "0.25.1" resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.1.tgz#b1c248b399cd7485da0fe7385c2fc7011843266e"