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