1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
|
/*----------------------------------------------------------------------*/
/* render.c --- Ghostscript rendering of background PostScript files */
/* Copyright (c) 2002 Tim Edwards, Johns Hopkins University */
/* These routines work only if ghostscript is on the system. */
/*----------------------------------------------------------------------*/
#undef GS_DEBUG
/* #define GS_DEBUG */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if defined(DARWIN)
#include <sys/malloc.h>
#else
#include <malloc.h>
#endif
#include <unistd.h>
#include <time.h>
#include <sys/wait.h> /* for waitpid() */
#include <sys/types.h>
#include <signal.h>
#include <fcntl.h>
#include <X11/Intrinsic.h>
#include <X11/StringDefs.h>
#include <X11/Xatom.h>
/*------------------------------------------------------------------------*/
/* Local includes */
/*------------------------------------------------------------------------*/
#include "colordefs.h"
#include "xcircuit.h"
/*----------------------------------------------------------------------*/
/* Function prototype declarations */
/*----------------------------------------------------------------------*/
#include "prototypes.h"
/*------------------------------------------------------------------------*/
/* External Variable definitions */
/*------------------------------------------------------------------------*/
extern char _STR2[250], _STR[150];
extern Globaldata xobjs;
extern Clientdata areastruct;
extern Display *dpy;
extern Window win;
extern short eventmode;
extern int *appcolors;
extern int number_colors;
extern colorindex *colorlist;
extern Cursor appcursors[NUM_CURSORS];
Pixmap bbuf = (Pixmap)NULL; /* background buffer */
#ifdef DOUBLEBUFFER
extern Pixmap dbuf;
#endif
#ifndef HAVE_VFORK
#define vfork fork
#endif
/*------------------------------------------------------*/
/* Global variable definitions */
/*------------------------------------------------------*/
Atom gv, gvc, gvpage, gvnext, gvdone;
pid_t gsproc = -1; /* ghostscript process */
int fgs[2]; /* stdin pipe pair for ghostscript */
Window mwin = 0; /* "false" window hack to get gs */
/* process to capture ClientMessage */
/* events. */
/*--------------------------------------------------------------*/
/* Preliminary in allowing generic PostScript backgrounds */
/* via the ghostscript interpreter: Set the GHOSTVIEW and */
/* GHOSTVIEW atoms, and set the GHOSTVIEW environtment */
/* variable. */
/*--------------------------------------------------------------*/
void ghostinit()
{
gv = XInternAtom(dpy, "GHOSTVIEW", False);
gvc = XInternAtom(dpy, "GHOSTVIEW_COLORS", False);
gvpage = XInternAtom(dpy, "PAGE", False);
gvnext = XInternAtom(dpy, "NEXT", False);
gvdone = XInternAtom(dpy, "DONE", False);
sprintf(_STR, "%ld %d %d %d %d %d %g %g %d %d %d %d",
0L, 0, 0, 0,
areastruct.width * 75 / 72,
areastruct.height * 75 / 72,
75.0, 75.0, 0, 0, 0, 0);
XChangeProperty(dpy, win, gv, XA_STRING, 8, PropModeReplace,
_STR, strlen(_STR));
sprintf(_STR, "%s %d %d", "Color", (int)FOREGROUND, (int)BACKGROUND);
XChangeProperty(dpy, win, gvc, XA_STRING, 8, PropModeReplace,
_STR, strlen(_STR));
XSync(dpy, False);
}
/*------------------------------------------------------*/
/* Send a ClientMessage event */
/*------------------------------------------------------*/
void send_client(Atom msg)
{
XEvent event;
if (mwin == 0) return; /* Have to wait for gs */
/* to give us window # */
event.xclient.type = ClientMessage;
event.xclient.display = dpy;
event.xclient.window = win;
event.xclient.message_type = msg;
event.xclient.format = 32;
event.xclient.data.l[0] = mwin;
event.xclient.data.l[1] = bbuf;
XSendEvent(dpy, mwin, False, 0, &event);
XFlush(dpy);
}
/*------------------------------------------------------*/
/* Ask ghostscript to produce the next page. */
/*------------------------------------------------------*/
void ask_for_next()
{
XSync(dpy, False);
send_client(gvnext);
#ifdef GS_DEBUG
printf("Xcircuit: Sent NEXT message to ghostscript\n");
#endif
}
/*------------------------------------------------------*/
/* Wait for the gs process to return "PAGE" */
/* (Block until gs is finished) */
/*------------------------------------------------------*/
void wait_for_page()
{
XEvent event;
for(;;) {
XNextEvent(dpy, &event);
if (event.type == ClientMessage) {
if (event.xclient.message_type == gvpage) break;
else if (event.xclient.message_type == gvdone) {
#ifdef GS_DEBUG
printf("Xcircuit: Received DONE message from ghostscript\n");
#endif
/* Catastrophic error in ghostscript */
return;
}
}
else if (event.type == KeyPress) { /* Check for Ctrl-C break */
XKeyEvent *keyevent = (XKeyEvent *)(&event);
if (keyevent->state == 4 && keyevent->keycode == 54) {
Wprintf("Ctrl-C break from render loop");
break;
}
else {
fprintf(stderr, "state = %d, keycode = %d\n", keyevent->state,
keyevent->keycode);
XtDispatchEvent(&event);
}
}
else XtDispatchEvent(&event);
}
#ifdef GS_DEBUG
printf("Xcircuit: Received PAGE message from ghostscript\n");
#endif
mwin = event.xclient.data.l[0];
}
/*------------------------------------------------------*/
void wait_for_done()
{
XEvent event;
for(;;) {
XNextEvent(dpy, &event);
if (event.type == ClientMessage) {
if (event.xclient.message_type == gvdone) break;
#ifdef GS_DEBUG
else if (event.xclient.message_type == gvpage)
printf("Xcircuit: Received PAGE message from ghostscript\n");
#endif
}
else XtDispatchEvent(&event);
}
#ifdef GS_DEBUG
printf("Xcircuit: Received DONE message from ghostscript\n");
#endif
mwin = 0;
}
/*--------------------------------------------------------*/
/* Start a ghostscript process and arrange the I/O pipes */
/* (Commented lines cause ghostscript to relay its stderr */
/* to xcircuit's stderr) */
/*--------------------------------------------------------*/
void start_gs()
{
int std_out[2], std_err[2], ret;
#ifdef HAVE_PUTENV
static char env_str1[128], env_str2[64];
#endif
if (bbuf != (Pixmap)NULL) XFreePixmap(dpy, bbuf);
bbuf = XCreatePixmap(dpy, win, areastruct.width, areastruct.height,
DefaultDepthOfScreen(XtScreen(areastruct.area)));
ret = pipe(fgs);
ret = pipe(std_out);
#ifndef GS_DEBUG
ret = pipe(std_err);
#endif
/* We need a complicated pipe here, with input going from xcircuit */
/* to gs to provide scale/position information, and input going from */
/* the background file to gs for rendering. */
/* Here is not the place to do it. Set up gs to take stdin as input.*/
if (gsproc < 0) { /* Ghostscript is not running yet */
gsproc = vfork();
if (gsproc == 0) { /* child process (gs) */
#ifdef GS_DEBUG
printf("Calling %s\n", GS_EXEC);
#endif
close(std_out[0]);
#ifndef GS_DEBUG
close(std_err[0]);
#endif
dup2(fgs[0], 0);
close(fgs[0]);
dup2(std_out[1], 1);
close(std_out[1]);
#ifndef GS_DEBUG
dup2(std_err[1], 2);
close(std_err[1]);
#endif
#ifdef HAVE_PUTENV
sprintf(env_str1, "DISPLAY=%s", XDisplayString(dpy));
putenv(env_str1);
#ifdef DOUBLEBUFFER
sprintf(env_str2, "GHOSTVIEW=%ld %ld", (long)win, (long)bbuf);
putenv(env_str2);
#else
sprintf(env_str2, "GHOSTVIEW=%ld", (long)win);
putenv(env_str2);
#endif
#else
setenv("DISPLAY", XDisplayString(dpy), True);
#ifdef DOUBLEBUFFER
sprintf(_STR, "%ld %ld", (long)win, (long)bbuf);
setenv("GHOSTVIEW", _STR, True);
#else
sprintf(_STR, "%ld", (long)win);
setenv("GHOSTVIEW", _STR, True);
#endif
#endif
execlp(GS_EXEC, "gs", "-dNOPAUSE", "-", NULL);
gsproc = -1;
printf("Exec of gs failed\n");
return;
}
else if (gsproc < 0) {
Wprintf("Error: ghostscript not running");
return; /* error condition */
}
}
}
/*--------------------------------------------------------*/
/* Parse the background file for Bounding Box information */
/*--------------------------------------------------------*/
void parse_bg(FILE *fi, FILE *fbg) {
char *bbptr;
Boolean bflag = False;
int llx, lly, urx, ury;
char line_in[256];
float psscale;
psscale = getpsscale(xobjs.pagelist[areastruct.page]->outscale, areastruct.page);
for(;;) {
if (fgets(line_in, 255, fi) == NULL) {
Wprintf("Error: end of file before end of insert.");
return;
}
else if (strstr(line_in, "end_insert") != NULL) break;
if (!bflag) {
if ((bbptr = strstr(line_in, "BoundingBox:")) != NULL) {
if (strstr(line_in, "(atend)") == NULL) {
bflag = True;
sscanf(bbptr + 12, "%d %d %d %d", &llx, &lly, &urx, &ury);
/* compute user coordinate bounds from PostScript bounds */
#ifdef GS_DEBUG
printf("BBox %d %d %d %d PostScript coordinates\n", llx, lly, urx, ury);
#endif
llx = (int)((float)llx / psscale);
lly = (int)((float)lly / psscale);
urx = (int)((float)urx / psscale);
ury = (int)((float)ury / psscale);
#ifdef GS_DEBUG
printf("BBox %d %d %d %d XCircuit coordinates\n", llx, lly, urx, ury);
#endif
xobjs.pagelist[areastruct.page]->background.lowerleft.x = llx;
xobjs.pagelist[areastruct.page]->background.lowerleft.y = lly;
xobjs.pagelist[areastruct.page]->background.upperright.x = urx;
xobjs.pagelist[areastruct.page]->background.upperright.y = ury;
if (fbg == (FILE *)NULL) break;
}
}
}
if (fbg != (FILE *)NULL) fputs(line_in, fbg);
}
}
/*-------------------------------------------------------*/
/* Get bounding box information from the background file */
/*-------------------------------------------------------*/
void bg_get_bbox()
{
FILE *fi;
char *fname;
fname = xobjs.pagelist[areastruct.page]->background.name;
if ((fi = fopen(fname, "r")) == NULL) {
printf("Failure to open background file to get bounding box info\n");
return;
}
parse_bg(fi, (FILE *)NULL);
fclose(fi);
}
/*------------------------------------------------------------*/
/* Adjust object's bounding box based on the background image */
/*------------------------------------------------------------*/
void backgroundbbox(int mpage)
{
short llx, lly, urx, ury;
objectptr thisobj = xobjs.pagelist[mpage]->pageobj;
psbkground *thisbg = &xobjs.pagelist[mpage]->background;
#ifdef SCHEMA
llx = thisobj->lleft2.x;
lly = thisobj->lleft2.y;
urx = thisobj->width2 + llx;
ury = thisobj->height2 + lly;
#else
llx = thisobj->lowerleft.x;
lly = thisobj->lowerleft.y;
urx = thisobj->width + llx;
ury = thisobj->height + lly;
#endif
if (thisbg->lowerleft.x < llx) llx = thisbg->lowerleft.x;
if (thisbg->lowerleft.y < lly) lly = thisbg->lowerleft.y;
if (thisbg->upperright.x > urx) urx = thisbg->upperright.x;
if (thisbg->upperright.y > ury) ury = thisbg->upperright.y;
#ifdef SCHEMA
thisobj->lleft2.x = llx;
thisobj->lleft2.y = lly;
thisobj->width2 = urx - llx;
thisobj->height2 = ury - lly;
#else
thisobj->lowerleft.x = llx;
thisobj->lowerleft.y = lly;
thisobj->width = urx - llx;
thisobj->height = ury - lly;
#endif
#ifdef SCHEMA
llx = thisobj->lowerleft.x;
lly = thisobj->lowerleft.y;
urx = thisobj->width + llx;
ury = thisobj->height + lly;
if (thisbg->lowerleft.x < llx) llx = thisbg->lowerleft.x;
if (thisbg->lowerleft.y < lly) lly = thisbg->lowerleft.y;
if (thisbg->upperright.x > urx) urx = thisbg->upperright.x;
if (thisbg->upperright.y > ury) ury = thisbg->upperright.y;
thisobj->lowerleft.x = llx;
thisobj->lowerleft.y = lly;
thisobj->width = urx - llx;
thisobj->height = ury - lly;
#endif
}
/*------------------------------------------------------*/
/* Read a background PostScript image from a file and */
/* store in a temporary file, passing that filename to */
/* the background property of the page. */
/*------------------------------------------------------*/
void readbackground(FILE *fi)
{
FILE *fbg = (FILE *)NULL;
int tfd;
char *file_in = (char *)malloc(9 + strlen(xobjs.tempdir));
/* "@" denotes a temporary file */
sprintf(file_in, "@%s/XXXXXX", xobjs.tempdir);
tfd = mkstemp(file_in + 1);
if (tfd == -1) printf("Error generating temporary filename\n");
else {
if ((fbg = fdopen(tfd, "w")) == NULL) {
printf("Error opening temporary file \"%s\"\n", file_in + 1);
}
}
/* Read the file to the restore directive or end_insertion */
/* Skip restore directive and end_insertion command */
parse_bg(fi, fbg);
if (fbg != (FILE *)NULL) {
fclose(fbg);
call_loadbg(file_in);
}
free(file_in);
}
/*------------------------------------------------------*/
/* Save a background PostScript image to the output */
/* file by streaming directly from the background file */
/*------------------------------------------------------*/
void savebackground(FILE *fo, char *psfilename)
{
FILE *psf;
char *fname = psfilename;
char line_in[256];
if (fname[0] == '@') fname++;
if ((psf = fopen(fname, "r")) == NULL) {
printf("Error opening background file \"%s\" for reading.\n", fname);
return;
}
for(;;) {
if (fgets(line_in, 255, psf) == NULL)
break;
else
fputs(line_in, fo);
}
fclose(psf);
}
/*------------------------------------------------------*/
/* Load a generic (non-xcircuit) postscript file as the */
/* background for the page. */
/*------------------------------------------------------*/
void call_loadbg(char *gsfile)
{
if (gsproc < 0)
start_gs();
else
reset_gs();
xobjs.pagelist[areastruct.page]->background.name =
(char *) malloc(strlen(gsfile) + 1);
strcpy(xobjs.pagelist[areastruct.page]->background.name, gsfile);
}
/*--------------------------------------------------------*/
void loadbackground()
{
call_loadbg(_STR2);
bg_get_bbox();
updatepagebounds(objectdata);
zoomview(NULL, NULL, NULL);
}
/*------------------------------------------------------*/
/* Send text to the ghostscript renderer */
/*------------------------------------------------------*/
void send_to_gs(char *text)
{
write(fgs[1], text, strlen(text));
#ifdef GS_DEBUG
printf("writing: %s", text);
#endif
}
/*------------------------------------------------------*/
/* Call ghostscript to render the background */
/*------------------------------------------------------*/
int renderbackground()
{
char *bgfile;
float psnorm, psxpos, psypos, defscale;
float devres = 0.96; /* = 72.0 / 75.0, ps_units/in : screen_dpi */
if (gsproc < 0) return -1;
defscale = (xobjs.pagelist[areastruct.page]->coordstyle == CM) ?
CMSCALE : INCHSCALE;
psnorm = xobjs.pagelist[areastruct.page]->outscale * (*areastruct.vscale)
* (1.0 / defscale) * devres;
psxpos = (float)(-areastruct.pcorner->x) * (*areastruct.vscale) * devres;
psypos = (float)(-areastruct.pcorner->y) * (*areastruct.vscale) * devres
+ ((float)areastruct.height / 12.0);
/* Conditions for re-rendering: Must have a background specified */
/* and must be on the page, not a library or other object. */
if (xobjs.pagelist[areastruct.page]->background.name == (char *)NULL)
return -1;
else if (areastruct.lastbackground == xobjs.pagelist[areastruct.page]->background.name)
return 0;
if (is_page(objectdata) == -1)
return -1;
ask_for_next();
bgfile = xobjs.pagelist[areastruct.page]->background.name;
if (*bgfile == '@') bgfile++;
#ifdef GS_DEBUG
printf("Rendering background from file \"%s\"\n", bgfile);
#endif
/* write scale and position to ghostscript */
/* and tell ghostscript to run the requested file */
/* send_to_gs("/GSobj save def\n"); */
send_to_gs("gsave\n");
sprintf(_STR, "%3.2f %3.2f translate\n", psxpos, psypos);
send_to_gs(_STR);
sprintf(_STR, "%3.2f %3.2f scale\n", psnorm, psnorm);
send_to_gs(_STR);
sprintf(_STR, "(%s) run\n", bgfile);
send_to_gs(_STR);
/* send_to_gs("GSobj restore\n"); */
send_to_gs("grestore\n");
/* ask ghostscript to produce the next page */
XDefineCursor(dpy, win, WAITFOR);
Wprintf("Rendering background image.");
wait_for_page();
Wprintf("Background finished.");
XDefineCursor(dpy, win, CROSS);
/* Mark this as the most recently rendered background, so we don't */
/* have to render more than necessary. */
areastruct.lastbackground = xobjs.pagelist[areastruct.page]->background.name;
return 0;
}
/*----------------------------------------------------------*/
/* Copy the rendered background pixmap to the buffer pixmap */
/*----------------------------------------------------------*/
int copybackground()
{
/* only draw on a top-level page */
if (is_page(objectdata) == -1)
return -1;
#ifdef DOUBLEBUFFER
XCopyArea(dpy, bbuf, dbuf, areastruct.gc, 0, 0,
areastruct.width, areastruct.height, 0, 0);
#else
XCopyArea(dpy, bbuf, win, areastruct.gc, 0, 0,
areastruct.width, areastruct.height, 0, 0);
#endif
return 0;
}
/*------------------------------------------------------*/
/* Exit ghostscript. . . gently */
/*------------------------------------------------------*/
int exit_gs()
{
if (gsproc < 0) return -1; /* gs not running */
send_to_gs("quit\n");
ask_for_next();
#ifdef GS_DEBUG
printf("Waiting for gs to exit\n");
#endif
waitpid(gsproc, NULL, 0);
#ifdef GS_DEBUG
printf("gs has exited\n");
#endif
mwin = 0;
gsproc = -1;
return 0;
}
/*------------------------------------------------------*/
/* Restart ghostscript */
/*------------------------------------------------------*/
int reset_gs()
{
if (gsproc < 0) return -1;
exit_gs();
ghostinit();
start_gs();
return 0;
}
/*----------------------------------------------------------------------*/
|