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
|
%!
% bb.ps --
%
% Prints a file, but keeps track of bounding box info, and prints the box at
% the end (around the figure.)
%
% $Header: /u/sy/beebe/src/psposter/RCS/bb.ps,v 1.3 1995/01/12 04:55:01 beebe Exp beebe $
% RCS log info at end
50 dict /$BoundingBox exch def
$BoundingBox begin
/xdef {
exch def
} def
/xstore {
exch store
} def
/addcoords {
exch
4 -1 roll add
3 1 roll add
} def
%
% Stubs of old functions.
%
/-stroke /stroke load def
/-fill /fill load def
/-eofill /eofill load def
/-image /image load def
/-show /show load def
/-awidthshow /awidthshow load def
/-showpage /showpage load def
/-restore /restore load def
/-imagemask /imagemask load def
end % $BoundingBox
%
% New Functions. --- These go into the user dict to intercept the calls
%
/stroke {
$BoundingBox begin
gsave
initmatrix
(stroke called\n) traceprint %%DEBUG
{
strokepath % Make sure to take line width into account.
0 setlinejoin
flattenpath
} stopped { % strokepath often hits a limitcheck.
(Can't set up a strokepath\n) traceprint % DEBUG
grestore % Restore the original path
gsave
} if
includepath % Accumulate it into our box.
grestore
-stroke
end % $BoundingBox
} def
/fill {
$BoundingBox begin
gsave
(fill called\n) traceprint %%DEBUG
includepath
grestore
-fill
end % $BoundingBox
} def
/eofill {
$BoundingBox begin
gsave
(eofill called\n) traceprint %%DEBUG
includepath
grestore
-eofill
end % $BoundingBox
} def
%
% Text is implemented by reducing everything to an `awidthshow'.
%
/show {
$BoundingBox begin
(show called\n) traceprint %%DEBUG
0 0 0 0 0 % Extra parameters for awidthshow
6 -1 roll % Bring the string back up
awidthshow
end % $BoundingBox
} def
/widthshow {
$BoundingBox begin
(widthshow called\n) traceprint %%DEBUG
0 0 % Extra parameters for awidthshow
3 -1 roll % Bring the string back up.
awidthshow
end % $BoundingBox
} def
/ashow {
$BoundingBox begin
(ashow called\n) traceprint %%DEBUG
0 0 0
6 3 roll
awidthshow
end % $BoundingBox
} def
% This does all of the work of the text-rendering operators
% What it does, is compute, basically brute force, what 'charpath'
% would have given us virtually for free, if 'show' were the only
% operator that we needed to do.
/awidthshow {
$BoundingBox begin
gsave
6 (awidthshow:) debug %%DEBUG
currentpoint
2 copy /@starty xdef /@startx xdef
2 index stringwidth % Get the natural length of the string
addcoords % Add to the start to get the end.
2 index length % How many characters?
dup % Add the offsets to each character
6 index mul
exch 5 index mul
addcoords
5 index 3 index
chcount % How many padding characters?
dup % Add the offsets for each pad.
9 index mul
exch 8 index mul
addcoords
/@endy xdef /@endx xdef
% We now have the left and right edges (in user coords)
% of the text. Now we need only correct for the vertical
% displacements needed for the font and we can get the
% top and bottom edges of the enclosing box
fontheight % Get the height and depth of the current font.
@startx @starty addcoords
/@starty xdef /@startx xdef
@endx @endy addcoords
/@endy xdef /@endx xdef
newpath
@startx @starty moveto
@endx @starty lineto
@endx @endy lineto
@startx @endy lineto
closepath
includepath
grestore
-awidthshow
end % $BoundingBox
} def
%
% `image':
%
% Assume here that the image lands in the unit square.
%
/image {
$BoundingBox begin
(image called\n) traceprint %%DEBUG
gsave
newpath
0 0 moveto
1 0 rlineto
1 1 rlineto
-1 0 rlineto
closepath
includepath
grestore
-image
end % $BoundingBox
} def
/imagemask
{
$BoundingBox begin
(imagemask called\n) traceprint %%DEBUG
gsave
newpath
0 0 moveto
1 0 rlineto
1 1 rlineto
-1 0 rlineto
closepath
includepath
grestore
-imagemask
end % $BoundingBox
} def
% Just define this one out of existence
/framedevice { pop pop pop pop } def
% Handle restoring VM --- this is all OK, except that we have to
% hang onto the bb info we collected while in the about-to-be-discarded
% environment
/restore
{
$BoundingBox begin
(restore called\n) traceprint %%DEBUG
tracedump %% HACK, but the only way I see right now to get this stuff!
bbox-llx bbox-lly bbox-urx bbox-ury
5 -1 roll
-restore
/bbox-ury xstore /bbox-urx xstore
/bbox-lly xstore /bbox-llx xstore
end % $BoundingBox
} def
%
% `showpage':
%
% Just draw the box around the figure and print the page, and then initialize
% the bounding box variables again.
%
$BoundingBox begin
/temp-string 10 string def
end % $BoundingBox
/showpage {
$BoundingBox begin
initgraphics
(showpage\n) traceprint % DEBUG
dump-bbox % DEBUG
/bbox-llx round_down
/bbox-lly round_down
/bbox-ury round_up
/bbox-urx round_up
bbox-llx bbox-lly moveto % Make the box
bbox-llx bbox-ury lineto
bbox-urx bbox-ury lineto
bbox-urx bbox-lly lineto
closepath
bwstroke % Draw the box.
% Print the size of the bounding box both above and below the actual box
0 setgray
/Courier findfont 10 scalefont setfont
bbox-llx 36 max bbox-lly 12 sub 36 max moveto
(%%BoundingBox: ) -show
bbox-llx temp-string cvs -show ( ) -show
bbox-lly temp-string cvs -show ( ) -show
bbox-urx temp-string cvs -show ( ) -show
bbox-ury temp-string cvs -show
bbox-llx 36 max bbox-ury 12 add 740 min moveto
(%%BoundingBox: ) -show
bbox-llx temp-string cvs -show ( ) -show
bbox-lly temp-string cvs -show ( ) -show
bbox-urx temp-string cvs -show ( ) -show
bbox-ury temp-string cvs -show
init
-showpage
tracedump %% DEBUG
end % $BoundingBox
} def
%
% BoundingBox functions:
%
% We accumulate the information about the bounding box into four variables.
% The data is stored in default coordinates.
%
$BoundingBox begin
/init {
/bbox-llx 99999 store
/bbox-lly 99999 store
/bbox-urx -99999 store
/bbox-ury -99999 store
} def
/bbox-llx 0 def
/bbox-lly 0 def
/bbox-urx 0 def
/bbox-ury 0 def
%
% - `includepath' -
%
% Incorporates the bounding box of the path into the bounding box info.
% ... Gets the bounding box in default coords
/includepath {
(Adding a path: ) traceprint %%DEBUG
gsave
initmatrix
{
0 setlinejoin
flattenpath
} stopped {
(Couldn't flatten the path\n) traceprint % DEBUG
grestore
gsave
initmatrix
} if
{ pathbbox } stopped not
{
4 2 roll % Just so we get lower-left first
2 copy dump-coord %%DEBUG
dup bbox-lly lt {
/bbox-lly xstore
} {
pop
} ifelse
dup bbox-llx lt {
/bbox-llx xstore
} {
pop
} ifelse
(; ) traceprint 2 copy dump-coord (\n) traceprint %%DEBUG
dup bbox-ury gt {
/bbox-ury xstore
} {
pop
} ifelse
dup bbox-urx gt {
/bbox-urx xstore
} {
pop
} ifelse
dump-bbox %%DEBUG
} if
grestore
} def
%
% A nice black-and white line drawing function.
%
/bwstroke {
0 setlinewidth % Thinnest possible lines
1 setgray % White first
[5] 0 setdash % Only half the line
gsave -stroke grestore
0 setgray % Then black
[5] 5 setdash % On the other half
-stroke
} def
%
% Stuff for text.
%
%
% char-code string `chcount' occurs
%
% Counts the number of times a character appears in a string.
%
/chcount {
0 exch
{
2 index eq {
1 add
} if
} forall
exch pop
} def
%
% - `fontheight' heightx heighty depthx depthy
%
% Returns the offsets to the lowest point and highest point in the current
% font.
%
/fontheight {
currentfont begin
/FontBBox load aload pop
exch pop 0 exch
FontMatrix transform
4 2 roll
exch pop 0 exch
FontMatrix transform
end
} def
% key round_{down|up} - These will round the value of the given key
% up or down, as appropriate, to the nearest integer
/round_up { dup load ceiling cvi store } def
/round_down { dup load floor cvi store } def
% key binddefinition - this will do a 'bind' on the procedure given by 'key'
/binddefinition
{
dup where
{
exch
2 copy
get bind put
}
{ undefined } ifelse
} def
% Given two numbers on the stack, return with just the smallest
/min { 2 copy ge { exch } if pop } def
% Dito for the largest of the pair
/max { 2 copy lt { exch } if pop } def
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
%
% Debugging utilities
%
/$tracedict where
{ % Trace package loaded... do the tracing
pop
% This is a debugging function to print out what is going on.
% Format <argn> <argn-1> ... <arg1> n <string> debug <argn> ... <arg1>
% (that is, the 'n' args will be *left* on the stack!)
/debug
{
traceprint (\n) traceprint
dup 1 add % Now total number of args (including arg count)
copy
{
( ) traceprint
trace=
(\n) traceprint
} repeat
pop % Remove the extra copy of the arg count
} def
% Print out a coordinate on the stack: x y ---
/dump-coord
{
(\() traceprint exch trace= (, ) traceprint trace= (\)) traceprint
} def
% Print out bb's current notion of its bounding box
/dump-bbox
{
(Bounding Box: ) traceprint
bbox-llx bbox-lly dump-coord
(; ) traceprint
bbox-urx bbox-ury dump-coord
(\n) traceprint
} def
tracebegin %% DEBUG
}
{ % No trace package loaded, so don't trace. Stub out the various calls
/traceprint { pop } def
/dump-coord { pop pop } def
/dump-bbox { } def
/debug { pop pop } def
/tracedump { } def
} ifelse
% % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
% Bind everything
/xdef binddefinition
/xstore binddefinition
/addcoords binddefinition
/stroke binddefinition
/fill binddefinition
/eofill binddefinition
/show binddefinition
/widthshow binddefinition
/ashow binddefinition
/awidthshow binddefinition
/image binddefinition
/showpage binddefinition
/init binddefinition
/includepath binddefinition
/bwstroke binddefinition
/chcount binddefinition
/fontheight binddefinition
/debug binddefinition
/dump-coord binddefinition
/dump-bbox binddefinition
% Start it up.
init
end % $BoundingBox
% end of bb.ps
|