Debian Codegraph @

texk/xdvik/special.c

raw
1 /*========================================================================*\
2
3 Copyright (c) 1990-2019 Paul Vojta and others
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 NOTE:
24 This module is based on prior work as noted below.
25
26 \*========================================================================*/
27
28 /*
29 * Support drawing routines for TeXsun and TeX
30 *
31 * Copyright, (C) 1987, 1988 Tim Morgan, UC Irvine
32 * Adapted for xdvi by Jeffrey Lee, U. of Toronto
33 *
34 * At the time these routines are called, the values of hh and vv should
35 * have been updated to the upper left corner of the graph (the position
36 * the \special appears at in the dvi file). Then the coordinates in the
37 * graphics commands are in terms of a virtual page with axes oriented the
38 * same as the Imagen and the SUN normally have:
39 *
40 * 0,0
41 * +-----------> +x
42 * |
43 * |
44 * |
45 * \ /
46 * +y
47 *
48 * Angles are measured in the conventional way, from +x towards +y.
49 * Unfortunately, that reverses the meaning of "counterclockwise"
50 * from what it's normally thought of.
51 *
52 * A lot of floating point arithmetic has been converted to integer
53 * arithmetic for speed. In some places, this is kind-of kludgy, but
54 * it's worth it.
55 */
56
57
58 #include "xdvi-config.h"
59 #include "xdvi.h"
60
61 #include <setjmp.h>
62
63 #include <ctype.h>
64
65 #include "kpathsea/c-fopen.h"
66 #include "kpathsea/c-stat.h"
67 #include "kpathsea/line.h"
68 #include "kpathsea/tex-file.h"
69
70 #include "special.h"
71 #include "hypertex.h"
72 #include "dvi.h"
73 #include "message-window.h"
74 #include "events.h"
75 #include "dvi-init.h"
76 #include "dvi-draw.h"
77 #include "statusline.h"
78 #include "util.h"
79 #include "image-magick.h"
80 #include "pagesel.h"
81 #include "my-snprintf.h"
82 #include "string-utils.h"
83
84 #if PS
85 # ifdef PS_DPS
86 # include "psdps.h"
87 # endif
88 # ifdef PS_NEWS
89 # include "psnews.h"
90 # endif
91 # ifdef PS_GS
92 # include "psgs.h"
93 # endif
94
95 /*
96 Code inside BUG_888087_FIXED improve the appearance of pic specials at magstep 1, but
97 _decreases_ it at other magnifications (bug #888087; see
98 http://sourceforge.net/tracker/index.php?func=detail&aid=888087&group_id=23164&atid=377580)
99 Disabled until this is fixed.
100 */
101 #define BUG_888087_FIXED 0
102
103
104 /*
105 Flag whether this page contains raw PS material;
106 used to warn user about this:
107 */
108 Boolean have_raw_postscript = False;
109
110 #endif
111
112
113 #if COLOR
114
115 /*
116 * Color stack used when scanning. These records stay around, to ease
117 * the burden on xmalloc(). The first entry is a dummy entry, giving
118 * the default foreground color (as modified).
119 */
120
121 struct colorframe {
122 struct colorframe *next, *prev;
123 struct rgb color;
124 };
125
126 static Boolean m_have_papersize_special = False;
127
128 void reset_papersize_special(void)
129 {
130 m_have_papersize_special = False;
131 }
132
133 Boolean have_papersize_special(void)
134 {
135 return m_have_papersize_special;
136 }
137
138 static struct colorframe scanstack_head;
139 static int scanstack_len;
140 static struct colorframe *scanstack_current;
141
142 /*
143 * Page stack when displaying. Again, the records stay around once
144 * allocated. Bottom entries in the stack (inherited from previous
145 * pages) are stored in an array instead (see comments in xdvi.h).
146 */
147
148 static struct colorframe *rcs_head;
149
150 static Boolean parse_color (const char *cp0, const char *cp, struct rgb *rgbp, Boolean generic_ps_flag);
151
152 #endif /* COLOR */
153
154
155 #ifndef S_IRUSR
156 # define S_IRUSR 0400
157 #endif
158 #ifndef S_IWUSR
159 # define S_IWUSR 0200
160 #endif
161
162 # if HAVE_SYS_WAIT_H
163 # include <sys/wait.h>
164 # endif
165 # ifndef WEXITSTATUS
166 # define WEXITSTATUS(stat_val) ((unsigned)(stat_val) >> 8)
167 # endif
168 # ifndef WIFEXITED
169 # define WIFEXITED(stat_val) (((stat_val) & 255) == 0)
170 # endif
171
172 #define MAXPOINTS 300 /* Max points in a path */
173 #define MAX_PEN_SIZE 7 /* Max pixels of pen width */
174 #define TWOPI (3.14159265359 * 2.0)
175
176 extern double floor(double);
177 #define rint(x) floor((x) + 0.5)
178
179 static int xx[MAXPOINTS], yy[MAXPOINTS]; /* Path in milli-inches */
180 static int path_len = 0; /* # points in current path */
181 static int pen_size = 1; /* Pixel width of lines drawn */
182
183 static Boolean whiten = False;
184 static Boolean shade = False;
185 static Boolean blacken = False;
186
187 Boolean psfig_begun = False;
188
189 #define CMD(x, y) ((x) << 8 | (y))
190
191
192 /*
193 * X drawing routines
194 */
195
196 #define toint(x) ((int) ((x) + 0.5))
197 #define xconv(x) (toint(tpic_conv*(x))/currwin.shrinkfactor + PXL_H)
198 #define yconv(y) (toint(tpic_conv*(y))/currwin.shrinkfactor + PXL_V)
199
200 /*
201 * Draw a line from (fx,fy) to (tx,ty).
202 */
203 static void
204 line_btw(int fx, int fy, int tx, int ty)
205 {
206 int fcx = xconv(fx);
207 int tcx = xconv(tx);
208 int fcy = yconv(fy);
209 int tcy = yconv(ty);
210
211 if ((fcx < globals.win_expose.max_x || tcx < globals.win_expose.max_x) && (fcx >= globals.win_expose.min_x || tcx >= globals.win_expose.min_x) &&
212 (fcy < globals.win_expose.max_y || tcy < globals.win_expose.max_y) && (fcy >= globals.win_expose.min_y || tcy >= globals.win_expose.min_y)) {
213 #if COLOR
214 if (fg_active != fg_current)
215 do_color_change();
216 #endif
217 XDrawLine(DISP, currwin.win, globals.gc.rule,
218 fcx - currwin.base_x, fcy - currwin.base_y,
219 tcx - currwin.base_x, tcy - currwin.base_y);
220 }
221 }
222
223 /*
224 * Draw a dot at (x,y)
225 */
226 static void
227 dot_at(int x, int y)
228 {
229 int cx = xconv(x);
230 int cy = yconv(y);
231
232 if (cx < globals.win_expose.max_x && cx >= globals.win_expose.min_x && cy < globals.win_expose.max_y && cy >= globals.win_expose.min_y){
233 #if COLOR
234 if (fg_active != fg_current)
235 do_color_change();
236 #endif
237 XDrawPoint(DISP, currwin.win, globals.gc.rule,
238 cx - currwin.base_x, cy - currwin.base_y);
239 }
240 }
241
242 /*
243 * Apply the requested attributes to the last path (box) drawn.
244 * Attributes are reset.
245 * (Not currently implemented.)
246 */
247 static void
248 do_attribute_path(int last_min_x, int last_max_x, int last_min_y, int last_max_y)
249 {
250 UNUSED(last_min_x);
251 UNUSED(last_max_x);
252 UNUSED(last_min_y);
253 UNUSED(last_max_y);
254 }
255
256 /*
257 * Set the size of the virtual pen used to draw in milli-inches
258 */
259 static void
260 set_pen_size(char *cp)
261 {
262 int ps;
263
264 if (sscanf(cp, " %d ", &ps) != 1) {
265 XDVI_WARNING((stderr, "invalid .ps command format: %s", cp));
266 return;
267 }
268 pen_size = (2 * ps * resource.pixels_per_inch / currwin.shrinkfactor + 1000) / 2000;
269 if (pen_size < 1)
270 pen_size = 1;
271 else if (pen_size > MAX_PEN_SIZE)
272 pen_size = MAX_PEN_SIZE;
273
274 #if BUG_888087_FIXED
275 if (globals.gc.rule) {
276 XGCValues values;
277 values.line_width = pen_size;
278 XChangeGC(DISP, globals.gc.rule, GCLineWidth, &values);
279 }
280 #endif
281 }
282
283
284 /*
285 * Print the line defined by previous path commands
286 */
287
288 static void
289 flush_path(void)
290 {
291 int i;
292 int last_min_x, last_max_x, last_min_y, last_max_y;
293
294 last_min_x = 30000;
295 last_min_y = 30000;
296 last_max_x = -30000;
297 last_max_y = -30000;
298 for (i = 1; i < path_len; i++) {
299 if (xx[i] > last_max_x)
300 last_max_x = xx[i];
301 if (xx[i] < last_min_x)
302 last_min_x = xx[i];
303 if (yy[i] > last_max_y)
304 last_max_y = yy[i];
305 if (yy[i] < last_min_y)
306 last_min_y = yy[i];
307 line_btw(xx[i], yy[i], xx[i + 1], yy[i + 1]);
308 }
309 if (xx[path_len] > last_max_x)
310 last_max_x = xx[path_len];
311 if (xx[path_len] < last_min_x)
312 last_min_x = xx[path_len];
313 if (yy[path_len] > last_max_y)
314 last_max_y = yy[path_len];
315 if (yy[path_len] < last_min_y)
316 last_min_y = yy[path_len];
317 path_len = 0;
318 do_attribute_path(last_min_x, last_max_x, last_min_y, last_max_y);
319 }
320
321
322 /*
323 * Print a dashed line along the previously defined path, with
324 * the dashes/inch defined.
325 */
326
327 static void
328 flush_dashed(char *cp, Boolean dotted)
329 {
330 int i;
331 int numdots;
332 int lx0, ly0, lx1, ly1;
333 int cx0, cy0, cx1, cy1;
334 float inchesperdash;
335 double d, spacesize, a, b = 0.0, dx, dy, milliperdash;
336
337 if (sscanf(cp, " %f ", &inchesperdash) != 1) {
338 XDVI_WARNING((stderr, "invalid format for dotted/dashed line: %s", cp));
339 return;
340 }
341 if (path_len <= 1 || inchesperdash <= 0.0) {
342 XDVI_WARNING((stderr, "invalid conditions for dotted/dashed line"));
343 return;
344 }
345 milliperdash = inchesperdash * 1000.0;
346 lx0 = xx[1];
347 ly0 = yy[1];
348 lx1 = xx[2];
349 ly1 = yy[2];
350 dx = lx1 - lx0;
351 dy = ly1 - ly0;
352 if (dotted) {
353 numdots = sqrt(dx * dx + dy * dy) / milliperdash + 0.5;
354 if (numdots == 0)
355 numdots = 1;
356 for (i = 0; i <= numdots; i++) {
357 a = (float)i / (float)numdots;
358 cx0 = lx0 + a * dx + 0.5;
359 cy0 = ly0 + a * dy + 0.5;
360 dot_at(cx0, cy0);
361 }
362 }
363 else {
364 d = sqrt(dx * dx + dy * dy);
365 numdots = d / (2.0 * milliperdash) + 1.0;
366 if (numdots <= 1)
367 line_btw(lx0, ly0, lx1, ly1);
368 else {
369 spacesize = (d - numdots * milliperdash) / (numdots - 1);
370 for (i = 0; i < numdots - 1; i++) {
371 a = i * (milliperdash + spacesize) / d;
372 b = a + milliperdash / d;
373 cx0 = lx0 + a * dx + 0.5;
374 cy0 = ly0 + a * dy + 0.5;
375 cx1 = lx0 + b * dx + 0.5;
376 cy1 = ly0 + b * dy + 0.5;
377 line_btw(cx0, cy0, cx1, cy1);
378 b += spacesize / d;
379 }
380 cx0 = lx0 + b * dx + 0.5;
381 cy0 = ly0 + b * dy + 0.5;
382 line_btw(cx0, cy0, lx1, ly1);
383 }
384 }
385 path_len = 0;
386 }
387
388
389 /*
390 * Add a point to the current path
391 */
392
393 static void
394 add_path(char *cp)
395 {
396 int pathx, pathy;
397
398 if (++path_len >= MAXPOINTS)
399 XDVI_FATAL((stderr, "Too many points"));
400 if (sscanf(cp, " %d %d ", &pathx, &pathy) != 2)
401 XDVI_FATAL((stderr, "Malformed path command"));
402 xx[path_len] = pathx;
403 yy[path_len] = pathy;
404 }
405
406
407 /*
408 * Draw to a floating point position
409 */
410
411 static void
412 im_fdraw(double x, double y)
413 {
414 if (++path_len >= MAXPOINTS)
415 XDVI_FATAL((stderr, "Too many arc points"));
416 xx[path_len] = x + 0.5;
417 yy[path_len] = y + 0.5;
418 }
419
420
421 /*
422 * Draw an ellipse with the indicated center and radices.
423 */
424
425 static void
426 draw_ellipse(int xc, int yc, int xr, int yr)
427 {
428 double angle, theta;
429 int n;
430 int px0, py0, px1, py1;
431
432 angle = (xr + yr) / 2.0;
433 theta = sqrt(1.0 / angle);
434 n = TWOPI / theta + 0.5;
435 if (n < 12)
436 n = 12;
437 else if (n > 80)
438 n = 80;
439 n /= 2;
440 theta = TWOPI / n;
441
442 angle = 0.0;
443 px0 = xc + xr; /* cos(0) = 1 */
444 py0 = yc; /* sin(0) = 0 */
445 while ((angle += theta) <= TWOPI) {
446 px1 = xc + xr * cos(angle) + 0.5;
447 py1 = yc + yr * sin(angle) + 0.5;
448 line_btw(px0, py0, px1, py1);
449 px0 = px1;
450 py0 = py1;
451 }
452 line_btw(px0, py0, xc + xr, yc);
453 }
454
455 /*
456 * Draw an arc
457 */
458
459 static void
460 arc(char *cp, Boolean invis)
461 {
462 int xc, yc, xrad, yrad, n;
463 float start_angle, end_angle, angle, theta, r;
464 double xradius, yradius, xcenter, ycenter;
465
466 n = sscanf(cp, " %d %d %d %d %f %f ", &xc, &yc, &xrad, &yrad,
467 &start_angle, &end_angle);
468
469 if (n != 6) {
470 XDVI_WARNING((stderr, "invalid arc specification: %s", cp));
471 return;
472 }
473
474 if (invis)
475 return;
476
477 /* We have a specialized fast way to draw closed circles/ellipses */
478 if (start_angle <= 0.0 && end_angle >= 6.282) {
479 draw_ellipse(xc, yc, xrad, yrad);
480 return;
481 }
482 xcenter = xc;
483 ycenter = yc;
484 xradius = xrad;
485 yradius = yrad;
486 r = (xradius + yradius) / 2.0;
487 theta = sqrt(1.0 / r);
488 #if BUG_888087_FIXED
489 n = (pen_size * TWOPI) / (theta * currwin.shrinkfactor) + 0.5;
490 #else
491 n = 0.3 * TWOPI / theta + 0.5;
492 #endif
493 if (n < 12)
494 n = 12;
495 else if (n > 80)
496 n = 80;
497 n /= 2;
498 theta = TWOPI / n;
499 flush_path();
500 im_fdraw(xcenter + xradius * cos(start_angle),
501 ycenter + yradius * sin(start_angle));
502 angle = start_angle + theta;
503 if (end_angle < start_angle)
504 end_angle += TWOPI;
505 while (angle < end_angle) {
506 im_fdraw(xcenter + xradius * cos(angle),
507 ycenter + yradius * sin(angle));
508 angle += theta;
509 }
510 im_fdraw(xcenter + xradius * cos(end_angle),
511 ycenter + yradius * sin(end_angle));
512 flush_path();
513 }
514
515
516 /*
517 * APPROXIMATE integer distance between two points
518 */
519
520 #define dist(x0, y0, x1, y1) (abs(x0 - x1) + abs(y0 - y1))
521
522
523 /*
524 * Draw a spline along the previously defined path
525 */
526
527 static void
528 flush_spline(void)
529 {
530 int xp, yp;
531 int N;
532 int lastx = -1, lasty = -1;
533 Boolean lastvalid = False;
534 int t1, t2, t3;
535 int steps;
536 int j;
537 int i, w;
538
539 N = path_len + 1;
540 xx[0] = xx[1];
541 yy[0] = yy[1];
542 xx[N] = xx[N - 1];
543 yy[N] = yy[N - 1];
544 for (i = 0; i < N - 1; i++) { /* interval */
545 steps = (dist(xx[i], yy[i], xx[i + 1], yy[i + 1]) +
546 dist(xx[i + 1], yy[i + 1], xx[i + 2], yy[i + 2])) / 80;
547 for (j = 0; j < steps; j++) { /* points within */
548 w = (j * 1000 + 500) / steps;
549 t1 = w * w / 20;
550 w -= 500;
551 t2 = (750000 - w * w) / 10;
552 w -= 500;
553 t3 = w * w / 20;
554 xp =
555 (t1 * xx[i + 2] + t2 * xx[i + 1] + t3 * xx[i] + 50000) / 100000;
556 yp =
557 (t1 * yy[i + 2] + t2 * yy[i + 1] + t3 * yy[i] + 50000) / 100000;
558 if (lastvalid)
559 line_btw(lastx, lasty, xp, yp);
560 lastx = xp;
561 lasty = yp;
562 lastvalid = True;
563 }
564 }
565 path_len = 0;
566 }
567
568
569 /*
570 * Shade the last box, circle, or ellipse
571 */
572
573 static void
574 shade_last(void)
575 {
576 blacken = whiten = False;
577 shade = True;
578 }
579
580
581 /*
582 * Make the last box, circle, or ellipse, white inside (shade with white)
583 */
584
585 static void
586 whiten_last(void)
587 {
588 whiten = True;
589 blacken = shade = False;
590 }
591
592
593 /*
594 * Make last box, etc, black inside
595 */
596
597 static void
598 blacken_last(void)
599 {
600 blacken = True;
601 whiten = shade = False;
602 }
603
604
605 /*
606 * Code for PostScript<tm> specials begins here.
607 */
608
609 #if PS
610
611 /*
612 * Information on how to search for PS header and figure files.
613 */
614
615 static void ps_startup(int, int, const char *);
616 static void ps_startup2(void);
617
618 /* dummy procedures */
619 static void NullProc(void) { }
620 static void NullProcInt(int flag) { UNUSED(flag); }
621 static void NullProcStr(const char *cp) { UNUSED(cp); }
622
623
624 struct psprocs psp = { /* used for lazy startup of the ps machinery */
625 /* toggle */ NullProcInt,
626 /* destroy */ NullProc,
627 /* interrupt */ NullProc,
628 /* endpage */ NullProc,
629 /* drawbegin */ ps_startup,
630 /* drawraw */ NullProcStr,
631 /* drawfile */ NULL,
632 /* drawend */ NullProcStr,
633 /* beginheader */ ps_startup2,
634 /* endheader */ NullProc,
635 /* newdoc */ NullProc
636 };
637
638 struct psprocs no_ps_procs = { /* used if postscript is unavailable */
639 /* toggle */ NullProcInt,
640 /* destroy */ NullProc,
641 /* interrupt */ NullProc,
642 /* endpage */ NullProc,
643 /* drawbegin */ drawbegin_none,
644 /* drawraw */ NullProcStr,
645 /* drawfile */ NULL,
646 /* drawend */ NullProcStr,
647 /* beginheader */ NullProc,
648 /* endheader */ NullProc,
649 /* newdoc */ NullProc
650 };
651
652 #endif /* PS */
653
654 #ifdef MAGICK
655 int bbox_angle;
656 Boolean bbox_valid;
657 unsigned int bbox_width;
658 unsigned int bbox_height;
659 int bbox_voffset;
660 #else
661 static int bbox_angle;
662 static Boolean bbox_valid;
663 static unsigned int bbox_width;
664 static unsigned int bbox_height;
665 static int bbox_voffset;
666 #endif
667
668 /* info on bboxes on this page */
669 struct bbox_info {
670 int x;
671 int y;
672 int w;
673 int h;
674 int angle;
675 };
676
677 static struct bbox_info *g_bbox_info = NULL;
678 static size_t g_bbox_info_size = 0;
679 static size_t g_bbox_info_max_size = 0;
680
681 /*
682 Append the current coordinates to g_bbox_info, unless it already
683 contains these coordinates.
684 */
685
686 static void
687 append_bbox_info(int x, int y, int w, int h, int angle)
688 {
689 Boolean found = False;
690 size_t i;
691 const size_t SIZE_STEP = 16;
692
693 /* is this box already present? */
694 for (i = 0; i < g_bbox_info_size; i++) {
695 if (g_bbox_info[i].x == x
696 && g_bbox_info[i].y == y
697 && g_bbox_info[i].w == w
698 && g_bbox_info[i].h == h
699 && g_bbox_info[i].angle == angle) {
700 found = True;
701 break;
702 }
703 }
704
705 if (!found) {
706 g_bbox_info_size++;
707
708 while (g_bbox_info_size >= g_bbox_info_max_size) {
709 g_bbox_info_max_size += SIZE_STEP;
710 }
711
712 g_bbox_info = xrealloc(g_bbox_info, g_bbox_info_max_size * sizeof *g_bbox_info);
713 g_bbox_info[g_bbox_info_size - 1].x = x;
714 g_bbox_info[g_bbox_info_size - 1].y = y;
715 g_bbox_info[g_bbox_info_size - 1].w = w;
716 g_bbox_info[g_bbox_info_size - 1].h = h;
717 g_bbox_info[g_bbox_info_size - 1].angle = angle;
718 }
719 }
720
721 static void
722 draw_bbox0(int xcorner, int ycorner)
723 {
724 if (bbox_valid) {
725 #if COLOR
726 if (fg_active != fg_current)
727 do_color_change();
728 #endif
729
730 if (bbox_angle == 0) {
731 ycorner -= bbox_voffset;
732 XDrawRectangle(DISP, currwin.win, globals.gc.high, xcorner, ycorner, bbox_width, bbox_height);
733 if (resource.postscript == 0) {
734 if (htex_inside_href) {
735 htex_set_anchorsize(xcorner, ycorner, xcorner + bbox_width, ycorner + bbox_height);
736 htex_set_objecttype(HTEX_IMG);
737 }
738 }
739 }
740 else {
741 float sin_a = sin(bbox_angle * (TWOPI / 360));
742 float cos_a = cos(bbox_angle * (TWOPI / 360));
743 float a, b, c, d;
744
745 a = cos_a * bbox_width;
746 b = -sin_a * bbox_width;
747 c = -sin_a * bbox_height;
748 d = -cos_a * bbox_height;
749
750 XDrawLine(DISP, currwin.win, globals.gc.high,
751 xcorner, ycorner,
752 xcorner + (int)rint(a), ycorner + (int)rint(b));
753 XDrawLine(DISP, currwin.win, globals.gc.high,
754 xcorner + (int)rint(a), ycorner + (int)rint(b),
755 xcorner + (int)rint(a + c), ycorner + (int)rint(b + d));
756 XDrawLine(DISP, currwin.win, globals.gc.high,
757 xcorner + (int)rint(a + c), ycorner + (int)rint(b + d),
758 xcorner + (int)rint(c), ycorner + (int)rint(d));
759 XDrawLine(DISP, currwin.win, globals.gc.high,
760 xcorner + (int)rint(c), ycorner + (int)rint(d),
761 xcorner, ycorner);
762 }
763 bbox_valid = False;
764 }
765 }
766
767 /*
768 Display the items in bbox_info.
769 Invoked from draw_page() in dvi-draw.c.
770 */
771 void
772 display_bboxes(void)
773 {
774 size_t i;
775
776 for (i = 0; i < g_bbox_info_size; i++) {
777 if (globals.debug & DBG_PS) {
778 fprintf(stderr, "drawing bbox %lu at %d %d, %d x %d, angle %d\n",
779 (unsigned long)i,
780 g_bbox_info[i].x,
781 g_bbox_info[i].y,
782 g_bbox_info[i].w,
783 g_bbox_info[i].h,
784 g_bbox_info[i].angle);
785 }
786 #if 0
787 XDrawRectangle(DISP, currwin.win, globals.gc.high,
788 g_bbox_info[i].x,
789 g_bbox_info[i].y,
790 g_bbox_info[i].w,
791 g_bbox_info[i].h);
792 #else
793 bbox_valid = True;
794 bbox_width = g_bbox_info[i].w;
795 bbox_height = bbox_voffset = g_bbox_info[i].h;
796 bbox_angle = g_bbox_info[i].angle;
797 draw_bbox0(g_bbox_info[i].x, g_bbox_info[i].y + bbox_height);
798 #endif
799 }
800 bbox_angle = 0;
801 bbox_valid = False;
802 }
803
804 void
805 clear_bboxes(void)
806 {
807 free(g_bbox_info);
808 g_bbox_info = NULL;
809 g_bbox_info_size = 0;
810 g_bbox_info_max_size = 0;
811 }
812
813 void
814 save_bbox(void)
815 {
816 int xcorner, ycorner;
817
818 if (bbox_valid) {
819 xcorner = PXL_H - currwin.base_x;
820 ycorner = PXL_V - currwin.base_y;
821
822 ycorner -= bbox_voffset;
823 append_bbox_info(xcorner, ycorner, bbox_width, bbox_height, bbox_angle);
824
825 /* register boundaries of this box as anchor boundaries */
826 if (htex_inside_href) {
827 htex_set_anchorsize(xcorner, ycorner, xcorner + bbox_width, ycorner + bbox_height);
828 htex_set_objecttype(HTEX_IMG);
829 }
830 }
831 }
832
833
834 void
835 draw_bbox(void)
836 {
837 draw_bbox0(PXL_H - currwin.base_x, PXL_V - currwin.base_y);
838 }
839
840 #if PS
841
842 static XIOErrorHandler oldhandler;
843
844 static int XDviIOErrorHandler(Display * disp)
845 {
846 ps_destroy();
847 return oldhandler(disp);
848 }
849
850 static void
851 actual_startup(int flag)
852 {
853 UNUSED(flag);
854 oldhandler = XSetIOErrorHandler(XDviIOErrorHandler);
855
856 /*
857 * Figure out what we want to use to display postscript figures
858 * and set at most one of the following to True:
859 * resource.useGS, resource.useDPS, resource.useNeWS
860 *
861 * Choose DPS then NEWS then Ghostscript if they are available
862 */
863 if (!(
864 #ifdef PS_DPS
865 (resource.useDPS && initDPS())
866 #if defined(PS_NEWS) || defined(PS_GS)
867 ||
868 #endif
869 #endif /* PS_DPS */
870 #ifdef PS_NEWS
871 (resource.useNeWS && initNeWS())
872 #ifdef PS_GS
873 ||
874 #endif
875 #endif /* PS_NEWS */
876 #ifdef PS_GS
877 (resource.useGS && initGS())
878 #endif
879 ))
880 psp = no_ps_procs;
881 }
882
883 static void
884 ps_startup(int xul, int yul, const char *cp)
885 {
886 if (resource.postscript == 0) {
887 draw_bbox();
888 return;
889 }
890 actual_startup(0);
891 psp.drawbegin(xul, yul, cp);
892 }
893
894 static void
895 ps_startup2(void)
896 {
897 actual_startup(0);
898 psp.beginheader();
899 }
900
901
902 /*
903 * dumb parsing of PostScript - search for rotation H. Zeller 1/97
904 * Returns true if we find a potentially non-supported command that
905 * we want to warn users about.
906 */
907 static Boolean
908 ps_parseraw(const char *PostScript_cmd)
909 {
910 const char *p;
911
912 bbox_angle = 0;
913 p = strstr(PostScript_cmd, "rotate");
914 if (p != NULL) {
915 while (*p != '\0' && !isdigit((int)*p))
916 --p;
917 while (*p != '\0' && isdigit((int)*p))
918 --p;
919 if (*p != '+' && *p != '-')
920 ++p;
921 sscanf(p, "%d neg rotate", &bbox_angle);
922 return True;
923 }
924 if (strstr(PostScript_cmd, " scale ") != NULL)
925 return True;
926 return False;
927 }
928
929
930 void
931 drawbegin_none(int xul, int yul, const char *cp)
932 {
933 UNUSED(xul);
934 UNUSED(yul);
935 UNUSED(cp);
936
937 draw_bbox();
938 }
939
940
941 /*
942 * Mechanism to keep track of included PS files.
943 */
944
945 struct avl_psinfo {
946 AVL_COMMON;
947 char *fullpath;
948 char *tempname;
949 dev_t ps_dev;
950 ino_t ps_ino;
951 time_t ps_mtime;
952 Boolean is_new; /* set if previous use failed because of !allow_shell */
953 };
954
955 static struct avl_psinfo *psinfo_head = NULL;
956
957
958 /*
959 * ps_clear_cache() resets the cache to the empty state.
960 */
961
962 static void
963 clear_psinfo_node(struct avl_psinfo *psinfp)
964 {
965 if (psinfp->fullpath != NULL) {
966 free(psinfp->fullpath);
967 psinfp->fullpath = NULL;
968 }
969 if (psinfp->tempname != NULL) {
970 if (unlink(psinfp->tempname) < 0)
971 perror(psinfp->tempname);
972 free(psinfp->tempname);
973 psinfp->tempname = NULL;
974 }
975 }
976
977 static void
978 clear_psinfo_tree(struct avl_psinfo *psinfp)
979 {
980 if (psinfp == NULL)
981 return;
982
983 clear_psinfo_tree((struct avl_psinfo *) psinfp->left);
984 clear_psinfo_tree((struct avl_psinfo *) psinfp->right);
985 free((char *) psinfp->key);
986 clear_psinfo_node(psinfp);
987 free(psinfp);
988 }
989
990 static void
991 ps_clear_cache(void)
992 {
993 clear_psinfo_tree(psinfo_head);
994 psinfo_head = NULL;
995 }
996
997
998 /*
999 * ps_clear_cache_nofree() does the same thing as ps_clear_cache(), but
1000 * without ever calling free(). This is because it may be called
1001 * following a segmentation fault, in which case the heap may be corrupted.
1002 */
1003
1004 static void
1005 clear_psinfo_node_nofree(struct avl_psinfo *psinfp)
1006 {
1007 if (psinfp->fullpath != NULL) {
1008 psinfp->fullpath = NULL;
1009 }
1010 if (psinfp->tempname != NULL) {
1011 if (unlink(psinfp->tempname) < 0)
1012 perror(psinfp->tempname);
1013 psinfp->tempname = NULL;
1014 }
1015 }
1016
1017 static void
1018 clear_psinfo_tree_nofree(struct avl_psinfo *psinfp)
1019 {
1020 if (psinfp == NULL)
1021 return;
1022
1023 clear_psinfo_tree_nofree((struct avl_psinfo *) psinfp->left);
1024 clear_psinfo_tree_nofree((struct avl_psinfo *) psinfp->right);
1025 clear_psinfo_node_nofree(psinfp);
1026 }
1027
1028 static void
1029 ps_clear_cache_nofree(void)
1030 {
1031 clear_psinfo_tree_nofree(psinfo_head);
1032 psinfo_head = NULL;
1033 }
1034
1035
1036 /*
1037 * ps_new_tempfile() - Opens a new temporary file.
1038 * Returns the fd, or -1 if an error occurred.
1039 */
1040
1041 static int
1042 ps_new_tempfile(struct avl_psinfo *psinfp)
1043 {
1044 int fd;
1045
1046 fd = xdvi_temp_fd(&psinfp->tempname);
1047 if (fd == -1) {
1048 XDVI_ERROR((stderr,
1049 "Cannot create temporary file for PostScript figure file: %s",
1050 strerror(errno)));
1051 free(psinfp->tempname);
1052 psinfp->tempname = NULL;
1053 }
1054 return fd;
1055 }
1056
1057 #ifndef UNCOMPRESS
1058 #define UNCOMPRESS "uncompress"
1059 #endif
1060
1061 #ifndef GUNZIP
1062 #define GUNZIP "gunzip"
1063 #endif
1064
1065 #ifndef BUNZIP2
1066 #define BUNZIP2 "bunzip2"
1067 #endif
1068
1069 static void send_ps_file(const char *filename, kpse_file_format_type pathinfo);
1070
1071 static void
1072 enable_specials_send_ps_file(XtPointer data)
1073 {
1074 const char *filename = (const char *)data;
1075 resource.allow_shell = True;
1076 redraw_page(); /* to erase the bounding box */
1077 statusline_info(STATUS_MEDIUM,
1078 "Shell specials enabled for this session.",
1079 filename);
1080 send_ps_file(filename, kpse_pict_format);
1081 }
1082
1083 static void
1084 try_open_tempname(int status, struct xchild *this)
1085 {
1086 if (WIFEXITED(status)) { /* child exited normally */
1087 if (WEXITSTATUS(status) != 0) {
1088 /* default error procedure */
1089 handle_child_exit(status, this);
1090 this = NULL; /* prevent freeing it twice */
1091 }
1092 else {
1093 struct avl_psinfo *psinfp = (struct avl_psinfo *) this->data;
1094 FILE *f = XFOPEN(psinfp->tempname, OPEN_MODE);
1095
1096 fprintf(stderr, "FILE: %s\n", psinfp->tempname);
1097 if (f == NULL) {
1098 perror(psinfp->tempname);
1099 }
1100 else {
1101 fprintf(stderr, "sending file: %s, %p\n", psinfp->tempname, (void *)f);
1102 /* There's no point in invoking
1103 psp.drawfile(psinfp->tempname, f);
1104 here, since usually it will be already too late (draw_part() which
1105 had called us via applicationDoSpecial() will already have terminated).
1106 So instead, we just close the file and force a redraw of the entire page.
1107 */
1108 fclose(f);
1109 globals.ev.flags |= EV_NEWPAGE;
1110 }
1111 }
1112 }
1113 else if (WIFSIGNALED(status)) {
1114 popup_message(globals.widgets.top_level,
1115 MSG_WARN,
1116 NULL,
1117 "Process `%s' terminated abnormally with signal %d.", this->name, WTERMSIG(status));
1118 }
1119 else if (WIFSTOPPED(status)) {
1120 popup_message(globals.widgets.top_level,
1121 MSG_WARN,
1122 NULL,
1123 "Process `%s' stopped by signal %d.", this->name, WSTOPSIG(status));
1124 }
1125 else {
1126 popup_message(globals.widgets.top_level,
1127 MSG_WARN,
1128 NULL,
1129 "Process `%s' terminated with unknown status.", this->name);
1130 }
1131 if (this != NULL) { /* if it wasn't freed already */
1132 free(this->name);
1133 free(this->io);
1134 free(this);
1135 }
1136 }
1137
1138 static void
1139 send_ps_file(const char *filename, kpse_file_format_type pathinfo)
1140 {
1141 FILE *f;
1142 int fd;
1143 static const char *argv[] = { NULL, "-c", NULL, NULL };
1144 char *bufp = NULL;
1145 size_t len;
1146 struct avl_psinfo *volatile psinfp;
1147 char magic1, magic2, magic3;
1148 static Boolean warned_about_shellescape = False;
1149 static size_t ffline_len = 0;
1150 static char *ffline = NULL;
1151 const size_t FFLINE_STEP = 128;
1152
1153 if (psp.drawfile == NULL || resource.postscript == 0) {
1154 return;
1155 }
1156
1157 len = strlen(filename);
1158 psinfp = (struct avl_psinfo *) avladd(filename, len,
1159 (struct avl **) &psinfo_head, sizeof(struct avl_psinfo));
1160
1161 if (psinfp->key == filename) { /* if new record */
1162 psinfp->is_new = True;
1163 psinfp->key = xmemdup(filename, len + 1);
1164 psinfp->fullpath = psinfp->tempname = NULL;
1165 }
1166
1167 if (filename[0] == '`') {
1168 /* To test this code, use \special{psfile="`cat file.ps"} */
1169 if (!resource.allow_shell) {
1170 if (!warned_about_shellescape) {
1171 choice_dialog_sized(globals.widgets.top_level,
1172 MSG_INFO,
1173 SIZE_MEDIUM,
1174 /* helptext */
1175 "To enable shell specials, use the \"-allowshell\" command "
1176 "line option.\n\n"
1177 "WARNING: You should NOT use shell specials like\n"
1178 "`gunzip -c file.eps.gz\n"
1179 "(e.g. via \\DeclareGraphicsRule{...}) "
1180 "to uncompress .eps.gz files, even though some obsolete "
1181 "LaTeX documentation might suggest it. Current versions "
1182 "of xdvi and dvips will handle .eps.gz files just fine without this trick.\n",
1183 #ifndef MOTIF
1184 NULL,
1185 #endif
1186 NULL, NULL, /* no pre_callbacks */
1187 /* Cancel label/callback */
1188 "Cancel", NULL, (XtPointer)NULL,
1189 /* Enable label/callback */
1190 "Enable", enable_specials_send_ps_file, (XtPointer)filename,
1191 /* msg */
1192 "This page contains a shell special \"%s\", but execution of shell specials "
1193 "is disabled.\n\nYou can now click "
1194 "\"Enable\" to enable shell specials for this session only, "
1195 "\"Cancel\" to leave the specials disabled, or \"Help\" for more help "
1196 "on this topic.\n\n"
1197 "Please note that shell specials are a security risk, since they "
1198 "allow execution of arbitrary shell commands from the TeX file. "
1199 "You should enable them only for DVI files that you created yourself.",
1200 filename);
1201 warned_about_shellescape = True;
1202 }
1203 else {
1204 statusline_error(STATUS_MEDIUM,
1205 "Info: Shell special \"%s\" disabled.",
1206 filename);
1207 }
1208 draw_bbox();
1209 return;
1210 }
1211
1212 if (!psinfp->is_new) { /* if we already have it */
1213 if (psinfp->tempname == NULL) { /* if previously known error */
1214 draw_bbox();
1215 return;
1216 }
1217 f = XFOPEN(psinfp->tempname, OPEN_MODE);
1218 if (f == NULL) {
1219 perror(psinfp->tempname);
1220 /* WARN1(XmDIALOG_ERROR,
1221 "Cannot open temporary PostScript file:\n%s",
1222 psinfp->tempname); */
1223 clear_psinfo_node(psinfp);
1224 draw_bbox();
1225 return;
1226 }
1227 psp.drawfile(psinfp->tempname, f);
1228 }
1229 else { /* it is new */
1230 char *argv[4];
1231
1232 psinfp->is_new = False;
1233
1234 /* Create the file (it wasn't cached).
1235 Fork so that we can collect the process's error messages. */
1236
1237 fd = ps_new_tempfile(psinfp);
1238 if (fd == -1) { /* if error */
1239 draw_bbox();
1240 return;
1241 }
1242
1243 /* FIXME: Insecure - this is a /tmp race;
1244 shouldn't close(fd), just re-use it */
1245 close(fd);
1246
1247 len = strlen(filename) + strlen(psinfp->tempname) + (4 - 1);
1248 if (len > ffline_len) {
1249 ffline_len += FFLINE_STEP;
1250 ffline = xrealloc(ffline, ffline_len);
1251 }
1252
1253 sprintf(ffline, "%s > %s", filename + 1, psinfp->tempname);
1254 argv[0] = "/bin/sh";
1255 argv[1] = "-c";
1256 argv[2] = ffline;
1257 argv[3] = NULL;
1258 fork_process("/bin/sh", False, globals.dvi_file.dirname,
1259 try_open_tempname, psinfp, -SIGKILL, argv);
1260 }
1261 }
1262 else { /* not shell escape */
1263 char *expanded_filename = NULL;
1264 struct stat statbuf;
1265
1266 if (psinfp->fullpath != NULL) { /* if existing rec., no error */
1267 /* Check file modification time */
1268 if (stat(psinfp->fullpath, &statbuf) != 0) {
1269 perror(psinfp->fullpath);
1270 if (psinfp->ps_mtime != 0) {
1271 clear_psinfo_node(psinfp);
1272 psinfp->is_new = True;
1273 }
1274 }
1275 else {
1276 if (psinfp->ps_dev != statbuf.st_dev
1277 || psinfp->ps_ino != statbuf.st_ino
1278 || psinfp->ps_mtime != statbuf.st_mtime) {
1279 clear_psinfo_node(psinfp);
1280 psinfp->is_new = True;
1281 }
1282 }
1283 }
1284
1285 if (!psinfp->is_new) {
1286 bufp = psinfp->tempname;
1287 if (bufp == NULL) {
1288 bufp = psinfp->fullpath;
1289 if (bufp == NULL) { /* if error already noted */
1290 draw_bbox();
1291 return;
1292 }
1293 }
1294 f = XFOPEN(bufp, OPEN_MODE);
1295 if (f == NULL) {
1296 XDVI_WARNING((stderr, "Could not find graphics or temporary file \"%s\"",
1297 bufp));
1298 statusline_info(STATUS_MEDIUM, "Warning: Could not find graphics or temporary file \"%s\"",
1299 bufp);
1300 clear_psinfo_node(psinfp);
1301 draw_bbox();
1302 return;
1303 }
1304 }
1305 else { /* node is new */
1306 psinfp->is_new = False;
1307
1308 expanded_filename = find_file(filename, &statbuf, pathinfo);
1309
1310 if (expanded_filename == NULL &&
1311 (pathinfo == kpse_enc_format || pathinfo == kpse_type1_format))
1312 {
1313 /* in this case, we also kpathsea-search in the `old' place for
1314 backwards compatibility: kpse_tex_ps_header_format
1315 (see comment for load_vector(), dvi-draw.c for details) */
1316 expanded_filename = kpse_find_file(filename,
1317 kpse_tex_ps_header_format, True);
1318 }
1319 if (expanded_filename == NULL) {
1320 expanded_filename = kpse_find_file(filename,
1321 kpse_program_text_format, True);
1322 }
1323 if (expanded_filename == NULL) { /* still no success, complain */
1324 /* FIXME: this warning may be overwritten by warning about raw
1325 PS specials, so additinally dump to stderr.
1326 TODO: make statusline printing respect more important
1327 messages. */
1328 XDVI_WARNING((stderr, "Could not find graphics file \"%s\"",
1329 filename));
1330 statusline_info(STATUS_MEDIUM,
1331 "Warning: Could not find graphics file \"%s\"",
1332 filename);
1333 draw_bbox();
1334 return;
1335 }
1336 if (globals.debug & DBG_OPEN)
1337 printf("%s:%d: |%s| expanded to |%s|\n", __FILE__, __LINE__,
1338 filename, expanded_filename);
1339
1340 f = XFOPEN(expanded_filename, OPEN_MODE);
1341 if (f == NULL) {
1342 XDVI_WARNING((stderr, "Could not open graphics file \"%s\": %s",
1343 expanded_filename, strerror(errno)));
1344 statusline_info(STATUS_MEDIUM,
1345 "Warning: Could not open graphics file \"%s\": %s",
1346 expanded_filename, strerror(errno));
1347 free(expanded_filename);
1348 draw_bbox();
1349 return;
1350 }
1351
1352 bufp = psinfp->fullpath = expanded_filename;
1353
1354 if (fstat(fileno(f), &statbuf) == 0) { /* get mod. time */
1355 psinfp->ps_dev = statbuf.st_dev;
1356 psinfp->ps_ino = statbuf.st_ino;
1357 psinfp->ps_mtime = statbuf.st_mtime;
1358 }
1359 else {
1360 perror(bufp);
1361 psinfp->ps_dev = 0;
1362 psinfp->ps_ino = 0;
1363 psinfp->ps_mtime = 0;
1364 }
1365
1366 /* check for compressed files */
1367
1368 len = strlen(filename);
1369 magic1 = '\037';
1370 magic3 = '\0';
1371 if ((len > 2 && strcmp(filename + len - 2, ".Z") == 0
1372 && (argv[0] = UNCOMPRESS, magic2 = '\235', True))
1373 || (len > 3 && strcmp(filename + len - 3, ".gz") == 0
1374 && (argv[0] = GUNZIP, magic2 = '\213', True))
1375 || (len > 4 && strcmp(filename + len - 4, ".bz2") == 0
1376 && (argv[0] = BUNZIP2, magic1 = 'B', magic2 = 'Z',
1377 magic3 = 'h', True))) {
1378 if (getc(f) != magic1 || (char)getc(f) != magic2
1379 || (magic3 != '\0' && getc(f) != magic3)) {
1380 rewind(f);
1381 }
1382 else {
1383 pid_t pid;
1384 int status;
1385
1386 fclose(f);
1387
1388 fd = ps_new_tempfile(psinfp);
1389 if (fd == -1) { /* if error */
1390 clear_psinfo_node(psinfp);
1391 return;
1392 }
1393 argv[2] = bufp;
1394 fflush(stderr); /* avoid double flushing */
1395 pid = vfork();
1396 if (pid == 0) { /* if child */
1397 (void)dup2(fd, 1);
1398 (void)execvp(argv[0], (char **)argv);
1399 XDVI_ERROR((stderr, "Execvp of %s failed: %s", argv[0],
1400 strerror(errno)));
1401 _exit(EXIT_FAILURE);
1402 }
1403 (void)close(fd);
1404
1405 for (;;) {
1406 #if HAVE_WAITPID
1407 if (waitpid(pid, &status, 0) != -1)
1408 break;
1409 #else
1410 # if HAVE_WAIT4
1411 if (wait4(pid, &status, 0, (struct rusage *)NULL)
1412 != -1)
1413 break;
1414 # else
1415 int retval;
1416
1417 retval = wait(&status);
1418 if (retval == pid)
1419 break;
1420 if (retval != -1)
1421 continue;
1422 # endif /* HAVE_WAIT4 */
1423 #endif /* HAVE_WAITPID */
1424 if (errno == EINTR)
1425 continue;
1426 perror("[xdvik] waitpid");
1427 return;
1428 }
1429 f = XFOPEN(psinfp->tempname, OPEN_MODE);
1430 if (f == NULL) {
1431 perror(psinfp->tempname);
1432 draw_bbox();
1433 return;
1434 }
1435 bufp = psinfp->tempname;
1436 }
1437 }
1438 }
1439 /* Success! */
1440 psp.drawfile(bufp, f); /* this is supposed to close the file */
1441 }
1442 }
1443
1444
1445 void
1446 ps_destroy(void)
1447 {
1448 /* Note: old NeXT systems (at least) lack atexit/on_exit. */
1449 psp.destroy();
1450 ps_clear_cache();
1451 }
1452
1453
1454 /*
1455 * Same as above, but refrains from calling free() (may be called
1456 * following a seg fault).
1457 */
1458
1459 void
1460 ps_destroy_nofree(void)
1461 {
1462 psp.destroy();
1463 ps_clear_cache_nofree();
1464 }
1465
1466
1467 #endif /* PS */
1468
1469
1470 void
1471 init_prescan(void)
1472 {
1473 scanned_page = scanned_page_reset = resource.prescan ? -1 : total_pages + 1;
1474 #if PS
1475 scanned_page_ps = scanned_page_ps_bak = scanned_page;
1476 #if COLOR
1477 scanned_page_color = scanned_page;
1478 #endif /* COLOR */
1479 #endif /* PS */
1480
1481 TRACE_FILES((stderr, "init_prescan: scanned_page = %d", scanned_page));
1482 #if PS
1483 if (resource.postscript == 0)
1484 scanned_page_ps = total_pages + 1;
1485
1486 ps_clear_cache();
1487 psp.newdoc();
1488 #endif
1489
1490 #if COLOR
1491 if (!resource.use_color)
1492 scanned_page_color = total_pages + 1;
1493 #endif
1494
1495 if (ignore_papersize_specials) {
1496 #if PS && COLOR
1497 scanned_page = scanned_page_ps < scanned_page_color ? scanned_page_ps : scanned_page_color;
1498 #elif PS
1499 scanned_page = scanned_page_ps;
1500 #elif COLOR
1501 scanned_page = scanned_page_color;
1502 #else
1503 scanned_page = total_pages + 1;
1504 #endif
1505 }
1506 }
1507
1508
1509 static void
1510 psfig_special(char *cp)
1511 {
1512 char *filename;
1513 int raww, rawh;
1514
1515 if (strncmp(cp, ":[begin]", 8) == 0) {
1516 cp += 8;
1517 bbox_valid = False;
1518 bbox_angle = 0;
1519 if (sscanf(cp, "%d %d\n", &raww, &rawh) >= 2) {
1520 bbox_valid = True;
1521 bbox_width = pixel_conv(spell_conv(raww));
1522 bbox_height = pixel_conv(spell_conv(rawh));
1523 bbox_voffset = 0;
1524 }
1525 if (INSIDE_MANE_WIN) {
1526 #if PS
1527 psp.drawbegin(PXL_H - currwin.base_x, PXL_V - currwin.base_y, cp);
1528 #else
1529 draw_bbox();
1530 #endif
1531 }
1532 psfig_begun = True;
1533 }
1534 else if (strncmp(cp, " plotfile ", 10) == 0) {
1535 cp += 10;
1536 while (isspace((int)*cp))
1537 cp++;
1538 /* handle "`zcat file". Borrowed from dvipsk... */
1539 if (*cp == '"') {
1540 cp++;
1541 for (filename = cp; *cp && (*cp != '"'); ++cp);
1542 }
1543 else {
1544 for (filename = cp; *cp && !isspace((int)*cp); ++cp);
1545 }
1546 *cp = '\0';
1547 #if PS
1548 if (INSIDE_MANE_WIN)
1549 send_ps_file(filename, kpse_pict_format);
1550 #endif
1551 }
1552 else if (strncmp(cp, ":[end]", 6) == 0) {
1553 cp += 6;
1554 #if PS
1555 if (INSIDE_MANE_WIN) {
1556 psp.drawend(cp);
1557 }
1558 #endif
1559 bbox_valid = False;
1560 psfig_begun = False;
1561 }
1562 else if (*cp == ':') {
1563 /* I am going to send some raw postscript stuff */
1564 ++cp; /* skip the colon */
1565 #if PS
1566 if (ps_parseraw(cp))
1567 have_raw_postscript = True;
1568 if (INSIDE_MANE_WIN)
1569 psp.drawraw(cp);
1570 #endif
1571 }
1572 else { /* attempt to parse pstricks color specials */
1573 #if COLOR
1574 struct rgb color;
1575 if (parse_color(cp, cp, &color, True)) {
1576 /* clear stack */
1577 if (rcs_head == NULL) {
1578 rcs_head = xmalloc(sizeof *rcs_head);
1579 rcs_head->prev = rcs_head->next = NULL;
1580 }
1581 rcs_top = rcs_head;
1582 color_bot_size = 0;
1583
1584 /* Change top of stack */
1585 rcs_top->color = color;
1586
1587 set_fg_color(&color);
1588 }
1589 #endif
1590 /* also raw PostScript, but no extra colon to skip */
1591 #if PS
1592 if (INSIDE_MANE_WIN) {
1593 if (ps_parseraw(cp))
1594 have_raw_postscript = True;
1595
1596 if (psfig_begun)
1597 psp.drawraw(cp);
1598 else {
1599 psp.drawbegin(PXL_H - currwin.base_x,
1600 PXL_V - currwin.base_y, cp);
1601 psp.drawend("");
1602 }
1603 }
1604 #endif
1605 }
1606 }
1607
1608
1609 /* Keys for epsf specials */
1610
1611 static const char *keytab[] = { "clip",
1612 "llx",
1613 "lly",
1614 "urx",
1615 "ury",
1616 "rwi",
1617 "rhi",
1618 "hsize",
1619 "vsize",
1620 "hoffset",
1621 "voffset",
1622 "hscale",
1623 "vscale",
1624 "angle"
1625 };
1626
1627 #define KEY_LLX keyval[0]
1628 #define KEY_LLY keyval[1]
1629 #define KEY_URX keyval[2]
1630 #define KEY_URY keyval[3]
1631 #define KEY_RWI keyval[4]
1632 #define KEY_RHI keyval[5]
1633
1634 #define NKEYS (sizeof keytab /sizeof *keytab)
1635 #define N_ARGLESS_KEYS 1
1636
1637 static void
1638 epsf_special(char *cp)
1639 {
1640 char *filename;
1641 static char *buffer;
1642 static unsigned int buflen = 0;
1643 unsigned int len;
1644 char *q;
1645 int flags = 0;
1646 double keyval[6] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 };
1647
1648 filename = cp;
1649 if (*cp == '\'' || *cp == '"') {
1650 do
1651 ++cp;
1652 while (*cp != '\0' && *cp != *filename);
1653 ++filename;
1654 }
1655 else
1656 while (*cp != '\0' && !isspace((int)*cp))
1657 ++cp;
1658 if (*cp != '\0')
1659 *cp++ = '\0';
1660 while (isspace((int)*cp))
1661 ++cp;
1662 len = strlen(cp) + NKEYS + 30;
1663 if (buflen < len) {
1664 if (buflen != 0)
1665 free(buffer);
1666 buflen = len;
1667 buffer = xmalloc(buflen);
1668 }
1669 strcpy(buffer, "@beginspecial");
1670 q = buffer + strlen(buffer);
1671 while (*cp != '\0') {
1672 char *p1 = cp;
1673 size_t keyno;
1674
1675 while (*p1 != '=' && !isspace((int)*p1) && *p1 != '\0')
1676 ++p1;
1677 for (keyno = 0;; ++keyno) {
1678 if (keyno >= NKEYS) {
1679 if (globals.warn_spec_now)
1680 XDVI_WARNING((stderr, "Ignoring unknown keyword (%.*s) in \\special",
1681 (int)(p1 - cp), cp));
1682 break;
1683 }
1684 if (memcmp(cp, keytab[keyno], p1 - cp) == 0) {
1685 if (keyno >= N_ARGLESS_KEYS) {
1686 while (isspace((int)*p1))
1687 ++p1;
1688 if (*p1 == '=') {
1689 ++p1;
1690 while (isspace((int)*p1))
1691 ++p1;
1692 }
1693 if (keyno < N_ARGLESS_KEYS + 6) {
1694 keyval[keyno - N_ARGLESS_KEYS] = atof(p1);
1695 flags |= (1 << (keyno - N_ARGLESS_KEYS));
1696 }
1697 *q++ = ' ';
1698 while (!isspace((int)*p1) && *p1 != '\0')
1699 *q++ = *p1++;
1700 }
1701 *q++ = ' ';
1702 *q++ = '@';
1703 strcpy(q, keytab[keyno]);
1704 q += strlen(q);
1705 break;
1706 }
1707 }
1708 cp = p1;
1709 while (!isspace((int)*cp) && *cp != '\0')
1710 ++cp;
1711 while (isspace((int)*cp))
1712 ++cp;
1713 }
1714 strcpy(q, " @setspecial\n");
1715
1716 bbox_valid = False;
1717 /* Validate the case where both rwi and rhi are undefined
1718 * (and llx, lly, urx, ury are properly defined) */
1719 if (!(flags & 0x30) && (flags & 0xf) == 0xf) {
1720 KEY_RWI = (KEY_URX - KEY_LLX) * 10;
1721 flags |= 0x10;
1722 }
1723 if ((flags & 0x30) == 0x30 || ((flags & 0x30) && (flags & 0xf) == 0xf)) {
1724 bbox_valid = True;
1725 bbox_width = 0.1 * ((flags & 0x10) ? KEY_RWI
1726 : KEY_RHI * (KEY_URX - KEY_LLX) / (KEY_URY -
1727 KEY_LLY)) *
1728 dimconv / currwin.shrinkfactor + 0.5;
1729 bbox_voffset = bbox_height =
1730 0.1 * ((flags & 0x20) ? KEY_RHI : KEY_RWI * (KEY_URY - KEY_LLY) /
1731 (KEY_URX - KEY_LLX))
1732 * dimconv / currwin.shrinkfactor + 0.5;
1733 }
1734
1735 if (INSIDE_MANE_WIN) {
1736 #if PS
1737 psp.drawbegin(PXL_H - currwin.base_x, PXL_V - currwin.base_y, buffer);
1738 /* talk directly with the DPSHandler here */
1739 send_ps_file(filename, kpse_pict_format);
1740 psp.drawend(" @endspecial");
1741 #else
1742 draw_bbox();
1743 #endif
1744 }
1745 bbox_valid = False;
1746 }
1747
1748
1749 static void
1750 quote_special(char *cp)
1751 {
1752 bbox_valid = False;
1753
1754 #if PS
1755 if (currwin.win == mane.win) {
1756 psp.drawbegin(PXL_H - currwin.base_x, PXL_V - currwin.base_y,
1757 "@beginspecial @setspecial ");
1758 /* talk directly with the DPSHandler here */
1759 psp.drawraw(cp + 1);
1760 psp.drawend(" @endspecial");
1761 }
1762 #endif
1763
1764 /* nothing else to do--there's no bbox here */
1765 }
1766
1767 #if PS
1768
1769 static void
1770 scan_header(char *cp)
1771 {
1772 char *filename;
1773 /* default for .pro files: */
1774 kpse_file_format_type our_format;
1775
1776 #if PS_GS
1777 if (gs_postpone_prescan)
1778 return;
1779 #endif
1780 filename = cp;
1781 if (*cp == '\'' || *cp == '"') {
1782 do
1783 ++cp;
1784 while (*cp != '\0' && *cp != *filename);
1785 *cp = '\0';
1786 ++filename;
1787 }
1788
1789 psp.beginheader();
1790 /* check for suffixes other than `.pro' (default PS headers),
1791 case-insensitive */
1792 if (str_is_suffix(".pfa", filename, False)
1793 || str_is_suffix(".pfb", filename, False))
1794 our_format = kpse_type1_format;
1795 else if (str_is_suffix(".enc", filename, False))
1796 our_format = kpse_enc_format;
1797 else
1798 our_format = kpse_tex_ps_header_format;
1799
1800 send_ps_file(filename, our_format);
1801 }
1802
1803 static void
1804 scan_bang(char *cp)
1805 {
1806 psp.beginheader();
1807 psp.drawraw(cp + 1);
1808 }
1809
1810 #endif /* PS */
1811
1812 #if COLOR
1813
1814 /*
1815 * Table of dvips color names. Produced by passing the table in
1816 * dvipsk/color.lpro through the following perl script, and then
1817 * through sort.
1818 *
1819 * #! /usr/bin/perl -w
1820 *
1821 * sub cvpart {
1822 * return $_[0] < 1.0 ? 1.0 - $_[0] : 0.0;
1823 * }
1824 *
1825 * $pat = "^\\/(\\w+)\\s*\\{\\s*([0-9.]+)\\s*([0-9.]+)\\s*([0-9.]+)"
1826 * . "\\s*([0-9.]+)\\s*setcmykcolor\\s*\\}\\s*DC\\s*\$";
1827 * while (<STDIN>) {
1828 * chop;
1829 * if (/^%%/) {next;}
1830 * if (/$pat/o) {
1831 * printf "\t\tDVIPSCOLORDESC(%2d, \"%s\", %g, %g, %g),\n",
1832 * length($1), $1,
1833 * cvpart($2 + $5), cvpart($3 + $5), cvpart($4 + $5);
1834 * }
1835 * else {
1836 * print "Bad line: ", $_, "\n";
1837 * }
1838 * }
1839 */
1840
1841 struct dvipscolor {
1842 const char *name;
1843 struct rgb color;
1844 };
1845
1846 static Boolean
1847 parse_color(const char *cp0, const char *cp,
1848 struct rgb *rgbp, Boolean generic_ps_flag)
1849 {
1850 double r, g, b;
1851 double k;
1852 double hue, sat, bri;
1853 char dummy[8];
1854
1855 #define CVPART(x) ((int)((x) * 65535 + 0.5))
1856 #define COLOR_DESC(name, r, g, b) \
1857 {name, {CVPART(r), CVPART(g), CVPART(b)}}
1858
1859 /* hash table to speed up lookup in following list of color names */
1860 static hashTableT colornames_hash;
1861 /*
1862 Use a prime close to sizof colornames / sizeof colornames[0].
1863 You might want to update this when extending the colornames array,
1864 and to check the filling factor and average chain length by uncommenting the
1865 hash_print(colornames_hash, True);
1866 below. I usually go for filling > 50% and avg. chain len < 2.
1867
1868 The 68-elem array below results in:
1869 79 buckets, 43 nonempty (54%); 68 entries, average chain 1.6.
1870 */
1871 static const size_t colornames_hash_size = 79;
1872
1873 static struct dvipscolor colornames[] = {
1874 COLOR_DESC("Red", 1, 0, 0),
1875 COLOR_DESC("Tan", 0.86, 0.58, 0.44),
1876 COLOR_DESC("Blue", 0, 0, 1),
1877 COLOR_DESC("Cyan", 0, 1, 1),
1878 COLOR_DESC("Gray", 0.5, 0.5, 0.5),
1879 COLOR_DESC("Plum", 0.5, 0, 1),
1880 COLOR_DESC("Black", 0, 0, 0),
1881 COLOR_DESC("Brown", 0.4, 0, 0),
1882 COLOR_DESC("Green", 0, 1, 0),
1883 COLOR_DESC("Melon", 1, 0.54, 0.5),
1884 COLOR_DESC("Peach", 1, 0.5, 0.3),
1885 COLOR_DESC("Sepia", 0.3, 0, 0),
1886 COLOR_DESC("White", 1, 1, 1),
1887 COLOR_DESC("Maroon", 0.68, 0, 0),
1888 COLOR_DESC("Orange", 1, 0.39, 0.13),
1889 COLOR_DESC("Orchid", 0.68, 0.36, 1),
1890 COLOR_DESC("Purple", 0.55, 0.14, 1),
1891 COLOR_DESC("Salmon", 1, 0.47, 0.62),
1892 COLOR_DESC("Violet", 0.21, 0.12, 1),
1893 COLOR_DESC("Yellow", 1, 1, 0),
1894 COLOR_DESC("Apricot", 1, 0.68, 0.48),
1895 COLOR_DESC("Emerald", 0, 1, 0.5),
1896 COLOR_DESC("Fuchsia", 0.45, 0.01, 0.92),
1897 COLOR_DESC("Magenta", 1, 0, 1),
1898 COLOR_DESC("SkyBlue", 0.38, 1, 0.88),
1899 COLOR_DESC("Thistle", 0.88, 0.41, 1),
1900 COLOR_DESC("BrickRed", 0.72, 0, 0),
1901 COLOR_DESC("Cerulean", 0.06, 0.89, 1),
1902 COLOR_DESC("Lavender", 1, 0.52, 1),
1903 COLOR_DESC("Mahogany", 0.65, 0, 0),
1904 COLOR_DESC("Mulberry", 0.64, 0.08, 0.98),
1905 COLOR_DESC("NavyBlue", 0.06, 0.46, 1),
1906 COLOR_DESC("SeaGreen", 0.31, 1, 0.5),
1907 COLOR_DESC("TealBlue", 0.12, 0.98, 0.64),
1908 COLOR_DESC("BlueGreen", 0.15, 1, 0.67),
1909 COLOR_DESC("CadetBlue", 0.38, 0.43, 0.77),
1910 COLOR_DESC("Dandelion", 1, 0.71, 0.16),
1911 COLOR_DESC("Goldenrod", 1, 0.9, 0.16),
1912 COLOR_DESC("LimeGreen", 0.5, 1, 0),
1913 COLOR_DESC("OrangeRed", 1, 0, 0.5),
1914 COLOR_DESC("PineGreen", 0, 0.75, 0.16),
1915 COLOR_DESC("RawSienna", 0.55, 0, 0),
1916 COLOR_DESC("RedOrange", 1, 0.23, 0.13),
1917 COLOR_DESC("RedViolet", 0.59, 0, 0.66),
1918 COLOR_DESC("Rhodamine", 1, 0.18, 1),
1919 COLOR_DESC("RoyalBlue", 0, 0.5, 1),
1920 COLOR_DESC("RubineRed", 1, 0, 0.87),
1921 COLOR_DESC("Turquoise", 0.15, 1, 0.8),
1922 COLOR_DESC("VioletRed", 1, 0.19, 1),
1923 COLOR_DESC("Aquamarine", 0.18, 1, 0.7),
1924 COLOR_DESC("BlueViolet", 0.1, 0.05, 0.96),
1925 COLOR_DESC("DarkOrchid", 0.6, 0.2, 0.8),
1926 COLOR_DESC("OliveGreen", 0, 0.6, 0),
1927 COLOR_DESC("Periwinkle", 0.43, 0.45, 1),
1928 COLOR_DESC("Bittersweet", 0.76, 0.01, 0),
1929 COLOR_DESC("BurntOrange", 1, 0.49, 0),
1930 COLOR_DESC("ForestGreen", 0, 0.88, 0),
1931 COLOR_DESC("GreenYellow", 0.85, 1, 0.31),
1932 COLOR_DESC("JungleGreen", 0.01, 1, 0.48),
1933 COLOR_DESC("ProcessBlue", 0.04, 1, 1),
1934 COLOR_DESC("RoyalPurple", 0.25, 0.1, 1),
1935 COLOR_DESC("SpringGreen", 0.74, 1, 0.24),
1936 COLOR_DESC("YellowGreen", 0.56, 1, 0.26),
1937 COLOR_DESC("MidnightBlue", 0, 0.44, 0.57),
1938 COLOR_DESC("YellowOrange", 1, 0.58, 0),
1939 COLOR_DESC("CarnationPink", 1, 0.37, 1),
1940 COLOR_DESC("CornflowerBlue", 0.35, 0.87, 1),
1941 COLOR_DESC("WildStrawberry", 1, 0.04, 0.61),
1942 };
1943 #undef CVPART
1944 #undef COLOR_DESC
1945
1946 UNUSED(cp0);
1947
1948 while (*cp == ' ') ++cp;
1949
1950 if (generic_ps_flag) {
1951 /*
1952 check for pstricks color specials.
1953 The dummy buffer is to ensure that there are no other (general PS)
1954 commands following (we wouldn't know how to deal with these).
1955 */
1956 if (sscanf(cp, "%lf %lf %lf setrgbcolor %7s", &r, &g, &b, dummy) == 3
1957 && r >= 0 && r <= 1 && g >= 0 && g <= 1 && b >= 0 && b <= 1) {
1958 rgbp->r = r * 65535 + 0.5;
1959 rgbp->g = g * 65535 + 0.5;
1960 rgbp->b = b * 65535 + 0.5;
1961 return True;
1962 }
1963 else if (sscanf(cp, "%lf %lf %lf %lf setcmykcolor %7s", &r, &g, &b, &k, dummy) == 4
1964 && r >= 0 && r <= 1 && g >= 0 && g <= 1 && b >= 0 && b <= 1
1965 && k >= 0 && k <= 1) {
1966 r = 1.0 - r - k; /* cyan --> red */
1967 rgbp->r = (r < 0 ? 0 : r * 65535 + 0.5);
1968 g = 1.0 - g - k; /* magenta --> green */
1969 rgbp->g = (g < 0 ? 0 : g * 65535 + 0.5);
1970 b = 1.0 - b - k; /* yellow --> blue */
1971 rgbp->b = (b < 0 ? 0 : b * 65535 + 0.5);
1972 return True;
1973 }
1974 else if (sscanf(cp, "%lf %lf %lf sethsbcolor %7s", &hue, &sat, &bri, dummy) == 3 && hue >= 0
1975 && hue <= 1 && sat >= 0 && sat <= 1 && bri >= 0 && bri <= 1) {
1976 int h = (int) (6 * hue);
1977 double p = bri * (1 - sat);
1978 double q = bri * (1 - sat * (6 * hue - h));
1979 double t = p - q + bri;
1980
1981 switch (h) {
1982 case 0: r = bri; g = t; b = p; break; /* Red - Yel */
1983 case 1: r = q; g = bri; b = p; break; /* Yel - Grn */
1984 case 2: r = p; g = bri; b = t; break; /* Grn - Cyn */
1985 case 3: r = p; g = q; b = bri; break; /* Cyn - Blu */
1986 case 4: r = t; g = p; b = bri; break; /* Blu - Mag */
1987 case 5: r = bri; g = p; b = q; break; /* Mag - Red */
1988 case 6: r = bri; g = b = p; break; /* Red */
1989 }
1990 rgbp->r = r * 65535 + 0.5;
1991 rgbp->g = g * 65535 + 0.5;
1992 rgbp->b = b * 65535 + 0.5;
1993 return True;
1994 }
1995 else if (sscanf(cp, "%lf setgray %7s", &r, dummy) == 1 && r >= 0 && r <= 1) {
1996 rgbp->r = rgbp->g = rgbp->b = r * 65535 + 0.5;
1997 return True;
1998 }
1999 else
2000 return False;
2001 }
2002
2003 /* no generic PS command; must be a dvips color special */
2004
2005 if (memicmp(cp, "rgb ", 4) == 0) {
2006 if (sscanf(cp + 3, "%lf %lf %lf", &r, &g, &b) == 3
2007 && r >= 0 && r <= 1 && g >= 0 && g <= 1 && b >= 0 && b <= 1) {
2008 rgbp->r = r * 65535 + 0.5;
2009 rgbp->g = g * 65535 + 0.5;
2010 rgbp->b = b * 65535 + 0.5;
2011 return True;
2012 }
2013 }
2014 else if (memicmp(cp, "gray ", 5) == 0) {
2015 if (sscanf(cp + 4, "%lf", &r) == 1 && r >= 0 && r <= 1) {
2016 rgbp->r = rgbp->g = rgbp->b = r * 65535 + 0.5;
2017 return True;
2018 }
2019 }
2020 else if (memicmp(cp, "cmyk ", 5) == 0) {
2021 if (sscanf(cp + 4, "%lf %lf %lf %lf", &r, &g, &b, &k) == 4
2022 && r >= 0 && r <= 1 && g >= 0 && g <= 1 && b >= 0 && b <= 1
2023 && k >= 0 && k <= 1) {
2024 r = 1.0 - r - k; /* cyan --> red */
2025 rgbp->r = (r < 0 ? 0 : r * 65535 + 0.5);
2026 g = 1.0 - g - k; /* magenta --> green */
2027 rgbp->g = (g < 0 ? 0 : g * 65535 + 0.5);
2028 b = 1.0 - b - k; /* yellow --> blue */
2029 rgbp->b = (b < 0 ? 0 : b * 65535 + 0.5);
2030 return True;
2031 }
2032 }
2033 else if (memicmp(cp, "hsb ", 4) == 0) {
2034 if (sscanf(cp + 3, "%lf %lf %lf", &hue, &sat, &bri) == 3 && hue >= 0
2035 && hue <= 1 && sat >= 0 && sat <= 1 && bri >= 0 && bri <= 1) {
2036 int h = (int) (6 * hue);
2037 double p = bri * (1 - sat);
2038 double q = bri * (1 - sat * (6 * hue - h));
2039 double t = p - q + bri;
2040
2041 switch (h) {
2042 case 0: r = bri; g = t; b = p; break; /* Red - Yel */
2043 case 1: r = q; g = bri; b = p; break; /* Yel - Grn */
2044 case 2: r = p; g = bri; b = t; break; /* Grn - Cyn */
2045 case 3: r = p; g = q; b = bri; break; /* Cyn - Blu */
2046 case 4: r = t; g = p; b = bri; break; /* Blu - Mag */
2047 case 5: r = bri; g = p; b = q; break; /* Mag - Red */
2048 case 6: r = bri; g = b = p; break; /* Red */
2049 }
2050 rgbp->r = r * 65535 + 0.5;
2051 rgbp->g = g * 65535 + 0.5;
2052 rgbp->b = b * 65535 + 0.5;
2053 return True;
2054 }
2055 }
2056 else { /* is it a dvips color name? */
2057 size_t idx;
2058 size_t len = 0;
2059 static char *testname = NULL;
2060 static size_t testname_len = 0;
2061 size_t testname_step = 16;
2062
2063 if (colornames_hash.size == 0) { /* initialize hash table */
2064 size_t i;
2065 colornames_hash = hash_create(colornames_hash_size);
2066 /* insert color names and array indexes */
2067 for (i = 0; i < (sizeof colornames / sizeof colornames[0]); i++) {
2068 put_str_int_hash(&colornames_hash, colornames[i].name, i);
2069 }
2070 }
2071 /* uncomment the following to get statistics on hashing: */
2072 /* hash_print(colornames_hash, True); */
2073
2074 /* need to copy to a null-terminated string for hash lookup ... */
2075 while (isalpha((int)cp[len])) {
2076 ++len; /* get length of color name */
2077 }
2078 while (len >= testname_len) {
2079 testname_len += testname_step;
2080 testname = xrealloc(testname, testname_len);
2081 }
2082 memcpy(testname, cp, len);
2083 testname[len] = '\0';
2084
2085 if (find_str_int_hash(&colornames_hash, testname, &idx)) {
2086 *rgbp = colornames[idx].color;
2087 return True;
2088 }
2089 }
2090
2091 /* not found */
2092 XDVI_WARNING((stderr, "Ignoring invalid color name in special `%s'", cp0 == NULL ? "<NULL>" : cp0));
2093 return False;
2094 }
2095
2096 void
2097 init_page_colors(struct rgb *foreground, struct rgb *background)
2098 {
2099 int i;
2100
2101 page_colors.size = total_pages;
2102 page_colors.stack = xmalloc(page_colors.size * sizeof *page_colors.stack);
2103
2104 for (i = 0; i <= scanned_page + 1; ++i) {
2105 page_colors.stack[i].bg = *background;
2106 page_colors.stack[i].colorstack = foreground;
2107 page_colors.stack[i].stacksize = 1;
2108 }
2109 for (; i < total_pages; i++) {
2110 page_colors.stack[i].colorstack = NULL;
2111 }
2112
2113 scanstack_head.color = *foreground;
2114 scanstack_len = 1; /* nothing yet */
2115 scanstack_current = &scanstack_head; /* stack position */
2116 }
2117
2118
2119 static void
2120 scan_bg_color(const char *cp)
2121 {
2122 if (!resource.use_color)
2123 return;
2124
2125 if (page_colors.stack == NULL)
2126 init_page_colors(&fg_initial, &bg_initial);
2127
2128 ASSERT(scanned_page < (int)page_colors.size, "page_colors.size too small");
2129 (void) parse_color(cp, cp + 11, &(page_colors.stack[scanned_page + 1].bg), False);
2130 }
2131
2132 void
2133 scan_color(const char *cp)
2134 {
2135 const char *cp1 = cp + 6;
2136
2137 if (!resource.use_color)
2138 return;
2139
2140 while (*cp1 == ' ') ++cp1;
2141
2142 if (page_colors.stack == NULL)
2143 init_page_colors(&fg_initial, &bg_initial);
2144
2145 if (memicmp(cp1, "push ", 5) == 0) {
2146 if (scanstack_current->next == NULL) { /* if at end */
2147 scanstack_current->next = xmalloc(sizeof *scanstack_current);
2148 scanstack_current->next->prev = scanstack_current;
2149 scanstack_current->next->next = NULL;
2150 }
2151 scanstack_current = scanstack_current->next;
2152 ++scanstack_len;
2153 if (!parse_color(cp, cp1 + 5, &scanstack_current->color, False))
2154 scanstack_current->color = scanstack_current->prev->color;
2155 }
2156 else if (memicmp(cp1, "pop", 3) == 0) {
2157 if (scanstack_len <= 1) {
2158 XDVI_WARNING((stderr, "Color pop occurred with empty color stack."));
2159 }
2160 else {
2161 scanstack_current = scanstack_current->prev;
2162 --scanstack_len;
2163 }
2164 }
2165 else {
2166 (void) parse_color(cp, cp1, &scanstack_head.color, False);
2167 if (scanstack_len > 1) {
2168 struct colorframe *cfp;
2169
2170 XDVI_WARNING((stderr, "Global color change occurred with non-empty color stack!\n"
2171 "Trying to recover by setting all stack entries to that color."));
2172 for (cfp = scanstack_head.next;; cfp = cfp->next) {
2173 cfp->color = scanstack_head.color;
2174 if (cfp == scanstack_current) break;
2175 }
2176 }
2177 }
2178 }
2179
2180 void
2181 scan_color_eop(void)
2182 {
2183 int i;
2184 struct rgb *prev, *p1, *rgbp;
2185 struct colorframe *cf;
2186
2187 if (page_colors.stack == NULL)
2188 return;
2189
2190 /* set background color for next page */
2191 if (scanned_page + 1 < total_pages) {
2192 ASSERT(scanned_page + 1 < (int)page_colors.size, "page_colors.size too small");
2193 page_colors.stack[scanned_page + 1].bg = page_colors.stack[scanned_page].bg;
2194 }
2195 ASSERT(scanned_page < (int)page_colors.size, "page_colors.size too small");
2196 /* save the stack contents */
2197 page_colors.stack[scanned_page].stacksize = scanstack_len;
2198
2199 prev = &fg_initial;
2200 i = 1;
2201 if (scanned_page > 0) {
2202 prev = page_colors.stack[scanned_page - 1].colorstack;
2203 i = page_colors.stack[scanned_page - 1].stacksize;
2204 }
2205 if (scanstack_len <= i) {
2206 /* try to reuse the previous array */
2207 p1 = prev;
2208 cf = &scanstack_head;
2209 for (i = 0;;) {
2210 if (p1->r != cf->color.r || p1->g != cf->color.g || p1->b != cf->color.b)
2211 break;
2212 if (++i >= scanstack_len) { /* end loop; reuse memory */
2213 page_colors.stack[scanned_page].colorstack = prev;
2214 return; /* done */
2215 }
2216 ++p1;
2217 cf = cf->next;
2218 }
2219 }
2220 page_colors.stack[scanned_page].colorstack = rgbp = xmalloc(scanstack_len * sizeof *rgbp);
2221 cf = &scanstack_head;
2222 for (i = 0; i < scanstack_len; ++i) {
2223 *rgbp++ = cf->color;
2224 cf = cf->next;
2225 }
2226 }
2227
2228
2229 /*
2230 * We don't actually do any X calls yet to change colors; that can wait
2231 * until a character or rule needs to be drawn.
2232 */
2233
2234 void
2235 set_fg_color(const struct rgb *color)
2236 {
2237 struct fgrec **fgpp;
2238
2239 if (fg_current != NULL
2240 && color->r == fg_current->color.r
2241 && color->g == fg_current->color.g
2242 && color->b == fg_current->color.b) {
2243 return;
2244 }
2245
2246 ASSERT(bg_current != NULL, "Background color not initialized");
2247 for (fgpp = &bg_current->fg_head;;) {
2248 fg_current = *fgpp;
2249 if (fg_current == NULL) { /* if color is not in list */
2250 fg_current = *fgpp = xmalloc(sizeof *fg_current);
2251 fg_current->next = NULL;
2252 fg_current->color = *color;
2253 fg_current->pixel_good = False;
2254 #if GREY
2255 fg_current->palette_good = False;
2256 #endif
2257 break;
2258 }
2259 if (fg_current->color.r == color->r
2260 && fg_current->color.g == color->g
2261 && fg_current->color.b == color->b)
2262 break;
2263 fgpp = &fg_current->next;
2264 }
2265 if (globals.debug & DBG_DVI)
2266 printf("Changing fg color to %5d %5d %5d\n",
2267 color->r, color->g, color->b);
2268 }
2269
2270 #if 0
2271 static void
2272 show_stack(void) {
2273 struct colorframe *ptr;
2274 int i;
2275 fprintf(stderr, "FG: %d,%d,%d\n", fg_current->color.r, fg_current->color.b, fg_current->color.g);
2276 for (i = 0, ptr = rcs_top; ptr != NULL; ptr = ptr->prev, i++) {
2277 fprintf(stderr, "stack %d: %d,%d,%d\n", i, ptr->color.r, ptr->color.b, ptr->color.g);
2278 }
2279 }
2280 #endif
2281
2282 void
2283 color_special(const char *cp)
2284 {
2285 while (*cp == ' ') ++cp;
2286
2287 if (memicmp(cp, "push ", 5) == 0) {
2288 if (rcs_top == NULL) {
2289 if (rcs_head == NULL) {
2290 rcs_head = xmalloc(sizeof *rcs_head);
2291 rcs_head->prev = rcs_head->next = NULL;
2292 }
2293 rcs_top = rcs_head;
2294 }
2295 else {
2296 struct colorframe *rcs_next;
2297
2298 rcs_next = rcs_top->next;
2299 if (rcs_next == NULL) {
2300 rcs_next = rcs_top->next = xmalloc(sizeof *rcs_next);
2301 rcs_next->prev = rcs_top;
2302 rcs_next->next = NULL;
2303 }
2304 rcs_top = rcs_next;
2305 }
2306 if (!parse_color(NULL, cp + 5, &rcs_top->color, False)) {
2307 if (rcs_top->prev != NULL)
2308 rcs_top->color = rcs_top->prev->color;
2309 else
2310 rcs_top->color = color_bottom[color_bot_size - 1];
2311 }
2312 set_fg_color(&rcs_top->color);
2313 }
2314 else if (memicmp(cp, "pop", 3) == 0) {
2315 /* Pop stack */
2316 if (rcs_top != NULL) {
2317 if (color_bot_size > 0)
2318 rcs_top = rcs_top->prev;
2319 else
2320 return;
2321 }
2322 else if (color_bot_size > 1)
2323 --color_bot_size;
2324 else {
2325 if (scanned_page_reset >= 0) {
2326 /* turn on scanning and redraw the page */
2327 scanned_page = scanned_page_color = scanned_page_reset = -1;
2328 #if PS
2329 scanned_page_ps = scanned_page;
2330 #endif
2331 /* fprintf(stderr, "forcing redraw!!!\n"); */
2332 globals.ev.flags |= EV_NEWPAGE; /* force a redraw */
2333 longjmp(globals.ev.canit, 1);
2334 }
2335 return;
2336 }
2337 /* fprintf(stderr, "bot_size: %d\n", color_bot_size); */
2338 set_fg_color(rcs_top != NULL ? &rcs_top->color
2339 : &color_bottom[color_bot_size - 1]);
2340 }
2341 else {
2342 struct rgb color;
2343
2344 if (!parse_color(NULL, cp, &color, False))
2345 return;
2346
2347 /* clear stack */
2348 if (rcs_head == NULL) {
2349 rcs_head = xmalloc(sizeof *rcs_head);
2350 rcs_head->prev = rcs_head->next = NULL;
2351 }
2352 rcs_top = rcs_head;
2353 color_bot_size = 0;
2354
2355 /* Change top of stack */
2356 rcs_top->color = color;
2357
2358 set_fg_color(&color);
2359 }
2360 }
2361
2362 #endif /* COLOR */
2363
2364 static unsigned int
2365 myatopix(const char **pp)
2366 {
2367 #define SCR_LEN 16
2368 unsigned int value;
2369 const char *cp = *pp;
2370 char scr[16];
2371 const char *p0, *p1;
2372
2373 p0 = cp;
2374 while ((*cp >= '0' && *cp <= '9') || *cp == '.')
2375 ++cp;
2376 p1 = cp;
2377 while (isspace((int)*cp))
2378 ++cp;
2379 if (*cp >= 'a' && *cp <= 'z' && cp[1] >= 'a' && cp[1] <= 'z') {
2380 /* if units are present */
2381 if (p1 - p0 <= SCR_LEN - 3) {
2382 sprintf(scr, "%.*s%c%c", (int)(p1 - p0), p0, *cp, cp[1]);
2383 value = atopix(scr);
2384 }
2385 else
2386 value = 0;
2387 cp += 2;
2388 }
2389 else
2390 value = atopix(p0);
2391
2392 *pp = cp;
2393 return value;
2394 #undef SCR_LEN
2395 }
2396
2397 static void
2398 scan_papersize(const char *cp0)
2399 {
2400 const char *cp = cp0;
2401 unsigned int w, h;
2402 double mag = 1.0;
2403
2404 if (ignore_papersize_specials)
2405 return;
2406
2407 if (*cp == '*') {
2408 do
2409 ++cp;
2410 while (isspace((int)*cp));
2411 mag = magnification * .001;
2412 }
2413
2414 w = myatopix(&cp) * mag + 0.5;
2415
2416 while (isspace((int)*cp))
2417 ++cp;
2418 if (*cp == ',')
2419 do
2420 ++cp;
2421 while (isspace((int)*cp));
2422
2423 h = myatopix(&cp) * mag + 0.5;
2424
2425 if (w == 0 || h == 0)
2426 XDVI_WARNING((stderr, "Invalid papersize special `%s'", cp0));
2427 else {
2428 /* we have a paper size special; disable xdvirc_geometry:
2429 fprintf(stderr, "---------- scanned papersize: %dx%d\n", w, h);
2430 resource.xdvirc_geometry = NULL;
2431 */
2432 pageinfo_set_page_width(scanned_page + 1, w);
2433 pageinfo_set_window_width(scanned_page + 1, w);
2434 pageinfo_set_page_height(scanned_page + 1, h);
2435 pageinfo_set_window_height(scanned_page + 1, h);
2436 }
2437 }
2438
2439 /*
2440 * The following copyright message applies to the rest of this file. --PV
2441 */
2442
2443 /*
2444 * This program is Copyright (C) 1987 by the Board of Trustees of the
2445 * University of Illinois, and by the author Dirk Grunwald.
2446 *
2447 * This program may be freely copied, as long as this copyright
2448 * message remaines affixed. It may not be sold, although it may
2449 * be distributed with other software which is sold. If the
2450 * software is distributed, the source code must be made available.
2451 *
2452 * No warranty, expressed or implied, is given with this software.
2453 * It is presented in the hope that it will prove useful.
2454 *
2455 * Hacked in ignorance and desperation by [email protected]
2456 */
2457
2458 /*
2459 * The code to handle the \specials generated by tpic was modified
2460 * by Dirk Grunwald using the code Tim Morgan at Univ. of Calif, Irvine
2461 * wrote for TeXsun.
2462 */
2463
2464 static char *
2465 endofcommand(char *cp)
2466 {
2467 while (isspace((int)*cp))
2468 ++cp;
2469 if (*cp != '=')
2470 return NULL;
2471 do
2472 ++cp;
2473 while (isspace((int)*cp));
2474 return cp;
2475 }
2476
2477 #define CMD(x, y) ((x) << 8 | (y))
2478
2479 void
2480 applicationDoSpecial(char *cp, size_t len)
2481 {
2482 char *p;
2483
2484 if (globals.debug & DBG_DVI)
2485 printf(" `%s'\n", cp);
2486
2487 while (isspace((int)*cp))
2488 ++cp;
2489
2490 /* Ignore initial "xdvi:" */
2491 if (memcmp(cp, "xdvi:", 5) == 0) {
2492 cp += 5;
2493 while (isspace((int)*cp))
2494 ++cp;
2495 }
2496
2497 /* PostScript specials */
2498 #ifdef MAGICK
2499 if (resource.useMAGICK) {
2500 if (Magick_parse_special(cp))
2501 return;
2502 }
2503 #endif
2504
2505 if (*cp == '"') {
2506 quote_special(cp);
2507 return;
2508 }
2509 if (memicmp(cp, "ps:", 3) == 0) {
2510 cp += 3;
2511 psfig_special(cp);
2512 /* check for hdvips hyperlinks */
2513 if (memicmp(cp, "sdict begin ", strlen("sdict begin ")) == 0) {
2514 static Boolean warned_hypertex_too_old = False;
2515 char *match = NULL;
2516 if (warned_hypertex_too_old) /* don't continue evaluating links in this case */
2517 return;
2518 cp += strlen("sdict begin ");
2519 if (memcmp(cp, "H.S", 3) == 0
2520 || memcmp(cp, "H.R", 3) == 0
2521 || memcmp(cp, "H.B", 3) == 0
2522 /* following 2 conditions could be more restrictive: between `begin' and H.A/H.L,
2523 there should be a single number (baselineskip in pt) */
2524 || (match = strstr(cp, "H.A")) != NULL
2525 || (match = strstr(cp, "H.L")) != NULL
2526 || (match = strstr(cp, "/Action")) != NULL
2527 || (match = strstr(cp, "/Link")) != NULL
2528 || (match = strstr(cp, "/View")) != NULL) {
2529 if (match != NULL)
2530 htex_do_special(match, len - 3 - (match - cp) - strlen("sdict begin "));
2531 else
2532 htex_do_special(cp, len - strlen("sdict begin "));
2533 }
2534 else if (!warned_hypertex_too_old && strstr(cp, "HyperStart") != NULL) {
2535 popup_message(globals.widgets.top_level,
2536 MSG_WARN, NULL,
2537 "This DVI was created with a too old version of the `dvips' hyperref driver - "
2538 "disabling hyperlinks.\n"
2539 "To fix this, you should either upgrade to a current version of hyperref "
2540 "(see http://www.tug.org/applications/hyperref/), "
2541 "or use the `hypertex' package option, like this:\n\\usepackage[hypertex]{hyperref}\n"
2542 "(Be aware though that this option won't work for PS->PDF conversion!)");
2543 warned_hypertex_too_old = True;
2544 }
2545 }
2546 /* When not ignoring SDict entries, the distiller and pagecolor
2547 code in lshort.dvi from CTAN:info/lshort/russian/lshrtdvi.zip
2548 causes a failed assertion for 'color_bot_size > 0' in dvi-draw.c;
2549 there's something wrong with the parsing order/event handling here
2550 (see bug #856547).
2551 But we also don't want those entries to trigger erasepage_gs()
2552 [deleted in r3810], so it's correct to ignore them here.
2553 */
2554 return;
2555 }
2556 if (memicmp(cp, "psfile", 6) == 0 && (p = endofcommand(cp + 6)) != NULL) {
2557 epsf_special(p);
2558 return;
2559 }
2560 if (memicmp(cp, "html:", 5) == 0) {
2561 htex_do_special(cp + 5, len - 5);
2562 return;
2563 }
2564
2565 #if COLOR
2566 if (memicmp(cp, "color ", 6) == 0) {
2567 /* fprintf(stderr, "------------- color special\n"); */
2568 if (resource.use_color)
2569 color_special(cp + 6);
2570 return;
2571 }
2572 #endif
2573
2574 /* these should have been scanned */
2575 if (*cp == '!'
2576 || (memicmp(cp, "header", 6) == 0 && endofcommand(cp + 6) != NULL)) {
2577 #ifdef PS
2578 if (resource.postscript != 0 && scanned_page_reset >= 0) {
2579 /* turn on scanning and redraw the page */
2580 scanned_page = scanned_page_ps = scanned_page_reset = -1;
2581 # if COLOR
2582 scanned_page_color = scanned_page;
2583 # endif
2584 globals.ev.flags |= EV_NEWPAGE; /* force a redraw */
2585 longjmp(globals.ev.canit, 1);
2586 }
2587 #endif /* PS */
2588 return;
2589 }
2590
2591 if (memicmp(cp, "background ", 11) == 0) {
2592 #if COLOR
2593 if (resource.use_color && scanned_page_reset >= 0) {
2594 /* turn on scanning and redraw the page */
2595 scanned_page = scanned_page_color = scanned_page_reset = -1;
2596 # if PS
2597 scanned_page_ps = scanned_page;
2598 # endif
2599 /* fprintf(stderr, "forcing redraw!\n"); */
2600 globals.ev.flags |= EV_NEWPAGE; /* force a redraw */
2601 longjmp(globals.ev.canit, 1);
2602 }
2603 #endif /* COLOR */
2604 return;
2605 }
2606
2607 if (memcmp(cp, "papersize", 9) == 0 && endofcommand(cp + 9) != NULL) {
2608 m_have_papersize_special = True;
2609 if (scanned_page_reset >= 0) {
2610 /* turn on scanning and redraw the page */
2611 scanned_page = scanned_page_reset = -1;
2612 #if PS
2613 scanned_page_ps = scanned_page;
2614 #endif
2615 #if COLOR
2616 scanned_page_color = scanned_page;
2617 #endif
2618 globals.ev.flags |= EV_NEWPAGE; /* force a redraw */
2619 longjmp(globals.ev.canit, 1);
2620 }
2621 return;
2622 }
2623
2624 /* tpic specials */
2625
2626 if (*cp >= 'a' && *cp <= 'z' && cp[1] >= 'a' && cp[1] <= 'z' &&
2627 (isspace((int)cp[2]) || cp[2] == '\0')) {
2628 switch (CMD(*cp, cp[1])) {
2629 case CMD('p', 'n'):
2630 set_pen_size(cp + 2);
2631 return;
2632 case CMD('f', 'p'):
2633 flush_path();
2634 return;
2635 case CMD('d', 'a'):
2636 flush_dashed(cp + 2, False);
2637 return;
2638 case CMD('d', 't'):
2639 flush_dashed(cp + 2, True);
2640 return;
2641 case CMD('p', 'a'):
2642 add_path(cp + 2);
2643 return;
2644 case CMD('a', 'r'):
2645 arc(cp + 2, False);
2646 return;
2647 case CMD('i', 'a'):
2648 arc(cp + 2, True);
2649 return;
2650 case CMD('s', 'p'):
2651 flush_spline();
2652 return;
2653 case CMD('s', 'h'):
2654 shade_last();
2655 return;
2656 case CMD('w', 'h'):
2657 whiten_last();
2658 return;
2659 case CMD('b', 'k'):
2660 blacken_last();
2661 return;
2662 case CMD('i', 'p'): /* throw away the path -- jansteen */
2663 path_len = 0;
2664 return;
2665 }
2666 }
2667
2668 if (memcmp(cp, "src:", 4) == 0) {
2669 have_src_specials = True;
2670 }
2671 else if (globals.warn_spec_now)
2672 XDVI_WARNING((stderr, "Special \"%s\" not implemented.", cp));
2673 }
2674
2675 #undef CMD
2676
2677
2678 Boolean
2679 scan_special(char *cp, int cp_len, void *data)
2680 {
2681 char *p;
2682 Boolean dummy_ret = True; /* currently unused;
2683 FIXME: use return value to avoid redundant calls (instead of longjmp()) */
2684 UNUSED(cp_len); /* TODO: could probably utilize this in call to htex_prescan_special() */
2685 ASSERT(data != NULL, "Must pass a data pointer when using HTEX");
2686
2687 if (globals.debug & DBG_PS)
2688 printf("Scanning special `%s'.\n", cp);
2689
2690 while (isspace((int)*cp))
2691 ++cp;
2692
2693 /* Ignore initial "xdvi:" */
2694 if (memcmp(cp, "xdvi:", 5) == 0) {
2695 cp += 5;
2696 while (isspace((int)*cp))
2697 ++cp;
2698 }
2699
2700 if (memicmp(cp, "ps:", 3) == 0) {
2701 cp += 3;
2702 /* check for hdvips hyperlinks */
2703 if (memicmp(cp, "sdict begin ", strlen("sdict begin ")) == 0) {
2704 static Boolean hypertex_too_old = False;
2705 char *match = NULL;
2706 cp += strlen("sdict begin ");
2707
2708 if (strstr(cp, "HyperStart") != NULL)
2709 hypertex_too_old = True;
2710 if (hypertex_too_old)
2711 return False;
2712
2713 if (memcmp(cp, "H.S", 3) == 0
2714 || memcmp(cp, "H.R", 3) == 0
2715 || memcmp(cp, "H.B", 3) == 0
2716 /* following 2 conditions could be more restrictive: between `begin' and H.A/H.L,
2717 there should be a single number (baselineskip in pt) */
2718 || (match = strstr(cp, "H.A")) != NULL
2719 || (match = strstr(cp, "H.L")) != NULL
2720 || (match = strstr(cp, "/Action")) != NULL
2721 || (match = strstr(cp, "/Link")) != NULL
2722 || (match = strstr(cp, "/View")) != NULL) {
2723 if (match != NULL)
2724 htex_prescan_special(match, cp_len, data);
2725 else
2726 htex_prescan_special(cp, cp_len, data);
2727 }
2728 }
2729
2730 }
2731 else if (memicmp(cp, "html:", strlen("html:")) == 0) {
2732 size_t offset = strlen("html:");
2733 #if 0
2734 Boolean found =
2735 #endif
2736 htex_prescan_special(cp + offset, cp_len - offset, data);
2737 #if 0
2738 /* if searching for a specific string, return as soon as it's found - not yet implemented */
2739 if (my_data != NULL && my_data->scan_type == HTEX_ANCHOR_STRING && found) {
2740 return True;
2741 }
2742 #endif
2743 }
2744 /* do the following only if not searching for an anchor string */
2745 if (((struct htex_prescan_data *)data)->scan_type != HTEX_ANCHOR_STRING) {
2746 #if PS
2747 # if COLOR
2748 if (scanned_page_ps <= scanned_page)
2749 # endif
2750 {
2751 if (*cp == '!') {
2752 scan_bang(cp);
2753 return dummy_ret;
2754 }
2755 else if (memicmp(cp, "header", 6) == 0 && (p = endofcommand(cp + 6)) != NULL) {
2756 scan_header(p);
2757 return dummy_ret;
2758 }
2759 }
2760 #endif /* PS */
2761
2762 #if COLOR
2763 # if PS
2764 if (scanned_page_color <= scanned_page)
2765 # endif
2766 {
2767 if (memicmp(cp, "background ", 11) == 0) {
2768 scan_bg_color(cp);
2769 return dummy_ret;
2770 }
2771 else if (memicmp(cp, "color ", 6) == 0) {
2772 scan_color(cp);
2773 return dummy_ret;
2774 }
2775 }
2776 #endif /* COLOR */
2777 if (memcmp(cp, "papersize", 9) == 0 && (p = endofcommand(cp + 9)) != NULL) {
2778 m_have_papersize_special = True;
2779 scan_papersize(p);
2780 return dummy_ret;
2781 }
2782 }
2783 return dummy_ret;
2784 }
2785
2786
2787 /* #define xspell_conv(n) spell_conv0(n, current_dimconv) */
2788 #define xpixel_conv(x) ((int) ((x) >> 16))
2789 #define G_PXL_H xpixel_conv(currinf.data.dvi_h)
2790
2791 /* cp is not const, because of endofcommand(). */
2792 void
2793 geom_do_special(struct scan_info *info, char *cp, double current_dimconv)
2794 {
2795 const char *p;
2796 struct geom_info *g_info = (struct geom_info *)info->data;
2797
2798 UNUSED(current_dimconv);
2799
2800 while (isspace((int)*cp))
2801 ++cp;
2802
2803 /* Ignore initial "xdvi:" */
2804 if (memcmp(cp, "xdvi:", 5) == 0) {
2805 cp += 5;
2806 while (isspace((int)*cp))
2807 ++cp;
2808 }
2809
2810 if (memicmp(cp, "psfile", 6) == 0 && (p = endofcommand(cp + 6)) != NULL) {
2811 /* compute epsf bounding box */
2812 char c;
2813 int flags = 0;
2814 double keyval[6];
2815
2816 c = *p;
2817 if (c == '\'' || c == '"') {
2818 do
2819 ++p;
2820 while (*p != '\0' && *p != c);
2821 }
2822 else
2823 while (*p != '\0' && !isspace((int)*p))
2824 ++p;
2825 while (isspace((int)*p))
2826 ++p;
2827 while (*p != '\0') {
2828 const char *p1 = p;
2829 size_t keyno;
2830
2831 while (*p1 != '=' && !isspace((int)*p1) && *p1 != '\0')
2832 ++p1;
2833 for (keyno = 0; keyno < NKEYS; ++keyno) {
2834 if (memcmp(p, keytab[keyno], p1 - p) == 0) {
2835 if (keyno >= N_ARGLESS_KEYS) {
2836 while (isspace((int)*p1))
2837 ++p1;
2838 if (*p1 == '=') {
2839 ++p1;
2840 while (isspace((int)*p1))
2841 ++p1;
2842 }
2843 if (keyno < N_ARGLESS_KEYS + 6) {
2844 keyval[keyno - N_ARGLESS_KEYS] = atof(p1);
2845 flags |= (1 << (keyno - N_ARGLESS_KEYS));
2846 }
2847 while (!isspace((int)*p1) && *p1 != '\0')
2848 ++p1;
2849 }
2850 break;
2851 }
2852 }
2853 p = p1;
2854 while (!isspace((int)*p) && *p != '\0')
2855 ++p;
2856 while (isspace((int)*p))
2857 ++p;
2858 }
2859
2860 if ((flags & 0x30) == 0x30 || ((flags & 0x30) && (flags & 0xf) == 0xf)) {
2861 long x = G_PXL_H;
2862 long y = PXL_V;
2863 long bbox_w;
2864 long bbox_h;
2865
2866 bbox_w = 0.1 * ((flags & 0x10) ? KEY_RWI
2867 : KEY_RHI * (KEY_URX - KEY_LLX) / (KEY_URY -
2868 KEY_LLY)) *
2869 dimconv + 0.5;
2870 bbox_h =
2871 0.1 * ((flags & 0x20) ? KEY_RHI : KEY_RWI *
2872 (KEY_URY - KEY_LLY) / (KEY_URX - KEY_LLX))
2873 * dimconv + 0.5;
2874
2875 g_info->geom_box(info, x, y - bbox_h, x + bbox_w, y);
2876 }
2877 }
2878 else if (memicmp(cp, "ps::[begin]", 11) == 0) {
2879 /* compute psfig bounding box */
2880 long bbox_w, bbox_h;
2881
2882 if (sscanf(cp + 11, "%ld %ld\n", &bbox_w, &bbox_h) >= 2) {
2883 long x = G_PXL_H;
2884 long y = PXL_V;
2885
2886 bbox_w = xpixel_conv(spell_conv(bbox_w));
2887 bbox_h = xpixel_conv(spell_conv(bbox_h));
2888
2889 g_info->geom_box(info, x, y, x + bbox_w, y + bbox_h);
2890 }
2891 }
2892 }