Skip to content

Commit 5992d0f

Browse files
committed
Fix the group bbox when the numbers are too big
It fixes #20872.
1 parent 918a319 commit 5992d0f

4 files changed

Lines changed: 42 additions & 24 deletions

File tree

src/core/evaluator.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,10 @@ class PartialEvaluator {
483483
const { dict } = xobj;
484484
const matrix = lookupMatrix(dict.getArray("Matrix"), null);
485485
const bbox = lookupNormalRect(dict.getArray("BBox"), null);
486+
let f32bbox = bbox && new Float32Array(bbox);
487+
if (f32bbox?.some(x => !isFinite(x))) {
488+
f32bbox = null;
489+
}
486490

487491
let optionalContent, groupOptions;
488492
if (dict.has("OC")) {
@@ -498,7 +502,7 @@ class PartialEvaluator {
498502
if (group) {
499503
groupOptions = {
500504
matrix,
501-
bbox,
505+
bbox: f32bbox,
502506
smask,
503507
isolated: false,
504508
knockout: false,
@@ -532,8 +536,7 @@ class PartialEvaluator {
532536
// bounding box and translated to the correct position so we don't need to
533537
// apply the bounding box to it.
534538
const f32matrix = matrix && new Float32Array(matrix);
535-
const f32bbox = (!group && bbox && new Float32Array(bbox)) || null;
536-
const args = [f32matrix, f32bbox];
539+
const args = [f32matrix, (!group && f32bbox) || null];
537540
operatorList.addOp(OPS.paintFormXObjectBegin, args);
538541

539542
const localResources = dict.get("Resources");

src/display/canvas.js

Lines changed: 27 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2576,18 +2576,6 @@ class CanvasGraphics {
25762576
if (group.matrix) {
25772577
currentCtx.transform(...group.matrix);
25782578
}
2579-
if (!group.bbox) {
2580-
throw new Error("Bounding box is required.");
2581-
}
2582-
2583-
// Based on the current transform figure out how big the bounding box
2584-
// will actually be.
2585-
let bounds = MIN_MAX_INIT.slice();
2586-
Util.axialAlignedBoundingBox(
2587-
group.bbox,
2588-
getCurrentTransform(currentCtx),
2589-
bounds
2590-
);
25912579

25922580
// Clip the bounding box to the current canvas.
25932581
const canvasBounds = [
@@ -2596,7 +2584,23 @@ class CanvasGraphics {
25962584
currentCtx.canvas.width,
25972585
currentCtx.canvas.height,
25982586
];
2599-
bounds = Util.intersect(bounds, canvasBounds) || [0, 0, 0, 0];
2587+
2588+
let bounds;
2589+
if (group.bbox) {
2590+
bounds = MIN_MAX_INIT.slice();
2591+
Util.axialAlignedBoundingBox(
2592+
group.bbox,
2593+
getCurrentTransform(currentCtx),
2594+
bounds
2595+
);
2596+
2597+
bounds = Util.intersect(bounds, canvasBounds) || [0, 0, 0, 0];
2598+
} else {
2599+
bounds = canvasBounds;
2600+
}
2601+
2602+
// Based on the current transform figure out how big the bounding box
2603+
// will actually be.
26002604
// Use ceil in case we're between sizes so we don't create canvas that is
26012605
// too small and make the canvas at least 1x1 pixels.
26022606
const offsetX = Math.floor(bounds[0]);
@@ -2624,15 +2628,17 @@ class CanvasGraphics {
26242628
groupCtx.transform(...currentTransform);
26252629

26262630
// Apply the bbox to the group context.
2627-
let clip = new Path2D();
2628-
const [x0, y0, x1, y1] = group.bbox;
2629-
clip.rect(x0, y0, x1 - x0, y1 - y0);
2630-
if (group.matrix) {
2631-
const path = new Path2D();
2632-
path.addPath(clip, new DOMMatrix(group.matrix));
2633-
clip = path;
2631+
if (group.bbox) {
2632+
let clip = new Path2D();
2633+
const [x0, y0, x1, y1] = group.bbox;
2634+
clip.rect(x0, y0, x1 - x0, y1 - y0);
2635+
if (group.matrix) {
2636+
const path = new Path2D();
2637+
path.addPath(clip, new DOMMatrix(group.matrix));
2638+
clip = path;
2639+
}
2640+
groupCtx.clip(clip);
26342641
}
2635-
groupCtx.clip(clip);
26362642

26372643
if (group.smask) {
26382644
// Saving state and cached mask to be used in setGState.

test/pdfs/issue20872.pdf.link

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://github.com/user-attachments/files/26001581/issue20872.pdf

test/test_manifest.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14012,5 +14012,13 @@
1401214012
"md5": "321f0901af604a6052baa7b2855a7e3e",
1401314013
"rounds": 1,
1401414014
"type": "text"
14015+
},
14016+
{
14017+
"id": "issue20872",
14018+
"file": "pdfs/issue20872.pdf",
14019+
"md5": "2394eea595990d0148b209fe49f035e5",
14020+
"rounds": 1,
14021+
"link": true,
14022+
"type": "eq"
1401514023
}
1401614024
]

0 commit comments

Comments
 (0)