@@ -63,6 +63,14 @@ const SCALE_MATRIX = new DOMMatrix();
63
63
// Used to get some coordinates.
64
64
const XY = new Float32Array ( 2 ) ;
65
65
66
+ // Initial rectangle values for the minMax array.
67
+ const MIN_MAX_INIT = new Float32Array ( [
68
+ Infinity ,
69
+ Infinity ,
70
+ - Infinity ,
71
+ - Infinity ,
72
+ ] ) ;
73
+
66
74
/**
67
75
* Overrides certain methods on a 2d ctx so that when they are called they
68
76
* will also call the same method on the destCtx. The methods that are
@@ -330,40 +338,19 @@ class CanvasExtraState {
330
338
this . activeSMask = null ;
331
339
this . transferMaps = "none" ;
332
340
333
- this . startNewPathAndClipBox ( [ 0 , 0 , width , height ] ) ;
341
+ this . clipBox = new Float32Array ( [ 0 , 0 , width , height ] ) ;
342
+ this . minMax = MIN_MAX_INIT . slice ( ) ;
334
343
}
335
344
336
345
clone ( ) {
337
346
const clone = Object . create ( this ) ;
338
347
clone . clipBox = this . clipBox . slice ( ) ;
348
+ clone . minMax = this . minMax . slice ( ) ;
339
349
return clone ;
340
350
}
341
351
342
- updateRectMinMax ( [ m0 , m1 , m2 , m3 , m4 , m5 ] , [ r0 , r1 , r2 , r3 ] ) {
343
- const m0r0m4 = m0 * r0 + m4 ;
344
- const m0r2m4 = m0 * r2 + m4 ;
345
- const m1r0m5 = m1 * r0 + m5 ;
346
- const m1r2m5 = m1 * r2 + m5 ;
347
- const m2r1 = m2 * r1 ;
348
- const m2r3 = m2 * r3 ;
349
- const m3r1 = m3 * r1 ;
350
- const m3r3 = m3 * r3 ;
351
- const a0 = m0r0m4 + m2r1 ;
352
- const a1 = m0r2m4 + m2r3 ;
353
- const a2 = m0r0m4 + m2r3 ;
354
- const a3 = m0r2m4 + m2r1 ;
355
- const b0 = m1r0m5 + m3r1 ;
356
- const b1 = m1r2m5 + m3r3 ;
357
- const b2 = m1r0m5 + m3r3 ;
358
- const b3 = m1r2m5 + m3r1 ;
359
- this . minX = Math . min ( this . minX , a0 , a1 , a2 , a3 ) ;
360
- this . maxX = Math . max ( this . maxX , a0 , a1 , a2 , a3 ) ;
361
- this . minY = Math . min ( this . minY , b0 , b1 , b2 , b3 ) ;
362
- this . maxY = Math . max ( this . maxY , b0 , b1 , b2 , b3 ) ;
363
- }
364
-
365
352
getPathBoundingBox ( pathType = PathType . FILL , transform = null ) {
366
- const box = [ this . minX , this . minY , this . maxX , this . maxY ] ;
353
+ const box = this . minMax . slice ( ) ;
367
354
if ( pathType === PathType . STROKE ) {
368
355
if ( ! transform ) {
369
356
unreachable ( "Stroke bounding box must include transform." ) ;
@@ -387,15 +374,12 @@ class CanvasExtraState {
387
374
}
388
375
389
376
isEmptyClip ( ) {
390
- return this . minX === Infinity ;
377
+ return this . minMax [ 0 ] === Infinity ;
391
378
}
392
379
393
380
startNewPathAndClipBox ( box ) {
394
- this . clipBox = box ;
395
- this . minX = Infinity ;
396
- this . minY = Infinity ;
397
- this . maxX = 0 ;
398
- this . maxY = 0 ;
381
+ this . clipBox . set ( box , 0 ) ;
382
+ this . minMax . set ( MIN_MAX_INIT , 0 ) ;
399
383
}
400
384
401
385
getClippedPathBoundingBox ( pathType = PathType . FILL , transform = null ) {
@@ -1014,10 +998,9 @@ class CanvasGraphics {
1014
998
0 ,
1015
999
] ) ;
1016
1000
maskToCanvas = Util . transform ( maskToCanvas , [ 1 , 0 , 0 , 1 , 0 , - height ] ) ;
1017
- const [ minX , minY , maxX , maxY ] = Util . getAxialAlignedBoundingBox (
1018
- [ 0 , 0 , width , height ] ,
1019
- maskToCanvas
1020
- ) ;
1001
+ const minMax = MIN_MAX_INIT . slice ( ) ;
1002
+ Util . axialAlignedBoundingBox ( [ 0 , 0 , width , height ] , maskToCanvas , minMax ) ;
1003
+ const [ minX , minY , maxX , maxY ] = minMax ;
1021
1004
const drawnWidth = Math . round ( maxX - minX ) || 1 ;
1022
1005
const drawnHeight = Math . round ( maxY - minY ) || 1 ;
1023
1006
const fillCanvas = this . cachedCanvases . getCanvas (
@@ -1458,7 +1441,11 @@ class CanvasGraphics {
1458
1441
}
1459
1442
path = path2d ;
1460
1443
}
1461
- this . current . updateRectMinMax ( getCurrentTransform ( this . ctx ) , minMax ) ;
1444
+ Util . axialAlignedBoundingBox (
1445
+ minMax ,
1446
+ getCurrentTransform ( this . ctx ) ,
1447
+ this . current . minMax
1448
+ ) ;
1462
1449
this [ op ] ( path ) ;
1463
1450
}
1464
1451
@@ -2240,10 +2227,9 @@ class CanvasGraphics {
2240
2227
const inv = getCurrentTransformInverse ( ctx ) ;
2241
2228
if ( inv ) {
2242
2229
const { width, height } = ctx . canvas ;
2243
- const [ x0 , y0 , x1 , y1 ] = Util . getAxialAlignedBoundingBox (
2244
- [ 0 , 0 , width , height ] ,
2245
- inv
2246
- ) ;
2230
+ const minMax = MIN_MAX_INIT . slice ( ) ;
2231
+ Util . axialAlignedBoundingBox ( [ 0 , 0 , width , height ] , inv , minMax ) ;
2232
+ const [ x0 , y0 , x1 , y1 ] = minMax ;
2247
2233
2248
2234
this . ctx . fillRect ( x0 , y0 , x1 - x0 , y1 - y0 ) ;
2249
2235
} else {
@@ -2282,7 +2268,11 @@ class CanvasGraphics {
2282
2268
this . baseTransform = getCurrentTransform ( this . ctx ) ;
2283
2269
2284
2270
if ( bbox ) {
2285
- this . current . updateRectMinMax ( this . baseTransform , bbox ) ;
2271
+ Util . axialAlignedBoundingBox (
2272
+ bbox ,
2273
+ this . baseTransform ,
2274
+ this . current . minMax
2275
+ ) ;
2286
2276
const [ x0 , y0 , x1 , y1 ] = bbox ;
2287
2277
const clip = new Path2D ( ) ;
2288
2278
clip . rect ( x0 , y0 , x1 - x0 , y1 - y0 ) ;
@@ -2346,10 +2336,13 @@ class CanvasGraphics {
2346
2336
2347
2337
// Based on the current transform figure out how big the bounding box
2348
2338
// will actually be.
2349
- let bounds = Util . getAxialAlignedBoundingBox (
2339
+ let bounds = MIN_MAX_INIT . slice ( ) ;
2340
+ Util . axialAlignedBoundingBox (
2350
2341
group . bbox ,
2351
- getCurrentTransform ( currentCtx )
2342
+ getCurrentTransform ( currentCtx ) ,
2343
+ bounds
2352
2344
) ;
2345
+
2353
2346
// Clip the bounding box to the current canvas.
2354
2347
const canvasBounds = [
2355
2348
0 ,
@@ -2448,9 +2441,11 @@ class CanvasGraphics {
2448
2441
this . restore ( ) ;
2449
2442
this . ctx . save ( ) ;
2450
2443
this . ctx . setTransform ( ...currentMtx ) ;
2451
- const dirtyBox = Util . getAxialAlignedBoundingBox (
2444
+ const dirtyBox = MIN_MAX_INIT . slice ( ) ;
2445
+ Util . axialAlignedBoundingBox (
2452
2446
[ 0 , 0 , groupCtx . canvas . width , groupCtx . canvas . height ] ,
2453
- currentMtx
2447
+ currentMtx ,
2448
+ dirtyBox
2454
2449
) ;
2455
2450
this . ctx . drawImage ( groupCtx . canvas , 0 , 0 ) ;
2456
2451
this . ctx . restore ( ) ;
0 commit comments