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