]>
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"
28 #include "fcserialize.c"
33 * Read a set of language orthographies and build C declarations for
34 * charsets which can then be used to identify which languages are
35 * supported by a given font. Note that this uses some utilities
36 * from the fontconfig library, so the necessary file is simply
37 * included in this compilation. A couple of extra utility
38 * functions are also needed in slightly modified form
42 FcMemAlloc (int kind
, int size
)
47 FcMemFree (int kind
, int size
)
52 FcCacheObjectReference (void *object
)
57 FcCacheObjectDereference (void *object
)
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 '#'
129 static const FcCharSet
*
130 scan (FILE *f
, char *file
, FcCharSetFreezer
*freezer
)
134 int start
, end
, ucs4
;
138 while (get_line (f
, line
, &lineno
))
140 if (!strncmp (line
, "include", 7))
142 file
= strchr (line
, ' ');
145 "invalid syntax, expected: include filename");
146 while (isspace(*file
))
150 fatal (file
, 0, "can't open");
151 n
= scan (f
, file
, freezer
);
155 if (strchr (line
, '-'))
157 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
158 fatal (file
, lineno
, "parse error");
162 if (sscanf (line
, "%x", &start
) != 1)
163 fatal (file
, lineno
, "parse error");
167 c
= FcCharSetCreate ();
168 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
170 if (!FcCharSetAddChar (c
, ucs4
))
171 fatal (file
, lineno
, "out of memory");
174 n
= FcCharSetFreeze (freezer
, c
);
175 FcCharSetDestroy (c
);
180 * Convert a file name into a name suitable for C declarations
183 get_name (char *file
)
188 dot
= strchr (file
, '.');
190 dot
= file
+ strlen(file
);
191 name
= malloc (dot
- file
+ 1);
192 strncpy (name
, file
, dot
- file
);
193 name
[dot
-file
] = '\0';
198 * Convert a C name into a language name
201 get_lang (char *name
)
203 char *lang
= malloc (strlen (name
) + 1);
207 while ((c
= *name
++))
209 if (isupper ((int) (unsigned char) c
))
210 c
= tolower ((int) (unsigned char) c
);
221 static int compare (const void *a
, const void *b
)
223 const FcChar8
*const *as
= a
, *const *bs
= b
;
224 return FcStrCmpIgnoreCase (*as
, *bs
);
227 #define MAX_LANG 1024
228 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
230 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
231 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
234 main (int argc
, char **argv
)
236 static char *files
[MAX_LANG
];
237 static const FcCharSet
*sets
[MAX_LANG
];
238 static int duplicate
[MAX_LANG
];
239 static int country
[MAX_LANG
];
240 static char *names
[MAX_LANG
];
241 static char *langs
[MAX_LANG
];
242 static int off
[MAX_LANG
];
249 int total_leaves
= 0;
251 static char line
[1024];
252 static FcChar32 map
[MAX_LANG_SET_MAP
];
253 int num_lang_set_map
;
254 int setRangeStart
[26];
256 FcChar8 setRangeChar
;
257 FcCharSetFreezer
*freezer
;
259 freezer
= FcCharSetFreezerCreate ();
261 fatal (argv
[0], 0, "out of memory");
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
], freezer
);
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
;
295 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
300 for (i
= 0; sets
[i
]; i
++)
302 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
304 for (l
= 0; l
< tl
; l
++)
305 if (leaves
[l
] == FcCharSetLeaf(sets
[i
], sl
))
308 leaves
[tl
++] = FcCharSetLeaf(sets
[i
], sl
);
313 * Scan the input until the marker is found
316 while (fgets (line
, sizeof (line
), stdin
))
318 if (!strncmp (line
, "@@@", 3))
320 fputs (line
, stdout
);
323 printf ("/* total size: %d unique leaves: %d */\n\n",
327 * Find duplicate charsets
330 for (i
= 1; sets
[i
]; i
++)
335 for (j
= 0; j
< i
; j
++)
336 if (sets
[j
] == sets
[i
])
344 for (i
= 0; sets
[i
]; i
++) {
345 if (duplicate
[i
] >= 0)
351 printf ("#define LEAF0 (%d * sizeof (FcLangCharSet))\n", nsets
);
352 printf ("#define OFF0 (LEAF0 + %d * sizeof (FcCharLeaf))\n", tl
);
353 printf ("#define NUM0 (OFF0 + %d * sizeof (intptr_t))\n", tn
);
354 printf ("#define SET(n) (n * sizeof (FcLangCharSet) + offsetof (FcLangCharSet, charset))\n");
355 printf ("#define OFF(s,o) (OFF0 + o * sizeof (intptr_t) - SET(s))\n");
356 printf ("#define NUM(s,n) (NUM0 + n * sizeof (FcChar16) - SET(s))\n");
357 printf ("#define LEAF(o,l) (LEAF0 + l * sizeof (FcCharLeaf) - (OFF0 + o * sizeof (intptr_t)))\n");
358 printf ("#define fcLangCharSets (fcLangData.langCharSets)\n");
361 printf ("static const struct {\n"
362 " FcLangCharSet langCharSets[%d];\n"
363 " FcCharLeaf leaves[%d];\n"
364 " intptr_t leaf_offsets[%d];\n"
365 " FcChar16 numbers[%d];\n"
366 "} fcLangData = {\n",
374 for (i
= 0; sets
[i
]; i
++)
376 int j
= duplicate
[i
];
381 printf (" { (FcChar8 *) \"%s\", "
382 " { FC_REF_CONSTANT, %d, OFF(%d,%d), NUM(%d,%d) } }, /* %d */\n",
384 sets
[j
]->num
, i
, off
[j
], i
, off
[j
], i
);
392 for (l
= 0; l
< tl
; l
++)
394 printf (" { { /* %d */", l
);
395 for (i
= 0; i
< 256/32; i
++)
399 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
401 printf ("\n } },\n");
409 for (i
= 0; sets
[i
]; i
++)
413 if (duplicate
[i
] >= 0)
415 printf (" /* %s */\n", names
[i
]);
416 for (n
= 0; n
< sets
[i
]->num
; n
++)
420 for (l
= 0; l
< tl
; l
++)
421 if (leaves
[l
] == FcCharSetLeaf(sets
[i
], n
))
424 fatal (names
[i
], 0, "can't find leaf");
425 printf (" LEAF(%3d,%3d),", off
[i
], l
);
436 for (i
= 0; sets
[i
]; i
++)
440 if (duplicate
[i
] >= 0)
442 printf (" /* %s */\n", names
[i
]);
443 for (n
= 0; n
< sets
[i
]->num
; n
++)
447 printf (" 0x%04x,", FcCharSetNumbers (sets
[i
])[n
]);
458 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
459 num_lang_set_map
= (i
+ 31) / 32;
460 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
462 * Dump indices with country codes
467 int ncountry_ent
= 0;
469 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
470 for (c
= 0; c
< ncountry
; c
++)
475 int lang
= strchr (langs
[i
], '-') - langs
[i
];
478 for (k
= 0; k
< num_lang_set_map
; k
++)
482 for (d
= c
+ 1; d
< ncountry
; d
++)
485 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
492 for (k
= 0; k
< num_lang_set_map
; k
++)
493 printf (" 0x%08x,", map
[k
]);
494 printf (" }, /* %*.*s */\n",
495 lang
, lang
, langs
[i
]);
500 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
505 * Find ranges for each letter for faster searching
508 memset(setRangeStart
, '\0', sizeof (setRangeStart
));
509 memset(setRangeEnd
, '\0', sizeof (setRangeEnd
));
510 for (i
= 0; sets
[i
]; i
++)
512 char c
= names
[i
][0];
514 while (setRangeChar
<= c
&& c
<= 'z')
515 setRangeStart
[setRangeChar
++ - 'a'] = i
;
517 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
518 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
519 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
522 * Dump sets start/finish for the fastpath
524 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
525 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
527 printf (" { %d, %d }, /* %c */\n",
528 setRangeStart
[setRangeChar
- 'a'],
529 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
533 while (fgets (line
, sizeof (line
), stdin
))
534 fputs (line
, stdout
);
537 exit (ferror (stdout
));