PageRenderTime 35ms CodeModel.GetById 1ms app.highlight 28ms RepoModel.GetById 1ms app.codeStats 0ms

/contrib/groff/src/libs/libxutil/DviChar.c

https://bitbucket.org/freebsd/freebsd-head/
C | 658 lines | 632 code | 20 blank | 6 comment | 17 complexity | 165d23c9812a6a998573f42f6c541eaa MD5 | raw file
  1/*
  2 * DviChar.c
  3 *
  4 * Map DVI (ditroff output) character names to
  5 * font indexes and back
  6 */
  7
  8#include <stdlib.h>
  9#include <string.h>
 10#include "DviChar.h"
 11
 12extern char *xmalloc(int);
 13
 14#define allocHash() ((DviCharNameHash *) xmalloc (sizeof (DviCharNameHash)))
 15
 16struct map_list {
 17	struct map_list	*next;
 18	DviCharNameMap	*map;
 19};
 20
 21static struct map_list	*world;
 22
 23static int	standard_maps_loaded = 0;
 24static void	load_standard_maps (void);
 25static int	hash_name (const char *);
 26static void	dispose_hash(DviCharNameMap *);
 27static void	compute_hash(DviCharNameMap *);
 28
 29DviCharNameMap *
 30DviFindMap (char *encoding)
 31{
 32	struct map_list	*m;
 33
 34	if (!standard_maps_loaded)
 35		load_standard_maps ();
 36	for (m = world; m; m=m->next)
 37		if (!strcmp (m->map->encoding, encoding))
 38			return m->map;
 39	return 0;
 40}
 41
 42void
 43DviRegisterMap (DviCharNameMap *map)
 44{
 45	struct map_list	*m;
 46
 47	if (!standard_maps_loaded)
 48		load_standard_maps ();
 49	for (m = world; m; m = m->next)
 50		if (!strcmp (m->map->encoding, map->encoding))
 51			break;
 52	if (!m) {
 53		m = (struct map_list *) xmalloc (sizeof *m);
 54		m->next = world;
 55		world = m;
 56	}
 57	dispose_hash (map);
 58	m->map = map;
 59	compute_hash (map);
 60}
 61
 62static void
 63dispose_hash (DviCharNameMap *map)
 64{
 65	DviCharNameHash	**buckets;
 66	DviCharNameHash	*h, *next;
 67	int		i;
 68
 69	buckets = map->buckets;
 70	for (i = 0; i < DVI_HASH_SIZE; i++) {
 71		for (h = buckets[i]; h; h=next) {
 72			next = h->next;
 73			free (h);
 74		}
 75	}
 76}
 77
 78static int
 79hash_name (const char *name)
 80{
 81	int	i = 0;
 82
 83	while (*name)
 84		i = (i << 1) ^ *name++;
 85	if (i < 0)
 86		i = -i;
 87	return i;
 88}
 89
 90static void
 91compute_hash (DviCharNameMap *map)
 92{
 93	DviCharNameHash	**buckets;
 94	int		c, s, i;
 95	DviCharNameHash	*h;
 96
 97	buckets = map->buckets;
 98	for (i = 0; i < DVI_HASH_SIZE; i++)
 99		buckets[i] = 0;
100	for (c = 0; c < DVI_MAP_SIZE; c++)
101		for (s = 0; s < DVI_MAX_SYNONYMS; s++) {
102			if (!map->dvi_names[c][s])
103				break;
104			i = hash_name (map->dvi_names[c][s]) % DVI_HASH_SIZE;
105			h = allocHash ();
106			h->next = buckets[i];
107			buckets[i] = h;
108			h->name = map->dvi_names[c][s];
109			h->position = c;
110		}
111	
112}
113
114int
115DviCharIndex (DviCharNameMap *map, const char *name)
116{
117	int		i;
118	DviCharNameHash	*h;
119
120	i = hash_name (name) % DVI_HASH_SIZE;
121	for (h = map->buckets[i]; h; h=h->next)
122		if (!strcmp (h->name, name))
123			return h->position;
124	return -1;
125}
126
127static DviCharNameMap ISO8859_1_map = {
128	"iso8859-1",
129	0,
130{
131{	0,			/* 0 */},
132{	0,			/* 1 */},
133{	0,			/* 2 */},
134{	0,			/* 3 */},
135{	0,			/* 4 */},
136{	0,			/* 5 */},
137{	0,			/* 6 */},
138{	0,			/* 7 */},
139{	0,			/* 8 */},
140{	0,			/* 9 */},
141{	0,			/* 10 */},
142{	0,			/* 11 */},
143{	0,			/* 12 */},
144{	0,			/* 13 */},
145{	0,			/* 14 */},
146{	0,			/* 15 */},
147{	0,			/* 16 */},
148{	0,			/* 17 */},
149{	0,			/* 18 */},
150{	0,			/* 19 */},
151{	0,			/* 20 */},
152{	0,			/* 21 */},
153{	0,			/* 22 */},
154{	0,			/* 23 */},
155{	0,			/* 24 */},
156{	0,			/* 25 */},
157{	0,			/* 26 */},
158{	0,			/* 27 */},
159{	0,			/* 28 */},
160{	0,			/* 29 */},
161{	0,			/* 30 */},
162{	0,			/* 31 */},
163{	0,			/* 32 */},
164{	"!",			/* 33 */},
165{	"\"", "dq",		/* 34 */},
166{	"#", "sh",		/* 35 */},
167{	"$", "Do",		/* 36 */},
168{	"%",			/* 37 */},
169{	"&",			/* 38 */},
170{	"'", "cq",		/* 39 */},
171{	"(",			/* 40 */},
172{	")",			/* 41 */},
173{	"*",			/* 42 */},
174{	"+",			/* 43 */},
175{	",",			/* 44 */},
176{	"\\-",			/* 45 */},
177{	".",			/* 46 */},
178{	"/", "sl",		/* 47 */},
179{	"0",			/* 48 */},
180{	"1",			/* 49 */},
181{	"2",			/* 50 */},
182{	"3",			/* 51 */},
183{	"4",			/* 52 */},
184{	"5",			/* 53 */},
185{	"6",			/* 54 */},
186{	"7",			/* 55 */},
187{	"8",			/* 56 */},
188{	"9",			/* 57 */},
189{	":",			/* 58 */},
190{	";",			/* 59 */},
191{	"<",			/* 60 */},
192{	"=",			/* 61 */},
193{	">",			/* 62 */},
194{	"?",			/* 63 */},
195{	"@", "at",		/* 64 */},
196{	"A",			/* 65 */},
197{	"B",			/* 66 */},
198{	"C",			/* 67 */},
199{	"D",			/* 68 */},
200{	"E",			/* 69 */},
201{	"F",			/* 70 */},
202{	"G",			/* 71 */},
203{	"H",			/* 72 */},
204{	"I",			/* 73 */},
205{	"J",			/* 74 */},
206{	"K",			/* 75 */},
207{	"L",			/* 76 */},
208{	"M",			/* 77 */},
209{	"N",			/* 78 */},
210{	"O",			/* 79 */},
211{	"P",			/* 80 */},
212{	"Q",			/* 81 */},
213{	"R",			/* 82 */},
214{	"S",			/* 83 */},
215{	"T",			/* 84 */},
216{	"U",			/* 85 */},
217{	"V",			/* 86 */},
218{	"W",			/* 87 */},
219{	"X",			/* 88 */},
220{	"Y",			/* 89 */},
221{	"Z",			/* 90 */},
222{	"[", "lB",		/* 91 */},
223{	"\\", "rs",		/* 92 */},
224{	"]", "rB",		/* 93 */},
225{	"^", "a^", "ha",	/* 94 */},
226{	"_",			/* 95 */},
227{	"`", "oq",		/* 96 */},
228{	"a",			/* 97 */},
229{	"b",			/* 98 */},
230{	"c",			/* 99 */},
231{	"d",			/* 100 */},
232{	"e",			/* 101 */},
233{	"f",			/* 102 */},
234{	"g",			/* 103 */},
235{	"h",			/* 104 */},
236{	"i",			/* 105 */},
237{	"j",			/* 106 */},
238{	"k",			/* 107 */},
239{	"l",			/* 108 */},
240{	"m",			/* 109 */},
241{	"n",			/* 110 */},
242{	"o",			/* 111 */},
243{	"p",			/* 112 */},
244{	"q",			/* 113 */},
245{	"r",			/* 114 */},
246{	"s",			/* 115 */},
247{	"t",			/* 116 */},
248{	"u",			/* 117 */},
249{	"v",			/* 118 */},
250{	"w",			/* 119 */},
251{	"x",			/* 120 */},
252{	"y",			/* 121 */},
253{	"z",			/* 122 */},
254{	"{", "lC",		/* 123 */},
255{	"|", "ba",		/* 124 */},
256{	"}", "rC",		/* 125 */},
257{	"~", "a~", "ti",	/* 126 */},
258{	0,			/* 127 */},
259{	0,			/* 128 */},
260{	0,			/* 129 */},
261{	0,			/* 130 */},
262{	0,			/* 131 */},
263{	0,			/* 132 */},
264{	0,			/* 133 */},
265{	0,			/* 134 */},
266{	0,			/* 135 */},
267{	0,			/* 136 */},
268{	0,			/* 137 */},
269{	0,			/* 138 */},
270{	0,			/* 139 */},
271{	0,			/* 140 */},
272{	0,			/* 141 */},
273{	0,			/* 142 */},
274{	0,			/* 143 */},
275{	0,			/* 144 */},
276{	0,			/* 145 */},
277{	0,			/* 146 */},
278{	0,			/* 147 */},
279{	0,			/* 148 */},
280{	0,			/* 149 */},
281{	0,			/* 150 */},
282{	0,			/* 151 */},
283{	0,			/* 152 */},
284{	0,			/* 153 */},
285{	0,			/* 154 */},
286{	0,			/* 155 */},
287{	0,			/* 156 */},
288{	0,			/* 157 */},
289{	0,			/* 158 */},
290{	0,			/* 159 */},
291{	0,			/* 160 */},
292{	"r!",			/* 161 */},
293{	"ct",			/* 162 */},
294{	"Po",			/* 163 */},
295{	"Cs",			/* 164 */},
296{	"Ye",			/* 165 */},
297{	"bb",			/* 166 */},
298{	"sc",			/* 167 */},
299{	"ad",			/* 168 */},
300{	"co",			/* 169 */},
301{	"Of",			/* 170 */},
302{	"Fo",			/* 171 */},
303{	"tno",			/* 172 */},
304{	"-", "hy",		/* 173 */},
305{	"rg",			/* 174 */},
306{	"a-",			/* 175 */},
307{	"de",			/* 176 */},
308{	"t+-",			/* 177 */},
309{	"S2",			/* 178 */},
310{	"S3",			/* 179 */},
311{	"aa",			/* 180 */},
312{	"mc",			/* 181 */},
313{	"ps",			/* 182 */},
314{	"pc",			/* 183 */},
315{	"ac",			/* 184 */},
316{	"S1",			/* 185 */},
317{	"Om",			/* 186 */},
318{	"Fc",			/* 187 */},
319{	"14",			/* 188 */},
320{	"12",			/* 189 */},
321{	"34",			/* 190 */},
322{	"r?",			/* 191 */},
323{	"`A",			/* 192 */},
324{	"'A",			/* 193 */},
325{	"^A",			/* 194 */},
326{	"~A",			/* 195 */},
327{	":A",			/* 196 */},
328{	"oA",			/* 197 */},
329{	"AE",			/* 198 */},
330{	",C",			/* 199 */},
331{	"`E",			/* 200 */},
332{	"'E",			/* 201 */},
333{	"^E",			/* 202 */},
334{	":E",			/* 203 */},
335{	"`I",			/* 204 */},
336{	"'I",			/* 205 */},
337{	"^I",			/* 206 */},
338{	":I",			/* 207 */},
339{	"-D",			/* 208 */},
340{	"~N",			/* 209 */},
341{	"`O",			/* 210 */},
342{	"'O",			/* 211 */},
343{	"^O",			/* 212 */},
344{	"~O",			/* 213 */},
345{	":O",			/* 214 */},
346{	"tmu",			/* 215 */},
347{	"/O",			/* 216 */},
348{	"`U",			/* 217 */},
349{	"'U",			/* 218 */},
350{	"^U",			/* 219 */},
351{	":U",			/* 220 */},
352{	"'Y",			/* 221 */},
353{	"TP",			/* 222 */},
354{	"ss",			/* 223 */},
355{	"`a",			/* 224 */},
356{	"'a",			/* 225 */},
357{	"^a",			/* 226 */},
358{	"~a",			/* 227 */},
359{	":a",			/* 228 */},
360{	"oa",			/* 229 */},
361{	"ae",			/* 230 */},
362{	",c",			/* 231 */},
363{	"`e",			/* 232 */},
364{	"'e",			/* 233 */},
365{	"^e",			/* 234 */},
366{	":e",			/* 235 */},
367{	"`i",			/* 236 */},
368{	"'i",			/* 237 */},
369{	"^i",			/* 238 */},
370{	":i",			/* 239 */},
371{	"Sd",			/* 240 */},
372{	"~n",			/* 241 */},
373{	"`o",			/* 242 */},
374{	"'o",			/* 243 */},
375{	"^o",			/* 244 */},
376{	"~o",			/* 245 */},
377{	":o",			/* 246 */},
378{	"tdi",			/* 247 */},
379{	"/o",			/* 248 */},
380{	"`u",			/* 249 */},
381{	"'u",			/* 250 */},
382{	"^u",			/* 251 */},
383{	":u",			/* 252 */},
384{	"'y",			/* 253 */},
385{	"Tp",			/* 254 */},
386{	":y",			/* 255 */},
387}};
388
389static DviCharNameMap Adobe_Symbol_map = {
390	"adobe-fontspecific",
391	1,
392{
393{	0,						/* 0 */},
394{	0,						/* 1 */},
395{	0,						/* 2 */},
396{	0,						/* 3 */},
397{	0,						/* 4 */},
398{	0,						/* 5 */},
399{	0,						/* 6 */},
400{	0,						/* 7 */},
401{	0,						/* 8 */},
402{	0,						/* 9 */},
403{	0,						/* 10 */},
404{	0,						/* 11 */},
405{	0,						/* 12 */},
406{	0,						/* 13 */},
407{	0,						/* 14 */},
408{	0,						/* 15 */},
409{	0,						/* 16 */},
410{	0,						/* 17 */},
411{	0,						/* 18 */},
412{	0,						/* 19 */},
413{	0,						/* 20 */},
414{	0,						/* 21 */},
415{	0,						/* 22 */},
416{	0,						/* 23 */},
417{	0,						/* 24 */},
418{	0,						/* 25 */},
419{	0,						/* 26 */},
420{	0,						/* 27 */},
421{	0,						/* 28 */},
422{	0,						/* 29 */},
423{	0,						/* 30 */},
424{	0,						/* 31 */},
425{	0,						/* 32 */},
426{	"!",						/* 33 */},
427{	"fa",						/* 34 */},
428{	"#", "sh",					/* 35 */},
429{	"te",						/* 36 */},
430{	"%",						/* 37 */},
431{	"&",						/* 38 */},
432{	"st",						/* 39 */},
433{	"(",						/* 40 */},
434{	")",						/* 41 */},
435{	"**",						/* 42 */},
436{	"+", "pl",					/* 43 */},
437{	",",						/* 44 */},
438{	"\\-", "mi",					/* 45 */},
439{	".",						/* 46 */},
440{	"/", "sl",					/* 47 */},
441{	"0",						/* 48 */},
442{	"1",						/* 49 */},
443{	"2",						/* 50 */},
444{	"3",						/* 51 */},
445{	"4",						/* 52 */},
446{	"5",						/* 53 */},
447{	"6",						/* 54 */},
448{	"7",						/* 55 */},
449{	"8",						/* 56 */},
450{	"9",						/* 57 */},
451{	":",						/* 58 */},
452{	";",						/* 59 */},
453{	"<",						/* 60 */},
454{	"=", "eq",					/* 61 */},
455{	">",						/* 62 */},
456{	"?",						/* 63 */},
457{	"=~",						/* 64 */},
458{	"*A",						/* 65 */},
459{	"*B",						/* 66 */},
460{	"*X",						/* 67 */},
461{	"*D",						/* 68 */},
462{	"*E",						/* 69 */},
463{	"*F",						/* 70 */},
464{	"*G",						/* 71 */},
465{	"*Y",						/* 72 */},
466{	"*I",						/* 73 */},
467{	"+h",						/* 74 */},
468{	"*K",						/* 75 */},
469{	"*L",						/* 76 */},
470{	"*M",						/* 77 */},
471{	"*N",						/* 78 */},
472{	"*O",						/* 79 */},
473{	"*P",						/* 80 */},
474{	"*H",						/* 81 */},
475{	"*R",						/* 82 */},
476{	"*S",						/* 83 */},
477{	"*T",						/* 84 */},
478{	0,						/* 85 */},
479{	"ts",						/* 86 */},
480{	"*W",						/* 87 */},
481{	"*C",						/* 88 */},
482{	"*Q",						/* 89 */},
483{	"*Z",						/* 90 */},
484{	"[", "lB",					/* 91 */},
485{	"tf", "3d",					/* 92 */},
486{	"]", "rB",					/* 93 */},
487{	"pp",						/* 94 */},
488{	"_",						/* 95 */},
489{	"radicalex",					/* 96 */},
490{	"*a",						/* 97 */},
491{	"*b",						/* 98 */},
492{	"*x",						/* 99 */},
493{	"*d",						/* 100 */},
494{	"*e",						/* 101 */},
495{	"*f",						/* 102 */},
496{	"*g",						/* 103 */},
497{	"*y",						/* 104 */},
498{	"*i",						/* 105 */},
499{	"+f",						/* 106 */},
500{	"*k",						/* 107 */},
501{	"*l",						/* 108 */},
502{	"*m",						/* 109 */},
503{	"*n",						/* 110 */},
504{	"*o",						/* 111 */},
505{	"*p",						/* 112 */},
506{	"*h",						/* 113 */},
507{	"*r",						/* 114 */},
508{	"*s",						/* 115 */},
509{	"*t",						/* 116 */},
510{	"*u",						/* 117 */},
511{	"+p",						/* 118 */},
512{	"*w",						/* 119 */},
513{	"*c",						/* 120 */},
514{	"*q",						/* 121 */},
515{	"*z",						/* 122 */},
516{	"lC", "{",					/* 123 */},
517{	"ba", "|",					/* 124 */},
518{	"rC", "}",					/* 125 */},
519{	"ap",						/* 126 */},
520{	0,						/* 127 */},
521{	0,						/* 128 */},
522{	0,						/* 129 */},
523{	0,						/* 130 */},
524{	0,						/* 131 */},
525{	0,						/* 132 */},
526{	0,						/* 133 */},
527{	0,						/* 134 */},
528{	0,						/* 135 */},
529{	0,						/* 136 */},
530{	0,						/* 137 */},
531{	0,						/* 138 */},
532{	0,						/* 139 */},
533{	0,						/* 140 */},
534{	0,						/* 141 */},
535{	0,						/* 142 */},
536{	0,						/* 143 */},
537{	0,						/* 144 */},
538{	0,						/* 145 */},
539{	0,						/* 146 */},
540{	0,						/* 147 */},
541{	0,						/* 148 */},
542{	0,						/* 149 */},
543{	0,						/* 150 */},
544{	0,						/* 151 */},
545{	0,						/* 152 */},
546{	0,						/* 153 */},
547{	0,						/* 154 */},
548{	0,						/* 155 */},
549{	0,						/* 156 */},
550{	0,						/* 157 */},
551{	0,						/* 158 */},
552{	0,						/* 159 */},
553{	0,						/* 160 */},
554{	"*U",						/* 161 */},
555{	"fm",						/* 162 */},
556{	"<=",						/* 163 */},
557{	"f/",						/* 164 */},
558{	"if",						/* 165 */},
559{	"Fn",						/* 166 */},
560{	"CL",						/* 167 */},
561{	"DI",						/* 168 */},
562{	"HE",						/* 169 */},
563{	"SP",						/* 170 */},
564{	"<>",						/* 171 */},
565{	"<-",						/* 172 */},
566{	"ua", "arrowverttp",				/* 173 */},
567{	"->",						/* 174 */},
568{	"da", "arrowvertbt",				/* 175 */},
569{	"de",						/* 176 */},
570{	"+-",						/* 177 */},
571{	"sd",						/* 178 */},
572{	">=",						/* 179 */},
573{	"mu",						/* 180 */},
574{	"pt",						/* 181 */},
575{	"pd",						/* 182 */},
576{	"bu",						/* 183 */},
577{	"di",						/* 184 */},
578{	"!=",						/* 185 */},
579{	"==",						/* 186 */},
580{	"~=", "~~",					/* 187 */},
581{	0,						/* 188 */},
582{	"arrowvertex",					/* 189 */},
583{	"an",						/* 190 */},
584{	"CR",						/* 191 */},
585{	"Ah",						/* 192 */},
586{	"Im",						/* 193 */},
587{	"Re",						/* 194 */},
588{	"wp",						/* 195 */},
589{	"c*",						/* 196 */},
590{	"c+",						/* 197 */},
591{	"es",						/* 198 */},
592{	"ca",						/* 199 */},
593{	"cu",						/* 200 */},
594{	"sp",						/* 201 */},
595{	"ip",						/* 202 */},
596{	"nb",						/* 203 */},
597{	"sb",						/* 204 */},
598{	"ib",						/* 205 */},
599{	"mo",						/* 206 */},
600{	"nm",						/* 207 */},
601{	"/_",						/* 208 */},
602{	"gr",						/* 209 */},
603{	"rg",						/* 210 */},
604{	"co",						/* 211 */},
605{	"tm",						/* 212 */},
606{	0,						/* 213 */},
607{	"sr", "sqrt",					/* 214 */},
608{	"md",						/* 215 */},
609{	"no",						/* 216 */},
610{	"AN",						/* 217 */},
611{	"OR",						/* 218 */},
612{	"hA",						/* 219 */},
613{	"lA",						/* 220 */},
614{	"uA",						/* 221 */},
615{	"rA",						/* 222 */},
616{	"dA",						/* 223 */},
617{	"lz",						/* 224 */},
618{	"la",						/* 225 */},
619{	0,						/* 226 */},
620{	0,						/* 227 */},
621{	0,						/* 228 */},
622{	0,						/* 229 */},
623{	"parenlefttp",					/* 230 */},
624{	"parenleftex",					/* 231 */},
625{	"parenleftbt",					/* 232 */},
626{	"bracketlefttp", "lc",				/* 233 */},
627{	"bracketleftex",				/* 234 */},
628{	"bracketleftbt", "lf",				/* 235 */},
629{	"bracelefttp", "lt",				/* 236 */},
630{	"braceleftmid", "lk",				/* 237 */},
631{	"braceleftbt", "lb",				/* 238 */},
632{	"bracerightex", "braceleftex", "braceex", "bv",	/* 239 */},
633{	0,						/* 240 */},
634{	"ra",						/* 241 */},
635{	"is", "integral",				/* 242 */},
636{	0,						/* 243 */},
637{	0,						/* 244 */},
638{	0,						/* 245 */},
639{	"parenrighttp",					/* 246 */},
640{	"parenrightex",					/* 247 */},
641{	"parenrightbt",					/* 248 */},
642{	"bracketrighttp", "rc",				/* 249 */},
643{	"bracketrightex",				/* 250 */},
644{	"bracketrightbt", "rf",				/* 251 */},
645{	"bracerighttp", "rt",				/* 252 */},
646{	"bracerightmid", "rk",				/* 253 */},
647{	"bracerightbt", "rb",				/* 254 */},
648{	0,						/* 255 */},
649}};
650
651
652static void
653load_standard_maps (void)
654{
655	standard_maps_loaded = 1;
656	DviRegisterMap (&ISO8859_1_map);
657	DviRegisterMap (&Adobe_Symbol_map);
658}