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