home *** CD-ROM | disk | FTP | other *** search
/ Source Code 1992 March / Source_Code_CD-ROM_Walnut_Creek_March_1992.iso / usenet / altsrcs / 1 / 1450 / utils.ps < prev   
Encoding:
Text File  |  1990-12-28  |  8.3 KB  |  335 lines

  1. %!PS
  2. % @(#) $Header: utils.ps,v 1.2 90/06/11 15:43:39 jef Exp $
  3. %
  4. % utils.ps - general PostScript utilities
  5. %
  6. % Copyright (C) 1990 by Jef Poskanzer and Craig Leres.  All rights reserved.
  7. %
  8. % This file defines:
  9. %   in - turn inches to standard PostScript units (points)
  10. %   black, white - for use with setgray
  11. %   strcat - concatenate two strings
  12. %   randrange - 2 ^ 31, range of the rand operator
  13. %   randuni - return a random float in [ 0.0 .. 1.0 )
  14. %   randchar - return a random character from a string
  15. %   randdig - return a random digit as a string
  16. %   randdigs - return N random digits as a string
  17. %   square - draw a square from currentpoint to w, h
  18. %   filledsquare - fill a square from currentpoint to w, h
  19. %   pagewidth, pageheight - width and height of page
  20. %   pagemargin - size of a comfortable margin around the edges
  21. %   showcenter - show a text string centered on currentpoint
  22. %   showright - show a text string right-justified to currentpoint
  23. %   showboxed - show a text string with exactly a specified width and height
  24. %   showlimited - show a text string with at most a specified width
  25. %   showcenterlimited - showlimited that centers the string
  26. %   showrightlimited - showlimited that right-justifies the string
  27. %   facesaverimage - read an appended FaceSaver file and display it
  28. %   photowidth, photoheight - default sizes for photo
  29. %   photo - draw a face saver photo and a box at currentpoint with specified w&h
  30. %   diagbox - draw a diagonal box at currentpoint with specified width and
  31. %     height, and with specified title and contents
  32. %   ellipser - draw a rotated ellipse
  33.  
  34.  
  35. % Use manual feed, if available.
  36. %{
  37. %    statusdict begin
  38. %       /manualfeed true def
  39. %    end
  40. %} stopped pop
  41.  
  42. % Check whether we are running NeWS or not.
  43. /currentprocess where /NeWS exch def
  44. % If NeWS, rotate by epsilon to ensure accurate font scaling.
  45. NeWS { 0.001 rotate } if
  46.  
  47.  
  48. /in  { 72.27 mul }  def
  49. /black  0  def
  50. /white  1  def
  51.  
  52. % Concatenate two strings.
  53. /strcat {                % str1 str2 -- str
  54.     2 copy                % str1 str2 str1 str2
  55.     length exch length            % str1 str2 len2 len1
  56.     dup 3 -1 roll add            % str1 str2 len1 len
  57.     string                % str1 str2 len1 str
  58.     dup 0 6 -1 roll putinterval        % str2 len1 str
  59.     dup 3 -1 roll 4 -1 roll putinterval    % str
  60. } def
  61.  
  62. /randrange 2147483648 def
  63.  
  64. /randuni {
  65.     rand randrange div
  66. } def
  67.  
  68. /randchar {        % str -- char
  69.     dup length randuni mul cvi 1 getinterval
  70. } def
  71.  
  72. /randdig {        % -- digit
  73.     (0123456789) randchar
  74. } def
  75.  
  76. /randdigs {        % N -- Ndigits
  77.     () exch
  78.     1 exch 1 exch {
  79.     pop
  80.     randdig strcat
  81.     } for
  82. } def
  83.  
  84. /squarepath {            % w h --
  85.     0  1 index  rlineto        % w h
  86.     1 index  0  rlineto        % w h
  87.     0  exch neg  rlineto    % w
  88.     neg  0  rlineto        %
  89. } def
  90.  
  91. /square {
  92.     squarepath
  93.     stroke
  94. } def
  95.  
  96. /filledsquare {
  97.     squarepath
  98.     fill
  99. } def
  100.  
  101. % Find size of page and define pageheight and pagewidth.
  102. newpath clippath pathbbox newpath    % llx lly urx ury
  103. 3 -1 roll                % llx urx ury lly
  104. sub                    % llx urx height
  105. /pageheight exch def            % llx urx
  106. exch sub                % width
  107. /pagewidth exch def
  108.  
  109. /pagemargin  0.5 in  def
  110.  
  111.  
  112. % Some text display utils.
  113.  
  114. /showcenter {
  115.     dup stringwidth pop        % get width of string
  116.     2 div            % divide by two
  117.     neg                % make negative
  118.     0 rmoveto            % back up
  119.     show
  120. } def
  121.  
  122. /showright {
  123.     dup stringwidth pop        % get width of string
  124.     neg                % make negative
  125.     0 rmoveto            % back up
  126.     show
  127. } def
  128.  
  129. /showboxed {            % deswid deshgt str
  130.     gsave
  131.     currentpoint translate
  132.     newpath  0 0 moveto  dup false charpath  flattenpath
  133.     pathbbox        % deswid deshgt str llx lly urx ury
  134.     3 -1 roll        % deswid deshgt str llx urx ury lly
  135.     sub            % deswid deshgt str llx urx strhgt
  136.     3 1 roll  exch        % deswid deshgt str strhgt urx llx
  137.     sub  exch        % deswid deshgt str actwid acthgt
  138.     5 -2 roll        % str actwid acthgt deswid deshgt
  139.     3 -1 roll        % str actwid deswid desght acthgt
  140.     div            % str actwid deswid yscale
  141.     3 1 roll  exch        % str yscale deswid actwid
  142.     div exch        % str xscale yscale
  143.     scale            % str
  144.     0 0 moveto  show
  145.     grestore
  146. } def
  147.  
  148. /showlimited {            % maxwid str --
  149.     gsave
  150.     dup stringwidth pop dup    % maxwid str wid wid
  151.     3 index gt {        % maxwid str wid
  152.         2 index exch div 1 scale
  153.     } {
  154.         pop
  155.     } ifelse
  156.     show pop
  157.     grestore
  158. } def
  159.  
  160. /showcenterlimited {            % maxwid str --
  161.     gsave
  162.     dup stringwidth pop dup    % maxwid str wid wid
  163.     3 index gt {        % maxwid str wid
  164.         2 index exch div 1 scale
  165.     } {
  166.         pop
  167.     } ifelse
  168.     showcenter pop
  169.     grestore
  170. } def
  171.  
  172. /showrightlimited {            % maxwid str --
  173.     gsave
  174.     dup stringwidth pop dup    % maxwid str wid wid
  175.     3 index gt {        % maxwid str wid
  176.         2 index exch div 1 scale
  177.     } {
  178.         pop
  179.     } ifelse
  180.     showright pop
  181.     grestore
  182. } def
  183.  
  184. % Read an appended FaceSaver file and display it in a 1 x 1 box at (0,0).
  185. % Defines strings for the header lines it finds.
  186.  
  187. /facesaverbuf 256 string def
  188.  
  189. /facesaverimage {
  190.     % Pre-define the standard FaceSaver header strings, in case an older
  191.     % FaceSaver file is used.
  192.     /FirstName: () def
  193.     /LastName: () def
  194.     /E-mail: () def
  195.     /Telephone: () def
  196.     /Company: () def
  197.     /Address1: () def
  198.     /Address2: () def
  199.     /CityStateZip: () def
  200.     /Date: () def
  201.     gsave
  202.     % Read and parse header lines, tossing the ones we don't need.
  203.     {
  204.         % Read line.
  205.         currentfile facesaverbuf readline not { errordict begin syntaxerror } if
  206.         % Exit loop on blank line.
  207.         dup length 0 eq { pop exit } if
  208.         % Parse for lines we care about.
  209.         token not {
  210.         % token found only whitespace - ignore this line.
  211.         } {
  212.         % Stack has <rest> <header>.
  213.         dup (PicData:) eq {
  214.             % Found a PicData line.
  215.             pop
  216.             token not { errordict begin syntaxerror } if
  217.             /PicDataWidth exch def
  218.             token not { errordict begin syntaxerror } if
  219.             /PicDataHeight exch def
  220.             token not { errordict begin syntaxerror } if
  221.             /PicDataBits exch def
  222.             pop
  223.         } {
  224.             dup (Image:) eq {
  225.             % Found an Image line.
  226.             pop
  227.             token not { errordict begin syntaxerror } if
  228.             /ImageWidth exch def
  229.             token not { errordict begin syntaxerror } if
  230.             /ImageHeight exch def
  231.             token not { errordict begin syntaxerror } if
  232.             /ImageBits exch def
  233.             pop
  234.             } {
  235.             % Line is nothing we're interested in - define a string.
  236.             cvlit exch dup length string copy def
  237.             } ifelse
  238.         } ifelse
  239.         } ifelse
  240.     } loop
  241.     grestore
  242.     PicDataBits ImageBits ne { errordict begin syntaxerror } if
  243.     gsave
  244.     % Read and display the hex image data.
  245.     0 1 translate
  246.     1 -1 scale
  247.     PicDataWidth PicDataHeight PicDataBits
  248.     [ PicDataWidth 0 0 PicDataHeight neg 0 PicDataHeight ]
  249.     { currentfile facesaverbuf readhexstring pop }
  250.     image
  251.     grestore
  252. } def
  253.  
  254.  
  255. /photowidth 0.9 in def
  256. /photoheight  photowidth 4 mul 3 div  def    % 4/3:1 is the FaceSaver ratio
  257.  
  258. /photo {
  259.     /thisphotoheight exch def
  260.     /thisphotowidth exch def
  261.     gsave
  262.     currentpoint translate
  263.     thisphotowidth thisphotoheight scale
  264.     facesaverimage
  265.     grestore
  266.     thisphotowidth  0  rlineto
  267.     0  thisphotoheight  rlineto
  268.     thisphotowidth neg  0  rlineto
  269.     0  thisphotoheight neg  rlineto
  270.     stroke
  271. } def
  272.  
  273. % Draw a cute diagonal box at currentpoint with specified width and height,
  274. % with specified title and contents.
  275. /diagoffsetfactor .375 def    % times height
  276. /diagfat 0.02 in def        % width of fat lines
  277. /diagbox {
  278.     /diagcontents exch def
  279.     /diagtitle exch def
  280.     /diagheight exch def
  281.     /diagwidth exch def
  282.     /diagoffset diagoffsetfactor diagheight mul def
  283.     gsave
  284.         currentpoint translate
  285.  
  286.     % Do box.
  287.     0 setlinewidth
  288.     newpath
  289.     0  0  moveto
  290.     diagwidth diagoffset sub  0  lineto
  291.     diagwidth  diagheight  lineto
  292.     diagoffset  diagheight  lineto
  293.     stroke
  294.  
  295.     % Do fat end.
  296.     0  0  moveto
  297.     diagfat  0  lineto
  298.     diagoffset diagfat add  diagheight  lineto
  299.     diagoffset  diagheight  lineto
  300.     0  0  lineto
  301.     fill
  302.  
  303.     % Do title.
  304.     diagoffset diagheight 0.15 mul add  diagheight 0.8 mul  moveto
  305.     /Helvetica findfont  diagheight 0.2 mul  scalefont setfont
  306.     diagtitle show
  307.  
  308.     % Do contents.
  309.     diagwidth 2 div  diagheight 0.3 mul  moveto
  310.     /Helvetica findfont  diagheight 0.5 mul  scalefont setfont
  311.     diagwidth diagoffset 2 mul sub diagcontents showcenterlimited
  312.  
  313.     grestore
  314. } def
  315.  
  316. % Canned ellipse routine, modified for rotation.
  317. /ellipserdict 9 dict def
  318. ellipserdict /mtrx matrix put
  319. /ellipser { ellipserdict begin
  320.     /endangle exch def
  321.     /startangle exch def
  322.     /yrad exch def
  323.     /xrad exch def
  324.     /rot exch def
  325.     /y exch def
  326.     /x exch def
  327.     /savematrix mtrx currentmatrix def
  328.     x y translate
  329.     rot rotate
  330.     xrad yrad scale
  331.     0 0 1 startangle endangle arc
  332.     savematrix setmatrix
  333. end } def
  334.  
  335.