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
)
56 FcValueList
** _fcValueLists
= 0;
57 FcPatternElt
** _fcPatternElts
= 0;
61 FcCacheBankToIndexMTF (int bank
)
69 return (FcChar8
*) getenv ("HOME");
73 fatal (const char *file
, int lineno
, const char *msg
)
76 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
78 fprintf (stderr
, "%s: %s\n", file
, msg
);
83 get_line (FILE *f
, char *line
, int *lineno
)
87 if (!fgets (line
, 1024, f
))
90 hash
= strchr (line
, '#');
95 while (end
> 0 && isspace (line
[end
-1]))
98 if (line
[0] == '\0' || line
[0] == '\n' || line
[0] == '\032' || line
[0] == '\r')
99 return get_line (f
, line
, lineno
);
103 static char *dir
= 0;
106 scanopen (char *file
)
110 f
= fopen (file
, "r");
118 f
= fopen (path
, "r");
124 * build a single charset from a source file
126 * The file format is quite simple, either
127 * a single hex value or a pair separated with a dash
129 * Comments begin with '#'
133 scan (FILE *f
, char *file
)
137 int start
, end
, ucs4
;
141 while (get_line (f
, line
, &lineno
))
143 if (!strncmp (line
, "include", 7))
145 file
= strchr (line
, ' ');
148 "invalid syntax, expected: include filename");
149 while (isspace(*file
))
153 fatal (file
, 0, "can't open");
158 if (strchr (line
, '-'))
160 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
161 fatal (file
, lineno
, "parse error");
165 if (sscanf (line
, "%x", &start
) != 1)
166 fatal (file
, lineno
, "parse error");
170 c
= FcCharSetCreate ();
171 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
173 if (!FcCharSetAddChar (c
, ucs4
))
174 fatal (file
, lineno
, "out of memory");
177 n
= FcCharSetFreeze (c
);
178 FcCharSetDestroy (c
);
183 * Convert a file name into a name suitable for C declarations
186 get_name (char *file
)
191 dot
= strchr (file
, '.');
193 dot
= file
+ strlen(file
);
194 name
= malloc (dot
- file
+ 1);
195 strncpy (name
, file
, dot
- file
);
196 name
[dot
-file
] = '\0';
201 * Convert a C name into a language name
204 get_lang (char *name
)
206 char *lang
= malloc (strlen (name
) + 1);
210 while ((c
= *name
++))
212 if (isupper ((int) (unsigned char) c
))
213 c
= tolower ((int) (unsigned char) c
);
224 static int compare (const void *a
, const void *b
)
226 const FcChar8
*const *as
= a
, *const *bs
= b
;
227 return FcStrCmpIgnoreCase (*as
, *bs
);
230 #define MAX_LANG 1024
231 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
233 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
234 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
237 main (int argc
, char **argv
)
239 static char *files
[MAX_LANG
];
240 static FcCharSet
*sets
[MAX_LANG
];
241 static int duplicate
[MAX_LANG
];
242 static int offsets
[MAX_LANG
];
243 static int country
[MAX_LANG
];
244 static char *names
[MAX_LANG
];
245 static char *langs
[MAX_LANG
];
252 int total_leaves
= 0;
253 int offset_count
= 0;
255 static char line
[1024];
256 static FcChar32 map
[MAX_LANG_SET_MAP
];
257 int num_lang_set_map
;
258 int setRangeStart
[26];
260 FcChar8 setRangeChar
;
265 if (!strcmp (argv
[argi
], "-d"))
272 fatal (argv
[0], 0, "Too many languages");
273 files
[i
++] = argv
[argi
++];
276 qsort (files
, i
, sizeof (char *), compare
);
280 f
= scanopen (files
[i
]);
282 fatal (files
[i
], 0, strerror (errno
));
283 sets
[i
] = scan (f
, files
[i
]);
284 names
[i
] = get_name (files
[i
]);
285 langs
[i
] = get_lang(names
[i
]);
286 if (strchr (langs
[i
], '-'))
287 country
[ncountry
++] = i
;
289 total_leaves
+= sets
[i
]->num
;
294 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
299 for (i
= 0; sets
[i
]; i
++)
301 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
303 for (l
= 0; l
< tl
; l
++)
304 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], sl
))
307 leaves
[tl
++] = FcCharSetGetLeaf(sets
[i
], sl
);
312 * Scan the input until the marker is found
315 while (fgets (line
, sizeof (line
), stdin
))
317 if (!strncmp (line
, "@@@", 3))
319 fputs (line
, stdout
);
322 printf ("/* total size: %d unique leaves: %d */\n\n",
327 printf ("const FcCharLeaf langBankLeaves[%d] = {\n", tl
);
328 for (l
= 0; l
< tl
; l
++)
330 printf (" { { /* %d */", l
);
331 for (i
= 0; i
< 256/32; i
++)
335 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
337 printf ("\n } },\n");
342 * Find duplicate charsets
345 for (i
= 1; sets
[i
]; i
++)
350 for (j
= 0; j
< i
; j
++)
351 if (sets
[j
] == sets
[i
])
359 * Find ranges for each letter for faster searching
362 for (i
= 0; sets
[i
]; i
++)
364 char c
= names
[i
][0];
366 while (setRangeChar
<= c
&& c
<= 'z')
367 setRangeStart
[setRangeChar
++ - 'a'] = i
;
369 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
370 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
371 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
376 for (i
= 0; sets
[i
]; i
++)
380 if (duplicate
[i
] >= 0)
383 for (n
= 0; n
< sets
[i
]->num
; n
++)
385 for (l
= 0; l
< tl
; l
++)
386 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], n
))
389 fatal (names
[i
], 0, "can't find leaf");
393 offset
+= sets
[i
]->num
;
396 printf ("const int langBankLeafIdx[%d] = {\n",
398 for (i
= 0; sets
[i
]; i
++)
402 if (duplicate
[i
] >= 0)
404 for (n
= 0; n
< sets
[i
]->num
; n
++)
408 for (l
= 0; l
< tl
; l
++)
409 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], n
))
412 fatal (names
[i
], 0, "can't find leaf");
422 printf ("const FcChar16 langBankNumbers[%d] = {\n",
425 for (i
= 0; sets
[i
]; i
++)
429 if (duplicate
[i
] >= 0)
431 for (n
= 0; n
< sets
[i
]->num
; n
++)
435 printf (" 0x%04x,", FcCharSetGetNumbers(sets
[i
])[n
]);
448 printf ("const FcLangCharSet fcLangCharSets[] = {\n");
449 for (i
= 0; sets
[i
]; i
++)
451 int j
= duplicate
[i
];
456 printf (" { (FcChar8 *) \"%s\",\n"
457 " { FC_REF_CONSTANT, %d, FC_BANK_LANGS, "
458 "{ { %d, %d } } } }, /* %d */\n",
460 sets
[j
]->num
, offsets
[j
], offsets
[j
], j
);
463 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
464 num_lang_set_map
= (i
+ 31) / 32;
465 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
467 * Dump indices with country codes
472 int ncountry_ent
= 0;
474 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
475 for (c
= 0; c
< ncountry
; c
++)
480 int lang
= strchr (langs
[i
], '-') - langs
[i
];
483 for (k
= 0; k
< num_lang_set_map
; k
++)
487 for (d
= c
+ 1; d
< ncountry
; d
++)
490 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
497 for (k
= 0; k
< num_lang_set_map
; k
++)
498 printf (" 0x%08x,", map
[k
]);
499 printf (" }, /* %*.*s */\n",
500 lang
, lang
, langs
[i
]);
505 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
510 * Dump sets start/finish for the fastpath
512 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
513 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
515 printf (" { %d, %d }, /* %c */\n",
516 setRangeStart
[setRangeChar
- 'a'],
517 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
521 while (fgets (line
, sizeof (line
), stdin
))
522 fputs (line
, stdout
);
525 exit (ferror (stdout
));