home *** CD-ROM | disk | FTP | other *** search
- %!PS
- % @(#) $Header: utils.ps,v 1.1 90/06/11 02:56:01 jef Exp $
- %
- % utils.ps - general PostScript utilities
- %
- % Copyright (C) 1990 by Jef Poskanzer. All rights reserved.
- %
- % This file defines:
- % in - turn inches to standard PostScript units (points)
- % black, white - for use with setgray
- % strcat - concatenate two strings
- % randrange - 2 ^ 31, range of the rand operator
- % randuni - return a random float in [ 0.0 .. 1.0 )
- % randchar - return a random character from a string
- % randdig - return a random digit as a string
- % randdigs - return N random digits as a string
- % square - draw a square from currentpoint to w, h
- % filledsquare - fill a square from currentpoint to w, h
- % pagewidth, pageheight - width and height of page
- % pagemargin - size of a comfortable margin around the edges
- % showcenter - show a text string centered on currentpoint
- % showright - show a text string right-justified to currentpoint
- % showboxed - show a text string with exactly a specified width and height
- % showlimited - show a text string with at most a specified width
- % showcenterlimited - showlimited that centers the string
- % showrightlimited - showlimited that right-justifies the string
- % facesaverimage - read an appended FaceSaver file and display it
- % photowidth, photoheight - default sizes for photo
- % photo - draw a face saver photo and a box at currentpoint with specified w&h
- % diagbox - draw a diagonal box at currentpoint with specified width and
- % height, and with specified title and contents
- % ellipser - draw a rotated ellipse
-
-
- % Use manual feed, if available.
- %{
- % statusdict begin
- % /manualfeed true def
- % end
- %} stopped pop
-
- % Check whether we are running NeWS or not.
- /currentprocess where /NeWS exch def
- % If NeWS, rotate by epsilon to ensure accurate font scaling.
- NeWS { 0.001 rotate } if
-
-
- /in { 72.27 mul } def
- /black 0 def
- /white 1 def
-
- % Concatenate two strings.
- /strcat { % str1 str2 -- str
- 2 copy % str1 str2 str1 str2
- length exch length % str1 str2 len2 len1
- dup 3 -1 roll add % str1 str2 len1 len
- string % str1 str2 len1 str
- dup 0 6 -1 roll putinterval % str2 len1 str
- dup 3 -1 roll 4 -1 roll putinterval % str
- } def
-
- /randrange 2147483648 def
-
- /randuni {
- rand randrange div
- } def
-
- /randchar { % str -- char
- dup length randuni mul cvi 1 getinterval
- } def
-
- /randdig { % -- digit
- (0123456789) randchar
- } def
-
- /randdigs { % N -- Ndigits
- () exch
- 1 exch 1 exch {
- pop
- randdig strcat
- } for
- } def
-
- /squarepath { % w h --
- 0 1 index rlineto % w h
- 1 index 0 rlineto % w h
- 0 exch neg rlineto % w
- neg 0 rlineto %
- } def
-
- /square {
- squarepath
- stroke
- } def
-
- /filledsquare {
- squarepath
- fill
- } def
-
- % Find size of page and define pageheight and pagewidth.
- newpath clippath pathbbox newpath % llx lly urx ury
- 3 -1 roll % llx urx ury lly
- sub % llx urx height
- /pageheight exch def % llx urx
- exch sub % width
- /pagewidth exch def
-
- /pagemargin 0.5 in def
-
-
- % Some text display utils.
-
- /showcenter {
- dup stringwidth pop % get width of string
- 2 div % divide by two
- neg % make negative
- 0 rmoveto % back up
- show
- } def
-
- /showright {
- dup stringwidth pop % get width of string
- neg % make negative
- 0 rmoveto % back up
- show
- } def
-
- /showboxed { % deswid deshgt str
- gsave
- currentpoint translate
- newpath 0 0 moveto dup false charpath flattenpath
- pathbbox % deswid deshgt str llx lly urx ury
- 3 -1 roll % deswid deshgt str llx urx ury lly
- sub % deswid deshgt str llx urx strhgt
- 3 1 roll exch % deswid deshgt str strhgt urx llx
- sub exch % deswid deshgt str actwid acthgt
- 5 -2 roll % str actwid acthgt deswid deshgt
- 3 -1 roll % str actwid deswid desght acthgt
- div % str actwid deswid yscale
- 3 1 roll exch % str yscale deswid actwid
- div exch % str xscale yscale
- scale % str
- 0 0 moveto show
- grestore
- } def
-
- /showlimited { % maxwid str --
- gsave
- dup stringwidth pop dup % maxwid str wid wid
- 3 index gt { % maxwid str wid
- 2 index exch div 1 scale
- } {
- pop
- } ifelse
- show pop
- grestore
- } def
-
- /showcenterlimited { % maxwid str --
- gsave
- dup stringwidth pop dup % maxwid str wid wid
- 3 index gt { % maxwid str wid
- 2 index exch div 1 scale
- } {
- pop
- } ifelse
- showcenter pop
- grestore
- } def
-
- /showrightlimited { % maxwid str --
- gsave
- dup stringwidth pop dup % maxwid str wid wid
- 3 index gt { % maxwid str wid
- 2 index exch div 1 scale
- } {
- pop
- } ifelse
- showright pop
- grestore
- } def
-
- % Read an appended FaceSaver file and display it in a 1 x 1 box at (0,0).
- % Defines strings for the header lines it finds.
-
- /facesaverbuf 256 string def
-
- /facesaverimage {
- gsave
- % Read and parse header lines, tossing the ones we don't need.
- {
- % Read line.
- currentfile facesaverbuf readline not { errordict begin syntaxerror } if
- % Exit loop on blank line.
- dup length 0 eq { pop exit } if
- % Parse for lines we care about.
- token not {
- % token found only whitespace - ignore this line.
- } {
- % Stack has <rest> <header>.
- dup (PicData:) eq {
- % Found a PicData line.
- pop
- token not { errordict begin syntaxerror } if
- /PicDataWidth exch def
- token not { errordict begin syntaxerror } if
- /PicDataHeight exch def
- token not { errordict begin syntaxerror } if
- /PicDataBits exch def
- pop
- } {
- dup (Image:) eq {
- % Found an Image line.
- pop
- token not { errordict begin syntaxerror } if
- /ImageWidth exch def
- token not { errordict begin syntaxerror } if
- /ImageHeight exch def
- token not { errordict begin syntaxerror } if
- /ImageBits exch def
- pop
- } {
- % Line is nothing we're interested in - define a string.
- cvlit exch dup length string copy def
- } ifelse
- } ifelse
- } ifelse
- } loop
- grestore
- PicDataBits ImageBits ne { errordict begin syntaxerror } if
- gsave
- % Read and display the hex image data.
- 0 1 translate
- 1 -1 scale
- PicDataWidth PicDataHeight PicDataBits
- [ PicDataWidth 0 0 PicDataHeight neg 0 PicDataHeight ]
- { currentfile facesaverbuf readhexstring pop }
- image
- grestore
- } def
-
-
- /photowidth 0.9 in def
- /photoheight photowidth 4 mul 3 div def % 4/3:1 is the FaceSaver ratio
-
- /photo {
- /thisphotoheight exch def
- /thisphotowidth exch def
- gsave
- currentpoint translate
- thisphotowidth thisphotoheight scale
- facesaverimage
- grestore
- thisphotowidth 0 rlineto
- 0 thisphotoheight rlineto
- thisphotowidth neg 0 rlineto
- 0 thisphotoheight neg rlineto
- stroke
- } def
-
- % Draw a cute diagonal box at currentpoint with specified width and height,
- % with specified title and contents.
- /diagoffsetfactor .375 def % times height
- /diagfat 0.02 in def % width of fat lines
- /diagbox {
- /diagcontents exch def
- /diagtitle exch def
- /diagheight exch def
- /diagwidth exch def
- /diagoffset diagoffsetfactor diagheight mul def
- gsave
- currentpoint translate
-
- % Do box.
- 0 setlinewidth
- newpath
- 0 0 moveto
- diagwidth diagoffset sub 0 lineto
- diagwidth diagheight lineto
- diagoffset diagheight lineto
- stroke
-
- % Do fat end.
- 0 0 moveto
- diagfat 0 lineto
- diagoffset diagfat add diagheight lineto
- diagoffset diagheight lineto
- 0 0 lineto
- fill
-
- % Do title.
- diagoffset diagheight 0.15 mul add diagheight 0.8 mul moveto
- /Helvetica findfont diagheight 0.2 mul scalefont setfont
- diagtitle show
-
- % Do contents.
- diagwidth 2 div diagheight 0.3 mul moveto
- /Helvetica findfont diagheight 0.5 mul scalefont setfont
- diagwidth diagoffset 2 mul sub diagcontents showcenterlimited
-
- grestore
- } def
-
- % Canned ellipse routine, modified for rotation.
- /ellipserdict 9 dict def
- ellipserdict /mtrx matrix put
- /ellipser { ellipserdict begin
- /endangle exch def
- /startangle exch def
- /yrad exch def
- /xrad exch def
- /rot exch def
- /y exch def
- /x exch def
- /savematrix mtrx currentmatrix def
- x y translate
- rot rotate
- xrad yrad scale
- 0 0 1 startangle endangle arc
- savematrix setmatrix
- end } def
-
-