home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / pc / source / bbfig / bb.ps next >
Encoding:
Text File  |  1990-06-30  |  11.0 KB  |  558 lines

  1. %! 
  2. % bb.ps --
  3. % Prints a file, but keeps track of bounding box info, and prints the box at
  4. % the end (around the figure.)
  5. % $Header: bb.ps,v 1.10 90/06/27 10:47:22 cosell Exp $
  6. % RCS log info at end
  7.  
  8. 50 dict /$BoundingBox exch def
  9.  
  10. $BoundingBox begin
  11.  
  12. /xdef {
  13.     exch def
  14. } def
  15.  
  16. /xstore {
  17.     exch store
  18. } def
  19.  
  20. /addcoords {
  21.     exch
  22.     4 -1 roll add
  23.     3 1 roll add
  24. } def
  25.  
  26. % Stubs of old functions.
  27.  
  28. /-stroke /stroke load def
  29. /-fill /fill load def
  30. /-eofill /eofill load def
  31. /-image /image load def
  32. /-show /show load def
  33. /-awidthshow /awidthshow load def
  34. /-showpage /showpage load def
  35. /-restore  /restore load def
  36. /-imagemask /imagemask load def
  37.  
  38. end % $BoundingBox
  39.  
  40. % New Functions.   --- These go into the user dict to intercept the calls
  41.  
  42. /stroke {
  43.         $BoundingBox begin
  44.     gsave
  45.     initmatrix
  46.     (stroke called\n) traceprint %%DEBUG
  47.     {
  48.          strokepath    % Make sure to take line width into account.
  49.         0 setlinejoin
  50.         flattenpath
  51.     } stopped {        % strokepath often hits a limitcheck.
  52.         (Can't set up a strokepath\n) traceprint % DEBUG
  53.         grestore    % Restore the original path
  54.         gsave
  55.     } if
  56.     includepath            % Accumulate it into our box.
  57.     grestore
  58.  
  59.     -stroke
  60.     end % $BoundingBox
  61. } def
  62.  
  63. /fill {
  64.         $BoundingBox begin
  65.     gsave
  66.     (fill called\n) traceprint %%DEBUG
  67.     includepath
  68.     grestore
  69.  
  70.     -fill
  71.     end % $BoundingBox
  72. } def
  73.  
  74. /eofill {
  75.         $BoundingBox begin
  76.     gsave
  77.     (eofill called\n) traceprint %%DEBUG
  78.     includepath
  79.     grestore
  80.  
  81.     -eofill
  82.     end % $BoundingBox
  83. } def
  84.  
  85. % Text is implemented by reducing everything to an `awidthshow'.
  86.  
  87. /show {
  88.         $BoundingBox begin
  89.     (show called\n) traceprint %%DEBUG
  90.     0 0 0 0 0        % Extra parameters for awidthshow
  91.     6 -1 roll        % Bring the string back up
  92.     awidthshow
  93.     end % $BoundingBox
  94. } def
  95.  
  96. /widthshow {
  97.         $BoundingBox begin
  98.         (widthshow called\n) traceprint %%DEBUG
  99.     0 0            % Extra parameters for awidthshow
  100.     3 -1 roll        % Bring the string back up.
  101.     awidthshow
  102.     end % $BoundingBox
  103. } def
  104.  
  105. /ashow {
  106.         $BoundingBox begin
  107.         (ashow called\n) traceprint %%DEBUG
  108.     0 0 0 
  109.     6 3 roll
  110.     awidthshow
  111.     end % $BoundingBox
  112. } def
  113.  
  114.  
  115. % This does all of the work of the text-rendering operators
  116. %   What it does, is compute, basically brute force, what 'charpath'
  117. %   would have given us virtually for free, if 'show' were the only
  118. %   operator that we needed to do.
  119.  
  120. /awidthshow {
  121.     $BoundingBox begin
  122.     gsave
  123.     6 (awidthshow:) debug %%DEBUG
  124.     currentpoint
  125.     2 copy /@starty xdef /@startx xdef
  126.     2 index stringwidth    % Get the natural length of the string
  127.     addcoords            % Add to the start to get the end.
  128.  
  129.     2 index length        % How many characters?
  130.  
  131.     dup            % Add the offsets to each character
  132.     6 index mul
  133.     exch 5 index mul
  134.     addcoords
  135.  
  136.     5 index 3 index
  137.     chcount        % How many padding characters?
  138.  
  139.     dup            % Add the offsets for each pad.
  140.     9 index mul
  141.     exch 8 index mul
  142.     addcoords
  143.  
  144.     /@endy xdef /@endx xdef
  145.  
  146.     % We now have the left and right edges (in user coords)
  147.     % of the text.  Now we need only correct for the vertical
  148.     % displacements needed for the font and we can get the
  149.     % top and bottom edges of the enclosing box
  150.  
  151.     fontheight        % Get the height and depth of the current font.
  152.     
  153.     @startx @starty addcoords
  154.     /@starty xdef /@startx xdef
  155.     @endx @endy addcoords
  156.     /@endy xdef /@endx xdef
  157.     newpath
  158.     @startx @starty moveto
  159.     @endx @starty lineto
  160.     @endx @endy lineto
  161.     @startx @endy lineto
  162.     closepath
  163.         includepath
  164.     grestore
  165.  
  166.     -awidthshow
  167.     end % $BoundingBox
  168. } def
  169.  
  170. % `image':
  171. % Assume here that the image lands in the unit square.
  172.  
  173. /image {
  174.         $BoundingBox begin
  175.         (image called\n) traceprint %%DEBUG
  176.     gsave
  177.     newpath
  178.     0 0 moveto
  179.     1 0 rlineto
  180.     1 1 rlineto
  181.     -1 0 rlineto
  182.     closepath
  183.     includepath
  184.     grestore
  185.  
  186.     -image
  187.     end % $BoundingBox
  188. } def
  189.  
  190. /imagemask
  191. {
  192.     $BoundingBox begin
  193.     (imagemask called\n) traceprint %%DEBUG
  194.     gsave
  195.     newpath
  196.     0 0 moveto
  197.     1 0 rlineto
  198.     1 1 rlineto
  199.     -1 0 rlineto
  200.     closepath
  201.     includepath
  202.     grestore
  203.  
  204.     -imagemask
  205.     end % $BoundingBox
  206. } def
  207.  
  208. % Just define this one out of existence
  209. /framedevice { pop pop pop pop } def
  210.  
  211. % Handle restoring VM --- this is all OK, except that we have to
  212. % hang onto the bb info we collected while in the about-to-be-discarded
  213. % environment
  214.  
  215. /restore
  216. {
  217.     $BoundingBox begin
  218.     bbox-llx bbox-lly bbox-urx bbox-ury 
  219.     5 -1 roll
  220.     -restore
  221.     /bbox-ury xstore /bbox-urx xstore
  222.     /bbox-lly xstore /bbox-llx xstore
  223.     end % $BoundingBox
  224. } def
  225.  
  226.     
  227. % `showpage':
  228. % Just draw the box around the figure and print the page, and then initialize
  229. % the bounding box variables again.
  230.  
  231. $BoundingBox begin
  232. /temp-string 10 string def
  233. end % $BoundingBox
  234.  
  235. /showpage {
  236.     $BoundingBox begin
  237.     initgraphics
  238.  
  239.         (showpage\n) traceprint % DEBUG
  240.     dump-bbox  % DEBUG
  241.  
  242.         /bbox-llx round_down
  243.     /bbox-lly round_down
  244.     /bbox-ury round_up
  245.     /bbox-urx round_up
  246.  
  247.     bbox-llx bbox-lly moveto        % Make the box
  248.     bbox-llx bbox-ury lineto
  249.     bbox-urx bbox-ury lineto
  250.     bbox-urx bbox-lly lineto
  251.     closepath
  252.  
  253.     bwstroke            % Draw the box.
  254.  
  255. % Print the size of the bounding box both above and below the actual box
  256.     0 setgray
  257.     /Courier findfont 10 scalefont setfont
  258.     100 100 moveto
  259.     bbox-llx bbox-lly 12 sub moveto
  260.     (%%BoundingBox: ) -show
  261.     bbox-llx temp-string cvs -show ( ) -show
  262.     bbox-lly temp-string cvs -show ( ) -show
  263.     bbox-urx temp-string cvs -show ( ) -show
  264.     bbox-ury temp-string cvs -show
  265.  
  266.         bbox-llx bbox-ury 12 add moveto
  267.     (%%BoundingBox: ) -show
  268.     bbox-llx temp-string cvs -show ( ) -show
  269.     bbox-lly temp-string cvs -show ( ) -show
  270.     bbox-urx temp-string cvs -show ( ) -show
  271.     bbox-ury temp-string cvs -show
  272.  
  273.     init
  274.     -showpage
  275.     tracedump        %% DEBUG
  276.     end % $BoundingBox
  277. } def
  278.  
  279. % BoundingBox functions:
  280. % We accumulate the information about the bounding box into four variables.
  281. % The data is stored in default coordinates.
  282.  
  283. $BoundingBox begin
  284.  
  285. /init {
  286.     /bbox-llx 99999 store
  287.     /bbox-lly 99999 store
  288.     /bbox-urx -99999 store
  289.     /bbox-ury -99999 store
  290. } def
  291.  
  292. /bbox-llx 0 def
  293. /bbox-lly 0 def
  294. /bbox-urx 0 def
  295. /bbox-ury 0 def
  296.  
  297. % - `includepath' -
  298. % Incorporates the bounding box of the path into the bounding box info.
  299. %   ... Gets the bounding box in default coords
  300.  
  301. /includepath {
  302.         (Adding a path: ) traceprint %%DEBUG
  303.     gsave
  304.         initmatrix
  305.     {
  306.         0 setlinejoin
  307.         flattenpath
  308.     } stopped {
  309.         (Couldn't flatten the path\n) traceprint % DEBUG
  310.         grestore
  311.         gsave
  312.         initmatrix
  313.     } if
  314.     pathbbox
  315.         4 2 roll    % Just so we get lower-left first
  316.     2 copy dump-coord %%DEBUG
  317.     dup bbox-lly lt {    
  318.         /bbox-lly xstore
  319.     } {
  320.         pop
  321.     } ifelse
  322.     dup bbox-llx lt {
  323.         /bbox-llx xstore
  324.     } {
  325.         pop
  326.     } ifelse
  327.  
  328.     (; ) traceprint 2 copy dump-coord (\n) traceprint %%DEBUG
  329.     dup bbox-ury gt {
  330.         /bbox-ury xstore
  331.     } {
  332.         pop
  333.     } ifelse
  334.     dup bbox-urx gt {
  335.         /bbox-urx xstore
  336.     } {
  337.         pop
  338.     } ifelse
  339.     dump-bbox  %%DEBUG
  340.     grestore
  341. } def
  342.  
  343. % A nice black-and white line drawing function.
  344.  
  345. /bwstroke {
  346.     0 setlinewidth            % Thinnest possible lines
  347.     1 setgray            % White first
  348.     [5] 0 setdash            % Only half the line
  349.     gsave -stroke grestore
  350.     0 setgray            % Then black
  351.     [5] 5 setdash            % On the other half
  352.     -stroke
  353. } def
  354.  
  355. % Stuff for text.
  356.  
  357. % char-code string `chcount' occurs
  358. % Counts the number of times a character appears in a string.
  359.  
  360. /chcount {
  361.     0 exch
  362.     {
  363.         2 index eq {
  364.             1 add
  365.         } if
  366.     } forall
  367.     exch pop
  368. } def
  369.  
  370. % - `fontheight' heightx heighty depthx depthy
  371. % Returns the offsets to the lowest point and highest point in the current
  372. % font.
  373.  
  374. /fontheight {
  375.     currentfont begin
  376.     /FontBBox load aload pop
  377.     exch pop 0 exch
  378.     FontMatrix transform
  379.     4 2 roll
  380.     exch pop 0 exch
  381.     FontMatrix transform
  382.     end
  383. } def
  384.  
  385. % key round_{down|up} -  These will round the value of the given key
  386. %                         up or down, as appropriate, to the nearest integer
  387. /round_up   { dup load ceiling cvi store } def
  388. /round_down { dup load floor   cvi store } def
  389.  
  390. % key binddefinition - this will do a 'bind' on the procedure given by 'key'
  391. /binddefinition
  392. {
  393.     dup where
  394.     {  
  395.         exch
  396.         2 copy
  397.         get bind put
  398.     }     
  399.     { undefined } ifelse
  400. } def  
  401.  
  402. % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
  403. %
  404. %   Debugging utilities
  405. %
  406.  
  407. /$tracedict where
  408. {  % Trace package loaded... do the tracing
  409.     pop
  410. % This is a debugging function to print out what is going on.
  411. %  Format <argn> <argn-1> ... <arg1> n <string> debug <argn> ... <arg1>
  412. %    (that is, the 'n' args will be *left* on the stack!)
  413. /debug
  414. {
  415.     traceprint (\n) traceprint 
  416.     dup 1 add   % Now total number of args (including arg count) 
  417.     copy
  418.     {
  419.         (    ) traceprint 
  420.     trace=
  421.         (\n) traceprint
  422.     } repeat
  423.     pop    % Remove the extra copy of the arg count
  424. } def
  425.  
  426. % Print out a coordinate on the stack:  x y --- 
  427. /dump-coord
  428. {
  429.     (\() traceprint exch trace= (, ) traceprint trace= (\)) traceprint
  430. } def
  431.  
  432. % Print out bb's current notion of its bounding box
  433.  
  434. /dump-bbox
  435. {
  436.     (Bounding Box: ) traceprint
  437.     bbox-llx bbox-lly dump-coord
  438.     (; ) traceprint
  439.     bbox-urx bbox-ury dump-coord
  440.     (\n) traceprint
  441. } def
  442.  
  443. tracebegin %% DEBUG
  444.  
  445. }
  446. { % No trace package loaded, so don't trace.  Stub out the various calls
  447.  
  448. /traceprint { pop } def
  449. /dump-coord { pop pop } def
  450. /dump-bbox { } def
  451. /debug { pop  pop } def
  452. /tracedump { } def
  453.  
  454. } ifelse
  455.  
  456. % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % 
  457.  
  458. % Bind everything
  459.  
  460. /xdef binddefinition
  461. /xstore binddefinition
  462. /addcoords binddefinition
  463. /stroke binddefinition
  464. /fill binddefinition
  465. /eofill binddefinition
  466. /show binddefinition
  467. /widthshow binddefinition
  468. /ashow binddefinition
  469. /awidthshow binddefinition
  470. /image binddefinition
  471. /showpage binddefinition
  472. /init binddefinition
  473. /includepath binddefinition
  474. /bwstroke binddefinition
  475. /chcount binddefinition
  476. /fontheight binddefinition
  477.  
  478. /debug binddefinition
  479. /dump-coord binddefinition
  480. /dump-bbox binddefinition
  481.  
  482. % Start it up.
  483.  
  484. init
  485.  
  486. end % $BoundingBox
  487.  
  488. %  end of bb.ps
  489.  
  490. % $Log:    bb.ps,v $
  491. % Revision 1.10  90/06/27  10:47:22  cosell
  492. % Added a bunch of improvements from Joe Pallas at stanford.
  493. % Revision 1.9  90/06/26  10:50:20  cosell
  494. % Stack got botched in the 'debug' stub
  495. % Revision 1.8  90/06/25  09:34:51  cosell
  496. % Minor bug in 'restore'
  497. % Revision 1.7  90/06/25  09:29:58  cosell
  498. % Added code to catch and deal with 'restore'.  Thanks to Frank
  499. % Jensen for finding this one
  500. % Revision 1.6  90/06/25  09:23:26  cosell
  501. % Small bugfix in the text-handling stuff
  502. % Revision 1.5  90/06/10  09:04:02  cosell
  503. % Changed the printed string to explictly say "%%BoundingBox"
  504. % Revision 1.4  90/06/10  08:55:39  cosell
  505. % Added 'bind' machinery to insulate this package from later redefinitions
  506. % of things we need from the systemdict.
  507. % Revision 1.3  90/06/10  08:28:53  cosell
  508. % Added debugging hooks.  They don't affect anything (and don't do
  509. % anything) in the normal use of bbfig.  But if the 'trace' package
  510. % is loaded ahead of this, it'll print out some helpful info.  Probably
  511. % I'll end up removing all of this if/when I really get the package
  512. % up to snuff.
  513. % Revision 1.2  90/05/25  12:08:24  cosell
  514. % Major improvements and tuneups:  fixed it to really use its private
  515. % discionary, and the most importnat: it now computes the bounding box
  516. % in *default* coords
  517. %
  518. % Revision 1.1  90/05/23  08:18:54  cosell
  519. % Initial revision
  520. %   This is Ned Bachelder's original version
  521.