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