@@ -27,7 +27,11 @@ import {
27
27
Util ,
28
28
warn ,
29
29
} from "../shared/util.js" ;
30
- import { getShadingPattern , TilingPattern } from "./pattern_helper.js" ;
30
+ import {
31
+ getShadingPattern ,
32
+ PathType ,
33
+ TilingPattern ,
34
+ } from "./pattern_helper.js" ;
31
35
import { PixelsPerInch } from "./display_utils.js" ;
32
36
33
37
// <canvas> contexts store most of the state we need natively.
@@ -38,10 +42,6 @@ const MIN_FONT_SIZE = 16;
38
42
const MAX_FONT_SIZE = 100 ;
39
43
const MAX_GROUP_SIZE = 4096 ;
40
44
41
- // This value comes from sampling a few PDFs that re-use patterns, there doesn't
42
- // seem to be any that benefit from caching more than 2 patterns.
43
- const MAX_CACHED_CANVAS_PATTERNS = 2 ;
44
-
45
45
// Defines the time the `executeOperatorList`-method is going to be executing
46
46
// before it stops and shedules a continue of execution.
47
47
const EXECUTION_TIME = 15 ; // ms
@@ -366,46 +366,6 @@ class CachedCanvases {
366
366
}
367
367
}
368
368
369
- /**
370
- * Least recently used cache implemented with a JS Map. JS Map keys are ordered
371
- * by last insertion.
372
- */
373
- class LRUCache {
374
- constructor ( maxSize = 0 ) {
375
- this . _cache = new Map ( ) ;
376
- this . _maxSize = maxSize ;
377
- }
378
-
379
- has ( key ) {
380
- return this . _cache . has ( key ) ;
381
- }
382
-
383
- get ( key ) {
384
- if ( this . _cache . has ( key ) ) {
385
- // Delete and set the value so it's moved to the end of the map iteration.
386
- const value = this . _cache . get ( key ) ;
387
- this . _cache . delete ( key ) ;
388
- this . _cache . set ( key , value ) ;
389
- }
390
- return this . _cache . get ( key ) ;
391
- }
392
-
393
- set ( key , value ) {
394
- if ( this . _maxSize <= 0 ) {
395
- return ;
396
- }
397
- if ( this . _cache . size + 1 > this . _maxSize ) {
398
- // Delete the least recently used.
399
- this . _cache . delete ( this . _cache . keys ( ) . next ( ) . value ) ;
400
- }
401
- this . _cache . set ( key , value ) ;
402
- }
403
-
404
- clear ( ) {
405
- this . _cache . clear ( ) ;
406
- }
407
- }
408
-
409
369
function compileType3Glyph ( imgData ) {
410
370
const POINT_TO_PROCESS_LIMIT = 1000 ;
411
371
const POINT_TYPES = new Uint8Array ( [
@@ -639,8 +599,24 @@ class CanvasExtraState {
639
599
this . updatePathMinMax ( transform , box [ 2 ] , box [ 3 ] ) ;
640
600
}
641
601
642
- getPathBoundingBox ( ) {
643
- return [ this . minX , this . minY , this . maxX , this . maxY ] ;
602
+ getPathBoundingBox ( pathType = PathType . FILL , transform = null ) {
603
+ const box = [ this . minX , this . minY , this . maxX , this . maxY ] ;
604
+ if ( pathType === PathType . STROKE ) {
605
+ if ( ! transform ) {
606
+ throw new Error ( "Stroke bounding box must include transform." ) ;
607
+ }
608
+ // Stroked paths can be outside of the path bounding box by 1/2 the line
609
+ // width.
610
+ const scale = Util . singularValueDecompose2dScale ( transform ) ;
611
+ const xStrokePad = ( scale [ 0 ] * this . lineWidth ) / 2 ;
612
+ const yStrokePad = ( scale [ 1 ] * this . lineWidth ) / 2 ;
613
+ // console.log(`increasing bounding box ${xStrokePad} ${yStrokePad}`);
614
+ box [ 0 ] -= xStrokePad ;
615
+ box [ 1 ] -= yStrokePad ;
616
+ box [ 2 ] += xStrokePad ;
617
+ box [ 3 ] += yStrokePad ;
618
+ }
619
+ return box ;
644
620
}
645
621
646
622
updateClipFromPath ( ) {
@@ -656,8 +632,11 @@ class CanvasExtraState {
656
632
this . maxY = 0 ;
657
633
}
658
634
659
- getClippedPathBoundingBox ( ) {
660
- return Util . intersect ( this . clipBox , this . getPathBoundingBox ( ) ) ;
635
+ getClippedPathBoundingBox ( pathType = PathType . FILL , transform = null ) {
636
+ return Util . intersect (
637
+ this . clipBox ,
638
+ this . getPathBoundingBox ( pathType , transform )
639
+ ) ;
661
640
}
662
641
}
663
642
@@ -1121,7 +1100,6 @@ class CanvasGraphics {
1121
1100
this . markedContentStack = [ ] ;
1122
1101
this . optionalContentConfig = optionalContentConfig ;
1123
1102
this . cachedCanvases = new CachedCanvases ( this . canvasFactory ) ;
1124
- this . cachedCanvasPatterns = new LRUCache ( MAX_CACHED_CANVAS_PATTERNS ) ;
1125
1103
this . cachedPatterns = new Map ( ) ;
1126
1104
if ( canvasCtx ) {
1127
1105
// NOTE: if mozCurrentTransform is polyfilled, then the current state of
@@ -1273,7 +1251,6 @@ class CanvasGraphics {
1273
1251
}
1274
1252
1275
1253
this . cachedCanvases . clear ( ) ;
1276
- this . cachedCanvasPatterns . clear ( ) ;
1277
1254
this . cachedPatterns . clear ( ) ;
1278
1255
1279
1256
if ( this . imageLayer ) {
@@ -1420,7 +1397,7 @@ class CanvasGraphics {
1420
1397
- offsetY ,
1421
1398
] ) ;
1422
1399
fillCtx . fillStyle = isPatternFill
1423
- ? fillColor . getPattern ( ctx , this , inverse , false )
1400
+ ? fillColor . getPattern ( ctx , this , inverse , PathType . FILL )
1424
1401
: fillColor ;
1425
1402
1426
1403
fillCtx . fillRect ( 0 , 0 , width , height ) ;
@@ -1772,7 +1749,8 @@ class CanvasGraphics {
1772
1749
ctx . strokeStyle = strokeColor . getPattern (
1773
1750
ctx ,
1774
1751
this ,
1775
- ctx . mozCurrentTransformInverse
1752
+ ctx . mozCurrentTransformInverse ,
1753
+ PathType . STROKE
1776
1754
) ;
1777
1755
// Prevent drawing too thin lines by enforcing a minimum line width.
1778
1756
ctx . lineWidth = Math . max ( lineWidth , this . current . lineWidth ) ;
@@ -1819,7 +1797,8 @@ class CanvasGraphics {
1819
1797
ctx . fillStyle = fillColor . getPattern (
1820
1798
ctx ,
1821
1799
this ,
1822
- ctx . mozCurrentTransformInverse
1800
+ ctx . mozCurrentTransformInverse ,
1801
+ PathType . FILL
1823
1802
) ;
1824
1803
needRestore = true ;
1825
1804
}
@@ -2154,7 +2133,8 @@ class CanvasGraphics {
2154
2133
const pattern = current . fillColor . getPattern (
2155
2134
ctx ,
2156
2135
this ,
2157
- ctx . mozCurrentTransformInverse
2136
+ ctx . mozCurrentTransformInverse ,
2137
+ PathType . FILL
2158
2138
) ;
2159
2139
patternTransform = ctx . mozCurrentTransform ;
2160
2140
ctx . restore ( ) ;
@@ -2427,10 +2407,7 @@ class CanvasGraphics {
2427
2407
if ( this . cachedPatterns . has ( objId ) ) {
2428
2408
pattern = this . cachedPatterns . get ( objId ) ;
2429
2409
} else {
2430
- pattern = getShadingPattern (
2431
- this . objs . get ( objId ) ,
2432
- this . cachedCanvasPatterns
2433
- ) ;
2410
+ pattern = getShadingPattern ( this . objs . get ( objId ) ) ;
2434
2411
this . cachedPatterns . set ( objId , pattern ) ;
2435
2412
}
2436
2413
if ( matrix ) {
@@ -2451,7 +2428,7 @@ class CanvasGraphics {
2451
2428
ctx ,
2452
2429
this ,
2453
2430
ctx . mozCurrentTransformInverse ,
2454
- true
2431
+ PathType . SHADING
2455
2432
) ;
2456
2433
2457
2434
const inv = ctx . mozCurrentTransformInverse ;
@@ -2839,7 +2816,7 @@ class CanvasGraphics {
2839
2816
maskCtx ,
2840
2817
this ,
2841
2818
ctx . mozCurrentTransformInverse ,
2842
- false
2819
+ PathType . FILL
2843
2820
)
2844
2821
: fillColor ;
2845
2822
maskCtx . fillRect ( 0 , 0 , width , height ) ;
0 commit comments