home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 2: PC / frozenfish_august_1995.bin / bbs / d09xx / d0975.lha / PCal / pcalinit.ps < prev    next >
Text File  |  1992-02-19  |  14KB  |  602 lines

  1. % pcalinit.ps - provides the PostScript routines for pcal.c
  2. %
  3. % 4.3    modified by Andrew Rogers:
  4. %
  5. %    removed definitions of dategray and fillgray; handled instead by
  6. %    Pcal itself (may be selected by user via -s flag)
  7. %
  8. %    enlarge dates in small and medium calendars; enlarge title and
  9. %    weekday names in medium calendars
  10. %
  11. % 4.2    modified by Andrew Rogers:
  12. %
  13. %    support -[kK] options to reposition small calendars
  14. %
  15. %    use same font size for "Notes" as for weekday names; delete heading
  16. %    from notes box if null
  17. %
  18. %    move definition of notesfontsize to Pcal proper; tweak some of the
  19. %    routines to take variable font size into account
  20. %
  21. %    add support for printing notes in any blank box on calendar and
  22. %    suppressing small calendars
  23. %
  24. % 4.1    modified by Andrew Rogers:
  25. %
  26. %    support -G option (cf. prtday) to print "gray" dates as filled
  27. %    outlines
  28. %
  29. % 4.0    modified by Andrew Rogers:
  30. %
  31. %    support -w ("whole year") option - cf. printmonth_[pl], startpage
  32. %    moved all the calendar calculations to pcal.c and moonphas.c (q.v.)
  33. %
  34. %    support -B option (leave unused boxes blank)
  35. %
  36. %    support -O option (print "gray" numbers as outlines)
  37. %
  38. %    revised several of the basic routines and added some others; dates,
  39. %    moons, text, Julian days are now relative to upper-left corner of box
  40. %
  41. %    enlarged title and dates in small calendars
  42. %
  43. % 3.0    modified by Andrew Rogers:
  44. %
  45. %    added xsval, ysval, xtval, ytval for scaling and translation
  46. %    as per Ed Hand
  47. %
  48. %    added day-of-year support (-j, -J flags)
  49. %
  50. % 2.6    * no modifications *
  51. %
  52. % 2.5    modified by Joe Brownlee:
  53. %
  54. %    made day numbers smaller, providing more room for event text
  55. %    repositioned event text and moons accordingly
  56. %    added support for variable first day of week
  57. %
  58. % 2.4    * no modifications *
  59. %
  60. % 2.3    modified by Jamie Zawinski <jwz@lucid.com>:
  61. %
  62. %    merged in moon routines (originally by Mark Hanson)
  63. %
  64. % 2.2    modified by Joe Brownlee:
  65. %
  66. %    add "notetext" to print notes in unused calendar box
  67. %
  68. % 2.1    modified by Mark Kantrowitz:
  69. %
  70. %    use symbolic names instead of magic numbers throughout
  71. %    support -L, -C, -R, -n options (all new)
  72. %    print holiday text in otherwise-wasted space next to date
  73. %    use larger text for dates in large calendars
  74. %
  75. % 2.0    modified by Andrew W. Rogers:
  76. %
  77. %    skip printing days of week on small calendars
  78. %    center month and year at top of calendar
  79. %    use correct algorithm for leap year calculation
  80. %    get month and day names from main program
  81. %    use table to determine color (black/gray) of weekdays and holidays
  82. %    use hanging indent to print continued text lines
  83.  
  84. /SM 0 def                % definitions for calendar sizes
  85. /MED 1 def
  86. /LG 2 def
  87.  
  88. /titlefontsize   [ 60 52 48 ] def    % font sizes for SM/MED/LG calendars
  89. /datefontsize    [ 60 48 25 ] def
  90. /weekdayfontsize [  0 28 12 ] def
  91. /footfontsize 12 def
  92.  
  93. /titlepos [ 19 43 25 ] def        % Y-offset (SM/MED/LG) of month/year title
  94. /Y0 35 def                % Y-coordinate of calendar grid origin
  95.  
  96. /daywidth 100 def            % dimensions of grid boxes
  97. /dayheight 80 def
  98. /gridwidth daywidth 7 mul def
  99. /gridheight dayheight 6 mul def
  100. /negdaywidth daywidth neg def
  101. /negdayheight dayheight neg def
  102. /neggridwidth gridwidth neg def
  103. /neggridheight gridheight neg def
  104.  
  105. /gridlinewidth 1.0 def            % width of grid lines
  106. /charlinewidth 0.1 def            % width of outline characters
  107. /moonlinewidth 0.1 def            % width of moon icon line
  108.  
  109. /hangingindent (   ) def        % for indenting continued text lines
  110.  
  111. %
  112. % Utility functions:
  113. %
  114.  
  115.  
  116. /center {        % print string centered in given width
  117.     /width exch def
  118.     /str exch def
  119.     width str stringwidth pop sub 2 div 0 rmoveto str show
  120. } def
  121.  
  122.  
  123. /strcat {        % concatenate two strings
  124.     2 copy
  125.     length exch length
  126.     dup 3 -1 roll add
  127.     string
  128.     dup 0 6 -1 roll putinterval
  129.     dup 3 -1 roll 4 -1 roll putinterval
  130. } def
  131.  
  132.  
  133. /prtday {        % print date in black, gray, outline, or filled outline
  134.     day 3 string cvs                % convert to string
  135.     color (black) eq {
  136.         show
  137.     } if
  138.     color (gray) eq {
  139.         dategray setgray show
  140.     } if
  141.     color (outline) eq {
  142.         true charpath stroke
  143.     } if
  144.     color (outline_gray) eq {
  145.         true charpath
  146.         gsave dategray setgray fill grestore
  147.         stroke
  148.     } if
  149. } def
  150.  
  151.  
  152. /nextbox {        % go to next column or start of next row
  153.     day startbox add 7 mod 0 eq                % end of week?
  154.         { neggridwidth daywidth add negdayheight rmoveto }  % next row
  155.         { daywidth 0 rmoveto }                    % next col
  156.     ifelse
  157. } def
  158.  
  159.  
  160. /boxpos {        % push coords of upper-left corner of box <arg> (0..41)
  161.     dup 7 mod daywidth mul                    % x-coord
  162.     exch 7 idiv negdayheight mul Y0 add            % y-coord
  163. } def
  164.  
  165.  
  166. /datepos {        % push coords of upper-left corner of box for day <arg>
  167.     startbox add 1 sub dup 7 mod daywidth mul        % x-coord
  168.     exch 7 idiv negdayheight mul Y0 add            % y-coord
  169. } def
  170.     
  171.  
  172. %
  173. % Functions for drawing parts of calendar:
  174. %
  175.  
  176.  
  177. /drawtitle {        % draw month and year title
  178.     titlefont findfont titlefontsize calsize get scalefont setfont
  179.     /month_name month_names month 1 sub get def
  180.     /yearstring year 10 string cvs def
  181.     0 Y0 titlepos calsize get add moveto
  182.     month_name (  ) strcat yearstring strcat gridwidth center
  183. } def
  184.  
  185.  
  186. /drawdaynames {        % draw day names above respective columns
  187.     dayfont findfont weekdayfontsize calsize get scalefont setfont
  188.     0 1 6 {
  189.         /i exch def
  190.         i daywidth mul Y0 5 add moveto
  191.         day_names i get
  192.         daywidth center
  193.     } for
  194. } def
  195.  
  196.  
  197. /drawgrid {        % draw the grid for the calendar
  198.     gridlinewidth setlinewidth
  199.     0 daywidth gridwidth {            % vertical lines
  200.         Y0 moveto
  201.         0 neggridheight rlineto
  202.         stroke
  203.     } for
  204.     0 negdayheight neggridheight {        % horizontal lines
  205.         0 exch Y0 add moveto
  206.         gridwidth 0 rlineto
  207.         stroke
  208.     } for
  209. } def
  210.  
  211.  
  212. /drawnums {        % place day numbers on calendar
  213.     dayfont findfont datefontsize calsize get scalefont setfont
  214.     /fontdiff datefontsize calsize get datefontsize LG get sub def
  215.     charlinewidth setlinewidth
  216.     1 datepos 20 fontdiff add sub exch 5 add exch moveto
  217.  
  218.     1 1 ndays {
  219.         /day exch def
  220.         /color (black) def
  221.         calsize SM ne {        % select alternate color
  222.             /gray day_gray day startbox add 1 sub 7 mod get def
  223.             holidays { day eq { /gray holiday_gray def } if } forall
  224.             gray {
  225.                 /color logical_gray def
  226.             } if
  227.         } if
  228.         gsave
  229.         prtday
  230.         grestore
  231.         nextbox
  232.     } for
  233. } def
  234.  
  235.  
  236. /drawjnums {        % place day-of-year numbers on calendar
  237.     notesfont findfont notesfontsize scalefont setfont
  238.     1 datepos dayheight 3 sub sub exch daywidth 3 sub add exch moveto
  239.  
  240.     1 1 ndays {
  241.         /day exch def
  242.         /jday jdstart day add 1 sub def
  243.         /str jday 3 string cvs def
  244.         julian-dates true eq {        % print days left in year?
  245.             /str str ( \050) strcat yearlen jday sub 3 string cvs
  246.                 strcat (\051) strcat def
  247.         } if
  248.         gsave
  249.         str dup stringwidth pop 0 exch sub 0 rmoveto show
  250.         grestore
  251.         nextbox
  252.     } for
  253. } def
  254.  
  255.  
  256. /fillboxes {        % used by drawfill to generate group of fill squares
  257.     /last exch def        % last box to fill (41 = lower right box)
  258.     /first exch def        % first box to fill (0 = upper left box)
  259.  
  260.     first 1 last {        % loop through range of boxes
  261.         /box exch def
  262.         /fillit true def
  263.         calsize LG eq {        % skip note and small calendar boxes
  264.             noteboxes { box eq { /fillit false def } if } forall
  265.             box prev_small_cal eq box next_small_cal eq or {
  266.                 /fillit false def
  267.             } if
  268.         } if
  269.         fillit {        % move to position and fill the box
  270.             box boxpos moveto
  271.             gsave
  272.             fillgray setgray
  273.             daywidth 0 rlineto
  274.             0 negdayheight rlineto
  275.             negdaywidth 0 rlineto
  276.             closepath fill
  277.             grestore
  278.         } if
  279.     } for
  280. } def
  281.  
  282.  
  283. /drawfill {        % generate fill squares where necessary
  284.     0 startbox 1 sub fillboxes        % fill boxes before calendar
  285.     startbox ndays add 41 fillboxes        % fill boxes after calendar
  286. } def
  287.  
  288.  
  289. /footstrings {        % print foot strings at bottom of page
  290.     titlefont findfont footfontsize scalefont setfont
  291.     /bottomrow { neggridheight 20 add } def
  292.     0 bottomrow moveto
  293.     Lfootstring show
  294.     gridwidth Rfootstring stringwidth pop sub bottomrow moveto
  295.     Rfootstring show
  296.     0 bottomrow moveto
  297.     Cfootstring gridwidth center
  298. } def
  299.  
  300.  
  301. %
  302. % Functions for printing text inside boxes:
  303. %
  304.  
  305.  
  306. /daytext {
  307.     /mytext exch def /day exch def
  308.     notesfont findfont notesfontsize scalefont setfont
  309.     day datepos datefontsize LG get notesfontsize add 2 sub sub dup
  310.     /ypos exch def exch 2 add exch moveto
  311.     currentpoint pop /LM exch def /RM LM 95 add def
  312.     showtext
  313. } def
  314.     
  315.  
  316. /holidaytext {        % print text for holiday (next to date)
  317.     notesfont findfont notesfontsize scalefont setfont
  318.     /mytext exch def /day exch def
  319.     /dwidth day 10 lt {20} {33} ifelse def        % width of date
  320.     /mwidth do-moon-p {16} {0} ifelse def        % width of moon icon
  321.     day datepos notesfontsize 2 add sub dup
  322.     /ypos exch def exch dwidth add exch moveto
  323.     currentpoint pop /LM exch def /RM LM 97 dwidth mwidth add sub add def
  324.     showtext
  325. } def
  326.  
  327.  
  328. /notetext {        % print text in notes box (0..41)
  329.     /mytext exch def /box exch def
  330.     notesheading () eq {            % skip heading if null
  331.         box boxpos notesfontsize 2 add sub dup
  332.     } {
  333.         box boxpos 11 sub exch 4 add exch moveto
  334.         dayfont findfont weekdayfontsize LG get scalefont setfont
  335.         notesheading show
  336.         box boxpos notesfontsize 13 add sub dup
  337.     } ifelse
  338.     /ypos exch def exch 4 add exch moveto
  339.     notesfont findfont notesfontsize scalefont setfont
  340.     /LM currentpoint pop def /RM LM 93 add def
  341.     showtext
  342. } def
  343.  
  344.  
  345. /crlf {            % simulate CR/LF sequence
  346.     ypos notesfontsize sub /ypos exch def LM ypos moveto
  347. } def
  348.  
  349.  
  350. /prstr {        % print string on current or next line
  351.     dup stringwidth pop currentpoint pop
  352.     add RM gt { crlf hangingindent show } if show
  353. } def
  354.  
  355.  
  356. /showtext {        % print words in "mytext", splitting into lines
  357.     mytext {
  358.         dup linesep eq            % force new line?
  359.             { crlf pop }        % yes - discard text
  360.             { prstr ( ) show }    % no - print string + space
  361.         ifelse
  362.     } forall
  363. } def
  364.  
  365.  
  366. %
  367. % Functions for printing months of various sizes and orientations:
  368. %
  369.  
  370.  
  371. /startpage {        % initialize new physical page
  372.     rval rotate
  373.     xsval ysval scale
  374.     xtval ytval translate
  375. } def
  376.  
  377.  
  378. /calendar        % draw calendar for month/year
  379. {
  380.     drawtitle                    % month/year heading
  381.  
  382.     calsize SM ne {                    % day names
  383.         drawdaynames
  384.     } if
  385.  
  386.     calsize LG eq {                    % foot strings
  387.         footstrings
  388.     } if
  389.  
  390.     drawnums                    % calendar dates
  391.  
  392.     julian-dates false ne calsize LG eq and {    % julian dates
  393.         drawjnums
  394.     } if
  395.  
  396.     fill-boxes {                    % fill unused boxes
  397.         drawfill
  398.     } if
  399.  
  400.     drawgrid                    % calendar grid
  401.  
  402.     draw-moons false ne calsize LG eq and {        % moon icons
  403.         drawmoons
  404.     } if
  405.  
  406.     0 0 moveto                    % return to origin
  407. } def
  408.  
  409.  
  410. /printmonth_l {        % print month on landscape page ("posn" = 0..11)
  411.     /calsize MED def
  412.  
  413.     posn 0 eq {        % assume first month printed on page is posn 0
  414.         startpage
  415.         footstrings
  416.     } if
  417.  
  418.     gsave            % draw medium calendar at selected position
  419.     .226 .25 scale        % landscape mode - 3 rows, 4 cols
  420.     posn 4 mod 800 mul
  421.     posn 4 idiv -700 mul 150 add
  422.     translate
  423.     calendar
  424.     grestore
  425. } def
  426.  
  427.  
  428. /printmonth_p {        % print month on portrait page ("posn" = 0..11)
  429.     /calsize MED def
  430.     /footfontsize 15 def    % compensate for scaling
  431.  
  432.     posn 0 eq {        % assume first month printed on page is posn 0
  433.         gsave        % print foot strings at original scale
  434.         startpage
  435.         0 20 translate  % move foot strings up slightly 
  436.         footstrings
  437.         grestore    % re-scale Y axis for portrait mode
  438.         /sv_ysval ysval def
  439.         /ysval ysval 1.675 mul def
  440.         startpage
  441.         /ysval sv_ysval def
  442.     } if
  443.  
  444.     gsave            % draw medium calendar at selected position
  445.     .304 .194 scale        % portrait mode - 4 rows, 3 cols
  446.     posn 3 mod 800 mul
  447.     posn 3 idiv -700 mul 300 add
  448.     translate
  449.     calendar
  450.     grestore
  451. } def
  452.  
  453.  
  454. /printsmallcals {        % print small calendars in lower right boxes
  455.     /calsize SM def
  456.     /sv_startbox startbox def
  457.  
  458.     % draw calendars for previous and next months in appropriate boxes
  459.  
  460.     prev_small_cal 0 ge {            % prev month/year
  461.         /year p_year def
  462.         /month p_month def
  463.         /startbox p_startbox def
  464.         /ndays p_ndays def
  465.         gsave
  466.         prev_small_cal boxpos translate
  467.         .138 .138 scale
  468.         12 -120 translate
  469.         calendar
  470.         grestore
  471.     } if
  472.  
  473.     next_small_cal 0 ge {            % next month/year
  474.         /year n_year def
  475.         /month n_month def
  476.         /startbox n_startbox def
  477.         /ndays n_ndays def
  478.         gsave
  479.         next_small_cal boxpos translate
  480.         .138 .138 scale
  481.         12 -120 translate
  482.         calendar
  483.         grestore
  484.     } if
  485.  
  486.     /startbox sv_startbox def        % required for text boxes
  487. } def
  488.  
  489.  
  490. /printmonth {        % print single month on page
  491.     startpage
  492.  
  493.     /calsize LG def            % main (large) calendar
  494.     calendar
  495.  
  496.     printsmallcals
  497.     
  498. } def
  499.  
  500.  
  501. %
  502. % Moon drawing functions:
  503. %
  504.  
  505.  
  506. /domoon {    % draw moon at phase (0 = new; .25 = 1q; .5 = full; .75 = 3q)
  507.     /phase exch def
  508.  
  509.     gsave
  510.     currentpoint translate
  511.     newpath
  512.  
  513.     % if moon is full, just draw unfilled circle
  514.  
  515.     phase halfperiod .01 sub ge phase halfperiod .01 add le and {
  516.         0 0 radius
  517.         0 360 arc stroke
  518.     }
  519.     {
  520.         % draw the line arc now; prepare (but don't draw) the fill arc
  521.  
  522.         0 0 radius            % for line and fill arcs
  523.         0 0 radius 
  524.         phase halfperiod lt {        % phase between new and full
  525.             270 90 arc stroke    % (line on right, fill on left)
  526.             0 radius neg moveto
  527.             270 90 arcn 
  528.         }
  529.         {                % phase between full and new
  530.             90 270 arc stroke    % (line on left, fill on right)
  531.             0 radius neg moveto
  532.             270 90 arc 
  533.             /phase phase halfperiod sub def
  534.         } ifelse
  535.  
  536.         % curveto uses (x0,y0) (current point), (x1,y1), (x2,y2),
  537.         % and (x3,y3) as the control points for drawing a Bezier
  538.         % cubic section, used here as the curve dividing the moon
  539.         % icon into dark and light sections.  x1 is in the range
  540.         % -R*sqrt(2) <= x1 <= R*sqrt(2) and y1 is in the range
  541.         % 0 <= y1 <= R; note that except in the degenerate case
  542.         % where x1 = y1 = x2 = y2 = 0, the curve does not actually
  543.         % pass through (x1,y1) or (x2,y2).
  544.  
  545.         /x1 quartperiod phase sub rect mul def
  546.         /y1 x1 abs 2 sqrt div def
  547.  
  548.         % push control points for curveto
  549.  
  550.                     % x0 = 0   (current
  551.                     % y0 = R    point)
  552.         x1            % x1
  553.         y1            % y1
  554.         x1            % x2 = x1
  555.         y1 neg            % y2 = -y1
  556.         0            % x3 = 0
  557.         radius neg        % y3 = -R
  558.  
  559.         % draw Bezier curve; fill area between curve and fill arc
  560.  
  561.         curveto
  562.         fill
  563.     } ifelse
  564.  
  565.     grestore
  566. } def
  567.  
  568.  
  569. /do-moon-p {        % draw a moon on "day"?
  570.     draw-moons (some) eq {        % printing quarter moons?  look up day
  571.         /p false def
  572.         quarter_moons { day eq { /p true def } if } forall
  573.         p
  574.     }
  575.     {
  576.         draw-moons        % all moons or no moons
  577.     } ifelse
  578. } def
  579.  
  580.  
  581. /drawmoons {        % main routine to draw moons on calendar
  582.     /halfperiod 0.5 def
  583.     /quartperiod 0.25 def
  584.     /radius 6 def
  585.     /offset radius 3 add def
  586.     /rect radius 2 sqrt mul quartperiod div def    % domoon{} scale factor
  587.     /n 0 def
  588.  
  589.     gsave
  590.     moonlinewidth setlinewidth
  591.     1 datepos offset sub exch daywidth add offset sub exch moveto
  592.     1 1 ndays {
  593.         /day exch def
  594.         do-moon-p {            % draw a moon today?
  595.             moon_phases n get domoon
  596.             /n n 1 add def
  597.         } if
  598.     nextbox
  599.     } for
  600.     grestore
  601. } def
  602.