1 /*
2  * Changes by Gunnar Ritter, Freiburg i. Br., Germany, October 2005.
3  *
4  * Derived from Plan 9 v4 /sys/src/cmd/pic/
5  *
6  * Copyright (C) 2003, Lucent Technologies Inc. and others.
7  * All Rights Reserved.
8  *
9  * Distributed under the terms of the Lucent Public License Version 1.02.
10  */
11 
12 /*	Sccsid @(#)blockgen.c	1.3 (gritter) 10/18/05	*/
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include "pic.h"
16 #include "y.tab.h"
17 
18 #define	NBRACK	20	/* depth of [...] */
19 #define	NBRACE	20	/* depth of {...} */
20 
21 struct pushstack stack[NBRACK];
22 int	nstack	= 0;
23 struct pushstack bracestack[NBRACE];
24 int	nbstack	= 0;
25 
26 void blockadj(obj *);
27 
leftthing(int c)28 obj *leftthing(int c)	/* called for {... or [... */
29 			/* really ought to be separate functions */
30 {
31 	obj *p;
32 
33 	if (c == '[') {
34 		if (nstack >= NBRACK)
35 			FATAL("[...] nested too deep");
36 		stack[nstack].p_x = curx;
37 		stack[nstack].p_y = cury;
38 		stack[nstack].p_hvmode = hvmode;
39 		curx = cury = 0;
40 		stack[nstack].p_xmin = xmin;
41 		stack[nstack].p_xmax = xmax;
42 		stack[nstack].p_ymin = ymin;
43 		stack[nstack].p_ymax = ymax;
44 		nstack++;
45 		xmin = ymin = 30000;
46 		xmax = ymax = -30000;
47 		p = makenode(BLOCK, 7);
48 		p->o_val[4] = nobj;	/* 1st item within [...] */
49 		if (p->o_nobj != nobj-1)
50 			fprintf(stderr, "nobjs wrong%d %d\n", p->o_nobj, nobj);
51 	} else {
52 		if (nbstack >= NBRACK)
53 			FATAL("{...} nested too deep");
54 		bracestack[nbstack].p_x = curx;
55 		bracestack[nbstack].p_y = cury;
56 		bracestack[nbstack].p_hvmode = hvmode;
57 		nbstack++;
58 		p = NULL;
59 	}
60 	return(p);
61 }
62 
rightthing(obj * p,int c)63 obj *rightthing(obj *p, int c)	/* called for ... ] or ... } */
64 {
65 	obj *q;
66 
67 	if (c == '}') {
68 		nbstack--;
69 		curx = bracestack[nbstack].p_x;
70 		cury = bracestack[nbstack].p_y;
71 		hvmode = bracestack[nbstack].p_hvmode;
72 		q = makenode(MOVE, 0);
73 		dprintf("M %g %g\n", curx, cury);
74 	} else {
75 		nstack--;
76 		curx = stack[nstack].p_x;
77 		cury = stack[nstack].p_y;
78 		hvmode = stack[nstack].p_hvmode;
79 		q = makenode(BLOCKEND, 7);
80 		q->o_val[4] = p->o_nobj + 1;	/* back pointer */
81 		p->o_val[5] = q->o_nobj - 1;	/* forward pointer */
82 		if (xmin > xmax)	/* nothing happened */
83 			xmin = xmax;
84 		if (ymin > ymax)
85 			ymin = ymax;
86 		p->o_val[0] = xmin; p->o_val[1] = ymin;
87 		p->o_val[2] = xmax; p->o_val[3] = ymax;
88 		p->o_symtab = q->o_symtab = stack[nstack+1].p_symtab;
89 		xmin = stack[nstack].p_xmin;
90 		ymin = stack[nstack].p_ymin;
91 		xmax = stack[nstack].p_xmax;
92 		ymax = stack[nstack].p_ymax;
93 	}
94 	return(q);
95 }
96 
blockgen(obj * p,obj * q)97 obj *blockgen(obj *p, obj *q)	/* handles [...] */
98 {
99 	int i, invis, at, with;
100 	double ddval, h, w, xwith, ywith;
101 	double x0, y0, x1, y1, cx, cy;
102 	obj *ppos;
103 	Attr *ap;
104 
105 	invis = at = 0;
106 	with = xwith = ywith = 0;
107 	ddval = 0;
108 	w = p->o_val[2] - p->o_val[0];
109 	h = p->o_val[3] - p->o_val[1];
110 	cx = (p->o_val[2] + p->o_val[0]) / 2;	/* geom ctr of [] wrt local orogin */
111 	cy = (p->o_val[3] + p->o_val[1]) / 2;
112 	dprintf("cx,cy=%g,%g\n", cx, cy);
113 	for (i = 0; i < nattr; i++) {
114 		ap = &attr[i];
115 		switch (ap->a_type) {
116 		case HEIGHT:
117 			h = ap->a_val.f;
118 			break;
119 		case WIDTH:
120 			w = ap->a_val.f;
121 			break;
122 		case WITH:
123 			with = ap->a_val.i;	/* corner */
124 			break;
125 		case PLACE:	/* actually with position ... */
126 			ppos = ap->a_val.o;
127 			xwith = cx - ppos->o_x;
128 			ywith = cy - ppos->o_y;
129 			with = PLACE;
130 			break;
131 		case AT:
132 		case FROM:
133 			ppos = ap->a_val.o;
134 			curx = ppos->o_x;
135 			cury = ppos->o_y;
136 			at++;
137 			break;
138 		case INVIS:
139 			invis = INVIS;
140 			break;
141 		case TEXTATTR:
142 			savetext(ap->a_sub, ap->a_val.p);
143 			break;
144 		}
145 	}
146 	if (with) {
147 		switch (with) {
148 		case NORTH:	ywith = -h / 2; break;
149 		case SOUTH:	ywith = h / 2; break;
150 		case EAST:	xwith = -w / 2; break;
151 		case WEST:	xwith = w / 2; break;
152 		case NE:	xwith = -w / 2; ywith = -h / 2; break;
153 		case SE:	xwith = -w / 2; ywith = h / 2; break;
154 		case NW:	xwith = w / 2; ywith = -h / 2; break;
155 		case SW:	xwith = w / 2; ywith = h / 2; break;
156 		}
157 		curx += xwith;
158 		cury += ywith;
159 	}
160 	if (!at) {
161 		if (isright(hvmode))
162 			curx += w / 2;
163 		else if (isleft(hvmode))
164 			curx -= w / 2;
165 		else if (isup(hvmode))
166 			cury += h / 2;
167 		else
168 			cury -= h / 2;
169 	}
170 	x0 = curx - w / 2;
171 	y0 = cury - h / 2;
172 	x1 = curx + w / 2;
173 	y1 = cury + h / 2;
174 	extreme(x0, y0);
175 	extreme(x1, y1);
176 	p->o_x = curx;
177 	p->o_y = cury;
178 	p->o_nt1 = ntext1;
179 	p->o_nt2 = ntext;
180 	ntext1 = ntext;
181 	p->o_val[0] = w;
182 	p->o_val[1] = h;
183 	p->o_val[2] = cx;
184 	p->o_val[3] = cy;
185 	p->o_val[5] = q->o_nobj - 1;		/* last item in [...] */
186 	p->o_ddval = ddval;
187 	p->o_attr = invis;
188 	dprintf("[] %g %g %g %g at %g %g, h=%g, w=%g\n", x0, y0, x1, y1, curx, cury, h, w);
189 	if (isright(hvmode))
190 		curx = x1;
191 	else if (isleft(hvmode))
192 		curx = x0;
193 	else if (isup(hvmode))
194 		cury = y1;
195 	else
196 		cury = y0;
197 	for (i = 0; i <= 5; i++)
198 		q->o_val[i] = p->o_val[i];
199 	stack[nstack+1].p_symtab = NULL;	/* so won't be found again */
200 	blockadj(p);	/* fix up coords for enclosed blocks */
201 	return(p);
202 }
203 
blockadj(obj * p)204 void blockadj(obj *p)	/* adjust coords in block starting at p */
205 {
206 	double dx, dy;
207 	int n, lev;
208 
209 	dx = p->o_x - p->o_val[2];
210 	dy = p->o_y - p->o_val[3];
211 	n = p->o_nobj + 1;
212 	dprintf("into blockadj: dx,dy=%g,%g\n", dx, dy);
213 	for (lev = 1; lev > 0; n++) {
214 		p = objlist[n];
215 		if (p->o_type == BLOCK)
216 			lev++;
217 		else if (p->o_type == BLOCKEND)
218 			lev--;
219 		dprintf("blockadj: type=%d o_x,y=%g,%g;", p->o_type, p->o_x, p->o_y);
220 		p->o_x += dx;
221 		p->o_y += dy;
222 		dprintf(" becomes %g,%g\n", p->o_x, p->o_y);
223 		switch (p->o_type) {	/* other absolute coords */
224 		case LINE:
225 		case ARROW:
226 		case SPLINE:
227 			p->o_val[0] += dx;
228 			p->o_val[1] += dy;
229 			break;
230 		case ARC:
231 			p->o_val[0] += dx;
232 			p->o_val[1] += dy;
233 			p->o_val[2] += dx;
234 			p->o_val[3] += dy;
235 			break;
236 		}
237 	}
238 }
239