]> git.wh0rd.org - fontconfig.git/blob - fc-lang/fc-lang.c
Switch back to -version-info for fontconfig as its at minor 0. Add
[fontconfig.git] / fc-lang / fc-lang.c
1 /*
2 * $XFree86: 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, member of The XFree86 Project, Inc.
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 /*
79 * build a single charset from a source file
80 *
81 * The file format is quite simple, either
82 * a single hex value or a pair separated with a dash
83 *
84 * Comments begin with '#'
85 */
86
87 static FcCharSet *
88 scan (FILE *f, char *file)
89 {
90 FcCharSet *c = 0;
91 FcCharSet *n;
92 int start, end, ucs4;
93 char line[1024];
94 int lineno = 0;
95
96 while (get_line (f, line, &lineno))
97 {
98 if (!strncmp (line, "include", 7))
99 {
100 file = strchr (line, ' ');
101 while (*file == ' ')
102 file++;
103 end = strlen (file);
104 if (file[end-1] == '\n')
105 file[end-1] = '\0';
106 f = fopen (file, "r");
107 if (!f)
108 fatal (file, 0, "can't open");
109 c = scan (f, file);
110 fclose (f);
111 return c;
112 }
113 if (strchr (line, '-'))
114 {
115 if (sscanf (line, "%x-%x", &start, &end) != 2)
116 fatal (file, lineno, "parse error");
117 }
118 else
119 {
120 if (sscanf (line, "%x", &start) != 1)
121 fatal (file, lineno, "parse error");
122 end = start;
123 }
124 if (!c)
125 c = FcCharSetCreate ();
126 for (ucs4 = start; ucs4 <= end; ucs4++)
127 {
128 if (!FcCharSetAddChar (c, ucs4))
129 fatal (file, lineno, "out of memory");
130 }
131 }
132 n = FcCharSetFreeze (c);
133 FcCharSetDestroy (c);
134 return n;
135 }
136
137 /*
138 * Convert a file name into a name suitable for C declarations
139 */
140 static char *
141 get_name (char *file)
142 {
143 char *name;
144 char *dot;
145
146 dot = strchr (file, '.');
147 if (!dot)
148 dot = file + strlen(file);
149 name = malloc (dot - file + 1);
150 strncpy (name, file, dot - file);
151 name[dot-file] = '\0';
152 return name;
153 }
154
155 /*
156 * Convert a C name into a language name
157 */
158 static char *
159 get_lang (char *name)
160 {
161 char *lang = malloc (strlen (name) + 1);
162 char *l = lang;
163 char c;
164
165 while ((c = *name++))
166 {
167 if (isupper (c))
168 c = tolower (c);
169 if (c == '_')
170 c = '-';
171 if (c == ' ')
172 continue;
173 *l++ = c;
174 }
175 *l++ = '\0';
176 return lang;
177 }
178
179 static int compare (const void *a, const void *b)
180 {
181 const FcChar8 *const *as = a, *const *bs = b;
182 return FcStrCmpIgnoreCase (*as, *bs);
183 }
184
185 #define MAX_LANG 1024
186 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
187
188 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
189 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
190
191 int
192 main (int argc, char **argv)
193 {
194 char *files[MAX_LANG];
195 FcCharSet *sets[MAX_LANG];
196 int duplicate[MAX_LANG];
197 int country[MAX_LANG];
198 char *names[MAX_LANG];
199 char *langs[MAX_LANG];
200 FILE *f;
201 int ncountry = 0;
202 int i = 0;
203 FcCharLeaf **leaves, **sleaves;
204 int total_leaves = 0;
205 int l, sl, tl;
206 int c;
207 char line[1024];
208 FcChar32 map[MAX_LANG_SET_MAP];
209 int num_lang_set_map;
210
211 while (*++argv)
212 {
213 if (i == MAX_LANG)
214 fatal (*argv, 0, "Too many languages");
215 files[i++] = *argv;
216 }
217 files[i] = 0;
218 qsort (files, i, sizeof (char *), compare);
219 i = 0;
220 while (files[i])
221 {
222 f = fopen (files[i], "r");
223 if (!f)
224 fatal (files[i], 0, strerror (errno));
225 sets[i] = scan (f, files[i]);
226 names[i] = get_name (files[i]);
227 langs[i] = get_lang(names[i]);
228 if (strchr (langs[i], '-'))
229 country[ncountry++] = i;
230
231 total_leaves += sets[i]->num;
232 i++;
233 fclose (f);
234 }
235 sets[i] = 0;
236 leaves = malloc (total_leaves * sizeof (FcCharLeaf *));
237 tl = 0;
238 /*
239 * Find unique leaves
240 */
241 for (i = 0; sets[i]; i++)
242 {
243 sleaves = sets[i]->leaves;
244 for (sl = 0; sl < sets[i]->num; sl++)
245 {
246 for (l = 0; l < tl; l++)
247 if (leaves[l] == sleaves[sl])
248 break;
249 if (l == tl)
250 leaves[tl++] = sleaves[sl];
251 }
252 }
253
254 /*
255 * Scan the input until the marker is found
256 */
257
258 while (fgets (line, sizeof (line), stdin))
259 {
260 if (!strncmp (line, "@@@", 3))
261 break;
262 fputs (line, stdout);
263 }
264
265 printf ("/* total size: %d unique leaves: %d */\n\n",
266 total_leaves, tl);
267 /*
268 * Dump leaves
269 */
270 printf ("static const FcCharLeaf leaves[%d] = {\n", tl);
271 for (l = 0; l < tl; l++)
272 {
273 printf (" { { /* %d */", l);
274 for (i = 0; i < 256/32; i++)
275 {
276 if (i % 4 == 0)
277 printf ("\n ");
278 printf (" 0x%08x,", leaves[l]->map[i]);
279 }
280 printf ("\n } },\n");
281 }
282 printf ("};\n\n");
283 printf ("#define L(n) ((FcCharLeaf *) &leaves[n])\n\n");
284
285 /*
286 * Find duplicate charsets
287 */
288 duplicate[0] = -1;
289 for (i = 1; sets[i]; i++)
290 {
291 int j;
292
293 duplicate[i] = -1;
294 for (j = 0; j < i; j++)
295 if (sets[j] == sets[i])
296 {
297 duplicate[i] = j;
298 break;
299 }
300 }
301
302 /*
303 * Dump arrays
304 */
305 for (i = 0; sets[i]; i++)
306 {
307 int n;
308
309 if (duplicate[i] >= 0)
310 continue;
311 printf ("static const FcCharLeaf *leaves_%s[%d] = {\n",
312 names[i], sets[i]->num);
313 for (n = 0; n < sets[i]->num; n++)
314 {
315 if (n % 8 == 0)
316 printf (" ");
317 for (l = 0; l < tl; l++)
318 if (leaves[l] == sets[i]->leaves[n])
319 break;
320 if (l == tl)
321 fatal (names[i], 0, "can't find leaf");
322 printf (" L(%3d),", l);
323 if (n % 8 == 7)
324 printf ("\n");
325 }
326 if (n % 8 != 0)
327 printf ("\n");
328 printf ("};\n\n");
329
330
331 printf ("static const FcChar16 numbers_%s[%d] = {\n",
332 names[i], sets[i]->num);
333 for (n = 0; n < sets[i]->num; n++)
334 {
335 if (n % 8 == 0)
336 printf (" ");
337 printf (" 0x%04x,", sets[i]->numbers[n]);
338 if (n % 8 == 7)
339 printf ("\n");
340 }
341 if (n % 8 != 0)
342 printf ("\n");
343 printf ("};\n\n");
344 }
345 printf ("#undef L\n\n");
346 /*
347 * Dump sets
348 */
349 printf ("static const FcLangCharSet fcLangCharSets[] = {\n");
350 for (i = 0; sets[i]; i++)
351 {
352 int j = duplicate[i];
353 if (j < 0)
354 j = i;
355 printf (" { (FcChar8 *) \"%s\",\n"
356 " { FC_REF_CONSTANT, %d, "
357 "(FcCharLeaf **) leaves_%s, "
358 "(FcChar16 *) numbers_%s } },\n",
359 langs[i],
360 sets[j]->num, names[j], names[j]);
361 }
362 printf ("};\n\n");
363 printf ("#define NUM_LANG_CHAR_SET %d\n", i);
364 num_lang_set_map = (i + 31) / 32;
365 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map);
366 /*
367 * Dump indices with country codes
368 */
369 if (ncountry)
370 {
371 int ncountry_ent = 0;
372 printf ("\n");
373 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
374 for (c = 0; c < ncountry; c++)
375 {
376 i = country[c];
377 if (i >= 0)
378 {
379 int l = strchr (langs[i], '-') - langs[i];
380 int d, k;
381
382 for (k = 0; k < num_lang_set_map; k++)
383 map[k] = 0;
384
385 BitSet (map, i);
386 for (d = c + 1; d < ncountry; d++)
387 {
388 int j = country[d];
389 if (j >= 0 && !strncmp (langs[j], langs[i], l))
390 {
391 BitSet(map, j);
392 country[d] = -1;
393 }
394 }
395 printf (" {");
396 for (k = 0; k < num_lang_set_map; k++)
397 printf (" 0x%08x,", map[k]);
398 printf (" }, /* %*.*s */\n",
399 l, l, langs[i]);
400 ++ncountry_ent;
401 }
402 }
403 printf ("};\n\n");
404 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent);
405 }
406
407 while (fgets (line, sizeof (line), stdin))
408 fputs (line, stdout);
409
410 fflush (stdout);
411 exit (ferror (stdout));
412 }