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