]> git.wh0rd.org - fontconfig.git/blame - fc-lang/fc-lang.c
Add functionality to allow fontconfig data structure serialization.
[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
25#include "fcint.h"
c647f6f1
KP
26#include "fccharset.c"
27#include "fcstr.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
ff3f1f98
KP
50FcChar8 *
51FcConfigHome (void)
52{
53 return getenv ("HOME");
54}
55
c1382a3d
KP
56static void
57fatal (char *file, int lineno, char *msg)
58{
59 fprintf (stderr, "%s:%d: %s\n", file, lineno, msg);
60 exit (1);
61}
62
63static char *
64get_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
394b2bf0
KP
78char *dir = 0;
79
6ae6acf3 80static FILE *
394b2bf0
KP
81scanopen (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
c1382a3d
KP
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
107static FcCharSet *
108scan (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';
394b2bf0 126 f = scanopen (file);
c1382a3d
KP
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 */
160static char *
161get_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 */
178static char *
179get_lang (char *name)
180{
181 char *lang = malloc (strlen (name) + 1);
182 char *l = lang;
183 char c;
184
185 while ((c = *name++))
186 {
996580dc
KP
187 if (isupper ((int) (unsigned char) c))
188 c = tolower ((int) (unsigned char) c);
c1382a3d
KP
189 if (c == '_')
190 c = '-';
191 if (c == ' ')
192 continue;
193 *l++ = c;
194 }
195 *l++ = '\0';
196 return lang;
197}
198
d8d73958
KP
199static 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
234397b4
DD
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
c1382a3d
KP
211int
212main (int argc, char **argv)
213{
234397b4
DD
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];
c1382a3d 220 FILE *f;
234397b4 221 int ncountry = 0;
c1382a3d 222 int i = 0;
cd2ec1a9 223 FcCharLeaf **leaves;
c1382a3d
KP
224 int total_leaves = 0;
225 int l, sl, tl;
234397b4 226 int c;
c1382a3d 227 char line[1024];
234397b4
DD
228 FcChar32 map[MAX_LANG_SET_MAP];
229 int num_lang_set_map;
0eadb052
KP
230 int setRangeStart[26];
231 int setRangeEnd[26];
232 FcChar8 setRangeChar;
c1382a3d
KP
233
234 while (*++argv)
234397b4 235 {
394b2bf0
KP
236 if (!strcmp (*argv, "-d"))
237 {
238 dir = *++argv;
239 continue;
240 }
234397b4
DD
241 if (i == MAX_LANG)
242 fatal (*argv, 0, "Too many languages");
d8d73958 243 files[i++] = *argv;
234397b4 244 }
d8d73958
KP
245 files[i] = 0;
246 qsort (files, i, sizeof (char *), compare);
247 i = 0;
248 while (files[i])
c1382a3d 249 {
394b2bf0 250 f = scanopen (files[i]);
c1382a3d 251 if (!f)
d8d73958
KP
252 fatal (files[i], 0, strerror (errno));
253 sets[i] = scan (f, files[i]);
254 names[i] = get_name (files[i]);
234397b4
DD
255 langs[i] = get_lang(names[i]);
256 if (strchr (langs[i], '-'))
257 country[ncountry++] = i;
258
c1382a3d
KP
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 {
c1382a3d
KP
271 for (sl = 0; sl < sets[i]->num; sl++)
272 {
273 for (l = 0; l < tl; l++)
cd2ec1a9 274 if (leaves[l] == FcCharSetGetLeaf(sets[i], sl))
c1382a3d
KP
275 break;
276 if (l == tl)
cd2ec1a9 277 leaves[tl++] = FcCharSetGetLeaf(sets[i], sl);
c1382a3d
KP
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");
2903c146
KP
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
0eadb052
KP
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
c1382a3d
KP
344 /*
345 * Dump arrays
346 */
347 for (i = 0; sets[i]; i++)
348 {
349 int n;
350
2903c146
KP
351 if (duplicate[i] >= 0)
352 continue;
c1382a3d
KP
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++)
cd2ec1a9 360 if (leaves[l] == FcCharSetGetLeaf(sets[i], n))
c1382a3d
KP
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 (" ");
cd2ec1a9 379 printf (" 0x%04x,", FcCharSetGetNumbers(sets[i])[n]);
c1382a3d
KP
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");
0eadb052 388
c1382a3d
KP
389 /*
390 * Dump sets
391 */
0eadb052 392
c1382a3d
KP
393 printf ("static const FcLangCharSet fcLangCharSets[] = {\n");
394 for (i = 0; sets[i]; i++)
395 {
2903c146 396 int j = duplicate[i];
0eadb052 397
2903c146
KP
398 if (j < 0)
399 j = i;
c1382a3d 400 printf (" { (FcChar8 *) \"%s\",\n"
cd2ec1a9
PL
401 " { FC_REF_CONSTANT, %d, FcStorageDynamic, "
402 "{ { (FcCharLeaf **) leaves_%s, "
403 "(FcChar16 *) numbers_%s } } } },\n",
234397b4 404 langs[i],
2903c146 405 sets[j]->num, names[j], names[j]);
c1382a3d
KP
406 }
407 printf ("};\n\n");
234397b4
DD
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
0eadb052
KP
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
c1382a3d
KP
465 while (fgets (line, sizeof (line), stdin))
466 fputs (line, stdout);
467
468 fflush (stdout);
469 exit (ferror (stdout));
470}