]>
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
);
85 f
= fopen (file
, "r");
93 f
= fopen (path
, "r");
99 * build a single charset from a source file
101 * The file format is quite simple, either
102 * a single hex value or a pair separated with a dash
104 * Comments begin with '#'
108 scan (FILE *f
, char *file
)
112 int start
, end
, ucs4
;
116 while (get_line (f
, line
, &lineno
))
118 if (!strncmp (line
, "include", 7))
120 file
= strchr (line
, ' ');
124 if (file
[end
-1] == '\n')
128 fatal (file
, 0, "can't open");
133 if (strchr (line
, '-'))
135 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
136 fatal (file
, lineno
, "parse error");
140 if (sscanf (line
, "%x", &start
) != 1)
141 fatal (file
, lineno
, "parse error");
145 c
= FcCharSetCreate ();
146 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
148 if (!FcCharSetAddChar (c
, ucs4
))
149 fatal (file
, lineno
, "out of memory");
152 n
= FcCharSetFreeze (c
);
153 FcCharSetDestroy (c
);
158 * Convert a file name into a name suitable for C declarations
161 get_name (char *file
)
166 dot
= strchr (file
, '.');
168 dot
= file
+ strlen(file
);
169 name
= malloc (dot
- file
+ 1);
170 strncpy (name
, file
, dot
- file
);
171 name
[dot
-file
] = '\0';
176 * Convert a C name into a language name
179 get_lang (char *name
)
181 char *lang
= malloc (strlen (name
) + 1);
185 while ((c
= *name
++))
187 if (isupper ((int) (unsigned char) c
))
188 c
= tolower ((int) (unsigned char) c
);
199 static int compare (const void *a
, const void *b
)
201 const FcChar8
*const *as
= a
, *const *bs
= b
;
202 return FcStrCmpIgnoreCase (*as
, *bs
);
205 #define MAX_LANG 1024
206 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
208 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
209 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
212 main (int argc
, char **argv
)
214 char *files
[MAX_LANG
];
215 FcCharSet
*sets
[MAX_LANG
];
216 int duplicate
[MAX_LANG
];
217 int country
[MAX_LANG
];
218 char *names
[MAX_LANG
];
219 char *langs
[MAX_LANG
];
223 FcCharLeaf
**leaves
, **sleaves
;
224 int total_leaves
= 0;
228 FcChar32 map
[MAX_LANG_SET_MAP
];
229 int num_lang_set_map
;
230 int setRangeStart
[26];
232 FcChar8 setRangeChar
;
236 if (!strcmp (*argv
, "-d"))
242 fatal (*argv
, 0, "Too many languages");
246 qsort (files
, i
, sizeof (char *), compare
);
250 f
= scanopen (files
[i
]);
252 fatal (files
[i
], 0, strerror (errno
));
253 sets
[i
] = scan (f
, files
[i
]);
254 names
[i
] = get_name (files
[i
]);
255 langs
[i
] = get_lang(names
[i
]);
256 if (strchr (langs
[i
], '-'))
257 country
[ncountry
++] = i
;
259 total_leaves
+= sets
[i
]->num
;
264 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
269 for (i
= 0; sets
[i
]; i
++)
271 sleaves
= sets
[i
]->leaves
;
272 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
274 for (l
= 0; l
< tl
; l
++)
275 if (leaves
[l
] == sleaves
[sl
])
278 leaves
[tl
++] = sleaves
[sl
];
283 * Scan the input until the marker is found
286 while (fgets (line
, sizeof (line
), stdin
))
288 if (!strncmp (line
, "@@@", 3))
290 fputs (line
, stdout
);
293 printf ("/* total size: %d unique leaves: %d */\n\n",
298 printf ("static const FcCharLeaf leaves[%d] = {\n", tl
);
299 for (l
= 0; l
< tl
; l
++)
301 printf (" { { /* %d */", l
);
302 for (i
= 0; i
< 256/32; i
++)
306 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
308 printf ("\n } },\n");
311 printf ("#define L(n) ((FcCharLeaf *) &leaves[n])\n\n");
314 * Find duplicate charsets
317 for (i
= 1; sets
[i
]; i
++)
322 for (j
= 0; j
< i
; j
++)
323 if (sets
[j
] == sets
[i
])
331 * Find ranges for each letter for faster searching
334 for (i
= 0; sets
[i
]; i
++)
336 char c
= names
[i
][0];
338 while (setRangeChar
<= c
&& c
<= 'z')
339 setRangeStart
[setRangeChar
++ - 'a'] = i
;
341 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
342 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
343 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
348 for (i
= 0; sets
[i
]; i
++)
352 if (duplicate
[i
] >= 0)
354 printf ("static const FcCharLeaf *leaves_%s[%d] = {\n",
355 names
[i
], sets
[i
]->num
);
356 for (n
= 0; n
< sets
[i
]->num
; n
++)
360 for (l
= 0; l
< tl
; l
++)
361 if (leaves
[l
] == sets
[i
]->leaves
[n
])
364 fatal (names
[i
], 0, "can't find leaf");
365 printf (" L(%3d),", l
);
374 printf ("static const FcChar16 numbers_%s[%d] = {\n",
375 names
[i
], sets
[i
]->num
);
376 for (n
= 0; n
< sets
[i
]->num
; n
++)
380 printf (" 0x%04x,", sets
[i
]->numbers
[n
]);
388 printf ("#undef L\n\n");
394 printf ("static const FcLangCharSet fcLangCharSets[] = {\n");
395 for (i
= 0; sets
[i
]; i
++)
397 int j
= duplicate
[i
];
401 printf (" { (FcChar8 *) \"%s\",\n"
402 " { FC_REF_CONSTANT, %d, "
403 "(FcCharLeaf **) leaves_%s, "
404 "(FcChar16 *) numbers_%s } },\n",
406 sets
[j
]->num
, names
[j
], names
[j
]);
409 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
410 num_lang_set_map
= (i
+ 31) / 32;
411 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
413 * Dump indices with country codes
417 int ncountry_ent
= 0;
419 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
420 for (c
= 0; c
< ncountry
; c
++)
425 int l
= strchr (langs
[i
], '-') - langs
[i
];
428 for (k
= 0; k
< num_lang_set_map
; k
++)
432 for (d
= c
+ 1; d
< ncountry
; d
++)
435 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
442 for (k
= 0; k
< num_lang_set_map
; k
++)
443 printf (" 0x%08x,", map
[k
]);
444 printf (" }, /* %*.*s */\n",
450 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
455 * Dump sets start/finish for the fastpath
457 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
458 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
460 printf (" { %d, %d }, /* %c */\n",
461 setRangeStart
[setRangeChar
- 'a'],
462 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
466 while (fgets (line
, sizeof (line
), stdin
))
467 fputs (line
, stdout
);
470 exit (ferror (stdout
));