]>
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; | |
56 | ||
212c9f43 | 57 | int |
b8948e85 | 58 | FcCacheBankToIndexMTF (int bank) |
212c9f43 PL |
59 | { |
60 | return -1; | |
61 | } | |
62 | ||
ff3f1f98 KP |
63 | FcChar8 * |
64 | FcConfigHome (void) | |
65 | { | |
8245771d | 66 | return (FcChar8 *) getenv ("HOME"); |
ff3f1f98 KP |
67 | } |
68 | ||
c1382a3d | 69 | static void |
67accef4 | 70 | fatal (const char *file, int lineno, const char *msg) |
c1382a3d | 71 | { |
67accef4 PL |
72 | if (lineno) |
73 | fprintf (stderr, "%s:%d: %s\n", file, lineno, msg); | |
74 | else | |
c7beacf9 | 75 | fprintf (stderr, "%s: %s\n", file, msg); |
c1382a3d KP |
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 | ||
394b2bf0 KP |
94 | char *dir = 0; |
95 | ||
6ae6acf3 | 96 | static FILE * |
394b2bf0 KP |
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 | ||
c1382a3d KP |
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'; | |
394b2bf0 | 142 | f = scanopen (file); |
c1382a3d KP |
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 | { | |
996580dc KP |
203 | if (isupper ((int) (unsigned char) c)) |
204 | c = tolower ((int) (unsigned char) c); | |
c1382a3d KP |
205 | if (c == '_') |
206 | c = '-'; | |
207 | if (c == ' ') | |
208 | continue; | |
209 | *l++ = c; | |
210 | } | |
211 | *l++ = '\0'; | |
212 | return lang; | |
213 | } | |
214 | ||
d8d73958 KP |
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 | ||
234397b4 DD |
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 | ||
c1382a3d KP |
227 | int |
228 | main (int argc, char **argv) | |
229 | { | |
69a3fc78 PL |
230 | static char *files[MAX_LANG]; |
231 | static FcCharSet *sets[MAX_LANG]; | |
232 | static int duplicate[MAX_LANG]; | |
233 | static int offsets[MAX_LANG]; | |
234 | static int country[MAX_LANG]; | |
235 | static char *names[MAX_LANG]; | |
236 | static char *langs[MAX_LANG]; | |
c1382a3d | 237 | FILE *f; |
a151aced | 238 | int offset = 0; |
234397b4 | 239 | int ncountry = 0; |
c1382a3d | 240 | int i = 0; |
67accef4 | 241 | int argi; |
cd2ec1a9 | 242 | FcCharLeaf **leaves; |
c1382a3d | 243 | int total_leaves = 0; |
a151aced | 244 | int offset_count = 0; |
c1382a3d | 245 | int l, sl, tl; |
234397b4 | 246 | int c; |
69a3fc78 PL |
247 | static char line[1024]; |
248 | static FcChar32 map[MAX_LANG_SET_MAP]; | |
234397b4 | 249 | int num_lang_set_map; |
0eadb052 KP |
250 | int setRangeStart[26]; |
251 | int setRangeEnd[26]; | |
252 | FcChar8 setRangeChar; | |
c1382a3d | 253 | |
67accef4 PL |
254 | argi = 1; |
255 | while (argv[argi]) | |
234397b4 | 256 | { |
67accef4 | 257 | if (!strcmp (argv[argi], "-d")) |
394b2bf0 | 258 | { |
67accef4 PL |
259 | argi++; |
260 | dir = argv[argi++]; | |
394b2bf0 KP |
261 | continue; |
262 | } | |
234397b4 | 263 | if (i == MAX_LANG) |
67accef4 PL |
264 | fatal (argv[0], 0, "Too many languages"); |
265 | files[i++] = argv[argi++]; | |
234397b4 | 266 | } |
d8d73958 KP |
267 | files[i] = 0; |
268 | qsort (files, i, sizeof (char *), compare); | |
269 | i = 0; | |
270 | while (files[i]) | |
c1382a3d | 271 | { |
394b2bf0 | 272 | f = scanopen (files[i]); |
c1382a3d | 273 | if (!f) |
d8d73958 KP |
274 | fatal (files[i], 0, strerror (errno)); |
275 | sets[i] = scan (f, files[i]); | |
276 | names[i] = get_name (files[i]); | |
234397b4 DD |
277 | langs[i] = get_lang(names[i]); |
278 | if (strchr (langs[i], '-')) | |
279 | country[ncountry++] = i; | |
280 | ||
c1382a3d KP |
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 | { | |
c1382a3d KP |
293 | for (sl = 0; sl < sets[i]->num; sl++) |
294 | { | |
295 | for (l = 0; l < tl; l++) | |
cd2ec1a9 | 296 | if (leaves[l] == FcCharSetGetLeaf(sets[i], sl)) |
c1382a3d KP |
297 | break; |
298 | if (l == tl) | |
cd2ec1a9 | 299 | leaves[tl++] = FcCharSetGetLeaf(sets[i], sl); |
c1382a3d KP |
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 | */ | |
82f35f8b | 319 | printf ("const FcCharLeaf langBankLeaves[%d] = {\n", tl); |
c1382a3d KP |
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"); | |
2903c146 KP |
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 | ||
0eadb052 KP |
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 | ||
c1382a3d KP |
365 | /* |
366 | * Dump arrays | |
367 | */ | |
368 | for (i = 0; sets[i]; i++) | |
369 | { | |
370 | int n; | |
371 | ||
2903c146 KP |
372 | if (duplicate[i] >= 0) |
373 | continue; | |
82f35f8b PL |
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"); | |
a151aced | 382 | offset_count++; |
82f35f8b | 383 | } |
a151aced PL |
384 | offsets[i] = offset; |
385 | offset += sets[i]->num; | |
82f35f8b PL |
386 | } |
387 | ||
388 | printf ("const int langBankLeafIdx[%d] = {\n", | |
a151aced | 389 | offset_count); |
82f35f8b PL |
390 | for (i = 0; sets[i]; i++) |
391 | { | |
392 | int n; | |
393 | ||
394 | if (duplicate[i] >= 0) | |
395 | continue; | |
c1382a3d KP |
396 | for (n = 0; n < sets[i]->num; n++) |
397 | { | |
398 | if (n % 8 == 0) | |
399 | printf (" "); | |
400 | for (l = 0; l < tl; l++) | |
cd2ec1a9 | 401 | if (leaves[l] == FcCharSetGetLeaf(sets[i], n)) |
c1382a3d KP |
402 | break; |
403 | if (l == tl) | |
404 | fatal (names[i], 0, "can't find leaf"); | |
82f35f8b | 405 | printf (" %3d,", l); |
c1382a3d KP |
406 | if (n % 8 == 7) |
407 | printf ("\n"); | |
408 | } | |
409 | if (n % 8 != 0) | |
410 | printf ("\n"); | |
82f35f8b PL |
411 | } |
412 | printf ("};\n\n"); | |
c1382a3d | 413 | |
82f35f8b | 414 | printf ("const FcChar16 langBankNumbers[%d] = {\n", |
a151aced | 415 | offset_count); |
82f35f8b PL |
416 | |
417 | for (i = 0; sets[i]; i++) | |
418 | { | |
419 | int n; | |
a151aced PL |
420 | |
421 | if (duplicate[i] >= 0) | |
422 | continue; | |
c1382a3d KP |
423 | for (n = 0; n < sets[i]->num; n++) |
424 | { | |
425 | if (n % 8 == 0) | |
426 | printf (" "); | |
cd2ec1a9 | 427 | printf (" 0x%04x,", FcCharSetGetNumbers(sets[i])[n]); |
c1382a3d KP |
428 | if (n % 8 == 7) |
429 | printf ("\n"); | |
430 | } | |
431 | if (n % 8 != 0) | |
432 | printf ("\n"); | |
c1382a3d | 433 | } |
82f35f8b | 434 | printf ("};\n\n"); |
0eadb052 | 435 | |
c1382a3d KP |
436 | /* |
437 | * Dump sets | |
438 | */ | |
0eadb052 | 439 | |
82f35f8b | 440 | printf ("const FcLangCharSet fcLangCharSets[] = {\n"); |
c1382a3d KP |
441 | for (i = 0; sets[i]; i++) |
442 | { | |
2903c146 | 443 | int j = duplicate[i]; |
0eadb052 | 444 | |
2903c146 KP |
445 | if (j < 0) |
446 | j = i; | |
82f35f8b | 447 | |
c1382a3d | 448 | printf (" { (FcChar8 *) \"%s\",\n" |
82f35f8b | 449 | " { FC_REF_CONSTANT, %d, FC_BANK_LANGS, " |
a151aced | 450 | "{ .stat = { %d, %d } } } }, /* %d */\n", |
234397b4 | 451 | langs[i], |
a151aced | 452 | sets[j]->num, offsets[j], offsets[j], j); |
c1382a3d KP |
453 | } |
454 | printf ("};\n\n"); | |
234397b4 DD |
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 | ||
0eadb052 KP |
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 | ||
c1382a3d KP |
512 | while (fgets (line, sizeof (line), stdin)) |
513 | fputs (line, stdout); | |
514 | ||
515 | fflush (stdout); | |
516 | exit (ferror (stdout)); | |
517 | } |