]>
git.wh0rd.org - fontconfig.git/blob - fc-lang/fc-lang.c
2 * $RCSId: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.3 2002/08/22 07:36:43 keithp Exp $
4 * Copyright © 2002 Keith Packard
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.
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.
26 #include "fccharset.c"
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
41 FcMemAlloc (int kind
, int size
)
46 FcMemFree (int kind
, int size
)
51 FcCacheBankToIndex (int bank
)
59 return (FcChar8
*) getenv ("HOME");
63 fatal (const char *file
, int lineno
, const char *msg
)
66 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
68 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
73 get_line (FILE *f
, char *line
, int *lineno
)
76 if (!fgets (line
, 1024, f
))
79 hash
= strchr (line
, '#');
82 if (line
[0] == '\0' || line
[0] == '\n' || line
[0] == '\032' || line
[0] == '\r')
83 return get_line (f
, line
, lineno
);
94 f
= fopen (file
, "r");
102 f
= fopen (path
, "r");
108 * build a single charset from a source file
110 * The file format is quite simple, either
111 * a single hex value or a pair separated with a dash
113 * Comments begin with '#'
117 scan (FILE *f
, char *file
)
121 int start
, end
, ucs4
;
125 while (get_line (f
, line
, &lineno
))
127 if (!strncmp (line
, "include", 7))
129 file
= strchr (line
, ' ');
133 if (file
[end
-1] == '\n')
137 fatal (file
, 0, "can't open");
142 if (strchr (line
, '-'))
144 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
145 fatal (file
, lineno
, "parse error");
149 if (sscanf (line
, "%x", &start
) != 1)
150 fatal (file
, lineno
, "parse error");
154 c
= FcCharSetCreate ();
155 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
157 if (!FcCharSetAddChar (c
, ucs4
))
158 fatal (file
, lineno
, "out of memory");
161 n
= FcCharSetFreeze (c
);
162 FcCharSetDestroy (c
);
167 * Convert a file name into a name suitable for C declarations
170 get_name (char *file
)
175 dot
= strchr (file
, '.');
177 dot
= file
+ strlen(file
);
178 name
= malloc (dot
- file
+ 1);
179 strncpy (name
, file
, dot
- file
);
180 name
[dot
-file
] = '\0';
185 * Convert a C name into a language name
188 get_lang (char *name
)
190 char *lang
= malloc (strlen (name
) + 1);
194 while ((c
= *name
++))
196 if (isupper ((int) (unsigned char) c
))
197 c
= tolower ((int) (unsigned char) c
);
208 static int compare (const void *a
, const void *b
)
210 const FcChar8
*const *as
= a
, *const *bs
= b
;
211 return FcStrCmpIgnoreCase (*as
, *bs
);
214 #define MAX_LANG 1024
215 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
217 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
218 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
221 main (int argc
, char **argv
)
223 char *files
[MAX_LANG
];
224 FcCharSet
*sets
[MAX_LANG
];
225 int duplicate
[MAX_LANG
];
226 int country
[MAX_LANG
];
227 char *names
[MAX_LANG
];
228 char *langs
[MAX_LANG
];
234 int total_leaves
= 0;
238 FcChar32 map
[MAX_LANG_SET_MAP
];
239 int num_lang_set_map
;
240 int setRangeStart
[26];
242 FcChar8 setRangeChar
;
247 if (!strcmp (argv
[argi
], "-d"))
254 fatal (argv
[0], 0, "Too many languages");
255 files
[i
++] = argv
[argi
++];
258 qsort (files
, i
, sizeof (char *), compare
);
262 f
= scanopen (files
[i
]);
264 fatal (files
[i
], 0, strerror (errno
));
265 sets
[i
] = scan (f
, files
[i
]);
266 names
[i
] = get_name (files
[i
]);
267 langs
[i
] = get_lang(names
[i
]);
268 if (strchr (langs
[i
], '-'))
269 country
[ncountry
++] = i
;
271 total_leaves
+= sets
[i
]->num
;
276 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
281 for (i
= 0; sets
[i
]; i
++)
283 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
285 for (l
= 0; l
< tl
; l
++)
286 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], sl
))
289 leaves
[tl
++] = FcCharSetGetLeaf(sets
[i
], sl
);
294 * Scan the input until the marker is found
297 while (fgets (line
, sizeof (line
), stdin
))
299 if (!strncmp (line
, "@@@", 3))
301 fputs (line
, stdout
);
304 printf ("/* total size: %d unique leaves: %d */\n\n",
309 printf ("static const FcCharLeaf leaves[%d] = {\n", tl
);
310 for (l
= 0; l
< tl
; l
++)
312 printf (" { { /* %d */", l
);
313 for (i
= 0; i
< 256/32; i
++)
317 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
319 printf ("\n } },\n");
322 printf ("#define L(n) ((FcCharLeaf *) &leaves[n])\n\n");
325 * Find duplicate charsets
328 for (i
= 1; sets
[i
]; i
++)
333 for (j
= 0; j
< i
; j
++)
334 if (sets
[j
] == sets
[i
])
342 * Find ranges for each letter for faster searching
345 for (i
= 0; sets
[i
]; i
++)
347 char c
= names
[i
][0];
349 while (setRangeChar
<= c
&& c
<= 'z')
350 setRangeStart
[setRangeChar
++ - 'a'] = i
;
352 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
353 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
354 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
359 for (i
= 0; sets
[i
]; i
++)
363 if (duplicate
[i
] >= 0)
365 printf ("static const FcCharLeaf *leaves_%s[%d] = {\n",
366 names
[i
], sets
[i
]->num
);
367 for (n
= 0; n
< sets
[i
]->num
; n
++)
371 for (l
= 0; l
< tl
; l
++)
372 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], n
))
375 fatal (names
[i
], 0, "can't find leaf");
376 printf (" L(%3d),", l
);
385 printf ("static const FcChar16 numbers_%s[%d] = {\n",
386 names
[i
], sets
[i
]->num
);
387 for (n
= 0; n
< sets
[i
]->num
; n
++)
391 printf (" 0x%04x,", FcCharSetGetNumbers(sets
[i
])[n
]);
399 printf ("#undef L\n\n");
405 printf ("static const FcLangCharSet fcLangCharSets[] = {\n");
406 for (i
= 0; sets
[i
]; i
++)
408 int j
= duplicate
[i
];
412 printf (" { (FcChar8 *) \"%s\",\n"
413 " { FC_REF_CONSTANT, %d, FC_BANK_DYNAMIC, "
414 "{ { (FcCharLeaf **) leaves_%s, "
415 "(FcChar16 *) numbers_%s } } } },\n",
417 sets
[j
]->num
, names
[j
], names
[j
]);
420 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
421 num_lang_set_map
= (i
+ 31) / 32;
422 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
424 * Dump indices with country codes
428 int ncountry_ent
= 0;
430 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
431 for (c
= 0; c
< ncountry
; c
++)
436 int l
= strchr (langs
[i
], '-') - langs
[i
];
439 for (k
= 0; k
< num_lang_set_map
; k
++)
443 for (d
= c
+ 1; d
< ncountry
; d
++)
446 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
453 for (k
= 0; k
< num_lang_set_map
; k
++)
454 printf (" 0x%08x,", map
[k
]);
455 printf (" }, /* %*.*s */\n",
461 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
466 * Dump sets start/finish for the fastpath
468 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
469 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
471 printf (" { %d, %d }, /* %c */\n",
472 setRangeStart
[setRangeChar
- 'a'],
473 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
477 while (fgets (line
, sizeof (line
), stdin
))
478 fputs (line
, stdout
);
481 exit (ferror (stdout
));