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