summaryrefslogtreecommitdiff
path: root/support/RTF-1_06a1/trf-table.c
blob: 1b2164e5e95fbc5c00e308b1155d31b695952f58 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*
	trf-table.c - table writing functions

	Ugly ugly ugly.
*/

# include	<stdio.h>
# include	<sys/types.h>
# include	"rtf.h"
# include	"rtf2troff.h"


static int	CellBorderLocIndex ();
static int	VCellBorderType ();
static char	*VCellBorderStr ();
static char	*HCellBorderStr ();


/*
	Border type tokens *follow* cell border location specifiers.
*/

void TblAttr ()
{
double	inch = (double) rtfParam / (double) rtfTpi;
int	i, j, loc;

	switch (rtfMinor)
	{
	case rtfCellBordBottom:
	case rtfCellBordTop:
	case rtfCellBordLeft:
	case rtfCellBordRight:
		if (its->nCells >= maxCell)
		{
			fprintf (stderr,
				"Borders specified for too many cells\n");
			break;
		}
		loc = CellBorderLocIndex (rtfMinor);
		/*
			Get border type tokens until non-type token seen,
			then route non-type token normally.
		*/
		for (;;)
		{
			(void) RTFGetToken ();
			if (!RTFCheckCM (rtfControl, rtfParAttr)
				|| (rtfMinor != rtfBorderSingle
					&& rtfMinor != rtfBorderThick
					&& rtfMinor != rtfBorderShadow
					&& rtfMinor != rtfBorderDouble
					&& rtfMinor != rtfBorderDot
					&& rtfMinor != rtfBorderHair))
				break;
			if (its->nCells < maxCell)
				its->border[its->nCells][loc] = rtfMinor;
		}
		RTFRouteToken ();	/* send non-border through router */
		break;
	case rtfRowDef:
		its->tableHeader = 0;
		its->nCells = 0;
		its->curCell = 0;
		for (i = 0; i < maxCell; i++)
		{
			its->cellPos[i] = 0;
			for (j = 0; j < 4; j++)
				its->border[i][j] = rtfNoBorderType;
		}
		its->tableLeft = 0;
		its->cellGap = 0;
		break;
	case rtfRowLeft:
		break;
	case rtfRowRight:
		break;
	case rtfRowCenter:
		break;
	case rtfRowGapH:
		its->cellGap = inch;
		break;
	case rtfRowHt:
		break;
	case rtfRowLeftEdge:
		its->tableLeft = inch;
		break;
	case rtfCellPos:
		if (its->nCells >= maxCell)
			fprintf (f, "max. table row cell count (%d) exceeded\n",
							maxCell);
		else
			its->cellPos[its->nCells++] = inch;
		break;
	case rtfMergeRngFirst:
		break;
	case rtfMergePrevious:
		break;
	}
}


void BeginTbl ()
{
int	i, n;
double	cwid;
char	*p;

	Flush ();
	FlushState ();
	SaveTblFPV ();		/* save current font, ps, vs */
	fprintf (f, ".TS\n");
	fprintf (f, "center tab(^);\n");
	for (i = 0; i < its->nCells; i++)
	{
		if ((p = VCellBorderStr (VCellBorderType (i))) != NULL)
			fprintf (f, "%s ", p);
		cwid = its->cellPos[i];
		if (i > 0)
			cwid -= its->cellPos[i-1];
		cwid -= EnWidth ();
		fprintf (f, "l1w(%gi) ", cwid);
	}
	if ((p = VCellBorderStr (VCellBorderType (its->nCells))) != NULL)
		fprintf (f, "%s ", p);
	fprintf (f, ".\n");
	++its->tableHeader;

	/* print top borders */
	n = 0;
	for (i = 0; i < its->nCells; i++)
	{
		if (its->border[i][topIndex] != rtfNoBorderType)
			++n;
	}
	if (n > 0)
	{
		for (i = 0; i < its->nCells; i++)
		{
			if (i > 0)
				fprintf (f, "^");
			if ((p = HCellBorderStr (its->border[i][topIndex]))
								!= NULL)
				fprintf (f, "%s", p);
		}
		fprintf (f, "\n");
	}
}


void EndTbl ()
{
int	i, n;
char	*p;

	/* print bottom borders */
	n = 0;
	for (i = 0; i < its->nCells; i++)
	{
		if (its->border[i][bottomIndex] != rtfNoBorderType)
			++n;
	}
	if (n > 0)
	{
		for (i = 0; i < its->nCells; i++)
		{
			if (i > 0)
				fprintf (f, "^");
			if ((p = HCellBorderStr (its->border[i][bottomIndex]))
								!= NULL)
				fprintf (f, "%s", p);
		}
		fprintf (f, "\n");
	}
	fprintf (f, ".TE\n");	/* this undoes ps/vs... */
	FlushTblFPV ();		/* so redo it.  ugh. */
	FlushState ();
	its->tableHeader = 0;
	its->curCell = 0;
}


/*
	BeginCell() called when first \intbl in row is seen and after
	each \cell; EndCell() called whenever \cell or \row are seen.
	These do nothing if the cell number is greater than would be
	expected given the number of cell positions found in the table
	layout information.  (It *is* possible to find information
	beyond the last cell; Word for Macintosh, at least, seems to put an
	empty cell at the end of each row.)
*/

void BeginCell ()
{
	/* accept cells 0..nCells-1 */
	if (its->curCell < its->nCells)
	{
		Flush ();
		fprintf (f, "T{\n");
		/*FlushState ();*/
		FlushTblFPV ();		/* set up correct font, ps, vs */
	}
	inTable = 1;
}


void EndCell ()
{
	Flush ();
	/* accept cells 0..nCells-1 */
	if (its->curCell < its->nCells)
	{
		fprintf (f, "T}");
	}
	++its->curCell;
	if (its->curCell < its->nCells)
		fprintf (f, "^");	/* more cells to go */
	else if (its->curCell == its->nCells)
		fprintf (f, "\n");	/* no more cells to go */
	inTable = 0;
}


static int CellBorderLocIndex (loc)
int	loc;
{
	switch (loc)
	{
	case rtfCellBordLeft: return (leftIndex);
	case rtfCellBordRight: return (rightIndex);
	case rtfCellBordTop: return (topIndex);
	case rtfCellBordBottom: return (bottomIndex);
	}
	fprintf (stderr, "CellBorderLocIndex: bad argument (%d)\n", loc);
	exit (1);
}


/*
	Determine vertical border for left of cell i.  Takes into account
	right border of i-1 and left border of i, with the latter taking
	precedence.  Two special cases are handled implicitly in code
	below.  When i = 0, return left border of cell 0.  When i = nCells,
	return right border of cell nCells-1.
*/

static int VCellBorderType (i)
int	i;
{
int	border = rtfNoBorderType;

	if (i >= 0 && i < its->nCells)
		border = its->border[i][leftIndex];
	if (i > 0 && i <= its->nCells && border == rtfNoBorderType)
		border = its->border[i-1][rightIndex];
	return (border);
}


static char *VCellBorderStr (type)
int	type;
{
	switch (type)
	{
	case rtfBorderShadow:
	case rtfBorderThick:
	case rtfBorderDot:
	case rtfBorderHair:
	case rtfBorderSingle: return ("|");
	case rtfBorderDouble: return ("||");
	}
	return (NULL);
}


static char *HCellBorderStr (type)
int	type;
{
	switch (type)
	{
	case rtfBorderShadow:
	case rtfBorderThick:
	case rtfBorderDot:
	case rtfBorderHair:
	case rtfBorderSingle: return ("_");
	case rtfBorderDouble: return ("=");
	}
	return (NULL);
}