Skip to content

Commit

Permalink
feat: Add LRUCache for font layout caching
Browse files Browse the repository at this point in the history
  • Loading branch information
abihf committed Jun 22, 2024
1 parent b35c6f9 commit 2a5baef
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 4 deletions.
9 changes: 6 additions & 3 deletions lib/font/embedded.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LRUCache } from 'lru-cache';
import PDFFont from '../font';

const toHex = function(num) {
Expand All @@ -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
});
}
}

Expand All @@ -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;
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"fontkit": "^1.8.1",
"jpeg-exif": "^1.1.4",
"linebreak": "^1.0.2",
"lru-cache": "^10.2.2",
"png-js": "^1.0.0"
},
"scripts": {
Expand Down Expand Up @@ -89,4 +90,4 @@
"<rootDir>/tests/unit/setupTests.js"
]
}
}
}
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5016,6 +5016,11 @@ longest@^1.0.1:
resolved "https://registry.yarnpkg.com/longest/-/longest-1.0.1.tgz#30a0b2da38f73770e8294a0d22e6625ed77d0097"
integrity sha1-MKCy2jj3N3DoKUoNIuZiXtd9AJc=

lru-cache@^10.2.2:
version "10.2.2"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-10.2.2.tgz#48206bc114c1252940c41b25b41af5b545aca878"
integrity sha512-9hp3Vp2/hFQUiIwKo8XCeFVnrg8Pk3TYNPIR7tJADKi5YfcF7vEaK7avFHTlSy3kOKYaJQaalfEo6YuXdceBOQ==

lru-cache@^5.1.1:
version "5.1.1"
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-5.1.1.tgz#1da27e6710271947695daf6848e847f01d84b920"
Expand Down

0 comments on commit 2a5baef

Please sign in to comment.