Esempio n. 1
0
main()
{
	float llx,lly,urx,ury;
	int width,height,i;
	char buf[LBUF],string[256];
	char *scalestring="0.88889 1.10769 scale\n"; /* 72/81, 72/65 */
	Display *dpy;
	XEvent ev;
	GC gc;
	Window win;
	DPSContext ctxt;

	/* open display */
	if ((dpy=XOpenDisplay(NULL))==NULL) {
		fprintf(stderr,"Cannot connect to display %s\n",
			XDisplayName(NULL));
		exit(-1);
	}
	
	/* determine window size */
	fgets(buf,LBUF,stdin);
	sscanf(buf,"%*s %s",string);
	string[3] = '\0';
	if (strcmp(string,"EPS")!=0) {
		width = 612;
		height = 792;
	} else {
		while(fgets(buf,LBUF,stdin)!=NULL) { 
			if (buf[0]!='%' || buf[1]!='%') continue;
			sscanf(buf,"%s",string);
			if (strcmp(string,"%%BoundingBox:")==0) {
				sscanf(buf,"%*s %s",string);
				if (strcmp(string,"(atend)")==0) {
					width = 612;
					height = 792;
				} else {
					sscanf(buf,"%*s %f %f %f %f",
						&llx,&lly,&urx,&ury);
					width = urx-llx;
					height = ury-lly;
				}
				break;
			} else if (strcmp(string,"%%EndComments")==0) {
				width = 612;
				height = 792;
				break;
			}
		}
	}
	
	/* create and map window */
	win = XCreateSimpleWindow(dpy,DefaultRootWindow(dpy),
		100,100,width,height,1,
                BlackPixel(dpy,DefaultScreen(dpy)),
		WhitePixel(dpy,DefaultScreen(dpy)));
	XMapWindow(dpy,win);
	
	/* create graphics context */
	gc = XCreateGC(dpy,RootWindow(dpy,DefaultScreen(dpy)),0,NULL);
	XSetForeground(dpy,gc,BlackPixel(dpy,DefaultScreen(dpy)));
	XSetBackground(dpy,gc,WhitePixel(dpy,DefaultScreen(dpy)));

	/* create and set Display PostScript context */
	ctxt = XDPSCreateSimpleContext(dpy,win,gc,0,height,
		DPSDefaultTextBackstop,DPSDefaultErrorProc,NULL);
	if (ctxt==NULL) {
		fprintf(stderr,"Cannot create DPS context\n");
		exit(-1);
	}
	DPSSetContext(ctxt);
	DPSWaitContext(ctxt);
	for (i=0; i<1000000; ++i);  /* KLUDGE synchronization */
	
	/* scale */
	/* no longer necessary, as of AIX Version 3.003? */
	/*
	DPSWriteData(ctxt,scalestring,strlen(scalestring));
	*/

	/* read PostScript from standard input and write to window */
	while (fgets(buf,LBUF,stdin)!=NULL) {
		sscanf(buf,"%s",string);
		if (strcmp(string,"showpage")==0) break;
		DPSWriteData(ctxt,buf,strlen(buf));
	}
	DPSFlushContext(ctxt);

	/* any key press to exit */
	XSelectInput(dpy,win,KeyPressMask);
	while(True) {
            XNextEvent(dpy,&ev);
	    if (ev.type==KeyPress) break;
	}

	/* clean up */
	DPSDestroySpace(DPSSpaceFromContext(ctxt));
	XFlush(dpy);
}
Esempio n. 2
0
main()
{
	int llx,lly,urx,ury,width,height,
		scrwidth,scrheight,scrwidthmm,scrheightmm;
	float xres,yres;
	char buf[LBUF];
	Display *dpy;
	int scr;
	unsigned long black,white;
	GC gcpix,gcwin;
	Window win;
	Pixmap pix;
	XEvent ev;
	DPSContext dps;

	/* open display */
	if ((dpy=XOpenDisplay(NULL))==NULL) {
		fprintf(stderr,"Cannot connect to display %s\n",
			XDisplayName(NULL));
		exit(-1);
	}
	scr = DefaultScreen(dpy);
	black = BlackPixel(dpy,scr);
	white = WhitePixel(dpy,scr);
	
	/* determine BoundingBox */
	llx = LLX_DEFAULT;
	lly = LLY_DEFAULT;
	urx = URX_DEFAULT;
	ury = URY_DEFAULT;
	fgets(buf,LBUF,stdin);
	if (strstr(buf,"EPS")!=NULL) {
		while(fgets(buf,LBUF,stdin)!=NULL) { 
			if (buf[0]!='%' || buf[1]!='%') continue;
			if (strstr(buf,"%%BoundingBox:")==buf) {
				if (strstr(buf,"(atend)")==NULL) {
					sscanf(&buf[14],"%d %d %d %d",
						&llx,&lly,&urx,&ury);
				}
				break;
			} else if (strstr(buf,"%%EndComments")==buf) {
				break;
			} else if (strstr(buf,"%%EndProlog")==buf) {
				break;
			}
		}
	}

	/* width and height in pixels */
	scrwidth = WidthOfScreen(DefaultScreenOfDisplay(dpy));
	scrheight = HeightOfScreen(DefaultScreenOfDisplay(dpy));
	scrwidthmm = WidthMMOfScreen(DefaultScreenOfDisplay(dpy));
	scrheightmm = HeightMMOfScreen(DefaultScreenOfDisplay(dpy));
	xres = (int)(25.4*scrwidth/scrwidthmm)/72.0;
	yres = (int)(25.4*scrheight/scrheightmm)/72.0;
	if (xres*(urx-llx)>scrwidth || yres*(ury-lly)>scrheight) {
		xres = (scrwidth-32.0)/(urx-llx);
		yres = (scrheight-32.0)/(ury-lly);
		xres = yres = (xres<yres)?xres:yres;
	}
	width = (urx-llx)*xres;
	height = (ury-lly)*yres;

	/* create pixmap and its gc */
	pix = XCreatePixmap(dpy,DefaultRootWindow(dpy),width,height,
		DefaultDepth(dpy,scr));
	gcpix = XCreateGC(dpy,pix,0,NULL);

	/* create and set Display PostScript context for pixmap */
	dps = XDPSCreateSimpleContext(dpy,pix,gcpix,0,height,
		DPSDefaultTextBackstop,DPSDefaultErrorProc,NULL);
	if (dps==NULL) {
		fprintf(stderr,"Cannot create DPS context\n");
		exit(-1);
	}
	DPSPrintf(dps,"\n resyncstart\n");
	DPSSetContext(dps);
	DPSFlushContext(dps);
	DPSWaitContext(dps);

	/* paint white background */
	DPSPrintf(dps,
		"gsave\n"
		"1 setgray\n"
		"0 0 %d %d rectfill\n"
		"grestore\n",
		urx-llx,ury-lly);
	
	/* translate */
	DPSPrintf(dps,"%d %d translate\n",-llx,-lly);

	/* read PostScript from standard input and render in pixmap */
	DPSPrintf(dps,"/showpage {} def\n");
	while (fgets(buf,LBUF,stdin)!=NULL)
		DPSWritePostScript(dps,buf,strlen(buf));
	DPSFlushContext(dps);
	DPSWaitContext(dps);
	
	/* create and map window */
	win = XCreateSimpleWindow(dpy,DefaultRootWindow(dpy),
		100,100,width,height,1,black,white);
	XSetStandardProperties(dpy,win,"EPS Pixmap","EPSpix",
		None,NULL,0,NULL);
	XMapWindow(dpy,win);

	/* copy pixmap to window; in pixmap, black=0 and white=1 */
	gcwin = XCreateGC(dpy,win,0,NULL);
	XCopyArea(dpy,pix,win,gcwin,0,0,width,height,0,0);

	/* main event loop */
	XSelectInput(dpy,win,
		KeyPressMask |
		ExposureMask);
	while(True) {
        	XNextEvent(dpy,&ev);
		if (ev.type==Expose) {
			while (XCheckTypedEvent(dpy,Expose,&ev));
			XCopyArea(dpy,pix,win,gcwin,0,0,width,height,0,0);
		} else if (ev.type==KeyPress) {
			break;
		}
	}

	/* clean up */
	DPSDestroySpace(DPSSpaceFromContext(dps));
	XFreePixmap(dpy,pix);
	XFreeGC(dpy,gcpix);
	XFreeGC(dpy,gcwin);
}