]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * $RCSId: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.3 2002/08/22 07:36:43 keithp Exp $ | |
3 | * | |
4 | * Copyright © 2002 Keith Packard | |
5 | * | |
6 | * Permission to use, copy, modify, distribute, and sell this software and its | |
7 | * documentation for any purpose is hereby granted without fee, provided that | |
8 | * the above copyright notice appear in all copies and that both that | |
9 | * copyright notice and this permission notice appear in supporting | |
10 | * documentation, and that the name of Keith Packard not be used in | |
11 | * advertising or publicity pertaining to distribution of the software without | |
12 | * specific, written prior permission. Keith Packard makes no | |
13 | * representations about the suitability of this software for any purpose. It | |
14 | * is provided "as is" without express or implied warranty. | |
15 | * | |
16 | * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, | |
17 | * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO | |
18 | * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR | |
19 | * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, | |
20 | * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER | |
21 | * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR | |
22 | * PERFORMANCE OF THIS SOFTWARE. | |
23 | */ | |
24 | ||
25 | #include "fcint.h" | |
26 | #include "fccharset.c" | |
27 | #include "fcstr.c" | |
28 | ||
29 | /* | |
30 | * fc-lang | |
31 | * | |
32 | * Read a set of language orthographies and build C declarations for | |
33 | * charsets which can then be used to identify which languages are | |
34 | * supported by a given font. Note that this uses some utilities | |
35 | * from the fontconfig library, so the necessary file is simply | |
36 | * included in this compilation. A couple of extra utility | |
37 | * functions are also needed in slightly modified form | |
38 | */ | |
39 | ||
40 | const FcChar16 langBankNumbers[1]; /* place holders so that externs resolve */ | |
41 | const FcCharLeaf langBankLeaves[1]; | |
42 | const int langBankLeafIdx[1]; | |
43 | ||
44 | void | |
45 | FcMemAlloc (int kind, int size) | |
46 | { | |
47 | } | |
48 | ||
49 | void | |
50 | FcMemFree (int kind, int size) | |
51 | { | |
52 | } | |
53 | ||
54 | int* _fcBankId = 0; | |
55 | int* _fcBankIdx = 0; | |
56 | ||
57 | int | |
58 | FcCacheBankToIndexMTF (int bank) | |
59 | { | |
60 | return -1; | |
61 | } | |
62 | ||
63 | FcChar8 * | |
64 | FcConfigHome (void) | |
65 | { | |
66 | return (FcChar8 *) getenv ("HOME"); | |
67 | } | |
68 | ||
69 | static void | |
70 | fatal (const char *file, int lineno, const char *msg) | |
71 | { | |
72 | if (lineno) | |
73 | fprintf (stderr, "%s:%d: %s\n", file, lineno, msg); | |
74 | else | |
75 | fprintf (stderr, "%s: %s\n", file, msg); | |
76 | exit (1); | |
77 | } | |
78 | ||
79 | static char * | |
80 | get_line (FILE *f, char *line, int *lineno) | |
81 | { | |
82 | char *hash; | |
83 | if (!fgets (line, 1024, f)) | |
84 | return 0; | |
85 | ++(*lineno); | |
86 | hash = strchr (line, '#'); | |
87 | if (hash) | |
88 | *hash = '\0'; | |
89 | if (line[0] == '\0' || line[0] == '\n' || line[0] == '\032' || line[0] == '\r') | |
90 | return get_line (f, line, lineno); | |
91 | return line; | |
92 | } | |
93 | ||
94 | char *dir = 0; | |
95 | ||
96 | static FILE * | |
97 | scanopen (char *file) | |
98 | { | |
99 | FILE *f; | |
100 | ||
101 | f = fopen (file, "r"); | |
102 | if (!f && dir) | |
103 | { | |
104 | char path[1024]; | |
105 | ||
106 | strcpy (path, dir); | |
107 | strcat (path, "/"); | |
108 | strcat (path, file); | |
109 | f = fopen (path, "r"); | |
110 | } | |
111 | return f; | |
112 | } | |
113 | ||
114 | /* | |
115 | * build a single charset from a source file | |
116 | * | |
117 | * The file format is quite simple, either | |
118 | * a single hex value or a pair separated with a dash | |
119 | * | |
120 | * Comments begin with '#' | |
121 | */ | |
122 | ||
123 | static FcCharSet * | |
124 | scan (FILE *f, char *file) | |
125 | { | |
126 | FcCharSet *c = 0; | |
127 | FcCharSet *n; | |
128 | int start, end, ucs4; | |
129 | char line[1024]; | |
130 | int lineno = 0; | |
131 | ||
132 | while (get_line (f, line, &lineno)) | |
133 | { | |
134 | if (!strncmp (line, "include", 7)) | |
135 | { | |
136 | file = strchr (line, ' '); | |
137 | while (*file == ' ') | |
138 | file++; | |
139 | end = strlen (file); | |
140 | if (file[end-1] == '\n') | |
141 | file[end-1] = '\0'; | |
142 | f = scanopen (file); | |
143 | if (!f) | |
144 | fatal (file, 0, "can't open"); | |
145 | c = scan (f, file); | |
146 | fclose (f); | |
147 | return c; | |
148 | } | |
149 | if (strchr (line, '-')) | |
150 | { | |
151 | if (sscanf (line, "%x-%x", &start, &end) != 2) | |
152 | fatal (file, lineno, "parse error"); | |
153 | } | |
154 | else | |
155 | { | |
156 | if (sscanf (line, "%x", &start) != 1) | |
157 | fatal (file, lineno, "parse error"); | |
158 | end = start; | |
159 | } | |
160 | if (!c) | |
161 | c = FcCharSetCreate (); | |
162 | for (ucs4 = start; ucs4 <= end; ucs4++) | |
163 | { | |
164 | if (!FcCharSetAddChar (c, ucs4)) | |
165 | fatal (file, lineno, "out of memory"); | |
166 | } | |
167 | } | |
168 | n = FcCharSetFreeze (c); | |
169 | FcCharSetDestroy (c); | |
170 | return n; | |
171 | } | |
172 | ||
173 | /* | |
174 | * Convert a file name into a name suitable for C declarations | |
175 | */ | |
176 | static char * | |
177 | get_name (char *file) | |
178 | { | |
179 | char *name; | |
180 | char *dot; | |
181 | ||
182 | dot = strchr (file, '.'); | |
183 | if (!dot) | |
184 | dot = file + strlen(file); | |
185 | name = malloc (dot - file + 1); | |
186 | strncpy (name, file, dot - file); | |
187 | name[dot-file] = '\0'; | |
188 | return name; | |
189 | } | |
190 | ||
191 | /* | |
192 | * Convert a C name into a language name | |
193 | */ | |
194 | static char * | |
195 | get_lang (char *name) | |
196 | { | |
197 | char *lang = malloc (strlen (name) + 1); | |
198 | char *l = lang; | |
199 | char c; | |
200 | ||
201 | while ((c = *name++)) | |
202 | { | |
203 | if (isupper ((int) (unsigned char) c)) | |
204 | c = tolower ((int) (unsigned char) c); | |
205 | if (c == '_') | |
206 | c = '-'; | |
207 | if (c == ' ') | |
208 | continue; | |
209 | *l++ = c; | |
210 | } | |
211 | *l++ = '\0'; | |
212 | return lang; | |
213 | } | |
214 | ||
215 | static int compare (const void *a, const void *b) | |
216 | { | |
217 | const FcChar8 *const *as = a, *const *bs = b; | |
218 | return FcStrCmpIgnoreCase (*as, *bs); | |
219 | } | |
220 | ||
221 | #define MAX_LANG 1024 | |
222 | #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32) | |
223 | ||
224 | #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f))) | |
225 | #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1) | |
226 | ||
227 | int | |
228 | main (int argc, char **argv) | |
229 | { | |
230 | char *files[MAX_LANG]; | |
231 | FcCharSet *sets[MAX_LANG]; | |
232 | int duplicate[MAX_LANG]; | |
233 | int offsets[MAX_LANG]; | |
234 | int country[MAX_LANG]; | |
235 | char *names[MAX_LANG]; | |
236 | char *langs[MAX_LANG]; | |
237 | FILE *f; | |
238 | int offset = 0; | |
239 | int ncountry = 0; | |
240 | int i = 0; | |
241 | int argi; | |
242 | FcCharLeaf **leaves; | |
243 | int total_leaves = 0; | |
244 | int offset_count = 0; | |
245 | int l, sl, tl; | |
246 | int c; | |
247 | char line[1024]; | |
248 | FcChar32 map[MAX_LANG_SET_MAP]; | |
249 | int num_lang_set_map; | |
250 | int setRangeStart[26]; | |
251 | int setRangeEnd[26]; | |
252 | FcChar8 setRangeChar; | |
253 | ||
254 | argi = 1; | |
255 | while (argv[argi]) | |
256 | { | |
257 | if (!strcmp (argv[argi], "-d")) | |
258 | { | |
259 | argi++; | |
260 | dir = argv[argi++]; | |
261 | continue; | |
262 | } | |
263 | if (i == MAX_LANG) | |
264 | fatal (argv[0], 0, "Too many languages"); | |
265 | files[i++] = argv[argi++]; | |
266 | } | |
267 | files[i] = 0; | |
268 | qsort (files, i, sizeof (char *), compare); | |
269 | i = 0; | |
270 | while (files[i]) | |
271 | { | |
272 | f = scanopen (files[i]); | |
273 | if (!f) | |
274 | fatal (files[i], 0, strerror (errno)); | |
275 | sets[i] = scan (f, files[i]); | |
276 | names[i] = get_name (files[i]); | |
277 | langs[i] = get_lang(names[i]); | |
278 | if (strchr (langs[i], '-')) | |
279 | country[ncountry++] = i; | |
280 | ||
281 | total_leaves += sets[i]->num; | |
282 | i++; | |
283 | fclose (f); | |
284 | } | |
285 | sets[i] = 0; | |
286 | leaves = malloc (total_leaves * sizeof (FcCharLeaf *)); | |
287 | tl = 0; | |
288 | /* | |
289 | * Find unique leaves | |
290 | */ | |
291 | for (i = 0; sets[i]; i++) | |
292 | { | |
293 | for (sl = 0; sl < sets[i]->num; sl++) | |
294 | { | |
295 | for (l = 0; l < tl; l++) | |
296 | if (leaves[l] == FcCharSetGetLeaf(sets[i], sl)) | |
297 | break; | |
298 | if (l == tl) | |
299 | leaves[tl++] = FcCharSetGetLeaf(sets[i], sl); | |
300 | } | |
301 | } | |
302 | ||
303 | /* | |
304 | * Scan the input until the marker is found | |
305 | */ | |
306 | ||
307 | while (fgets (line, sizeof (line), stdin)) | |
308 | { | |
309 | if (!strncmp (line, "@@@", 3)) | |
310 | break; | |
311 | fputs (line, stdout); | |
312 | } | |
313 | ||
314 | printf ("/* total size: %d unique leaves: %d */\n\n", | |
315 | total_leaves, tl); | |
316 | /* | |
317 | * Dump leaves | |
318 | */ | |
319 | printf ("const FcCharLeaf langBankLeaves[%d] = {\n", tl); | |
320 | for (l = 0; l < tl; l++) | |
321 | { | |
322 | printf (" { { /* %d */", l); | |
323 | for (i = 0; i < 256/32; i++) | |
324 | { | |
325 | if (i % 4 == 0) | |
326 | printf ("\n "); | |
327 | printf (" 0x%08x,", leaves[l]->map[i]); | |
328 | } | |
329 | printf ("\n } },\n"); | |
330 | } | |
331 | printf ("};\n\n"); | |
332 | ||
333 | /* | |
334 | * Find duplicate charsets | |
335 | */ | |
336 | duplicate[0] = -1; | |
337 | for (i = 1; sets[i]; i++) | |
338 | { | |
339 | int j; | |
340 | ||
341 | duplicate[i] = -1; | |
342 | for (j = 0; j < i; j++) | |
343 | if (sets[j] == sets[i]) | |
344 | { | |
345 | duplicate[i] = j; | |
346 | break; | |
347 | } | |
348 | } | |
349 | ||
350 | /* | |
351 | * Find ranges for each letter for faster searching | |
352 | */ | |
353 | setRangeChar = 'a'; | |
354 | for (i = 0; sets[i]; i++) | |
355 | { | |
356 | char c = names[i][0]; | |
357 | ||
358 | while (setRangeChar <= c && c <= 'z') | |
359 | setRangeStart[setRangeChar++ - 'a'] = i; | |
360 | } | |
361 | for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++) | |
362 | setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1; | |
363 | setRangeEnd[setRangeChar - 'a'] = i - 1; | |
364 | ||
365 | /* | |
366 | * Dump arrays | |
367 | */ | |
368 | for (i = 0; sets[i]; i++) | |
369 | { | |
370 | int n; | |
371 | ||
372 | if (duplicate[i] >= 0) | |
373 | continue; | |
374 | ||
375 | for (n = 0; n < sets[i]->num; n++) | |
376 | { | |
377 | for (l = 0; l < tl; l++) | |
378 | if (leaves[l] == FcCharSetGetLeaf(sets[i], n)) | |
379 | break; | |
380 | if (l == tl) | |
381 | fatal (names[i], 0, "can't find leaf"); | |
382 | offset_count++; | |
383 | } | |
384 | offsets[i] = offset; | |
385 | offset += sets[i]->num; | |
386 | } | |
387 | ||
388 | printf ("const int langBankLeafIdx[%d] = {\n", | |
389 | offset_count); | |
390 | for (i = 0; sets[i]; i++) | |
391 | { | |
392 | int n; | |
393 | ||
394 | if (duplicate[i] >= 0) | |
395 | continue; | |
396 | for (n = 0; n < sets[i]->num; n++) | |
397 | { | |
398 | if (n % 8 == 0) | |
399 | printf (" "); | |
400 | for (l = 0; l < tl; l++) | |
401 | if (leaves[l] == FcCharSetGetLeaf(sets[i], n)) | |
402 | break; | |
403 | if (l == tl) | |
404 | fatal (names[i], 0, "can't find leaf"); | |
405 | printf (" %3d,", l); | |
406 | if (n % 8 == 7) | |
407 | printf ("\n"); | |
408 | } | |
409 | if (n % 8 != 0) | |
410 | printf ("\n"); | |
411 | } | |
412 | printf ("};\n\n"); | |
413 | ||
414 | printf ("const FcChar16 langBankNumbers[%d] = {\n", | |
415 | offset_count); | |
416 | ||
417 | for (i = 0; sets[i]; i++) | |
418 | { | |
419 | int n; | |
420 | ||
421 | if (duplicate[i] >= 0) | |
422 | continue; | |
423 | for (n = 0; n < sets[i]->num; n++) | |
424 | { | |
425 | if (n % 8 == 0) | |
426 | printf (" "); | |
427 | printf (" 0x%04x,", FcCharSetGetNumbers(sets[i])[n]); | |
428 | if (n % 8 == 7) | |
429 | printf ("\n"); | |
430 | } | |
431 | if (n % 8 != 0) | |
432 | printf ("\n"); | |
433 | } | |
434 | printf ("};\n\n"); | |
435 | ||
436 | /* | |
437 | * Dump sets | |
438 | */ | |
439 | ||
440 | printf ("const FcLangCharSet fcLangCharSets[] = {\n"); | |
441 | for (i = 0; sets[i]; i++) | |
442 | { | |
443 | int j = duplicate[i]; | |
444 | ||
445 | if (j < 0) | |
446 | j = i; | |
447 | ||
448 | printf (" { (FcChar8 *) \"%s\",\n" | |
449 | " { FC_REF_CONSTANT, %d, FC_BANK_LANGS, " | |
450 | "{ .stat = { %d, %d } } } }, /* %d */\n", | |
451 | langs[i], | |
452 | sets[j]->num, offsets[j], offsets[j], j); | |
453 | } | |
454 | printf ("};\n\n"); | |
455 | printf ("#define NUM_LANG_CHAR_SET %d\n", i); | |
456 | num_lang_set_map = (i + 31) / 32; | |
457 | printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map); | |
458 | /* | |
459 | * Dump indices with country codes | |
460 | */ | |
461 | if (ncountry) | |
462 | { | |
463 | int ncountry_ent = 0; | |
464 | printf ("\n"); | |
465 | printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n"); | |
466 | for (c = 0; c < ncountry; c++) | |
467 | { | |
468 | i = country[c]; | |
469 | if (i >= 0) | |
470 | { | |
471 | int l = strchr (langs[i], '-') - langs[i]; | |
472 | int d, k; | |
473 | ||
474 | for (k = 0; k < num_lang_set_map; k++) | |
475 | map[k] = 0; | |
476 | ||
477 | BitSet (map, i); | |
478 | for (d = c + 1; d < ncountry; d++) | |
479 | { | |
480 | int j = country[d]; | |
481 | if (j >= 0 && !strncmp (langs[j], langs[i], l)) | |
482 | { | |
483 | BitSet(map, j); | |
484 | country[d] = -1; | |
485 | } | |
486 | } | |
487 | printf (" {"); | |
488 | for (k = 0; k < num_lang_set_map; k++) | |
489 | printf (" 0x%08x,", map[k]); | |
490 | printf (" }, /* %*.*s */\n", | |
491 | l, l, langs[i]); | |
492 | ++ncountry_ent; | |
493 | } | |
494 | } | |
495 | printf ("};\n\n"); | |
496 | printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent); | |
497 | } | |
498 | ||
499 | ||
500 | /* | |
501 | * Dump sets start/finish for the fastpath | |
502 | */ | |
503 | printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n"); | |
504 | for (setRangeChar = 'a'; setRangeChar <= 'z' ; setRangeChar++) | |
505 | { | |
506 | printf (" { %d, %d }, /* %c */\n", | |
507 | setRangeStart[setRangeChar - 'a'], | |
508 | setRangeEnd[setRangeChar - 'a'], setRangeChar); | |
509 | } | |
510 | printf ("};\n\n"); | |
511 | ||
512 | while (fgets (line, sizeof (line), stdin)) | |
513 | fputs (line, stdout); | |
514 | ||
515 | fflush (stdout); | |
516 | exit (ferror (stdout)); | |
517 | } |