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