home *** CD-ROM | disk | FTP | other *** search
/ Graphics 16,000 / graphics-16000.iso / msdos / fractal / fdesi313 / fdes13s / fdesprin.c < prev    next >
Text File  |  1990-01-17  |  14KB  |  448 lines

  1. /******************************************************************************
  2.         Printer Module
  3. ******************************************************************************/
  4. #include <stdio.h>
  5. #include <stdarg.h>
  6. #include <bios.h>
  7. #include <graphics.h>
  8. #include <conio.h>
  9. #include <stdlib.h>
  10. #include <dos.h>
  11. #include "fdesplot.h"
  12. #include "fdesvirt.h"
  13. #include "fdesmenu.h"
  14.  
  15. #define ESC 033
  16.  
  17. int printer_type = 0;                   /* printer types:
  18.                                                 0: undefined
  19.                                                 1: HP laser jet II
  20.                                                 2: Epson 9 pin
  21.                                                 3: Epson 24 pin
  22.                                         */
  23. int print_abort;
  24.  
  25. /******************************************************************************
  26.                         BIOS printer I/O
  27. ******************************************************************************/
  28. int kb_throttle;           /* throttles checking for keyboard interrupt */
  29. void pputc(char ch)
  30. {
  31.         do {
  32.                 if ((kb_throttle++ & 0xff) == 0)
  33.                 {
  34.                 if (kbhit())
  35.                 {
  36.             if (getch() == 0) getch();
  37.                         if (print_abort == 1)   /* really stuck, two keys hit */
  38.                         {
  39.                                 restorecrtmode();
  40.                                 printf("Two keys hit during print, aborting program");
  41.                                 exit(0);
  42.                         }
  43.                         print_abort = 1;
  44.                         printer_type = 0;
  45.                         putmsg(150,150,"Print is being stopped",WHITE,RED);
  46.                         delay(2000);
  47.                         clrmsg();
  48.                 }
  49.                 }
  50.         } while (biosprint(2,0,0) != 0x90) ;
  51.         biosprint(0,ch,0);
  52. }
  53.  
  54. int pprintf(char *format, ...)
  55. {
  56. char buf[133];
  57. char *ptr;
  58. va_list parmlist;
  59.  
  60.     va_start(parmlist,format);
  61.     vsprintf(buf,format,parmlist);
  62.     va_end(parmlist);
  63.         ptr = buf;
  64.         while (*ptr)
  65.         {
  66.                 pputc(*ptr++);
  67.         }
  68.         return(0);
  69. }
  70. /*****************************************************************************
  71.                 HP Laser Jet II support
  72. ******************************************************************************/
  73. void HPII_resolution(int res)
  74. {
  75.         pprintf("\033*t%dR",res);
  76. }
  77. void HPII_graphics(void)
  78. {
  79.         HPII_resolution(150);
  80. /*        pprintf("\033E");       /* reset */
  81.       pprintf("\033*r3F");    /* image along width of page */
  82.       pprintf("\033*r0A");    /* left margin at x position 0 */
  83.       pprintf("\033*b0M");    /* no compression */
  84. */
  85. }
  86. void HPII_endpage(void)
  87. {
  88.         pprintf("\033*rB");             /* end raster graphics */
  89.         pprintf("\033E");               /* printer reset */
  90. }
  91. void HPII_line_print(char *buf,int num_bytes)
  92. {
  93. int i;
  94.         pprintf("\033*b%dW",num_bytes);  /* transfer raster graphics */
  95.         for (i=0; i<num_bytes; i++)
  96.         {
  97.                 pputc(buf[i]);
  98.         }
  99. }
  100.  
  101.  
  102. void HPII_printscreen(void)
  103. {
  104. int row;
  105. int col;
  106. int databyte;
  107. int i;
  108. char buf[80];
  109. char *bufptr;
  110.  
  111.         if (print_abort) return;
  112.  
  113.         HPII_graphics();
  114.  
  115.         for (row=0; row<=maxy; row++)
  116.         {
  117.                 bufptr = buf;
  118.                 for (col=0; col <= maxx; col += 8)
  119.                 {
  120.                         databyte = 0;
  121.                         for (i=col; i<(col+8); i++)
  122.                         {
  123.                                 databyte <<=1;
  124.                 if (getpixel(i,row)) databyte |= 1;
  125.                         }
  126.                         *bufptr++ = databyte;
  127.                 }
  128.                 HPII_line_print(buf,80);
  129.                 if (print_abort) return;
  130.         }
  131. }
  132. void HPII_printvscreen(void)
  133. {
  134. int row;
  135. int col;
  136. int databyte;
  137. int i;
  138. char buf[VHEIGHT/8 + 1];
  139. char *bufptr;
  140.  
  141.         HPII_graphics();
  142.  
  143.         for (col=0; col<VWIDTH; col++)
  144.         {
  145.                 bufptr = buf;
  146.                 for (row=VHEIGHT; row >= 0; row -= 8)
  147.                 {
  148.                         databyte = 0;
  149.                         for (i=row; i>(row-8); i--)
  150.                         {
  151.                                 databyte <<=1;
  152.                                 if (vgetpixel(col,i)) databyte |= 1;
  153.                         }
  154.                         *bufptr++ = databyte;
  155.                 }
  156.         HPII_line_print(buf,VHEIGHT/8);
  157.                 if (print_abort) return;
  158.         }
  159. }
  160. /*****************************************************************************
  161.                 Epson 9 pin support
  162. ******************************************************************************/
  163. void E9_line_spacing(int height)
  164. {
  165.         pprintf("\033\063%c",height);      /* line spacing in units of 1/3 dot */
  166. }
  167. void E9_endpage(void)
  168. {
  169.         E9_line_spacing(12*3);            /* default line spacing */
  170.         if (print_abort) return;
  171.         pprintf("\014");                /* control-L (page feed) */
  172. }
  173.  
  174. void E9_line_print(char *buf,int num_bytes)
  175. {
  176. int i;
  177.         pprintf("\r\n\033L%c%c",(num_bytes&0xff),(num_bytes>>8));
  178.                                         /* tell epson how many columns */
  179.                                         /* 'L' is super-res, 'K' is normal-res */
  180.         for (i=0; i<num_bytes; i++)
  181.         {
  182.                 pputc(buf[i]);
  183.         }
  184. }
  185.  
  186. void E9_line_print_ovly(char *buf,int num_bytes)        /* 'double' print */
  187. {
  188. int i;
  189.         pprintf("\033J\001\r\033L%c%c",(num_bytes&0xff),(num_bytes>>8));
  190.                                         /* tell epson how many columns */
  191.                                         /* 'L' is super-res, 'K' is normal-res */
  192.         for (i=0; i<num_bytes; i++)
  193.         {
  194.                 pputc(buf[i]);
  195.         }
  196. }
  197.  
  198.  
  199. void E9_printscreen(void)
  200. {
  201. int row;
  202. int col;
  203. int databyte;
  204. int i;
  205. char buf[640];
  206. char *bufptr;
  207.  
  208.         if (print_abort) return;
  209.  
  210.         E9_line_spacing(7*3-1);
  211.  
  212.         for (row=0; row<=maxy; row += 14)
  213.         {
  214.                 bufptr = buf;
  215.                 for (col=0; col <= maxx; col++)
  216.                 {
  217.                         databyte = 0;
  218.                         for (i=row; i<(row+14); i += 2)
  219.                         {
  220.                                 databyte <<=1;
  221.                                 if ((i <= maxy) && (getpixel(col,i) != 0))
  222.                                         databyte |= 1;
  223.                         }
  224.                         *bufptr++ = databyte;
  225.                 }
  226.                 E9_line_print(buf,640);
  227.  
  228.                 /* now do the 'overwrite' line (really it is one pixel down) */
  229.  
  230.                 bufptr = buf;
  231.                 for (col=0; col <= maxx; col++)
  232.                 {
  233.                         databyte = 0;
  234.                         for (i=row+1; i<(row+14); i += 2)
  235.                         {
  236.                                 databyte <<=1;
  237.                                 if ((i <= maxy) && (getpixel(col,i) != 0))
  238.                                         databyte |= 1;
  239.                         }
  240.                         *bufptr++ = databyte;
  241.                 }
  242.                 E9_line_print_ovly(buf,640);
  243.                 if (print_abort) return;
  244.         }
  245. }
  246. void E9_printvscreen(void)
  247. {
  248. int row;
  249. int col;
  250. int databyte;
  251. int i;
  252. char buf[VHEIGHT + 1];
  253. char *bufptr;
  254.  
  255.         E9_line_spacing(7*3-1);
  256.  
  257.         for (col=0; col<VWIDTH; col += 14)
  258.         {
  259.                 bufptr = buf;
  260.                 for (row=VHEIGHT; row >= 0; row--)
  261.                 {
  262.                         databyte = 0;
  263.                         for (i=col; i<(col+14); i += 2)
  264.                         {
  265.                                 databyte <<=1;
  266.                                 if (vgetpixel(i,row)) databyte |= 1;
  267.                         }
  268.                         *bufptr++ = databyte;
  269.                 }
  270.                 E9_line_print(buf,VHEIGHT);
  271.  
  272.                 /* now the overprint line */
  273.                 bufptr = buf;
  274.                 for (row=VHEIGHT; row >= 0; row--)
  275.                 {
  276.                         databyte = 0;
  277.                         for (i=col+1; i<(col+14); i += 2)
  278.                         {
  279.                                 databyte <<=1;
  280.                                 if (vgetpixel(i,row)) databyte |= 1;
  281.                         }
  282.                         *bufptr++ = databyte;
  283.                 }
  284.                 E9_line_print_ovly(buf,VHEIGHT);
  285.                 if (print_abort) return;
  286.         }
  287. }
  288.  
  289. /*****************************************************************************
  290.                 Epson 24 pin support
  291. ******************************************************************************/
  292. void E24_endpage(void)
  293. {
  294.         if (print_abort) return;
  295.         pprintf("\014");                /* control-L (page feed) */
  296. }
  297.  
  298. void E24_printscreen(void)
  299. {
  300. int row;
  301. int col;
  302. int databyte;
  303. int i;
  304.  
  305.         if (print_abort) return;
  306.  
  307.         pprintf("\033\063\030");        /* 24/180 line spacing */
  308.  
  309.         for (row=0; row<=maxy; row += 24)
  310.         {
  311.                 pprintf("\r\n\033*\047\0200\002");  /* triple density graphics
  312.                                                    (180 dots/in), 640 columns */
  313.                 for (col=0; col <= maxx; col++)
  314.                 {
  315.                         databyte = 0;
  316.                         for (i=row; i<(row+8); i++)
  317.                         {
  318.                                 databyte <<=1;
  319.                                 if ((i <= maxy) && (getpixel(col,i) != 0))
  320.                                         databyte |= 1;
  321.                         }
  322.                         pputc(databyte);
  323.                         databyte = 0;
  324.                         for (i=row+8; i<(row+16); i++)
  325.                         {
  326.                                 databyte <<=1;
  327.                                 if ((i <= maxy) && (getpixel(col,i) != 0))
  328.                                         databyte |= 1;
  329.                         }
  330.                         pputc(databyte);
  331.                         databyte = 0;
  332.                         for (i=row+16; i<(row+24); i++)
  333.                         {
  334.                                 databyte <<=1;
  335.                                 if ((i <= maxy) && (getpixel(col,i) != 0))
  336.                                         databyte |= 1;
  337.                         }
  338.                         pputc(databyte);
  339.  
  340.                 }
  341.                 if (print_abort) return;
  342.         }
  343. }
  344. void E24_printvscreen(void)
  345. {
  346. int row;
  347. int col;
  348. int databyte;
  349. int i;
  350.  
  351.         for (col=0; col<VWIDTH; col += 24)
  352.         {
  353.                 pprintf("\r\n\033*\047\0260\004");  /* triple density graphics
  354.                                                    (180 dots/in), 1200 columns */
  355.  
  356.                 for (row=(VHEIGHT-1); row >= 0; row--)
  357.                 {
  358.                         databyte = 0;
  359.                         for (i=col; i<(col+8); i++)
  360.                         {
  361.                                 databyte <<=1;
  362.                                 if (vgetpixel(i,row)) databyte |= 1;
  363.                         }
  364.                         pputc(databyte);
  365.                         databyte = 0;
  366.                         for (i=col+8; i<(col+16); i++)
  367.                         {
  368.                                 databyte <<=1;
  369.                                 if (vgetpixel(i,row)) databyte |= 1;
  370.                         }
  371.                         pputc(databyte);
  372.                         databyte = 0;
  373.                         for (i=col+16; i<(col+24); i++)
  374.                         {
  375.                                 databyte <<=1;
  376.                                 if (vgetpixel(i,row)) databyte |= 1;
  377.                         }
  378.                         pputc(databyte);
  379.                 }
  380.                 if (print_abort) return;
  381.         }
  382. }
  383.  
  384. /*****************************************************************************
  385.                 Printer selection
  386. *****************************************************************************/
  387.  
  388. void (*printscreen)(void);              /* vectors pointing to printer handler */
  389. void (*prn_endpage)(void);
  390. void (*printvscreen)(void);
  391.  
  392. popmenu ptype = {
  393.         4,
  394.         "HP Laser Jet II", "Epson 9 pin", "Epson 24 pin", "Cancel Print"
  395.         };
  396.  
  397. int printer_type_check(void)            /* returns 1 if print cancelled */
  398. {
  399. int select;
  400.         if (printer_type != 0) return(0);
  401.  
  402.     select = popup(100,100,&ptype,WHITE,BLUE);
  403.         switch (select) {
  404.                 case 1:
  405.                         prn_endpage = HPII_endpage;
  406.                         printscreen = HPII_printscreen;
  407.                         printvscreen = HPII_printvscreen;
  408.                         break;
  409.                 case 2:
  410.                         prn_endpage = E9_endpage;
  411.                         printscreen = E9_printscreen;
  412.                         printvscreen = E9_printvscreen;
  413.                         break;
  414.                 case 3:
  415.                         prn_endpage = E24_endpage;
  416.                         printscreen = E24_printscreen;
  417.                         printvscreen = E24_printvscreen;
  418.                         break;
  419.                 case 4:
  420.                         return(1);
  421.         }
  422.         printer_type = select;
  423.         return(0);
  424. }
  425.  
  426. popmenu pconfirm = {
  427.         2,
  428.         "Continue", "Cancel Print"
  429.         };
  430.  
  431. int print_confirm(void)                 /* returns 1 if user confirmed print */
  432. {
  433. int select;
  434.     select = popup(100,100,&pconfirm,WHITE,BLUE);
  435.         if (select == 1)
  436.         {
  437.                 print_abort = 0;
  438.                 return(0);
  439.         }
  440.         else
  441.         {
  442.                 printer_type = 0;
  443.                 return(1);
  444.         }
  445. }
  446.  
  447.  
  448.