13
13
* limitations under the License.
14
14
*/
15
15
16
+ import {
17
+ createMatrix ,
18
+ getShadingPattern ,
19
+ TilingPattern ,
20
+ } from "./pattern_helper.js" ;
16
21
import {
17
22
FONT_IDENTITY_MATRIX ,
18
23
IDENTITY_MATRIX ,
@@ -27,7 +32,6 @@ import {
27
32
Util ,
28
33
warn ,
29
34
} from "../shared/util.js" ;
30
- import { getShadingPattern , TilingPattern } from "./pattern_helper.js" ;
31
35
32
36
// <canvas> contexts store most of the state we need natively.
33
37
// However, PDF needs a bit more state, which we store here.
@@ -193,6 +197,17 @@ function addContextCurrentTransform(ctx) {
193
197
} ;
194
198
}
195
199
200
+ function getAdjustmentTransformation ( transform , width , height ) {
201
+ // The pattern will be created at the size of the current page or form object,
202
+ // but the mask is usually scaled differently and offset, so we must account
203
+ // for these to shift and rescale the pattern to the correctly location.
204
+ let patternTransform = createMatrix ( transform ) ;
205
+ patternTransform = patternTransform . scale ( 1 / width , - 1 / height ) ;
206
+ patternTransform = patternTransform . translate ( 0 , - height ) ;
207
+ patternTransform = patternTransform . inverse ( ) ;
208
+ return patternTransform ;
209
+ }
210
+
196
211
class CachedCanvases {
197
212
constructor ( canvasFactory ) {
198
213
this . canvasFactory = canvasFactory ;
@@ -2294,8 +2309,16 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
2294
2309
2295
2310
maskCtx . globalCompositeOperation = "source-in" ;
2296
2311
2312
+ let patternTransform = null ;
2313
+ if ( isPatternFill ) {
2314
+ patternTransform = getAdjustmentTransformation (
2315
+ ctx . mozCurrentTransform ,
2316
+ width ,
2317
+ height
2318
+ ) ;
2319
+ }
2297
2320
maskCtx . fillStyle = isPatternFill
2298
- ? fillColor . getPattern ( maskCtx , this )
2321
+ ? fillColor . getPattern ( maskCtx , this , false , patternTransform )
2299
2322
: fillColor ;
2300
2323
maskCtx . fillRect ( 0 , 0 , width , height ) ;
2301
2324
@@ -2332,14 +2355,23 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
2332
2355
2333
2356
maskCtx . globalCompositeOperation = "source-in" ;
2334
2357
2358
+ const ctx = this . ctx ;
2359
+ let patternTransform = null ;
2360
+ if ( isPatternFill ) {
2361
+ patternTransform = getAdjustmentTransformation (
2362
+ ctx . mozCurrentTransform ,
2363
+ width ,
2364
+ height
2365
+ ) ;
2366
+ }
2367
+
2335
2368
maskCtx . fillStyle = isPatternFill
2336
- ? fillColor . getPattern ( maskCtx , this )
2369
+ ? fillColor . getPattern ( maskCtx , this , false , patternTransform )
2337
2370
: fillColor ;
2338
2371
maskCtx . fillRect ( 0 , 0 , width , height ) ;
2339
2372
2340
2373
maskCtx . restore ( ) ;
2341
2374
2342
- const ctx = this . ctx ;
2343
2375
for ( let i = 0 , ii = positions . length ; i < ii ; i += 2 ) {
2344
2376
ctx . save ( ) ;
2345
2377
ctx . transform (
@@ -2381,8 +2413,17 @@ const CanvasGraphics = (function CanvasGraphicsClosure() {
2381
2413
2382
2414
maskCtx . globalCompositeOperation = "source-in" ;
2383
2415
2416
+ let patternTransform = null ;
2417
+ if ( isPatternFill ) {
2418
+ patternTransform = getAdjustmentTransformation (
2419
+ ctx . mozCurrentTransform ,
2420
+ width ,
2421
+ height
2422
+ ) ;
2423
+ }
2424
+
2384
2425
maskCtx . fillStyle = isPatternFill
2385
- ? fillColor . getPattern ( maskCtx , this )
2426
+ ? fillColor . getPattern ( maskCtx , this , false , patternTransform )
2386
2427
: fillColor ;
2387
2428
maskCtx . fillRect ( 0 , 0 , width , height ) ;
2388
2429
0 commit comments