xref: /illumos-gate/usr/src/cmd/tbl/t6.c (revision 55fea89d)
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  /* t6.c: compute tab stops */
17 # define tx(a) (a>(char *)0 && a<(char *)128)
18 # include "t..c"
19 
20 void	wide(char *, char *, char *);
21 
22 void
maktab(void)23 maktab(void)
24 {
25 # define FN(i,c) font[stynum[i]][c]
26 # define SZ(i,c) csize[stynum[i]][c]
27 /* define the tab stops of the table */
28 int icol, ilin, tsep, k, ik, vforml, il, text;
29 int doubled[MAXCOL], acase[MAXCOL];
30 char *s;
31 for(icol=0; icol <ncol; icol++)
32 	{
33 	doubled[icol] = acase[icol] = 0;
34 	fprintf(tabout, ".nr %d 0\n", icol+CRIGHT);
35    for(text=0; text<2; text++)
36 	{
37 	if (text)
38 		fprintf(tabout, ".%02d\n.rm %02d\n", icol+80, icol+80);
39 	for(ilin=0; ilin<nlin; ilin++)
40 		{
41 		if (instead[ilin]|| fullbot[ilin]) continue;
42 		vforml=ilin;
43 		for(il=prev(ilin); il>=0 && vspen(table[il][icol].col); il=prev(il))
44 			vforml=il;
45 		if (fspan(vforml,icol)) continue;
46 		if (filler(table[ilin][icol].col)) continue;
47 		switch(ctype(vforml,icol))
48 			{
49 			case 'a':
50 				acase[icol]=1;
51 				s = table[ilin][icol].col;
52 				if (s>(char *)0 && s<(char *)128 && text)
53 					{
54 					if (doubled[icol]==0)
55 						fprintf(tabout, ".nr %d 0\n.nr %d 0\n",S1,S2);
56 					doubled[icol]=1;
57 					fprintf(tabout, ".if \\n(%c->\\n(%d .nr %d \\n(%c-\n",s,S2,S2,s);
58 					}
59 				/* FALLTHROUGH */
60 			case 'n':
61 				if (table[ilin][icol].rcol!=0)
62 					{
63 					if (doubled[icol]==0 && text==0)
64 						fprintf(tabout, ".nr %d 0\n.nr %d 0\n", S1, S2);
65 					doubled[icol]=1;
66 					if (real(s=table[ilin][icol].col) && !vspen(s))
67 						{
68 						if (tx(s) != text) continue;
69 						fprintf(tabout, ".nr %d ", TMP);
70 						wide(s, FN(vforml,icol), SZ(vforml,icol)); fprintf(tabout, "\n");
71 						fprintf(tabout, ".if \\n(%d<\\n(%d .nr %d \\n(%d\n", S1, TMP, S1, TMP);
72 						}
73 					if (text==0 && real(s=table[ilin][icol].rcol) && !vspen(s) && !barent(s))
74 						{
75 						fprintf(tabout, ".nr %d \\w%c%s%c\n",TMP, F1, s, F1);
76 						fprintf(tabout, ".if \\n(%d<\\n(%d .nr %d \\n(%d\n",S2,TMP,S2,TMP);
77 						}
78 					continue;
79 					}
80 				/* FALLTHROUGH */
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 	}
133 if (textflg) untext();
134 /* if even requested, make all columns widest width */
135 # define TMP1 S1
136 # define TMP2 S2
137 if (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 */
161 for(tsep=icol=0; icol<ncol; icol++)
162 	tsep+= sep[icol];
163 if (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 	}
177 else
178 	fprintf(tabout, ".nr %d 1n\n", TMP);
179 fprintf(tabout, ".nr %d 0\n",CRIGHT-1);
180 tsep= (boxflg || allflg || dboxflg || left1flg) ? 1 : 0;
181 for(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 	}
194 if (rightl)
195 	fprintf(tabout, ".nr %d (\\n(%d+\\n(%d)/2\n",ncol+CRIGHT-1, ncol+CLEFT-1, ncol+CRIGHT-2);
196 fprintf(tabout, ".nr TW \\n(%d\n", ncol+CRIGHT-1);
197 if (boxflg || allflg || dboxflg)
198 	fprintf(tabout, ".nr TW +%d*\\n(%d\n", sep[ncol-1], TMP);
199 fprintf(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);
201 return;
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 	{
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 	}
217 else
218 	fprintf(tabout, "\\n(%c-", s);
219 }
220 
221 int
filler(char * s)222 filler(char *s)
223 {
224 return (point(s) && s[0]=='\\' && s[1] == 'R');
225 }
226