xref: /plan9/sys/src/cmd/pic/textgen.c (revision 7dd7cddf99dd7472612f1413b4da293630e6b1bc)
1 #include	<stdio.h>
2 #include	"pic.h"
3 #include	"y.tab.h"
4 
textgen(void)5 obj *textgen(void)
6 {
7 	int i, sub, nstr, at, with, hset, invis;
8 	double xwith, ywith, h, w, x0, y0, x1, y1;
9 	obj *p, *ppos;
10 	static double prevh = 0;
11 	static double prevw = 0;
12 	Attr *ap;
13 
14 	at = with = nstr = hset = invis = 0;
15 	h = getfval("textht");
16 	w = getfval("textwid");
17 	for (i = 0; i < nattr; i++) {
18 		ap = &attr[i];
19 		switch (ap->a_type) {
20 		case HEIGHT:
21 			h = ap->a_val.f;
22 			hset++;
23 			break;
24 		case WIDTH:
25 			w = ap->a_val.f;
26 			break;
27 		case WITH:
28 			with = ap->a_val.i;
29 			break;
30 		case INVIS:
31 			invis = INVIS;
32 			break;
33 		case AT:
34 			ppos = ap->a_val.o;
35 			curx = ppos->o_x;
36 			cury = ppos->o_y;
37 			at++;
38 			break;
39 		case TEXTATTR:
40 			sub = ap->a_sub;
41 			if (ap->a_val.p == NULL)	/* an isolated modifier */
42 				text[ntext-1].t_type = sub;
43 			else {
44 				savetext(sub, ap->a_val.p);
45 				nstr++;
46 			}
47 			break;
48 		}
49 	}
50 	if (hset == 0)		/* no explicit ht cmd */
51 		h *= nstr;
52 	if (with) {
53 		xwith = ywith = 0.0;
54 		switch (with) {
55 		case NORTH:	ywith = -h / 2; break;
56 		case SOUTH:	ywith = h / 2; break;
57 		case EAST:	xwith = -w / 2; break;
58 		case WEST:	xwith = w / 2; break;
59 		case NE:	xwith = -w / 2; ywith = -h / 2; break;
60 		case SE:	xwith = -w / 2; ywith = h / 2; break;
61 		case NW:	xwith = w / 2; ywith = -h / 2; break;
62 		case SW:	xwith = w / 2; ywith = h / 2; break;
63 		}
64 		curx += xwith;
65 		cury += ywith;
66 	}
67 	if (!at) {
68 		if (isright(hvmode))
69 			curx += w / 2;
70 		else if (isleft(hvmode))
71 			curx -= w / 2;
72 		else if (isup(hvmode))
73 			cury += h / 2;
74 		else
75 			cury -= h / 2;
76 	}
77 	x0 = curx - w / 2;
78 	y0 = cury - h / 2;
79 	x1 = curx + w / 2;
80 	y1 = cury + h / 2;
81 	extreme(x0, y0);
82 	extreme(x1, y1);
83 	dprintf("Text h %g w %g at %g,%g\n", h, w, curx, cury);
84 	p = makenode(TEXT, 2);
85 	p->o_attr = invis;
86 	p->o_val[0] = w;
87 	p->o_val[1] = h;
88 	if (isright(hvmode))
89 		curx = x1;
90 	else if (isleft(hvmode))
91 		curx = x0;
92 	else if (isup(hvmode))
93 		cury = y1;
94 	else
95 		cury = y0;
96 	prevh = h;
97 	prevw = w;
98 	return(p);
99 }
100 
troffgen(char * s)101 obj *troffgen(char *s)	/* save away a string of troff commands */
102 {
103 	savetext(CENTER, s);	/* use the existing text mechanism */
104 	return makenode(TROFF, 0);
105 }
106 
savetext(int t,char * s)107 void savetext(int t, char *s)	/* record text elements for current object */
108 {
109 	if (ntext >= ntextlist)
110 		text = (Text *) grow((char *) text, "text", ntextlist += 200, sizeof(Text));
111 	text[ntext].t_type = t;
112 	text[ntext].t_val = s;
113 	dprintf("saving %d text %s at %d\n", t, s, ntext);
114 	ntext++;
115 }
116