static int FinishUp( DPSContext context, DPSPointer cookie) { static char restorebuf[] = "\n$Adobe$DPS$Lib$Dict /EPSFsave get restore grestore\n"; StatusInfo *info = StatusList; int err; /* Check the results of the imaging: Get the error status and restore the context */ _DPSPCheckForError(context, &err); /* Can't do this is a wrap because of restore semantics */ DPSWritePostScript(context, restorebuf, strlen(restorebuf)); (void) XDPSPopContextParameters(cookie); /* See if we have an info record and delete it if so */ while (info != NULL && info->ctxt != context) info = info->next; if (info != NULL) { if (info == StatusList) StatusList = info->next; else info->prev->next = info->next; if (info->next != NULL) info->next->prev = info->prev; XDPSRegisterStatusProc(context, info->oldProc); free(info); } if (err) return dps_status_postscript_error; else return dps_status_success; }
void DPSPrintf(DPSContext ctxt, char *fmt, ...) { va_list args; va_start(args, fmt); #ifdef USE_DOPRINT DPSdoprint(ctxt, fmt, &args); #else /* USE_DOPRINT */ { char buffer[10000]; vsprintf(buffer, fmt, args); DPSWritePostScript(ctxt, buffer, strlen(buffer)); } #endif /* USE_DOPRINT */ va_end(args); }
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); }
int XDPSImageFileIntoDrawable( DPSContext context, Screen *screen, Drawable dest, FILE *file, int drawableHeight, int drawableDepth, XRectangle *bbox, int xOffset, int yOffset, double pixelsPerPoint, Bool clear, Bool createMask, Bool waitForCompletion, Bool *doneFlag) { #define BUFSIZE 256 #define EXECLEN 6 char buf[BUFSIZE]; static char eobuf[] = "\n$Adobe$DPS$Lib$Dict /execSuccess true put\n\ stop\n\ Magic end of data line )))))))))) 99#2 2#99 <xyz> // 7gsad,32h4ghNmndFgj2\n"; XDPSStandardColormap maskMap; XDPSStandardColormap rgbMap; unsigned int flags = 0; int status; Bool inited; DPSPointer cookie; int doublings; int ms; XDPSStatusProc oldProc; unsigned long startReqNum = 0, endReqNum; if (screen == NULL || dest == None || drawableHeight <= 0 || drawableDepth <= 0 || pixelsPerPoint <= 0) { return dps_status_illegal_value; } if (context == NULL) { context = XDPSGetSharedContext(DisplayOfScreen(screen)); if (context == NULL) { FillPixmapWithGray(screen, dest, bbox, xOffset, yOffset, pixelsPerPoint, createMask); return dps_status_no_extension; } } (*rewindFunction)(file, rewindClientData); if (!waitForCompletion) { DPSWaitContext(context); /* Any status events before this point go to old handler */ startReqNum = NextRequest(DisplayOfScreen(screen)); } status = _XDPSTestComponentInitialized(context, dps_init_bit_preview, &inited); if (status != dps_status_success) return status; if (!inited) { (void) _XDPSSetComponentInitialized(context, dps_init_bit_preview); _DPSPDefineExecFunction(context); } if (createMask) { if (drawableDepth != 1) return dps_status_illegal_value; maskMap.colormap = None; maskMap.red_max = 1; maskMap.red_mult = -1; maskMap.base_pixel = 1; rgbMap.colormap = None; rgbMap.red_max = rgbMap.green_max = rgbMap.blue_max = rgbMap.red_mult = rgbMap.green_mult = rgbMap.blue_mult = rgbMap.base_pixel = 0; flags = XDPSContextGrayMap | XDPSContextRGBMap; } status = XDPSPushContextParameters(context, screen, drawableDepth, dest, drawableHeight, &rgbMap, &maskMap, flags | XDPSContextScreenDepth | XDPSContextDrawable, &cookie); if (status != dps_status_success) return status; _DPSPSetMatrix(context, xOffset, yOffset, pixelsPerPoint); if (clear) _DPSPClearArea(context, (int) bbox->x, (int) bbox->y, (int) bbox->width, (int) bbox->height); if (createMask) _DPSPSetMaskTransfer(context); /* Prepare to read PostScript code */ _DPSPSaveBeforeExec(context, !waitForCompletion); DPSWritePostScript(context, "\nexec\n", EXECLEN); imaging = True; while ((*getsFunction)(buf, BUFSIZE, file, getsClientData) != NULL) { DPSWritePostScript(context, buf, strlen(buf)); } imaging = False; /* This marks the end of the data stream */ DPSWritePostScript(context, eobuf, strlen(eobuf)); if (!waitForCompletion) { *doneFlag = False; oldProc = XDPSRegisterStatusProc(context, HandlePreviewStatus); SetUpStatusVariables(context, cookie, doneFlag, startReqNum, oldProc); XDPSSetStatusMask(context, 0, 0, PSFROZENMASK); ms = timeStart; /* Check for done until we run out of time */ doublings = 0; while (1) { if (XDPSGetContextStatus(context) == PSFROZEN) { waitForCompletion = True; XDPSUnfreezeContext(context); break; } if (doublings >= maxDoubles) break; /* Wait a while */ msleep(ms); ms *= 2; doublings++; } } /* If previous decided imaging is done, it changed waitForCompletion */ if (waitForCompletion) return FinishUp(context, cookie); else { endReqNum = NextRequest(DisplayOfScreen(screen)) - 1; SetEndReqNum(context, endReqNum); return dps_status_imaging_incomplete; } #undef EXECLEN #undef BUFSIZE }