]> git.wh0rd.org - fontconfig.git/blob - fc-lang/fc-lang.c
Add functionality to allow fontconfig data structure serialization.
[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 void
41 FcMemAlloc (int kind, int size)
42 {
43 }
44
45 void
46 FcMemFree (int kind, int size)
47 {
48 }
49
50 FcChar8 *
51 FcConfigHome (void)
52 {
53 return getenv ("HOME");
54 }
55
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
78 char *dir = 0;
79
80 static FILE *
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
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';
126 f = scanopen (file);
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 {
187 if (isupper ((int) (unsigned char) c))
188 c = tolower ((int) (unsigned char) c);
189 if (c == '_')
190 c = '-';
191 if (c == ' ')
192 continue;
193 *l++ = c;
194 }
195 *l++ = '\0';
196 return lang;
197 }
198
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
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
211 int
212 main (int argc, char **argv)
213 {
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];
220 FILE *f;
221 int ncountry = 0;
222 int i = 0;
223 FcCharLeaf **leaves;
224 int total_leaves = 0;
225 int l, sl, tl;
226 int c;
227 char line[1024];
228 FcChar32 map[MAX_LANG_SET_MAP];
229 int num_lang_set_map;
230 int setRangeStart[26];
231 int setRangeEnd[26];
232 FcChar8 setRangeChar;
233
234 while (*++argv)
235 {
236 if (!strcmp (*argv, "-d"))
237 {
238 dir = *++argv;
239 continue;
240 }
241 if (i == MAX_LANG)
242 fatal (*argv, 0, "Too many languages");
243 files[i++] = *argv;
244 }
245 files[i] = 0;
246 qsort (files, i, sizeof (char *), compare);
247 i = 0;
248 while (files[i])
249 {
250 f = scanopen (files[i]);
251 if (!f)
252 fatal (files[i], 0, strerror (errno));
253 sets[i] = scan (f, files[i]);
254 names[i] = get_name (files[i]);
255 langs[i] = get_lang(names[i]);
256 if (strchr (langs[i], '-'))
257 country[ncountry++] = i;
258
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 for (sl = 0; sl < sets[i]->num; sl++)
272 {
273 for (l = 0; l < tl; l++)
274 if (leaves[l] == FcCharSetGetLeaf(sets[i], sl))
275 break;
276 if (l == tl)
277 leaves[tl++] = FcCharSetGetLeaf(sets[i], sl);
278 }
279 }
280
281 /*
282 * Scan the input until the marker is found
283 */
284
285 while (fgets (line, sizeof (line), stdin))
286 {
287 if (!strncmp (line, "@@@", 3))
288 break;
289 fputs (line, stdout);
290 }
291
292 printf ("/* total size: %d unique leaves: %d */\n\n",
293 total_leaves, tl);
294 /*
295 * Dump leaves
296 */
297 printf ("static const FcCharLeaf leaves[%d] = {\n", tl);
298 for (l = 0; l < tl; l++)
299 {
300 printf (" { { /* %d */", l);
301 for (i = 0; i < 256/32; i++)
302 {
303 if (i % 4 == 0)
304 printf ("\n ");
305 printf (" 0x%08x,", leaves[l]->map[i]);
306 }
307 printf ("\n } },\n");
308 }
309 printf ("};\n\n");
310 printf ("#define L(n) ((FcCharLeaf *) &leaves[n])\n\n");
311
312 /*
313 * Find duplicate charsets
314 */
315 duplicate[0] = -1;
316 for (i = 1; sets[i]; i++)
317 {
318 int j;
319
320 duplicate[i] = -1;
321 for (j = 0; j < i; j++)
322 if (sets[j] == sets[i])
323 {
324 duplicate[i] = j;
325 break;
326 }
327 }
328
329 /*
330 * Find ranges for each letter for faster searching
331 */
332 setRangeChar = 'a';
333 for (i = 0; sets[i]; i++)
334 {
335 char c = names[i][0];
336
337 while (setRangeChar <= c && c <= 'z')
338 setRangeStart[setRangeChar++ - 'a'] = i;
339 }
340 for (setRangeChar = 'a'; setRangeChar < 'z'; setRangeChar++)
341 setRangeEnd[setRangeChar - 'a'] = setRangeStart[setRangeChar+1-'a'] - 1;
342 setRangeEnd[setRangeChar - 'a'] = i - 1;
343
344 /*
345 * Dump arrays
346 */
347 for (i = 0; sets[i]; i++)
348 {
349 int n;
350
351 if (duplicate[i] >= 0)
352 continue;
353 printf ("static const FcCharLeaf *leaves_%s[%d] = {\n",
354 names[i], sets[i]->num);
355 for (n = 0; n < sets[i]->num; n++)
356 {
357 if (n % 8 == 0)
358 printf (" ");
359 for (l = 0; l < tl; l++)
360 if (leaves[l] == FcCharSetGetLeaf(sets[i], n))
361 break;
362 if (l == tl)
363 fatal (names[i], 0, "can't find leaf");
364 printf (" L(%3d),", l);
365 if (n % 8 == 7)
366 printf ("\n");
367 }
368 if (n % 8 != 0)
369 printf ("\n");
370 printf ("};\n\n");
371
372
373 printf ("static const FcChar16 numbers_%s[%d] = {\n",
374 names[i], sets[i]->num);
375 for (n = 0; n < sets[i]->num; n++)
376 {
377 if (n % 8 == 0)
378 printf (" ");
379 printf (" 0x%04x,", FcCharSetGetNumbers(sets[i])[n]);
380 if (n % 8 == 7)
381 printf ("\n");
382 }
383 if (n % 8 != 0)
384 printf ("\n");
385 printf ("};\n\n");
386 }
387 printf ("#undef L\n\n");
388
389 /*
390 * Dump sets
391 */
392
393 printf ("static const FcLangCharSet fcLangCharSets[] = {\n");
394 for (i = 0; sets[i]; i++)
395 {
396 int j = duplicate[i];
397
398 if (j < 0)
399 j = i;
400 printf (" { (FcChar8 *) \"%s\",\n"
401 " { FC_REF_CONSTANT, %d, FcStorageDynamic, "
402 "{ { (FcCharLeaf **) leaves_%s, "
403 "(FcChar16 *) numbers_%s } } } },\n",
404 langs[i],
405 sets[j]->num, names[j], names[j]);
406 }
407 printf ("};\n\n");
408 printf ("#define NUM_LANG_CHAR_SET %d\n", i);
409 num_lang_set_map = (i + 31) / 32;
410 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map);
411 /*
412 * Dump indices with country codes
413 */
414 if (ncountry)
415 {
416 int ncountry_ent = 0;
417 printf ("\n");
418 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
419 for (c = 0; c < ncountry; c++)
420 {
421 i = country[c];
422 if (i >= 0)
423 {
424 int l = strchr (langs[i], '-') - langs[i];
425 int d, k;
426
427 for (k = 0; k < num_lang_set_map; k++)
428 map[k] = 0;
429
430 BitSet (map, i);
431 for (d = c + 1; d < ncountry; d++)
432 {
433 int j = country[d];
434 if (j >= 0 && !strncmp (langs[j], langs[i], l))
435 {
436 BitSet(map, j);
437 country[d] = -1;
438 }
439 }
440 printf (" {");
441 for (k = 0; k < num_lang_set_map; k++)
442 printf (" 0x%08x,", map[k]);
443 printf (" }, /* %*.*s */\n",
444 l, l, langs[i]);
445 ++ncountry_ent;
446 }
447 }
448 printf ("};\n\n");
449 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent);
450 }
451
452
453 /*
454 * Dump sets start/finish for the fastpath
455 */
456 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
457 for (setRangeChar = 'a'; setRangeChar <= 'z' ; setRangeChar++)
458 {
459 printf (" { %d, %d }, /* %c */\n",
460 setRangeStart[setRangeChar - 'a'],
461 setRangeEnd[setRangeChar - 'a'], setRangeChar);
462 }
463 printf ("};\n\n");
464
465 while (fgets (line, sizeof (line), stdin))
466 fputs (line, stdout);
467
468 fflush (stdout);
469 exit (ferror (stdout));
470 }