Debian Codegraph @

texk/xdvipsk/drawPS.c

raw
1 /*
2 * D R A W P S . C
3 *
4 * Changed to take magnification into account, 27 August 1990.
5 *
6 * (minor mods by don on 5 Jan 90 to accommodate highres PostScript)
7 *
8 * $Revision: 1.1 $
9 *
10 * $Log: drawPS.c,v $
11 * Revision 1.1 90/03/10 20:32:48 grunwald
12 * Initial revision
13 *
14 *
15 * 89/04/24: [email protected], [email protected]
16 * added three shading levels for fig,
17 * removed '\n' in cmdout() calls
18 *
19 * Revision 1.4 87/05/07 15:06:24 dorab
20 * relinked back hh to h and vv to v undoing a previous change.
21 * this was causing subtle quantization problems. the main change
22 * is in the definition of hconvPS and vconvPS.
23 *
24 * changed the handling of the UC seal. the PS file should now be
25 * sent via the -i option.
26 *
27 * Revision 1.3 86/04/29 23:20:55 dorab
28 * first general release
29 *
30 * Revision 1.3 86/04/29 22:59:21 dorab
31 * first general release
32 *
33 * Revision 1.2 86/04/29 13:23:40 dorab
34 * Added distinctive RCS header
35 *
36 */
37 #ifndef lint
38 char RCSid[] =
39 "@(#)$Header: /usr/local/src/TeX/Dvips-5.0.2/RCS/drawPS.c,v 1.1 90/03/10 20:32:48 grunwald Exp $ (UCLA)";
40 #endif
41
42 /*
43 the driver for handling the \special commands put out by
44 the tpic program as modified by Tim Morgan <[email protected]>
45 the co-ordinate system is with the origin at the top left
46 and the x-axis is to the right, and the y-axis is to the bottom.
47 when these routines are called, the origin is set at the last dvi position,
48 which has to be gotten from the dvi-driver (in this case, dvips) and will
49 have to be converted to device co-ordinates (in this case, by [hv]convPS).
50 all dimensions are given in milli-inches and must be converted to what
51 dvips has set up (i.e. there are convRESOLUTION units per inch).
52
53 it handles the following \special commands
54 pn n set pen size to n
55 pa x y add path segment to (x,y)
56 fp flush path
57 ip flush invisible path, i.e. do shading only
58 da l flush dashed - each dash is l (inches)
59 dt l flush dotted - one dot per l (inches)
60 sp [l] flush spline - optional l is dot/dash length
61 ar x y xr yr sa ea arc centered at (x,y) with x-radius xr
62 and y-radius yr, start angle sa (in rads),
63 end angle ea (in rads)
64 ia x y xr yr sa ea invisible arc with same parameters as above
65 sh [s] shade last path (box, circle, ellipse)
66 s = gray level (0 = white, 1 = black)
67 wh whiten last path (box, circle, ellipse)
68 bk blacken last path (box,circle, ellipse)
69 tx texture command - Added by T. Priol
70 ([email protected]), enhanced by M. Jourdan:
71 textures are translated into uniform gray levels
72
73 this code is in the public domain
74
75 written by dorab patel <[email protected]>
76 december 1985
77 released feb 1986
78 changes for dvips july 1987
79
80 */
81
82 #ifndef XDVIPSK
83 #include "dvips.h"
84 #else
85 #include "xdvips.h" /* The copyright notice in that file is included too! */
86 #endif /* XDVIPSK */
87
88 #ifdef TPIC /* rest of the file !! */
89
90 #include <math.h> /* used in function "arc" */
91
92 /*
93 * The external declarations:
94 */
95 #include "protos.h"
96
97 #define convRESOLUTION DPI
98 #define convVRESOLUTION VDPI
99 #define tpicRESOLUTION 1000 /* number of tpic units per inch */
100 #ifndef TRUE
101 #define TRUE 1
102 #define FALSE 0
103 #endif
104
105 /* convert from tpic units to PS units */
106 #define PixRound(a,b) zPixRound((a),(b))
107 #define convPS(x) PixRound((x), dir ? convVRESOLUTION : convRESOLUTION)
108 #define convVPS(x) PixRound((x), dir ? convRESOLUTION : convVRESOLUTION)
109 #define ConvPS(x) PixRound((x), convRESOLUTION)
110 #define ConvVPS(x) PixRound((x), convVRESOLUTION)
111
112 /* convert from tpic locn to abs horiz PS locn */
113 #define hconvPS(x) dir ? (vv + ConvVPS(x)) : (hh + ConvPS(x))
114 /* convert from tpic locn to abs vert PS locn */
115 #define vconvPS(x) dir ? (-hh + ConvPS(x)) : (vv + ConvVPS(x))
116 /* convert to degrees */
117 #define convDeg(x) (360*(x)/(2*3.14159265358979))
118
119 /* if PostScript had splines, i wouldnt need to store the path */
120 #define MAXPATHS 6000 /* maximum number of path segments */
121 #define NONE 0 /* for shading */
122 #define BLACK 1 /* for shading */
123 #define GRAY 2 /* MJ; for shading */
124 #define WHITE 3 /* for shading */
125
126 /* the following defines are used to make the PostScript shorter;
127 the corresponding defines are in the PostScript prolog special.lpro */
128 #define MOVETO "a"
129 #define LINETO "li"
130 #define RCURVETO "rc"
131 #define RLINETO "rl"
132 #define STROKE "st"
133 #define FILL "fil"
134 #define NEWPATH "np"
135 #define CLOSEPATH "closepath"
136 /*
137 * STROKE and FILL must restore the current point to that
138 * saved by NEWPATH
139 */
140
141 static double xx[MAXPATHS], yy[MAXPATHS]; /* the current path in milli-inches */
142 static integer pathLen = 0; /* the current path length */
143 static integer shading = NONE; /* what to shade the last figure */
144 static integer penSize = 2; /* pen size in PS units */
145
146 /* forward declarations */
147 /* static void doShading(); */
148 static double zPixRound(double, double); /* (x/y)PixRound(x,y) */
149 static double shadetp = 0.5;
150 /* shading level, initialized as requested by tpic 2.0 -- MJ */
151
152 void
153 setPenSize(char *cp)
154 {
155 long ps;
156
157 if (sscanf(cp, " %ld ", &ps) != 1)
158 {
159 error("Illegal .ps command format");
160 return;
161 }
162
163 penSize = convPS(ps);
164 doubleout((integer)penSize);
165 cmdout("setlinewidth");
166 } /* end setPenSize */
167
168 void
169 addPath(char *cp)
170 {
171 double x,y;
172
173 if (++pathLen >= MAXPATHS) error("! Too many points");
174 if (sscanf(cp, " %lg %lg ", &x, &y) != 2)
175 error("! Malformed path expression");
176 xx[pathLen] = x;
177 yy[pathLen] = y;
178 } /* end of addPath */
179
180 void
181 arc(char *cp, int invis)
182 {
183 double xc, yc, xrad, yrad;
184 double startAngle, endAngle;
185
186 if (sscanf(cp, " %lg %lg %lg %lg %lg %lg ", &xc, &yc, &xrad, &yrad,
187 &startAngle, &endAngle) != 6)
188 {
189 error("Illegal arc specification");
190 return;
191 }
192
193 /* To have filled ellipses/circles also have borders, we duplicate the object
194 specification; first time we emit a FILL command, second time we emit a
195 STROKE command.
196 There certainly exists a better way to do that in PostScript, by
197 remembering the object specification (path) across the fill command.
198 However I (MJ) am completely unproficient at PostScript...
199 */
200
201 /* we need the newpath since STROKE doesnt do a newpath */
202
203 if (shading) {
204 /* first time for shading */
205 cmdout(NEWPATH);
206 doubleout(hconvPS(xc));
207 doubleout(vconvPS(yc));
208 doubleout(convPS(xrad));
209 if (xrad != yrad || VDPI != DPI)
210 doubleout(convVPS(yrad));
211 doubleout(convDeg(startAngle));
212 doubleout(convDeg(endAngle));
213
214 if (xrad == yrad && VDPI == DPI) /* for arcs and circles */
215 cmdout("arc");
216 else
217 cmdout("ellipse");
218
219 cmdout(FILL);
220 shading = NONE;
221 cmdout("0 setgray"); /* default of black */
222 }
223
224 if (!invis) {
225 /* There is a problem with tpic 2.0's handling of dotted ellipses.
226 In most (all?) cases, after conversion to degrees and rounding
227 to integers, startAngle and endAngle become the same, so nothing,
228 not even a dot (as requested), is printed in the output (at least
229 on my Apple LaserWriter).
230 So I (MJ) singled out this case.
231 */
232 double degStAng = convDeg(startAngle),
233 degEnAng = convDeg(endAngle);
234
235 cmdout(NEWPATH); /* save current point */
236
237 if (degStAng != degEnAng) { /* normal case */
238 doubleout(hconvPS(xc));
239 doubleout(vconvPS(yc));
240 doubleout(convPS(xrad));
241 if (xrad != yrad || VDPI != DPI)
242 doubleout(convVPS(yrad));
243 doubleout(degStAng);
244 doubleout(degEnAng);
245
246 if (xrad == yrad && VDPI == DPI) /* for arcs and circles */
247 cmdout("arc");
248 else
249 cmdout("ellipse");
250 }
251 else { /* draw a single dot */
252 double xdot, ydot;
253 xdot = ((double)xc + (double)xrad * cos ((startAngle + endAngle) / 2.0));
254 ydot = ((double)yc + (double)yrad * sin ((startAngle + endAngle) / 2.0));
255 doubleout(hconvPS(xdot));
256 doubleout(vconvPS(ydot));
257 cmdout(MOVETO);
258 /* +1 to make sure the dot is printed */
259 doubleout(hconvPS(xdot)+1);
260 doubleout(vconvPS(ydot)+1);
261 cmdout(LINETO);
262 }
263
264 cmdout(STROKE);
265 }
266 } /* end of arc */
267
268 /*
269 * In pic dashed lines are created in a manner which makes boths ends
270 * of a line solid.
271 * This means that the corner of a dotted box is a dot and the corner
272 * of a dashed box is solid.
273 * The number of inches/dash must be adjusted accordingly.
274 */
275 static void
276 flushDashedPath(int dotted, double inchesPerDash)
277 {
278 register int i;
279 int nipd = (integer) convPS((int) inchesPerDash);
280
281 if (nipd == 0)
282 nipd = 1;
283 if (pathLen < 2)
284 {
285 error("Path less than 2 points - ignored");
286 pathLen = 0;
287 return;
288 }
289
290 cmdout(NEWPATH); /* to save the current point */
291 for (i=2; i <= pathLen; i++) {
292 integer dx = hconvPS(xx[i-1]) - hconvPS(xx[i]);
293 integer dy = vconvPS(yy[i-1]) - vconvPS(yy[i]);
294 /* Cast to double before multiplying; otherwise, can result in
295 integer overflow and thus a negative result (eepic-nan.test). */
296 double delta = sqrt(((double) dx) * dx + ((double) dy) * dy);
297 double ipd;
298 int ndashes;
299
300 /*
301 * If a line is dashed there must be an odd number of dashes.
302 * This makes the first and last dash of the line solid.
303 * If a line is dotted the length of a dot + space sequence must be
304 * chosen such that (n+1) * dotlength + n space length = linelength
305 * => (dot + space) = (line - dot) / n
306 */
307 if (dotted)
308 delta -= penSize;
309 ndashes = (integer) (delta / nipd + 0.5); /* number of dashes on line */
310 /* only odd when dashed, choose odd number with smallest deviation */
311 if (ndashes > 2 && (ndashes & 0x1) == 0 && !dotted) {
312 if (fabs(nipd - delta/(ndashes - 1)) <
313 (fabs(nipd - delta/(ndashes + 1))))
314 ndashes -= 1;
315 else
316 ndashes += 1;
317 } else if (ndashes < 1)
318 ndashes = 1;
319 ipd = delta / ndashes;
320 if (ipd <= 0.0)
321 ipd = 1.0;
322 cmdout("[");
323 if (dotted) {
324 doubleout(penSize);
325 doubleout(fabs(ipd - penSize));
326 } else /* if dashed */
327 doubleout(ipd);
328
329 cmdout("]");
330 doubleout(0);
331 cmdout("setdash");
332 doubleout(hconvPS(xx[i-1]));
333 doubleout(vconvPS(yy[i-1]));
334 cmdout(MOVETO);
335 doubleout(hconvPS(xx[i]));
336 doubleout(vconvPS(yy[i]));
337 cmdout(LINETO);
338 cmdout(STROKE);
339 }
340 cmdout("[] 0 setdash");
341 pathLen = 0;
342 }
343
344 void
345 flushPath(int invis)
346 {
347 register int i;
348
349 if (pathLen < 2)
350 {
351 error("Path less than 2 points - ignored");
352 pathLen = 0;
353 return;
354 }
355
356 /* To have filled boxes also have borders, we duplicate the object
357 specification; first time we emit a FILL command, second time we emit a
358 STROKE command.
359 There certainly exists a better way to do that in PostScript, by
360 remembering the object specification (path) across the fill command.
361 However I (MJ) am completely unproficient at PostScript...
362 */
363
364 #ifdef DEBUG
365 if (dd(D_SPECIAL))
366 fprintf(stderr,
367 #ifdef SHORTINT
368 "flushpath(1): hh=%ld, vv=%ld, x=%lg, y=%lg, xPS=%lg, yPS=%lg\n",
369 #else /* ~SHORTINT */
370 "flushpath(1): hh=%d, vv=%d, x=%lg, y=%lg, xPS=%lg, yPS=%lg\n",
371 #endif /* ~SHORTINT */
372 hh, vv, xx[1], yy[1], hconvPS(xx[1]), vconvPS(yy[1]));
373 #endif /* DEBUG */
374 if (shading) {
375 /* first time for shading */
376 cmdout(NEWPATH); /* to save the current point */
377 doubleout(hconvPS(xx[1]));
378 doubleout(vconvPS(yy[1]));
379 cmdout(MOVETO);
380 for (i=2; i < pathLen; i++) {
381 #ifdef DEBUG
382 if (dd(D_SPECIAL))
383 fprintf(stderr,
384 #ifdef SHORTINT
385 "flushpath(%ld): hh=%ld, vv=%ld, x=%lg, y=%lg, xPS=%lg, yPS=%lg\n",
386 #else /* ~SHORTINT */
387 "flushpath(%d): hh=%d, vv=%d, x=%lg, y=%lg, xPS=%lg, yPS=%lg\n",
388 #endif /* ~SHORTINT */
389 i, hh, vv, xx[i], yy[i], hconvPS(xx[i]), vconvPS(yy[i]));
390 #endif /* DEBUG */
391 doubleout(hconvPS(xx[i]));
392 doubleout(vconvPS(yy[i]));
393 cmdout(LINETO);
394 }
395 /* Shading should always apply to a closed path
396 force it and complain if necessary */
397 if (xx[1] == xx[pathLen] && yy[1] == yy[pathLen])
398 cmdout(CLOSEPATH);
399 else {
400 doubleout(hconvPS(xx[pathLen]));
401 doubleout(vconvPS(yy[pathLen]));
402 cmdout(LINETO);
403 cmdout(CLOSEPATH);
404 error("Attempt to fill a non-closed path");
405 fprintf(stderr,
406 #ifdef SHORTINT
407 "\tfirst point: x=%lg, y=%lg; last point: x=%lg, y=%lg\n",
408 #else /* ~SHORTINT */
409 "\tfirst point: x=%lg, y=%lg; last point: x=%lg, y=%lg\n",
410 #endif /* ~SHORTINT */
411 xx[1], yy[1], xx[pathLen], yy[pathLen]);
412
413 }
414 cmdout(FILL);
415 shading = NONE;
416 cmdout("0 setgray"); /* default of black */
417 }
418
419 if (!invis) {
420 cmdout(NEWPATH); /* to save the current point */
421 doubleout(hconvPS(xx[1]));
422 doubleout(vconvPS(yy[1]));
423 cmdout(MOVETO);
424 for (i=2; i < pathLen; i++) {
425 doubleout(hconvPS(xx[i]));
426 doubleout(vconvPS(yy[i]));
427 cmdout(LINETO);
428 }
429 if (xx[1] == xx[pathLen] && yy[1] == yy[pathLen])
430 cmdout(CLOSEPATH);
431 else {
432 doubleout(hconvPS(xx[pathLen]));
433 doubleout(vconvPS(yy[pathLen]));
434 cmdout(LINETO);
435 }
436 cmdout(STROKE);
437 }
438
439 pathLen = 0;
440 } /* end of flushPath */
441
442 void
443 flushDashed(char *cp, int dotted)
444 {
445 double inchesPerDash;
446 int savelen = pathLen;
447
448 if (sscanf(cp, " %lg ", &inchesPerDash) != 1)
449 {
450 error ("Illegal format for dotted/dashed line");
451 return;
452 }
453
454 if (inchesPerDash <= 0.0)
455 {
456 error ("Length of dash/dot cannot be negative");
457 return;
458 }
459
460 inchesPerDash = 1000 * inchesPerDash; /* to get milli-inches */
461 flushPath(1);/* print filled, always invisible bounds */
462 pathLen = savelen;
463 flushDashedPath(dotted,inchesPerDash);
464
465 cmdout("[] 0 setdash");
466 } /* end of flushDashed */
467
468 void
469 flushSpline(char *cp)
470 { /* as exact as psdit!!! */
471 register long i;
472 register double px, py, dx, dy;
473
474 if (*cp) {
475 double inchesPerDash;
476 int ipd;
477
478 if (sscanf(cp, "%lg ", &inchesPerDash) != 1) {
479 error ("Illegal format for dotted/dashed spline");
480 return;
481 }
482
483 ipd = (int)(1000.0 * inchesPerDash); /* to get milli-inches */
484
485 if (ipd != 0) {
486 cmdout("[");
487 if (inchesPerDash < 0.0) /* dotted */ {
488 doubleout(penSize);
489 doubleout(fabs(convPS((int)-ipd) - penSize));
490 } else /* dashed */
491 doubleout(convPS((int)ipd));
492
493 cmdout("]");
494 doubleout(0);
495 cmdout("setdash");
496 }
497 }
498
499 if (pathLen < 2)
500 {
501 error("Spline less than two points - ignored");
502 return;
503 }
504
505 cmdout(NEWPATH); /* to save the current point */
506 doubleout(hconvPS(xx[1]));
507 doubleout(vconvPS(yy[1]));
508 cmdout(MOVETO);
509 px = convPS(xx[1]);
510 py = convVPS(yy[1]);
511 doubleout(dx = convPS((xx[2]+xx[1])/2) - px);
512 doubleout(dy = convVPS((yy[2]+yy[1])/2) - py);
513 cmdout(RLINETO);
514
515 for (i=2; i < pathLen; i++)
516 {
517 px += dx;
518 py += dy;
519 doubleout(convPS((xx[i-1]+5*xx[i])/6) - px);
520 doubleout(convVPS((yy[i-1]+5*yy[i])/6) - py);
521 doubleout(convPS((5*xx[i]+xx[i+1])/6) - px);
522 doubleout(convVPS((5*yy[i]+yy[i+1])/6) - py);
523 doubleout(dx = convPS((xx[i]+xx[i+1])/2) - px);
524 doubleout(dy = convVPS((yy[i]+yy[i+1])/2) - py);
525 cmdout(RCURVETO);
526 }
527
528 doubleout(hconvPS(xx[pathLen]));
529 doubleout(vconvPS(yy[pathLen]));
530 cmdout(LINETO);
531
532 /* doShading(); */
533 cmdout(STROKE); /* assume no shaded splines */
534 pathLen = 0;
535
536 if (*cp)
537 cmdout("[] 0 setdash");
538
539 } /* end of flushSpline */
540
541 /* set shading level (used with Fig 1.4-TFX). [email protected], 89/04 */
542 /* A better trial at setting shading level -- [email protected] */
543 /* Count number of black bits in the pattern together with total number, */
544 /* compute the average and use this as the PostScript gray level */
545 void
546 SetShade(register char *cp)
547 {
548 int blackbits = 0, totalbits = 0;
549
550 while (*cp) {
551 switch (*cp) {
552 case '0':
553 totalbits += 4;
554 break;
555 case '1':
556 case '2':
557 case '4':
558 case '8':
559 blackbits += 1;
560 totalbits += 4;
561 break;
562 case '3':
563 case '5':
564 case '6':
565 case '9':
566 case 'a':
567 case 'A':
568 case 'c':
569 case 'C':
570 blackbits += 2;
571 totalbits += 4;
572 break;
573 case '7':
574 case 'b':
575 case 'B':
576 case 'd':
577 case 'D':
578 case 'e':
579 case 'E':
580 blackbits += 3;
581 totalbits += 4;
582 break;
583 case 'f':
584 case 'F':
585 blackbits += 4;
586 totalbits += 4;
587 break;
588 case ' ':
589 break;
590 default:
591 error("Invalid character in .tx pattern");
592 break;
593 }
594 cp++;
595 }
596 shadetp = 1.0 - ((double) blackbits / (double) totalbits);
597 shading = GRAY;
598 } /* end of SetShade */
599
600 void
601 shadeLast(char *cp)
602 {
603 char tpout[100];
604
605 if (*cp) {
606 double tempShadetp;
607
608 if (sscanf(cp, "%lg ", &tempShadetp) != 1)
609 error ("Illegal format for shade level");
610 else if (tempShadetp < 0.0 || tempShadetp > 1.0)
611 error ("Invalid shade level");
612 else
613 /* if "sh" has an argument we can safely assume that tpic 2.0 is used
614 so that all subsequent "sh" commands will come with an explicit
615 argument. Hence we may overwrite shadetp's old value */
616 /* Also note the inversion of gray levels for tpic 2.0 (0 = white,
617 1 = black) w.r.t. PostScript (0 = black, 1 = white) */
618 shadetp = 1.0 - tempShadetp;
619 }
620
621 shading = GRAY;
622 snprintf(tpout, sizeof(tpout), "%1.3f setgray", shadetp);
623 /* [email protected], MJ */
624 cmdout(tpout);
625 } /* end of shadeLast */
626
627 void
628 whitenLast(void)
629 {
630 shading = WHITE;
631 cmdout("1 setgray");
632 } /* end of whitenLast */
633
634 void
635 blackenLast(void)
636 {
637 shading = BLACK;
638 cmdout("0 setgray"); /* actually this aint needed */
639 } /* end of whitenLast */
640
641 #if 0
642 /* Not used */
643 static void
644 doShading(void)
645 {
646 if (shading)
647 {
648 cmdout(FILL);
649 shading = NONE;
650 cmdout("0 setgray"); !* default of black *!
651 }
652 else
653 cmdout(STROKE);
654 } !* end of doShading *!
655 #endif
656
657 /*
658 * We need to calculate (x * convDPI * mag) / (tpicResolution * 1000)
659 * So we use doubleing point. (This should have a very small impact
660 * on the speed of the program.)
661 */
662 static double
663 zPixRound(register double x, /* in DVI units */
664 register double convDPI /* dots per inch */
665 ) /* return rounded number of pixels */
666 {
667 return ((x * mag * (double)convDPI /
668 (1000.0 * tpicRESOLUTION)));
669 }
670
671 #endif /* TPIC */