|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T g
Length: 15494 (0x3c86) Types: TextFile Names: »gdevx.c«
└─⟦a05ed705a⟧ Bits:30007078 DKUUG GNU 2/12/89 └─⟦ff23ba0e6⟧ »./ghostscript-1.3.tar.Z« └─⟦a24a58cd3⟧ └─⟦this⟧ »gdevx.c«
/* Copyright (C) 1989 Aladdin Enterprises. All rights reserved. Distributed by Free Software Foundation, Inc. This file is part of Ghostscript. Ghostscript is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY. No author or distributor accepts responsibility to anyone for the consequences of using it or for whether it serves any particular purpose or works at all, unless he says so in writing. Refer to the Ghostscript General Public License for full details. Everyone is granted permission to copy, modify and redistribute Ghostscript, but only under the conditions described in the Ghostscript General Public License. A copy of this license is supposed to have been given to you along with Ghostscript so you can know your rights and responsibilities. It should be in a file named COPYING. Among other things, the copyright notice and this notice must be preserved on all copies. */ /* gdevx.c */ /* X Windows driver for GhostScript library */ #include <X11/Xlib.h> #include <X11/Xatom.h> #include <X11/Xutil.h> #include <stdio.h> /* Hack to get around the fact that something in the X library */ /* defines uint and ushort.... */ # define uint _uint # define ushort _ushort #include "gx.h" /* for gx_bitmap; includes std.h */ # undef _uint # undef _ushort #include "malloc_.h" #include "gsmatrix.h" /* needed for gxdevice.h */ #include "gxbitmap.h" #include "gxdevice.h" typedef struct gx_device_s gx_device; /* Flags for patching around bugs in the X library */ #define use_XPutImage 0 #define use_XSetTile 0 /* Procedures */ int x_open(P1(gx_device *)); int x_close(P1(gx_device *)); gx_color_index x_map_rgb_color(P4(gx_device *, ushort, ushort, ushort)); int x_map_color_rgb(P3(gx_device *, gx_color_index, ushort *)); int x_sync(P1(gx_device *)); int x_fill_rectangle(P6(gx_device *, int, int, int, int, gx_color_index)); int x_tile_rectangle(P8(gx_device *, gx_bitmap *, int, int, int, int, gx_color_index, gx_color_index)); int x_copy_mono(P10(gx_device *, byte *, int, int, int, int, int, int, gx_color_index, gx_color_index)); int x_copy_color(P9(gx_device *, byte *, int, int, int, int, int, int, int)); int x_draw_line(P6(gx_device *, int, int, int, int, gx_color_index)); int x_fill_trapezoid(P8(gx_device *, int, int, int, int, int, int, gx_color_index)); /* The device descriptor */ private gx_device_procs x_procs = { x_open, x_close, x_map_rgb_color, x_map_color_rgb, x_sync, x_fill_rectangle, x_tile_rectangle, x_copy_mono, x_copy_color, x_draw_line, x_fill_trapezoid, gx_default_tile_trapezoid }; /* Define the X Windows device */ typedef struct gx_device_X_s { gx_device_common; /* An XIMage object for writing bitmap images to the screen */ XImage image; /* Global X state */ Display *dpy; Screen *scr; Visual *vis; Colormap cmap; Window win; GC gc; /* Structure for dealing with the halftone tile. */ /* Later this will become a multi-element cache. */ struct { Pixmap pixmap; Pixmap no_pixmap; /* kludge to get around X bug */ int width, height, raster; byte *bits; int bits_size; } ht; /* Cache the fill style from the GC */ int fill_style; #define set_fill_style(style)\ if ( xdev->fill_style != style )\ XSetFillStyle(xdev->dpy, xdev->gc, (xdev->fill_style = style)) /* Map color indices to X pixel values */ unsigned long colors[8]; #define black colors[0] #define white colors[7] gx_color_index back_color, fore_color; #define set_back_color(color)\ if ( xdev->back_color != color )\ XSetBackground(xdev->dpy, xdev->gc, xdev->colors[xdev->back_color = color]) #define set_fore_color(color)\ if ( xdev->fore_color != color )\ XSetForeground(xdev->dpy, xdev->gc, xdev->colors[xdev->fore_color = color]) } gx_device_X; /* The instance is public. */ gx_device_X x_device = { sizeof(gx_device_X), &x_procs, identity_matrix_body, 640, 350, /* x and y extent */ /* Following parameters are initialized for monochrome */ 0, /* has color */ 1, /* max r-g-b value */ 1, /* bits per color pixel */ /* End of monochrome/color parameters */ 1, /* bit-big-endian (for now) */ 0, /* connection not initialized */ { 0, 0, /* width, height */ 0, XYBitmap, NULL, /* xoffset, format, data */ LSBFirst, 8, /* byte-order, bitmap-unit */ MSBFirst, 8, 1 /* bitmap-bit-order, bitmap-pad, depth */ } }; /* Macro for casting gx_device argument */ #define xdev ((gx_device_X *)dev) /* Macro to validate arguments */ #define check_rect()\ if ( w <= 0 || h <= 0 ) return 0;\ if ( x < 0 || x > xdev->width - w || y < 0 || y > xdev->height - h )\ return -1 #if !use_XPutImage /* XPutImage doesn't work, do it ourselves. */ # undef XPutImage private void alt_put_image(); # define XPutImage(dpy,win,gc,im,sx,sy,x,y,w,h)\ alt_put_image(dev,dpy,win,gc,im,sx,sy,x,y,w,h) #endif int x_open(register gx_device *dev) { XSetWindowAttributes xswa; int winW, winH; int winX, winY; #ifdef DEBUG if ( gs_debug['X'] ) { extern int _Xdebug; _Xdebug = 1; } #endif if ( !(xdev->dpy = XOpenDisplay(NULL)) ) { printf("Cannot open display\n"); exit(1); } xdev->scr = DefaultScreenOfDisplay(xdev->dpy); xdev->vis = DefaultVisualOfScreen(xdev->scr); xdev->cmap = DefaultColormapOfScreen(xdev->scr); /* Make the window half the screen (vertically oriented) */ winW = WidthOfScreen(xdev->scr) >> 1; winX = winW; xdev->width = winW; /* Leave some space for a window manager's title bar */ winH = HeightOfScreen(xdev->scr) - 15; winY = 0; xdev->height = winH; xdev->black = BlackPixelOfScreen(xdev->scr); xdev->white = WhitePixelOfScreen(xdev->scr); xdev->back_color = xdev->fore_color = -1; /* undefined */ /* Figure out monochrome vs. color */ switch ( xdev->vis->class ) { case StaticGray: case GrayScale: xdev->has_color = 0; { int i; for ( i = 1; i < 7; i++ ) xdev->colors[i] = xdev->white; } break; default: /* color */ xdev->has_color = 1; /* Just do primary colors for now */ { XColor xc; int i; for ( i = 1; i < 7; i++ ) { xc.red = (i & 4 ? (ushort)-1 : 0); xc.green = (i & 2 ? (ushort)-1 : 0); xc.blue = (i & 1 ? (ushort)-1 : 0); XAllocColor(xdev->dpy, xdev->cmap, &xc); xdev->colors[i] = xc.pixel; } } } xdev->ht.pixmap = (Pixmap)0; xdev->ht.bits = 0; xdev->fill_style = FillSolid; xswa.event_mask = 0; xswa.background_pixel = xdev->black; xdev->win = XCreateWindow(xdev->dpy, RootWindowOfScreen(xdev->scr), winX, winY, winW, winH, 0, DefaultDepthOfScreen(xdev->scr), InputOutput, xdev->vis, CWEventMask | CWBackPixel, &xswa); XChangeProperty(xdev->dpy, xdev->win, XA_WM_NAME, XA_STRING, 8, PropModeReplace, "Ghostscript", 11); XMapWindow(xdev->dpy, xdev->win); xdev->ht.no_pixmap = XCreatePixmap(xdev->dpy, xdev->win, 1, 1, DefaultDepthOfScreen(xdev->scr)); /* Set up a graphics context */ xdev->gc = XCreateGC(xdev->dpy, xdev->win, 0, NULL); XSetFunction(xdev->dpy, xdev->gc, GXcopy); XSetLineAttributes(xdev->dpy, xdev->gc, 0, LineSolid, CapButt, JoinMiter); XSync(xdev->dpy, 0); getchar(); /****** X LOSES OUTPUT OTHERWISE ******/ return 0; } /* Close the device. NOT SURE WHAT TO DO HERE YET. */ int x_close(gx_device *dev) { return 0; } /* Map a color. The "device colors" are just r,g,b packed together. */ gx_color_index x_map_rgb_color(register gx_device *dev, ushort r, ushort g, ushort b) { return (r << 2) + (g << 1) + b; } /* Map a "device color" back to r-g-b. */ int x_map_color_rgb(register gx_device *dev, gx_color_index color, ushort *prgb) { prgb[0] = (color >> 2) & 1; prgb[1] = (color >> 1) & 1; prgb[2] = color & 1; } /* Synchronize the display with the commands already given */ int x_sync(register gx_device *dev) { XSync(xdev->dpy, 0); return 0; } /* Fill a rectangle with a color. */ int x_fill_rectangle(register gx_device *dev, int x, int y, int w, int h, gx_color_index color) { check_rect(); set_fill_style(FillSolid); set_fore_color(color); XFillRectangle(xdev->dpy, xdev->win, xdev->gc, x, y, w, h); #ifdef DEBUG if ( gs_debug['F'] ) printf("[F] fill (%d,%d):(%d,%d) %ld\n", x, y, w, h, (long)color); #endif return 0; } /* Tile a rectangle. */ int x_tile_rectangle(register gx_device *dev, gx_bitmap *tile, int x, int y, int w, int h, gx_color_index zero, gx_color_index one) { check_rect(); set_back_color(zero); set_fore_color(one); if ( !set_tile(dev, tile) ) alt_tile_rectangle(dev, tile, x, y, w, h); else { /* Use the tile to fill the rectangle */ XFillRectangle(xdev->dpy, xdev->win, xdev->gc, x, y, w, h); } #ifdef DEBUG if ( gs_debug['F'] ) printf("[F] tile (%d,%d):(%d,%d) %ld,%ld\n", x, y, w, h, (long)zero, (long)one); #endif return 0; } /* Set up with a specified tile. */ /* Return false if we can't do it for some reason. */ int set_tile(register gx_device *dev, register gx_bitmap *tile) { #ifdef DEBUG if ( gs_debug['T'] ) return 0; #endif /* Set up the tile Pixmap */ if ( tile->width != xdev->ht.width || tile->height != xdev->ht.height || xdev->ht.pixmap == (Pixmap)0 ) { if ( xdev->ht.pixmap != (Pixmap)0 ) XFreePixmap(xdev->dpy, xdev->ht.pixmap); if ( xdev->ht.bits ) { free(xdev->ht.bits); xdev->ht.bits = 0; } xdev->ht.pixmap = XCreatePixmap(xdev->dpy, xdev->win, tile->width, tile->height, DefaultDepthOfScreen(xdev->scr)); if ( xdev->ht.pixmap == (Pixmap)0 ) return 0; xdev->ht.bits_size = tile->raster * tile->height; xdev->ht.bits = (byte *)malloc(xdev->ht.bits_size); if ( xdev->ht.bits == 0 ) { XFreePixmap(xdev->dpy, xdev->ht.pixmap); xdev->ht.pixmap = (Pixmap)0; return 0; } xdev->ht.width = tile->width, xdev->ht.height = tile->height; xdev->ht.raster = tile->raster; *xdev->ht.bits = ~*tile->data; /* force copying */ } /* Copy the tile into the Pixmap if needed */ if ( memcmp(xdev->ht.bits, tile->data, xdev->ht.bits_size) ) { memcpy(xdev->ht.bits, tile->data, xdev->ht.bits_size); xdev->image.data = (char *)tile->data; xdev->image.width = tile->width; xdev->image.height = tile->height; xdev->image.bytes_per_line = tile->raster; set_fill_style(FillSolid); #ifdef DEBUG if ( gs_debug['H'] ) { int i; printf("[H] 0x%x: width=%d height=%d raster=%d\n", tile->data, tile->width, tile->height, tile->raster); for ( i = 0; i < tile->raster * tile->height; i++ ) printf(" %02x", tile->data[i]); printf("\n"); } #endif XSetTile(xdev->dpy, xdev->gc, xdev->ht.no_pixmap); /* *** X bug *** */ XPutImage(xdev->dpy, xdev->ht.pixmap, xdev->gc, &xdev->image, 0, 0, 0, 0, tile->width, tile->height); XSetTile(xdev->dpy, xdev->gc, xdev->ht.pixmap); } set_fill_style(FillTiled); return use_XSetTile; } /* Fallback code for tiling when we can't allocate the Pixmap, */ /* or XSetTile/XPutImage doesn't work. */ int alt_tile_rectangle(register gx_device *dev, gx_bitmap *tile, int x, int y, int w, int h) { int tile_width = tile->width, tile_height = tile->height; int ex = x + w, ey = y + h; int etx = ex - tile_width, ety = ey - tile_height; int tx, ty; int xmod = x % tile_width, ymod = y % tile_height; int th = tile_height - ymod; int tw = tile_width - xmod; if ( tw > w ) tw = w; if ( th > h ) th = h; xdev->image.data = (char *)tile->data; xdev->image.width = tile->width; xdev->image.height = tile->height; xdev->image.bytes_per_line = tile->raster; set_fill_style(FillSolid); for ( ty = y; ty < ey; ) { XPutImage(xdev->dpy, xdev->win, xdev->gc, &xdev->image, xmod, ymod, x, ty, tw, th); tx = x + tw; while ( tx <= etx ) { XPutImage(xdev->dpy, xdev->win, xdev->gc, &xdev->image, 0, ymod, tx, ty, tile_width, th); tx += tile_width; } if ( tx < ex ) XPutImage(xdev->dpy, xdev->win, xdev->gc, &xdev->image, 0, ymod, tx, ty, ex - tx, th); ty += th; th = (ty > ety ? ey - ty : tile_height); ymod = 0; } } /* Copy a monochrome bitmap. */ int x_copy_mono(register gx_device *dev, byte *base, int sourcex, int raster, int x, int y, int w, int h, gx_color_index zero, gx_color_index one) { check_rect(); xdev->image.width = raster << 3; xdev->image.height = h; xdev->image.data = (char *)base; xdev->image.bytes_per_line = raster; /****** HANDLE zero OR one=gx_no_color_index! ******/ set_back_color(zero); set_fore_color(one); set_fill_style(FillSolid); XPutImage(xdev->dpy, xdev->win, xdev->gc, &xdev->image, sourcex, 0, x, y, w, h); return 0; } /* Copy a "color" bitmap. Since "color" is the same as monochrome, */ /* this just reduces to copying a monochrome bitmap. */ int x_copy_color(register gx_device *dev, byte *base, int sourcex, int raster, int x, int y, int w, int h) { return x_copy_mono(dev, base, sourcex, raster, x, y, w, h, (gx_color_index)0, (gx_color_index)7); } /* Draw a line */ int x_draw_line(register gx_device *dev, int x0, int y0, int x1, int y1, gx_color_index color) { set_fore_color(color); XDrawLine(xdev->dpy, xdev->win, xdev->gc, x0, y0, x1, y1); return 0; } /* Fill a trapezoid */ int x_fill_trapezoid(register gx_device *dev, int x0, int y0, int w0, int x1, int y1, int w1, gx_color_index color) { XPoint vlist[4]; XPoint *pv = vlist + 1; vlist[0].x = x0, vlist[0].y = y0; if ( w0 ) pv->x = x0 + w0, pv->y = y0, pv++; pv->x = x1 + w1, pv->y = y1, pv++; if ( w1 ) pv->x = x1, pv->y = y1, pv++; set_fore_color(color); set_fill_style(FillSolid); XFillPolygon(xdev->dpy, xdev->win, xdev->gc, vlist, pv - vlist, Convex, CoordModeOrigin); return 0; } /* ------ Internal procedures ------ */ /* Substitute for XPutImage using XFillRectangle. */ /* This is a total hack to get around an apparent bug */ /* in the X server. It only works with the specific */ /* parameters (bit/byte order, padding) used above. */ private void alt_put_image(gx_device *dev, Display *dpy, Drawable win, GC gc, XImage *pi, int sx, int sy, int dx, int dy, unsigned w, unsigned h) { int raster = pi->bytes_per_line; byte *data = (byte *)pi->data + sy * raster + (sx >> 3); int init_mask = 0x80 >> (sx & 7); int invert; int yi; #define nrects 40 XRectangle rects[nrects]; XRectangle *rp = rects; if ( xdev->fore_color >= 0 ) { if ( xdev->back_color >= 0 ) { XSetForeground(dpy, gc, xdev->colors[xdev->back_color]); XFillRectangle(dpy, win, gc, dx, dy, w, h); } XSetForeground(dpy, gc, xdev->colors[xdev->fore_color]); invert = 0; } else if ( xdev->back_color >= 0 ) { XSetForeground(dpy, gc, xdev->colors[xdev->back_color]); invert = 0xff; } else return; for ( yi = 0; yi < h; yi++, data += raster ) { register int mask = init_mask; register byte *dp = data; register int xi = 0; while ( xi < w ) { if ( (*dp ^ invert) & mask ) { int xleft = xi; if ( rp == &rects[nrects] ) { XFillRectangles(dpy, win, gc, rects, nrects); rp = rects; } /* Scan over a run of 1-bits */ rp->x = dx + xi, rp->y = dy + yi; do { if ( !(mask >>= 1) ) mask = 0x80, dp++; xi++; } while ( xi < w && (*dp & mask) ); rp->width = xi - xleft, rp->height = 1; rp++; } else { if ( !(mask >>= 1) ) mask = 0x80, dp++; xi++; } } } XFillRectangles(dpy, win, gc, rects, rp - rects); }