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
40 const FcChar16 langBankNumbers
[1]; /* place holders so that externs resolve */
41 const FcCharLeaf langBankLeaves
[1];
42 const int langBankLeafIdx
[1];
45 FcMemAlloc (int kind
, int size
)
50 FcMemFree (int kind
, int size
)
58 FcCacheBankToIndexMTF (int bank
)
66 return (FcChar8
*) getenv ("HOME");
70 fatal (const char *file
, int lineno
, const char *msg
)
73 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
75 fprintf (stderr
, "%s: %s\n", file
, msg
);
80 get_line (FILE *f
, char *line
, int *lineno
)
84 if (!fgets (line
, 1024, f
))
87 hash
= strchr (line
, '#');
92 while (end
> 0 && isspace (line
[end
-1]))
95 if (line
[0] == '\0' || line
[0] == '\n' || line
[0] == '\032' || line
[0] == '\r')
96 return get_line (f
, line
, lineno
);
100 static char *dir
= 0;
103 scanopen (char *file
)
107 f
= fopen (file
, "r");
115 f
= fopen (path
, "r");
121 * build a single charset from a source file
123 * The file format is quite simple, either
124 * a single hex value or a pair separated with a dash
126 * Comments begin with '#'
130 scan (FILE *f
, char *file
)
134 int start
, end
, ucs4
;
138 while (get_line (f
, line
, &lineno
))
140 if (!strncmp (line
, "include", 7))
142 file
= strchr (line
, ' ');
143 while (isspace(*file
))
147 fatal (file
, 0, "can't open");
152 if (strchr (line
, '-'))
154 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
155 fatal (file
, lineno
, "parse error");
159 if (sscanf (line
, "%x", &start
) != 1)
160 fatal (file
, lineno
, "parse error");
164 c
= FcCharSetCreate ();
165 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
167 if (!FcCharSetAddChar (c
, ucs4
))
168 fatal (file
, lineno
, "out of memory");
171 n
= FcCharSetFreeze (c
);
172 FcCharSetDestroy (c
);
177 * Convert a file name into a name suitable for C declarations
180 get_name (char *file
)
185 dot
= strchr (file
, '.');
187 dot
= file
+ strlen(file
);
188 name
= malloc (dot
- file
+ 1);
189 strncpy (name
, file
, dot
- file
);
190 name
[dot
-file
] = '\0';
195 * Convert a C name into a language name
198 get_lang (char *name
)
200 char *lang
= malloc (strlen (name
) + 1);
204 while ((c
= *name
++))
206 if (isupper ((int) (unsigned char) c
))
207 c
= tolower ((int) (unsigned char) c
);
218 static int compare (const void *a
, const void *b
)
220 const FcChar8
*const *as
= a
, *const *bs
= b
;
221 return FcStrCmpIgnoreCase (*as
, *bs
);
224 #define MAX_LANG 1024
225 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
227 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
228 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
231 main (int argc
, char **argv
)
233 static char *files
[MAX_LANG
];
234 static FcCharSet
*sets
[MAX_LANG
];
235 static int duplicate
[MAX_LANG
];
236 static int offsets
[MAX_LANG
];
237 static int country
[MAX_LANG
];
238 static char *names
[MAX_LANG
];
239 static char *langs
[MAX_LANG
];
246 int total_leaves
= 0;
247 int offset_count
= 0;
249 static char line
[1024];
250 static FcChar32 map
[MAX_LANG_SET_MAP
];
251 int num_lang_set_map
;
252 int setRangeStart
[26];
254 FcChar8 setRangeChar
;
259 if (!strcmp (argv
[argi
], "-d"))
266 fatal (argv
[0], 0, "Too many languages");
267 files
[i
++] = argv
[argi
++];
270 qsort (files
, i
, sizeof (char *), compare
);
274 f
= scanopen (files
[i
]);
276 fatal (files
[i
], 0, strerror (errno
));
277 sets
[i
] = scan (f
, files
[i
]);
278 names
[i
] = get_name (files
[i
]);
279 langs
[i
] = get_lang(names
[i
]);
280 if (strchr (langs
[i
], '-'))
281 country
[ncountry
++] = i
;
283 total_leaves
+= sets
[i
]->num
;
288 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
293 for (i
= 0; sets
[i
]; i
++)
295 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
297 for (l
= 0; l
< tl
; l
++)
298 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], sl
))
301 leaves
[tl
++] = FcCharSetGetLeaf(sets
[i
], sl
);
306 * Scan the input until the marker is found
309 while (fgets (line
, sizeof (line
), stdin
))
311 if (!strncmp (line
, "@@@", 3))
313 fputs (line
, stdout
);
316 printf ("/* total size: %d unique leaves: %d */\n\n",
321 printf ("const FcCharLeaf langBankLeaves[%d] = {\n", tl
);
322 for (l
= 0; l
< tl
; l
++)
324 printf (" { { /* %d */", l
);
325 for (i
= 0; i
< 256/32; i
++)
329 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
331 printf ("\n } },\n");
336 * Find duplicate charsets
339 for (i
= 1; sets
[i
]; i
++)
344 for (j
= 0; j
< i
; j
++)
345 if (sets
[j
] == sets
[i
])
353 * Find ranges for each letter for faster searching
356 for (i
= 0; sets
[i
]; i
++)
358 char c
= names
[i
][0];
360 while (setRangeChar
<= c
&& c
<= 'z')
361 setRangeStart
[setRangeChar
++ - 'a'] = i
;
363 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
364 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
365 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
370 for (i
= 0; sets
[i
]; i
++)
374 if (duplicate
[i
] >= 0)
377 for (n
= 0; n
< sets
[i
]->num
; n
++)
379 for (l
= 0; l
< tl
; l
++)
380 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], n
))
383 fatal (names
[i
], 0, "can't find leaf");
387 offset
+= sets
[i
]->num
;
390 printf ("const int langBankLeafIdx[%d] = {\n",
392 for (i
= 0; sets
[i
]; i
++)
396 if (duplicate
[i
] >= 0)
398 for (n
= 0; n
< sets
[i
]->num
; n
++)
402 for (l
= 0; l
< tl
; l
++)
403 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], n
))
406 fatal (names
[i
], 0, "can't find leaf");
416 printf ("const FcChar16 langBankNumbers[%d] = {\n",
419 for (i
= 0; sets
[i
]; i
++)
423 if (duplicate
[i
] >= 0)
425 for (n
= 0; n
< sets
[i
]->num
; n
++)
429 printf (" 0x%04x,", FcCharSetGetNumbers(sets
[i
])[n
]);
442 printf ("const FcLangCharSet fcLangCharSets[] = {\n");
443 for (i
= 0; sets
[i
]; i
++)
445 int j
= duplicate
[i
];
450 printf (" { (FcChar8 *) \"%s\",\n"
451 " { FC_REF_CONSTANT, %d, FC_BANK_LANGS, "
452 "{ .stat = { %d, %d } } } }, /* %d */\n",
454 sets
[j
]->num
, offsets
[j
], offsets
[j
], j
);
457 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
458 num_lang_set_map
= (i
+ 31) / 32;
459 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
461 * Dump indices with country codes
466 int ncountry_ent
= 0;
468 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
469 for (c
= 0; c
< ncountry
; c
++)
474 int lang
= strchr (langs
[i
], '-') - langs
[i
];
477 for (k
= 0; k
< num_lang_set_map
; k
++)
481 for (d
= c
+ 1; d
< ncountry
; d
++)
484 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
491 for (k
= 0; k
< num_lang_set_map
; k
++)
492 printf (" 0x%08x,", map
[k
]);
493 printf (" }, /* %*.*s */\n",
494 lang
, lang
, langs
[i
]);
499 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
504 * Dump sets start/finish for the fastpath
506 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
507 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
509 printf (" { %d, %d }, /* %c */\n",
510 setRangeStart
[setRangeChar
- 'a'],
511 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
515 while (fgets (line
, sizeof (line
), stdin
))
516 fputs (line
, stdout
);
519 exit (ferror (stdout
));