home *** CD-ROM | disk | FTP | other *** search
/ Shareware Overload / ShartewareOverload.cdr / graf / fract5.zip / PRINTER.C < prev    next >
Text File  |  1989-07-02  |  10KB  |  231 lines

  1. /* Printer.c
  2.  *    Simple screen printing functions for FRACTINT    
  3.  *    By Matt Saucier CIS: [72371,3101]      7/2/89 
  4.  *    "True-to-the-spirit" of FRACTINT, this code makes few checks that you
  5.  *    have specified a valid resolution for the printer (just in case yours
  6.  *      has more dots/line than the Standard HP and IBM/EPSON,
  7.  *    (eg, Wide Carridge, etc.))
  8.  *
  9.  *    This code supports printers attached to a LPTx (1-3) parallel port.
  10.  *    It also now supports serial printers AFTER THEY ARE CONFIGURED AND
  11.  *    WORKING WITH THE DOS MODE COMMAND, eg. MODE COM1:9600,n,8,1 (for HP)
  12.  *    Printing calls are made directly to the BIOS for DOS can't handle fast
  13.  *    transfer of data to the HP.  (Or maybe visa-versa, HP can't handle the
  14.  *    slow transfer of data from DOS)
  15.  *
  16.  *    Supported Printers:     Tested Ok:
  17.  *     HP LaserJet         
  18.  *         LJ+,LJII         MDS
  19.  *     Toshiba PageLaser     MDS (Set FRACTINT to use HP)
  20.  *       IBM Graphics         MDS
  21.  *      EPSON                   
  22.  *          Models?         Untested.
  23.  *
  24.  *    Future support to include OKI 20 (color) printer, and just about
  25.  *    any printer you request.
  26.  *
  27.  *    Future modifications to include a more flexible, standard interface
  28.  *    with the surrounding program, for easier portability to other 
  29.  *      programs.
  30.  *
  31.  * Compile with  MSC 5.1 or TurboC 2.0 (or QC?? Someone PLEASE Verify this)
  32.  *
  33.  * Tabs at 8.
  34.  */
  35.  
  36. #include <bios.h>
  37. #include <dos.h>
  38. #include <stdarg.h>
  39. #include "fractint.h"
  40.  
  41. /********      PROTOTYPES     ********/
  42.  
  43. int Printer_printf(int LPTn,char *fmt,...);
  44. int Printer(int a,int b,int c);
  45.  
  46. /********  EXTRN GLOBAL VARS  ********/
  47.  
  48. extern int xdots,ydots,        /* size of screen     */
  49.               extraseg;        /* used for buffering */
  50.  
  51. /********       GLOBALS       ********/
  52.  
  53. int Printer_Resolution,        /* 75,100,150,300 for HP; 60,120,240 for IBM */
  54.              LPTNumber,         /* ==1,2,3 LPTx; or 11,12,13,14 for COM1-4   */
  55.           Printer_Type;        /* ==1 HP,  ==2 IBM/EPSON             */
  56.  
  57.  
  58. #ifdef __TURBOC__
  59.         #define         _bios_printer(a,b,c)    biosprint((a),(c),(b))
  60.     #define        _bios_serialcom(a,b,c)    bioscom((a),(c),(b))
  61. #endif
  62.  
  63. void Print_Screen()
  64. {
  65.         register int y,j;               /* indices                          */
  66.         char buff[192];                 /* buffer for 192 sets of pixels    */
  67.                                         /* This is very large so that we can*/
  68.                                         /* get reasonable times printing    */
  69.                                         /* from modes like 2048x2048 disk-  */
  70.                                         /* video.  When this was 24, a 2048 */
  71.                                         /* by 2048 pic took over 2 hours to */
  72.                                         /* print.  It takes about 15 min now*/
  73.     int BuffSiz;            /* how much of buff[] we'll use     */
  74.         char far *es;                   /* pointer to extraseg for buffer   */
  75.     int i,x,                        /* more indices                     */
  76.             imax,                       /* maximum i value (ydots/8)        */
  77.             res,                        /* resolution we're gonna' use      */
  78.             LPTn,                       /* printer number we're gonna use   */
  79.         COM,            /* if LPTn>10 COM == com port to use*/
  80.             high, low,                  /* misc                             */
  81.                                         /************************************/
  82.             ptrid;                      /* Printer Id code.                 */
  83.                                         /* Currently, the following are     */
  84.                                         /* assigned:                        */
  85.                                         /*            1.  HPLJ (all)        */
  86.                                         /*                Toshiba PageLaser */
  87.                                         /*            2.  IBM Graphics      */
  88.                                         /************************************/
  89.  
  90.         /********   SETUP VARIABLES  ********/
  91.         for (j=0;j<192;j++) {
  92.                 buff[j]=0;      /* Clear buffer */
  93.         }
  94.         if (extraseg) {
  95.             #ifdef __TURBOC__
  96.                 es=MK_FP(extraseg,0);
  97.             #else
  98.                 FP_SEG(es)=extraseg;
  99.                 FP_OFF(es)=0;
  100.             #endif
  101.         }
  102.         LPTn=LPTNumber-1;
  103.     if (((LPTn<10)&&(LPTn>2))||
  104.            (LPTn<0)||(LPTn>13)) LPTn=0;    /* default of LPT1 (==0)  */
  105.         ptrid=Printer_Type;
  106.         if ((ptrid<1)||(ptrid>2)) ptrid=2; /* default of IBM/EPSON */
  107.         res=Printer_Resolution;
  108.         switch (ptrid) {
  109.                 case 1: if (res<75) res=75;
  110.             if ( (res<= 75)&&(ydots> 600)) res=100;
  111.              if ( (res<=100)&&(ydots> 800)) res=150;
  112.             if (((res<=150)&&(ydots>1200))||(res>300)) res=300;
  113.                         break;
  114.                 case 2: if (res<60) res=60;
  115.             if ((res<=60)&&(ydots>480)) res=120;
  116.                  if (((res<=120)&&(ydots>960))||(res>240)) res=240;
  117.             break;
  118.         }
  119.  
  120.     /*****  Set up buffer size for immediate user gratification *****/
  121.     /*****    AKA, if we don't have to, don't buffer the data   *****/
  122.     BuffSiz=8;
  123.     if (xdots>1024) BuffSiz=192;
  124.  
  125.     /*****   Initialize printer  *****/
  126.         printer(1,LPTn,0);
  127.  
  128.     /******  INITIALIZE GRAPHICS MODES  ******/
  129.         switch (ptrid) {
  130.                 case 1:
  131.                         Printer_printf(LPTn,"\x1B*t%iR\x1B*r0A",res);  /* HP */
  132.                         break;
  133.                 case 2:
  134.                         Printer_printf(LPTn,"\x1B\x33\x19");          /* IBM */
  135.                         break;
  136.         }
  137.  
  138.     if (keypressed()) return; /* one last chance before we start... */
  139.  
  140.                 /*****  Get And Print Screen *****/
  141.         switch (ptrid) {
  142.                 case 1: {       /* HP LaserJet (et al) */
  143.                         imax=(ydots/8)-1;
  144.                         for (x=0;((x<xdots)&&(!keypressed()));x+=BuffSiz) {
  145.                                 for (i=imax;((i>=0)&&(!keypressed()));i--) {
  146.                                         for (y=7;((y>=0)&&(!keypressed()));y--) {
  147.                                           for (j=0;((j<BuffSiz)&&(!keypressed()));j++) {
  148.                                             if ((x+j)<xdots) {
  149.                                               buff[j]<<=1;
  150.                                               buff[j]+=(getcolor(x+j,i*8+y)&1);
  151.                                             }
  152.                                           }
  153.                                         }
  154.                                         for (j=0;j<BuffSiz;j++) {
  155.                                                 *(es+j+BuffSiz*i)=buff[j];
  156.                                                 buff[j]=0;
  157.                                         }
  158.                                 }
  159.                                 for (j=0;((j<BuffSiz)&&(!keypressed()));j++) {
  160.                                         if ((x+j)<xdots) {
  161.                                            Printer_printf(LPTn,"\x1B*b%iW"
  162.                                                                       ,imax+1);
  163.                                            for (i=imax;((i>=0)&&(!keypressed()));i--) {
  164.                                               printer(0,LPTn,*(es+j+BuffSiz*i));
  165.                                            }
  166.                                        }
  167.                                 }
  168.                         }
  169.                         if (!keypressed()) Printer_printf(LPTn,"\x1B*rB\x0C");
  170.                         break;
  171.                 }
  172.                 case 2: {       /* IBM Graphics/Epson */
  173.                         for (x=0;((x<xdots)&&(!keypressed()));x+=8) {
  174.                                 switch (res) {
  175.                                         case 60:  Printer_printf(LPTn,"\x1BK"); break;
  176.                                         case 120: Printer_printf(LPTn,"\x1BL"); break;
  177.                                         case 240: Printer_printf(LPTn,"\x1BZ"); break;
  178.                                 }
  179.                                 high=ydots/256;
  180.                                 low=ydots-(high*256);
  181.                                 printer(0,LPTn,low);
  182.                                 printer(0,LPTn,high);
  183.                                 for (y=ydots-1;((y>=0)&&(!keypressed()));y--) {
  184.                                         buff[0]=0;
  185.                                         for (i=0;i<8;i++) {
  186.                                                 buff[0]<<=1;
  187.                                                 buff[0]+=(getcolor(x+i,y)&1);
  188.                                         }
  189.                                         printer(0,LPTn,buff[0]);
  190.                                 }
  191.                 if (keypressed()) break;
  192.                                 printer(0,LPTn,13);
  193.                                 printer(0,LPTn,10);
  194.                         }
  195.             if (!keypressed()) printer(0,LPTn,12);
  196.                         break;
  197.                 }
  198.         }
  199. }
  200.  
  201. /* This function prints a string to the the printer with BIOS calls.
  202.  * It returns the printer's status.
  203.  */
  204. int Printer_printf(int LPTn,char *fmt,...)
  205. {
  206.     char s[500];
  207.     int x=0;
  208.     va_list arg;
  209.     va_start(arg,fmt);
  210.     vsprintf(s,fmt,arg);
  211.     while ((s[x])&&(!keypressed())) {
  212.                 if (printer(0,LPTn,s[x++])!=0) {
  213.             while (!keypressed()) {
  214.                 if (printer(0,LPTn,s[x-1])==0) break;
  215.             }
  216.         }
  217.     }
  218.         return (printer(2,LPTn,0));
  219. }
  220.  
  221. /* This function standardizes both _bios_printer and _bios_serialcom
  222.  * in one function.  It takes it's arguments and re-aranges them and calls
  223.  * the appropriate bios call.  It the return result !=0, there is a problem
  224.  * with the printer.
  225.  */
  226. int printer(int a,int b,int c)
  227. {
  228.     if (b<9) return (((_bios_printer(a,b,c))+0x0010)&0x0010);
  229.     return ((_bios_serialcom(((a!=0)?3:1),(b-10),c))&0x9E00);
  230. }
  231.