aboutsummaryrefslogtreecommitdiff
path: root/postscript/prologues/grabit.ps
blob: dab313c51f69564bca599bffed6d44291c5c3780 (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
%
% Dump a PostScript object, occasionally in a form that can be sent back
% through the interpreter. Similiar to Adobe's == procedure, but output
% is usually easier to read. No binding so operators like rcheck and exec
% can be conviently redefined.
%

/GrabitDict 100 dict dup begin

/recursive true def
/scratchstring 200 string def
/slowdown 100 def

/column 0 def
/lastcolumn 80 def
/level 0 def
/multiline 100 array def
/nextname 0 def
/arraylength 0 def
/lengthonly false def

/GrabitSetup {
	counttomark {OmitNames exch true put} repeat pop
	0 0 moveto		% for hardcopy output
} def

/OmitNames 30 dict def		% ignore these names
/OtherDicts 200 dict def	% unrecognized dictionaries

%
% All strings returned to the host go through Print. First pass through an
% array has lengthonly set to true.
%

/Print {
	dup type /stringtype ne {scratchstring cvs} if
	lengthonly {
		length arraylength add /arraylength exch def
	}{
		dup length column add /column exch def
		print flush
		slowdown {1 pop} repeat
	} ifelse
} def

/Indent {level {(    ) Print} repeat} def
/Newline {(\n) Print lengthonly not {/column 0 def} if} def

/NextLevel {/level level 1 add def multiline level 0 put} def
/LastLevel {/level level 1 sub def} def

%
% Make a unique name for each unrecognized dictionary and remember the name
% and dictionary in OtherDicts.
%

/Register {
	dup type /dicttype eq {
		/nextname nextname 1 add def
		dup (UnknownDict   ) dup
        	(UnknownDict) length nextname (   ) cvs putinterval
		0 (UnknownDict) length nextname (   ) cvs length add getinterval cvn
		exch OtherDicts 3 1 roll put
	} if
} def

%
% Replace array or dictionary values by known names. Lookups are in the
% standard PostScript dictionaries and in OtherDicts. If found replace
% the value by the name and make it executable so nametype omits the
% leading /.
%

/Replace {
	false
	1 index type /dicttype eq {pop true} if
	1 index type /arraytype eq 2 index xcheck not and {pop true} if
	{
		false
		[userdict systemdict statusdict serverdict OtherDicts] {
			{
				3 index eq
					{exch pop exch pop cvx true exit}
					{pop}
				ifelse
			} forall
			dup {exit} if
		} forall
		pop
	} if
} def

%
% Simple type handlers. In some cases (e.g. savetype) what's returned can't
% be sent back through the interpreter.
%

/booleantype {{(true )}{(false )} ifelse Print} def
/marktype {pop (mark ) Print} def
/nulltype {pop (null ) Print} def
/integertype {Print ( ) Print} def
/realtype {Print ( ) Print} def
/filetype {pop (-file- ) Print} def
/fonttype {pop (-fontID- ) Print} def
/savetype {pop (-saveobj- ) Print} def

%
% Special formatting for operators is enabled if the flag in multiline
% (for the current level) is set to 1. In that case each operator, after
% being printed, is looked up in OperatorDict. If found the value is used
% as an index into the OperatorProcs array and the object at that index
% is retrieved and executed. Currently only used to choose the operators
% that end a line.
%

/operatortype {
	dup Print ( ) Print
	multiline level get 1 eq {
		scratchstring cvs cvn dup OperatorDict exch known {
			OperatorDict exch get
			OperatorProcs exch get exec
		}{
			pop
			column lastcolumn gt {Newline Indent} if
		} ifelse
	}{pop} ifelse
} def

%
% Executable names are passed to operatortype. Non-executable names get a
% leading /.
%

/nametype {
	dup xcheck {
		operatortype
	}{
		(/) Print Print ( ) Print
	} ifelse
} def

%
% Arrays are processed in two passes. The first computes the length of the
% string returned to the host without any special formatting. If it extends
% past the last column special formatting is enabled by setting a flag in
% array multiline. Arrays are processed in a for loop so the last element
% easily recognized. At that point special fortmatting is disabled.
%

/packedarraytype {arraytype} def

/arraytype {
	NextLevel
	lengthonly not {
		/lengthonly true def
		/arraylength 0 def
		dup dup type exec
		arraylength 20 gt arraylength column add lastcolumn gt and {
			multiline level 1 put
		} if
		/lengthonly false def
	} if

	dup rcheck not {
		(-array- ) Print pop
	}{
		dup xcheck {({)}{([)} ifelse Print
		multiline level get 0 ne {Newline Indent}{( ) Print} ifelse
		0 1 2 index length 1 sub {
			2 copy exch length 1 sub eq multiline level get 1 eq and {
				multiline level 2 put
			} if
			2 copy get exch pop
			dup type /dicttype eq {
				Replace
				dup type /dicttype eq {
					dup Register Replace
					recursive {
						2 copy cvlit
						/def load 3 1 roll
						count 3 roll
					} if
					exch pop
				} if
			} if
			dup type exec
			dup xcheck not multiline level get 1 eq and {
				0 index type /arraytype eq
				1 index type /packedarray eq or
				1 index type /stringtype eq or {Newline Indent} if
			} if
		} for
		multiline level get 0 ne {Newline LastLevel Indent NextLevel} if
		xcheck {(} )}{(] )} ifelse Print
	} ifelse
	LastLevel
} def

%
% Dictionary handler. Try to replace the value by a name before processing
% the dictionary.
%

/dicttype {
	dup
	rcheck not {
		(-dictionary- ) Print pop
	}{
		dup maxlength Print ( dict dup begin) Print Newline
		NextLevel
		{
			1 index OmitNames exch known {
				pop pop
			}{
				Indent
				Replace		% arrays and dicts by known names
				Register	% new dictionaries in OtherDicts
				exch
				cvlit dup type exec	% key first - force a /
				dup type exec		% then the value
				(def) Print Newline
			} ifelse
		} forall
		LastLevel
		Indent
		(end ) Print
	} ifelse
} def

%
% Strings containing characters not in AsciiDict are returned in hex. All
% others are ASCII strings and use AsciiDict for character mapping.
%

/onecharstring ( ) def
/twocharstring (  ) def

/stringtype {
	dup
	rcheck not {
		(-string- ) Print
	}{
		/hexit false def
		dup {
			onecharstring 0 3 -1 roll put
			AsciiDict onecharstring cvn known not {
				/hexit true def exit
			} if
		} forall

		hexit {(<)}{(\()} ifelse Print
		0 1 2 index length 1 sub {
			2 copy 1 getinterval exch pop
			hexit {
				0 get /n exch def
				n -4 bitshift 16#F and 16 twocharstring cvrs pop
				n 16#F and twocharstring 1 1 getinterval 16 exch cvrs pop
				twocharstring
			}{cvn AsciiDict exch get} ifelse
			Print
			column lastcolumn gt {
				hexit not {(\\) Print} if
				Newline
			} if
		} for
		hexit {(> )}{(\) )} ifelse Print
	} ifelse
	pop
} def

%
% ASCII characters and replacement strings. Ensures the returned string will
% reproduce the original when passed through the scanner. Strings containing
% characters not in this list should be returned as hex strings.
%

/AsciiDict 128 dict dup begin
	(\n) cvn (\\n) def
	(\r) cvn (\\r) def
	(\t) cvn (\\t) def
	(\b) cvn (\\b) def
	(\f) cvn (\\f) def
	( ) cvn ( ) def
	(!) cvn (!) def
	(") cvn (") def
	(#) cvn (#) def
	($) cvn ($) def
	(%) cvn (\\%) def
	(&) cvn (&) def
	(') cvn (') def
	(\() cvn (\\\() def
	(\)) cvn (\\\)) def
	(*) cvn (*) def
	(+) cvn (+) def
	(,) cvn (,) def
	(-) cvn (-) def
	(.) cvn (.) def
	(/) cvn (/) def
	(0) cvn (0) def
	(1) cvn (1) def
	(2) cvn (2) def
	(3) cvn (3) def
	(4) cvn (4) def
	(5) cvn (5) def
	(6) cvn (6) def
	(7) cvn (7) def
	(8) cvn (8) def
	(9) cvn (9) def
	(:) cvn (:) def
	(;) cvn (;) def
	(<) cvn (<) def
	(=) cvn (=) def
	(>) cvn (>) def
	(?) cvn (?) def
	(@) cvn (@) def
	(A) cvn (A) def
	(B) cvn (B) def
	(C) cvn (C) def
	(D) cvn (D) def
	(E) cvn (E) def
	(F) cvn (F) def
	(G) cvn (G) def
	(H) cvn (H) def
	(I) cvn (I) def
	(J) cvn (J) def
	(K) cvn (K) def
	(L) cvn (L) def
	(M) cvn (M) def
	(N) cvn (N) def
	(O) cvn (O) def
	(P) cvn (P) def
	(Q) cvn (Q) def
	(R) cvn (R) def
	(S) cvn (S) def
	(T) cvn (T) def
	(U) cvn (U) def
	(V) cvn (V) def
	(W) cvn (W) def
	(X) cvn (X) def
	(Y) cvn (Y) def
	(Z) cvn (Z) def
	([) cvn ([) def
	(\\) cvn (\\\\) def
	(]) cvn (]) def
	(^) cvn (^) def
	(_) cvn (_) def
	(`) cvn (`) def
	(a) cvn (a) def
	(b) cvn (b) def
	(c) cvn (c) def
	(d) cvn (d) def
	(e) cvn (e) def
	(f) cvn (f) def
	(g) cvn (g) def
	(h) cvn (h) def
	(i) cvn (i) def
	(j) cvn (j) def
	(k) cvn (k) def
	(l) cvn (l) def
	(m) cvn (m) def
	(n) cvn (n) def
	(o) cvn (o) def
	(p) cvn (p) def
	(q) cvn (q) def
	(r) cvn (r) def
	(s) cvn (s) def
	(t) cvn (t) def
	(u) cvn (u) def
	(v) cvn (v) def
	(w) cvn (w) def
	(x) cvn (x) def
	(y) cvn (y) def
	(z) cvn (z) def
	({) cvn ({) def
	(|) cvn (|) def
	(}) cvn (}) def
	(~) cvn (~) def
end def

%
% OperatorDict can help format procedure listings. The value assigned to each
% name is used as an index into the OperatorProcs array. The procedure at that
% index is fetched and executed after the named operator is printed. What's in
% OperatorDict is a matter of taste rather than correctness. The default list
% represents our choice of which of Adobe's operators should end a line.
%

/OperatorProcs [{} {Newline Indent}] def

/OperatorDict 250 dict def

OperatorDict	/arc			1 put
OperatorDict	/arcn			1 put
OperatorDict	/ashow			1 put
OperatorDict	/awidthshow		1 put
OperatorDict	/banddevice		1 put
OperatorDict	/begin			1 put
OperatorDict	/charpath		1 put
OperatorDict	/clear			1 put
OperatorDict	/cleardictstack		1 put
OperatorDict	/cleartomark		1 put
OperatorDict	/clip			1 put
OperatorDict	/clippath		1 put
OperatorDict	/closefile		1 put
OperatorDict	/closepath		1 put
OperatorDict	/concat			1 put
OperatorDict	/copypage		1 put
OperatorDict	/curveto		1 put
OperatorDict	/def			1 put
OperatorDict	/end			1 put
OperatorDict	/eoclip			1 put
OperatorDict	/eofill			1 put
OperatorDict	/erasepage		1 put
OperatorDict	/exec			1 put
OperatorDict	/exit			1 put
OperatorDict	/fill			1 put
OperatorDict	/flattenpath		1 put
OperatorDict	/flush			1 put
OperatorDict	/flushfile		1 put
OperatorDict	/for			1 put
OperatorDict	/forall			1 put
OperatorDict	/framedevice		1 put
OperatorDict	/grestore		1 put
OperatorDict	/grestoreall		1 put
OperatorDict	/gsave			1 put
OperatorDict	/handleerror		1 put
OperatorDict	/if			1 put
OperatorDict	/ifelse			1 put
OperatorDict	/image			1 put
OperatorDict	/imagemask		1 put
OperatorDict	/initclip		1 put
OperatorDict	/initgraphics		1 put
OperatorDict	/initmatrix		1 put
OperatorDict	/kshow			1 put
OperatorDict	/lineto			1 put
OperatorDict	/loop			1 put
OperatorDict	/moveto			1 put
OperatorDict	/newpath		1 put
OperatorDict	/nulldevice		1 put
OperatorDict	/pathforall		1 put
OperatorDict	/print			1 put
OperatorDict	/prompt			1 put
OperatorDict	/put			1 put
OperatorDict	/putinterval		1 put
OperatorDict	/quit			1 put
OperatorDict	/rcurveto		1 put
OperatorDict	/renderbands		1 put
OperatorDict	/repeat			1 put
OperatorDict	/resetfile		1 put
OperatorDict	/restore		1 put
OperatorDict	/reversepath		1 put
OperatorDict	/rlineto		1 put
OperatorDict	/rmoveto		1 put
OperatorDict	/rotate			1 put
OperatorDict	/run			1 put
OperatorDict	/scale			1 put
OperatorDict	/setcachedevice		1 put
OperatorDict	/setcachelimit		1 put
OperatorDict	/setcacheparams		1 put
OperatorDict	/setcharwidth		1 put
OperatorDict	/setdash		1 put
OperatorDict	/setdefaulttimeouts	1 put
OperatorDict	/setdostartpage		1 put
OperatorDict	/seteescratch		1 put
OperatorDict	/setflat		1 put
OperatorDict	/setfont		1 put
OperatorDict	/setgray		1 put
OperatorDict	/sethsbcolor		1 put
OperatorDict	/setidlefonts		1 put
OperatorDict	/setjobtimeout		1 put
OperatorDict	/setlinecap		1 put
OperatorDict	/setlinejoin		1 put
OperatorDict	/setlinewidth		1 put
OperatorDict	/setmargins		1 put
OperatorDict	/setmatrix		1 put
OperatorDict	/setmiterlimit		1 put
OperatorDict	/setpacking		1 put
OperatorDict	/setpagetype		1 put
OperatorDict	/setprintname		1 put
OperatorDict	/setrgbcolor		1 put
OperatorDict	/setsccbatch		1 put
OperatorDict	/setsccinteractive	1 put
OperatorDict	/setscreen		1 put
OperatorDict	/settransfer		1 put
OperatorDict	/show			1 put
OperatorDict	/showpage		1 put
OperatorDict	/start			1 put
OperatorDict	/stop			1 put
OperatorDict	/store			1 put
OperatorDict	/stroke			1 put
OperatorDict	/strokepath		1 put
OperatorDict	/translate		1 put
OperatorDict	/widthshow		1 put
OperatorDict	/write			1 put
OperatorDict	/writehexstring		1 put
OperatorDict	/writestring		1 put

end def

%
% Put an object on the stack and call Grabit. Output continues until stack
% is empty. For example,
%
%		/letter load Grabit
%
% prints a listing of the letter procedure.
%

/Grabit {
	/saveobj save def
	GrabitDict begin
		{
			count 0 eq {exit} if
			count {dup type exec} repeat
			(\n) print flush
		} loop
	end
	currentpoint			% for hardcopy output
	saveobj restore
	moveto
} def