1c9d58bfcSjaap #ifndef lint
2*306722fdSbostic static char sccsid[] = "@(#)maktab.c	1.4 (Berkeley) 07/27/93";
3c9d58bfcSjaap #endif lint
4c9d58bfcSjaap 
5c9d58bfcSjaap 
60ad32596Sjaap /*
70ad32596Sjaap  * compute tab stops
80ad32596Sjaap  */
9c9d58bfcSjaap 
10c9d58bfcSjaap #define tx(a)		(a>0 && a<128)
11c9d58bfcSjaap 
12c9d58bfcSjaap #define FN(i,c)		font[stynum[i]][c]
13c9d58bfcSjaap #define SZ(i,c)		csize[stynum[i]][c]
14c9d58bfcSjaap 
15c9d58bfcSjaap #include "defs.h"
16c9d58bfcSjaap #include "ext.h"
17c9d58bfcSjaap 
18c9d58bfcSjaap /*
19c9d58bfcSjaap  * define the tab stops of the table
20c9d58bfcSjaap  */
maktab()21c9d58bfcSjaap maktab()
22c9d58bfcSjaap {
23c9d58bfcSjaap 	int icol, ilin, tsep, k, ik, vforml, il, text;
24c9d58bfcSjaap 	int doubled[MAXCOL], acase[MAXCOL];
25c9d58bfcSjaap 	char *s;
26c9d58bfcSjaap 
27c9d58bfcSjaap 	for(icol = 0; icol < ncol; icol++){
28c9d58bfcSjaap 		doubled[icol] = acase[icol] = 0;
29c9d58bfcSjaap 		printf(".nr %2s 0\n", reg(icol, CRIGHT));
30c9d58bfcSjaap 		for(text = 0; text < 2; text++){
31c9d58bfcSjaap 			if(text)
32c9d58bfcSjaap 				printf(".%2s\n.rm %2s\n", reg(icol, CRIGHT),
33c9d58bfcSjaap 							reg(icol, CRIGHT));
34c9d58bfcSjaap 			for(ilin = 0; ilin < nlin; ilin++){
35c9d58bfcSjaap 				if(instead[ilin] || fullbot[ilin]){
36c9d58bfcSjaap 					continue;
37c9d58bfcSjaap 				}
38c9d58bfcSjaap 				vforml = ilin;
39c9d58bfcSjaap 				for(il = prev(ilin);
40c9d58bfcSjaap 				    il >= 0 && vspen(table[il][icol].col);
41c9d58bfcSjaap 				    il = prev(il))
42c9d58bfcSjaap 					vforml = il;
43c9d58bfcSjaap 				if(fspan(vforml, icol)){
44c9d58bfcSjaap 					continue;
45c9d58bfcSjaap 				}
46c9d58bfcSjaap 				if(filler(table[ilin][icol].col)){
47c9d58bfcSjaap 					continue;
48c9d58bfcSjaap 				}
49c9d58bfcSjaap 				if((ctop[stynum[ilin]][icol] & ZEROW) != 0){
50c9d58bfcSjaap 					continue;
51c9d58bfcSjaap 				}
52c9d58bfcSjaap 				switch(ctype(vforml, icol)){
53c9d58bfcSjaap 
54c9d58bfcSjaap 				case 'a':
55c9d58bfcSjaap 					acase[icol] = 1;
56c9d58bfcSjaap 					s = table[ilin][icol].col;
57c9d58bfcSjaap 					if(s > 0 && s < (char *)128 && text){
58c9d58bfcSjaap 						if(doubled[icol] == 0)
59c9d58bfcSjaap 							printf(
60c9d58bfcSjaap 							 ".nr %d 0\n.nr %d 0\n",
61c9d58bfcSjaap 							 S1, S2);
62c9d58bfcSjaap 						doubled[icol] = 1;
63c9d58bfcSjaap 						printf(
64c9d58bfcSjaap 					  ".if \\n(%c->\\n(%d .nr %d \\n(%c-\n",
65c9d58bfcSjaap 								s, S2, S2, s);
66c9d58bfcSjaap 					}
67c9d58bfcSjaap 				case 'n':
68c9d58bfcSjaap 					if(table[ilin][icol].rcol != 0){
69c9d58bfcSjaap 						if(doubled[icol] == 0
70c9d58bfcSjaap 							 && text == 0)
71c9d58bfcSjaap 							printf(
72c9d58bfcSjaap 							 ".nr %d 0\n.nr %d 0\n",
73c9d58bfcSjaap 							 S1, S2);
74c9d58bfcSjaap 						doubled[icol] = 1;
75c9d58bfcSjaap 						if(real(s=table[ilin][icol].col)
76c9d58bfcSjaap 						    && !vspen(s)){
77c9d58bfcSjaap 							if(tx((int)s) != text)
78c9d58bfcSjaap 								continue;
79c9d58bfcSjaap 							printf(".nr %d ", TMP);
80c9d58bfcSjaap 							wide(s,
81c9d58bfcSjaap 							      FN(vforml, icol),
82c9d58bfcSjaap 							      SZ(vforml, icol));
83c9d58bfcSjaap 							printf("\n");
84c9d58bfcSjaap 							printf(
85c9d58bfcSjaap 					   ".if \\n(%d<\\n(%d .nr %d \\n(%d\n",
86c9d58bfcSjaap 							      S1, TMP, S1, TMP);
87c9d58bfcSjaap 						}
88c9d58bfcSjaap 						if(text == 0
89c9d58bfcSjaap 						   && real(s=table[ilin][icol].rcol)
90c9d58bfcSjaap 						   && !vspen(s) && !barent(s)){
91c9d58bfcSjaap 							printf(
92c9d58bfcSjaap 							   ".nr %d \\w%c%s%c\n",
93c9d58bfcSjaap 							  	TMP, F1, s, F1);
94c9d58bfcSjaap 							printf(
95c9d58bfcSjaap 					    ".if \\n(%d<\\n(%d .nr %d \\n(%d\n",
96c9d58bfcSjaap 							      S2, TMP, S2, TMP);
97c9d58bfcSjaap 						}
98c9d58bfcSjaap 						continue;
99c9d58bfcSjaap 					}
100c9d58bfcSjaap 				case 'r':
101c9d58bfcSjaap 				case 'c':
102c9d58bfcSjaap 				case 'l':
103c9d58bfcSjaap 					if(real(s = table[ilin][icol].col)
104c9d58bfcSjaap 					   && !vspen(s)){
105c9d58bfcSjaap 						if(tx((int)s) != text)
106c9d58bfcSjaap 							continue;
107c9d58bfcSjaap 						printf(".nr %d ", TMP);
108c9d58bfcSjaap 						wide(s,
109c9d58bfcSjaap 							FN(vforml, icol),
110c9d58bfcSjaap 							SZ(vforml, icol));
111c9d58bfcSjaap 						printf("\n");
112c9d58bfcSjaap 						printf(
113c9d58bfcSjaap 	   ".if \\n(%2s<\\n(%d .nr %2s \\n(%d\n", reg(icol, CRIGHT), TMP,
114c9d58bfcSjaap 							reg(icol, CRIGHT), TMP);
115c9d58bfcSjaap 					}
116c9d58bfcSjaap 				}
117c9d58bfcSjaap 			}
118c9d58bfcSjaap 		}
119c9d58bfcSjaap 		if(acase[icol]){
120c9d58bfcSjaap 			printf(".if \\n(%d>=\\n(%2s .nr %2s \\n(%du+2n\n",
121c9d58bfcSjaap 				S2, reg(icol, CRIGHT), reg(icol, CRIGHT), S2);
122c9d58bfcSjaap 		}
123c9d58bfcSjaap 		if(doubled[icol]){
124c9d58bfcSjaap 			printf(".nr %2s \\n(%d\n", reg(icol, CMID), S1);
125c9d58bfcSjaap 			printf(".nr %d \\n(%2s+\\n(%d\n", TMP, reg(icol, CMID),
126c9d58bfcSjaap 									S2);
127c9d58bfcSjaap 			printf(".if \\n(%d>\\n(%2s .nr %2s \\n(%d\n", TMP,
128c9d58bfcSjaap 				    reg(icol, CRIGHT), reg(icol, CRIGHT), TMP);
129c9d58bfcSjaap 			printf(
130c9d58bfcSjaap 			     ".if \\n(%d<\\n(%2s .nr %2s +(\\n(%2s-\\n(%d)/2\n",
131c9d58bfcSjaap 			     TMP, reg(icol, CRIGHT), reg(icol, CMID),
132c9d58bfcSjaap 			     				reg(icol, CRIGHT), TMP);
133c9d58bfcSjaap 		}
134c9d58bfcSjaap 		if(cll[icol][0]){
135c9d58bfcSjaap 			printf(".nr %d %sn\n", TMP, cll[icol]);
136c9d58bfcSjaap 			printf(".if \\n(%2s<\\n(%d .nr %2s \\n(%d\n",
137c9d58bfcSjaap 				reg(icol, CRIGHT), TMP, reg(icol, CRIGHT), TMP);
138c9d58bfcSjaap 		}
139c9d58bfcSjaap 		for(ilin = 0; ilin < nlin; ilin++)
140c9d58bfcSjaap 			if(k = lspan(ilin, icol)){
141c9d58bfcSjaap 				s = table[ilin][icol - k].col;
142c9d58bfcSjaap 				if(!real(s) || barent(s) || vspen(s))
143c9d58bfcSjaap 					continue;
144c9d58bfcSjaap 				printf(".nr %d ", TMP);
145c9d58bfcSjaap 				wide(table[ilin][icol - k].col,
146c9d58bfcSjaap 				      FN(ilin, icol - k), SZ(ilin, icol - k));
147c9d58bfcSjaap 				for(ik = k; ik >= 0; ik--){
148c9d58bfcSjaap 					printf("-\\n(%2s", reg(icol-ik,CRIGHT));
149c9d58bfcSjaap 					if(!expflg && ik > 0)
150c9d58bfcSjaap 						printf( "-%dn", sep[icol - ik]);
151c9d58bfcSjaap 				}
152c9d58bfcSjaap 				printf("\n");
153c9d58bfcSjaap 				printf(".if \\n(%d>0 .nr %d \\n(%d/%d\n",
154c9d58bfcSjaap 							TMP, TMP, TMP, k);
155c9d58bfcSjaap 				printf(".if \\n(%d<0 .nr %d 0\n",
156c9d58bfcSjaap 							TMP, TMP);
157c9d58bfcSjaap 				for(ik = 1; ik <= k; ik++){
158c9d58bfcSjaap 					if(doubled[icol - k + ik]){
159c9d58bfcSjaap 						printf(".nr %2s +\\n(%d/2\n",
160c9d58bfcSjaap 							reg(icol - k + ik,CMID),
161c9d58bfcSjaap 								TMP);
162c9d58bfcSjaap 					}
163c9d58bfcSjaap 					printf(".nr %2s +\\n(%d\n",
164c9d58bfcSjaap 						reg(icol - k + ik, CRIGHT),TMP);
165c9d58bfcSjaap 				}
166c9d58bfcSjaap 			}
167c9d58bfcSjaap 	}
168c9d58bfcSjaap 	if(textflg)
169c9d58bfcSjaap 		untext();
170c9d58bfcSjaap 	/*
171c9d58bfcSjaap 	 * if even requested, make all columns widest width
172c9d58bfcSjaap 	 */
173c9d58bfcSjaap 
174c9d58bfcSjaap #define TMP1 S1
175c9d58bfcSjaap #define TMP2 S2
176c9d58bfcSjaap 
177c9d58bfcSjaap 	if(evenflg){
178c9d58bfcSjaap 		printf(".nr %d 0\n", TMP);
179c9d58bfcSjaap 		for(icol = 0; icol < ncol; icol++){
180c9d58bfcSjaap 			if(evenup[icol] == 0)
181c9d58bfcSjaap 				continue;
182c9d58bfcSjaap 			printf(".if \\n(%2s>\\n(%d .nr %d \\n(%2s\n",
183c9d58bfcSjaap 				reg(icol, CRIGHT), TMP, TMP, reg(icol, CRIGHT));
184c9d58bfcSjaap 		}
185c9d58bfcSjaap 		for(icol = 0; icol < ncol; icol++){
186c9d58bfcSjaap  			/*
187c9d58bfcSjaap 			 * if column not evened just retain old interval
188c9d58bfcSjaap 			 */
189c9d58bfcSjaap 			if(evenup[icol] == 0)
190c9d58bfcSjaap 				continue;
191c9d58bfcSjaap 			if(doubled[icol])
192c9d58bfcSjaap 				printf(
193c9d58bfcSjaap 				   ".nr %2s (100*\\n(%2s/\\n(%2s)*\\n(%d/100\n",
194c9d58bfcSjaap 				   	reg(icol, CMID), reg(icol, CMID),
195c9d58bfcSjaap 				   		reg(icol, CRIGHT), TMP);
196c9d58bfcSjaap 			/*
197c9d58bfcSjaap 		 	* that nonsense with the 100's and parens tries to avoid
198c9d58bfcSjaap 		 	* overflow while proportionally shifting the middle of
199c9d58bfcSjaap 		 	* the number
200c9d58bfcSjaap 		 	*/
201c9d58bfcSjaap 			printf(".nr %2s \\n(%d\n", reg(icol, CRIGHT), TMP);
202c9d58bfcSjaap 		}
203c9d58bfcSjaap 	}
204c9d58bfcSjaap 	/*
205c9d58bfcSjaap 	 * now adjust for total table width
206c9d58bfcSjaap 	 */
207c9d58bfcSjaap 	for(tsep = icol = 0; icol < ncol; icol++)
208c9d58bfcSjaap 		tsep += sep[icol];
209c9d58bfcSjaap 	if(expflg){
210c9d58bfcSjaap 		printf(".nr %d 0", TMP);
211c9d58bfcSjaap 		for(icol = 0; icol < ncol; icol++)
212c9d58bfcSjaap 			printf("+\\n(%2s", reg(icol, CRIGHT));
213c9d58bfcSjaap 		printf("\n");
214c9d58bfcSjaap 		/*
215c9d58bfcSjaap 		 * Bug fix: Most users expect the expand to take place
216c9d58bfcSjaap 		 * over the line length minus the current indentation
217c9d58bfcSjaap 		 * (I do as well, a bit ugly to see the table creeping
218c9d58bfcSjaap 		 * in the right margin (jna))
219c9d58bfcSjaap 		 */
220c9d58bfcSjaap 		printf(".nr %d \\n(.l-\\n(.i-\\n(%d\n", TMP, TMP);
221c9d58bfcSjaap 		if(boxflg || dboxflg || allflg)
222c9d58bfcSjaap 			tsep += 1;
223c9d58bfcSjaap 		else
224c9d58bfcSjaap 			tsep -= sep[ncol - 1];
225c9d58bfcSjaap 		printf(".nr %d \\n(%d/%d\n", TMP, TMP, tsep);
226c9d58bfcSjaap 		printf(".if \\n(%d<0 .nr %d 0\n", TMP, TMP);
227c9d58bfcSjaap 	} else
228c9d58bfcSjaap 		printf(".nr %d 1n\n", TMP);
229c9d58bfcSjaap 	printf(".nr %2s 0\n", reg(-1,CRIGHT));
230c9d58bfcSjaap 	tsep = (boxflg || allflg || dboxflg || left1flg) ? 1 : 0;
231c9d58bfcSjaap 	for(icol = 0; icol < ncol; icol++){
232c9d58bfcSjaap 		printf(".nr %2s \\n(%2s+(%d*\\n(%d)\n", reg(icol, CLEFT),
233c9d58bfcSjaap 					reg(icol -1, CRIGHT), tsep, TMP);
234c9d58bfcSjaap 		printf(".nr %2s +\\n(%2s\n",reg(icol, CRIGHT),reg(icol, CLEFT));
235c9d58bfcSjaap 		if(doubled[icol]){
236c9d58bfcSjaap 			/*
237c9d58bfcSjaap 			 * the next line is last-ditch effort to avoid
238c9d58bfcSjaap 			 * zero field width
239c9d58bfcSjaap 			 */
240c9d58bfcSjaap 			/*
241c9d58bfcSjaap 			printf(".if \\n(%2s=0 .nr %2s 1\n", reg(icol,CMID),
242c9d58bfcSjaap 		   				reg(icol,CMID));
243c9d58bfcSjaap 			 */
244c9d58bfcSjaap 			printf(".nr %2s +\\n(%2s\n", reg(icol, CMID),
245c9d58bfcSjaap 							reg(icol, CLEFT));
246c9d58bfcSjaap 			/*
247c9d58bfcSjaap 			printf(".if n .if \\n(%2s%%24>0 .nr %2s +12u\n",
248c9d58bfcSjaap 					reg(icol, CMID), reg(icol, CMID));
249c9d58bfcSjaap 			 */
250c9d58bfcSjaap 		}
251c9d58bfcSjaap 		tsep = sep[icol];
252c9d58bfcSjaap 	}
253c9d58bfcSjaap 	if(rightl)
254c9d58bfcSjaap 		printf(".nr %2s (\\n(%2s+\\n(%2s)/2\n", reg(ncol-1, CRIGHT),
255c9d58bfcSjaap 							reg(ncol-1, CLEFT),
256c9d58bfcSjaap 							reg(ncol-2, CRIGHT));
257c9d58bfcSjaap 	printf(".nr TW \\n(%2s\n", reg(ncol-1, CRIGHT));
258c9d58bfcSjaap 	if(boxflg || allflg || dboxflg)
259c9d58bfcSjaap 		printf(".nr TW +%d*\\n(%d\n", sep[ncol - 1], TMP);
260c9d58bfcSjaap 	printf(
261*306722fdSbostic ".if t .if\\n(TW>\\n(.lu .tm Table at line %d file %s is too wide - \\n(TW units\n",
26249fc16cbSjaap 		iline - 1, strlen(oldname) ? oldname : ifile);
263c9d58bfcSjaap 	return;
264c9d58bfcSjaap }
265c9d58bfcSjaap 
266c9d58bfcSjaap /*
267c9d58bfcSjaap  * tell troff to calculate width of an entry
268c9d58bfcSjaap  */
wide(s,fn,size)269c9d58bfcSjaap wide(s, fn, size)
270c9d58bfcSjaap char *s, *size, *fn;
271c9d58bfcSjaap {
272c9d58bfcSjaap 	/*
273c9d58bfcSjaap 	 * if s is a pointer to a string, calculate the with of that string
274c9d58bfcSjaap 	 */
275c9d58bfcSjaap 	if(point(s)){
276c9d58bfcSjaap 		printf("\\w%c", F1);
277c9d58bfcSjaap 		if(*fn > 0)
278c9d58bfcSjaap 			putfont(fn);
279c9d58bfcSjaap 		if(*size)
280c9d58bfcSjaap 			putsize(size);
281c9d58bfcSjaap 		printf("%s", s);
282c9d58bfcSjaap 		if(*fn > 0)
283c9d58bfcSjaap 			putfont("P");
284c9d58bfcSjaap 		if(*size)
285c9d58bfcSjaap 			putsize("0");
286c9d58bfcSjaap 		printf("%c", F1);
287c9d58bfcSjaap 	} else
288c9d58bfcSjaap 		/*
289c9d58bfcSjaap 		 * it is the name of a diversion, so we know the witdh
290c9d58bfcSjaap 		 * is in <diversionname>-
291c9d58bfcSjaap 		 */
292c9d58bfcSjaap 		printf("\\n(%c-", s);
293c9d58bfcSjaap }
294c9d58bfcSjaap 
filler(s)295c9d58bfcSjaap filler(s)
296c9d58bfcSjaap char *s;
297c9d58bfcSjaap {
298c9d58bfcSjaap 	return(point(s) && s[0] == '\\' && s[1] == 'R');
299c9d58bfcSjaap }
300