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