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

Add language attribute to canvas #17770

Merged
merged 1 commit into from
May 21, 2024
Merged
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
18 changes: 11 additions & 7 deletions src/display/text_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class TextLayer {

static #ascentCache = new Map();

static #canvasCtx = null;
static #canvasContexts = new Map();

static #pendingTextLayers = new Set();

Expand Down Expand Up @@ -423,13 +423,15 @@ class TextLayer {
return;
}
this.#ascentCache.clear();

this.#canvasCtx?.canvas.remove();
this.#canvasCtx = null;
for (const { canvas } of this.#canvasContexts.values()) {
canvas.remove();
}
this.#canvasContexts.clear();
}

static #getCtx(lang = null) {
if (!this.#canvasCtx) {
let canvasContext = this.#canvasContexts.get((lang ||= ""));
if (!canvasContext) {
// We don't use an OffscreenCanvas here because we use serif/sans serif
// fonts with it and they depends on the locale.
// In Firefox, the <html> element get a lang attribute that depends on
Expand All @@ -442,10 +444,12 @@ class TextLayer {
// OffscreenCanvas.
const canvas = document.createElement("canvas");
canvas.className = "hiddenCanvasElement";
canvas.lang = lang;
document.body.append(canvas);
this.#canvasCtx = canvas.getContext("2d", { alpha: false });
canvasContext = canvas.getContext("2d", { alpha: false });
this.#canvasContexts.set(lang, canvasContext);
}
return this.#canvasCtx;
return canvasContext;
}

static #getAscent(fontFamily, lang) {
Expand Down
1 change: 1 addition & 0 deletions test/pdfs/issue16843.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/mozilla/pdf.js/files/12366234/16_2020-044.pdf
9 changes: 9 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -2202,6 +2202,15 @@
"lastPage": 1,
"type": "eq"
},
{
"id": "issue16843-text",
"file": "pdfs/issue16843.pdf",
"md5": "2845b2f7f02690c388c24c488297028a",
"rounds": 1,
"link": true,
"lastPage": 1,
"type": "text"
},
{
"id": "issue5509",
"file": "pdfs/issue5509.pdf",
Expand Down