Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.

Commit a1ca766

Browse files
committed
Bug 1917430 - Update PDF.js to new version 5369a24c9c41a10565e8003f01960f74b5cd8a21 r=pdfjs-reviewers,Snuffleupagus
Differential Revision: https://phabricator.services.mozilla.com/D221407
1 parent e0b6a07 commit a1ca766

File tree

9 files changed

+231
-49
lines changed

9 files changed

+231
-49
lines changed

browser/base/content/test/static/browser_parsable_css.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ let ignoreList = [
4545
errorMessage: /Property contained reference to invalid variable.*color/i,
4646
isFromDevTools: true,
4747
},
48-
// PDF.js uses a property that is currently only supported in chrome.
4948
{
5049
sourceName: /web\/viewer\.css$/i,
5150
errorMessage:
@@ -145,6 +144,15 @@ let propNameAllowlist = [
145144
propName: "--timing-marker-load-color",
146145
isFromDevTools: true,
147146
},
147+
// These variables are used in JS in viewer.mjs (PDF.js).
148+
{
149+
propName: "--scale-round-x",
150+
isFromDevTools: false,
151+
},
152+
{
153+
propName: "--scale-round-y",
154+
isFromDevTools: false,
155+
},
148156
];
149157

150158
// Add suffix to stylesheets' URI so that we always load them here and

toolkit/components/pdfjs/content/build/pdf.mjs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1635,8 +1635,8 @@ function setLayerDimensions(div, viewport, mustFlip = false, mustRotate = true)
16351635
const useRound = util_FeatureTest.isCSSRoundSupported;
16361636
const w = `var(--scale-factor) * ${pageWidth}px`,
16371637
h = `var(--scale-factor) * ${pageHeight}px`;
1638-
const widthStr = useRound ? `round(${w}, 1px)` : `calc(${w})`,
1639-
heightStr = useRound ? `round(${h}, 1px)` : `calc(${h})`;
1638+
const widthStr = useRound ? `round(down, ${w}, var(--scale-round-x, 1px))` : `calc(${w})`,
1639+
heightStr = useRound ? `round(down, ${h}, var(--scale-round-y, 1px))` : `calc(${h})`;
16401640
if (!mustFlip || viewport.rotation % 180 === 0) {
16411641
style.width = widthStr;
16421642
style.height = heightStr;
@@ -4962,7 +4962,7 @@ class AnnotationEditor {
49624962
const {
49634963
firstChild
49644964
} = annotation.container;
4965-
if (firstChild.nodeName === "DIV" && firstChild.classList.contains("annotationContent")) {
4965+
if (firstChild?.nodeName === "DIV" && firstChild.classList.contains("annotationContent")) {
49664966
firstChild.remove();
49674967
}
49684968
}
@@ -9728,7 +9728,7 @@ function getDocument(src = {}) {
97289728
}
97299729
const docParams = {
97309730
docId,
9731-
apiVersion: "4.6.99",
9731+
apiVersion: "4.6.107",
97329732
data,
97339733
password,
97349734
disableAutoFetch,
@@ -11397,8 +11397,8 @@ class InternalRenderTask {
1139711397
}
1139811398
}
1139911399
}
11400-
const version = "4.6.99";
11401-
const build = "4fb045b9e";
11400+
const version = "4.6.107";
11401+
const build = "5369a24c9";
1140211402

1140311403
;// CONCATENATED MODULE: ./src/shared/scripting_utils.js
1140411404
function makeColorComp(n) {
@@ -18073,7 +18073,7 @@ class AnnotationEditorLayer {
1807318073
const {
1807418074
target
1807518075
} = event;
18076-
if (target === this.#textLayer.div || target.classList.contains("endOfContent") && this.#textLayer.div.contains(target)) {
18076+
if (target === this.#textLayer.div || (target.getAttribute("role") === "img" || target.classList.contains("endOfContent")) && this.#textLayer.div.contains(target)) {
1807718077
const {
1807818078
isMac
1807918079
} = util_FeatureTest.platform;
@@ -18083,7 +18083,11 @@ class AnnotationEditorLayer {
1808318083
this.#uiManager.showAllEditors("highlight", true, true);
1808418084
this.#textLayer.div.classList.add("free");
1808518085
this.toggleDrawing();
18086-
HighlightEditor.startHighlighting(this, this.#uiManager.direction === "ltr", event);
18086+
HighlightEditor.startHighlighting(this, this.#uiManager.direction === "ltr", {
18087+
target: this.#textLayer.div,
18088+
x: event.x,
18089+
y: event.y
18090+
});
1808718091
this.#textLayer.div.addEventListener("pointerup", () => {
1808818092
this.#textLayer.div.classList.remove("free");
1808918093
this.toggleDrawing(true);
@@ -18639,8 +18643,8 @@ class DrawLayer {
1863918643

1864018644

1864118645

18642-
const pdfjsVersion = "4.6.99";
18643-
const pdfjsBuild = "4fb045b9e";
18646+
const pdfjsVersion = "4.6.107";
18647+
const pdfjsBuild = "5369a24c9";
1864418648

1864518649
var __webpack_exports__AbortException = __webpack_exports__.AbortException;
1864618650
var __webpack_exports__AnnotationEditorLayer = __webpack_exports__.AnnotationEditorLayer;

toolkit/components/pdfjs/content/build/pdf.scripting.mjs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4007,8 +4007,8 @@ function initSandbox(params) {
40074007

40084008
;// CONCATENATED MODULE: ./src/pdf.scripting.js
40094009

4010-
const pdfjsVersion = "4.6.99";
4011-
const pdfjsBuild = "4fb045b9e";
4010+
const pdfjsVersion = "4.6.107";
4011+
const pdfjsBuild = "5369a24c9";
40124012
globalThis.pdfjsScripting = {
40134013
initSandbox: initSandbox
40144014
};

toolkit/components/pdfjs/content/build/pdf.worker.mjs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37276,10 +37276,26 @@ class StructTreePage {
3727637276
obj.role = node.role;
3727737277
obj.children = [];
3727837278
parent.children.push(obj);
37279-
const alt = node.dict.get("Alt");
37279+
let alt = node.dict.get("Alt");
37280+
if (typeof alt !== "string") {
37281+
alt = node.dict.get("ActualText");
37282+
}
3728037283
if (typeof alt === "string") {
3728137284
obj.alt = stringToPDFString(alt);
3728237285
}
37286+
const a = node.dict.get("A");
37287+
if (a instanceof Dict) {
37288+
const bbox = lookupNormalRect(a.getArray("BBox"), null);
37289+
if (bbox) {
37290+
obj.bbox = bbox;
37291+
} else {
37292+
const width = a.get("Width");
37293+
const height = a.get("Height");
37294+
if (typeof width === "number" && width > 0 && typeof height === "number" && height > 0) {
37295+
obj.bbox = [0, 0, width, height];
37296+
}
37297+
}
37298+
}
3728337299
const lang = node.dict.get("Lang");
3728437300
if (typeof lang === "string") {
3728537301
obj.lang = stringToPDFString(lang);
@@ -55790,7 +55806,7 @@ class WorkerMessageHandler {
5579055806
docId,
5579155807
apiVersion
5579255808
} = docParams;
55793-
const workerVersion = "4.6.99";
55809+
const workerVersion = "4.6.107";
5579455810
if (apiVersion !== workerVersion) {
5579555811
throw new Error(`The API version "${apiVersion}" does not match ` + `the Worker version "${workerVersion}".`);
5579655812
}
@@ -56354,8 +56370,8 @@ if (typeof window === "undefined" && !isNodeJS && typeof self !== "undefined" &&
5635456370

5635556371
;// CONCATENATED MODULE: ./src/pdf.worker.js
5635656372

56357-
const pdfjsVersion = "4.6.99";
56358-
const pdfjsBuild = "4fb045b9e";
56373+
const pdfjsVersion = "4.6.107";
56374+
const pdfjsBuild = "5369a24c9";
5635956375

5636056376
var __webpack_exports__WorkerMessageHandler = __webpack_exports__.WorkerMessageHandler;
5636156377
export { __webpack_exports__WorkerMessageHandler as WorkerMessageHandler };

toolkit/components/pdfjs/content/web/viewer-geckoview.css

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,11 @@
444444
z-index:1;
445445
}
446446

447+
span[role="img"]{
448+
user-select:none;
449+
cursor:default;
450+
}
451+
447452
.highlight{
448453
--highlight-bg-color:rgb(180 0 170 / 0.25);
449454
--highlight-selected-bg-color:rgb(0 100 0 / 0.25);
@@ -1249,6 +1254,8 @@
12491254
canvas{
12501255
margin:0;
12511256
display:block;
1257+
width:100%;
1258+
height:100%;
12521259

12531260
&[hidden]{
12541261
display:none;
@@ -1267,6 +1274,9 @@
12671274
}
12681275

12691276
.pdfViewer .page{
1277+
--scale-round-x:1px;
1278+
--scale-round-y:1px;
1279+
12701280
direction:ltr;
12711281
width:816px;
12721282
height:1056px;

toolkit/components/pdfjs/content/web/viewer-geckoview.mjs

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@ function toggleExpandedBtn(button, toggle, view = null) {
532532
button.setAttribute("aria-expanded", toggle);
533533
view?.classList.toggle("hidden", !toggle);
534534
}
535+
const calcRound = Math.fround;
535536

536537
;// CONCATENATED MODULE: ./web/app_options.js
537538
const OptionKind = {
@@ -4692,8 +4693,11 @@ class StructTreeLayerBuilder {
46924693
#treeDom = null;
46934694
#treePromise;
46944695
#elementAttributes = new Map();
4695-
constructor(pdfPage) {
4696+
#rawDims;
4697+
#elementsToAddToTextLayer = null;
4698+
constructor(pdfPage, rawDims) {
46964699
this.#promise = pdfPage.getStructTree();
4700+
this.#rawDims = rawDims;
46974701
}
46984702
async render() {
46994703
if (this.#treePromise) {
@@ -4760,6 +4764,52 @@ class StructTreeLayerBuilder {
47604764
htmlElement.setAttribute("lang", removeNullCharacters(lang, true));
47614765
}
47624766
}
4767+
#addImageInTextLayer(node, element) {
4768+
const {
4769+
alt,
4770+
bbox,
4771+
children
4772+
} = node;
4773+
const child = children?.[0];
4774+
if (!this.#rawDims || !alt || !bbox || child?.type !== "content") {
4775+
return false;
4776+
}
4777+
const {
4778+
id
4779+
} = child;
4780+
if (!id) {
4781+
return false;
4782+
}
4783+
element.setAttribute("aria-owns", id);
4784+
const img = document.createElement("span");
4785+
(this.#elementsToAddToTextLayer ||= new Map()).set(id, img);
4786+
img.setAttribute("role", "img");
4787+
img.setAttribute("aria-label", removeNullCharacters(alt));
4788+
const {
4789+
pageHeight,
4790+
pageX,
4791+
pageY
4792+
} = this.#rawDims;
4793+
const calc = "calc(var(--scale-factor)*";
4794+
const {
4795+
style
4796+
} = img;
4797+
style.width = `${calc}${bbox[2] - bbox[0]}px)`;
4798+
style.height = `${calc}${bbox[3] - bbox[1]}px)`;
4799+
style.left = `${calc}${bbox[0] - pageX}px)`;
4800+
style.top = `${calc}${pageHeight - bbox[3] + pageY}px)`;
4801+
return true;
4802+
}
4803+
addElementsToTextLayer() {
4804+
if (!this.#elementsToAddToTextLayer) {
4805+
return;
4806+
}
4807+
for (const [id, img] of this.#elementsToAddToTextLayer) {
4808+
document.getElementById(id)?.append(img);
4809+
}
4810+
this.#elementsToAddToTextLayer.clear();
4811+
this.#elementsToAddToTextLayer = null;
4812+
}
47634813
#walk(node) {
47644814
if (!node) {
47654815
return null;
@@ -4776,6 +4826,9 @@ class StructTreeLayerBuilder {
47764826
} else if (PDF_ROLE_TO_HTML_ROLE[role]) {
47774827
element.setAttribute("role", PDF_ROLE_TO_HTML_ROLE[role]);
47784828
}
4829+
if (role === "Figure" && this.#addImageInTextLayer(node, element)) {
4830+
return element;
4831+
}
47794832
}
47804833
this.#setAttributes(node, element);
47814834
if (node.children) {
@@ -5355,6 +5408,8 @@ class PDFPageView {
53555408
#layerProperties = null;
53565409
#loadingId = null;
53575410
#previousRotation = null;
5411+
#scaleRoundX = 1;
5412+
#scaleRoundY = 1;
53585413
#renderError = null;
53595414
#renderingState = RenderingStates.INITIAL;
53605415
#textLayerMode = TextLayerMode.ENABLE;
@@ -5571,9 +5626,12 @@ class PDFPageView {
55715626
return;
55725627
}
55735628
const treeDom = await this.structTreeLayer?.render();
5574-
if (treeDom && this.canvas && treeDom.parentNode !== this.canvas) {
5629+
if (treeDom) {
55755630
this.l10n.pause();
5576-
this.canvas.append(treeDom);
5631+
this.structTreeLayer?.addElementsToTextLayer();
5632+
if (this.canvas && treeDom.parentNode !== this.canvas) {
5633+
this.canvas.append(treeDom);
5634+
}
55775635
this.l10n.resume();
55785636
}
55795637
this.structTreeLayer?.show();
@@ -5788,7 +5846,7 @@ class PDFPageView {
57885846
this.annotationLayer = null;
57895847
this._annotationCanvasMap = null;
57905848
}
5791-
if (this.structTreeLayer && !(this.textLayer || this.annotationLayer)) {
5849+
if (this.structTreeLayer && !this.textLayer) {
57925850
this.structTreeLayer = null;
57935851
}
57945852
if (this.annotationEditorLayer && (!keepAnnotationEditorLayer || !this.annotationEditorLayer.div)) {
@@ -5998,13 +6056,20 @@ class PDFPageView {
59986056
}
59996057
const sfx = approximateFraction(outputScale.sx);
60006058
const sfy = approximateFraction(outputScale.sy);
6001-
canvas.width = floorToDivide(width * outputScale.sx, sfx[0]);
6002-
canvas.height = floorToDivide(height * outputScale.sy, sfy[0]);
6003-
const {
6004-
style
6005-
} = canvas;
6006-
style.width = floorToDivide(width, sfx[1]) + "px";
6007-
style.height = floorToDivide(height, sfy[1]) + "px";
6059+
const canvasWidth = canvas.width = floorToDivide(calcRound(width * outputScale.sx), sfx[0]);
6060+
const canvasHeight = canvas.height = floorToDivide(calcRound(height * outputScale.sy), sfy[0]);
6061+
const pageWidth = floorToDivide(calcRound(width), sfx[1]);
6062+
const pageHeight = floorToDivide(calcRound(height), sfy[1]);
6063+
outputScale.sx = canvasWidth / pageWidth;
6064+
outputScale.sy = canvasHeight / pageHeight;
6065+
if (this.#scaleRoundX !== sfx[1]) {
6066+
div.style.setProperty("--scale-round-x", `${sfx[1]}px`);
6067+
this.#scaleRoundX = sfx[1];
6068+
}
6069+
if (this.#scaleRoundY !== sfy[1]) {
6070+
div.style.setProperty("--scale-round-y", `${sfy[1]}px`);
6071+
this.#scaleRoundY = sfy[1];
6072+
}
60086073
this.#viewportMap.set(canvas, viewport);
60096074
const transform = outputScale.scaled ? [outputScale.sx, 0, 0, outputScale.sy, 0, 0] : null;
60106075
const renderContext = {
@@ -6023,7 +6088,7 @@ class PDFPageView {
60236088
showCanvas?.(true);
60246089
await this.#finishRenderTask(renderTask);
60256090
if (this.textLayer || this.annotationLayer) {
6026-
this.structTreeLayer ||= new StructTreeLayerBuilder(pdfPage);
6091+
this.structTreeLayer ||= new StructTreeLayerBuilder(pdfPage, viewport.rawDims);
60276092
}
60286093
this.#renderTextLayer();
60296094
if (this.annotationLayer) {
@@ -6191,7 +6256,7 @@ class PDFViewer {
61916256
#scaleTimeoutId = null;
61926257
#textLayerMode = TextLayerMode.ENABLE;
61936258
constructor(options) {
6194-
const viewerVersion = "4.6.99";
6259+
const viewerVersion = "4.6.107";
61956260
if (version !== viewerVersion) {
61966261
throw new Error(`The API version "${version}" does not match the Viewer version "${viewerVersion}".`);
61976262
}
@@ -9817,8 +9882,8 @@ function beforeUnload(evt) {
98179882

98189883

98199884

9820-
const pdfjsVersion = "4.6.99";
9821-
const pdfjsBuild = "4fb045b9e";
9885+
const pdfjsVersion = "4.6.107";
9886+
const pdfjsBuild = "5369a24c9";
98229887
const AppConstants = null;
98239888
window.PDFViewerApplication = PDFViewerApplication;
98249889
window.PDFViewerApplicationConstants = AppConstants;

0 commit comments

Comments
 (0)