Skip to content

Commit 803dfa4

Browse files
committed
feat: Add LRUCache for font layout caching
1 parent b35c6f9 commit 803dfa4

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": "^7.0.0",
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
@@ -5030,6 +5030,11 @@ lru-cache@^6.0.0:
50305030
dependencies:
50315031
yallist "^4.0.0"
50325032

5033+
lru-cache@^7.0.0:
5034+
version "7.18.3"
5035+
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.18.3.tgz#f793896e0fd0e954a59dfdd82f0773808df6aa89"
5036+
integrity sha512-jumlc0BIUrS3qJGgIkWZsyfAM7NCWiBcCDhnd+3NNM5KbBmLTgHVfWBcg6W+rLUsIpzpERPsvwUP7CckAQSOoA==
5037+
50335038
50345039
version "0.25.1"
50355040
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.1.tgz#b1c248b399cd7485da0fe7385c2fc7011843266e"

0 commit comments

Comments
 (0)