]>
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
)
53 return getenv ("HOME");
57 fatal (char *file
, int lineno
, char *msg
)
59 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
64 get_line (FILE *f
, char *line
, int *lineno
)
67 if (!fgets (line
, 1024, f
))
70 hash
= strchr (line
, '#');
73 if (line
[0] == '\0' || line
[0] == '\n' || line
[0] == '\032' || line
[0] == '\r')
74 return get_line (f
, line
, lineno
);
79 * build a single charset from a source file
81 * The file format is quite simple, either
82 * a single hex value or a pair separated with a dash
84 * Comments begin with '#'
88 scan (FILE *f
, char *file
)
96 while (get_line (f
, line
, &lineno
))
98 if (!strncmp (line
, "include", 7))
100 file
= strchr (line
, ' ');
104 if (file
[end
-1] == '\n')
106 f
= fopen (file
, "r");
108 fatal (file
, 0, "can't open");
113 if (strchr (line
, '-'))
115 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
116 fatal (file
, lineno
, "parse error");
120 if (sscanf (line
, "%x", &start
) != 1)
121 fatal (file
, lineno
, "parse error");
125 c
= FcCharSetCreate ();
126 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
128 if (!FcCharSetAddChar (c
, ucs4
))
129 fatal (file
, lineno
, "out of memory");
132 n
= FcCharSetFreeze (c
);
133 FcCharSetDestroy (c
);
138 * Convert a file name into a name suitable for C declarations
141 get_name (char *file
)
146 dot
= strchr (file
, '.');
148 dot
= file
+ strlen(file
);
149 name
= malloc (dot
- file
+ 1);
150 strncpy (name
, file
, dot
- file
);
151 name
[dot
-file
] = '\0';
156 * Convert a C name into a language name
159 get_lang (char *name
)
161 char *lang
= malloc (strlen (name
) + 1);
165 while ((c
= *name
++))
167 if (isupper ((int) (unsigned char) c
))
168 c
= tolower ((int) (unsigned char) c
);
179 static int compare (const void *a
, const void *b
)
181 const FcChar8
*const *as
= a
, *const *bs
= b
;
182 return FcStrCmpIgnoreCase (*as
, *bs
);
185 #define MAX_LANG 1024
186 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
188 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
189 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
192 main (int argc
, char **argv
)
194 char *files
[MAX_LANG
];
195 FcCharSet
*sets
[MAX_LANG
];
196 int duplicate
[MAX_LANG
];
197 int country
[MAX_LANG
];
198 char *names
[MAX_LANG
];
199 char *langs
[MAX_LANG
];
203 FcCharLeaf
**leaves
, **sleaves
;
204 int total_leaves
= 0;
208 FcChar32 map
[MAX_LANG_SET_MAP
];
209 int num_lang_set_map
;
210 int setRangeStart
[26];
212 FcChar8 setRangeChar
;
217 fatal (*argv
, 0, "Too many languages");
221 qsort (files
, i
, sizeof (char *), compare
);
225 f
= fopen (files
[i
], "r");
227 fatal (files
[i
], 0, strerror (errno
));
228 sets
[i
] = scan (f
, files
[i
]);
229 names
[i
] = get_name (files
[i
]);
230 langs
[i
] = get_lang(names
[i
]);
231 if (strchr (langs
[i
], '-'))
232 country
[ncountry
++] = i
;
234 total_leaves
+= sets
[i
]->num
;
239 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
244 for (i
= 0; sets
[i
]; i
++)
246 sleaves
= sets
[i
]->leaves
;
247 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
249 for (l
= 0; l
< tl
; l
++)
250 if (leaves
[l
] == sleaves
[sl
])
253 leaves
[tl
++] = sleaves
[sl
];
258 * Scan the input until the marker is found
261 while (fgets (line
, sizeof (line
), stdin
))
263 if (!strncmp (line
, "@@@", 3))
265 fputs (line
, stdout
);
268 printf ("/* total size: %d unique leaves: %d */\n\n",
273 printf ("static const FcCharLeaf leaves[%d] = {\n", tl
);
274 for (l
= 0; l
< tl
; l
++)
276 printf (" { { /* %d */", l
);
277 for (i
= 0; i
< 256/32; i
++)
281 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
283 printf ("\n } },\n");
286 printf ("#define L(n) ((FcCharLeaf *) &leaves[n])\n\n");
289 * Find duplicate charsets
292 for (i
= 1; sets
[i
]; i
++)
297 for (j
= 0; j
< i
; j
++)
298 if (sets
[j
] == sets
[i
])
306 * Find ranges for each letter for faster searching
309 for (i
= 0; sets
[i
]; i
++)
311 char c
= names
[i
][0];
313 while (setRangeChar
<= c
&& c
<= 'z')
314 setRangeStart
[setRangeChar
++ - 'a'] = i
;
316 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
317 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
318 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
323 for (i
= 0; sets
[i
]; i
++)
327 if (duplicate
[i
] >= 0)
329 printf ("static const FcCharLeaf *leaves_%s[%d] = {\n",
330 names
[i
], sets
[i
]->num
);
331 for (n
= 0; n
< sets
[i
]->num
; n
++)
335 for (l
= 0; l
< tl
; l
++)
336 if (leaves
[l
] == sets
[i
]->leaves
[n
])
339 fatal (names
[i
], 0, "can't find leaf");
340 printf (" L(%3d),", l
);
349 printf ("static const FcChar16 numbers_%s[%d] = {\n",
350 names
[i
], sets
[i
]->num
);
351 for (n
= 0; n
< sets
[i
]->num
; n
++)
355 printf (" 0x%04x,", sets
[i
]->numbers
[n
]);
363 printf ("#undef L\n\n");
369 printf ("static const FcLangCharSet fcLangCharSets[] = {\n");
370 for (i
= 0; sets
[i
]; i
++)
372 int j
= duplicate
[i
];
376 printf (" { (FcChar8 *) \"%s\",\n"
377 " { FC_REF_CONSTANT, %d, "
378 "(FcCharLeaf **) leaves_%s, "
379 "(FcChar16 *) numbers_%s } },\n",
381 sets
[j
]->num
, names
[j
], names
[j
]);
384 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
385 num_lang_set_map
= (i
+ 31) / 32;
386 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
388 * Dump indices with country codes
392 int ncountry_ent
= 0;
394 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
395 for (c
= 0; c
< ncountry
; c
++)
400 int l
= strchr (langs
[i
], '-') - langs
[i
];
403 for (k
= 0; k
< num_lang_set_map
; k
++)
407 for (d
= c
+ 1; d
< ncountry
; d
++)
410 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
417 for (k
= 0; k
< num_lang_set_map
; k
++)
418 printf (" 0x%08x,", map
[k
]);
419 printf (" }, /* %*.*s */\n",
425 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
430 * Dump sets start/finish for the fastpath
432 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
433 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
435 printf (" { %d, %d }, /* %c */\n",
436 setRangeStart
[setRangeChar
- 'a'],
437 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
441 while (fgets (line
, sizeof (line
), stdin
))
442 fputs (line
, stdout
);
445 exit (ferror (stdout
));