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