Debian Codegraph @

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