home *** CD-ROM | disk | FTP | other *** search
/ Mega Top 1 / os2_top1.zip / os2_top1 / APPS / TEKST / GSPMSRC / SRC / GDEVX.C < prev    next >
C/C++ Source or Header  |  1993-12-16  |  31KB  |  1,016 lines

  1. /* Copyright (C) 1989, 1992, 1993 Aladdin Enterprises.  All rights reserved.
  2.  
  3. This file is part of Ghostscript.
  4.  
  5. Ghostscript is distributed in the hope that it will be useful, but
  6. WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility
  7. to anyone for the consequences of using it or for whether it serves any
  8. particular purpose or works at all, unless he says so in writing.  Refer
  9. to the Ghostscript General Public License for full details.
  10.  
  11. Everyone is granted permission to copy, modify and redistribute
  12. Ghostscript, but only under the conditions described in the Ghostscript
  13. General Public License.  A copy of this license is supposed to have been
  14. given to you along with Ghostscript so you can know your rights and
  15. responsibilities.  It should be in a file named COPYING.  Among other
  16. things, the copyright notice and this notice must be preserved on all
  17. copies.  */
  18.  
  19. /* gdevx.c */
  20. /* X Windows driver for Ghostscript library */
  21. /* The X include files include <sys/types.h>, which, on some machines */
  22. /* at least, define uint, ushort, and ulong, which std.h also defines. */
  23. /* std.h has taken care of this. */
  24. #include "gx.h"            /* for gx_bitmap; includes std.h */
  25. #include "memory_.h"
  26. #include "x_.h"
  27. #include "gserrors.h"
  28. #include "gsprops.h"
  29. #include "gsutil.h"        /* for props_extract */
  30. #include "gxdevice.h"
  31. #include "gdevx.h"
  32.  
  33. /* Define the maximum size of the temporary pixmap for copy_mono */
  34. /* that we are willing to leave lying around in the server */
  35. /* between uses.  (Assume 32-bit ints here!) */
  36. private int max_temp_pixmap = 20000;
  37.  
  38. /* Forward references */
  39. private int set_tile(P2(gx_device *, const gx_bitmap *));
  40. private void free_cp(P1(gx_device *));
  41. /* Screen updating machinery */
  42. #define update_init(dev)\
  43.   ((gx_device_X *)(dev))->up_area = 0,\
  44.   ((gx_device_X *)(dev))->up_count = 0
  45. #define update_flush(dev)\
  46.   if ( ((gx_device_X *)(dev))->up_area != 0 ) update_do_flush(dev)
  47. private void update_do_flush(P1(gx_device *));
  48. private void x_send_event(P2(gx_device *, Atom));
  49.  
  50. /* Procedures */
  51.  
  52. extern int gdev_x_open(P1(gx_device_X *));
  53. private dev_proc_open_device(x_open);
  54. private dev_proc_get_initial_matrix(x_get_initial_matrix);
  55. private dev_proc_sync_output(x_sync);
  56. private dev_proc_output_page(x_output_page);
  57. private dev_proc_close_device(x_close);
  58. private dev_proc_map_rgb_color(x_map_rgb_color);
  59. private dev_proc_map_color_rgb(x_map_color_rgb);
  60. private dev_proc_fill_rectangle(x_fill_rectangle);
  61. private dev_proc_tile_rectangle(x_tile_rectangle);
  62. private dev_proc_copy_mono(x_copy_mono);
  63. private dev_proc_copy_color(x_copy_color);
  64. private dev_proc_draw_line(x_draw_line);
  65. private dev_proc_put_props(x_put_props);
  66. dev_proc_get_xfont_procs(x_get_xfont_procs);
  67.  
  68. /* The device descriptor */
  69. private gx_device_procs x_procs = {
  70.     x_open,
  71.     x_get_initial_matrix,
  72.     x_sync,
  73.     x_output_page,
  74.     x_close,
  75.     x_map_rgb_color,
  76.     x_map_color_rgb,
  77.     x_fill_rectangle,
  78.     x_tile_rectangle,
  79.     x_copy_mono,
  80.     x_copy_color,
  81.     x_draw_line,
  82.     gx_default_get_bits,
  83.     gx_default_get_props,
  84.     x_put_props,
  85.     NULL,
  86.     x_get_xfont_procs
  87. };
  88.  
  89. /* The instance is public. */
  90. gx_device_X gs_x11_device = {
  91.     sizeof(gx_device_X),
  92.     &x_procs,
  93.     "x11",
  94.     (int)(8.5*FAKE_RES),
  95.     (int)(11*FAKE_RES),    /* x and y extent (nominal) */
  96.     FAKE_RES, FAKE_RES,    /* x and y density (nominal) */
  97.     no_margins,
  98.     dci_black_and_white,
  99.     0,            /* connection not initialized */
  100.     { /* image */
  101.       0, 0,            /* width, height */
  102.       0, XYBitmap, NULL,    /* xoffset, format, data */
  103.       LSBFirst, 8,        /* byte-order, bitmap-unit */
  104.       MSBFirst, 8, 1,    /* bitmap-bit-order, bitmap-pad, depth */
  105.       0, 1,            /* bytes_per_line, bits_per_pixel */
  106.       0, 0, 0,        /* red_mask, green_mask, blue_mask */
  107.       NULL,            /* *obdata */
  108.        { NULL,            /* *(*create_image)() */
  109.          NULL,            /* (*destroy_image)() */
  110.          NULL,            /* (*get_pixel)() */
  111.          NULL,            /* (*put_pixel)() */
  112.          NULL,            /* *(*sub_image)() */
  113.          NULL            /* (*add_pixel)() */
  114.        },
  115.     },
  116.     NULL, NULL,        /* dpy, scr */
  117.                 /* (connection not initialized) */
  118.     NULL,            /* vinfo */
  119.     (Colormap)None,        /* cmap */
  120.     (Window)None,        /* win */
  121.     NULL,            /* gc */
  122.     (Pixmap)0,        /* bpixmap */
  123.     0,            /* ghostview */
  124.     (Window)None,        /* mwin */
  125. #if HaveStdCMap
  126.     NULL,            /* std_cmap */
  127. #endif
  128.     identity_matrix_body,    /* initial matrix (filled in) */
  129.     (Atom)0, (Atom)0, (Atom)0,    /* Atoms: NEXT, PAGE, DONE */
  130.      { 0, 0, 0, 0 }, 0, 0,    /* update, up_area, up_count */
  131.     (Pixmap)0,        /* dest */
  132.     0L, ~0L,        /* colors_or, colors_and */
  133.      { /* cp */
  134.        (Pixmap)0,        /* pixmap */
  135.        NULL,        /* gc */
  136.        -1, -1        /* raster, height */
  137.      },
  138.      { /* ht */
  139.        (Pixmap)None,        /* pixmap */
  140.        (Pixmap)None,        /* no_pixmap */
  141.        gx_no_bitmap_id,        /* id */
  142.        0, 0, 0,            /* width, height, raster */
  143.        0, 0                /* fore_c, back_c */
  144.      },
  145.     GXcopy,            /* function */
  146.     FillSolid,        /* fill_style */
  147.     0,            /* font */
  148.     0, 0,            /* back_color, fore_color */
  149.     0, 0,            /* background, foreground */
  150.     NULL,            /* dither_colors */
  151.     0, 0,            /* color_mask, half_dev_color */
  152.     NULL,            /* dynamic_colors */
  153.     0, 0,            /* dynamic_size, dynamic_number */
  154.     0, 0,            /* borderColor, borderWidth */
  155.     NULL,            /* geometry */
  156.     128, 5,            /* maxGrayRamp, maxRGBRamp */
  157.     NULL,            /* palette */
  158.     NULL, NULL, NULL,    /* regularFonts, symbolFonts, dingbatFonts */
  159.     NULL, NULL, NULL,    /* regular_fonts, symbol_fonts, dingbat_fonts */
  160.     1, 1,            /* useXFonts, useFontExtensions */
  161.     1, 0,            /* useScalableFonts, logXFonts */
  162.     0.0, 0.0,        /* xResolution, yResolution */
  163.     1,            /* useBackingPixmap */
  164.     1, 1,            /* useXPutImage, useXSetTile */
  165. };
  166.  
  167. /* If XPutImage doesn't work, do it ourselves. */
  168. private void alt_put_image();
  169. #define put_image(dpy,win,gc,im,sx,sy,x,y,w,h)\
  170.   if ( xdev->useXPutImage) XPutImage(dpy,win,gc,im,sx,sy,x,y,w,h);\
  171.   else alt_put_image(dev,dpy,win,gc,im,sx,sy,x,y,w,h)
  172.  
  173.  
  174. /* Open the device.  Most of the code is in gdevxini.c. */
  175. private int
  176. x_open(gx_device *dev)
  177. {
  178.     gx_device_X *xdev = (gx_device_X *)dev;
  179.     int code = gdev_x_open(xdev);
  180.  
  181.     if (code < 0) return code;
  182.     update_init(dev);
  183.     return 0;
  184. }
  185.  
  186. /* Close the device. */
  187. private int
  188. x_close(gx_device *dev)
  189. {
  190.     gx_device_X *xdev = (gx_device_X *)dev;
  191.  
  192.     if (xdev->ghostview) x_send_event(dev, xdev->done);
  193.     if (xdev->vinfo) {
  194.     XFree((char *)xdev->vinfo);
  195.     xdev->vinfo = NULL;
  196.     }
  197.     if (xdev->dither_colors) {
  198.     if (gx_device_has_color(xdev))
  199. #define cube(r) (r*r*r)
  200.         gs_free((char *)xdev->dither_colors, sizeof(x_pixel),
  201.             cube(xdev->color_info.dither_rgb), "gdev_x_rgb_cube");
  202. #undef cube
  203.     else
  204.         gs_free((char *)xdev->dither_colors, sizeof(x_pixel),
  205.             xdev->color_info.dither_gray, "gdev_x_gray_ramp");
  206.     xdev->dither_colors = NULL;
  207.     }
  208.     if (xdev->dynamic_colors) {
  209.     gs_free((char *)xdev->dynamic_colors, sizeof(XColor),
  210.         xdev->dynamic_size, "gdev_x_dynamic_colors");
  211.     xdev->dynamic_colors = NULL;
  212.     }
  213.     while (xdev->regular_fonts) {
  214.     x11fontmap *font = xdev->regular_fonts;
  215.     xdev->regular_fonts = font->next;
  216.     if (font->std_names) XFreeFontNames(font->std_names);
  217.     if (font->iso_names) XFreeFontNames(font->iso_names);
  218.     gs_free(font->x11_name, sizeof(char), strlen(font->x11_name)+1,
  219.         "gdev_x_font_x11name");
  220.     gs_free(font->ps_name, sizeof(char), strlen(font->ps_name)+1,
  221.         "gdev_x_font_psname");
  222.     gs_free((char *)font, sizeof(x11fontmap), 1, "gdev_x_fontmap");
  223.     }
  224.     while (xdev->symbol_fonts) {
  225.     x11fontmap *font = xdev->symbol_fonts;
  226.     xdev->symbol_fonts = font->next;
  227.     if (font->std_names) XFreeFontNames(font->std_names);
  228.     if (font->iso_names) XFreeFontNames(font->iso_names);
  229.     gs_free(font->x11_name, sizeof(char), strlen(font->x11_name)+1,
  230.         "gdev_x_font_x11name");
  231.     gs_free(font->ps_name, sizeof(char), strlen(font->ps_name)+1,
  232.         "gdev_x_font_psname");
  233.     gs_free((char *)font, sizeof(x11fontmap), 1, "gdev_x_fontmap");
  234.     }
  235.     while (xdev->dingbat_fonts) {
  236.     x11fontmap *font = xdev->dingbat_fonts;
  237.     xdev->dingbat_fonts = font->next;
  238.     if (font->std_names) XFreeFontNames(font->std_names);
  239.     if (font->iso_names) XFreeFontNames(font->iso_names);
  240.     gs_free(font->x11_name, sizeof(char), strlen(font->x11_name)+1,
  241.         "gdev_x_font_x11name");
  242.     gs_free(font->ps_name, sizeof(char), strlen(font->ps_name)+1,
  243.         "gdev_x_font_psname");
  244.     gs_free((char *)font, sizeof(x11fontmap), 1, "gdev_x_fontmap");
  245.     }
  246.     XCloseDisplay(xdev->dpy);
  247.     return 0;
  248. }
  249.  
  250. /* Map a color.  The "device colors" are just r,g,b packed together. */
  251. private gx_color_index
  252. x_map_rgb_color(register gx_device *dev,
  253.         gx_color_value r, gx_color_value g, gx_color_value b)
  254. {
  255.     gx_device_X *xdev = (gx_device_X *)dev;
  256.     unsigned short dr = r & xdev->color_mask;    /* Nearest color that */
  257.     unsigned short dg = g & xdev->color_mask;    /* the X device can   */
  258.     unsigned short db = b & xdev->color_mask;    /* represent          */
  259.  
  260.     /* foreground and background get special treatment */
  261.     /* They maybe mapped to other colors. */
  262. #define cv_max (gx_max_color_value & xdev->color_mask)
  263.     if (dr == 0 && dg == 0 && db == 0) {
  264.     return xdev->foreground;
  265.     }
  266.     if (dr == cv_max && dg == cv_max && db == cv_max) {
  267.     return xdev->background;
  268.     }
  269. #define cv_denom (gx_max_color_value + 1)
  270. #if HaveStdCMap
  271.     /* check the standard colormap first */
  272.     if (xdev->std_cmap) {
  273.     XStandardColormap *cmap = xdev->std_cmap;
  274.  
  275.     if (gx_device_has_color(xdev)) {
  276.         unsigned short cr, cg, cb;        /* rgb cube indices */
  277.         unsigned short cvr, cvg, cvb;    /* color value on cube */
  278.  
  279.         cr = r * (cmap->red_max + 1) / cv_denom;
  280.         cg = g * (cmap->green_max + 1) / cv_denom;
  281.         cb = b * (cmap->blue_max + 1) / cv_denom;
  282.         cvr = (0xffff * cr / cmap->red_max);
  283.         cvg = (0xffff * cg / cmap->green_max);
  284.         cvb = (0xffff * cb / cmap->blue_max);
  285.         if (abs((int)r - (int)cvr) < xdev->half_dev_color &&
  286.         abs((int)g - (int)cvg) < xdev->half_dev_color &&
  287.         abs((int)b - (int)cvb) < xdev->half_dev_color)
  288.         return cr * cmap->red_mult + cg * cmap->green_mult +
  289.                cb * cmap->blue_mult + cmap->base_pixel;
  290.     } else {
  291.         unsigned short cr;
  292.         unsigned short cvr;
  293.  
  294.         cr = r * (xdev->color_info.max_gray + 1) / cv_denom;
  295.         cvr = (0xffff * cr / cmap->red_max);
  296.         if (abs((int)r - (int)cvr) < xdev->half_dev_color)
  297.         return cr * cmap->red_mult + cmap->base_pixel;
  298.     }
  299.     } else
  300. #endif
  301.     /* If there is no standard colormap, check the dither cube/ramp */
  302.     if (xdev->dither_colors) {
  303.     if (gx_device_has_color(xdev)) {
  304.         unsigned short cr, cg, cb;        /* rgb cube indices */
  305.         unsigned short cvr, cvg, cvb;    /* color value on cube */
  306.  
  307. #define max_rgb (xdev->color_info.dither_rgb - 1)
  308.         cr = r * (xdev->color_info.dither_rgb) / cv_denom;
  309.         cg = g * (xdev->color_info.dither_rgb) / cv_denom;
  310.         cb = b * (xdev->color_info.dither_rgb) / cv_denom;
  311.         cvr = (0xffff * cr / max_rgb);
  312.         cvg = (0xffff * cg / max_rgb);
  313.         cvb = (0xffff * cb / max_rgb);
  314.         if (abs((int)r - (int)cvr) < xdev->half_dev_color &&
  315.         abs((int)g - (int)cvg) < xdev->half_dev_color &&
  316.         abs((int)b - (int)cvb) < xdev->half_dev_color) {
  317.         return xdev->dither_colors[cube_index(cr, cg, cb)];
  318.         }
  319. #undef max_rgb
  320.     } else {
  321.         unsigned short cr;
  322.         unsigned short cvr;
  323.  
  324. #define max_gray (xdev->color_info.dither_gray - 1)
  325.         cr = r * (xdev->color_info.dither_gray) / cv_denom;
  326.         cvr = (0xffff * cr / max_gray) & xdev->color_mask;
  327.         if (abs((int)r - (int)cvr) < xdev->half_dev_color)
  328.         return xdev->dither_colors[cr];
  329. #undef max_gray
  330.     }
  331.     }
  332. #undef cv_denom
  333.     /* Finally look through the list of dynamic colors */
  334.     if (xdev->dynamic_colors) {
  335.     int i;
  336.     XColor *xcp = xdev->dynamic_colors;
  337.     XColor xc;
  338.  
  339.     for (i = 0; i < xdev->dynamic_number; xcp++, i++) {
  340.         if (xcp->red == dr && xcp->green == dg && xcp->blue == db) {
  341.         return xcp->pixel;
  342.         }
  343.     }
  344.  
  345.     /* If not in our list of dynamic colors, ask the X server and */
  346.     /* add an entry if there is room. */
  347.     xc.red = dr;
  348.     xc.green = dg;
  349.     xc.blue = db;
  350.     if (XAllocColor(xdev->dpy, xdev->cmap, &xc)) {
  351.         if (xdev->dynamic_number < xdev->dynamic_size) {
  352.         xcp->red = dr;
  353.         xcp->green = dg;
  354.         xcp->blue = db;
  355.         xcp->pixel = xc.pixel;
  356.         xdev->dynamic_number++;
  357.         }
  358.         return xc.pixel;
  359.     } else {
  360.         return gx_no_color_index;
  361.     }
  362.     }
  363.     return gx_no_color_index;
  364. }
  365.  
  366.  
  367. /* Map a "device color" back to r-g-b. */
  368. /* This doesn't happed often, so we just ask the display */
  369. /* Foreground and background be mapped to other colors, so */
  370. /* they are handled specially. */
  371. private int
  372. x_map_color_rgb(register gx_device *dev, gx_color_index color,
  373.         gx_color_value prgb[3])
  374. {
  375.     gx_device_X *xdev = (gx_device_X *)dev;
  376.  
  377.     if (color == xdev->foreground)
  378.     prgb[0] = prgb[1] = prgb[2] = 0;
  379.     else if (color == xdev->background)
  380.     prgb[0] = prgb[1] = prgb[2] = gx_max_color_value;
  381.     else {
  382.     XColor xc;
  383.  
  384.     xc.pixel = color;
  385.     XQueryColor(xdev->dpy, xdev->cmap, &xc);
  386.     prgb[0] = xc.red;
  387.     prgb[1] = xc.green;
  388.     prgb[2] = xc.blue;
  389.     }
  390.     return 0;
  391. }
  392.  
  393. /* Get initial matrix for X device */
  394. private void
  395. x_get_initial_matrix(register gx_device *dev, register gs_matrix *pmat)
  396. {
  397.     gx_device_X *xdev = (gx_device_X *)dev;
  398.  
  399.     pmat->xx = xdev->initial_matrix.xx;
  400.     pmat->xy = xdev->initial_matrix.xy;
  401.     pmat->yx = xdev->initial_matrix.yx;
  402.     pmat->yy = xdev->initial_matrix.yy;
  403.     pmat->tx = xdev->initial_matrix.tx;
  404.     pmat->ty = xdev->initial_matrix.ty;
  405. }
  406.  
  407. /* Synchronize the display with the commands already given */
  408. private int
  409. x_sync(register gx_device *dev)
  410. {
  411.     gx_device_X *xdev = (gx_device_X *)dev;
  412.  
  413.     update_flush(dev);
  414.     XSync(xdev->dpy, 0);
  415.     return 0;
  416. }
  417.  
  418. /* Send event to ghostview process */
  419. private void
  420. x_send_event(gx_device *dev, Atom msg)
  421. {
  422.     gx_device_X *xdev = (gx_device_X *)dev;
  423.     XEvent event;
  424.  
  425.     event.xclient.type = ClientMessage;
  426.     event.xclient.display = xdev->dpy;
  427.     event.xclient.window = xdev->win;
  428.     event.xclient.message_type = msg;
  429.     event.xclient.format = 32;
  430.     event.xclient.data.l[0] = xdev->mwin;
  431.     event.xclient.data.l[1] = xdev->dest;
  432.     XSendEvent(xdev->dpy, xdev->win, False, 0, &event);
  433. }
  434.  
  435. /* Output "page" */
  436. private int
  437. x_output_page(gx_device *dev, int num_copies, int flush)
  438. {
  439.     gx_device_X *xdev = (gx_device_X *)dev;
  440.  
  441.     x_sync(dev);
  442.  
  443.     /* Send ghostview a "page" client event */
  444.     /* Wait for a "next" client event */
  445.     if (xdev->ghostview) {
  446.     XEvent event;
  447.  
  448.     x_send_event(dev, xdev->page);
  449.     XNextEvent(xdev->dpy, &event);
  450.     while (event.type != ClientMessage ||
  451.            event.xclient.message_type != xdev->next) {
  452.         XNextEvent(xdev->dpy, &event);
  453.     }
  454.     }
  455.     return 0;
  456. }
  457.  
  458. /* Fill a rectangle with a color. */
  459. private int
  460. x_fill_rectangle(register gx_device *dev,
  461.          int x, int y, int w, int h, gx_color_index color)
  462. {
  463.     gx_device_X *xdev = (gx_device_X *)dev;
  464.  
  465.     fit_fill(dev, x, y, w, h);
  466.     set_fill_style(FillSolid);
  467.     set_fore_color(color);
  468.     set_function(GXcopy);
  469.     XFillRectangle(xdev->dpy, xdev->dest, xdev->gc, x, y, w, h);
  470.     /* If we are filling the entire screen, reset */
  471.     /* colors_or and colors_and.  It's wasteful to do this */
  472.     /* on every operation, but there's no separate driver routine */
  473.     /* for erasepage (yet). */
  474.     if (x == 0 && y == 0 && w == xdev->width && h == xdev->height) {
  475.     if (color == xdev->foreground || color == xdev->background) {
  476.         int i;
  477.         XErrorHandler oldhandler = XSetErrorHandler(x_catch_free_colors);
  478.         for (i = 0; i < xdev->dynamic_number; i++) {
  479.         XFreeColors(xdev->dpy, xdev->cmap,
  480.                 &xdev->dynamic_colors[i].pixel, 1, 0);
  481.         }
  482.         XSync(xdev->dpy, False);    /* Force any errors */
  483.         oldhandler = XSetErrorHandler(oldhandler);
  484.         xdev->dynamic_number = 0;
  485.     }
  486.     xdev->colors_or = xdev->colors_and = color;
  487.     }
  488.     if (xdev->bpixmap != (Pixmap) 0) {
  489.     x_update_add(dev, x, y, w, h);
  490.     }
  491. #ifdef DEBUG
  492.     if (gs_debug['F'])
  493.     dprintf5("[F] fill (%d,%d):(%d,%d) %ld\n",
  494.          x, y, w, h, (long)color);
  495. #endif
  496.     return 0;
  497. }
  498.  
  499. /* Tile a rectangle. */
  500. private int
  501. x_tile_rectangle(register gx_device *dev, const gx_bitmap *tile,
  502.            int x, int y, int w, int h,
  503.          gx_color_index zero, gx_color_index one,
  504.          int px, int py)
  505. {
  506.     gx_device_X *xdev = (gx_device_X *)dev;
  507.  
  508.     fit_fill(dev, x, y, w, h);
  509.  
  510.     /* Check for a colored tile.  We should implement this */
  511.     /* properly someday, since X can handle it. */
  512.  
  513.     if (one == gx_no_color_index && zero == gx_no_color_index)
  514.     return -1;
  515.  
  516.     /* For the moment, give up if the phase is non-zero. */
  517.     if (px | py)
  518.     return -1;
  519.  
  520.     /*
  521.      * Remember, an X tile is already filled with particular
  522.      * pixel values (i.e., colors).  Therefore if we are changing
  523.      * fore/background color, we must invalidate the tile (using
  524.      * the same technique as in set_tile).  This problem only
  525.      * bites when using grayscale -- you may want to change
  526.      * fg/bg but use the same halftone screen.
  527.      */
  528.     if ((zero != xdev->ht.back_c) || (one != xdev->ht.fore_c))
  529.     xdev->ht.id = ~tile->id;    /* force reload */
  530.  
  531.     set_back_color(zero);
  532.     set_fore_color(one);
  533.     if (!set_tile(dev, tile)) {
  534.     /* Bad news.  Fall back to the default algorithm. */
  535.     return gx_default_tile_rectangle(dev, tile, x, y, w, h,
  536.                      zero, one, px, py);
  537.     } else {    /* Use the tile to fill the rectangle */
  538.     set_fill_style(FillTiled);
  539.     set_function(GXcopy);
  540.     XFillRectangle(xdev->dpy, xdev->dest, xdev->gc, x, y, w, h);
  541.     if (xdev->bpixmap != (Pixmap) 0) {
  542.         x_update_add(dev, x, y, w, h);
  543.     }
  544.     }
  545. #ifdef DEBUG
  546.     if (gs_debug['F'])
  547.     dprintf6("[F] tile (%d,%d):(%d,%d) %ld,%ld\n",
  548.          x, y, w, h, (long)zero, (long)one);
  549. #endif
  550.     return 0;
  551. }
  552.  
  553. /* Set up with a specified tile. */
  554. /* Return false if we can't do it for some reason. */
  555. private int
  556. set_tile(register gx_device *dev, register const gx_bitmap *tile)
  557. {
  558.     gx_device_X *xdev = (gx_device_X *)dev;
  559.  
  560. #ifdef DEBUG
  561.     if (gs_debug['T'])
  562.     return 0;
  563. #endif
  564.     if (tile->id == xdev->ht.id && tile->id != gx_no_bitmap_id)
  565.     return xdev->useXSetTile;
  566.     /* Set up the tile Pixmap */
  567.     if (tile->size.x != xdev->ht.width ||
  568.     tile->size.y != xdev->ht.height ||
  569.     xdev->ht.pixmap == (Pixmap) 0) {
  570.     if (xdev->ht.pixmap != (Pixmap) 0)
  571.         XFreePixmap(xdev->dpy, xdev->ht.pixmap);
  572.     xdev->ht.pixmap = XCreatePixmap(xdev->dpy, xdev->win,
  573.                     tile->size.x, tile->size.y,
  574.                     xdev->vinfo->depth);
  575.     if (xdev->ht.pixmap == (Pixmap) 0)
  576.         return 0;
  577.     xdev->ht.width = tile->size.x, xdev->ht.height = tile->size.y;
  578.     xdev->ht.raster = tile->raster;
  579.     }
  580.     xdev->ht.fore_c = xdev->fore_color;
  581.     xdev->ht.back_c = xdev->back_color;
  582.     /* Copy the tile into the Pixmap */
  583.     xdev->image.data = (char *)tile->data;
  584.     xdev->image.width = tile->size.x;
  585.     xdev->image.height = tile->size.y;
  586.     xdev->image.bytes_per_line = tile->raster;
  587.     xdev->image.format = XYBitmap;
  588.     set_fill_style(FillSolid);
  589. #ifdef DEBUG
  590.     if (gs_debug['H']) {
  591.     int i;
  592.  
  593.     dprintf4("[H] 0x%x: width=%d height=%d raster=%d\n",
  594.          tile->data, tile->size.x, tile->size.y, tile->raster);
  595.     for (i = 0; i < tile->raster * tile->size.y; i++)
  596.         dprintf1(" %02x", tile->data[i]);
  597.     dputc('\n');
  598.     }
  599. #endif
  600.     XSetTile(xdev->dpy, xdev->gc, xdev->ht.no_pixmap);    /* *** X bug *** */
  601.     set_function(GXcopy);
  602.     put_image(xdev->dpy, xdev->ht.pixmap, xdev->gc, &xdev->image,
  603.           0, 0, 0, 0, tile->size.x, tile->size.y);
  604.     XSetTile(xdev->dpy, xdev->gc, xdev->ht.pixmap);
  605.     xdev->ht.id = tile->id;
  606.     return xdev->useXSetTile;
  607. }
  608.  
  609. /* Copy a monochrome bitmap. */
  610. private int
  611. x_copy_mono(register gx_device *dev,
  612.         const byte *base, int sourcex, int raster, gx_bitmap_id id,
  613.         int x, int y, int w, int h,
  614.         gx_color_index zero, gx_color_index one)
  615. /*
  616.  * X doesn't directly support the simple operation of writing a color
  617.  * through a mask specified by an image.  The plot is the following:
  618.  *  If neither color is gx_no_color_index ("transparent"),
  619.  *    use XPutImage with the "copy" function as usual.
  620.  *  If the color either bitwise-includes or is bitwise-included-in
  621.  *      every color written to date
  622.  *      (a special optimization for writing black/white on color displays),
  623.  *    use XPutImage with an appropriate Boolean function.
  624.  *  Otherwise, do the following complicated stuff:
  625.  *    Create pixmap of depth 1 if necessary.
  626.  *    If foreground color is "transparent" then
  627.  *      invert the raster data.
  628.  *    Use XPutImage to copy the raster image to the newly
  629.  *      created Pixmap.
  630.  *    Install the Pixmap as the clip_mask in the X GC and
  631.  *      tweak the clip origin.
  632.  *    Do an XFillRectangle, fill style=solid, specifying a
  633.  *      rectangle the same size as the original raster data.
  634.  *    De-install the clip_mask.
  635.  */
  636. {
  637.     gx_device_X *xdev = (gx_device_X *)dev;
  638.     int function = GXcopy;
  639.  
  640.     x_pixel
  641.     bc = zero,
  642.     fc = one;
  643.  
  644.     fit_copy(dev, base, sourcex, raster, id, x, y, w, h);
  645.  
  646.     xdev->image.width = raster << 3;
  647.     xdev->image.height = h;
  648.     xdev->image.data = (char *)base;
  649.     xdev->image.bytes_per_line = raster;
  650.     set_fill_style(FillSolid);
  651.  
  652.     /* Check for null, easy 1-color, hard 1-color, and 2-color cases. */
  653.     if (zero != gx_no_color_index) {
  654.     if (one != gx_no_color_index) {
  655.         /* 2-color case. */
  656.         /* Simply replace existing bits with what's in the image. */
  657.     } else if (!(~xdev->colors_and & bc)) {
  658.         function = GXand;
  659.         fc = ~(x_pixel) 0;
  660.     } else if (!(~bc & xdev->colors_or)) {
  661.         function = GXor;
  662.         fc = 0;
  663.     } else {
  664.         goto hard;
  665.     }
  666.     } else {
  667.     if (one == gx_no_color_index) {    /* no-op */
  668.         return 0;
  669.     } else if (!(~xdev->colors_and & fc)) {
  670.         function = GXand;
  671.         bc = ~(x_pixel) 0;
  672.     } else if (!(~fc & xdev->colors_or)) {
  673.         function = GXor;
  674.         bc = 0;
  675.     } else {
  676.         goto hard;
  677.     }
  678.     }
  679.     xdev->image.format = XYBitmap;
  680.     set_function(function);
  681.     if (bc != xdev->back_color) {
  682.     XSetBackground(xdev->dpy, xdev->gc, (xdev->back_color = bc));
  683.     }
  684.     if (fc != xdev->fore_color) {
  685.     XSetForeground(xdev->dpy, xdev->gc, (xdev->fore_color = fc));
  686.     }
  687.     if (zero != gx_no_color_index)
  688.     note_color(zero);
  689.     if (one != gx_no_color_index)
  690.     note_color(one);
  691.     put_image(xdev->dpy, xdev->dest, xdev->gc, &xdev->image,
  692.           sourcex, 0, x, y, w, h);
  693.  
  694.     goto out;
  695.  
  696. hard:    /* Handle the hard 1-color case. */
  697.     if (raster > xdev->cp.raster || h > xdev->cp.height) {
  698.     /* Must allocate a new pixmap and GC. */
  699.     /* Release the old ones first. */
  700.     free_cp(dev);
  701.  
  702.     /* Create the clipping pixmap, depth must be 1. */
  703.     xdev->cp.pixmap =
  704.         XCreatePixmap(xdev->dpy, xdev->win, raster << 3, h, 1);
  705.     if (xdev->cp.pixmap == (Pixmap) 0) {
  706.         lprintf("x_copy_mono: can't allocate pixmap\n");
  707.         exit(1);
  708.     }
  709.     xdev->cp.gc = XCreateGC(xdev->dpy, xdev->cp.pixmap, 0, 0);
  710.     if (xdev->cp.gc == (GC) 0) {
  711.         lprintf("x_copy_mono: can't allocate GC\n");
  712.         exit(1);
  713.     }
  714.     xdev->cp.raster = raster;
  715.     xdev->cp.height = h;
  716.     }
  717.     /* Initialize static mask image params */
  718.     xdev->image.format = ZPixmap;
  719.     set_function(GXcopy);
  720.  
  721.     /* Select polarity based on fg/bg transparency. */
  722.     if (one == gx_no_color_index) {    /* invert */
  723.     XSetBackground(xdev->dpy, xdev->cp.gc, (x_pixel) 1);
  724.     XSetForeground(xdev->dpy, xdev->cp.gc, (x_pixel) 0);
  725.     set_fore_color(zero);
  726.     } else {
  727.     XSetBackground(xdev->dpy, xdev->cp.gc, (x_pixel) 0);
  728.     XSetForeground(xdev->dpy, xdev->cp.gc, (x_pixel) 1);
  729.     set_fore_color(one);
  730.     }
  731.     put_image(xdev->dpy, xdev->cp.pixmap, xdev->cp.gc,
  732.           &xdev->image, sourcex, 0, 0, 0, w, h);
  733.  
  734.     /* Install as clipmask. */
  735.     XSetClipMask(xdev->dpy, xdev->gc, xdev->cp.pixmap);
  736.     XSetClipOrigin(xdev->dpy, xdev->gc, x, y);
  737.  
  738.     /*
  739.      * Draw a solid rectangle through the raster clip mask.
  740.      * Note fill style is guaranteed to be solid from above.
  741.      */
  742.     XFillRectangle(xdev->dpy, xdev->dest, xdev->gc, x, y, w, h);
  743.  
  744.     /* Tidy up.  Free the pixmap if it's big. */
  745.     XSetClipMask(xdev->dpy, xdev->gc, None);
  746.     if (raster * h > max_temp_pixmap)
  747.     free_cp(dev);
  748.  
  749. out:if (xdev->bpixmap != (Pixmap) 0) {
  750.     /* We wrote to the pixmap, so update the display now. */
  751.     x_update_add(dev, x, y, w, h);
  752.     }
  753.     return 0;
  754. }
  755.  
  756. /* Internal routine to free the GC and pixmap used for copying. */
  757. private void
  758. free_cp(register gx_device *dev)
  759. {
  760.     gx_device_X *xdev = (gx_device_X *)dev;
  761.  
  762.     if (xdev->cp.gc != NULL) {
  763.     XFreeGC(xdev->dpy, xdev->cp.gc);
  764.     xdev->cp.gc = NULL;
  765.     }
  766.     if (xdev->cp.pixmap != (Pixmap) 0) {
  767.     XFreePixmap(xdev->dpy, xdev->cp.pixmap);
  768.     xdev->cp.pixmap = (Pixmap) 0;
  769.     }
  770.     xdev->cp.raster = -1;    /* mark as unallocated */
  771. }
  772.  
  773. /* Copy a "color" bitmap.  Since "color" is the same as monochrome, */
  774. /* this just reduces to copying a monochrome bitmap. */
  775. /****** THIS ROUTINE IS COMPLETELY WRONG, SINCE WE DO SUPPORT COLOR. ******/
  776. /* Fortunately, no one uses it at the moment. */
  777. private int
  778. x_copy_color(register gx_device *dev,
  779.          const byte *base, int sourcex, int raster, gx_bitmap_id id,
  780.          int x, int y, int w, int h)
  781. {
  782.     gx_device_X *xdev = (gx_device_X *)dev;
  783.  
  784.     return x_copy_mono(dev, base, sourcex, raster, id, x, y, w, h,
  785.                xdev->foreground, xdev->background);
  786. }
  787.  
  788. /* Draw a line */
  789. private int
  790. x_draw_line(register gx_device *dev,
  791.         int x0, int y0, int x1, int y1, gx_color_index color)
  792. {
  793.     gx_device_X *xdev = (gx_device_X *)dev;
  794.  
  795.     set_fore_color(color);
  796.     set_fill_style(FillSolid);
  797.     set_function(GXcopy);
  798.     XDrawLine(xdev->dpy, xdev->dest, xdev->gc, x0, y0, x1, y1);
  799.     if (xdev->bpixmap != (Pixmap) 0) {
  800.     int x = x0, y = y0, w = x1 - x0, h = y1 - y0;
  801.  
  802.     if (w < 0) x = x1, w = -w;
  803.     if (h < 0) y = y1, h = -h;
  804.     w++;
  805.     h++;
  806.     fit_fill(dev, x, y, w, h);
  807.     x_update_add(dev, x, y, w, h);
  808.     }
  809.     return 0;
  810. }
  811.  
  812. /* Set the device properties.  We reimplement this so we can resize */
  813. /* the window and avoid closing and reopening the device. */
  814. private const gs_prop_item x_props[] =
  815. {
  816.     prop_def("HWResolution", prt_float_array),
  817.     prop_def("HWSize", prt_int_array),
  818.         /* Slots for arrays */
  819.     prop_float, prop_float,
  820.     prop_int, prop_int,
  821. };
  822.  
  823. private int
  824. x_put_props(gx_device *dev, gs_prop_item *plist, int count)
  825. {
  826.     gx_device_X *xdev = (gx_device_X *)dev;
  827.     gs_prop_item *known[2];
  828.     int code = 0;
  829.     gx_device_X temp_dev;
  830.  
  831.     temp_dev = *xdev;
  832.     props_extract(plist, count, x_props, 2, known, 0);
  833.     if (known[1] != 0) {
  834.     if (known[1]->value.a.size != 2)
  835.         known[1]->status = pv_typecheck,
  836.         code = gs_error_typecheck;
  837.     else {
  838.         gs_prop_item *ap = known[1]->value.a.p.v;
  839.  
  840.         if (ap[0].value.i <= 0 || ap[0].value.i > 0x7fff ||
  841.         ap[1].value.i <= 0 || ap[1].value.i > 0x7fff)
  842.         known[1]->status = pv_rangecheck,
  843.         code = gs_error_rangecheck;
  844.         else {
  845.         temp_dev.width = ap[0].value.i;
  846.         temp_dev.height = ap[1].value.i;
  847.         }
  848.     }
  849.     }
  850.     if (known[0] != 0) {
  851.     if (known[0]->value.a.size != 2)
  852.         known[0]->status = pv_typecheck,
  853.         code = gs_error_typecheck;
  854.     else {
  855.         gs_prop_item *ap = known[0]->value.a.p.v;
  856.  
  857.         if (ap[0].value.f <= 0 || ap[1].value.f <= 0)
  858.         known[0]->status = pv_rangecheck,
  859.         code = gs_error_rangecheck;
  860.         else {
  861.         temp_dev.x_pixels_per_inch = ap[0].value.f;
  862.         temp_dev.y_pixels_per_inch = ap[1].value.f;
  863.         }
  864.     }
  865.     }
  866.     if (code < 0) return_error(code);
  867.     dev->x_pixels_per_inch = temp_dev.x_pixels_per_inch;
  868.     dev->y_pixels_per_inch = temp_dev.y_pixels_per_inch;
  869.     dev->width = temp_dev.width;
  870.     dev->height = temp_dev.height;
  871.     /* If the device is open, resize the window. */
  872.     /* Don't do this if Ghostview is active. */
  873.     if (dev->is_open && (known[0] != 0 || known[1] != 0) && !xdev->ghostview) {
  874.     XResizeWindow(xdev->dpy, xdev->win, dev->width, dev->height);
  875.     if (xdev->bpixmap != (Pixmap) 0) {
  876.         XFreePixmap(xdev->dpy, xdev->bpixmap);
  877.         xdev->bpixmap = (Pixmap) 0;
  878.     }
  879.     xdev->dest = 0;
  880.     gdev_x_clear_window(xdev);
  881.     }
  882.     return gx_default_put_props(dev, plist, count);
  883. }
  884.  
  885.  
  886. /* ------ Screen update procedures ------ */
  887.  
  888. /* Flush updates to the screen if needed. */
  889. private void
  890. update_do_flush(register gx_device *dev)
  891. {
  892.     gx_device_X *xdev = (gx_device_X *)dev;
  893.     int xo = xdev->update.xo, yo = xdev->update.yo;
  894.  
  895.     set_function(GXcopy);
  896.     XCopyArea(xdev->dpy, xdev->bpixmap, xdev->win, xdev->gc,
  897.           xo, yo, xdev->update.xe - xo, xdev->update.ye - yo,
  898.           xo, yo);
  899.     update_init(dev);
  900. }
  901.  
  902. /* Add a region to be updated. */
  903. /* This is only called if xdev->bpixmap != 0. */
  904. void
  905. x_update_add(register gx_device *dev, int xo, int yo, int w, int h)
  906. {
  907.     gx_device_X *xdev = (gx_device_X *)dev;
  908.     int xe = xo + w, ye = yo + h;
  909.     long new_area = (long)w * h;
  910.  
  911.     ++xdev->up_count;
  912.     if (xdev->up_area != 0) {    /* See whether adding this rectangle */
  913.     /* would result in too much being copied unnecessarily. */
  914.     long old_area = xdev->up_area;
  915.     long new_up_area;
  916.     rect u;
  917.  
  918.     u.xo = min(xo, xdev->update.xo);
  919.     u.yo = min(yo, xdev->update.yo);
  920.     u.xe = max(xe, xdev->update.xe);
  921.     u.ye = max(ye, xdev->update.ye);
  922.     new_up_area = (long)(u.xe - u.xo) * (u.ye - u.yo);
  923.     if (new_up_area > 100 &&
  924.         old_area + new_area < new_up_area * 2 / 3 ||
  925.         xdev->up_count >= 200)
  926.         update_do_flush(dev);
  927.     else {
  928.         xdev->update = u;
  929.         xdev->up_area = new_up_area;
  930.         return;
  931.     }
  932.     }
  933.     xdev->update.xo = xo;
  934.     xdev->update.yo = yo;
  935.     xdev->update.xe = xe;
  936.     xdev->update.ye = ye;
  937.     xdev->up_area = new_area;
  938. }
  939.  
  940. /* ------ Internal procedures ------ */
  941.  
  942. /* Substitute for XPutImage using XFillRectangle. */
  943. /* This is a total hack to get around an apparent bug */
  944. /* in some X servers.  It only works with the specific */
  945. /* parameters (bit/byte order, padding) used above. */
  946. private void
  947. alt_put_image(gx_device *dev, Display *dpy, Drawable win, GC gc,
  948.       XImage *pi, int sx, int sy, int dx, int dy, unsigned w, unsigned h)
  949. {
  950.     gx_device_X *xdev = (gx_device_X *)dev;
  951.     int raster = pi->bytes_per_line;
  952.     byte *data = (byte *) pi->data + sy * raster + (sx >> 3);
  953.     int init_mask = 0x80 >> (sx & 7);
  954.     int invert = 0;
  955.     int yi;
  956.  
  957. #define nrects 40
  958.     XRectangle rects[nrects];
  959.     XRectangle *rp = rects;
  960.  
  961.     XGCValues gcv;
  962.  
  963.     XGetGCValues(dpy, gc, (GCFunction | GCForeground | GCBackground), &gcv);
  964.  
  965.     if (gcv.function == GXcopy) {
  966.     XSetForeground(dpy, gc, gcv.background);
  967.     XFillRectangle(dpy, win, gc, dx, dy, w, h);
  968.     XSetForeground(dpy, gc, gcv.foreground);
  969.     } else if (gcv.function == GXand) {
  970.     if (gcv.background != ~(x_pixel) 0) {
  971.         XSetForeground(dpy, gc, gcv.background);
  972.         invert = 0xff;
  973.     }
  974.     } else if (gcv.function == GXor) {
  975.     if (gcv.background != 0) {
  976.         XSetForeground(dpy, gc, gcv.background);
  977.         invert = 0xff;
  978.     }
  979.     } else {
  980.     lprintf("alt_put_image: unimplemented function.\n");
  981.     exit(1);
  982.     }
  983.  
  984.     for (yi = 0; yi < h; yi++, data += raster) {
  985.     register int mask = init_mask;
  986.     register byte *dp = data;
  987.     register int xi = 0;
  988.  
  989.     while (xi < w) {
  990.         if ((*dp ^ invert) & mask) {
  991.         int xleft = xi;
  992.  
  993.         if (rp == &rects[nrects]) {
  994.             XFillRectangles(dpy, win, gc, rects, nrects);
  995.             rp = rects;
  996.         }
  997.         /* Scan over a run of 1-bits */
  998.         rp->x = dx + xi, rp->y = dy + yi;
  999.         do {
  1000.             if (!(mask >>= 1))
  1001.             mask = 0x80, dp++;
  1002.             xi++;
  1003.         } while (xi < w && (*dp & mask));
  1004.         rp->width = xi - xleft, rp->height = 1;
  1005.         rp++;
  1006.         } else {
  1007.         if (!(mask >>= 1))
  1008.             mask = 0x80, dp++;
  1009.         xi++;
  1010.         }
  1011.     }
  1012.     }
  1013.     XFillRectangles(dpy, win, gc, rects, rp - rects);
  1014.     if (invert) XSetForeground(dpy, gc, gcv.foreground);
  1015. }
  1016.