summaryrefslogtreecommitdiff
path: root/gs/lib/pdf_draw.ps
blob: 458788227c7a1f65e499d0acc8596e70b0a76cf9 (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
%    Copyright (C) 1994, 1995, 1997, 1998 Aladdin Enterprises.  All rights reserved.
% 
% This file is part of Aladdin Ghostscript.
% 
% Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
% or distributor accepts any responsibility for the consequences of using it,
% or for whether it serves any particular purpose or works at all, unless he
% or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
% License (the "License") for full details.
% 
% Every copy of Aladdin Ghostscript must include a copy of the License,
% normally in a plain ASCII text file named PUBLIC.  The License grants you
% the right to copy, modify and redistribute Aladdin Ghostscript, but only
% under certain conditions described in the License.  Among other things, the
% License requires that the copyright notice and this notice be preserved on
% all copies.

% 
% pdf_draw.ps
% PDF drawing operations (graphics, text, and images).

% We don't handle the following PDF 1.0/1.1 elements yet: (identified by
%	style strings, except in a few known fonts
%	font descriptor resources, except for MissingWidth
%	text clipping modes
%		What do these mean??

/.setlanguagelevel where { pop 2 .setlanguagelevel } if
.currentglobal true .setglobal
/pdfdict where { pop } { /pdfdict 100 dict def } ifelse
GS_PDF_ProcSet begin
pdfdict begin

% For simplicity, we use a single interpretation dictionary for all
% PDF graphics operations, even though this is too liberal.
/drawopdict 100 dict def

% ================================ Graphics ================================ %

% ---------------- Functions ---------------- %

/fnrdict mark
  0 { .resolvefn0 }
  2 { }
  3 { .resolvefn3 }
.dicttomark readonly def

/.resolvefn0 {
		% Don't lose our place in PDFfile.
  PDFfile fileposition exch
  dup true resolvestream
		% The stream isn't positionable, so read all the data now.
		% Stack: filepos fndict stream
  1 index /Range oget length 2 idiv 2 index /BitsPerSample oget mul
  2 index /Size oget { mul } forall
  7 add 8 idiv string
  1 index exch readstring pop exch closefile
		% Stack: filepos fndict data
  exch dup length 1 add dict .copydict
  dup /DataSource 4 -1 roll put
  .buildfunction
  exch PDFfile exch setfileposition
} bdef

/.resolvefn3 {
  dup /Functions oget mark exch dup { resolvefunction } forall
  counttomark -1 roll astore cleartomark
} bdef

/resolvefunction {	% <fndict> resolvefunction <function>
  dup /FunctionType oget //fnrdict exch get exec
} bdef

/resolveidfunction {
  dup /Identity eq { pop { } } { resolvefunction } ifelse
} bdef

% ---------------- Halftones ---------------- %

/spotfunctions mark
  /Round {
    abs exch abs 2 copy add 1 le {
      dup mul exch dup mul add 1 exch sub 
    } {
      1 sub dup mul exch 1 sub dup mul add 1 sub
    } ifelse
  }
  /Diamond {
    abs exch abs 2 copy add .75 le {
      dup mul exch dup mul add 1 exch sub
    } {
      2 copy add 1.23 le {
	.85 mul add 1 exch sub
      } {
	1 sub dup mul exch 1 sub dup mul add 1 sub
      } ifelse
    } ifelse
  }
  /Ellipse {
    abs exch abs 2 copy 3 mul exch 4 mul add 3 sub dup 0 lt {
      pop dup mul exch .75 div dup mul add 4 div 1 exch sub
    } {
      dup 1 gt {
	pop 1 exch sub dup mul exch 1 exch sub
	.75 div dup mul add 4 div 1 sub
      } {
	.5 exch sub exch pop exch pop
      } ifelse
    } ifelse
  }
  /EllipseA { dup mul .9 mul exch dup mul add 1 exch sub }
  /InvertedEllipseA { dup mul .9 mul exch dup mul add 1 sub }
  /EllipseB { dup 5 mul 8 div mul exch dup mul exch add sqrt 1 exch sub }
  /EllipseC { dup mul .9 mul exch dup mul add 1 exch sub }
  /InvertedEllipseC { dup mul .9 mul exch dup mul add 1 sub }
  /Line { exch pop abs neg }
  /LineX { pop }
  /LineY { exch pop }
  /Square { abs exch abs 2 copy lt { exch } if pop neg }
  /Cross { abs exch abs 2 copy gt { exch } if pop neg }
  /Rhomboid { abs exch abs 0.9 mul add 2 div }
  /DoubleDot { 2 {360 mul sin 2 div exch } repeat add }
  /InvertedDoubleDot { 2 {360 mul sin 2 div exch } repeat add neg }
  /SimpleDot { dup mul exch dup mul add 1 exch sub }
  /InvertedSimpleDot { dup mul exch dup mul add 1 sub }
  /CosineDot { 180 mul cos exch 180 mul cos add 2 div }
  /Double { exch 2 div exch 2 { 360 mul sin 2 div exch } repeat add }
  /InvertedDouble {
    exch 2 div exch 2 { 360 mul sin 2 div exch } repeat add neg
  }
.dicttomark readonly def

/htrdict mark
  1 { .resolveht1 }
  5 { .resolveht5 }
	% We don't support types 6, 10, or 16 yet.
.dicttomark readonly def

/.resolveht1 {
  mark exch {
    oforce
    1 index /SpotFunction eq {
      dup type /nametype eq
	{ //spotfunctions exch get } { resolvefunction }
      ifelse
    } {
      1 index /TransferFunction eq {
	resolveidfunction
      } if
    } ifelse
  } forall .dicttomark
} bdef

/.resolveht5 {
  mark exch {
    oforce dup type /dicttype eq { resolvehalftone } if
  } forall .dicttomark
} bdef

/resolvehalftone {	% <dict> resolvehalftone <halftone>
  dup /HalftoneType get //htrdict exch get exec
} bdef

% ---------------- Graphics state management ---------------- %

/cmmatrix matrix def
drawopdict begin
			% Graphics state stack
  /q { q } def
  /Q { Q } def
			% Graphics state setting
  /cm { //cmmatrix astore concat } def
  /i /setflat load def
  /J /setlinecap load def
  /d /setdash load def
  /j /setlinejoin load def
  /w /setlinewidth load def
  /M /setmiterlimit load def
  /gs { gs } def
end

/gsparamdict mark
  /Type { pop }
  /SA { setstrokeadjust }
  /OP { setoverprint }
  /BG { resolveidfunction setblackgeneration }
  /UCR { resolveidfunction setundercolorremoval }
  /TR { resolveidfunction settransfer }
  /HT {
    dup /Default eq {
      pop .setdefaultscreen
    } {
	%****** NOT FINISHED ******
      resolvehalftone sethalftone
    } ifelse
  }
  /HTP { aload pop sethalftonephase }
  /SM { setsmoothness }
  /Font { aload pop Tf }
  /LW { setlinewidth }
  /LC { setlinecap }
  /LJ { setlinejoin }
  /ML { setmiterlimit }
  /D { aload pop setdash }
  /FL { setflat }
  /RI { ri }
.dicttomark readonly def
/gs {			% <gsres> gs -
  Page /ExtGState rget {
    { oforce exch gsparamdict exch .knownget { exec } { pop } ifelse }
    forall
  } if
} bdef

% ---------------- Color setting ---------------- %

% The keys here are resolved (PostScript, not PDF) color space names.
/csncompdict mark
  /DeviceGray { pop 1 }
  /DeviceRGB { pop 3 }
  /DeviceCMYK { pop 4 }
  /CIEBasedA { pop 1 }
  /CIEBasedABC { pop 3 }
  /Separation { pop 1 }
  /DeviceN { 2 oget length }
.dicttomark readonly def

% Perhaps some of the values in the following need to be modified
% depending on the WhitePoint value....
/cslabinit mark
  /DecodeABC [{16 add 116 div} bind {500 div} bind {200 div} bind]
  /MatrixABC [1 1 1 1 0 0 0 0 -1]
  /DecodeLMN [
    {dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse
     0.9505 mul} bind
    {dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse
     } bind
    {dup 6 29 div ge {dup dup mul mul} {4 29 div sub 108 841 div mul} ifelse
     1.0890 mul} bind
  ]
.dicttomark readonly def

/csrdict mark
  /DeviceGray {
    /DefaultGray Page /ColorSpace rget { exch pop resolvecolorspace } if
  }
  /DeviceRGB {
    /DefaultRGB Page /ColorSpace rget { exch pop resolvecolorspace } if
  }
  /DeviceCMYK { }
  /CalGray {
    1 oget dup /Gamma knownoget {
      exch dup length 1 add dict .copydict
      dup /DecodeA 4 -1 roll /exp load 2 packedarray cvx put
    } if
    /CIEBasedA exch 2 array astore
  }
  /CalRGB {
    1 oget dup /Gamma known 1 index /Matrix known or {
      dup length 2 add dict .copydict
      dup /Matrix knownoget { 1 index /MatrixABC 3 -1 roll put } if
      dup /Gamma knownoget {
        [ exch { /exp load 2 packedarray cvx } forall
        ] 1 index /DecodeABC 3 -1 roll put
      } if
    }
    if /CIEBasedABC exch 2 array astore 
  }
  /CalCMYK {
    pop /DeviceCMYK		% not defined by Adobe
  }
  /Lab {
    1 oget dup length 3 add dict .copydict
    dup /Range knownoget not { [-100 100 -100 100] } if
    [-100 0 null null null null] dup 2 4 -1 roll putinterval
    1 index /RangeABC 3 -1 roll put
    //cslabinit exch copy
    /CIEBasedABC exch 2 array astore
  }
  /ICCBased {
		% Since the PostScript interpreter doesn't support these yet,
		% always substitute an alternate space.
    dup /Alternate knownoget {
      exch pop
    } {
      /N get { null /DeviceGray null /DeviceRGB /DeviceCMYK } exch get
    } ifelse resolvecolorspace
  }
  /Separation {
    dup 2 2 copy oget resolvecolorspace put
    dup 3 oget resolvefunction
    1 index 3 3 -1 roll put
  }
  /DeviceN {
    dup 1 oget pop		% just force it
    dup 2 2 copy oget resolvecolorspace put
    dup 3 oget resolvefunction
    1 index 3 3 -1 roll put
  }
  /Indexed
   { dup 1 2 copy oget resolvecolorspace put
     dup 3 oget dup type /stringtype eq
      { pop
      }
      {		% The color lookup table is a stream.
		% Get its contents.  Don't lose our place in PDFfile.
	PDFfile fileposition 3 1 roll
	true resolvestream
	1 index 2 get 1 add
		% Stack: filepos cspace lookupstream len
	2 index 1 get	% basespace
	  dup dup type /arraytype eq { 0 get } if
	  //csncompdict exch get exec mul
	string readstring pop
		% Stack: filepos cspace table
	1 index 3 3 -1 roll put
	exch PDFfile exch setfileposition
      }
     ifelse
   }
  /Pattern
   { dup type /nametype ne
      { dup length 1 ge
	 { 1 get resolvecolorspace
	   /Pattern exch 2 array astore
	 }
	if
      }
     if
   }
.dicttomark readonly def

/cssubst {		% <csname> cssubst <cspace'> true
			% <csname> cssubst false
  dup resolvecolorspace
  dup 1 index ne { exch pop true } { pop pop false } ifelse
} bdef

/csresolve		% <csresourcename> csresolve <cspace>
 { dup Page /ColorSpace rget
    { exch pop resolvecolorspace }
    { /undefined cvx signalerror }
   ifelse
 } bdef
/resolvecolorspace {	% <cspace> resolvecolorspace <cspace'>
  dup dup type /arraytype eq { 0 get } if
  //csrdict exch .knownget {
    exec dup type /nametype ne { dup length 1 eq { 0 get } if } if
  } {
    csresolve
  } ifelse
} bdef

/scresolve	% <c0> ... scresolve <multi>
 {		% We can't really make sc[n] and SC[N] work, because
		% the color space information isn't available at
		% conversion time; so we hack it by assuming that
		% all the operands on the stack are used, and that
		% if the top operand is a name, it's a Pattern resource.
   dup type /nametype eq
    { Page /Pattern rget { resolvepattern } { null } ifelse }
   if
   count 1 gt
 } bdef
/resolvepattern		% <patternstreamdict> resolvepattern <patterndict>
 {		% Don't do the resolvestream now: just capture the data
		% from the file if necessary.
   dup length dict copy
   dup /FilePosition .knownget
    { 1 index /File get dup fileposition 3 1 roll
		% Stack: dict savepos pos file
      dup 3 -1 roll setfileposition
      dup 3 index /Length get string readstring pop
		% Stack: dict savepos file string
      3 1 roll exch setfileposition
      1 index /File 3 -1 roll put
      dup /FilePosition undef
    }
   if
   dup /PaintProc
    { false resolvestream pdfopdict .pdfrun
    }
   put
 } bdef

drawopdict begin
  /g { /DeviceGray cssubst { cs sc1 } { g } ifelse } bdef
  /rg { /DeviceRGB cssubst { cs sc* } { rg } ifelse } bdef
  /k { k } bdef
  /cs { csresolve cs } bdef
  /sc { scresolve { sc* } { sc1 } ifelse } bdef
  /scn /sc load def
  /G { /DeviceGray cssubst { CS SC1 } { G } ifelse } bdef
  /RG { /DeviceRGB cssubst { CS SC* } { RG } ifelse } bdef
  /K { K } bdef
  /CS { csresolve CS } bdef
  /SC { scresolve { SC* } { SC1 } ifelse } bdef
  /SCN /SC load def
end

% ---------------- Paths ---------------- %

drawopdict begin
			% Path construction
  /m /moveto load def
  /l /lineto load def
  /c /curveto load def
  /v { currentpoint 6 2 roll curveto } def
  /y { 2 copy curveto } def
  /re {
   4 2 roll moveto  exch dup 0 rlineto  0 3 -1 roll rlineto  neg 0 rlineto
   closepath
  } def
  /h /closepath load def
			% Path painting and clipping
  /n { n } def
  /S { S } def
  /s { s } def
  /f { f } def
  /f* { f* } def
  /B { B } def
  /b { b } def
  /B* { B* } def
  /b* { b* } def
  /W { W } def
  /W* { W* } def
end

% ---------------- XObjects ---------------- %

/xobjectprocs mark		% <dict> -proc- -
  /Image { DoImage }
  /Form { DoForm }
  /PS { DoPS }
.dicttomark readonly def

% Note that the keys in defaultdecodedict are resolved (PostScript, not PDF)
% color space names.
/01_1 [0 1] readonly def
/01_3 [0 1 0 1 0 1] readonly def
/01_4 [0 1 0 1 0 1 0 1] readonly def
/defaultdecodedict mark
  /DeviceGray { pop //01_1 }
  /DeviceRGB { pop //01_3 }
  /DeviceCMYK { pop //01_4 }
  /CIEBasedA { 1 get /RangeA .knownget not { //01_1 } if }
  /CIEBasedABC { 1 get /RangeABC .knownget not { //01_3 } if }
  /Separation { pop //01_1 }
  /DeviceN {
    dup 1 oget length [ exch {0 1} repeat ] readonly
  }
.dicttomark readonly def

/makeimagedict
 { dup length dict
   1 index /ColorSpace knownoget
    { resolvecolorspace
      dup type /arraytype eq { dup length 1 eq { 0 get } if } if
      exch begin /ColorSpace exch def
    }
    { begin
    }
   ifelse
   /ImageType 1 def
		% Always define ImageMask appropriately.
   dup /ImageMask knownoget dup { and } if
     /ImageMask exch def
   /Width 2 copy oget def
   /Height 2 copy oget def
   /BitsPerComponent 2 copy oget def
   /Decode 2 copy knownoget not
    {		% Decode is required for the PostScript image operators.
      ImageMask
       { [0 1]
       }
       { ColorSpace dup type /arraytype eq { 0 get } if dup /Indexed eq
	  { pop [ 0 1 BitsPerComponent bitshift 1 sub ] }
	  { defaultdecodedict exch get ColorSpace exch exec }
	 ifelse
       }
      ifelse
    }
   if def
   /Interpolate 2 copy knownoget { def } { pop } ifelse
   /ImageMatrix Width 0 0 Height neg 0 Height 6 array astore def
		% Even though we're going to read data,
		% pass false to resolvestream so that
		% it doesn't try to use Length (which may not be present).
   false resolvestream
     /DataSource exch def
  currentdict end
} bdef

/DoImage {
  makeimagedict
  dup /Mask knownoget {
    dup type /arraytype eq {
      1 index /ImageType 4 put
      1 index /MaskColor 3 -1 roll put
    } {
		% Mask is a stream, another Image XObject.
      makeimagedict
      4 dict begin
      /ImageType 3 def
      /InterleaveType 3 def
      /MaskDict exch def
      /DataDict exch def
      currentdict end
    } ifelse
  } if
  dup /ImageMask get
   { setfillcolor imagemask }
   { dup /ColorSpace get csset setcolorspace pop image }
  ifelse
} bdef

/DoForm {
  dup length dict copy
  dup [ /pop load 2 index /Resources knownoget { oforce } { 0 dict } ifelse
  3 index false /resolvestream cvx
  pdfopdict /.pdfruncontext cvx
  ] cvx /PaintProc exch put
  execform
} bdef

/DoPS {
  true resolvestream cvx exec
} bdef

drawopdict begin
  /Do
    { PDFfile fileposition exch
      dup Page /XObject rget not { /undefined cvx signalerror } if
      exch pop dup /Subtype get xobjectprocs exch get exec
      PDFfile exch setfileposition
    } bdef
end

% ---------------- In-line images ---------------- %

% Undo the abbreviations in an in-line image dictionary.
% Note that these can appear as keys, values, or members of array values.
% /I is ambiguous; we check specially for it below.
/unabbrevdict mark
	% Top-level dictionary keys
  /BPC /BitsPerComponent  /CS /ColorSpace  /D /Decode  /DP /DecodeParms
  /F /Filter  /H /Height  /IM /ImageMask  /W /Width
	% Values
  /AHx /ASCIIHexDecode  /A85 /ASCII85Decode  /CC /CalCMYK
  /CCF /CCITTFaxDecode  /CG /CalGray  /CR /CalRGB
  /DCT /DCTDecode  /CMYK /DeviceCMYK  /Fl /FlateDecode
  /G /DeviceGray  /RGB /DeviceRGB
  /I /Indexed  /LZW /LZWDecode  /RL /RunLengthDecode
.dicttomark readonly def
/unabbrev		% <obj> unabbrev <obj'>
 { dup type /nametype eq
    { unabbrevdict 1 index .knownget { exch pop } if
    }
    { dup type /arraytype eq
       { dup 0 1 2 index length 1 sub
	  { 2 copy get unabbrev put dup
	  }
	 for pop
       }
      if
    }
   ifelse
 } bdef

drawopdict begin
  /BI { mark } bdef
  /ID
    { counttomark
       { counttomark 1 roll
	 dup /I eq { pop /Interpolate } { unabbrev } ifelse
       }
      repeat
      /File PDFsource
      .dicttomark DoImage
      PDFsource token pop /EI ne { /ID cvx /syntaxerror signalerror } if
    } bdef
end

% ================================ Text ================================ %

drawopdict begin
			% Text control
  /BT { BT } def
  /ET { ET } def
  /Tc { Tc } def
  /TL { TL } def
  /Tr { Tr } def
  /Ts { Ts } def
  /Tw { Tw } def
  /Tz { Tz } def
			% Text positioning
  /Td { Td } def
  /TD { TD } def
  /Tm { Tm } def
  /T* { T* } def
			% Text painting
  /Tj { Tj } def
  /' { ' } def
  /" { " } def
  /TJ { TJ } def
end

end			% pdfdict
end			% GS_PDF_ProcSet
.setglobal