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

  1. %!PS
  2. % @(#) $Header: utils.ps,v 1.1 90/06/11 02:56:01 jef Exp $
  3. %
  4. % utils.ps - general PostScript utilities
  5. %
  6. % Copyright (C) 1990 by Jef Poskanzer.  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.     gsave
  191.     % Read and parse header lines, tossing the ones we don't need.
  192.     {
  193.         % Read line.
  194.         currentfile facesaverbuf readline not { errordict begin syntaxerror } if
  195.         % Exit loop on blank line.
  196.         dup length 0 eq { pop exit } if
  197.         % Parse for lines we care about.
  198.         token not {
  199.         % token found only whitespace - ignore this line.
  200.         } {
  201.         % Stack has <rest> <header>.
  202.         dup (PicData:) eq {
  203.             % Found a PicData line.
  204.             pop
  205.             token not { errordict begin syntaxerror } if
  206.             /PicDataWidth exch def
  207.             token not { errordict begin syntaxerror } if
  208.             /PicDataHeight exch def
  209.             token not { errordict begin syntaxerror } if
  210.             /PicDataBits exch def
  211.             pop
  212.         } {
  213.             dup (Image:) eq {
  214.             % Found an Image line.
  215.             pop
  216.             token not { errordict begin syntaxerror } if
  217.             /ImageWidth exch def
  218.             token not { errordict begin syntaxerror } if
  219.             /ImageHeight exch def
  220.             token not { errordict begin syntaxerror } if
  221.             /ImageBits exch def
  222.             pop
  223.             } {
  224.             % Line is nothing we're interested in - define a string.
  225.             cvlit exch dup length string copy def
  226.             } ifelse
  227.         } ifelse
  228.         } ifelse
  229.     } loop
  230.     grestore
  231.     PicDataBits ImageBits ne { errordict begin syntaxerror } if
  232.     gsave
  233.     % Read and display the hex image data.
  234.     0 1 translate
  235.     1 -1 scale
  236.     PicDataWidth PicDataHeight PicDataBits
  237.     [ PicDataWidth 0 0 PicDataHeight neg 0 PicDataHeight ]
  238.     { currentfile facesaverbuf readhexstring pop }
  239.     image
  240.     grestore
  241. } def
  242.  
  243.  
  244. /photowidth 0.9 in def
  245. /photoheight  photowidth 4 mul 3 div  def    % 4/3:1 is the FaceSaver ratio
  246.  
  247. /photo {
  248.     /thisphotoheight exch def
  249.     /thisphotowidth exch def
  250.     gsave
  251.     currentpoint translate
  252.     thisphotowidth thisphotoheight scale
  253.     facesaverimage
  254.     grestore
  255.     thisphotowidth  0  rlineto
  256.     0  thisphotoheight  rlineto
  257.     thisphotowidth neg  0  rlineto
  258.     0  thisphotoheight neg  rlineto
  259.     stroke
  260. } def
  261.  
  262. % Draw a cute diagonal box at currentpoint with specified width and height,
  263. % with specified title and contents.
  264. /diagoffsetfactor .375 def    % times height
  265. /diagfat 0.02 in def        % width of fat lines
  266. /diagbox {
  267.     /diagcontents exch def
  268.     /diagtitle exch def
  269.     /diagheight exch def
  270.     /diagwidth exch def
  271.     /diagoffset diagoffsetfactor diagheight mul def
  272.     gsave
  273.         currentpoint translate
  274.  
  275.     % Do box.
  276.     0 setlinewidth
  277.     newpath
  278.     0  0  moveto
  279.     diagwidth diagoffset sub  0  lineto
  280.     diagwidth  diagheight  lineto
  281.     diagoffset  diagheight  lineto
  282.     stroke
  283.  
  284.     % Do fat end.
  285.     0  0  moveto
  286.     diagfat  0  lineto
  287.     diagoffset diagfat add  diagheight  lineto
  288.     diagoffset  diagheight  lineto
  289.     0  0  lineto
  290.     fill
  291.  
  292.     % Do title.
  293.     diagoffset diagheight 0.15 mul add  diagheight 0.8 mul  moveto
  294.     /Helvetica findfont  diagheight 0.2 mul  scalefont setfont
  295.     diagtitle show
  296.  
  297.     % Do contents.
  298.     diagwidth 2 div  diagheight 0.3 mul  moveto
  299.     /Helvetica findfont  diagheight 0.5 mul  scalefont setfont
  300.     diagwidth diagoffset 2 mul sub diagcontents showcenterlimited
  301.  
  302.     grestore
  303. } def
  304.  
  305. % Canned ellipse routine, modified for rotation.
  306. /ellipserdict 9 dict def
  307. ellipserdict /mtrx matrix put
  308. /ellipser { ellipserdict begin
  309.     /endangle exch def
  310.     /startangle exch def
  311.     /yrad exch def
  312.     /xrad exch def
  313.     /rot exch def
  314.     /y exch def
  315.     /x exch def
  316.     /savematrix mtrx currentmatrix def
  317.     x y translate
  318.     rot rotate
  319.     xrad yrad scale
  320.     0 0 1 startangle endangle arc
  321.     savematrix setmatrix
  322. end } def
  323.  
  324.