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