summaryrefslogtreecommitdiff
path: root/graphics/pgf/base/tex/pgfsys-dvips.def
blob: 8a791ff5ad1d55f0b149ca6d8e37a1ebc56842cb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
% Copyright 2019 by Till Tantau
%
% PS-3 shadings, image and opacity masks (fadings), transparency groups,
% blend modes copyright 2020 by Alexander Grahn
%
% This file may be distributed and/or modified
%
% 1. under the LaTeX Project Public License and/or
% 2. under the GNU Public License.
%
% See the file doc/generic/pgf/licenses/LICENSE for more details.

\ProvidesFileRCS{pgfsys-dvips.def}

% Driver commands for dvips

\def\pgfsys@invoke#1{\special{ps\ifpgfsys@ps@boxmode\else:\fi: #1}}
\def\pgfsys@outerinvoke#1{\special{ps: #1}}
\def\pgf@sys@postscript@header#1{\pgfutil@insertatbegincurrentpage{\special{! #1}}}
\def\pgf@sys@postscript@object#1{\special{! #1}}


%
% Load common postscript commands:
%
\input pgfsys-common-postscript.def


%
% dvips-specific stuff:
%

\def\pgfsys@beginpicture{\pgfsys@ps@boxmodefalse\special{ps: }\special{ps::[begin]}\pgfsys@invoke{pgfo}}
\def\pgfsys@endpicture{\pgfsys@invoke{pgfc}\special{ps::[end]}}
\def\pgfsys@hbox#1{%
  \pgfsys@begin@idscope%
  \pgfsys@invoke{pgfs}%
    \special{ps::[end]}%
      \wd#1=0pt
      \ht#1=0pt
      \dp#1=0pt
      \box#1
    \special{ps::[begin]}%
  \pgfsys@invoke{pgfr}%
  \pgfsys@end@idscope%
}
\def\pgfsys@begininvisible{%
  \special{ps::[begin]}%
  \pgfsys@invoke{gsave nulldevice}%
  \special{ps::[end]}}
\def\pgfsys@endinvisible{%
  \special{ps::[begin]}%
  \pgfsys@invoke{grestore}%
  \special{ps::[end]}%
}
\def\pgfsys@color@unstacked#1{\special{ps: \XC@usecolor{#1}}}
\def\pgfsys@imagesuffixlist{.epsi:.eps:.ps:}

\def\pgfsys@papersize#1#2{%
 \special{papersize=#1,#2}%
 % \expandafter\gdef\expandafter\pgfsys@atbegindocument\expandafter{\pgfsys@atbegindocument\special{papersize=#1,#2}}%
}
\def\pgfsys@prepare@papersize#1#2{%
 \expandafter\gdef\expandafter\pgfsys@atbegindocument\expandafter{\pgfsys@atbegindocument\special{papersize=#1,#2}}%
}

\expandafter\gdef\expandafter\pgfsys@atbegindocument\expandafter{\pgfsys@atbegindocument%
  \pgf@sys@postscript@header{
    /pgfsc{}bind def% stroke color is empty by default
    /pgffc{}bind def% fill color is empty by default
    /pgfstr{stroke}bind def%
    /pgffill{fill}bind def%
    /pgfeofill{eofill}bind def%
    /pgfe{a dup 0 rlineto exch 0 exch rlineto neg 0 rlineto closepath}bind def% rectangle
    /pgfw{setlinewidth}bind def% setlinewidth
    /pgfs{save pgfpd 72 Resolution div 72 VResolution div neg scale
      magscale{1 DVImag div dup scale}if
      pgfx neg pgfy neg translate pgffoa .setopacityalpha}bind def% save
    /pgfr{pgfsd restore}bind def %restore
    userdict begin%
    /pgfo{pgfsd /pgfx currentpoint /pgfy exch def def @beginspecial}bind def %open
    /pgfc{newpath @endspecial pgfpd}bind def %close
    /pgfsd{globaldict /pgfdelta /delta where {pop delta} {0} ifelse put}bind def% save delta
    /pgfpd{/delta globaldict /pgfdelta get def}bind def % put delta
    /.setblendmode where {pop} {/.setblendmode{pop}def} ifelse % install .setblendmode
    /.setfillconstantalpha where {%
      pop /.setopacityalpha {.setfillconstantalpha} def%
    } {%
      /.setopacityalpha where {pop} {/.setopacityalpha {pop} def} ifelse% install .setopacityalpha (fallback)
    } ifelse
    /.pgfsetfillopacityalpha{/pgffoa exch def
      /.setfillconstantalpha where {pop pgffoa .setfillconstantalpha} {%
        /pgffill{gsave pgffoa .setopacityalpha fill 1 .setopacityalpha newpath fill grestore newpath}bind def
        /pgfeofill{gsave pgffoa .setopacityalpha eofill 1 .setopacityalpha newpath eofill grestore newpath}bind def%
      } ifelse%
    } bind def
    /.pgfsetstrokeopacityalpha{/pgfsoa exch def
      /.setstrokeconstantalpha where {pop pgfsoa .setstrokeconstantalpha} {%
        /pgfstr{gsave pgfsoa .setopacityalpha stroke grestore newpath}bind def%
      } ifelse%
    }bind def
    /pgffoa 1 def
    /pgfsoa 1 def
    % mandatory, transparency-related per-page operations, see
    % https://www.ghostscript.com/doc/current/Language.htm#Transparency
    % (bop-hook & eop-hook automatically executed if defined in userdict, see texdoc dvips)
    /.pushpdf14devicefilter where {pop
      [userdict /bop-hook known {userdict /bop-hook get aload pop} if
        {0 .pushpdf14devicefilter} aload pop] cvx userdict exch /bop-hook exch put
      [userdict /eop-hook known {userdict /eop-hook get aload pop} if
        {.poppdf14devicefilter} aload pop] cvx userdict exch /eop-hook exch put%
    } if
    % keeps printers from choking on pdfmark operator
    systemdict /pdfmark known not {userdict /pdfmark systemdict /cleartomark get put} if
    end
  }%
}

\AtBeginDocument{ % needed for plain TeX
  \pgfsys@atbegindocument
  \let\pgfsys@atbegindocument=\pgfutil@empty
}



%
% Position tracking (needs pdftex or luatex in dvi mode)
%
\ifx\savepos\@undefined
  \ifx\pdfsavepos\@undefined
  \else
    \let\pgf@savepos\pdfsavepos
    \let\pgf@lastxpos\pdflastxpos
    \let\pgf@lastypos\pdflastypos
  \fi
\else
  \let\pgf@savepos\savepos
  \let\pgf@lastxpos\lastxpos
  \let\pgf@lastypos\lastypos
\fi


\ifx\pgf@savepos\@undefined
\else

\def\pgfsys@markposition#1{%
  \pgf@savepos%
  \edef\pgf@temp{#1}%
  \expandafter\pgfutil@writetoaux\expandafter{%
    \expandafter\noexpand\expandafter\pgfsyspdfmark\expandafter{\pgf@temp}{\the\pgf@lastxpos}{\the\pgf@lastypos}}%
}

\def\pgfsyspdfmark#1#2#3{%
  \expandafter\gdef\csname pgf@sys@pdf@mark@pos@#1\endcsname{\pgfqpoint{#2sp}{#3sp}}%
  \pgfutil@check@rerun{#1}{{#2}{#3}}%
}

\def\pgfsys@getposition#1#2{%
  \edef\pgf@marshal{\let\noexpand#2=\expandafter\noexpand\csname pgf@sys@pdf@mark@pos@#1\endcsname}%
  \pgf@marshal%
}

\def\pgf@sys@pdf@mark@pos@pgfpageorigin{\pgfpointorigin}

\fi


% Path usage:
\def\pgfsys@discardpath{%
  \ifpgfsys@@clipnext%
    \ifpgfsys@eorule%
      \pgfsysprotocol@literal{%
        /pgfsmaskinplace dup where {exch get}{pop false} ifelse not {eoclip} if}%
    \else%
      \pgfsysprotocol@literal{%
        /pgfsmaskinplace dup where {exch get}{pop false} ifelse not {clip} if}%
    \fi%
    \pgfsys@@clipnextfalse%
  \fi%
  \pgfsysprotocol@literal{newpath}}


%
% Blending
%

\def\pgfsys@blend@mode#1{%
  \expandafter\ifx\csname pgf@sys@pdf@bm@#1\endcsname\relax%
    \expandafter\let\expandafter\pgf@temp\csname pgf@sys@pdf@blend@mode@map@#1\endcsname%
    \expandafter\xdef\csname pgf@sys@pdf@bm@#1\endcsname{/\pgf@temp}%
    \ifx\pgf@temp\relax%
      \pgferror{Unknown blend mode '#1'}%
      \def\pgf@temp{Normal}%
    \fi%
  \fi%
  \pgfsysprotocol@literal{\csname pgf@sys@pdf@bm@#1\endcsname\space .setblendmode}%
}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@normal\endcsname{Normal}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@multiply\endcsname{Multiply}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@screen\endcsname{Screen}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@overlay\endcsname{Overlay}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@darken\endcsname{Darken}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@lighten\endcsname{Lighten}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@color dodge\endcsname{ColorDodge}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@color burn\endcsname{ColorBurn}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@hard light\endcsname{HardLight}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@soft light\endcsname{SoftLight}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@difference\endcsname{Difference}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@exclusion\endcsname{Exclusion}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@saturation\endcsname{Saturation}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@color\endcsname{Color}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@hue\endcsname{Hue}
\expandafter\def\csname pgf@sys@pdf@blend@mode@map@luminosity\endcsname{Luminosity}


%
% Shadings
%

\def\pgfsys@horishading#1#2#3{%
  {%
    \pgf@parsefunc{#3}%
    \pgf@xb=\pgf@max
    \pgfmathsetlength\pgf@y{#2}%
    \pgf@yb=\pgf@y
    \pgf@sys@bp@correct{\pgf@xb}%
    \pgf@sys@bp@correct{\pgf@yb}%
    \xdef\pgfutil@tempa{\hbox to \the\pgf@max{%
      \noexpand\vrule width0pt height\the\pgf@y%
      \noexpand\pgfsys@beginpurepicture%
        \noexpand\pgfsys@outerinvoke{
          \pgf@sys@tonumber{\pgf@yb} \pgf@sys@tonumber{\pgf@xb} 0 0 pgfe <<
            /PatternType 2
            /Shading <<
              /ShadingType 2
              /ColorSpace \pgf@shading@device\space
              /Domain [\pgf@pdfparseddomain]
              /Coords [\pgf@doma\space0 \pgf@domb\space0]
              /Function \pgf@pdfparsedfunction
            >>
          >> matrix makepattern setpattern fill
        }%
        \hss%
      \noexpand\pgfsys@endpurepicture%
    }}%
  }%
  \global\expandafter\let\csname @pgfshading#1!\endcsname\pgfutil@tempa%
}
\def\pgfsys@vertshading#1#2#3{%
  {%
    \pgf@parsefunc{#3}%
    \pgfmathsetlength\pgf@x{#2}%
    \pgf@xb=\pgf@x
    \pgf@yb=\pgf@max
    \pgf@sys@bp@correct{\pgf@xb}%
    \pgf@sys@bp@correct{\pgf@yb}%
    \xdef\pgfutil@tempa{\hbox to \the\pgf@x{%
      \noexpand\vrule width0pt height\the\pgf@max%
      \noexpand\pgfsys@beginpurepicture%
        \noexpand\pgfsys@outerinvoke{
          \pgf@sys@tonumber{\pgf@yb} \pgf@sys@tonumber{\pgf@xb} 0 0 pgfe <<
            /PatternType 2
            /Shading <<
              /ShadingType 2
              /ColorSpace \pgf@shading@device\space
              /Domain [\pgf@pdfparseddomain]
              /Coords [0 \pgf@doma\space0 \pgf@domb]
              /Function \pgf@pdfparsedfunction
            >>
          >> matrix makepattern setpattern fill
        }%
        \hss%
      \noexpand\pgfsys@endpurepicture%
    }}%
  }%
  \global\expandafter\let\csname @pgfshading#1!\endcsname\pgfutil@tempa%
}
\def\pgfsys@radialshading#1#2#3{%
  {%
    \pgf@parsefunc{#3}%
    \pgf@process{#2}%
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \pgf@process{\pgfpoint{\pgf@max}{\pgf@max}}%
    \advance\pgf@xa by \pgf@x
    \advance\pgf@ya by \pgf@y
    \pgf@sys@bp@correct{\pgf@x}%
    \pgf@sys@bp@correct{\pgf@y}%
    \pgf@sys@bp@correct{\pgf@xa}%
    \pgf@sys@bp@correct{\pgf@ya}%
    \pgf@max=2\pgf@max
    \xdef\pgfutil@tempa{\hbox to \the\pgf@max{%
      \noexpand\vrule width0pt height\the\pgf@max%
      \noexpand\pgfsys@beginpurepicture%
        \noexpand\pgfsys@outerinvoke{
          <<
            /ShadingType 3
            /ColorSpace \pgf@shading@device\space
            /Domain [\pgf@pdfparseddomain]
            /Coords [\pgf@sys@tonumber{\pgf@xa} \pgf@sys@tonumber{\pgf@ya} \pgf@doma\space
              \pgf@sys@tonumber{\pgf@x} \pgf@sys@tonumber{\pgf@y} \pgf@domb]
            /Function \pgf@pdfparsedfunction
            /Extend [true false]
          >> shfill
        }%
        \hss%
      \noexpand\pgfsys@endpurepicture%
    }}%
  }%
  \global\expandafter\let\csname @pgfshading#1!\endcsname\pgfutil@tempa%
}
\def\pgfsys@functionalshading#1#2#3#4{%
  {%
    \pgf@process{#2}%
    \pgf@xa=\pgf@x
    \pgf@ya=\pgf@y
    \pgf@process{#3}%
    \pgf@xb=\pgf@x
    \pgf@yb=\pgf@y
    \advance\pgf@x by-\pgf@xa
    \advance\pgf@y by-\pgf@ya
    \pgf@sys@bp@correct{\pgf@xa}%
    \pgf@sys@bp@correct{\pgf@ya}%
    \pgf@sys@bp@correct{\pgf@xb}%
    \pgf@sys@bp@correct{\pgf@yb}%
    \pgf@xc=-\pgf@xa
    \pgf@yc=-\pgf@ya
    \xdef\pgfutil@tempa{%
      \hbox to\the\pgf@x{\vbox to\the\pgf@y{\vfil
          \noexpand\pgfsys@beginpurepicture%
          \noexpand\pgfsys@invoke{%
            % patch coords
            /pgfpatchllx \pgf@sys@tonumber{\pgf@xa} def
            /pgfpatchlly \pgf@sys@tonumber{\pgf@ya} def
            % shading patch width and height
            /pgfpatchX \pgf@sys@tonumber{\pgf@xb} pgfpatchllx sub def
            /pgfpatchY \pgf@sys@tonumber{\pgf@yb} pgfpatchlly sub def
            % number of samples in each direction (with samples spaced by approx. 1bp)
            /pgfsamplesx pgfpatchX round cvi 1 add def
            /pgfsamplesy pgfpatchY round cvi 1 add def
            % exact sample distance in each direction, in bp
            /pgfpatchdx pgfpatchX pgfsamplesx 1 sub div def
            /pgfpatchdy pgfpatchY pgfsamplesy 1 sub div def
            %
            pgfpatchllx neg pgfpatchlly neg translate
            /pgfproc {#4} bind def
            %
            % sampling procedure; repeatedly called by /ReusableStreamDecode filter;
            % on each call, /pgfsamplingproc puts one colour sample (rgb, cmyk or gray)
            % on the operand stack which is then consumed by the filter
            userdict /pgfsampleidx 0 put % internal index; updated on each call 
            /pgfsamplingproc {
              pgfsampleidx pgfsamplesx pgfsamplesy mul eq {
                () % push empty string on the stack to signal end-of-data
              } {
                pgfcolorsample\pgf@shading@model\space  % -str-
                pgfsampleidx pgfsamplesx mod pgfpatchdx mul pgfpatchllx add   % -str- x
                pgfsampleidx pgfsamplesx idiv pgfpatchdy mul pgfpatchlly add  % -str- x y
                pgfproc                                 % -str- <colour>
                pgfwritesample\pgf@shading@model\space  % -str-
                /pgfsampleidx pgfsampleidx 1 add store  % (update index)
              } ifelse
            } bind def
            %
            pgfpatchY pgfpatchX pgfpatchllx pgfpatchlly pgfe <<
              /PatternType 2
              /Shading <<
                /ShadingType 1
                /Domain [0 1 0 1]
                /Matrix [pgfpatchX 0 0 pgfpatchY pgfpatchllx pgfpatchlly]
                /ColorSpace \pgf@shading@device\space
                /Function <<
                  /FunctionType 0
                  /Order 1
                  /Domain [0 1 0 1]
                  /Range pgfrange\pgf@shading@model
                  /BitsPerSample pgfchanneldepth\pgf@shading@model % bits per channel, actually
                  /Size [pgfsamplesx pgfsamplesy]
                  /DataSource /pgfsamplingproc load /ReusableStreamDecode filter
                >>
              >>
            >> matrix makepattern setpattern fill
          }%
        \noexpand\pgfsys@endpurepicture%
      }\hss}%
    }%
  }%
  \global\expandafter\let\csname @pgfshading#1!\endcsname\pgfutil@tempa%
}

% helpers for converting FunctionType-4 to FunctionType-0, i. e. sampled, functions;
% FunctionType-4 functions are not defined in PostScript-3
%
% These are the different colour models we are going to use:
% +-------+-------------------+-----------+------------------+
% | model | channel depth/bit | max value | sample size/byte |
% +-------+-------------------+-----------+------------------+
% |  cmyk |                 8 |       255 |                4 |
% |   rgb |                 8 |       255 |                3 |
% |  gray |                24 |  16777215 |                3 |
% +-------+-------------------+-----------+------------------+
\expandafter\gdef\expandafter\pgfsys@atbegindocument\expandafter{\pgfsys@atbegindocument%
  \pgf@sys@postscript@header{
    %
    % -str- c m y k pgfwritesamplecmyk ==> -str-
    % -str- r g b   pgfwritesamplergb  ==> -str-
    % -str- gray    pgfwritesamplegray ==> -str-
    % writes a single colour sample into the /pgfcolorsample* string;
    % colour components (c,m,y,k; r,g,b; gray) between 0.0 to 1.0
    %
    /pgfwritesamplecmyk {
      4 index 0 5 index pgfcheckcolorrange 255 mul round cvi put
      4 index 1 4 index pgfcheckcolorrange 255 mul round cvi put
      4 index 2 3 index pgfcheckcolorrange 255 mul round cvi put
      4 index 3 2 index pgfcheckcolorrange 255 mul round cvi put
      pop pop pop pop
    } bind def
    %
    /pgfwritesamplergb {
      3 index 0 4 index pgfcheckcolorrange 255 mul round cvi put
      3 index 1 3 index pgfcheckcolorrange 255 mul round cvi put
      3 index 2 2 index pgfcheckcolorrange 255 mul round cvi put
      pop pop pop
    } bind def
    %
    /pgfwritesamplegray {
      % grayvalue between 0 and 16777215 (24 bit)
      pgfcheckcolorrange 16777215 mul round cvi % -str- gray24
      1 index 0 2 index           -16 bitshift put % high byte
      1 index 1 2 index 65535 and  -8 bitshift put % middle byte
      1 index 2 2 index   255 and              put % low byte
      pop
    } bind def
    %
    % <colour component> pgfcheckcolorrange ==> <something between 0.0 and 1.0>
    % truncates value to the allowed range (user-defined Type-4 functions may
    % happen to provide values outside this range)
    /pgfcheckcolorrange {
      dup 0.0 lt {pop 0.0} if
      dup 1.0 gt {pop 1.0} if
    } bind def
    %
    %colour depths per channel (bit)
    /pgfchanneldepthcmyk 8 def
    /pgfchanneldepthrgb 8 def
    /pgfchanneldepthgray 24 def
    %
    % allocate strings as buffers for storing a single colour sample
    /pgfcolorsamplecmyk 4 string def
    /pgfcolorsamplergb 3 string def
    /pgfcolorsamplegray 3 string def
    %
    %/Range array in the FunctionType 0 dictionary
    /pgfrangecmyk [0 1 0 1 0 1 0 1] def
    /pgfrangergb [0 1 0 1 0 1] def
    /pgfrangegray [0 1] def
  }%
}


%
% Opacity masks
%

\def\pgfsys@fadingfrombox#1#2{%
  \ifcsname pgfsmaskxform@#1\endcsname
    \pgfwarning{Fading '#1' already defined.\MessageBreak Going to replace it with new definition}%
  \fi%
  \global\advance\pgf@objectcount\@ne%
  {%
    \pgf@x=-.5\wd#2%
    \pgf@y=-.5\ht#2%
    \advance\pgf@y by.5\dp#2%
    \expandafter\xdef\csname pgfsmasktrans@#1\endcsname{%
      \noexpand\pgftransformcm{1}{0}{0}{1}{\noexpand\pgfqpoint{\the\pgf@x}{\the\pgf@y}}}%
    \edef\@tempa{\noexpand\pgfutil@insertatbegincurrentpagefrombox{%
      % box coordinates (reference point and top) needed for ps2pdf
      \pgfsys@outerinvoke{currentpoint /pgf@refy exch def /pgf@refx exch def}%
      \hbox to 0pt {\hbox to \ifdim\ht#2>\ht\strutbox\ht#2\else\ht\strutbox\fi{\hss\pgfsys@outerinvoke{%
        /pgf@top pgf@refy currentpoint pop pgf@refx sub VResolution Resolution div mul sub def%
      }}\hss}%
      % lower-right box corner coordinates for viewing
      \hbox to 0pt {\hbox to \wd#2{\hss\pgfsys@outerinvoke{/pgf@right currentpoint pop def}}\hss}%
      \hbox to 0pt {\hbox to \ifdim\dp#2>\dp\strutbox\dp#2\else\dp\strutbox\fi{\hss\pgfsys@outerinvoke{%
        /pgf@bot pgf@refy currentpoint pop pgf@refx sub VResolution Resolution div mul add def%
      }}\hss}%
      \pgfsys@outerinvoke{gsave
        clippath pathbbox newpath pop pop translate
        systemdict /pdfmark known {%
          % for ps2pdf, translate box to upper-left page corner, so we have the whole clipping
          % path (i. e. page area) available, as outlying parts get clipped
          pgf@refx neg pgf@top neg translate%
        } {%
          % for viewing, however, we want to hide the box by moving it off-page; we do so by aligning
          % the lower-right box corner with the upper-left page corner
          pgf@right neg pgf@bot neg translate%
        } ifelse
        % translate origin (0,0) to the reference point
        gsave pgf@refx pgf@refy translate
        mark /_objdef {pgfsmaskxform@\the\pgf@objectcount}
          /BBox [clippath pathbbox newpath] /BP pdfmark grestore%
      }%
      \wd#2=0pt
      \ht#2=0pt
      \dp#2=0pt
      \box#2%
      \pgfsys@outerinvoke{mark /EP pdfmark grestore}%
    }}\@tempa%
    \expandafter\xdef\csname pgfsmaskxform@#1\endcsname{\the\pgf@objectcount}%
  }%
}
\def\pgfsys@usefading#1#2#3#4#5#6#7{%
  \ifcsname pgfsmaskxform@#1\endcsname%
  {%
     \pgftransformreset%
     \pgftransformcm{#2}{#3}{#4}{#5}{\pgfpoint{#6}{#7}}%
     \csname pgfsmasktrans@#1\endcsname%
     {%
       \pgflowlevelsynccm%
       \pgfsys@outerinvoke{%
         /.begintransparencymaskgroup where {pop
           /pgf@sys@currentcolorspace currentcolorspace def
           /pgf@sys@currentcolor [currentcolor] cvx def
           /pgf@sys@OverrideICC currentuserparams /OverrideICC get def % Chr. Liddell
           <</OverrideICC true>> setuserparams                         % of Artifex
           /DeviceGray setcolorspace
           true <</Subtype/Luminosity>> clippath pathbbox newpath .begintransparencymaskgroup
           gsave
           72 Resolution div 72 VResolution div scale
           1 DVImag div 1 DVImag div neg scale
           mark {pgfsmaskxform@\csname pgfsmaskxform@#1\endcsname} /SP pdfmark
           grestore
           0 .endtransparencymask
           <</OverrideICC pgf@sys@OverrideICC>> setuserparams % restore orig. settings
           pgf@sys@currentcolorspace setcolorspace
           pgf@sys@currentcolor setcolor
           /pgfsmaskinplace true def
         } if%
       }%
     }%
     \pgftransforminvert%
     \pgflowlevelsynccm%
  }%
  \else
    \pgferror{Undefined fading '#1'}%
  \fi%
}
\def\pgfsys@clipfading{%
  \pgfsyssoftpath@invokecurrentpath%
  \pgfsys@clipnext%
  \pgfsys@discardpath%
}


%
% Transparency groups
%
\def\pgfsys@transparencygroupfrombox#1{%
  \global\advance\pgf@objectcount\@ne%
  \pgfsys@outerinvoke{%
    mark /_objdef {pgfxform@\the\pgf@objectcount} /BBox [clippath pathbbox newpath] /BP pdfmark
    gsave
    /pgf@sys@foa pgffoa def /pgf@sys@soa pgfsoa def
    /.setstrokeconstantalpha where
      {pop 1 .pgfsetfillopacityalpha 1 .pgfsetstrokeopacityalpha} if%
  }%
  \wd#1=0pt
  \ht#1=0pt
  \dp#1=0pt
  \hskip\pgf@picminx\box#1\hskip-\pgf@picminx
  \pgfsys@outerinvoke{%
    /pgffoa pgf@sys@foa def /pgfsoa pgf@sys@soa def
    grestore
    mark /EP pdfmark
    mark {pgfxform@\the\pgf@objectcount} << /Group << /S /Transparency
      /I \ifpgfsys@transparency@group@isolated true \else false \fi
      /K \ifpgfsys@transparency@group@knockout true \else false \fi  >> >> /PUT pdfmark%
  }%
  \setbox#1=\hbox{\pgfsys@outerinvoke{mark {pgfxform@\the\pgf@objectcount} /SP pdfmark}}%
}


%
% Image inclusion, adding SMask reference and image interpolation if mandated
%

\def\pgfsys@defineimage{%
  \ifx\pgf@imagewidth\pgfutil@empty\else\edef\pgf@imagewidth{width=\pgf@imagewidth,}\fi%
  \ifx\pgf@imageheight\pgfutil@empty\else\edef\pgf@imageheight{height=\pgf@imageheight,}\fi%
  \begingroup%
  \def\pgf@imageaddedkeys{}\def\@tempa{true}%
  \ifx\pgf@imageinterpolate\@tempa\def\pgf@imageaddedkeys{/Interpolate true}\fi%
  \ifx\pgf@imagemask\pgfutil@empty\else\edef\pgf@imageaddedkeys{\pgf@imageaddedkeys /SMask {\pgf@imagemask}}\fi%
  \edef\@tempa{%
    \ifx\pgf@imageaddedkeys\pgfutil@empty\else%
      \noexpand\pgfsys@outerinvoke{%
        save /pgfparent [(img) cvx cvn rand] cvx def %random name for current image
        mark /_objdef /pgfparent load /NI pdfmark
        % sometimes, image metadata contain embedded pdfmarks that prevent us from
        % writing to the image dictionary ourselves, so we disable them temporarily
        save /pdfmark /cleartomark load def
      }%
    \fi%
    \noexpand\includegraphics[\pgf@imageheight\pgf@imagewidth]{\pgf@filename}%
    \ifx\pgf@imageaddedkeys\pgfutil@empty\else%
      \noexpand\pgfsys@outerinvoke{%
        % empty image dummy, in case the provided file is not a valid raster image
        0 0 1 [1 0 0 1 0 0] {} image restore
        mark /pgfparent load <<\pgf@imageaddedkeys>> /PUT pdfmark restore
      }%
    \fi%
  }%
  \expandafter\endgroup%
  \expandafter\def\expandafter\pgf@image\expandafter{\@tempa}%
}


%
% Image masks
%

\def\pgfsys@definemask{%
  \global\advance\pgf@objectcount\@ne%
  \ifx\pgf@maskmatte\pgfutil@empty%
  \else%
    \edef\pgf@maskmatte{/Matte [\pgf@maskmatte]}%
  \fi%
  \pgfsys@outerinvoke{%
    mark /_objdef {pgfmask\the\pgf@objectcount} /NI pdfmark
    1 dict begin /pdfmark /cleartomark load def
  }%
  \special{psfile=\pgf@filename\space hsize=0 vsize=0}%
  \pgfsys@outerinvoke{%
    0 0 1 [1 0 0 1 0 0] {} image
    end mark {pgfmask\the\pgf@objectcount} <</Decode [1 0] \pgf@maskmatte>> /PUT pdfmark
  }%
  \edef\pgf@mask{pgfmask\the\pgf@objectcount}%
}

\endinput

%%% Local Variables:
%%% mode: latex
%%% End: