]>
Commit | Line | Data |
---|---|---|
c1382a3d | 1 | /* |
0eadb052 | 2 | * $RCSId: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.3 2002/08/22 07:36:43 keithp Exp $ |
c1382a3d | 3 | * |
46b51147 | 4 | * Copyright © 2002 Keith Packard |
c1382a3d KP |
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" | |
c647f6f1 KP |
26 | #include "fccharset.c" |
27 | #include "fcstr.c" | |
c1382a3d KP |
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 | |
c647f6f1 KP |
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 | |
c1382a3d KP |
38 | */ |
39 | ||
a81f23c0 PL |
40 | const FcChar16 langBankNumbers[1]; /* place holders so that externs resolve */ |
41 | const FcCharLeaf langBankLeaves[1]; | |
42 | const int langBankLeafIdx[1]; | |
82f35f8b | 43 | |
c647f6f1 KP |
44 | void |
45 | FcMemAlloc (int kind, int size) | |
46 | { | |
47 | } | |
48 | ||
49 | void | |
50 | FcMemFree (int kind, int size) | |
51 | { | |
52 | } | |
53 | ||
b8948e85 PL |
54 | int* _fcBankId = 0; |
55 | int* _fcBankIdx = 0; | |
67ed0b72 PL |
56 | FcValueList ** _fcValueLists = 0; |
57 | FcPatternElt ** _fcPatternElts = 0; | |
58 | int FcDebugVal = 0; | |
b8948e85 | 59 | |
212c9f43 | 60 | int |
b8948e85 | 61 | FcCacheBankToIndexMTF (int bank) |
212c9f43 PL |
62 | { |
63 | return -1; | |
64 | } | |
65 | ||
ff3f1f98 KP |
66 | FcChar8 * |
67 | FcConfigHome (void) | |
68 | { | |
8245771d | 69 | return (FcChar8 *) getenv ("HOME"); |
ff3f1f98 KP |
70 | } |
71 | ||
c1382a3d | 72 | static void |
67accef4 | 73 | fatal (const char *file, int lineno, const char *msg) |
c1382a3d | 74 | { |
67accef4 PL |
75 | if (lineno) |
76 | fprintf (stderr, "%s:%d: %s\n", file, lineno, msg); | |
77 | else | |
c7beacf9 | 78 | fprintf (stderr, "%s: %s\n", file, msg); |
c1382a3d KP |
79 | exit (1); |
80 | } | |
81 | ||
82 | static char * | |
83 | get_line (FILE *f, char *line, int *lineno) | |
84 | { | |
85 | char *hash; | |
cf5cf4ca | 86 | int end; |
c1382a3d KP |
87 | if (!fgets (line, 1024, f)) |
88 | return 0; | |
89 | ++(*lineno); | |
90 | hash = strchr (line, '#'); | |
91 | if (hash) | |
92 | *hash = '\0'; | |
cf5cf4ca PL |
93 | |
94 | end = strlen (line); | |
95 | while (end > 0 && isspace (line[end-1])) | |
96 | line[--end] = '\0'; | |
97 | ||
c1382a3d KP |
98 | if (line[0] == '\0' || line[0] == '\n' || line[0] == '\032' || line[0] == '\r') |
99 | return get_line (f, line, lineno); | |
100 | return line; | |
101 | } | |
102 | ||
0d745819 | 103 | static char *dir = 0; |
394b2bf0 | 104 | |
6ae6acf3 | 105 | static FILE * |
394b2bf0 KP |
106 | scanopen (char *file) |
107 | { | |
108 | FILE *f; | |
109 | ||
110 | f = fopen (file, "r"); | |
111 | if (!f && dir) | |
112 | { | |
113 | char path[1024]; | |
114 | ||
115 | strcpy (path, dir); | |
116 | strcat (path, "/"); | |
117 | strcat (path, file); | |
118 | f = fopen (path, "r"); | |
119 | } | |
120 | return f; | |
121 | } | |
122 | ||
c1382a3d KP |
123 | /* |
124 | * build a single charset from a source file | |
125 | * | |
126 | * The file format is quite simple, either | |
127 | * a single hex value or a pair separated with a dash | |
128 | * | |
129 | * Comments begin with '#' | |
130 | */ | |
131 | ||
132 | static FcCharSet * | |
133 | scan (FILE *f, char *file) | |
134 | { | |
135 | FcCharSet *c = 0; | |
136 | FcCharSet *n; | |
137 | int start, end, ucs4; | |
138 | char line[1024]; | |
139 | int lineno = 0; | |
140 | ||
141 | while (get_line (f, line, &lineno)) | |
142 | { | |
143 | if (!strncmp (line, "include", 7)) | |
144 | { | |
145 | file = strchr (line, ' '); | |
cf5cf4ca | 146 | while (isspace(*file)) |
c1382a3d | 147 | file++; |
394b2bf0 | 148 | f = scanopen (file); |
c1382a3d KP |
149 | if (!f) |
150 | fatal (file, 0, "can't open"); | |
151 | c = scan (f, file); | |
152 | fclose (f); | |
153 | return c; | |
154 | } | |
155 | if (strchr (line, '-')) | |
156 | { | |
157 | if (sscanf (line, "%x-%x", &start, &end) != 2) | |
158 | fatal (file, lineno, "parse error"); | |
159 | } | |
160 | else | |
161 | { | |
162 | if (sscanf (line, "%x", &start) != 1) | |
163 | fatal (file, lineno, "parse error"); | |
164 | end = start; | |
165 | } | |
166 | if (!c) | |
167 | c = FcCharSetCreate (); | |
168 | for (ucs4 = start; ucs4 <= end; ucs4++) | |
169 | { | |
170 | if (!FcCharSetAddChar (c, ucs4)) | |
171 | fatal (file, lineno, "out of memory"); | |
172 | } | |
173 | } | |
174 | n = FcCharSetFreeze (c); | |
175 | FcCharSetDestroy (c); | |
176 | return n; | |
177 | } | |
178 | ||
179 | /* | |
180 | * Convert a file name into a name suitable for C declarations | |
181 | */ | |
182 | static char * | |
183 | get_name (char *file) | |
184 | { | |
185 | char *name; | |
186 | char *dot; | |
187 | ||
188 | dot = strchr (file, '.'); | |
189 | if (!dot) | |
190 | dot = file + strlen(file); | |
191 | name = malloc (dot - file + 1); | |
192 | strncpy (name, file, dot - file); | |
193 | name[dot-file] = '\0'; | |
194 | return name; | |
195 | } | |
196 | ||
197 | /* | |
198 | * Convert a C name into a language name | |
199 | */ | |
200 | static char * | |
201 | get_lang (char *name) | |
202 | { | |
203 | char *lang = malloc (strlen (name) + 1); | |
204 | char *l = lang; | |
205 | char c; | |
206 | ||
207 | while ((c = *name++)) | |
208 | { | |
996580dc KP |
209 | if (isupper ((int) (unsigned char) c)) |
210 | c = tolower ((int) (unsigned char) c); | |
c1382a3d KP |
211 | if (c == '_') |
212 | c = '-'; | |
213 | if (c == ' ') | |
214 | continue; | |
215 | *l++ = c; | |
216 | } | |
217 | *l++ = '\0'; | |
218 | return lang; | |
219 | } | |
220 | ||
d8d73958 KP |
221 | static int compare (const void *a, const void *b) |
222 | { | |
223 | const FcChar8 *const *as = a, *const *bs = b; | |
224 | return FcStrCmpIgnoreCase (*as, *bs); | |
225 | } | |
226 | ||
234397b4 DD |
227 | #define MAX_LANG 1024 |
228 | #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32) | |
229 | ||
230 | #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f))) | |
231 | #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1) | |
232 | ||
c1382a3d KP |
233 | int |
234 | main (int argc, char **argv) | |
235 | { | |
69a3fc78 PL |
236 | static char *files[MAX_LANG]; |
237 | static FcCharSet *sets[MAX_LANG]; | |
238 | static int duplicate[MAX_LANG]; | |
239 | static int offsets[MAX_LANG]; | |
240 | static int country[MAX_LANG]; | |
241 | static char *names[MAX_LANG]; | |
242 | static char *langs[MAX_LANG]; | |
c1382a3d | 243 | FILE *f; |
a151aced | 244 | int offset = 0; |
234397b4 | 245 | int ncountry = 0; |
c1382a3d | 246 | int i = 0; |
67accef4 | 247 | int argi; |
cd2ec1a9 | 248 | FcCharLeaf **leaves; |
c1382a3d | 249 | int total_leaves = 0; |
a151aced | 250 | int offset_count = 0; |
c1382a3d | 251 | int l, sl, tl; |
69a3fc78 PL |
252 | static char line[1024]; |
253 | static FcChar32 map[MAX_LANG_SET_MAP]; | |
234397b4 | 254 | int num_lang_set_map; |
0eadb052 KP |
255 | int setRangeStart[26]; |
256 | int setRangeEnd[26]; | |
257 | FcChar8 setRangeChar; | |
c1382a3d | 258 | |
67accef4 PL |
259 | argi = 1; |
260 | while (argv[argi]) | |
234397b4 | 261 | { |
67accef4 | 262 | if (!strcmp (argv[argi], "-d")) |
394b2bf0 | 263 | { |
67accef4 PL |
264 | argi++; |
265 | dir = argv[argi++]; | |
394b2bf0 KP |
266 | continue; |
267 | } | |
234397b4 | 268 | if (i == MAX_LANG) |
67accef4 PL |
269 | fatal (argv[0], 0, "Too many languages"); |
270 | files[i++] = argv[argi++]; | |
234397b4 | 271 | } |
d8d73958 KP |
272 | files[i] = 0; |
273 | qsort (files, i, sizeof (char *), compare); | |
274 | i = 0; | |
275 | while (files[i]) | |
c1382a3d | 276 | { |
394b2bf0 | 277 | f = scanopen (files[i]); |
c1382a3d | 278 | if (!f) |
d8d73958 KP |
279 | fatal (files[i], 0, strerror (errno)); |
280 | sets[i] = scan (f, files[i]); | |
281 | names[i] = get_name (files[i]); | |
234397b4 DD |
282 | langs[i] = get_lang(names[i]); |
283 | if (strchr (langs[i], '-')) | |
284 | country[ncountry++] = i; | |
285 | ||
c1382a3d KP |
286 | total_leaves += sets[i]->num; |
287 | i++; | |
288 | fclose (f); | |
289 | } | |
290 | sets[i] = 0; | |
291 | leaves = malloc (total_leaves * sizeof (FcCharLeaf *)); | |
292 | tl = 0; | |
293 | /* | |
294 | * Find unique leaves | |
295 | */ | |
296 | for (i = 0; sets[i]; i++) | |
297 | { | |
c1382a3d KP |
298 | for (sl = 0; sl < sets[i]->num; sl++) |
299 | { | |
300 | for (l = 0; l < tl; l++) | |
cd2ec1a9 | 301 | if (leaves[l] == FcCharSetGetLeaf(sets[i], sl)) |
c1382a3d KP |
302 | break; |
303 | if (l == tl) | |
cd2ec1a9 | 304 | leaves[tl++] = FcCharSetGetLeaf(sets[i], sl); |
c1382a3d KP |
305 | } |
306 | } | |
307 | ||
308 | /* | |
309 | * Scan the input until the marker is found | |
310 | */ | |
311 | ||
312 | while (fgets (line, sizeof (line), stdin)) | |
313 | { | |
314 | if (!strncmp (line, "@@@", 3)) | |
315 | break; | |
316 | fputs (line, stdout); | |
317 | } | |
318 | ||
319 | printf ("/* total size: %d unique leaves: %d */\n\n", | |
320 | total_leaves, tl); | |
321 | /* | |
322 | * Dump leaves | |
323 | */ | |
82f35f8b | 324 | printf ("const FcCharLeaf langBankLeaves[%d] = {\n", tl); |
c1382a3d KP |
325 | for (l = 0; l < tl; l++) |
326 | { | |
327 | printf (" { { /* %d */", l); | |
328 | for (i = 0; i < 256/32; i++) | |
329 | { | |
330 | if (i % 4 == 0) | |
331 | printf ("\n "); | |
332 | printf (" 0x%08x,", leaves[l]->map[i]); | |
333 | } | |
334 | printf ("\n } },\n"); | |
335 | } | |
336 | printf ("};\n\n"); | |
2903c146 KP |
337 | |
338 | /* | |
339 | * Find duplicate charsets | |
340 | */ | |
341 | duplicate[0] = -1; | |
342 | for (i = 1; sets[i]; i++) | |
343 | { | |
344 | int j; | |
345 | ||
346 | duplicate[i] = -1; | |
347 | for (j = 0; j < i; j++) | |
348 | if (sets[j] == sets[i]) | |
349 | { | |
350 | duplicate[i] = j; | |
351 | break; | |
352 | } | |
353 | } | |
354 | ||
0eadb052 KP |
355 | /* |
356 | * Find ranges for each letter for faster searching | |
357 | */ | |
358 | setRangeChar = 'a'; | |
359 | for (i = 0; sets[i]; i++) | |
360 | { | |
361 | char c = names[i][0]; | |
362 | ||
363 | while (setRangeChar <= c && c <= 'z') | |
364 | setRangeStart[setRangeChar++ - 'a'] = i; | |
365 | } | |
366 | for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++) | |
367 | setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1; | |
368 | setRangeEnd[setRangeChar - 'a'] = i - 1; | |
369 | ||
c1382a3d KP |
370 | /* |
371 | * Dump arrays | |
372 | */ | |
373 | for (i = 0; sets[i]; i++) | |
374 | { | |
375 | int n; | |
376 | ||
2903c146 KP |
377 | if (duplicate[i] >= 0) |
378 | continue; | |
82f35f8b PL |
379 | |
380 | for (n = 0; n < sets[i]->num; n++) | |
381 | { | |
382 | for (l = 0; l < tl; l++) | |
383 | if (leaves[l] == FcCharSetGetLeaf(sets[i], n)) | |
384 | break; | |
385 | if (l == tl) | |
386 | fatal (names[i], 0, "can't find leaf"); | |
a151aced | 387 | offset_count++; |
82f35f8b | 388 | } |
a151aced PL |
389 | offsets[i] = offset; |
390 | offset += sets[i]->num; | |
82f35f8b PL |
391 | } |
392 | ||
393 | printf ("const int langBankLeafIdx[%d] = {\n", | |
a151aced | 394 | offset_count); |
82f35f8b PL |
395 | for (i = 0; sets[i]; i++) |
396 | { | |
397 | int n; | |
398 | ||
399 | if (duplicate[i] >= 0) | |
400 | continue; | |
c1382a3d KP |
401 | for (n = 0; n < sets[i]->num; n++) |
402 | { | |
403 | if (n % 8 == 0) | |
404 | printf (" "); | |
405 | for (l = 0; l < tl; l++) | |
cd2ec1a9 | 406 | if (leaves[l] == FcCharSetGetLeaf(sets[i], n)) |
c1382a3d KP |
407 | break; |
408 | if (l == tl) | |
409 | fatal (names[i], 0, "can't find leaf"); | |
82f35f8b | 410 | printf (" %3d,", l); |
c1382a3d KP |
411 | if (n % 8 == 7) |
412 | printf ("\n"); | |
413 | } | |
414 | if (n % 8 != 0) | |
415 | printf ("\n"); | |
82f35f8b PL |
416 | } |
417 | printf ("};\n\n"); | |
c1382a3d | 418 | |
82f35f8b | 419 | printf ("const FcChar16 langBankNumbers[%d] = {\n", |
a151aced | 420 | offset_count); |
82f35f8b PL |
421 | |
422 | for (i = 0; sets[i]; i++) | |
423 | { | |
424 | int n; | |
a151aced PL |
425 | |
426 | if (duplicate[i] >= 0) | |
427 | continue; | |
c1382a3d KP |
428 | for (n = 0; n < sets[i]->num; n++) |
429 | { | |
430 | if (n % 8 == 0) | |
431 | printf (" "); | |
cd2ec1a9 | 432 | printf (" 0x%04x,", FcCharSetGetNumbers(sets[i])[n]); |
c1382a3d KP |
433 | if (n % 8 == 7) |
434 | printf ("\n"); | |
435 | } | |
436 | if (n % 8 != 0) | |
437 | printf ("\n"); | |
c1382a3d | 438 | } |
82f35f8b | 439 | printf ("};\n\n"); |
0eadb052 | 440 | |
c1382a3d KP |
441 | /* |
442 | * Dump sets | |
443 | */ | |
0eadb052 | 444 | |
82f35f8b | 445 | printf ("const FcLangCharSet fcLangCharSets[] = {\n"); |
c1382a3d KP |
446 | for (i = 0; sets[i]; i++) |
447 | { | |
2903c146 | 448 | int j = duplicate[i]; |
0eadb052 | 449 | |
2903c146 KP |
450 | if (j < 0) |
451 | j = i; | |
82f35f8b | 452 | |
c1382a3d | 453 | printf (" { (FcChar8 *) \"%s\",\n" |
82f35f8b | 454 | " { FC_REF_CONSTANT, %d, FC_BANK_LANGS, " |
f23f5f38 | 455 | "{ { %d, %d } } } }, /* %d */\n", |
234397b4 | 456 | langs[i], |
a151aced | 457 | sets[j]->num, offsets[j], offsets[j], j); |
c1382a3d KP |
458 | } |
459 | printf ("};\n\n"); | |
234397b4 DD |
460 | printf ("#define NUM_LANG_CHAR_SET %d\n", i); |
461 | num_lang_set_map = (i + 31) / 32; | |
462 | printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map); | |
463 | /* | |
464 | * Dump indices with country codes | |
465 | */ | |
466 | if (ncountry) | |
467 | { | |
0d745819 | 468 | int c; |
234397b4 DD |
469 | int ncountry_ent = 0; |
470 | printf ("\n"); | |
471 | printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n"); | |
472 | for (c = 0; c < ncountry; c++) | |
473 | { | |
474 | i = country[c]; | |
475 | if (i >= 0) | |
476 | { | |
0d745819 | 477 | int lang = strchr (langs[i], '-') - langs[i]; |
234397b4 DD |
478 | int d, k; |
479 | ||
480 | for (k = 0; k < num_lang_set_map; k++) | |
481 | map[k] = 0; | |
482 | ||
483 | BitSet (map, i); | |
484 | for (d = c + 1; d < ncountry; d++) | |
485 | { | |
486 | int j = country[d]; | |
487 | if (j >= 0 && !strncmp (langs[j], langs[i], l)) | |
488 | { | |
489 | BitSet(map, j); | |
490 | country[d] = -1; | |
491 | } | |
492 | } | |
493 | printf (" {"); | |
494 | for (k = 0; k < num_lang_set_map; k++) | |
495 | printf (" 0x%08x,", map[k]); | |
496 | printf (" }, /* %*.*s */\n", | |
0d745819 | 497 | lang, lang, langs[i]); |
234397b4 DD |
498 | ++ncountry_ent; |
499 | } | |
500 | } | |
501 | printf ("};\n\n"); | |
502 | printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent); | |
503 | } | |
504 | ||
0eadb052 KP |
505 | |
506 | /* | |
507 | * Dump sets start/finish for the fastpath | |
508 | */ | |
509 | printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n"); | |
510 | for (setRangeChar = 'a'; setRangeChar <= 'z' ; setRangeChar++) | |
511 | { | |
512 | printf (" { %d, %d }, /* %c */\n", | |
513 | setRangeStart[setRangeChar - 'a'], | |
514 | setRangeEnd[setRangeChar - 'a'], setRangeChar); | |
515 | } | |
516 | printf ("};\n\n"); | |
517 | ||
c1382a3d KP |
518 | while (fgets (line, sizeof (line), stdin)) |
519 | fputs (line, stdout); | |
520 | ||
521 | fflush (stdout); | |
522 | exit (ferror (stdout)); | |
523 | } |