123956Sjaap #ifndef lint
2*24092Sjaap static char sccsid[] = "@(#)textgen.c	3.1 (CWI) 85/07/30";
323956Sjaap #endif lint
4*24092Sjaap 
523956Sjaap #include	<stdio.h>
623956Sjaap #include	"pic.h"
723956Sjaap #include	"y.tab.h"
823956Sjaap 
textgen()924020Sjaap obj *textgen()
1023956Sjaap {
1124020Sjaap 	int i, type, sub, nstr, at, with, hset;
1224020Sjaap 	float xwith, ywith, h, w, x0, y0, x1, y1;
1324020Sjaap 	obj *p, *ppos;
1424020Sjaap 	static float prevh = 0;
1524020Sjaap 	static float prevw = 0;
1624020Sjaap 	Attr *ap;
1723956Sjaap 
1824020Sjaap 	sub = CENTER;
1924020Sjaap 	at = with = nstr = hset = 0;
2024020Sjaap 	h = getfval("textht");
2124020Sjaap 	w = getfval("textwid");
2224020Sjaap 	for (i = 0; i < nattr; i++) {
2324020Sjaap 		ap = &attr[i];
2424020Sjaap 		switch (ap->a_type) {
2524020Sjaap 		case HEIGHT:
2624020Sjaap 			h = ap->a_val.f;
2724020Sjaap 			hset++;
2823956Sjaap 			break;
2924020Sjaap 		case WIDTH:
3024020Sjaap 			w = ap->a_val.f;
3123956Sjaap 			break;
3224020Sjaap 		case WITH:
3324020Sjaap 			with = ap->a_val.i;
3423956Sjaap 			break;
3523956Sjaap 		case AT:
3624020Sjaap 			ppos = ap->a_val.o;
3723956Sjaap 			curx = ppos->o_x;
3823956Sjaap 			cury = ppos->o_y;
3924020Sjaap 			at++;
4023956Sjaap 			break;
4124020Sjaap 		case TEXTATTR:
4224020Sjaap 			sub = ap->a_sub;
4324020Sjaap 			if (ap->a_val.p == NULL)	/* an isolated modifier */
4424020Sjaap 				text[ntext-1].t_type = sub;
4524020Sjaap 			else {
4624020Sjaap 				savetext(sub, ap->a_val.p);
4724020Sjaap 				nstr++;
4824020Sjaap 			}
4923956Sjaap 			break;
5023956Sjaap 		}
5124020Sjaap 	}
5224020Sjaap 	if (hset == 0)		/* no explicit ht cmd */
5324020Sjaap 		h *= nstr;
5424020Sjaap 	if (with) {
5524020Sjaap 		xwith = ywith = 0.0;
5624020Sjaap 		switch (with) {
5724020Sjaap 		case NORTH:	ywith = -h / 2; break;
5824020Sjaap 		case SOUTH:	ywith = h / 2; break;
5924020Sjaap 		case EAST:	xwith = -w / 2; break;
6024020Sjaap 		case WEST:	xwith = w / 2; break;
6124020Sjaap 		case NE:	xwith = -w / 2; ywith = -h / 2; break;
6224020Sjaap 		case SE:	xwith = -w / 2; ywith = h / 2; break;
6324020Sjaap 		case NW:	xwith = w / 2; ywith = -h / 2; break;
6424020Sjaap 		case SW:	xwith = w / 2; ywith = h / 2; break;
6524020Sjaap 		}
6624020Sjaap 		curx += xwith;
6724020Sjaap 		cury += ywith;
6824020Sjaap 	}
6924020Sjaap 	if (!at) {
7024020Sjaap 		if (isright(hvmode))
7124020Sjaap 			curx += w / 2;
7224020Sjaap 		else if (isleft(hvmode))
7324020Sjaap 			curx -= w / 2;
7424020Sjaap 		else if (isup(hvmode))
7524020Sjaap 			cury += h / 2;
7624020Sjaap 		else
7724020Sjaap 			cury -= h / 2;
7824020Sjaap 	}
7924020Sjaap 	x0 = curx - w / 2;
8024020Sjaap 	y0 = cury - h / 2;
8124020Sjaap 	x1 = curx + w / 2;
8224020Sjaap 	y1 = cury + h / 2;
8324020Sjaap 	extreme(x0, y0);
8424020Sjaap 	extreme(x1, y1);
8524020Sjaap 	dprintf("Text h %g w %g at %g,%g\n", h, w, curx, cury);
8623956Sjaap 	p = makenode(TEXT, 2);
8724020Sjaap 	p->o_val[0] = w;
8824020Sjaap 	p->o_val[1] = h;
8924020Sjaap 	if (isright(hvmode))
9024020Sjaap 		curx = x1;
9124020Sjaap 	else if (isleft(hvmode))
9224020Sjaap 		curx = x0;
9324020Sjaap 	else if (isup(hvmode))
9424020Sjaap 		cury = y1;
9524020Sjaap 	else
9624020Sjaap 		cury = y0;
9724020Sjaap 	prevh = h;
9824020Sjaap 	prevw = w;
9923956Sjaap 	return(p);
10023956Sjaap }
10124020Sjaap 
troffgen(s)10224020Sjaap obj *troffgen(s)	/* save away a string of troff commands */
10324020Sjaap 	YYSTYPE s;
10424020Sjaap {
10524020Sjaap 	savetext(CENTER, s.p);	/* use the existing text mechanism */
10624020Sjaap 	return makenode(TROFF, 0);
10724020Sjaap }
10824020Sjaap 
savetext(t,s)10924020Sjaap savetext(t, s)	/* record text elements for current object */
11024020Sjaap 	int t;
11124020Sjaap 	char *s;
11224020Sjaap {
11324020Sjaap 	if (ntext >= ntextlist)
11424020Sjaap 		text = (Text *) grow(text, "text", ntextlist += 200, sizeof(Text));
11524020Sjaap 	text[ntext].t_type = t;
11624020Sjaap 	text[ntext].t_val = s;
11724020Sjaap 	dprintf("saving %d text %s at %d\n", t, s, ntext);
11824020Sjaap 	ntext++;
11924020Sjaap }
120