Skip to content

Commit 9d093d9

Browse files
authored
Merge pull request #20626 from nicolo-ribaudo/images-right-click
Add support for right-clicking on images (bug 1012805)
2 parents 98f7e85 + 4f7a025 commit 9d093d9

22 files changed

Lines changed: 1166 additions & 226 deletions

src/display/api.js

Lines changed: 45 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ import {
3838
PrintAnnotationStorage,
3939
SerializableEmpty,
4040
} from "./annotation_storage.js";
41+
import {
42+
CanvasBBoxTracker,
43+
CanvasDependencyTracker,
44+
CanvasImagesTracker,
45+
} from "./canvas_dependency_tracker.js";
4146
import {
4247
deprecated,
4348
isDataScheme,
@@ -68,7 +73,6 @@ import {
6873
NodeStandardFontDataFactory,
6974
NodeWasmFactory,
7075
} from "display-node_utils";
71-
import { CanvasDependencyTracker } from "./canvas_dependency_tracker.js";
7276
import { CanvasGraphics } from "./canvas.js";
7377
import { DOMCanvasFactory } from "./canvas_factory.js";
7478
import { DOMCMapReaderFactory } from "display-cmap_reader_factory";
@@ -1273,6 +1277,7 @@ class PDFDocumentProxy {
12731277
* annotation ids with canvases used to render them.
12741278
* @property {PrintAnnotationStorage} [printAnnotationStorage]
12751279
* @property {boolean} [isEditing] - Render the page in editing mode.
1280+
* @property {boolean} [recordImages] - Record the location of images in the PDF
12761281
* @property {boolean} [recordOperations] - Record the dependencies and bounding
12771282
* boxes of all PDF operations that render onto the canvas.
12781283
* @property {OperationsFilter} [operationsFilter] - If provided, only
@@ -1357,6 +1362,7 @@ class PDFPageProxy {
13571362
this.destroyed = false;
13581363
this.recordedBBoxes = null;
13591364
this.#pagesMapper = pagesMapper;
1365+
this.imageCoordinates = null;
13601366
}
13611367

13621368
/**
@@ -1488,6 +1494,7 @@ class PDFPageProxy {
14881494
pageColors = null,
14891495
printAnnotationStorage = null,
14901496
isEditing = false,
1497+
recordImages = false,
14911498
recordOperations = false,
14921499
operationsFilter = null,
14931500
}) {
@@ -1539,6 +1546,7 @@ class PDFPageProxy {
15391546
);
15401547
const shouldRecordOperations =
15411548
!this.recordedBBoxes && (recordOperations || recordForDebugger);
1549+
const shouldRecordImages = !this.imageCoordinates && recordImages;
15421550

15431551
const complete = error => {
15441552
intentState.renderTasks.delete(internalRenderTask);
@@ -1557,6 +1565,10 @@ class PDFPageProxy {
15571565
}
15581566
}
15591567

1568+
if (shouldRecordImages && !error) {
1569+
this.imageCoordinates = internalRenderTask.gfx?.imagesTracker.take();
1570+
}
1571+
15601572
// Attempt to reduce memory usage during *printing*, by always running
15611573
// cleanup immediately once rendering has finished.
15621574
if (intentPrint) {
@@ -1585,18 +1597,30 @@ class PDFPageProxy {
15851597
}
15861598
};
15871599

1600+
let dependencyTracker = null;
1601+
let bboxTracker = null;
1602+
if (shouldRecordOperations || shouldRecordImages) {
1603+
bboxTracker = new CanvasBBoxTracker(
1604+
canvas,
1605+
intentState.operatorList.length
1606+
);
1607+
}
1608+
if (shouldRecordOperations) {
1609+
dependencyTracker = new CanvasDependencyTracker(
1610+
bboxTracker,
1611+
recordForDebugger
1612+
);
1613+
}
1614+
15881615
const internalRenderTask = new InternalRenderTask({
15891616
callback: complete,
15901617
// Only include the required properties, and *not* the entire object.
15911618
params: {
15921619
canvas,
15931620
canvasContext,
1594-
dependencyTracker: shouldRecordOperations
1595-
? new CanvasDependencyTracker(
1596-
canvas,
1597-
intentState.operatorList.length,
1598-
recordForDebugger
1599-
)
1621+
dependencyTracker: dependencyTracker ?? bboxTracker,
1622+
imagesTracker: shouldRecordImages
1623+
? new CanvasImagesTracker(canvas)
16001624
: null,
16011625
viewport,
16021626
transform,
@@ -3288,6 +3312,10 @@ class RenderTask {
32883312
(separateAnnots.canvas && annotationCanvasMap?.size > 0)
32893313
);
32903314
}
3315+
3316+
get imageCoordinates() {
3317+
return this._internalRenderTask.imageCoordinates || null;
3318+
}
32913319
}
32923320

32933321
/**
@@ -3345,6 +3373,7 @@ class InternalRenderTask {
33453373
this._canvasContext = params.canvas ? null : params.canvasContext;
33463374
this._enableHWA = enableHWA;
33473375
this._dependencyTracker = params.dependencyTracker;
3376+
this._imagesTracker = params.imagesTracker;
33483377
this._operationsFilter = operationsFilter;
33493378
}
33503379

@@ -3375,7 +3404,13 @@ class InternalRenderTask {
33753404
this.stepper.init(this.operatorList);
33763405
this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();
33773406
}
3378-
const { viewport, transform, background, dependencyTracker } = this.params;
3407+
const {
3408+
viewport,
3409+
transform,
3410+
background,
3411+
dependencyTracker,
3412+
imagesTracker,
3413+
} = this.params;
33793414

33803415
// When printing in Firefox, we get a specific context in mozPrintCallback
33813416
// which cannot be created from the canvas itself.
@@ -3395,7 +3430,8 @@ class InternalRenderTask {
33953430
{ optionalContentConfig },
33963431
this.annotationCanvasMap,
33973432
this.pageColors,
3398-
dependencyTracker
3433+
dependencyTracker,
3434+
imagesTracker
33993435
);
34003436
this.gfx.beginDrawing({
34013437
transform,

src/display/canvas.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,8 @@ class CanvasGraphics {
658658
{ optionalContentConfig, markedContentStack = null },
659659
annotationCanvasMap,
660660
pageColors,
661-
dependencyTracker
661+
dependencyTracker,
662+
imagesTracker
662663
) {
663664
this.ctx = canvasCtx;
664665
this.current = new CanvasExtraState(
@@ -698,6 +699,7 @@ class CanvasGraphics {
698699
this._cachedBitmapsMap = new Map();
699700

700701
this.dependencyTracker = dependencyTracker ?? null;
702+
this.imagesTracker = imagesTracker ?? null;
701703
}
702704

703705
getObject(opIdx, data, fallback = null) {
@@ -3064,11 +3066,19 @@ class CanvasGraphics {
30643066
imgData.interpolate
30653067
);
30663068

3067-
this.dependencyTracker
3068-
?.resetBBox(opIdx)
3069-
.recordBBox(opIdx, ctx, 0, width, -height, 0)
3070-
.recordDependencies(opIdx, Dependencies.imageXObject)
3071-
.recordOperation(opIdx);
3069+
if (this.dependencyTracker) {
3070+
this.dependencyTracker
3071+
.resetBBox(opIdx)
3072+
.recordBBox(opIdx, ctx, 0, width, -height, 0)
3073+
.recordDependencies(opIdx, Dependencies.imageXObject)
3074+
.recordOperation(opIdx);
3075+
this.imagesTracker?.record(
3076+
ctx,
3077+
width,
3078+
height,
3079+
this.dependencyTracker.clipBox
3080+
);
3081+
}
30723082

30733083
drawImageAtIntegerCoords(
30743084
ctx,

0 commit comments

Comments
 (0)