;+
; NAME:
;     TOPRINTER
; PURPOSE:
;     General routine to print text to the printer.
; CATEGORY:
;     OVRO APC UTILITY
; CALLING SEQUENCE:
;     toprinter,text[,/fit | ,perpage=perpage][,/xcenter]
; INPUTS:
;     text     The string array of text to be printed
; OPTIONAL (KEYWORD) INPUT PARAMETERS:
;     fit      A keyword indicating that the font size should be
;                adjusted so that the entire text will fit on a
;                page.  Maximum font size is 50 (in device units)
;                and minimum is 20, so text will only fit if it is
;                not too many lines (generally about 160 lines).
;                Setting this keyword overrides the PERPAGE keyword.
;     perpage  A keyword giving the number of lines per page.  This
;                adjusts the font size so that exactly PERPAGE lines
;                will fit. Maximum font size is 50 (in device units)
;                and minimum is 20, so specifying less than 53 lines
;                or more than about 160 lines will force the actual
;                number of lines to be 53 or 160, respectively.
;     xcenter  A keyword that forces the text to be horizontally
;                centered on the page.  If the text is too wide, the
;                left margin will default to 5 characters and the
;                long lines will extend off the page.
; ROUTINES CALLED:
; OUTPUTS:
; COMMENTS:
; SIDE EFFECTS:
; RESTRICTIONS:
; MODIFICATION HISTORY:
;     Written 21-Aug-1998 by Dale E. Gary
;     26-Jul-1999  DG
;       Several changes to allow automatic choice of LANDSCAPE printing
;       if that is the most appropriate direction for the text to be
;       printed.
;-

pro toprinter,text,fit=fit,perpage=perpage,xcenter=xcenter

   ; Save the old device name for later restoration
   oldname = !d.name
   oldfont = !p.font

   ; Set the plot device to the printer device
   set_plot,'printer'
   device,/inch,/portrait,xsize=8.0,ysize=10.5,xoff=0,yoff=0

   ; Set !P.FONT to indicate hardware font will be used.
   !p.font=0

   ; Top and Bottom margins set at 5 characters (note character size is
   ; subject to change.
   tmargin = 5
   bmargin = 5

   ; Number of lines of text
   nlines = n_elements(text)
   ; Number of characters of longest line
   nxchar = max(strlen(text))
   ; Number of lines per page
   if (keyword_set(perpage)) then begin
      nperpage = perpage
   endif else if (keyword_set(fit)) then begin
      nperpage = nlines
   endif else begin
      nperpage = 60
   endelse

   aspect = 1.31*(nxchar*3./(nperpage*5.))
   if (nxchar gt 80 and aspect gt 1.0) then begin
      ; More than 80 characters of text, and the aspect ratio > 1.0, so
      ; this will fit better as landscape
      device,/land,/inch,ysize=8.0,xsize=10.5
   endif

   ; FIT keyword overrides PERPAGE keyword.  Max font size is 50, min is 20
;   if (keyword_set(fit)) then begin
      ; Adjust font size so that N lines will fit, taking the margins into
      ; account.  Fontsize will not exceed 50, or fall below 20.
      fontsiz = nint(!d.y_size/(1.0*nperpage+tmargin+bmargin))<50>20
      fontsiz = nint(!d.x_size/(nxchar*0.6))<fontsiz>20
;   endif else if (keyword_set(perpage)) then begin
;      ; Adjust font size so that PERPAGE lines will fit, taking the margins into
;      ; account.  Fontsize will not exceed 50, or fall below 20.
;      fontsiz = (!d.y_size/(perpage+tmargin+bmargin))<50>20
;   endif

   ; Convert FONTSIZ to a string and use it to specify the Lucida Console font
   fontstr = string(fontsiz,format='(I2)')
   device,set_font='Lucida Console*'+fontstr

   ; Adjust left margin according to XCENTER keyword, or default to 5 characters
   if (keyword_set(xcenter)) then begin
      nx = !d.x_size/!d.x_ch_size
      maxl = max(strlen(text))
      lmargin = ((nx-maxl)/2)>5
   endif else lmargin = 5

   ; Find out character size in device units
   dy = !d.y_ch_size
   dx = !d.x_ch_size

   ; Find horizontal center, for typing page numbers at bottom of page
   xcen = !d.x_size/2.

   ; Number of lines that will fit on the page, taking margins into account
   nylines = !d.y_size/dy - tmargin - bmargin
   pageno = 0

   ; Loop over the number of lines of text
   for i = 0, nlines-1 do begin
      ; Get line number of current line on page.  J ranges from 0 to NYLINES-1
      j = i mod nylines

      ; This is the end of a page
      if (j eq 0) then begin
         if (pageno ne 0) then begin
            ; If this is not the zeroth page, then the current page is full.
            ; Write the page number at the bottom and ERASE to eject the page
            if (pageno ge 10) then pstr = string(pageno,format='("- ",I2," -")') $
                              else pstr = string(pageno,format='("- ",I1," -")')
            xyouts,/device,xcen-2*dx,(bmargin/2.)*dy,pstr
            erase
         endif
         ; Advance the page number
         pageno = pageno+1   ; Page number we are currently working on
      endif

      ; Write the line onto the page
      xyouts,/device,lmargin*dx,!d.y_size-(j+1+tmargin)*dy,text(i)
   endfor

   ; All lines have been written. Write the page number on the last page.
   if (pageno ge 10) then pstr = string(pageno,format='("- ",I2," -")') $
                     else pstr = string(pageno,format='("- ",I1," -")')
   xyouts,/device,xcen-2*dx,(bmargin/2.)*dy,pstr

   ; Close the printer device and reset the original device and font setting.
   device,/close
   set_plot,oldname
   !p.font = oldfont

return
end
