| 1 |
/*========================================================================*\ |
| 2 |
|
| 3 |
Copyright (c) 1994-2004 Paul Vojta |
| 4 |
|
| 5 |
Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 |
of this software and associated documentation files (the "Software"), to |
| 7 |
deal in the Software without restriction, including without limitation the |
| 8 |
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 9 |
sell copies of the Software, and to permit persons to whom the Software is |
| 10 |
furnished to do so, subject to the following conditions: |
| 11 |
|
| 12 |
The above copyright notice and this permission notice shall be included in |
| 13 |
all copies or substantial portions of the Software. |
| 14 |
|
| 15 |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 |
PAUL VOJTA OR ANY OTHER AUTHOR OF THIS SOFTWARE BE LIABLE FOR ANY CLAIM, |
| 19 |
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, |
| 20 |
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 21 |
OTHER DEALINGS IN THE SOFTWARE. |
| 22 |
|
| 23 |
NOTES: |
| 24 |
This code was originally written by Ricardo Telichevesky |
| 25 |
([email protected]) and Luis Miguel Silveira |
| 26 |
([email protected]). |
| 27 |
It was largely influenced by similar code in the SeeTeX/XTeX |
| 28 |
package by Dirk Grunwald ([email protected]). |
| 29 |
|
| 30 |
\*========================================================================*/ |
| 31 |
|
| 32 |
#ifdef PS_DPS /* entire file */ |
| 33 |
|
| 34 |
|
| 35 |
#include "xdvi-config.h" |
| 36 |
#include "special.h" |
| 37 |
#include "util.h" |
| 38 |
#include "xdvi.h" |
| 39 |
|
| 40 |
#include <setjmp.h> |
| 41 |
#include <signal.h> |
| 42 |
#include <X11/X.h> |
| 43 |
#include <X11/Xlib.h> |
| 44 |
#include <DPS/XDPSlib.h> |
| 45 |
#include <DPS/dpsXclient.h> |
| 46 |
#include <DPS/dpsexcept.h> |
| 47 |
#include <DPS/dpsclient.h> |
| 48 |
|
| 49 |
#ifdef X_NOT_STDC_ENV |
| 50 |
extern int errno; |
| 51 |
#endif |
| 52 |
|
| 53 |
#if defined(sun) || defined(__sun) |
| 54 |
#ifndef SUNHACK |
| 55 |
#define SUNHACK 1 |
| 56 |
#endif |
| 57 |
#endif |
| 58 |
|
| 59 |
|
| 60 |
#include "dvi-init.h" |
| 61 |
#include "dvi-draw.h" |
| 62 |
#include "events.h" |
| 63 |
|
| 64 |
/* |
| 65 |
* This string reads chunks (delimited by %%xdvimark). |
| 66 |
* The first character of a chunk tells whether a given chunk |
| 67 |
* is to be done within save/restore or not. |
| 68 |
* The `H' at the end tells it that the first group is a |
| 69 |
* header; i.e., no save/restore. |
| 70 |
*/ |
| 71 |
#if !SUNHACK |
| 72 |
static char preamble[] = |
| 73 |
"/xdvi$line 81 string def " |
| 74 |
"/xdvi$run {{$error null ne {$error /newerror false put}if " |
| 75 |
" currentfile cvx stopped " |
| 76 |
" $error null eq {false} {$error /newerror get} ifelse and " |
| 77 |
" {handleerror} if} stopped pop} def " |
| 78 |
"/xdvi$dslen countdictstack def " |
| 79 |
"{currentfile read not {exit} if 72 eq " |
| 80 |
" {xdvi$run} " |
| 81 |
" {/xdvi$sav save def xdvi$run " |
| 82 |
" clear countdictstack xdvi$dslen sub {end} repeat xdvi$sav restore} " |
| 83 |
" ifelse " |
| 84 |
" {(%%xdvimark) currentfile xdvi$line {readline} stopped " |
| 85 |
" {clear} {pop eq {exit} if} ifelse }loop " |
| 86 |
" (xdvi$Ack\n) print flush " |
| 87 |
"}loop\nH"; |
| 88 |
#else /* SUNHACK */ |
| 89 |
static char preamble[] = |
| 90 |
"/xdvi$line 81 string def " |
| 91 |
"/xdvi$run {{$error null ne {$error /newerror false put}if " |
| 92 |
"currentfile cvx stopped " |
| 93 |
"$error null eq {false} {$error /newerror get} ifelse and " |
| 94 |
"{handleerror} if} stopped pop} def " |
| 95 |
"/xdvi$dslen countdictstack def " |
| 96 |
"/xdvi$ack {{(%%xdvimark) currentfile xdvi$line {readline} stopped " |
| 97 |
" {clear} {pop eq {exit} if} ifelse }loop " |
| 98 |
" (xdvi$Ack\n) print flush} bind def " |
| 99 |
"errordict begin /interrupt{(xdvi$Int\n) print flush stop}bind def " |
| 100 |
"end " |
| 101 |
"{{currentfile read not {exit} if 72 eq " |
| 102 |
" {xdvi$run} " |
| 103 |
" {/xdvi$sav save def xdvi$run " |
| 104 |
" clear countdictstack xdvi$dslen sub {end} repeat xdvi$sav restore} " |
| 105 |
" ifelse " |
| 106 |
" xdvi$ack " |
| 107 |
" }loop " |
| 108 |
"xdvi$ack " |
| 109 |
"}loop\nH"; |
| 110 |
#endif /* SUNHACK */ |
| 111 |
|
| 112 |
extern const char psheader[]; |
| 113 |
extern unsigned psheaderlen; |
| 114 |
|
| 115 |
|
| 116 |
/* global procedures (besides initDPS) */ |
| 117 |
|
| 118 |
static void toggleDPS(int flag); |
| 119 |
static void destroyDPS(void); |
| 120 |
static void interruptDPS(void); |
| 121 |
static void endpageDPS(void); |
| 122 |
static void drawbeginDPS(int, int, const char *); |
| 123 |
static void drawbeginDPS_box(int, int, const char *); |
| 124 |
static void drawrawDPS(const char *); |
| 125 |
static void drawfileDPS(const char *, FILE *); |
| 126 |
static void drawendDPS(const char *); |
| 127 |
static void beginheaderDPS(void); |
| 128 |
static void endheaderDPS(void); |
| 129 |
static void newdocDPS(void); |
| 130 |
|
| 131 |
static struct psprocs dps_procs = { |
| 132 |
/* toggle */ toggleDPS, |
| 133 |
/* destroy */ destroyDPS, |
| 134 |
/* interrupt */ interruptDPS, |
| 135 |
/* endpage */ endpageDPS, |
| 136 |
/* drawbegin */ drawbeginDPS, |
| 137 |
/* drawraw */ drawrawDPS, |
| 138 |
/* drawfile */ drawfileDPS, |
| 139 |
/* drawend */ drawendDPS, |
| 140 |
/* beginheader */ beginheaderDPS, |
| 141 |
/* endheader */ endheaderDPS, |
| 142 |
/* newdoc */ newdocDPS |
| 143 |
}; |
| 144 |
|
| 145 |
#define DPS_MASK_NORMAL EV_GE_NEWPAGE |
| 146 |
#define DPS_MASK_HEADER EV_GE_PS_TOGGLE |
| 147 |
#define DPS_MASK_INIT (EV_GE_TERM | EV_PS_TOGGLE) |
| 148 |
|
| 149 |
static DPSContext DPS_ctx = NULL; |
| 150 |
static DPSSpace DPS_space = NULL; |
| 151 |
static unsigned long DPS_mag; /* magnification currently in use */ |
| 152 |
static int DPS_shrink; /* shrink factor currently in use */ |
| 153 |
static Boolean DPS_active; /* if we've started a page */ |
| 154 |
static int DPS_pending; /* number of ack's we're expecting */ |
| 155 |
static Boolean DPS_in_header; /* if we're sending a header */ |
| 156 |
static Boolean DPS_in_doc; /* if we've sent header information */ |
| 157 |
|
| 158 |
static int DPS_ev_mask = DPS_MASK_NORMAL; /* events for which we'll stop */ |
| 159 |
|
| 160 |
|
| 161 |
#if 0 |
| 162 |
static void DPSErrorProcHandler(void); |
| 163 |
#else |
| 164 |
#define DPSErrorProcHandler DPSDefaultErrorProc |
| 165 |
#endif |
| 166 |
|
| 167 |
|
| 168 |
static char ackstr[] = "xdvi$Ack\n"; |
| 169 |
#if SUNHACK |
| 170 |
static char intstr[] = "xdvi$Int\n"; |
| 171 |
static char intstr2[] = "xdvi$In2\n"; |
| 172 |
#endif |
| 173 |
|
| 174 |
#define LINELEN 81 |
| 175 |
#define BUFLEN (LINELEN + sizeof ackstr) |
| 176 |
static char line[BUFLEN + 1]; |
| 177 |
static char *linepos = line; |
| 178 |
|
| 179 |
static void |
| 180 |
TextProc(DPSContext ctxt, char *buf, unsigned long count) |
| 181 |
{ |
| 182 |
unsigned long i; |
| 183 |
char *p; |
| 184 |
char *p0; |
| 185 |
|
| 186 |
UNUSED(ctxt); |
| 187 |
|
| 188 |
while (count > 0) { |
| 189 |
i = line + BUFLEN - linepos; |
| 190 |
if (i > count) |
| 191 |
i = count; |
| 192 |
memmove(linepos, buf, i); |
| 193 |
linepos += i; |
| 194 |
buf += i; |
| 195 |
count -= i; |
| 196 |
p0 = line; |
| 197 |
for (;;) { |
| 198 |
if (p0 >= linepos) { |
| 199 |
linepos = line; |
| 200 |
break; |
| 201 |
} |
| 202 |
p = memchr(p0, '\n', linepos - p0); |
| 203 |
if (p == NULL) { |
| 204 |
if (p0 != line) { |
| 205 |
memmove(line, p0, linepos - p0); |
| 206 |
linepos -= p0 - line; |
| 207 |
} |
| 208 |
else if (linepos == line + BUFLEN) { |
| 209 |
char c; |
| 210 |
|
| 211 |
c = line[LINELEN]; |
| 212 |
line[LINELEN] = '\0'; |
| 213 |
printf("DPS: %s\n", line); |
| 214 |
line[LINELEN] = c; |
| 215 |
linepos -= LINELEN; |
| 216 |
memmove(line, line + LINELEN, linepos - line); |
| 217 |
} |
| 218 |
break; |
| 219 |
} |
| 220 |
if (p >= p0 + 8 && memcmp(p - 8, ackstr, 9) == 0) { |
| 221 |
--DPS_pending; |
| 222 |
if (DPS_pending == 0) |
| 223 |
globals.ev.flags |= EV_ACK; |
| 224 |
if (globals.debug & DBG_PS) |
| 225 |
printf("Got DPS ack; %d pending.\n", DPS_pending); |
| 226 |
++p; |
| 227 |
memmove(p, p - 9, p, linepos - p); |
| 228 |
linepos -= 9; |
| 229 |
continue; |
| 230 |
} |
| 231 |
#if SUNHACK |
| 232 |
if (p >= p0 + 8 && memcmp(p - 8, intstr, 9) == 0) { |
| 233 |
if (globals.debug & DBG_PS) |
| 234 |
puts("Got DPS int."); |
| 235 |
++p; |
| 236 |
memmove(p - 9, p, linepos - p); |
| 237 |
linepos -= 9; |
| 238 |
globals.ev.flags |= EV_ACK; |
| 239 |
continue; |
| 240 |
} |
| 241 |
if (p >= p0 + 8 && memcmp(p - 8, intstr2, 9) == 0) { |
| 242 |
if (globals.debug & DBG_PS) |
| 243 |
puts("Got DPS int 2."); |
| 244 |
++p; |
| 245 |
memmove(p - 9, p, linepos - p); |
| 246 |
linepos -= 9; |
| 247 |
DPS_pending = 3; |
| 248 |
|
| 249 |
continue; |
| 250 |
} |
| 251 |
#endif /* SUNHACK */ |
| 252 |
*p = '\0'; |
| 253 |
printf("DPS: %s\n", p0); |
| 254 |
p0 = p + 1; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
/*---------------------------------------------------------------------------* |
| 261 |
waitack() |
| 262 |
|
| 263 |
Arguments: none. |
| 264 |
|
| 265 |
Returns: (void) |
| 266 |
|
| 267 |
Description: |
| 268 |
Waits until the requisite number of acknowledgements has been received from |
| 269 |
the context. |
| 270 |
|
| 271 |
+----------------------------------------------------------------------------*/ |
| 272 |
|
| 273 |
#if SUNHACK |
| 274 |
static void DPS_alarm(struct xtimer *this, void *data); |
| 275 |
|
| 276 |
static struct xtimer DPS_timer = {NULL, {0, 0}, XTM_DEFAULT, DPS_alarm, NULL |
| 277 |
#if XDVI_XT_TIMER_HACK |
| 278 |
, NULL, NULL |
| 279 |
#endif |
| 280 |
}; |
| 281 |
|
| 282 |
static Boolean DPS_timer_set; |
| 283 |
|
| 284 |
static void DPS_alarm(struct xtimer *this, void *data) |
| 285 |
{ |
| 286 |
UNUSED(this); |
| 287 |
UNUSED(data); |
| 288 |
if (globals.debug & DBG_PS) |
| 289 |
puts("Received DPS alarm"); |
| 290 |
|
| 291 |
DPS_timer_set = False; |
| 292 |
globals.ev.flags |= EV_ACK; |
| 293 |
} |
| 294 |
#endif /* SUNHACK */ |
| 295 |
|
| 296 |
static void |
| 297 |
waitack(void) |
| 298 |
{ |
| 299 |
if (DPS_pending <= 0) |
| 300 |
return; |
| 301 |
|
| 302 |
DPSFlushContext(DPS_ctx); |
| 303 |
if (read_events(DPS_ev_mask | EV_ACK) & EV_ACK) { |
| 304 |
globals.ev.flags &= ~EV_ACK; |
| 305 |
return; |
| 306 |
} |
| 307 |
|
| 308 |
if (globals.debug & DBG_PS) |
| 309 |
printf("Interrupting DPS in waitack(); code is now %d %x\n", |
| 310 |
XDPSGetContextStatus(DPS_ctx), globals.ev.flags); |
| 311 |
|
| 312 |
DPS_active = DPS_in_header = DPS_in_doc = False; |
| 313 |
DPS_ev_mask = DPS_MASK_NORMAL; |
| 314 |
|
| 315 |
/* |
| 316 |
* I would really like to use DPSInterruptContext() here, but (at least |
| 317 |
* on an RS6000) I can't get it to work. |
| 318 |
*/ |
| 319 |
|
| 320 |
#if SUNHACK |
| 321 |
|
| 322 |
/* |
| 323 |
* On the other hand, under OpenWindows 3.3 (at least), destroying and |
| 324 |
* re-creating contexts has a nasty habit of crashing the server. |
| 325 |
*/ |
| 326 |
|
| 327 |
DPS_pending = 32767; |
| 328 |
DPSInterruptContext(DPS_ctx); |
| 329 |
DPSFlushContext(DPS_ctx); |
| 330 |
(void) read_events(EV_GE_TERM | EV_ACK); |
| 331 |
globals.ev.flags &= ~EV_ACK; |
| 332 |
|
| 333 |
XSync(DISP, False); |
| 334 |
DPSPrintf(DPS_ctx, " stop\n%%%%xdvimark\n stop\n%%%%xdvimark\n"); |
| 335 |
DPSPrintf(DPS_ctx, " (xdvi$In2\n) print flush stop\n%%%%xdvimark\n"); |
| 336 |
DPSPrintf(DPS_ctx, " stop\n%%%%xdvimark\n stop\n%%%%xdvimark\n"); |
| 337 |
DPSFlushContext(DPS_ctx); |
| 338 |
|
| 339 |
if (globals.debug & DBG_PS) |
| 340 |
puts("Setting DPS timer"); |
| 341 |
|
| 342 |
DPS_timer_set = True; |
| 343 |
set_timer(&DPS_timer, 500); |
| 344 |
(void) read_events(EV_GE_TERM | EV_ACK); |
| 345 |
globals.ev.flags &= ~EV_ACK; |
| 346 |
|
| 347 |
if (DPS_pending <= 0) { |
| 348 |
if (DPS_timer_set) { |
| 349 |
cancel_timer(&DPS_timer); |
| 350 |
} |
| 351 |
|
| 352 |
return; |
| 353 |
} |
| 354 |
|
| 355 |
/* |
| 356 |
* However, under Solaris 2.6 (at least), sometimes interrupting the |
| 357 |
* context leaves it in an uncommunicative state, so destruction |
| 358 |
* is the only alternative. |
| 359 |
*/ |
| 360 |
|
| 361 |
if (globals.debug & DBG_PS) |
| 362 |
puts("Plan B: Destroying DPS context"); |
| 363 |
|
| 364 |
#endif /* SUNHACK */ |
| 365 |
|
| 366 |
DPSDestroyContext(DPS_ctx); |
| 367 |
DPS_ctx = NULL; |
| 368 |
DPS_pending = 0; |
| 369 |
} |
| 370 |
|
| 371 |
|
| 372 |
/*---------------------------------------------------------------------------* |
| 373 |
initDPS() |
| 374 |
|
| 375 |
Arguments: (none) |
| 376 |
Returns: (void) |
| 377 |
Side-Effects: DPS_ctx may be set as well as other static variables. |
| 378 |
|
| 379 |
Description: |
| 380 |
Initializes variables from the application main loop. Checks to see if |
| 381 |
a connection to the DPS server can be opened. |
| 382 |
|
| 383 |
+----------------------------------------------------------------------------*/ |
| 384 |
|
| 385 |
static int |
| 386 |
get_shift(Pixel mask) |
| 387 |
{ |
| 388 |
int k; |
| 389 |
|
| 390 |
for (k = 0; (mask & 1) == 0; ++k) |
| 391 |
mask >>= 1; |
| 392 |
return k; |
| 393 |
} |
| 394 |
|
| 395 |
Boolean initDPS(void) |
| 396 |
{ |
| 397 |
|
| 398 |
/* Try to create a context */ |
| 399 |
|
| 400 |
#if GREY |
| 401 |
|
| 402 |
if (G_colormap == DefaultColormapOfScreen(SCRN)) |
| 403 |
DPS_ctx = XDPSCreateSimpleContext(DISP, mane.win, globals.gc.copy, 0, 0, |
| 404 |
TextProc, DPSDefaultErrorProc, NULL); |
| 405 |
else { |
| 406 |
static XStandardColormap *ccube = NULL; |
| 407 |
static XStandardColormap *grayramp = NULL; |
| 408 |
int shift; |
| 409 |
|
| 410 |
if (grayramp == NULL) { |
| 411 |
grayramp = XAllocStandardColormap(); |
| 412 |
if (grayramp == NULL) |
| 413 |
return False; |
| 414 |
} |
| 415 |
|
| 416 |
if (ccube == NULL) { |
| 417 |
ccube = XAllocStandardColormap(); |
| 418 |
if (ccube == NULL) |
| 419 |
return False; |
| 420 |
} |
| 421 |
|
| 422 |
shift = get_shift(G_visual->red_mask); |
| 423 |
ccube->red_max = G_visual->red_mask >> shift; |
| 424 |
ccube->red_mult = 1 << shift; |
| 425 |
|
| 426 |
shift = get_shift(G_visual->green_mask); |
| 427 |
ccube->green_max = G_visual->green_mask >> shift; |
| 428 |
ccube->green_mult = 1 << shift; |
| 429 |
|
| 430 |
shift = get_shift(G_visual->blue_mask); |
| 431 |
ccube->blue_max = G_visual->blue_mask >> shift; |
| 432 |
ccube->blue_mult = 1 << shift; |
| 433 |
|
| 434 |
grayramp->red_max = ccube->red_max & ccube->green_max & ccube->blue_max; |
| 435 |
grayramp->red_mult = ccube->red_mult + ccube->green_mult |
| 436 |
+ ccube->blue_mult; |
| 437 |
|
| 438 |
ccube->colormap = grayramp->colormap = G_colormap; |
| 439 |
ccube->visualid = grayramp->visualid = G_visual->visualid; |
| 440 |
|
| 441 |
DPS_ctx = XDPSCreateContext(DISP, mane.win, globals.gc.copy, 0, 0, |
| 442 |
0, grayramp, ccube, |
| 443 |
/* actual */ |
| 444 |
(ccube->red_max + 1) * (ccube->green_max + |
| 445 |
1) * |
| 446 |
(ccube->blue_max + 1), TextProc, |
| 447 |
DPSDefaultErrorProc, NULL); |
| 448 |
} |
| 449 |
|
| 450 |
#else /* not GREY */ |
| 451 |
|
| 452 |
DPS_ctx = XDPSCreateSimpleContext(DISP, mane.win, globals.gc.copy, 0, 0, |
| 453 |
TextProc, DPSDefaultErrorProc, NULL); |
| 454 |
|
| 455 |
#endif /* not GREY */ |
| 456 |
|
| 457 |
if (DPS_ctx == NULL) |
| 458 |
return False; |
| 459 |
|
| 460 |
DPS_mag = DPS_shrink = -1; |
| 461 |
DPS_active = False; |
| 462 |
DPS_pending = 1; |
| 463 |
|
| 464 |
DPS_space = DPSSpaceFromContext(DPS_ctx); |
| 465 |
DPSWritePostScript(DPS_ctx, preamble, (sizeof preamble) - 1); |
| 466 |
DPSWritePostScript(DPS_ctx, (char *)psheader, psheaderlen); |
| 467 |
DPSPrintf(DPS_ctx, "matrix setmatrix stop\n%%%%xdvimark\n"); |
| 468 |
DPSFlushContext(DPS_ctx); |
| 469 |
|
| 470 |
psp = dps_procs; |
| 471 |
return True; |
| 472 |
} |
| 473 |
|
| 474 |
|
| 475 |
/*---------------------------------------------------------------------------* |
| 476 |
toggleDPS(int flag) |
| 477 |
|
| 478 |
Arguments: flag for toggling PostScript |
| 479 |
Returns: (void) |
| 480 |
Side-Effects: psp.drawbegin is changed. |
| 481 |
|
| 482 |
Description: |
| 483 |
Used to toggle the rendering of PostScript by the DPS server |
| 484 |
This routine may be called from within read_events(). |
| 485 |
|
| 486 |
+----------------------------------------------------------------------------*/ |
| 487 |
|
| 488 |
static void |
| 489 |
toggleDPS(int flag) |
| 490 |
{ |
| 491 |
if (globals.debug & DBG_PS) |
| 492 |
fprintf(stderr, "Toggling DPS to %d", flag); |
| 493 |
|
| 494 |
switch (flag) { |
| 495 |
case 0: |
| 496 |
psp.drawbegin = drawbegin_none; |
| 497 |
break; |
| 498 |
case 1: |
| 499 |
psp.drawbegin = drawbeginDPS; |
| 500 |
break; |
| 501 |
default: |
| 502 |
psp.drawbegin = drawbeginDPS_box; |
| 503 |
break; |
| 504 |
} |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
/*---------------------------------------------------------------------------* |
| 509 |
destroyDPS() |
| 510 |
|
| 511 |
Arguments: none |
| 512 |
Returns: (void) |
| 513 |
Side-Effects: the context is nulled out and destroyed. |
| 514 |
|
| 515 |
Description: |
| 516 |
Close the connection to the DPS server; used when rendering is terminated |
| 517 |
in any way. |
| 518 |
|
| 519 |
+----------------------------------------------------------------------------*/ |
| 520 |
|
| 521 |
static void |
| 522 |
destroyDPS(void) |
| 523 |
{ |
| 524 |
if (globals.debug & DBG_PS) |
| 525 |
puts("Calling destroyDPS()"); |
| 526 |
if (linepos > line) { |
| 527 |
*linepos = '\0'; |
| 528 |
printf("DPS: %s\n", line); |
| 529 |
} |
| 530 |
if (DPS_space != NULL) |
| 531 |
DPSDestroySpace(DPS_space); |
| 532 |
psp = no_ps_procs; |
| 533 |
scanned_page = scanned_page_ps = scanned_page_reset; |
| 534 |
} |
| 535 |
|
| 536 |
|
| 537 |
/*---------------------------------------------------------------------------* |
| 538 |
interruptDPS() |
| 539 |
|
| 540 |
Arguments: none |
| 541 |
Returns: (void) |
| 542 |
Side-Effects: the context may be nulled out and destroyed. |
| 543 |
|
| 544 |
Description: |
| 545 |
Close the connection to the DPS server; used when rendering is terminated |
| 546 |
because of an interruption in the viewing of the current page. |
| 547 |
|
| 548 |
+----------------------------------------------------------------------------*/ |
| 549 |
|
| 550 |
static void |
| 551 |
interruptDPS(void) |
| 552 |
{ |
| 553 |
if (globals.debug & DBG_PS) |
| 554 |
puts("Running interruptDPS()"); |
| 555 |
|
| 556 |
if (DPS_pending <= 0) |
| 557 |
return; /* nothing to do */ |
| 558 |
|
| 559 |
if (DPS_active) { |
| 560 |
DPSPrintf(DPS_ctx, "stop\n%%%%xdvimark\n"); |
| 561 |
DPS_active = False; |
| 562 |
} |
| 563 |
|
| 564 |
waitack(); |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
/*---------------------------------------------------------------------------* |
| 569 |
endpageDPS() |
| 570 |
|
| 571 |
Arguments: none |
| 572 |
Returns: (void) |
| 573 |
Side-Effects: the DPS_active variable is cleared. |
| 574 |
|
| 575 |
Description: |
| 576 |
Should be called at the end of a page to end this chunk for the DPS server. |
| 577 |
|
| 578 |
+----------------------------------------------------------------------------*/ |
| 579 |
|
| 580 |
static void |
| 581 |
endpageDPS(void) |
| 582 |
{ |
| 583 |
if (DPS_active) { |
| 584 |
if (globals.debug & DBG_PS) |
| 585 |
puts("Endpage sent to context"); |
| 586 |
DPSPrintf(DPS_ctx, "stop\n%%%%xdvimark\n"); |
| 587 |
DPS_active = False; |
| 588 |
waitack(); |
| 589 |
} |
| 590 |
} |
| 591 |
|
| 592 |
|
| 593 |
/* |
| 594 |
* checkDPS - Check that the DPS interpreter is still running. |
| 595 |
* Return True for success, False for failure. |
| 596 |
*/ |
| 597 |
|
| 598 |
static Boolean |
| 599 |
checkDPS(void) |
| 600 |
{ |
| 601 |
/* static char faulty_display_vs[] |
| 602 |
* ="DECWINDOWS DigitalEquipmentCorporation UWS4.2LA"; */ |
| 603 |
|
| 604 |
if (DPS_ctx == NULL) { |
| 605 |
DPS_ctx = XDPSCreateSimpleContext(DISP, mane.win, globals.gc.copy, 0, 0, |
| 606 |
TextProc, DPSErrorProcHandler, DPS_space); |
| 607 |
if (DPS_ctx == NULL) { |
| 608 |
psp = no_ps_procs; |
| 609 |
draw_bbox(); |
| 610 |
return False; |
| 611 |
} |
| 612 |
DPSWritePostScript(DPS_ctx, preamble, (sizeof preamble) - 1); |
| 613 |
/* it already has psheader */ |
| 614 |
DPSPrintf(DPS_ctx, "matrix setmatrix stop\n%%%%xdvimark\n"); |
| 615 |
DPS_mag = DPS_shrink = -1; |
| 616 |
DPS_active = False; |
| 617 |
DPS_pending = 1; |
| 618 |
} |
| 619 |
|
| 620 |
return True; |
| 621 |
} |
| 622 |
|
| 623 |
|
| 624 |
/*---------------------------------------------------------------------------* |
| 625 |
drawbeginDPS () |
| 626 |
|
| 627 |
Arguments: xul, yul - coordinates of the upper left corner of the figure |
| 628 |
cp - string with the bounding box line data |
| 629 |
Returns: (void) |
| 630 |
Side-Effects: DPS_ctx is set is set and connection to DPS server is |
| 631 |
opened. |
| 632 |
|
| 633 |
Description: |
| 634 |
Opens a connection to the DPS server and send in the preamble and the |
| 635 |
bounding box information after correctly computing resolution factors. |
| 636 |
In case no rendering is to be done, outlines the figure. |
| 637 |
An outline is also generated whenever the a context cannot be allocated |
| 638 |
|
| 639 |
+----------------------------------------------------------------------------*/ |
| 640 |
|
| 641 |
static void |
| 642 |
drawbeginDPS(int xul, int yul, const char *cp) |
| 643 |
{ |
| 644 |
if (globals.debug & DBG_PS) |
| 645 |
printf("Begin drawing at xul= %d, yul= %d.\n", xul, yul); |
| 646 |
|
| 647 |
if (!checkDPS()) return; |
| 648 |
|
| 649 |
if (!DPS_active) { |
| 650 |
/* send initialization to context */ |
| 651 |
if (magnification != DPS_mag) { |
| 652 |
DPSPrintf(DPS_ctx, "H TeXDict begin /DVImag %d 1000 div def " |
| 653 |
"end stop\n%%%%xdvimark\n", DPS_mag = magnification); |
| 654 |
++DPS_pending; |
| 655 |
} |
| 656 |
if (mane.shrinkfactor != DPS_shrink) { |
| 657 |
DPSPrintf(DPS_ctx, "H TeXDict begin %d %d div dup " |
| 658 |
"/Resolution X /VResolution X " |
| 659 |
"end stop\n%%%%xdvimark\n", resource.pixels_per_inch, DPS_shrink = mane.shrinkfactor); |
| 660 |
++DPS_pending; |
| 661 |
} |
| 662 |
DPSPrintf(DPS_ctx, " TeXDict begin\n"); |
| 663 |
DPS_active = True; |
| 664 |
++DPS_pending; |
| 665 |
} |
| 666 |
|
| 667 |
if (globals.ev.flags & DPS_MASK_NORMAL) |
| 668 |
longjmp(globals.ev.canit, 1); |
| 669 |
|
| 670 |
DPSPrintf(DPS_ctx, "%d %d moveto\n", xul, yul); |
| 671 |
DPSPrintf(DPS_ctx, "%s\n", cp); |
| 672 |
} |
| 673 |
|
| 674 |
static void |
| 675 |
drawbeginDPS_box(int xul, int yul, const char *cp) |
| 676 |
{ |
| 677 |
drawbeginDPS(xul, yul, cp); |
| 678 |
draw_bbox(); |
| 679 |
} |
| 680 |
|
| 681 |
/*---------------------------------------------------------------------------* |
| 682 |
|
| 683 |
drawrawDPS() |
| 684 |
|
| 685 |
Arguments: cp - the raw string to be sent to the postscript interpreter |
| 686 |
Returns: (void) |
| 687 |
Side-Effects: (none) |
| 688 |
|
| 689 |
Description: |
| 690 |
If there is a valid postscript context, just send the string to the |
| 691 |
interpreter, else leave. |
| 692 |
|
| 693 |
+----------------------------------------------------------------------------*/ |
| 694 |
|
| 695 |
static void |
| 696 |
drawrawDPS(const char *cp) |
| 697 |
{ |
| 698 |
if (!DPS_active || (globals.ev.flags & DPS_ev_mask)) |
| 699 |
return; |
| 700 |
|
| 701 |
if (globals.debug & DBG_PS) |
| 702 |
printf("Sending raw PS to context: %s\n", cp); |
| 703 |
|
| 704 |
DPSPrintf(DPS_ctx, "%s\n", cp); |
| 705 |
} |
| 706 |
|
| 707 |
|
| 708 |
/*---------------------------------------------------------------------------* |
| 709 |
drawfileDPS() |
| 710 |
|
| 711 |
Arguments: cp - string with the postscript file pathname |
| 712 |
psfile - opened file pointer |
| 713 |
Returns: (void) |
| 714 |
Side-Effects: none |
| 715 |
|
| 716 |
Description: |
| 717 |
Postscript file containing the figure is opened and sent to the DPS server. |
| 718 |
|
| 719 |
+----------------------------------------------------------------------------*/ |
| 720 |
|
| 721 |
static void |
| 722 |
drawfileDPS(const char *cp, FILE *psfile) |
| 723 |
{ |
| 724 |
char buffer[1025]; |
| 725 |
int blen; |
| 726 |
|
| 727 |
if (DPS_active && !(read_events(EV_NOWAIT) & DPS_ev_mask)) { |
| 728 |
if (globals.debug & DBG_PS) |
| 729 |
printf("sending file %s\n", cp); |
| 730 |
for (;;) { |
| 731 |
blen = fread(buffer, sizeof(char), 1024, psfile); |
| 732 |
if (blen == 0) break; |
| 733 |
DPSWritePostScript(DPS_ctx, buffer, blen); |
| 734 |
if (read_events(EV_NOWAIT) & DPS_ev_mask) { |
| 735 |
/* ||| stop at a good place */ |
| 736 |
if (globals.debug & DBG_PS) |
| 737 |
puts("Interrupting in drawfileDPS"); |
| 738 |
break; |
| 739 |
} |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
fclose(psfile); |
| 744 |
|
| 745 |
if (globals.ev.flags & DPS_ev_mask) |
| 746 |
longjmp(globals.ev.canit, 1); |
| 747 |
} |
| 748 |
|
| 749 |
|
| 750 |
/*---------------------------------------------------------------------------* |
| 751 |
drawendDPS() |
| 752 |
|
| 753 |
Arguments: cp - string with indication of the end of the special |
| 754 |
Returns: (void) |
| 755 |
Side-Effects: none |
| 756 |
|
| 757 |
Description: |
| 758 |
Sends the indication of end of the figure PostScript code. |
| 759 |
|
| 760 |
+----------------------------------------------------------------------------*/ |
| 761 |
|
| 762 |
static void |
| 763 |
drawendDPS(const char *cp) |
| 764 |
{ |
| 765 |
if (!DPS_active || (globals.ev.flags & DPS_MASK_NORMAL)) |
| 766 |
return; |
| 767 |
|
| 768 |
if (globals.debug & DBG_PS) |
| 769 |
printf("End PS: %s\n", cp); |
| 770 |
DPSPrintf(DPS_ctx, "%s\n", cp); |
| 771 |
} |
| 772 |
|
| 773 |
|
| 774 |
/*---------------------------------------------------------------------------* |
| 775 |
beginheaderDPS() |
| 776 |
|
| 777 |
Arguments: none |
| 778 |
Returns: (void) |
| 779 |
|
| 780 |
Description: |
| 781 |
Prepares the PostScript interpreter for receipt of header code. |
| 782 |
|
| 783 |
+----------------------------------------------------------------------------*/ |
| 784 |
|
| 785 |
static void |
| 786 |
beginheaderDPS(void) |
| 787 |
{ |
| 788 |
if (globals.ev.flags & DPS_MASK_HEADER || !checkDPS()) |
| 789 |
return; |
| 790 |
|
| 791 |
if (globals.debug & DBG_PS) |
| 792 |
puts("Running beginheaderDPS()"); |
| 793 |
|
| 794 |
if (DPS_active) { |
| 795 |
if (!DPS_in_header) |
| 796 |
XDVI_FATAL((stderr, "Internal error in beginheaderDPS().")); |
| 797 |
return; |
| 798 |
} |
| 799 |
|
| 800 |
DPS_in_header = True; |
| 801 |
DPS_ev_mask = DPS_MASK_HEADER; |
| 802 |
|
| 803 |
if (DPS_in_doc) |
| 804 |
DPSPrintf(DPS_ctx, "H"); |
| 805 |
else { |
| 806 |
DPSPrintf(DPS_ctx, "Hsave /xdvi$doc exch def\n"); |
| 807 |
DPS_in_doc = True; |
| 808 |
} |
| 809 |
DPS_active = True; |
| 810 |
++DPS_pending; |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
/*---------------------------------------------------------------------------* |
| 815 |
endheaderDPS() |
| 816 |
|
| 817 |
Arguments: none |
| 818 |
Returns: (void) |
| 819 |
|
| 820 |
Description: |
| 821 |
Prepares the PostScript interpreter for receipt of header code. |
| 822 |
|
| 823 |
+----------------------------------------------------------------------------*/ |
| 824 |
|
| 825 |
static void |
| 826 |
endheaderDPS(void) |
| 827 |
{ |
| 828 |
if (globals.debug & DBG_PS) |
| 829 |
puts("Running endheaderDPS()"); |
| 830 |
|
| 831 |
if (DPS_active) { |
| 832 |
DPSPrintf(DPS_ctx, "stop\n%%%%xdvimark\n"); |
| 833 |
DPS_active = False; |
| 834 |
waitack(); |
| 835 |
DPS_in_header = False; |
| 836 |
DPS_ev_mask = DPS_MASK_NORMAL; |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
|
| 841 |
/*---------------------------------------------------------------------------* |
| 842 |
newdocDPS() |
| 843 |
|
| 844 |
Arguments: none |
| 845 |
Returns: (void) |
| 846 |
|
| 847 |
Description: |
| 848 |
Clears out headers stored from the previous document. |
| 849 |
|
| 850 |
+----------------------------------------------------------------------------*/ |
| 851 |
|
| 852 |
static void |
| 853 |
newdocDPS(void) |
| 854 |
{ |
| 855 |
if (globals.debug & DBG_PS) |
| 856 |
puts("Running newdocDPS()"); |
| 857 |
|
| 858 |
if (DPS_in_doc) { |
| 859 |
DPSPrintf(DPS_ctx, "H xdvi$doc restore stop\n%%%%xdvimark\n"); |
| 860 |
++DPS_pending; |
| 861 |
DPS_mag = DPS_shrink = -1; |
| 862 |
DPS_in_doc = False; |
| 863 |
} |
| 864 |
} |
| 865 |
|
| 866 |
#else |
| 867 |
/* silence `empty compilation unit' warnings */ |
| 868 |
static void bar(void); static void foo(void) { bar(); } static void bar(void) { foo(); } |
| 869 |
#endif /* PS_DPS */ |