mirror of
https://github.com/zen-browser/pdf.js.git
synced 2025-07-08 09:20:06 +02:00
Add lang
attribute to canvas element
Fixes issue #16843. In certain cases, the text layer was misaligned due to a difference between the `lang` attribute of the viewer and the canvas. This commit addresses the problem by adding the `lang` attribute to the canvas. The issue was caused because PDF.js uses serif/sans-serif fonts to generate the text layer and relies on system fonts. The difference in the `lang` attribute led to different fonts being picked, causing the misalignment.
This commit is contained in:
parent
9ee7c07b83
commit
9edca0a5ed
3 changed files with 21 additions and 7 deletions
|
@ -81,7 +81,7 @@ class TextLayer {
|
||||||
|
|
||||||
static #ascentCache = new Map();
|
static #ascentCache = new Map();
|
||||||
|
|
||||||
static #canvasCtx = null;
|
static #canvasContexts = new Map();
|
||||||
|
|
||||||
static #pendingTextLayers = new Set();
|
static #pendingTextLayers = new Set();
|
||||||
|
|
||||||
|
@ -423,13 +423,15 @@ class TextLayer {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.#ascentCache.clear();
|
this.#ascentCache.clear();
|
||||||
|
for (const { canvas } of this.#canvasContexts.values()) {
|
||||||
this.#canvasCtx?.canvas.remove();
|
canvas.remove();
|
||||||
this.#canvasCtx = null;
|
}
|
||||||
|
this.#canvasContexts.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
static #getCtx(lang = null) {
|
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
|
// We don't use an OffscreenCanvas here because we use serif/sans serif
|
||||||
// fonts with it and they depends on the locale.
|
// fonts with it and they depends on the locale.
|
||||||
// In Firefox, the <html> element get a lang attribute that depends on
|
// In Firefox, the <html> element get a lang attribute that depends on
|
||||||
|
@ -442,10 +444,12 @@ class TextLayer {
|
||||||
// OffscreenCanvas.
|
// OffscreenCanvas.
|
||||||
const canvas = document.createElement("canvas");
|
const canvas = document.createElement("canvas");
|
||||||
canvas.className = "hiddenCanvasElement";
|
canvas.className = "hiddenCanvasElement";
|
||||||
|
canvas.lang = lang;
|
||||||
document.body.append(canvas);
|
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) {
|
static #getAscent(fontFamily, lang) {
|
||||||
|
|
1
test/pdfs/issue16843.pdf.link
Normal file
1
test/pdfs/issue16843.pdf.link
Normal file
|
@ -0,0 +1 @@
|
||||||
|
https://github.com/mozilla/pdf.js/files/12366234/16_2020-044.pdf
|
|
@ -2202,6 +2202,15 @@
|
||||||
"lastPage": 1,
|
"lastPage": 1,
|
||||||
"type": "eq"
|
"type": "eq"
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"id": "issue16843-text",
|
||||||
|
"file": "pdfs/issue16843.pdf",
|
||||||
|
"md5": "2845b2f7f02690c388c24c488297028a",
|
||||||
|
"rounds": 1,
|
||||||
|
"link": true,
|
||||||
|
"lastPage": 1,
|
||||||
|
"type": "text"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"id": "issue5509",
|
"id": "issue5509",
|
||||||
"file": "pdfs/issue5509.pdf",
|
"file": "pdfs/issue5509.pdf",
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue