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
)
83 if (!fgets (line
, 1024, f
))
86 hash
= strchr (line
, '#');
89 if (line
[0] == '\0' || line
[0] == '\n' || line
[0] == '\032' || line
[0] == '\r')
90 return get_line (f
, line
, lineno
);
101 f
= fopen (file
, "r");
109 f
= fopen (path
, "r");
115 * build a single charset from a source file
117 * The file format is quite simple, either
118 * a single hex value or a pair separated with a dash
120 * Comments begin with '#'
124 scan (FILE *f
, char *file
)
128 int start
, end
, ucs4
;
132 while (get_line (f
, line
, &lineno
))
134 if (!strncmp (line
, "include", 7))
136 file
= strchr (line
, ' ');
140 if (file
[end
-1] == '\n')
144 fatal (file
, 0, "can't open");
149 if (strchr (line
, '-'))
151 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
152 fatal (file
, lineno
, "parse error");
156 if (sscanf (line
, "%x", &start
) != 1)
157 fatal (file
, lineno
, "parse error");
161 c
= FcCharSetCreate ();
162 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
164 if (!FcCharSetAddChar (c
, ucs4
))
165 fatal (file
, lineno
, "out of memory");
168 n
= FcCharSetFreeze (c
);
169 FcCharSetDestroy (c
);
174 * Convert a file name into a name suitable for C declarations
177 get_name (char *file
)
182 dot
= strchr (file
, '.');
184 dot
= file
+ strlen(file
);
185 name
= malloc (dot
- file
+ 1);
186 strncpy (name
, file
, dot
- file
);
187 name
[dot
-file
] = '\0';
192 * Convert a C name into a language name
195 get_lang (char *name
)
197 char *lang
= malloc (strlen (name
) + 1);
201 while ((c
= *name
++))
203 if (isupper ((int) (unsigned char) c
))
204 c
= tolower ((int) (unsigned char) c
);
215 static int compare (const void *a
, const void *b
)
217 const FcChar8
*const *as
= a
, *const *bs
= b
;
218 return FcStrCmpIgnoreCase (*as
, *bs
);
221 #define MAX_LANG 1024
222 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
224 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
225 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
228 main (int argc
, char **argv
)
230 static char *files
[MAX_LANG
];
231 static FcCharSet
*sets
[MAX_LANG
];
232 static int duplicate
[MAX_LANG
];
233 static int offsets
[MAX_LANG
];
234 static int country
[MAX_LANG
];
235 static char *names
[MAX_LANG
];
236 static char *langs
[MAX_LANG
];
243 int total_leaves
= 0;
244 int offset_count
= 0;
247 static char line
[1024];
248 static FcChar32 map
[MAX_LANG_SET_MAP
];
249 int num_lang_set_map
;
250 int setRangeStart
[26];
252 FcChar8 setRangeChar
;
257 if (!strcmp (argv
[argi
], "-d"))
264 fatal (argv
[0], 0, "Too many languages");
265 files
[i
++] = argv
[argi
++];
268 qsort (files
, i
, sizeof (char *), compare
);
272 f
= scanopen (files
[i
]);
274 fatal (files
[i
], 0, strerror (errno
));
275 sets
[i
] = scan (f
, files
[i
]);
276 names
[i
] = get_name (files
[i
]);
277 langs
[i
] = get_lang(names
[i
]);
278 if (strchr (langs
[i
], '-'))
279 country
[ncountry
++] = i
;
281 total_leaves
+= sets
[i
]->num
;
286 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
291 for (i
= 0; sets
[i
]; i
++)
293 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
295 for (l
= 0; l
< tl
; l
++)
296 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], sl
))
299 leaves
[tl
++] = FcCharSetGetLeaf(sets
[i
], sl
);
304 * Scan the input until the marker is found
307 while (fgets (line
, sizeof (line
), stdin
))
309 if (!strncmp (line
, "@@@", 3))
311 fputs (line
, stdout
);
314 printf ("/* total size: %d unique leaves: %d */\n\n",
319 printf ("const FcCharLeaf langBankLeaves[%d] = {\n", tl
);
320 for (l
= 0; l
< tl
; l
++)
322 printf (" { { /* %d */", l
);
323 for (i
= 0; i
< 256/32; i
++)
327 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
329 printf ("\n } },\n");
334 * Find duplicate charsets
337 for (i
= 1; sets
[i
]; i
++)
342 for (j
= 0; j
< i
; j
++)
343 if (sets
[j
] == sets
[i
])
351 * Find ranges for each letter for faster searching
354 for (i
= 0; sets
[i
]; i
++)
356 char c
= names
[i
][0];
358 while (setRangeChar
<= c
&& c
<= 'z')
359 setRangeStart
[setRangeChar
++ - 'a'] = i
;
361 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
362 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
363 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
368 for (i
= 0; sets
[i
]; i
++)
372 if (duplicate
[i
] >= 0)
375 for (n
= 0; n
< sets
[i
]->num
; n
++)
377 for (l
= 0; l
< tl
; l
++)
378 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], n
))
381 fatal (names
[i
], 0, "can't find leaf");
385 offset
+= sets
[i
]->num
;
388 printf ("const int langBankLeafIdx[%d] = {\n",
390 for (i
= 0; sets
[i
]; i
++)
394 if (duplicate
[i
] >= 0)
396 for (n
= 0; n
< sets
[i
]->num
; n
++)
400 for (l
= 0; l
< tl
; l
++)
401 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], n
))
404 fatal (names
[i
], 0, "can't find leaf");
414 printf ("const FcChar16 langBankNumbers[%d] = {\n",
417 for (i
= 0; sets
[i
]; i
++)
421 if (duplicate
[i
] >= 0)
423 for (n
= 0; n
< sets
[i
]->num
; n
++)
427 printf (" 0x%04x,", FcCharSetGetNumbers(sets
[i
])[n
]);
440 printf ("const FcLangCharSet fcLangCharSets[] = {\n");
441 for (i
= 0; sets
[i
]; i
++)
443 int j
= duplicate
[i
];
448 printf (" { (FcChar8 *) \"%s\",\n"
449 " { FC_REF_CONSTANT, %d, FC_BANK_LANGS, "
450 "{ .stat = { %d, %d } } } }, /* %d */\n",
452 sets
[j
]->num
, offsets
[j
], offsets
[j
], j
);
455 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
456 num_lang_set_map
= (i
+ 31) / 32;
457 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
459 * Dump indices with country codes
463 int ncountry_ent
= 0;
465 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
466 for (c
= 0; c
< ncountry
; c
++)
471 int l
= strchr (langs
[i
], '-') - langs
[i
];
474 for (k
= 0; k
< num_lang_set_map
; k
++)
478 for (d
= c
+ 1; d
< ncountry
; d
++)
481 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
488 for (k
= 0; k
< num_lang_set_map
; k
++)
489 printf (" 0x%08x,", map
[k
]);
490 printf (" }, /* %*.*s */\n",
496 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
501 * Dump sets start/finish for the fastpath
503 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
504 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
506 printf (" { %d, %d }, /* %c */\n",
507 setRangeStart
[setRangeChar
- 'a'],
508 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
512 while (fgets (line
, sizeof (line
), stdin
))
513 fputs (line
, stdout
);
516 exit (ferror (stdout
));