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