Skip to content

Commit 2a5baef

Browse files
committed
feat: Add LRUCache for font layout caching
1 parent b35c6f9 commit 2a5baef

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

lib/font/embedded.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LRUCache } from 'lru-cache';
12
import PDFFont from '../font';
23

34
const toHex = function(num) {
@@ -24,7 +25,9 @@ class EmbeddedFont extends PDFFont {
2425
this.bbox = this.font.bbox;
2526

2627
if (document.options.fontLayoutCache !== false) {
27-
this.layoutCache = Object.create(null);
28+
this.layoutCache = new LRUCache({
29+
max: document.options.fontLayoutMaxCacheSize || 1000
30+
});
2831
}
2932
}
3033

@@ -49,12 +52,12 @@ class EmbeddedFont extends PDFFont {
4952
return this.layoutRun(text);
5053
}
5154
let cached;
52-
if ((cached = this.layoutCache[text])) {
55+
if ((cached = this.layoutCache.get(text))) {
5356
return cached;
5457
}
5558

5659
const run = this.layoutRun(text);
57-
this.layoutCache[text] = run;
60+
this.layoutCache.set(text, run);
5861
return run;
5962
}
6063

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"fontkit": "^1.8.1",
5353
"jpeg-exif": "^1.1.4",
5454
"linebreak": "^1.0.2",
55+
"lru-cache": "^10.2.2",
5556
"png-js": "^1.0.0"
5657
},
5758
"scripts": {
@@ -89,4 +90,4 @@
8990
"<rootDir>/tests/unit/setupTests.js"
9091
]
9192
}
92-
}
93+
}

yarn.lock

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5016,6 +5016,11 @@ longest@^1.0.1:
50165016
resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097"
50175017
integrity sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc=
50185018

5019+
lru-cache@^10.2.2:
5020+
version "10.2.2"
5021+
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.2.2.tgz#48206bc114c1252940c41b25b41af5b545aca878"
5022+
integrity sha512-9hp3Vp2/hFQUiIwKo8XCeFVnrg8Pk3TYNPIR7tJADKi5YfcF7vEaK7avFHTlSy3kOKYaJQaalfEo6YuXdceBOQ==
5023+
50195024
lru-cache@^5.1.1:
50205025
version "5.1.1"
50215026
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"

0 commit comments

Comments
 (0)