home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 February / PCWK0296.iso / sharewar / dos / program / gs300sr1 / gs300sr1.exe / GDEVBJ10.C < prev    next >
C/C++ Source or Header  |  1994-07-27  |  14KB  |  391 lines

  1. /* Copyright (C) 1990, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of Aladdin Ghostscript.
  4.   
  5.   Aladdin Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author
  6.   or distributor accepts any responsibility for the consequences of using it,
  7.   or for whether it serves any particular purpose or works at all, unless he
  8.   or she says so in writing.  Refer to the Aladdin Ghostscript Free Public
  9.   License (the "License") for full details.
  10.   
  11.   Every copy of Aladdin Ghostscript must include a copy of the License,
  12.   normally in a plain ASCII text file named PUBLIC.  The License grants you
  13.   the right to copy, modify and redistribute Aladdin Ghostscript, but only
  14.   under certain conditions described in the License.  Among other things, the
  15.   License requires that the copyright notice and this notice be preserved on
  16.   all copies.
  17. */
  18.  
  19. /* gdevbj10.c */
  20. /* Canon Bubble Jet BJ-10e printer driver for Ghostscript */
  21. #include "gdevprn.h"
  22.  
  23. private dev_proc_get_initial_matrix(bj_get_initial_matrix);
  24.  
  25. /*
  26.  * The only available resolutions are (180,360)x(180,360).
  27.  */
  28.  
  29. /* The device descriptor */
  30. private dev_proc_print_page(bj10e_print_page);
  31. gx_device_printer far_data gs_bj10e_device =
  32.   prn_device(prn_std_procs, "bj10e",
  33.     80,                /* width_10ths, 8" */
  34.     105,                /* height_10ths, 10.5" */
  35.     360,                /* x_dpi */
  36.     360,                /* y_dpi */
  37.     0,0,0,0,            /* margins */
  38.     1, bj10e_print_page);
  39.  
  40. /*
  41.  * The following is taken from the BJ200 Programmer's manual.  The top
  42.  * margin is 3mm (0.12"), and the bottom margin is 6.4mm (0.25").  The
  43.  * left and right margin depend on the type of paper -- US letter or
  44.  * A4 -- but ultimately rest on a print width of 203.2mm (8").  For letter
  45.  * paper, the left margin (and hence the right) is 6.4mm (0.25"), while
  46.  * for A4 paper, both are 3.4mm (0.13").
  47.  *
  48.  * The bottom margin requires a bit of care.  The image is printed
  49.  * as strips, each about 3.4mm wide.  We can only attain the bottom 
  50.  * margin if the final strip coincides with it.  Note that each strip
  51.  * is generated using only 48 of the available 64 jets, and the absence
  52.  * of those bottom 16 jets makes our bottom margin, in effect, about
  53.  * 1.1mm (0.04") larger.
  54.  *
  55.  * The bj200 behaves, in effect, as though the origin were at the first
  56.  * printable position, rather than the top left corner of the page, so
  57.  * we add a translation to the initial matrix to compensate for this.
  58.  *
  59.  * Except for the details of getting the margins correct, the bj200 is
  60.  * no different from the bj10e, and uses the same routine to print each
  61.  * page.
  62.  *
  63.  * NOTE:  The bj200 has a DIP switch called "Text scale mode" and if
  64.  * set, it allows the printer to get 66 lines on a letter-sized page
  65.  * by reducing the line spacing by a factor of 14/15.  If this DIP
  66.  * switch is set, the page image printed by ghostscript will also be
  67.  * similarly squeezed.  Thus text scale mode is something ghostscript
  68.  * would like to disable.
  69.  *
  70.  * According to the bj200 manual, which describes the bj10 commands,
  71.  * the printer can be reset either to the settings determined by the
  72.  * DIP switches, or to the factory defaults, and then some of those
  73.  * settings can be specifically overriden.  Unfortunately, the text
  74.  * scale mode and horizontal print position (for letter vs A4 paper)
  75.  * can not be overriden.  On my bj200, the factory settings are for
  76.  * no text scaling and letter paper, thus using the factory defaults
  77.  * also implies letter paper.  I don't know if this is necessarily
  78.  * true for bj200's sold elsewhere, or for other printers that use
  79.  * the same command set.
  80.  *
  81.  * If your factory defaults are in fact the same, you can compile
  82.  * the driver with USE_FACTORY_DEFAULTS defined, in which case the
  83.  * printer will be reset to the factory defaults for letter paper,
  84.  * and reset to the DIP switch settings for A4 paper.  In this case,
  85.  * with letter-sized paper, the text scale mode will be disabled.
  86.  * Further, by leaving the horizontal print position DIP switch set
  87.  * for A4 paper, gs will be able to print on either A4 or letter
  88.  * paper without changing the DIP switch.  Since it's not clear that
  89.  * the factory defaults are universal, the default behaviour is not
  90.  * to define USE_FACTORY_DEFAULTS, and the printer will always be
  91.  * reset to the DIP switch defaults.
  92.  */
  93.  
  94. #define BJ200_TOP_MARGIN        0.12
  95. #define BJ200_BOTTOM_MARGIN        0.29
  96. #define BJ200_LETTER_SIDE_MARGIN    0.25
  97. #define BJ200_A4_SIDE_MARGIN        0.13
  98.  
  99. private dev_proc_open_device(bj200_open);
  100.  
  101. private gx_device_procs prn_bj200_procs =
  102.   prn_matrix_procs(bj200_open, bj_get_initial_matrix,
  103.     gdev_prn_output_page, gdev_prn_close);
  104.  
  105. gx_device_printer far_data gs_bj200_device =
  106.   prn_device(prn_bj200_procs, "bj200",
  107.     DEFAULT_WIDTH_10THS,
  108.     DEFAULT_HEIGHT_10THS,
  109.     360,                /* x_dpi */
  110.     360,                /* y_dpi */
  111.     0, 0, 0, 0,            /* margins filled in by bj200_open */
  112.     1, bj10e_print_page);
  113.  
  114. /*
  115.  * Notes on the BJ10e/BJ200 command set.
  116.  *
  117.  
  118. According to the BJ200 manual, the "set initial condition" sequence (ESC [
  119. K) has 2 bytes which can override the DIP switches -- these are the last 2
  120. bytes.  Several bits are listed as "reserved" -- one or more may possibly
  121. affect the sheet feeder.  The first is referred to as <P1>, with the
  122. following meaning:
  123.                 1        0
  124. bit 7    ignore/process P1    ignore        process
  125. bit 6    reserved
  126. bit 5    alarm            disabled    enabled
  127. bit 4    automatic CR        CR+LF        CR
  128. bit 3    automatic LF        CR+LF        LF
  129. bit 2    page length        12 inches    11 inches
  130. bit 1    style for zero        slashed        not slashed
  131. bit 0    character set        set 2        set 1
  132.  
  133. The last byte is <P2>, with the following meaning:
  134.                 1        0
  135. bit 7    ignore/process P2    ignore        process
  136. bit 6    code page        850        437
  137. bit 5    reserved
  138. bit 4    reserved
  139. bit 3    reserved
  140. bit 2    reserved
  141. bit 1    reserved
  142. bit 0    reserved
  143.  
  144. The automatic CR setting is important to gs, but the rest shouldn't matter
  145. (gs doesn't print characters or send LF, and it explicitly sets the page
  146. length).  The sequence ESC 5 <n> controls automatic CR -- if <n> is 0x00,
  147. it is turned off (CR only) and if <n> is 0x01, it is turned on (CR + LF).
  148. So we do following: Change the initialization string to so that the last 2
  149. of the 9 bytes are \200 rather than \000.  Then add
  150.     |* Turn off automatic carriage return, otherwise we get line feeds. *|
  151.     fwrite("\0335\000", 1, 3, prn_stream);
  152. after the initialization.  (Actually, instead of setting the last 2 bytes
  153. to \200, we suppress them altogether by changing the byte count from \004
  154. to \002 (the byte count is the 4th (low 8 bits) and 5th (high 8 bits) bytes
  155. in the initialization sequence).)
  156.  
  157. */
  158.  
  159. /* ------ Internal routines ------ */
  160.  
  161. /* Open the printer, and set the margins. */
  162. private int
  163. bj200_open(gx_device *pdev)
  164. {    /* Change the margins according to the paper size. */
  165.  
  166.     /* The top and bottom margins don't seem to depend on the
  167.        page length, but on the paper handling mechanism. */
  168.     pdev->t_margin = BJ200_TOP_MARGIN;
  169.     pdev->b_margin = BJ200_BOTTOM_MARGIN;
  170.  
  171.     /* The side margins do depend on the paper width, as the
  172.        printer centres the 8" print line on the page. */
  173.     if ( pdev->width / pdev->x_pixels_per_inch <= 8.4 )
  174.         pdev->l_margin = pdev->r_margin = BJ200_A4_SIDE_MARGIN;
  175.     else
  176.         pdev->l_margin = pdev->r_margin = BJ200_LETTER_SIDE_MARGIN;
  177.  
  178.     return gdev_prn_open(pdev);
  179. }
  180.  
  181. /* Shift the origin from the top left corner of the pysical page to the
  182.    first printable pixel, as defined by the top and left margins. */
  183. private void
  184. bj_get_initial_matrix(gx_device *dev, gs_matrix *pmat)
  185. {    gx_default_get_initial_matrix(dev, pmat);
  186.     pmat->tx -= dev->l_margin * dev->x_pixels_per_inch;
  187.     pmat->ty -= dev->t_margin * dev->y_pixels_per_inch;
  188. }
  189.  
  190. /* Send the page to the printer. */
  191. private int
  192. bj10e_print_page(gx_device_printer *pdev, FILE *prn_stream)
  193. {    int line_size = gx_device_raster((gx_device *)pdev, 0);
  194.     int xres = pdev->x_pixels_per_inch;
  195.     int yres = pdev->y_pixels_per_inch;
  196.     int mode = (yres == 180 ?
  197.             (xres == 180 ? 11 : 12) :
  198.             (xres == 180 ? 14 : 16));
  199.     int bytes_per_column = (yres == 180) ? 3 : 6;
  200.     int bits_per_column = bytes_per_column * 8;
  201.     int skip_unit = bytes_per_column * 3;
  202.     byte *in = (byte *)gs_malloc(8, line_size, "bj10e_print_page(in)");
  203.     byte *out = (byte *)gs_malloc(bits_per_column, line_size, "bj10e_print_page(out)");
  204.     int lnum = 0;
  205.     int skip = 0;
  206.     int code = 0;
  207.     int last_row = pdev->height - (pdev->t_margin + pdev->b_margin) * yres;
  208.     int limit = last_row - bits_per_column;
  209.  
  210.     if ( in == 0 || out == 0 )
  211.     {    code = gs_error_VMerror;
  212.         gs_note_error(code);
  213.         goto fin;
  214.     }
  215.  
  216.     /* Initialize the printer. */
  217. #ifdef USE_FACTORY_DEFAULTS
  218.     /* Check for U.S. letter vs. A4 paper. */
  219.     fwrite(( pdev->width / pdev->x_pixels_per_inch <= 8.4 ?
  220.         "\033[K\002\000\000\044"    /*A4--DIP switch defaults*/ :
  221.         "\033[K\002\000\004\044"    /*letter--factory defaults*/ ),
  222.            1, 9, prn_stream);
  223. #else
  224.     fwrite("\033[K\002\000\000\044", 1, 9, prn_stream);
  225. #endif
  226.  
  227.     /* Turn off automatic carriage return, otherwise we get line feeds. */
  228.     fwrite("\0335\000", 1, 3, prn_stream);
  229.  
  230.     /* Set vertical spacing. */
  231.     fwrite("\033[\\\004\000\000\000", 1, 7, prn_stream);
  232.     fputc(yres & 0xff, prn_stream);
  233.     fputc(yres >> 8, prn_stream);
  234.  
  235.     /* Set the page length.  This is the printable length, in inches. */
  236.     fwrite("\033C\000", 1, 3, prn_stream);
  237.     fputc((last_row + yres - 1)/yres, prn_stream);
  238.  
  239.     /* Transfer pixels to printer.  The last row we can print is defined
  240.        by "last_row".  Only the bottom of the print head can print at the
  241.        bottom margin, and so we align the final printing pass.  The print
  242.        head is kept from moving below "limit", which is exactly one pass
  243.        above the bottom margin.  Once it reaches this limit, we make our
  244.        final printing pass of a full "bits_per_column" rows. */
  245.     while ( lnum < last_row )
  246.        {    
  247.         byte *in_data;
  248.         byte *in_end = in + line_size;
  249.         byte *out_beg = out;
  250.         byte *out_end = out + bytes_per_column * pdev->width;
  251.         byte *outl = out;
  252.         int bnum;
  253.  
  254.         /* Copy 1 scan line and test for all zero. */
  255.         code = gdev_prn_get_bits(pdev, lnum, in, &in_data);
  256.         if ( code < 0 ) goto xit;
  257.         /* The mem... or str... functions should be faster than */
  258.         /* the following code, but all systems seem to implement */
  259.         /* them so badly that this code is faster. */
  260.            {    register const long *zip = (const long *)in_data;
  261.             register int zcnt = line_size;
  262.             register const byte *zipb;
  263.             for ( ; zcnt >= 4 * sizeof(long); zip += 4, zcnt -= 4 * sizeof(long) )
  264.                {    if ( zip[0] | zip[1] | zip[2] | zip[3] )
  265.                     goto notz;
  266.                }
  267.             zipb = (const byte *)zip;
  268.             while ( --zcnt >= 0 )
  269.                {
  270.                 if ( *zipb++ )
  271.                     goto notz;
  272.                }
  273.             /* Line is all zero, skip */
  274.             lnum++;
  275.             skip++;
  276.             continue;
  277. notz:            ;
  278.            }
  279.  
  280.         /* Vertical tab to the appropriate position.  Note here that
  281.            we make sure we don't move below limit. */
  282.         if ( lnum > limit )
  283.             {    skip -= (limit - lnum);
  284.             lnum = limit;
  285.             }
  286.         while ( skip > 255 )
  287.            {    fputs("\033J\377", prn_stream);
  288.             skip -= 255;
  289.            }
  290.         if ( skip )
  291.             fprintf(prn_stream, "\033J%c", skip);
  292.  
  293.         /* If we've printed as far as "limit", then reset "limit"
  294.            to "last_row" for the final printing pass. */
  295.         if ( lnum == limit )
  296.             limit = last_row;
  297.         skip = 0;
  298.  
  299.         /* Transpose in blocks of 8 scan lines. */
  300.         for ( bnum = 0; bnum < bits_per_column; bnum += 8 )
  301.            {    int lcnt = min(8, limit - lnum);
  302.             byte *inp = in;
  303.             byte *outp = outl;
  304.                lcnt = gdev_prn_copy_scan_lines(pdev,
  305.                 lnum, in, lcnt * line_size);
  306.             if ( lcnt < 0 )
  307.                {    code = lcnt;
  308.                 goto xit;
  309.                }
  310.             if ( lcnt < 8 )
  311.                 memset(in + lcnt * line_size, 0,
  312.                        (8 - lcnt) * line_size);
  313.             for ( ; inp < in_end; inp++, outp += bits_per_column )
  314.                {    gdev_prn_transpose_8x8(inp, line_size,
  315.                     outp, bytes_per_column);
  316.                }
  317.             outl++;
  318.             lnum += lcnt;
  319.             skip += lcnt;
  320.            }
  321.  
  322.         /* Send the bits to the printer.  We alternate horizontal
  323.            skips with the data.  The horizontal skips are in units
  324.            of 1/120 inches, so we look at the data in groups of
  325.            3 columns, since 3/360 = 1/120, and 3/180 = 2/120.  */
  326.         outl = out;
  327.         do
  328.            {    int count;
  329.             int n;
  330.             byte *out_ptr;
  331.  
  332.             /* First look for blank groups of columns. */
  333.             while(outl < out_end)
  334.                {    n = count = min(out_end - outl, skip_unit);
  335.                 out_ptr = outl;
  336.                 while ( --count >= 0 )
  337.                    {    if ( *out_ptr++ )
  338.                         break;
  339.                    }
  340.                 if ( count >= 0 )
  341.                     break;
  342.                 else
  343.                     outl = out_ptr;
  344.                }
  345.             if (outl >= out_end)
  346.                 break;
  347.             if (outl > out_beg)
  348.                {    count = (outl - out_beg) / skip_unit;
  349.                 if ( xres == 180 ) count <<= 1;
  350.                 fprintf(prn_stream, "\033d%c%c",
  351.                     count & 0xff, count >> 8);
  352.                }
  353.  
  354.             /* Next look for non-blank groups of columns. */
  355.             out_beg = outl;
  356.             outl += n;
  357.             while(outl < out_end)
  358.                {    n = count = min(out_end - outl, skip_unit);
  359.                 out_ptr = outl;
  360.                 while ( --count >= 0 )
  361.                    {    if ( *out_ptr++ )
  362.                         break;
  363.                    }
  364.                 if ( count < 0 )
  365.                     break;
  366.                 else
  367.                     outl += n;
  368.                }
  369.             count = outl - out_beg + 1;
  370.             fprintf(prn_stream, "\033[g%c%c%c",
  371.                 count & 0xff, count >> 8, mode);
  372.             fwrite(out_beg, 1, count - 1, prn_stream);
  373.             out_beg = outl;
  374.             outl += n;
  375.            }
  376.         while ( out_beg < out_end );
  377.  
  378.         fputc('\r', prn_stream);
  379.        }
  380.  
  381.     /* Eject the page */
  382. xit:    fputc(014, prn_stream);    /* form feed */
  383.     fflush(prn_stream);
  384. fin:    if ( out != 0 )
  385.         gs_free((char *)out, bits_per_column, line_size,
  386.             "bj10e_print_page(out)");
  387.     if ( in != 0 )
  388.         gs_free((char *)in, 8, line_size, "bj10e_print_page(in)");
  389.     return code;
  390. }
  391.