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

  1. /* Copyright (C) 1989, 1992, 1993, 1994 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. /* gdevwprn.c */
  20. /*
  21.  * Microsoft Windows 3.n printer driver for Ghostscript.
  22.  * Original version by Russell Lang and
  23.  * L. Peter Deutsch, Aladdin Enterprises.
  24.  */
  25. #include "gdevmswn.h"
  26. #include "gp.h"
  27. #include "commdlg.h"
  28.  
  29. /*
  30.  ****** NOTE: this module and gdevwddb should be refactored.
  31.  * The drawing routines are almost identical.
  32.  * The differences are that the mswinprn doesn't use an extra
  33.  * palette (gdevwddb.c could probably be made to work with 
  34.  * one palette also), mswinprn doesn't call win_update() because
  35.  * hwndimg doesn't exist, and the HDC is hdcmf not hdcbit.
  36.  ******/
  37.  
  38. /* Make sure we cast to the correct structure type. */
  39. typedef struct gx_device_win_prn_s gx_device_win_prn;
  40. #undef wdev
  41. #define wdev ((gx_device_win_prn *)dev)
  42.  
  43. /* Forward references */
  44. private void near win_prn_addtool(P2(gx_device_win_prn *, int));
  45. private void near win_prn_maketools(P2(gx_device_win_prn *, HDC));
  46. private void near win_prn_destroytools(P1(gx_device_win_prn *));
  47.  
  48. /* Device procedures */
  49.  
  50. /* See gxdevice.h for the definitions of the procedures. */
  51. private dev_proc_open_device(win_prn_open);
  52. private dev_proc_close_device(win_prn_close);
  53. private dev_proc_sync_output(win_prn_sync_output);
  54. private dev_proc_output_page(win_prn_output_page);
  55. private dev_proc_map_rgb_color(win_prn_map_rgb_color);
  56. private dev_proc_fill_rectangle(win_prn_fill_rectangle);
  57. private dev_proc_tile_rectangle(win_prn_tile_rectangle);
  58. private dev_proc_copy_mono(win_prn_copy_mono);
  59. private dev_proc_copy_color(win_prn_copy_color);
  60. private dev_proc_draw_line(win_prn_draw_line);
  61.  
  62. /* The device descriptor */
  63. struct gx_device_win_prn_s {
  64.     gx_device_common;
  65.     gx_device_win_common;
  66.  
  67.     /* Handles */
  68.  
  69.     HPEN hpen, *hpens;
  70.     uint hpensize;
  71.     HBRUSH hbrush, *hbrushs;
  72.     uint hbrushsize;
  73. #define select_brush(color)\
  74.   if (wdev->hbrush != wdev->hbrushs[color])\
  75.    {    wdev->hbrush = wdev->hbrushs[color];\
  76.     SelectObject(wdev->hdcmf,wdev->hbrush);\
  77.    }
  78.     /* A staging bitmap for copy_mono. */
  79.     /* We want one big enough to handle the standard 16x16 halftone; */
  80.     /* this is also big enough for ordinary-size characters. */
  81.  
  82. #define bmWidthBytes 4        /* must be even */
  83. #define bmWidthBits (bmWidthBytes * 8)
  84. #define bmHeight 32
  85.     HBITMAP FAR hbmmono;
  86.     HDC FAR hdcmono;
  87.     gx_bitmap_id bm_id;
  88.  
  89.     HDC hdcprn;
  90.     HDC hdcmf;
  91.     char mfname[128];
  92.     DLGPROC lpfnAbortProc;
  93. };
  94. private gx_device_procs win_prn_procs = {
  95.     win_prn_open,
  96.     NULL,            /* get_initial_matrix */
  97.     win_prn_sync_output,
  98.     win_prn_output_page,
  99.     win_prn_close,
  100.     win_prn_map_rgb_color,
  101.     win_map_color_rgb,
  102.     win_prn_fill_rectangle,
  103.     win_prn_tile_rectangle,
  104.     win_prn_copy_mono,
  105.     win_prn_copy_color,
  106.     win_prn_draw_line,
  107.     NULL,            /* get_bits */
  108.     NULL,            /* get_params */
  109.     NULL,            /* put_params */
  110.     NULL,            /* map_cmyk_color */
  111.     win_get_xfont_procs
  112. };
  113. gx_device_win_prn gs_mswinprn_device = {
  114.     sizeof(gx_device_win_prn),
  115.     &win_prn_procs,
  116.     "mswinprn",
  117.     INITIAL_WIDTH, INITIAL_HEIGHT,     /* win_prn_open() fills these in later */
  118.     INITIAL_RESOLUTION, INITIAL_RESOLUTION,    /* win_prn_open() fills these in later */
  119.     no_margins,
  120.     dci_black_and_white,
  121.     dev_init_misc, { 0 },        /* not open yet */
  122.     0,                /* BitsPerPixel */
  123.     5000,                /* UpdateInterval (in milliseconds) */
  124.     "\0",                /* GSVIEW_STR */
  125.     2,                /* nColors */
  126. };
  127.  
  128. /* Open the win_prn driver */
  129. private int
  130. win_prn_open(gx_device *dev)
  131. {    int depth;
  132.     PRINTDLG pd;
  133.     FILE *f;
  134.     POINT offset;
  135.     POINT size;
  136.  
  137.     memset(&pd, 0, sizeof(PRINTDLG));
  138.     pd.lStructSize = sizeof(PRINTDLG);
  139.     pd.hwndOwner = hwndtext;
  140.     pd.Flags = PD_PRINTSETUP | PD_RETURNDC;
  141.     if (!PrintDlg(&pd)) {
  142.         /* device not opened - exit ghostscript */
  143.         return gs_error_limitcheck;
  144.     }
  145.     GlobalFree(pd.hDevMode);
  146.     GlobalFree(pd.hDevNames);
  147.     pd.hDevMode = pd.hDevNames = NULL;
  148.     wdev->hdcprn = pd.hDC;
  149.     if (!(GetDeviceCaps(wdev->hdcprn, RASTERCAPS) != RC_BITBLT)) {
  150.         DeleteDC(wdev->hdcprn);
  151.         return gs_error_limitcheck;
  152.     }
  153.  
  154.     wdev->lpfnAbortProc = (DLGPROC)MakeProcInstance((FARPROC)AbortProc,phInstance);
  155.     Escape(wdev->hdcprn,SETABORTPROC,0,(LPSTR)wdev->lpfnAbortProc,NULL);  
  156.     if (Escape(wdev->hdcprn, STARTDOC, strlen(szAppName),szAppName, NULL) <= 0) {
  157.         FreeProcInstance((FARPROC)wdev->lpfnAbortProc);
  158.         DeleteDC(wdev->hdcprn);
  159.         return gs_error_limitcheck;
  160.     }
  161.  
  162.     f = gp_open_scratch_file(gp_scratch_file_name_prefix, 
  163.             wdev->mfname, "wb");
  164.     if (f == (FILE *)NULL) {
  165.         Escape(wdev->hdcprn,ENDDOC,0,NULL,NULL);
  166.         FreeProcInstance((FARPROC)wdev->lpfnAbortProc);
  167.         DeleteDC(wdev->hdcprn);
  168.         return  gs_error_limitcheck;
  169.     }
  170.     unlink(wdev->mfname);
  171.     wdev->hdcmf = CreateMetaFile(wdev->mfname);
  172.  
  173.     dev->x_pixels_per_inch = GetDeviceCaps(wdev->hdcprn, LOGPIXELSX);
  174.     dev->y_pixels_per_inch = GetDeviceCaps(wdev->hdcprn, LOGPIXELSY);
  175.     Escape(wdev->hdcprn, GETPHYSPAGESIZE, NULL, NULL, (LPPOINT)&size);
  176.     dev->width = size.x;
  177.     dev->height = size.y;
  178.     Escape(wdev->hdcprn, GETPRINTINGOFFSET, NULL, NULL, (LPPOINT)&offset);
  179.     dev->l_margin = offset.x / dev->x_pixels_per_inch;
  180.     dev->t_margin = offset.y / dev->y_pixels_per_inch;
  181.     dev->r_margin =
  182.         (size.x - offset.x - GetDeviceCaps(wdev->hdcprn, HORZRES))
  183.          / dev->x_pixels_per_inch;
  184.     dev->b_margin =
  185.         (size.y - offset.y - GetDeviceCaps(wdev->hdcprn, VERTRES))
  186.          / dev->y_pixels_per_inch
  187.         + 0.15;  /* hack to add a bit more margin for deskjet printer */
  188.  
  189.     wdev->hdctext = NULL;
  190.  
  191.     /* Set parameters that were unknown before opening device */
  192.     /* Find out if the device supports color */
  193.     /* We recognize 2, 16 or 256 color devices */
  194.     depth = GetDeviceCaps(wdev->hdcprn,PLANES) * GetDeviceCaps(wdev->hdcprn,BITSPIXEL);
  195.     if ( depth >= 8 ) { /* use 64 static colors and 166 dynamic colors from 8 planes */
  196.         static const gx_device_color_info win_256color = dci_color(8,31,4);
  197.         dev->color_info = win_256color;
  198.         wdev->nColors = 64;
  199.     }
  200.     else if ( depth >= 4 ) {
  201.         static const gx_device_color_info win_16ega_color = dci_color(4, 2, 3);
  202.         dev->color_info = win_16ega_color;
  203.         wdev->nColors = 16;
  204.     } 
  205.     else {   /* default is black_and_white */
  206.         wdev->nColors = 2;
  207.     }
  208.  
  209.     /* create palette for display */
  210.     if ((wdev->limgpalette = win_makepalette((gx_device_win *)dev))
  211.         == (LPLOGPALETTE)NULL) {
  212.         HMETAFILE hmf = CloseMetaFile(wdev->hdcmf);
  213.         DeleteMetaFile(hmf);
  214.         unlink(wdev->mfname);
  215.         Escape(wdev->hdcprn,ENDDOC,0,NULL,NULL);
  216.         FreeProcInstance((FARPROC)wdev->lpfnAbortProc);
  217.         DeleteDC(wdev->hdcprn);
  218.         return win_nomemory();
  219.     }
  220.     wdev->himgpalette = CreatePalette(wdev->limgpalette);
  221.  
  222.     /* Create the bitmap and DC for copy_mono. */
  223.     wdev->hbmmono = CreateBitmap(bmWidthBits, bmHeight, 1, 1, NULL);
  224.     wdev->hdcmono = CreateCompatibleDC(wdev->hdcprn);
  225.     if ( wdev->hbmmono == NULL || wdev->hdcmono == NULL ) {
  226.         HMETAFILE hmf = CloseMetaFile(wdev->hdcmf);
  227.         DeleteMetaFile(hmf);
  228.         unlink(wdev->mfname);
  229.         Escape(wdev->hdcprn,ENDDOC,0,NULL,NULL);
  230.         FreeProcInstance((FARPROC)wdev->lpfnAbortProc);
  231.         DeleteDC(wdev->hdcprn);
  232.         gs_free((char *)(wdev->limgpalette), 1, sizeof(LOGPALETTE) + 
  233.             (1<<(wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  234.             "win_prn_open");
  235.         return win_nomemory();
  236.     }
  237.     SetMapMode(wdev->hdcmono, GetMapMode(wdev->hdcprn));
  238.     SelectObject(wdev->hdcmono, wdev->hbmmono);
  239.     (void) SelectPalette(wdev->hdcmf,wdev->himgpalette,NULL);
  240.     RealizePalette(wdev->hdcmf);
  241.     win_prn_maketools(wdev,wdev->hdcmf);
  242.     wdev->bm_id = gx_no_bitmap_id;
  243.  
  244.     return 0;
  245. }
  246.  
  247.  
  248. /* Close the win_prn driver */
  249. private int
  250. win_prn_close(gx_device *dev)
  251. {
  252. HMETAFILE hmf;
  253.     /* Free resources */
  254.     Escape(wdev->hdcprn,ENDDOC,0,NULL,NULL);
  255.     FreeProcInstance((FARPROC)wdev->lpfnAbortProc);
  256.     DeleteDC(wdev->hdcprn);
  257.     hmf = CloseMetaFile(wdev->hdcmf);
  258.     DeleteMetaFile(hmf);
  259.     unlink(wdev->mfname);
  260.  
  261.     win_prn_destroytools(wdev);
  262.     DeleteDC(wdev->hdcmono);
  263.     DeleteObject(wdev->hbmmono);
  264.     DeleteObject(wdev->himgpalette);
  265.     gs_free((char *)(wdev->limgpalette), 1, sizeof(LOGPALETTE) + 
  266.         (1<<(wdev->color_info.depth)) * sizeof(PALETTEENTRY),
  267.         "win_prn_close");
  268.     return(0);
  269. }
  270.  
  271. /* Do nothing */
  272. int
  273. win_prn_sync_output(gx_device *dev)
  274. {
  275.     return 0;
  276. }
  277.  
  278. /* Write page to printer */
  279. int
  280. win_prn_output_page(gx_device *dev, int copies, int flush)
  281. {
  282. RECT rect;
  283. HMETAFILE hmf;
  284.     hmf = CloseMetaFile(wdev->hdcmf);
  285.     
  286.     Escape(wdev->hdcprn, NEXTBAND, NULL, NULL, (LPRECT)&rect);
  287.     while (!IsRectEmpty(&rect)) {
  288.         PlayMetaFile(wdev->hdcprn, hmf);
  289.         if (Escape(wdev->hdcprn, NEXTBAND, NULL, NULL, (LPRECT)&rect) <= 0)
  290.         break;
  291.     }
  292.     DeleteMetaFile(hmf);
  293.     unlink(wdev->mfname);
  294.     wdev->hdcmf = CreateMetaFile(wdev->mfname);
  295.     (void) SelectPalette(wdev->hdcmf,wdev->himgpalette,NULL);
  296.     RealizePalette(wdev->hdcmf);
  297.     SelectObject(wdev->hdcmf,wdev->hpen);
  298.     SelectObject(wdev->hdcmf,wdev->hbrush);
  299.  
  300.     return 0;
  301. }
  302.  
  303.  
  304. /* Map a r-g-b color to the colors available under Windows */
  305. private gx_color_index
  306. win_prn_map_rgb_color(gx_device *dev, gx_color_value r, gx_color_value g,
  307.   gx_color_value b)
  308. {    int i = wdev->nColors;
  309.     gx_color_index color = win_map_rgb_color(dev, r, g, b);
  310.     if ( color != i ) return color;
  311.     (void) SelectPalette(wdev->hdcmf,wdev->himgpalette,NULL);
  312.     RealizePalette(wdev->hdcmf);
  313.     win_prn_addtool(wdev, i);
  314.  
  315.     return color;
  316. }
  317.  
  318.  
  319. /* Macro for filling a rectangle with a color. */
  320. /* Note that it starts with a declaration. */
  321. #define fill_rect(x, y, w, h, color)\
  322. RECT rect;\
  323. rect.left = x, rect.top = y;\
  324. rect.right = x + w, rect.bottom = y + h;\
  325. FillRect(wdev->hdcmf, &rect, wdev->hbrushs[(int)color])
  326.  
  327.  
  328. /* Fill a rectangle. */
  329. private int
  330. win_prn_fill_rectangle(gx_device *dev, int x, int y, int w, int h,
  331.   gx_color_index color)
  332. {
  333.     fit_fill(dev, x, y, w, h);
  334.     /* Use PatBlt for filling.  Special-case black. */
  335.     if ( color == 0 )
  336.         PatBlt(wdev->hdcmf, x, y, w, h, rop_write_0s);
  337.     else
  338.     {    select_brush((int)color);
  339.         PatBlt(wdev->hdcmf, x, y, w, h, rop_write_pattern);
  340.     }
  341.  
  342.     return 0;
  343. }
  344.  
  345. /* Tile a rectangle.  If neither color is transparent, */
  346. /* pre-clear the rectangle to color0 and just tile with color1. */
  347. /* This is faster because of how win_copy_mono is implemented. */
  348. /* Note that this also does the right thing for colored tiles. */
  349. private int
  350. win_prn_tile_rectangle(gx_device *dev, const gx_tile_bitmap *tile,
  351.   int x, int y, int w, int h, gx_color_index czero, gx_color_index cone,
  352.   int px, int py)
  353. {    fit_fill(dev, x, y, w, h);
  354.     if ( czero != gx_no_color_index && cone != gx_no_color_index )
  355.        {    fill_rect(x, y, w, h, czero);
  356.         czero = gx_no_color_index;
  357.        }
  358.     if ( tile->raster == bmWidthBytes && tile->size.y <= bmHeight &&
  359.          (px | py) == 0 && cone != gx_no_color_index
  360.        )
  361.     {    /* We can do this much more efficiently */
  362.         /* by using the internal algorithms of copy_mono */
  363.         /* and gx_default_tile_rectangle. */
  364.         int width = tile->size.x;
  365.         int height = tile->size.y;
  366.         int rwidth = tile->rep_width;
  367.         int irx = ((rwidth & (rwidth - 1)) == 0 ? /* power of 2 */
  368.             x & (rwidth - 1) :
  369.             x % rwidth);
  370.         int ry = y % tile->rep_height;
  371.         int icw = width - irx;
  372.         int ch = height - ry;
  373.         int ex = x + w, ey = y + h;
  374.         int fex = ex - width, fey = ey - height;
  375.         int cx, cy;
  376.  
  377.         select_brush((int)cone);
  378.  
  379.         if ( tile->id != wdev->bm_id || tile->id == gx_no_bitmap_id )
  380.         {    wdev->bm_id = tile->id;
  381.             SetBitmapBits(wdev->hbmmono,
  382.                       (DWORD)(bmWidthBytes * tile->size.y),
  383.                       (BYTE *)tile->data);
  384.         }
  385.  
  386. #define copy_tile(srcx, srcy, tx, ty, tw, th)\
  387.   BitBlt(wdev->hdcmf, tx, ty, tw, th, wdev->hdcmono, srcx, srcy, rop_write_at_1s)
  388.  
  389.         if ( ch > h ) ch = h;
  390.         for ( cy = y; ; )
  391.            {    if ( w <= icw )
  392.                 copy_tile(irx, ry, x, cy, w, ch);
  393.             else
  394.             {    copy_tile(irx, ry, x, cy, icw, ch);
  395.                 cx = x + icw;
  396.                 while ( cx <= fex )
  397.                 {    copy_tile(0, ry, cx, cy, width, ch);
  398.                     cx += width;
  399.                 }
  400.                 if ( cx < ex )
  401.                 {    copy_tile(0, ry, cx, cy, ex - cx, ch);
  402.                 }
  403.             }
  404.             if ( (cy += ch) >= ey ) break;
  405.             ch = (cy > fey ? ey - cy : height);
  406.             ry = 0;
  407.            }
  408.  
  409.         return 0;
  410.     }
  411.     return gx_default_tile_rectangle(dev, tile, x, y, w, h, czero, cone, px, py);
  412. }
  413.  
  414.  
  415. /* Draw a line */
  416. private int
  417. win_prn_draw_line(gx_device *dev, int x0, int y0, int x1, int y1,
  418.   gx_color_index color)
  419. {
  420.     if (wdev->hpen != wdev->hpens[(int)color]) {
  421.         wdev->hpen = wdev->hpens[(int)color];
  422.         SelectObject(wdev->hdcmf,wdev->hpen);
  423.     }
  424. #ifdef __WIN32__
  425.     MoveToEx(wdev->hdcmf, x0, y0, NULL);
  426. #else
  427.     MoveTo(wdev->hdcmf, x0, y0);
  428. #endif
  429.     LineTo(wdev->hdcmf, x1, y1);
  430.     return 0;
  431. }
  432.  
  433. /* Copy a monochrome bitmap.  The colors are given explicitly. */
  434. /* Color = gx_no_color_index means transparent (no effect on the image). */
  435. private int
  436. win_prn_copy_mono(gx_device *dev,
  437.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  438.   int x, int y, int w, int h,
  439.   gx_color_index zero, gx_color_index one)
  440. {    int endx;
  441.     const byte *ptr_line;
  442.     int width_bytes, height;
  443.     DWORD rop = rop_write_at_1s;
  444.     int color;
  445.     BYTE aBit[bmWidthBytes * bmHeight];
  446.     BYTE *aptr = aBit;
  447.  
  448.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  449.  
  450.     if ( sourcex & ~7 )
  451.     {    base += sourcex >> 3;
  452.         sourcex &= 7;
  453.     }
  454.  
  455.     /* Break up large transfers into smaller ones. */
  456.     while ( (endx = sourcex + w) > bmWidthBits )
  457.     {    int lastx = (endx - 1) & -bmWidthBits;
  458.         int subw = endx - lastx;
  459.         int code = win_prn_copy_mono(dev, base, lastx,
  460.                          raster, gx_no_bitmap_id,
  461.                          x + lastx - sourcex, y,
  462.                          subw, h, zero, one);
  463.         if ( code < 0 ) return code;
  464.         w -= subw;
  465.     }
  466.     while ( h > bmHeight )
  467.     {    int code;
  468.         h -= bmHeight;
  469.         code = win_prn_copy_mono(dev, base + h * raster, sourcex,
  470.                      raster, gx_no_bitmap_id,
  471.                      x, y + h, w, bmHeight, zero, one);
  472.         if ( code < 0 ) return code;
  473.     }
  474.  
  475.     width_bytes = (sourcex + w + 7) >> 3;
  476.     ptr_line = base;
  477.  
  478.     if ( zero == gx_no_color_index )
  479.        {    if ( one == gx_no_color_index ) return 0;
  480.         color = (int)one;
  481.         if ( color == 0 )
  482.             rop = rop_write_0_at_1s;
  483.         else
  484.             select_brush(color);
  485.        }
  486.     else
  487.        {    if ( one == gx_no_color_index )
  488.            {    color = (int)zero;
  489.             rop = rop_write_at_0s;
  490.            }
  491.         else
  492.            {    /* Pre-clear the rectangle to zero */
  493.             fill_rect(x, y, w, h, zero);
  494.             color = (int)one;
  495.            }
  496.         select_brush(color);
  497.        }
  498.  
  499.     if ( id != wdev->bm_id || id == gx_no_bitmap_id )
  500.     {    wdev->bm_id = id;
  501.         if ( raster == bmWidthBytes )
  502.         {    /* We can do the whole thing in a single transfer! */
  503.             SetBitmapBits(wdev->hbmmono,
  504.                       (DWORD)(bmWidthBytes * h),
  505.                       (BYTE *)base);
  506.         }
  507.         else
  508.         {    for ( height = h; height--;
  509.                   ptr_line += raster, aptr += bmWidthBytes
  510.                 )
  511.             {    /* Pack the bits into the bitmap. */
  512.                 switch ( width_bytes )
  513.                 {
  514.                     default: memcpy(aptr, ptr_line, width_bytes); break;
  515.                     case 4: aptr[3] = ptr_line[3];
  516.                     case 3: aptr[2] = ptr_line[2];
  517.                     case 2: aptr[1] = ptr_line[1];
  518.                     case 1: aptr[0] = ptr_line[0];
  519.                 }
  520.             }
  521.             SetBitmapBits(wdev->hbmmono,
  522.                       (DWORD)(bmWidthBytes * h),
  523.                       &aBit[0]);
  524.         }
  525.     }
  526.  
  527.     BitBlt(wdev->hdcmf, x, y, w, h, wdev->hdcmono, sourcex, 0, rop);
  528.     return 0;
  529. }
  530.  
  531.  
  532. /* Copy a color pixel map.  This is just like a bitmap, except that */
  533. /* each pixel takes 8 or 4 bits instead of 1 when device driver has color. */
  534. private int
  535. win_prn_copy_color(gx_device *dev,
  536.   const byte *base, int sourcex, int raster, gx_bitmap_id id,
  537.   int x, int y, int w, int h)
  538. {
  539.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  540.  
  541.     if ( gx_device_has_color(dev) )
  542.     {
  543.     switch(dev->color_info.depth) {
  544.       case 8:
  545.         {    int xi, yi;
  546.         int skip = raster - w;
  547.         const byte *sptr = base + sourcex;
  548.           if ( w <= 0 ) return 0;
  549.           if ( x < 0 || x + w > dev->width )
  550.             return_error(gs_error_rangecheck);
  551.         for ( yi = y; yi - y < h; yi++ )
  552.            {
  553.             for ( xi = x; xi - x < w; xi++ )
  554.                {    int color =  *sptr++;
  555.                 SetPixel(wdev->hdcmf,xi,yi,PALETTEINDEX(color));
  556.                }
  557.             sptr += skip;
  558.            }
  559.         }
  560.         break;
  561.       case 4:
  562.        {    /* color device, four bits per pixel */
  563.         const byte *line = base + (sourcex >> 1);
  564.         int dest_y = y, end_x = x + w;
  565.  
  566.         if ( w <= 0 ) return 0;
  567.         while ( h-- )              /* for each line */
  568.            {    const byte *source = line;
  569.             register int dest_x = x;
  570.             if ( sourcex & 1 )    /* odd nibble first */
  571.                {    int color =  *source++ & 0xf;
  572.                 SetPixel(wdev->hdcmf,dest_x,dest_y,PALETTEINDEX(color));
  573.                 dest_x++;
  574.                }
  575.             /* Now do full bytes */
  576.             while ( dest_x < end_x )
  577.                {    int color = *source >> 4;
  578.                 SetPixel(wdev->hdcmf,dest_x,dest_y,PALETTEINDEX(color));
  579.                 dest_x++;
  580.                 if ( dest_x < end_x )
  581.                    {    color =  *source++ & 0xf;
  582.                     SetPixel(wdev->hdcmf,dest_x,dest_y,PALETTEINDEX(color));
  583.                     dest_x++;
  584.                    }
  585.                }
  586.             dest_y++;
  587.             line += raster;
  588.            }
  589.        }
  590.        break;
  591.     default:
  592.         return(-1); /* panic */
  593.     }
  594.     }
  595.     else 
  596.     /* monochrome device: one bit per pixel */
  597.        {    /* bitmap is the same as win_copy_mono: one bit per pixel */
  598.         win_prn_copy_mono(dev, base, sourcex, raster, id, x, y, w, h,
  599.             (gx_color_index)0, 
  600.             (gx_color_index)(dev->color_info.depth==8 ? 63 : dev->color_info.max_gray));
  601.        }
  602.     return 0;
  603. }
  604.  
  605.  
  606. /* ------ Internal routines ------ */
  607.  
  608. #undef wdev
  609.  
  610.  
  611. private void near
  612. win_prn_addtool(gx_device_win_prn *wdev, int i)
  613. {
  614.     wdev->hpens[i] = CreatePen(PS_SOLID, 1, PALETTEINDEX(i));
  615.     wdev->hbrushs[i] = CreateSolidBrush(PALETTEINDEX(i));
  616. }
  617.  
  618.  
  619. private void near
  620. win_prn_maketools(gx_device_win_prn *wdev, HDC hdc)
  621. {    int i;
  622.     wdev->hpensize = (1<<(wdev->color_info.depth)) * sizeof(HPEN);
  623.     wdev->hpens = (HPEN *)gs_malloc(1, wdev->hpensize,
  624.                     "win_prn_maketools(pens)");
  625.     wdev->hbrushsize = (1<<(wdev->color_info.depth)) * sizeof(HBRUSH);
  626.     wdev->hbrushs = (HBRUSH *)gs_malloc(1, wdev->hbrushsize,
  627.                         "win_prn_maketools(brushes)");
  628.     if (wdev->hpens && wdev->hbrushs) {
  629.         for (i=0; i<wdev->nColors; i++)
  630.             win_prn_addtool(wdev, i);
  631.  
  632.         wdev->hpen = wdev->hpens[0];
  633.         SelectObject(hdc,wdev->hpen);
  634.  
  635.         wdev->hbrush = wdev->hbrushs[0];
  636.         SelectObject(hdc,wdev->hbrush);
  637.     }
  638. }
  639.  
  640.  
  641. private void near
  642. win_prn_destroytools(gx_device_win_prn *wdev)
  643. {    int i;
  644.     for (i=0; i<wdev->nColors; i++) {
  645.         DeleteObject(wdev->hpens[i]);
  646.         DeleteObject(wdev->hbrushs[i]);
  647.     }
  648.     gs_free((char *)wdev->hbrushs, 1, wdev->hbrushsize,
  649.         "win_prn_destroytools(brushes)");
  650.     gs_free((char *)wdev->hpens, 1, wdev->hpensize,
  651.         "win_prn_destroytools(pens)");
  652. }
  653.