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