summaryrefslogtreecommitdiff
path: root/support/RTF-1_06a1/trf-charmap.c
blob: b6dc0f898ea9c7a6e2157573653af6d2592d2f2e (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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
	trf-charmap.c - Unnecessarily complex routines for mapping special
	characters in the non-ASCII range (128..255) onto troff equivalents.

	This file may require modification for local troff versions.

	Tries to handle the four different character sets allowed in the
	RTF specification (ANSI, Macintosh, PC and PCA).  That part in itself
	is easy, but the complexity stems from trying to allow for local
	variations in troff versions (not all troff's understand the same
	set of special characters) and for particular macro packages (-me,
	-me, -ms).  The last one is silly at this point since no other part
	of rtf2troff knows anything about writing macro-package-specific
	output.  Maybe someday.

	SelectFormatterMaps () - selects character maps specific to the given
		formatter, e.g., xroff, pstroff, eroff.
	SelectMacPackMaps () - select override maps for the given macro
		package.
	SelectCharSetMaps () - select maps for particular character set.
	CharMapping () - return troff equivalent for a special character.
*/

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


/*
	Structure for mapping character values >= 128 to troff strings
	for different character sets.

	The reason char value/string pairs are used instead of a 128-entry
	table is that while the latter would be faster, it's much easier
	to build an incomplete table with the former, and the entries don't
	need to be in any particular order.
*/

typedef struct CharMap	CharMap;

struct CharMap
{
	int	charVal;
	char	*charStr;
};


/*
	Structure for associating default and override maps with
	particular versions of troff.
*/

# define	ansiMap		0
# define	macMap		1
# define	pcMap		2
# define	pcaMap		3
# define	mapTypes	4

typedef struct CharMapTable	CharMapTable;

struct CharMapTable
{
	char	*formatter;
	CharMap	*defCharMap[mapTypes];
	CharMap	*meCharMap[mapTypes];
	CharMap	*mmCharMap[mapTypes];
	CharMap	*msCharMap[mapTypes];
};


/*
	ANSI char map is taken from Windows 3.0 and Word for
	Windows 1.1 manuals.  Note that these conflict, so there
	are some guesses.
	PC char map is taken from Windows 3.0 manual.
*/

static CharMap	ansiCharMap [] =
{
	0x91,	"`",		/* left curly single quote */
	0x92,	"'",		/* right curly single quote */
	0x93,	"``",		/* left curly double quote */
	0x94,	"''",		/* right curly double quote */
	0x95,	"o",		/* ?? */
	0xa2,	"\\(ct",	/* cent */
	0xa6,	"|",		/* vert. bar */
	0xa7,	"\\(sc",	/* section */
	0xa9,	"\\(co",	/* copyright */
	0xab,	"<<",		/* alternate quote */
	0xac,	"\\(no",	/* not */
	0xad,	"\\(hy",	/* short dash */
	0xae,	"\\(rg",	/* registered */
	0xaf,	"\\(em",	/* long dash */
	0xb0,	"\\(de",	/* degree */
	0xb1,	"\\(+-",	/* plus or minus */
	0xb4,	"\\(aa",	/* acute accent */
	0xb5,	"\\(*m",	/* micro (use greek mu) */
	0xbb,	">>",		/* alternate end-quote*/
	0xbc,	"\\(14",	/* one-fourth */
	0xbd,	"\\(12",	/* one-half */
	0xbe,	"\\(34",	/* three-fourths */
	0xc6,	"AE",		/* joined A-E */
	0xd7,	"\\(mu",	/* math multiply */
	0xdf,	"\\(*b",	/* German B? (fake with greek beta) */
	0xe6,	"ae",		/* joined a-e */
	0xf7,	"\\(di",	/* divide */
	0,	NULL
};


static CharMap	macCharMap [] =
{
	0xa0,	"\\(dg",	/* dagger */
	0xa1,	"\\(de",	/* degree */
	0xa2,	"\\(ct",	/* cent */
	0xa4,	"\\(sc",	/* section */
	0xa5,	"\\(bu",	/* bullet */
	0xa7,	"\\(*b",	/* German B? (fake with greek beta) */
	0xa8,	"\\(rg",	/* registered */
	0xa9,	"\\(co",	/* copyright */
	0xaa,	"(TM)",		/* trademark */
	0xab,	"\\(aa",	/* acute accent */
	0xad,	"\\(!=",	/* not equal */
	0xae,	"AE",		/* joined A-E */
	0xb0,	"\\(if",	/* infinity */
	0xb1,	"\\(+-",	/* plus or minus */
	0xb2,	"\\(<=",	/* less than or equal */
	0xb3,	"\\(>=",	/* greater than or equal */
	0xb5,	"\\(*m",	/* micro (use greek mu) */
	0xb6,	"\\(*d",	/* delta */
	0xb7,	"\\(*S",	/* summation (Sigma) */
	0xb8,	"\\(*P",	/* Pi */
	0xb9,	"\\(*p",	/* pi */
	0xba,	"\\(is",	/* integral sign */
	0xbd,	"\\(*W",	/* ohm-sign (greek Omega) */
	0xbe,	"ae",		/* joined a-e */
	0xc2,	"\\(no",	/* not */
	0xc3,	"\\(sr",	/* square root */
	0xc5,	"\\(ap",	/* approximately */
	0xc6,	"\\(*D",	/* Delta */
	0xc7,	"<<",		/* alternate quote */
	0xc8,	">>",		/* alternate end-quote*/
	0xc9,	"...",		/* ellipsis */
	0xca,	"\\ ",		/* unbreakable space */
	0xd0,	"\\(hy",	/* short dash */
	0xd1,	"\\(em",	/* long dash */
	0xd2,	"``",		/* left curly double quote */
	0xd3,	"''",		/* right curly double quote */
	0xd4,	"`",		/* left curly single quote */
	0xd5,	"'",		/* right curly single quote */
	0xd6,	"\\(di",	/* divide */
	0,	NULL
};


static CharMap	pcCharMap [] =
{
	0x91,	"ae",		/* joined a-e */
	0x9b,	"\\(ct",	/* cent */
	0xa9,	"\\(em",	/* long dash */
	0xaa,	"\\(no",	/* not */
	0xab,	"\\(12",	/* one-half */
	0xac,	"\\(14",	/* one-fourth */
	0xae,	"<<",		/* alternate quote */
	0xaf,	">>",		/* alternate end-quote*/
	0xf1,	"\\(+-",	/* plus or minus */
	0xf8,	"\\(de",	/* degree */
	0,	NULL
};


static CharMap	pcaCharMap [] =
{
	0,	NULL
};


/*
	These -me maps are just here for example
*/


static CharMap	meAnsiCharMap [] =
{
	0,	NULL
};


static CharMap	meMacCharMap [] =
{
	0,	NULL
};


static CharMap	mePcCharMap [] =
{
	0,	NULL
};


static CharMap	mePcaCharMap [] =
{
	0,	NULL
};


static CharMapTable charMapTable [] =
{
    {
	"troff",
	{ ansiCharMap, macCharMap, pcCharMap, pcaCharMap },	/* defaults */
	{ NULL, NULL, NULL, NULL },			/* -me overrides */
	{ NULL, NULL, NULL, NULL },			/* -mm overrides */
	{ NULL, NULL, NULL, NULL }			/* -ms overrides */
    },
    {
	"xroff",
	{ ansiCharMap, macCharMap, pcCharMap, pcaCharMap },	/* defaults */
	{ NULL, NULL, NULL, NULL },			/* -me overrides */
	{ NULL, NULL, NULL, NULL },			/* -mm overrides */
	{ NULL, NULL, NULL, NULL }			/* -ms overrides */
    },
    {
	"pstroff",
	{ ansiCharMap, macCharMap, pcCharMap, pcaCharMap },	/* defaults */
	{ NULL, NULL, NULL, NULL },			/* -me overrides */
	{ NULL, NULL, NULL, NULL },			/* -mm overrides */
	{ NULL, NULL, NULL, NULL }			/* -ms overrides */
    },
    {
	/*
		This should be the last entry.  Do not change it.
		Put new entries before this one.
	*/
	NULL
    }
};


/*
	curCharMaps points to current entry in character map table.
	macroMaps points to current set of override maps (default NULL
	until SelectMacPackMaps() is called).  defaultMap points to the
	default map within curCharMaps (default is the ANSI map).
	overrideMap points to the map within the macro package override
	maps (default is the ANSI map after SelectMacPackMaps() is called).
*/

static CharMapTable	*curCharMaps = &charMapTable[0];
static CharMap		**macroMaps = NULL;
static CharMap		*defaultMap = ansiCharMap;
static CharMap		*overrideMap = NULL;


void SelectFormatterMaps (name)
char	*name;
{
CharMapTable	*cmtp;

	for (cmtp = charMapTable; cmtp->formatter != NULL; cmtp++)
	{
		if (strcmp (name, cmtp->formatter) == 0)
		{
			curCharMaps = cmtp;
			macroMaps = NULL;
			defaultMap = curCharMaps->defCharMap[ansiMap];
			overrideMap = NULL;
			return;
		}
	}
	fprintf (stderr, "Unknown formatter: %s\n", name);
	exit (1);
}


void SelectMacPackMaps (name)
char	*name;
{
	if (strcmp (name, "-me") == 0)
		macroMaps = curCharMaps->meCharMap;
	else if (strcmp (name, "-mm") == 0)
		macroMaps = curCharMaps->mmCharMap;
	else if (strcmp (name, "-ms") == 0)
		macroMaps = curCharMaps->msCharMap;
	else
	{
		fprintf (stderr, "Unknown macro package: %s\n", name);
		exit (1);
	}
	if (macroMaps != NULL)
		overrideMap = macroMaps[ansiMap];
}


/*
	mapSet should be an rtfCharSet minor number
*/

void SelectCharSetMaps (mapSet)
int	mapSet;
{
int	mapIndex;

	switch (mapSet)
	{
	case rtfAnsiCharSet:
		mapIndex = ansiMap;
		break;
	case rtfMacCharSet:
		mapIndex = macMap;
		break;
	case rtfPcCharSet:
		mapIndex = pcMap;
		break;
	case rtfPcaCharSet:
		mapIndex = pcaMap;
		break;
	default:
		fprintf (stderr, "Unknown charset: %d\n", mapSet);
		exit (1);
	}
	defaultMap = curCharMaps->defCharMap[mapIndex];
	if (macroMaps != NULL)
		overrideMap = macroMaps[mapIndex];
}


/*
	Figure out what string a special (non-ASCII) character should map
	to.  (Determine troff equivalent using "normal" characters.)
*/

static char *_CharMapping (c, map)
int	c;
CharMap	*map;
{
CharMap	*cmp;
char	*p = NULL;

	for (cmp = map; cmp->charStr != NULL; cmp++)
	{
		if (c == cmp->charVal)
		{
			p = cmp->charStr;
			break;
		}
	}
	return (p);
}


char *CharMapping (c)
int	c;
{
CharMap	*cmp;
char	*p = NULL;

	if (overrideMap != NULL)
		p = _CharMapping (c, overrideMap);
	if (p == NULL && defaultMap != NULL)
		p = _CharMapping (c, defaultMap);
	return (p != NULL ? p : "<<UNKNOWN>>");
}