xref: /plan9-contrib/sys/src/cmd/tbl/t6.c (revision 219b2ee8daee37f4aad58d63f21287faa8e4ffdc)
1 /* t6.c: compute tab stops */
2 # define tx(a) (a>0 && a<128)
3 # include "t.h"
4 # define FN(i,c) font[c][stynum[i]]
5 # define SZ(i,c) csize[c][stynum[i]]
6 # define TMP1 S1
7 # define TMP2 S2
8 
9 void
10 maktab(void)			/* define the tab stops of the table */
11 {
12 	int	icol, ilin, tsep, k, ik, vforml, il, text;
13 	char	*s;
14 
15 	for (icol = 0; icol < ncol; icol++) {
16 		doubled[icol] = acase[icol] = 0;
17 		Bprint(&tabout, ".nr %2s 0\n", reg(icol, CRIGHT));
18 		for (text = 0; text < 2; text++) {
19 			if (text)
20 				Bprint(&tabout, ".%2s\n.rm %2s\n", reg(icol, CRIGHT),
21 				    reg(icol, CRIGHT));
22 			for (ilin = 0; ilin < nlin; ilin++) {
23 				if (instead[ilin] || fullbot[ilin])
24 					continue;
25 				vforml = ilin;
26 				for (il = prev(ilin); il >= 0 && vspen(table[il][icol].col); il = prev(il))
27 					vforml = il;
28 				if (fspan(vforml, icol))
29 					continue;
30 				if (filler(table[ilin][icol].col))
31 					continue;
32 				if ((flags[icol][stynum[ilin]] & ZEROW) != 0)
33 					continue;
34 				switch (ctype(vforml, icol)) {
35 				case 'a':
36 					acase[icol] = 1;
37 					s = table[ilin][icol].col;
38 					if ((int)s > 0 && (int)s < 128 && text) {
39 						if (doubled[icol] == 0)
40 							Bprint(&tabout, ".nr %d 0\n.nr %d 0\n",
41 							    S1, S2);
42 						doubled[icol] = 1;
43 						Bprint(&tabout, ".if \\n(%c->\\n(%d .nr %d \\n(%c-\n",
44 						    s, S2, S2, s);
45 					}
46 				case 'n':
47 					if (table[ilin][icol].rcol != 0) {
48 						if (doubled[icol] == 0 && text == 0)
49 							Bprint(&tabout, ".nr %d 0\n.nr %d 0\n",
50 							    S1, S2);
51 						doubled[icol] = 1;
52 						if (real(s = table[ilin][icol].col) && !vspen(s)) {
53 							if (tx((int)s) != text)
54 								continue;
55 							Bprint(&tabout, ".nr %d ", TMP);
56 							wide(s, FN(vforml, icol), SZ(vforml, icol));
57 							Bprint(&tabout, "\n");
58 							Bprint(&tabout, ".if \\n(%d<\\n(%d .nr %d \\n(%d\n",
59 							    S1, TMP, S1, TMP);
60 						}
61 						if (text == 0 && real(s = table[ilin][icol].rcol) && !vspen(s) && !barent(s)) {
62 							Bprint(&tabout, ".nr %d \\w%c%s%c\n",
63 							    TMP, F1, s, F1);
64 							Bprint(&tabout, ".if \\n(%d<\\n(%d .nr %d \\n(%d\n", S2, TMP, S2,
65 							     TMP);
66 						}
67 						continue;
68 					}
69 				case 'r':
70 				case 'c':
71 				case 'l':
72 					if (real(s = table[ilin][icol].col) && !vspen(s)) {
73 						if (tx((int)s) != text)
74 							continue;
75 						Bprint(&tabout, ".nr %d ", TMP);
76 						wide(s, FN(vforml, icol), SZ(vforml, icol));
77 						Bprint(&tabout, "\n");
78 						Bprint(&tabout, ".if \\n(%2s<\\n(%d .nr %2s \\n(%d\n",
79 						     reg(icol, CRIGHT), TMP, reg(icol, CRIGHT), TMP);
80 					}
81 				}
82 			}
83 		}
84 		if (acase[icol]) {
85 			Bprint(&tabout, ".if \\n(%d>=\\n(%2s .nr %2s \\n(%du+2n\n",
86 			     S2, reg(icol, CRIGHT), reg(icol, CRIGHT), S2);
87 		}
88 		if (doubled[icol]) {
89 			Bprint(&tabout, ".nr %2s \\n(%d\n", reg(icol, CMID), S1);
90 			Bprint(&tabout, ".nr %d \\n(%2s+\\n(%d\n", TMP, reg(icol, CMID), S2);
91 			Bprint(&tabout, ".if \\n(%d>\\n(%2s .nr %2s \\n(%d\n", TMP,
92 			    reg(icol, CRIGHT), reg(icol, CRIGHT), TMP);
93 			Bprint(&tabout, ".if \\n(%d<\\n(%2s .nr %2s +(\\n(%2s-\\n(%d)/2\n",
94 			     TMP, reg(icol, CRIGHT), reg(icol, CMID), reg(icol, CRIGHT), TMP);
95 		}
96 		if (cll[icol][0]) {
97 			Bprint(&tabout, ".nr %d %sn\n", TMP, cll[icol]);
98 			Bprint(&tabout, ".if \\n(%2s<\\n(%d .nr %2s \\n(%d\n",
99 			    reg(icol, CRIGHT), TMP, reg(icol, CRIGHT), TMP);
100 		}
101 		for (ilin = 0; ilin < nlin; ilin++)
102 			if (k = lspan(ilin, icol)) {
103 				s = table[ilin][icol-k].col;
104 				if (!real(s) || barent(s) || vspen(s) )
105 					continue;
106 				Bprint(&tabout, ".nr %d ", TMP);
107 				wide(table[ilin][icol-k].col, FN(ilin, icol - k), SZ(ilin, icol - k));
108 				for (ik = k; ik >= 0; ik--) {
109 					Bprint(&tabout, "-\\n(%2s", reg(icol - ik, CRIGHT));
110 					if (!expflg && ik > 0)
111 						Bprint(&tabout, "-%dn", sep[icol-ik]);
112 				}
113 				Bprint(&tabout, "\n");
114 				Bprint(&tabout, ".if \\n(%d>0 .nr %d \\n(%d/%d\n", TMP,
115 				      TMP, TMP, k);
116 				Bprint(&tabout, ".if \\n(%d<0 .nr %d 0\n", TMP, TMP);
117 				for (ik = 1; ik <= k; ik++) {
118 					if (doubled[icol-k+ik])
119 						Bprint(&tabout, ".nr %2s +\\n(%d/2\n",
120 						     reg(icol - k + ik, CMID), TMP);
121 					Bprint(&tabout, ".nr %2s +\\n(%d\n",
122 					     reg(icol - k + ik, CRIGHT), TMP);
123 				}
124 			}
125 	}
126 	if (textflg)
127 		untext();
128 				/* if even requested, make all columns widest width */
129 	if (evenflg) {
130 		Bprint(&tabout, ".nr %d 0\n", TMP);
131 		for (icol = 0; icol < ncol; icol++) {
132 			if (evenup[icol] == 0)
133 				continue;
134 			Bprint(&tabout, ".if \\n(%2s>\\n(%d .nr %d \\n(%2s\n",
135 			    reg(icol, CRIGHT), TMP, TMP, reg(icol, CRIGHT));
136 		}
137 		for (icol = 0; icol < ncol; icol++) {
138 			if (evenup[icol] == 0)
139 				/* if column not evened just retain old interval */
140 				continue;
141 			if (doubled[icol])
142 				Bprint(&tabout, ".nr %2s (100*\\n(%2s/\\n(%2s)*\\n(%d/100\n",
143 				    reg(icol, CMID), reg(icol, CMID), reg(icol, CRIGHT), TMP);
144 			/* that nonsense with the 100's and parens tries
145 				   to avoid overflow while proportionally shifting
146 				   the middle of the number */
147 			Bprint(&tabout, ".nr %2s \\n(%d\n", reg(icol, CRIGHT), TMP);
148 		}
149 	}
150 				/* now adjust for total table width */
151 	for (tsep = icol = 0; icol < ncol; icol++)
152 		tsep += sep[icol];
153 	if (expflg) {
154 		Bprint(&tabout, ".nr %d 0", TMP);
155 		for (icol = 0; icol < ncol; icol++)
156 			Bprint(&tabout, "+\\n(%2s", reg(icol, CRIGHT));
157 		Bprint(&tabout, "\n");
158 		Bprint(&tabout, ".nr %d \\n(.l-\\n(%d\n", TMP, TMP);
159 		if (boxflg || dboxflg || allflg)
160 			/* tsep += 1; */ ;
161 		else
162 			tsep -= sep[ncol-1];
163 		Bprint(&tabout, ".nr %d \\n(%d/%d\n", TMP, TMP,  tsep);
164 		Bprint(&tabout, ".if \\n(%d<0 .nr %d 0\n", TMP, TMP);
165 	} else
166 		Bprint(&tabout, ".nr %d 1n\n", TMP);
167 	Bprint(&tabout, ".nr %2s 0\n", reg(-1, CRIGHT));
168 	tsep = (boxflg || allflg || dboxflg || left1flg) ? 2 : 0;
169 	if (sep[-1] >= 0)
170 		tsep = sep[-1];
171 	for (icol = 0; icol < ncol; icol++) {
172 		Bprint(&tabout, ".nr %2s \\n(%2s+((%d*\\n(%d)/2)\n", reg(icol, CLEFT),
173 		    reg(icol - 1, CRIGHT), tsep, TMP);
174 		Bprint(&tabout, ".nr %2s +\\n(%2s\n", reg(icol, CRIGHT), reg(icol, CLEFT));
175 		if (doubled[icol]) {
176 			/* the next line is last-ditch effort to avoid zero field width */
177 			/*Bprint(&tabout, ".if \\n(%2s=0 .nr %2s 1\n",reg(icol,CMID), reg(icol,CMID));*/
178 			Bprint(&tabout, ".nr %2s +\\n(%2s\n", reg(icol, CMID),
179 			    reg(icol, CLEFT));
180 			/*  Bprint(&tabout, ".if n .if \\n(%s%%24>0 .nr %s +12u\n",reg(icol,CMID), reg(icol,CMID)); */
181 		}
182 		tsep = sep[icol] * 2;
183 	}
184 	if (rightl)
185 		Bprint(&tabout, ".nr %s (\\n(%s+\\n(%s)/2\n", reg(ncol - 1, CRIGHT),
186 		      reg(ncol - 1, CLEFT), reg(ncol - 2, CRIGHT));
187 	Bprint(&tabout, ".nr TW \\n(%2s\n", reg(ncol - 1, CRIGHT));
188 	tsep = sep[ncol-1];
189 	if (boxflg || allflg || dboxflg)
190 		Bprint(&tabout, ".nr TW +((%d*\\n(%d)/2)\n", tsep, TMP);
191 	Bprint(&tabout,
192 	    ".if t .if (\\n(TW+\\n(.o)>7.65i .tm Table at line %d file %s is too wide - \\n(TW units\n", iline - 1, ifile);
193 	return;
194 }
195 
196 
197 void
198 wide(char *s, char *fn, char *size)
199 {
200 	if (point(s)) {
201 		Bprint(&tabout, "\\w%c", F1);
202 		if (*fn > 0)
203 			putfont(fn);
204 		if (*size)
205 			putsize(size);
206 		Bprint(&tabout, "%s", s);
207 		if (*fn > 0)
208 			putfont("P");
209 		if (*size)
210 			putsize("0");
211 		Bprint(&tabout, "%c", F1);
212 	} else
213 		Bprint(&tabout, "\\n(%c-", s);
214 }
215 
216 
217 int
218 filler(char *s)
219 {
220 	return (point(s) && s[0] == '\\' && s[1] == 'R');
221 }
222 
223 
224