]>
git.wh0rd.org - fontconfig.git/blob - fc-lang/fc-lang.c
e302c4a69f6a0db82286b4a8d3d099aa7efb1d3c
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 FcCacheNextOffset (int fd
)
59 return getenv ("HOME");
63 fatal (char *file
, int lineno
, char *msg
)
65 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
70 get_line (FILE *f
, char *line
, int *lineno
)
73 if (!fgets (line
, 1024, f
))
76 hash
= strchr (line
, '#');
79 if (line
[0] == '\0' || line
[0] == '\n' || line
[0] == '\032' || line
[0] == '\r')
80 return get_line (f
, line
, lineno
);
91 f
= fopen (file
, "r");
99 f
= fopen (path
, "r");
105 * build a single charset from a source file
107 * The file format is quite simple, either
108 * a single hex value or a pair separated with a dash
110 * Comments begin with '#'
114 scan (FILE *f
, char *file
)
118 int start
, end
, ucs4
;
122 while (get_line (f
, line
, &lineno
))
124 if (!strncmp (line
, "include", 7))
126 file
= strchr (line
, ' ');
130 if (file
[end
-1] == '\n')
134 fatal (file
, 0, "can't open");
139 if (strchr (line
, '-'))
141 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
142 fatal (file
, lineno
, "parse error");
146 if (sscanf (line
, "%x", &start
) != 1)
147 fatal (file
, lineno
, "parse error");
151 c
= FcCharSetCreate ();
152 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
154 if (!FcCharSetAddChar (c
, ucs4
))
155 fatal (file
, lineno
, "out of memory");
158 n
= FcCharSetFreeze (c
);
159 FcCharSetDestroy (c
);
164 * Convert a file name into a name suitable for C declarations
167 get_name (char *file
)
172 dot
= strchr (file
, '.');
174 dot
= file
+ strlen(file
);
175 name
= malloc (dot
- file
+ 1);
176 strncpy (name
, file
, dot
- file
);
177 name
[dot
-file
] = '\0';
182 * Convert a C name into a language name
185 get_lang (char *name
)
187 char *lang
= malloc (strlen (name
) + 1);
191 while ((c
= *name
++))
193 if (isupper ((int) (unsigned char) c
))
194 c
= tolower ((int) (unsigned char) c
);
205 static int compare (const void *a
, const void *b
)
207 const FcChar8
*const *as
= a
, *const *bs
= b
;
208 return FcStrCmpIgnoreCase (*as
, *bs
);
211 #define MAX_LANG 1024
212 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
214 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
215 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
218 main (int argc
, char **argv
)
220 char *files
[MAX_LANG
];
221 FcCharSet
*sets
[MAX_LANG
];
222 int duplicate
[MAX_LANG
];
223 int country
[MAX_LANG
];
224 char *names
[MAX_LANG
];
225 char *langs
[MAX_LANG
];
230 int total_leaves
= 0;
234 FcChar32 map
[MAX_LANG_SET_MAP
];
235 int num_lang_set_map
;
236 int setRangeStart
[26];
238 FcChar8 setRangeChar
;
242 if (!strcmp (*argv
, "-d"))
248 fatal (*argv
, 0, "Too many languages");
252 qsort (files
, i
, sizeof (char *), compare
);
256 f
= scanopen (files
[i
]);
258 fatal (files
[i
], 0, strerror (errno
));
259 sets
[i
] = scan (f
, files
[i
]);
260 names
[i
] = get_name (files
[i
]);
261 langs
[i
] = get_lang(names
[i
]);
262 if (strchr (langs
[i
], '-'))
263 country
[ncountry
++] = i
;
265 total_leaves
+= sets
[i
]->num
;
270 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
275 for (i
= 0; sets
[i
]; i
++)
277 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
279 for (l
= 0; l
< tl
; l
++)
280 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], sl
))
283 leaves
[tl
++] = FcCharSetGetLeaf(sets
[i
], sl
);
288 * Scan the input until the marker is found
291 while (fgets (line
, sizeof (line
), stdin
))
293 if (!strncmp (line
, "@@@", 3))
295 fputs (line
, stdout
);
298 printf ("/* total size: %d unique leaves: %d */\n\n",
303 printf ("static const FcCharLeaf leaves[%d] = {\n", tl
);
304 for (l
= 0; l
< tl
; l
++)
306 printf (" { { /* %d */", l
);
307 for (i
= 0; i
< 256/32; i
++)
311 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
313 printf ("\n } },\n");
316 printf ("#define L(n) ((FcCharLeaf *) &leaves[n])\n\n");
319 * Find duplicate charsets
322 for (i
= 1; sets
[i
]; i
++)
327 for (j
= 0; j
< i
; j
++)
328 if (sets
[j
] == sets
[i
])
336 * Find ranges for each letter for faster searching
339 for (i
= 0; sets
[i
]; i
++)
341 char c
= names
[i
][0];
343 while (setRangeChar
<= c
&& c
<= 'z')
344 setRangeStart
[setRangeChar
++ - 'a'] = i
;
346 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
347 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
348 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
353 for (i
= 0; sets
[i
]; i
++)
357 if (duplicate
[i
] >= 0)
359 printf ("static const FcCharLeaf *leaves_%s[%d] = {\n",
360 names
[i
], sets
[i
]->num
);
361 for (n
= 0; n
< sets
[i
]->num
; n
++)
365 for (l
= 0; l
< tl
; l
++)
366 if (leaves
[l
] == FcCharSetGetLeaf(sets
[i
], n
))
369 fatal (names
[i
], 0, "can't find leaf");
370 printf (" L(%3d),", l
);
379 printf ("static const FcChar16 numbers_%s[%d] = {\n",
380 names
[i
], sets
[i
]->num
);
381 for (n
= 0; n
< sets
[i
]->num
; n
++)
385 printf (" 0x%04x,", FcCharSetGetNumbers(sets
[i
])[n
]);
393 printf ("#undef L\n\n");
399 printf ("static const FcLangCharSet fcLangCharSets[] = {\n");
400 for (i
= 0; sets
[i
]; i
++)
402 int j
= duplicate
[i
];
406 printf (" { (FcChar8 *) \"%s\",\n"
407 " { FC_REF_CONSTANT, %d, FcStorageDynamic, "
408 "{ { (FcCharLeaf **) leaves_%s, "
409 "(FcChar16 *) numbers_%s } } } },\n",
411 sets
[j
]->num
, names
[j
], names
[j
]);
414 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
415 num_lang_set_map
= (i
+ 31) / 32;
416 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
418 * Dump indices with country codes
422 int ncountry_ent
= 0;
424 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
425 for (c
= 0; c
< ncountry
; c
++)
430 int l
= strchr (langs
[i
], '-') - langs
[i
];
433 for (k
= 0; k
< num_lang_set_map
; k
++)
437 for (d
= c
+ 1; d
< ncountry
; d
++)
440 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
447 for (k
= 0; k
< num_lang_set_map
; k
++)
448 printf (" 0x%08x,", map
[k
]);
449 printf (" }, /* %*.*s */\n",
455 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
460 * Dump sets start/finish for the fastpath
462 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
463 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
465 printf (" { %d, %d }, /* %c */\n",
466 setRangeStart
[setRangeChar
- 'a'],
467 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
471 while (fgets (line
, sizeof (line
), stdin
))
472 fputs (line
, stdout
);
475 exit (ferror (stdout
));