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 803dfa4
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": "^7.0.0",
"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 @@ -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==

[email protected]:
version "0.25.1"
resolved "https://registry.yarnpkg.com/magic-string/-/magic-string-0.25.1.tgz#b1c248b399cd7485da0fe7385c2fc7011843266e"
Expand Down

0 comments on commit 803dfa4

Please sign in to comment.