@@ -604,8 +604,23 @@ class CanvasExtraState {
604
604
this . maxY = Math . max ( this . maxY , y ) ;
605
605
}
606
606
607
- updateCurvePathMinMax ( transform , x0 , y0 , x1 , y1 , x2 , y2 , x3 , y3 ) {
607
+ updateScalingPathMinMax ( transform , minMax ) {
608
+ Util . scaleMinMax ( transform , minMax ) ;
609
+ this . minX = Math . min ( this . minX , minMax [ 0 ] ) ;
610
+ this . maxX = Math . max ( this . maxX , minMax [ 1 ] ) ;
611
+ this . minY = Math . min ( this . minY , minMax [ 2 ] ) ;
612
+ this . maxY = Math . max ( this . maxY , minMax [ 3 ] ) ;
613
+ }
614
+
615
+ updateCurvePathMinMax ( transform , x0 , y0 , x1 , y1 , x2 , y2 , x3 , y3 , minMax ) {
608
616
const box = Util . bezierBoundingBox ( x0 , y0 , x1 , y1 , x2 , y2 , x3 , y3 ) ;
617
+ if ( minMax ) {
618
+ minMax [ 0 ] = Math . min ( minMax [ 0 ] , box [ 0 ] , box [ 2 ] ) ;
619
+ minMax [ 1 ] = Math . max ( minMax [ 1 ] , box [ 0 ] , box [ 2 ] ) ;
620
+ minMax [ 2 ] = Math . min ( minMax [ 2 ] , box [ 1 ] , box [ 3 ] ) ;
621
+ minMax [ 3 ] = Math . max ( minMax [ 3 ] , box [ 1 ] , box [ 3 ] ) ;
622
+ return ;
623
+ }
609
624
this . updatePathMinMax ( transform , box [ 0 ] , box [ 1 ] ) ;
610
625
this . updatePathMinMax ( transform , box [ 2 ] , box [ 3 ] ) ;
611
626
}
@@ -1737,12 +1752,25 @@ class CanvasGraphics {
1737
1752
}
1738
1753
1739
1754
// Path
1740
- constructPath ( ops , args ) {
1755
+ constructPath ( ops , args , minMax ) {
1741
1756
const ctx = this . ctx ;
1742
1757
const current = this . current ;
1743
1758
let x = current . x ,
1744
1759
y = current . y ;
1745
1760
let startX , startY ;
1761
+ const currentTransform = ctx . mozCurrentTransform ;
1762
+
1763
+ // Most of the time the current transform is a scaling matrix
1764
+ // so we don't need to transform points before computing min/max:
1765
+ // we can compute min/max first and then smartly "apply" the
1766
+ // transform (see Util.scaleMinMax).
1767
+ // For rectangle, moveTo and lineTo, min/max are computed in the
1768
+ // worker (see evaluator.js).
1769
+ const isScalingMatrix =
1770
+ ( currentTransform [ 0 ] === 0 && currentTransform [ 3 ] === 0 ) ||
1771
+ ( currentTransform [ 1 ] === 0 && currentTransform [ 2 ] === 0 ) ;
1772
+ const minMaxForBezier = isScalingMatrix ? minMax . slice ( 0 ) : null ;
1773
+
1746
1774
for ( let i = 0 , j = 0 , ii = ops . length ; i < ii ; i ++ ) {
1747
1775
switch ( ops [ i ] | 0 ) {
1748
1776
case OPS . rectangle :
@@ -1761,21 +1789,27 @@ class CanvasGraphics {
1761
1789
ctx . lineTo ( xw , yh ) ;
1762
1790
ctx . lineTo ( x , yh ) ;
1763
1791
}
1764
- current . updatePathMinMax ( ctx . mozCurrentTransform , x , y ) ;
1765
- current . updatePathMinMax ( ctx . mozCurrentTransform , xw , yh ) ;
1792
+ if ( ! isScalingMatrix ) {
1793
+ current . updatePathMinMax ( currentTransform , x , y ) ;
1794
+ current . updatePathMinMax ( currentTransform , xw , yh ) ;
1795
+ }
1766
1796
ctx . closePath ( ) ;
1767
1797
break ;
1768
1798
case OPS . moveTo :
1769
1799
x = args [ j ++ ] ;
1770
1800
y = args [ j ++ ] ;
1771
1801
ctx . moveTo ( x , y ) ;
1772
- current . updatePathMinMax ( ctx . mozCurrentTransform , x , y ) ;
1802
+ if ( ! isScalingMatrix ) {
1803
+ current . updatePathMinMax ( currentTransform , x , y ) ;
1804
+ }
1773
1805
break ;
1774
1806
case OPS . lineTo :
1775
1807
x = args [ j ++ ] ;
1776
1808
y = args [ j ++ ] ;
1777
1809
ctx . lineTo ( x , y ) ;
1778
- current . updatePathMinMax ( ctx . mozCurrentTransform , x , y ) ;
1810
+ if ( ! isScalingMatrix ) {
1811
+ current . updatePathMinMax ( currentTransform , x , y ) ;
1812
+ }
1779
1813
break ;
1780
1814
case OPS . curveTo :
1781
1815
startX = x ;
@@ -1791,15 +1825,16 @@ class CanvasGraphics {
1791
1825
y
1792
1826
) ;
1793
1827
current . updateCurvePathMinMax (
1794
- ctx . mozCurrentTransform ,
1828
+ currentTransform ,
1795
1829
startX ,
1796
1830
startY ,
1797
1831
args [ j ] ,
1798
1832
args [ j + 1 ] ,
1799
1833
args [ j + 2 ] ,
1800
1834
args [ j + 3 ] ,
1801
1835
x ,
1802
- y
1836
+ y ,
1837
+ minMaxForBezier
1803
1838
) ;
1804
1839
j += 6 ;
1805
1840
break ;
@@ -1815,15 +1850,16 @@ class CanvasGraphics {
1815
1850
args [ j + 3 ]
1816
1851
) ;
1817
1852
current . updateCurvePathMinMax (
1818
- ctx . mozCurrentTransform ,
1853
+ currentTransform ,
1819
1854
startX ,
1820
1855
startY ,
1821
1856
x ,
1822
1857
y ,
1823
1858
args [ j ] ,
1824
1859
args [ j + 1 ] ,
1825
1860
args [ j + 2 ] ,
1826
- args [ j + 3 ]
1861
+ args [ j + 3 ] ,
1862
+ minMaxForBezier
1827
1863
) ;
1828
1864
x = args [ j + 2 ] ;
1829
1865
y = args [ j + 3 ] ;
@@ -1836,15 +1872,16 @@ class CanvasGraphics {
1836
1872
y = args [ j + 3 ] ;
1837
1873
ctx . bezierCurveTo ( args [ j ] , args [ j + 1 ] , x , y , x , y ) ;
1838
1874
current . updateCurvePathMinMax (
1839
- ctx . mozCurrentTransform ,
1875
+ currentTransform ,
1840
1876
startX ,
1841
1877
startY ,
1842
1878
args [ j ] ,
1843
1879
args [ j + 1 ] ,
1844
1880
x ,
1845
1881
y ,
1846
1882
x ,
1847
- y
1883
+ y ,
1884
+ minMaxForBezier
1848
1885
) ;
1849
1886
j += 4 ;
1850
1887
break ;
@@ -1853,6 +1890,11 @@ class CanvasGraphics {
1853
1890
break ;
1854
1891
}
1855
1892
}
1893
+
1894
+ if ( isScalingMatrix ) {
1895
+ current . updateScalingPathMinMax ( currentTransform , minMaxForBezier ) ;
1896
+ }
1897
+
1856
1898
current . setCurrentPoint ( x , y ) ;
1857
1899
}
1858
1900
0 commit comments