[gnu.ghostscript.bug] Simpler implementatioin of GhostScript on SunView

aida@CSL.SRI.COM (Hitoshi Aida) (05/01/89)

I sent my port of GhostScript to SunView environment some time ago,
but this is more simple implementation.

Hitoshi AIDA (aida@csl.sri.com)
Computer Science Laboratory, SRI International

########################## begin file "gdevsun.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.  */

/* gdevsun.c */
/* SunView driver for GhostScript library */
#include <suntool/sunview.h>
#include <suntool/canvas.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 "gxdevice.h"

/* Procedures */
int sun_open(P1(gx_device *));
int sun_close(P1(gx_device *));
int sun_sync(P1(gx_device *));
int sun_fill_rectangle(P6(gx_device *, int, int, int, int, int));
int sun_copy_mono(P10(gx_device *, byte *, int, int, int, int, int, int, int, int));
int sun_copy_color(P9(gx_device *, byte *, int, int, int, int, int, int, int));
int sun_draw_line(P6(gx_device *, int, int, int, int, int));

/* The device descriptor */
static gx_device_procs sun_procs = {
	sun_open,
	sun_close,
	gx_default_map_rgb_color,
	gx_default_map_color_rgb,
	sun_sync,
	sun_fill_rectangle,
	gx_default_tile_rectangle,
	sun_copy_mono,
	sun_copy_color,
	sun_draw_line,
	gx_default_fill_trapezoid,
	gx_default_tile_trapezoid
};

/* Define the X Windows device */
typedef struct gx_device_sun {
	gx_device_common;
	Frame	frame;
	Canvas	canvas;
	Pixwin	*pw;
	struct mpr_data mpr;
	Pixrect	pr;
} gx_device_sun;

/* The instance is public. */
gx_device_sun x_device = {
	sizeof(gx_device_sun),
	&sun_procs,
	(int)(8.5*80), 11*80,	/* 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 */
};

/* Macro for casting gx_device argument */
#define xdev ((gx_device_sun *)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

/* Process switches */
int
sun_swproc(char sw, char *arg)
{
	switch (sw) {
	default:
		return -1;
	case 'w':
		if ((x_device.width = atoi(arg)) <= 0)
			return -1;
		break;
	case 'h':
		if ((x_device.height = atoi(arg)) <= 0)
			return -1;
		break;
	}
	return 0;
}

void
sun_window_create(int *argc_ptr, char **argv)
{
	x_device.frame =
		window_create(NULL, FRAME, FRAME_LABEL, "ghostscript",
					FRAME_ARGC_PTR_ARGV, argc_ptr, argv, 0);
}

int
sun_open(register gx_device *dev)
{
#ifdef gs_DEBUG
if ( gs_debug['X'] )
	{ extern int _Xdebug;
	  _Xdebug = 1;
	}
#endif
	if (xdev->frame == (Frame)0)
	    xdev->frame =
		window_create(NULL, FRAME, FRAME_LABEL, "ghostscript", 0);
	xdev->canvas = window_create(xdev->frame, CANVAS,
			CANVAS_AUTO_EXPAND,		FALSE,
			CANVAS_AUTO_SHRINK,		FALSE,
			CANVAS_WIDTH,			xdev->width,
			CANVAS_HEIGHT,			xdev->height,
			WIN_VERTICAL_SCROLLBAR,		scrollbar_create(0),
			WIN_HORIZONTAL_SCROLLBAR,	scrollbar_create(0),
			0);
	xdev->pw = canvas_pixwin(xdev->canvas);
	window_set(xdev->frame, WIN_SHOW, TRUE, 0);
	(void) notify_do_dispatch();
	(void) notify_dispatch();
	return 0;
}

/* Close the device.  NOT SURE WHAT TO DO HERE YET. */
int
sun_close(gx_device *dev)
{
	window_destroy(xdev->frame);
	xdev->frame = (Frame)0;
	xdev->canvas = (Canvas)0;
	xdev->pw = (Pixwin *)0;
	return 0;
}

/* Synchronize the display with the commands already given */
int
sun_sync(register gx_device *dev)
{
	(void) notify_dispatch();
	return 0;
}

/* Fill a rectangle with a color. */
int
sun_fill_rectangle(register gx_device *dev,
  int x, int y, int w, int h, int color)
{
#ifdef gs_DEBUG
if ( gs_debug['F'] )
	printf("[F] fill (%d,%d):(%d,%d) %d\n",
	       x, y, w, h, color);
#endif
	check_rect();
	if (color != -1)
		pw_write(xdev->pw, x, y, w, h, color ? PIX_CLR : PIX_SET,
			(Pixrect *)0, 0, 0);
	(void) notify_dispatch();
	return 0;
}

/* Copy a monochrome bitmap. */
int
sun_copy_mono(register gx_device *dev, byte *base, int sourcex, int raster,
  int x, int y, int w, int h, int zero, int one)
{
	static int optab[3][3] = {
		PIX_DST,	PIX_SRC|PIX_DST, PIX_NOT(PIX_SRC)&PIX_DST,
		PIX_NOT(PIX_SRC)|PIX_DST, PIX_SET,	PIX_NOT(PIX_SRC),
		PIX_SRC&PIX_DST,	PIX_SRC,	PIX_CLR,
	};
	int op;
	register i;
	extern struct pixrectops mem_ops;

	check_rect();
	if ((op = optab[zero+1][one+1]) == PIX_DST)
		return 0;
	xdev->pr.pr_ops = &mem_ops;
	xdev->pr.pr_width = w + sourcex + 8;
	xdev->pr.pr_height = h;
	xdev->pr.pr_depth = 1;
	xdev->pr.pr_data = (caddr_t)&(xdev->mpr);
	if ((raster & 1) == 0) {
		xdev->mpr.md_linebytes = raster;
		xdev->mpr.md_image = (short *)((int)base & ~1);
		pw_write(xdev->pw, x, y, w, h, op, &(xdev->pr), 
			((int)base & 1) ? sourcex + 8 : sourcex, 0);
	} else {
		xdev->pr.pr_height = 1;
		for (i = 0; i < h; i++) {
			xdev->mpr.md_linebytes = raster;
			xdev->mpr.md_image = (short *)((int)base & ~1);
			pw_write(xdev->pw, x, y, w, 1, op, &(xdev->pr), 
				((int)base & 1) ? sourcex + 8 : sourcex, 0);
			base += raster;
			y++;
		}
	}
	(void) notify_dispatch();
	return 0;
}

/* Copy a "color" bitmap.  Since "color" is the same as monochrome, */
/* this just reduces to copying a monochrome bitmap. */
int
sun_copy_color(register gx_device *dev, byte *base, int sourcex, int raster,
  int x, int y, int w, int h, int color_transparent)
{	int zero = 0, one = 1;
	switch ( color_transparent )
	   {
	case 0: zero = -1; break;
	case 1: one = -1; break;
	   }
	return sun_copy_mono(dev, base, sourcex, raster, x, y, w, h, zero, one);
}

/* Draw a line */
int
sun_draw_line(register gx_device *dev,
  int x0, int y0, int x1, int y1, int color)
{
	if (color != -1)
		pw_vector(xdev->pw, x0, y0, x1, y1, color?PIX_CLR:PIX_SET, 0);
	(void) notify_dispatch();
	return 0;
}
########################### end file "gdevsun.c" ###########################

########################### begin context diffs ############################
*** Makefile	Thu Mar  2 10:14:49 1989
--- ux-cc-x.mak	Thu Feb 23 22:46:57 1989
***************
*** 32,43 ****
  
  # Define the other compilation flags.
  
! CFLAGS=-O -DSUNWINDOW
  
  # Define platform flags for ld.
  # The following are appropriate for Sun OS4.0.
  
! LDPLAT=
  
  # ---------------------------- End of options --------------------------- #
  
--- 32,43 ----
  
  # Define the other compilation flags.
  
! CFLAGS=-O
  
  # Define platform flags for ld.
  # The following are appropriate for Sun OS4.0.
  
! LDPLAT=-Bstatic
  
  # ---------------------------- End of options --------------------------- #
  
***************
*** 60,76 ****
  
  # ---------------------------- Device drivers ---------------------------- #
  
! ## The Sunview device
  
! DEVSUN=gdevsun.$(OBJ)
  
! gdevsun.$(OBJ): gdevsun.c gx.h gxdevice.h
  
  # -------------------------------- Library -------------------------------- #
  
  # Choose the output device
  
! DEVFORUNIX=$(DEVSUN)
  LIBUNIX=$(LIB) $(DEVFORUNIX)
  
  gs_lib.o: $(LIBUNIX)
--- 60,76 ----
  
  # ---------------------------- Device drivers ---------------------------- #
  
! ## The X.11 device
  
! DEVX=gdevx.$(OBJ)
  
! gdevx.$(OBJ): gdevx.c gx.h gxdevice.h
  
  # -------------------------------- Library -------------------------------- #
  
  # Choose the output device
  
! DEVFORUNIX=$(DEVX)
  LIBUNIX=$(LIB) $(DEVFORUNIX)
  
  gs_lib.o: $(LIBUNIX)
***************
*** 87,101 ****
  
  # Library test programs
  
! x: x.$(OBJ) gdevsun.$(OBJ)
! 	cc $(LDPLAT) -X -o x x.$(OBJ) gdevsun.$(OBJ) -lsuntool -lsunwindow -lpixrect -lm
  
  GTUNIX=gt.$(OBJ) gsmain.$(OBJ) utrace.$(OBJ) gs_lib0.$(OBJ) $(DEVFORUNIX)
  gt: $(GTUNIX)
! 	cc $(CFLAGS) $(LDPLAT) -X -o gt $(GTUNIX) -lsuntool -lsunwindow -lpixrect -lm
  
  # Interpreter main program
  
! GSUNIX=gs.$(OBJ) gsmain.$(OBJ) utrace.$(OBJ) $(INT) gs_lib0.$(OBJ) gdevsun.$(OBJ)
  gs: $(GSUNIX)
! 	cc $(CFLAGS) $(LDPLAT) -X -o gs $(GSUNIX) -lsuntool -lsunwindow -lpixrect -lm
--- 87,101 ----
  
  # Library test programs
  
! x: x.$(OBJ) gdevx.$(OBJ)
! 	cc $(LDPLAT) -X -o x x.$(OBJ) gdevx.$(OBJ) -lX11 -lm
  
  GTUNIX=gt.$(OBJ) gsmain.$(OBJ) utrace.$(OBJ) gs_lib0.$(OBJ) $(DEVFORUNIX)
  gt: $(GTUNIX)
! 	cc $(CFLAGS) $(LDPLAT) -X -o gt $(GTUNIX) -lX11 -lm
  
  # Interpreter main program
  
! GSUNIX=gs.$(OBJ) gsmain.$(OBJ) utrace.$(OBJ) $(INT) gs_lib0.$(OBJ) gdevx.$(OBJ)
  gs: $(GSUNIX)
! 	cc $(CFLAGS) $(LDPLAT) -X -o gs $(GSUNIX) -lX11 -lm
*** gs.c	Mon Apr 24 14:26:16 1989
--- gs.c.org	Thu Feb 23 23:53:16 1989
***************
*** 53,62 ****
  	int code;
  	int swproc(P2(char, char *));
  	void argproc(P2(char *, int));
- #ifdef SUNWINDOW
- 	void sun_window_create(P2(int *, char **));
- 	sun_window_create(&argc, argv);
- #endif
  	/* Execute files named in the command line, */
  	/* processing options along the way. */
  	/* Wait until the first file name (or the end */
--- 53,58 ----
***************
*** 95,105 ****
  {	switch ( sw )
  	   {
  	default:
- #ifdef SUNWINDOW
- 		return sun_swproc(sw, arg);
- #else
  		return -1;
- #endif
  	case 'D':			/* define names */
  	   {	char *eqp = strchr(arg, '=');
  		ref value;
--- 91,97 ----
*** gxcache.c	Fri Apr 28 15:40:10 1989
--- gxcache.c.org	Fri Feb 24 01:19:57 1989
***************
*** 38,44 ****
  cached_char *
  gx_alloc_char_bits(gs_font_dir *dir, gx_device_memory *dev,
    ushort iwidth, ushort iheight)
! {	unsigned iraster = ((iwidth + 15) >> 3) & ~1;	/* 04/28/89 H.AIDA */
  	unsigned isize = iraster * iheight;
  	cached_char *cc;
  	if ( iraster != 0 && iheight > dir->upper / iraster )
--- 38,44 ----
  cached_char *
  gx_alloc_char_bits(gs_font_dir *dir, gx_device_memory *dev,
    ushort iwidth, ushort iheight)
! {	unsigned iraster = (iwidth + 7) >> 3;
  	unsigned isize = iraster * iheight;
  	cached_char *cc;
  	if ( iraster != 0 && iheight > dir->upper / iraster )
############################ end context diffs #############################