Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add LRUCache for font layout caching #1528

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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