refer: read the input even if no bib file is given
[troff.git] / pic / print.c
blobad21e0d70ed99b3df401e6156a36c425061934f7
1 #include <stdio.h>
2 #include <math.h>
3 #include "pic.h"
4 #include "y.tab.h"
6 void dotext(obj *);
7 void dotline(double, double, double, double, int, double);
8 void dotbox(double, double, double, double, int, double);
9 void ellipse(double, double, double, double);
10 void circle(double, double, double);
11 void arc(double, double, double, double, double, double);
12 void arrow(double, double, double, double, double, double, double, int);
13 void line(double, double, double, double);
14 void box(double, double, double, double);
15 void spline(double x, double y, double n, ofloat *p, int dashed, double ddval);
16 void move(double, double);
17 void troff(char *);
18 void dot(void);
19 void fillstart(double), fillend(int vis, int noedge);
21 void print(void)
23 obj *p;
24 int i, j, k, m;
25 int fill, vis, invis;
26 double x0, y0, x1, y1, ox, oy, dx, dy, ndx, ndy;
28 for (i = 0; i < nobj; i++) {
29 p = objlist[i];
30 ox = p->o_x;
31 oy = p->o_y;
32 if (p->o_count >= 1)
33 x1 = p->o_val[0];
34 if (p->o_count >= 2)
35 y1 = p->o_val[1];
36 m = p->o_mode;
37 fill = p->o_attr & FILLBIT;
38 invis = p->o_attr & INVIS;
39 vis = !invis;
40 switch (p->o_type) {
41 case TROFF:
42 troff(text[p->o_nt1].t_val);
43 break;
44 case BOX:
45 case BLOCK:
46 x0 = ox - x1 / 2;
47 y0 = oy - y1 / 2;
48 x1 = ox + x1 / 2;
49 y1 = oy + y1 / 2;
50 if (fill) {
51 move(x0, y0);
52 fillstart(p->o_fillval);
54 if (p->o_type == BLOCK)
55 ; /* nothing at all */
56 else if (invis && !fill)
57 ; /* nothing at all */
58 else if (p->o_attr & (DOTBIT|DASHBIT))
59 dotbox(x0, y0, x1, y1, p->o_attr, p->o_ddval);
60 else
61 box(x0, y0, x1, y1);
62 if (fill)
63 fillend(vis, fill);
64 move(ox, oy);
65 dotext(p); /* if there are any text strings */
66 if (ishor(m))
67 move(isright(m) ? x1 : x0, oy); /* right side */
68 else
69 move(ox, isdown(m) ? y0 : y1); /* bottom */
70 break;
71 case BLOCKEND:
72 break;
73 case CIRCLE:
74 if (fill)
75 fillstart(p->o_fillval);
76 if (vis || fill)
77 circle(ox, oy, x1);
78 if (fill)
79 fillend(vis, fill);
80 move(ox, oy);
81 dotext(p);
82 if (ishor(m))
83 move(ox + isright(m) ? x1 : -x1, oy);
84 else
85 move(ox, oy + isup(m) ? x1 : -x1);
86 break;
87 case ELLIPSE:
88 if (fill)
89 fillstart(p->o_fillval);
90 if (vis || fill)
91 ellipse(ox, oy, x1, y1);
92 if (fill)
93 fillend(vis, fill);
94 move(ox, oy);
95 dotext(p);
96 if (ishor(m))
97 move(ox + isright(m) ? x1 : -x1, oy);
98 else
99 move(ox, oy - isdown(m) ? y1 : -y1);
100 break;
101 case ARC:
102 if (fill) {
103 move(ox, oy);
104 fillstart(p->o_fillval);
106 if (p->o_attr & HEAD1)
107 arrow(x1 - (y1 - oy), y1 + (x1 - ox),
108 x1, y1, p->o_val[4], p->o_val[5], p->o_val[5]/p->o_val[6]/2, p->o_nhead);
109 if (invis && !fill)
110 /* probably wrong when it's cw */
111 move(x1, y1);
112 else
113 arc(ox, oy, x1, y1, p->o_val[2], p->o_val[3]);
114 if (p->o_attr & HEAD2)
115 arrow(p->o_val[2] + p->o_val[3] - oy, p->o_val[3] - (p->o_val[2] - ox),
116 p->o_val[2], p->o_val[3], p->o_val[4], p->o_val[5], -p->o_val[5]/p->o_val[6]/2, p->o_nhead);
117 if (fill)
118 fillend(vis, fill);
119 if (p->o_attr & CW_ARC)
120 move(x1, y1); /* because drawn backwards */
121 move(ox, oy);
122 dotext(p);
123 break;
124 case LINE:
125 case ARROW:
126 case SPLINE:
127 if (fill) {
128 move(ox, oy);
129 fillstart(p->o_fillval);
131 if (vis && p->o_attr & HEAD1)
132 arrow(ox + p->o_val[5], oy + p->o_val[6], ox, oy, p->o_val[2], p->o_val[3], 0.0, p->o_nhead);
133 if (invis && !fill)
134 move(x1, y1);
135 else if (p->o_type == SPLINE)
136 spline(ox, oy, p->o_val[4], &p->o_val[5], p->o_attr & (DOTBIT|DASHBIT), p->o_ddval);
137 else {
138 dx = ox;
139 dy = oy;
140 for (k=0, j=5; k < p->o_val[4]; k++, j += 2) {
141 ndx = dx + p->o_val[j];
142 ndy = dy + p->o_val[j+1];
143 if (p->o_attr & (DOTBIT|DASHBIT))
144 dotline(dx, dy, ndx, ndy, p->o_attr, p->o_ddval);
145 else
146 line(dx, dy, ndx, ndy);
147 dx = ndx;
148 dy = ndy;
151 if (vis && p->o_attr & HEAD2) {
152 dx = ox;
153 dy = oy;
154 for (k = 0, j = 5; k < p->o_val[4] - 1; k++, j += 2) {
155 dx += p->o_val[j];
156 dy += p->o_val[j+1];
158 arrow(dx, dy, x1, y1, p->o_val[2], p->o_val[3], 0.0, p->o_nhead);
160 if (fill)
161 fillend(vis, fill);
162 move((ox + x1)/2, (oy + y1)/2); /* center */
163 dotext(p);
164 break;
165 case MOVE:
166 move(ox, oy);
167 break;
168 case TEXT:
169 move(ox, oy);
170 if (vis)
171 dotext(p);
172 break;
177 void dotline(double x0, double y0, double x1, double y1, int ddtype, double ddval) /* dotted line */
179 static double prevval = 0.05; /* 20 per inch by default */
180 int i, numdots;
181 double a, b, dx, dy;
183 if (ddval == 0)
184 ddval = prevval;
185 prevval = ddval;
186 /* don't save dot/dash value */
187 dx = x1 - x0;
188 dy = y1 - y0;
189 if (ddtype & DOTBIT) {
190 numdots = sqrt(dx*dx + dy*dy) / prevval + 0.5;
191 if (numdots > 0)
192 for (i = 0; i <= numdots; i++) {
193 a = (double) i / (double) numdots;
194 move(x0 + (a * dx), y0 + (a * dy));
195 dot();
197 } else if (ddtype & DASHBIT) {
198 double d, dashsize, spacesize;
199 d = sqrt(dx*dx + dy*dy);
200 if (d <= 2 * prevval) {
201 line(x0, y0, x1, y1);
202 return;
204 numdots = d / (2 * prevval) + 1; /* ceiling */
205 dashsize = prevval;
206 spacesize = (d - numdots * dashsize) / (numdots - 1);
207 for (i = 0; i < numdots-1; i++) {
208 a = i * (dashsize + spacesize) / d;
209 b = a + dashsize / d;
210 line(x0 + (a*dx), y0 + (a*dy), x0 + (b*dx), y0 + (b*dy));
211 a = b;
212 b = a + spacesize / d;
213 move(x0 + (a*dx), y0 + (a*dy));
215 line(x0 + (b * dx), y0 + (b * dy), x1, y1);
217 prevval = 0.05;
220 void dotbox(double x0, double y0, double x1, double y1, int ddtype, double ddval) /* dotted or dashed box */
222 dotline(x0, y0, x1, y0, ddtype, ddval);
223 dotline(x1, y0, x1, y1, ddtype, ddval);
224 dotline(x1, y1, x0, y1, ddtype, ddval);
225 dotline(x0, y1, x0, y0, ddtype, ddval);
228 void dotext(obj *p) /* print text strings of p in proper vertical spacing */
230 int i, nhalf;
231 void label(char *, int, int);
233 nhalf = p->o_nt2 - p->o_nt1 - 1;
234 for (i = p->o_nt1; i < p->o_nt2; i++) {
235 label(text[i].t_val, text[i].t_type, nhalf);
236 nhalf -= 2;