]>
git.wh0rd.org - fontconfig.git/blob - fc-lang/fc-lang.c
631411d1bb2d7f3bb0c41fa4b9289ab86c632d63
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 (FcChar8
*) getenv ("HOME");
57 fatal (const char *file
, int lineno
, const char *msg
)
60 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
62 fprintf (stderr
, "%s: %s\n", file
, msg
);
67 get_line (FILE *f
, char *line
, int *lineno
)
71 if (!fgets (line
, 1024, f
))
74 hash
= strchr (line
, '#');
79 while (end
> 0 && isspace (line
[end
-1]))
82 if (line
[0] == '\0' || line
[0] == '\n' || line
[0] == '\032' || line
[0] == '\r')
83 return get_line (f
, line
, lineno
);
94 f
= fopen (file
, "r");
102 f
= fopen (path
, "r");
108 * build a single charset from a source file
110 * The file format is quite simple, either
111 * a single hex value or a pair separated with a dash
113 * Comments begin with '#'
117 scan (FILE *f
, char *file
)
121 int start
, end
, ucs4
;
125 while (get_line (f
, line
, &lineno
))
127 if (!strncmp (line
, "include", 7))
129 file
= strchr (line
, ' ');
132 "invalid syntax, expected: include filename");
133 while (isspace(*file
))
137 fatal (file
, 0, "can't open");
142 if (strchr (line
, '-'))
144 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
145 fatal (file
, lineno
, "parse error");
149 if (sscanf (line
, "%x", &start
) != 1)
150 fatal (file
, lineno
, "parse error");
154 c
= FcCharSetCreate ();
155 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
157 if (!FcCharSetAddChar (c
, ucs4
))
158 fatal (file
, lineno
, "out of memory");
161 n
= FcCharSetFreeze (c
);
162 FcCharSetDestroy (c
);
167 * Convert a file name into a name suitable for C declarations
170 get_name (char *file
)
175 dot
= strchr (file
, '.');
177 dot
= file
+ strlen(file
);
178 name
= malloc (dot
- file
+ 1);
179 strncpy (name
, file
, dot
- file
);
180 name
[dot
-file
] = '\0';
185 * Convert a C name into a language name
188 get_lang (char *name
)
190 char *lang
= malloc (strlen (name
) + 1);
194 while ((c
= *name
++))
196 if (isupper ((int) (unsigned char) c
))
197 c
= tolower ((int) (unsigned char) c
);
208 static int compare (const void *a
, const void *b
)
210 const FcChar8
*const *as
= a
, *const *bs
= b
;
211 return FcStrCmpIgnoreCase (*as
, *bs
);
214 #define MAX_LANG 1024
215 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
217 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
218 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
221 main (int argc
, char **argv
)
223 static char *files
[MAX_LANG
];
224 static FcCharSet
*sets
[MAX_LANG
];
225 static int duplicate
[MAX_LANG
];
226 static int country
[MAX_LANG
];
227 static char *names
[MAX_LANG
];
228 static char *langs
[MAX_LANG
];
229 static int off
[MAX_LANG
];
236 int total_leaves
= 0;
238 static char line
[1024];
239 static FcChar32 map
[MAX_LANG_SET_MAP
];
240 int num_lang_set_map
;
241 int setRangeStart
[26];
243 FcChar8 setRangeChar
;
248 if (!strcmp (argv
[argi
], "-d"))
255 fatal (argv
[0], 0, "Too many languages");
256 files
[i
++] = argv
[argi
++];
259 qsort (files
, i
, sizeof (char *), compare
);
263 f
= scanopen (files
[i
]);
265 fatal (files
[i
], 0, strerror (errno
));
266 sets
[i
] = scan (f
, files
[i
]);
267 names
[i
] = get_name (files
[i
]);
268 langs
[i
] = get_lang(names
[i
]);
269 if (strchr (langs
[i
], '-'))
270 country
[ncountry
++] = i
;
272 total_leaves
+= sets
[i
]->num
;
278 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
283 for (i
= 0; sets
[i
]; i
++)
285 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
287 for (l
= 0; l
< tl
; l
++)
288 if (leaves
[l
] == FcCharSetLeaf(sets
[i
], sl
))
291 leaves
[tl
++] = FcCharSetLeaf(sets
[i
], sl
);
296 * Scan the input until the marker is found
299 while (fgets (line
, sizeof (line
), stdin
))
301 if (!strncmp (line
, "@@@", 3))
303 fputs (line
, stdout
);
306 printf ("/* total size: %d unique leaves: %d */\n\n",
310 * Find duplicate charsets
313 for (i
= 1; sets
[i
]; i
++)
318 for (j
= 0; j
< i
; j
++)
319 if (sets
[j
] == sets
[i
])
327 for (i
= 0; sets
[i
]; i
++) {
328 if (duplicate
[i
] >= 0)
334 printf ("#define LEAF0 (%d * sizeof (FcLangCharSet))\n", nsets
);
335 printf ("#define OFF0 (LEAF0 + %d * sizeof (FcCharLeaf))\n", tl
);
336 printf ("#define NUM0 (OFF0 + %d * sizeof (intptr_t))\n", tn
);
337 printf ("#define SET(n) (n * sizeof (FcLangCharSet) + offsetof (FcLangCharSet, charset))\n");
338 printf ("#define OFF(s,o) (OFF0 + o * sizeof (intptr_t) - SET(s))\n");
339 printf ("#define NUM(s,n) (NUM0 + n * sizeof (FcChar16) - SET(s))\n");
340 printf ("#define LEAF(o,l) (LEAF0 + l * sizeof (FcCharLeaf) - (OFF0 + o * sizeof (intptr_t)))\n");
341 printf ("#define fcLangCharSets (fcLangData.langCharSets)\n");
344 printf ("static const struct {\n"
345 " FcLangCharSet langCharSets[%d];\n"
346 " FcCharLeaf leaves[%d];\n"
347 " intptr_t leaf_offsets[%d];\n"
348 " FcChar16 numbers[%d];\n"
349 "} fcLangData = {\n",
357 for (i
= 0; sets
[i
]; i
++)
359 int j
= duplicate
[i
];
364 printf (" { (FcChar8 *) \"%s\", "
365 " { FC_REF_CONSTANT, %d, OFF(%d,%d), NUM(%d,%d) } }, /* %d */\n",
367 sets
[j
]->num
, i
, off
[j
], i
, off
[j
], i
);
375 for (l
= 0; l
< tl
; l
++)
377 printf (" { { /* %d */", l
);
378 for (i
= 0; i
< 256/32; i
++)
382 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
384 printf ("\n } },\n");
392 for (i
= 0; sets
[i
]; i
++)
396 if (duplicate
[i
] >= 0)
398 printf (" /* %s */\n", names
[i
]);
399 for (n
= 0; n
< sets
[i
]->num
; n
++)
403 for (l
= 0; l
< tl
; l
++)
404 if (leaves
[l
] == FcCharSetLeaf(sets
[i
], n
))
407 fatal (names
[i
], 0, "can't find leaf");
408 printf (" LEAF(%3d,%3d),", off
[i
], l
);
419 for (i
= 0; sets
[i
]; i
++)
423 if (duplicate
[i
] >= 0)
425 printf (" /* %s */\n", names
[i
]);
426 for (n
= 0; n
< sets
[i
]->num
; n
++)
430 printf (" 0x%04x,", FcCharSetNumbers (sets
[i
])[n
]);
441 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
442 num_lang_set_map
= (i
+ 31) / 32;
443 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
445 * Dump indices with country codes
450 int ncountry_ent
= 0;
452 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
453 for (c
= 0; c
< ncountry
; c
++)
458 int lang
= strchr (langs
[i
], '-') - langs
[i
];
461 for (k
= 0; k
< num_lang_set_map
; k
++)
465 for (d
= c
+ 1; d
< ncountry
; d
++)
468 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
475 for (k
= 0; k
< num_lang_set_map
; k
++)
476 printf (" 0x%08x,", map
[k
]);
477 printf (" }, /* %*.*s */\n",
478 lang
, lang
, langs
[i
]);
483 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
488 * Find ranges for each letter for faster searching
491 memset(setRangeStart
, '\0', sizeof (setRangeStart
));
492 memset(setRangeEnd
, '\0', sizeof (setRangeEnd
));
493 for (i
= 0; sets
[i
]; i
++)
495 char c
= names
[i
][0];
497 while (setRangeChar
<= c
&& c
<= 'z')
498 setRangeStart
[setRangeChar
++ - 'a'] = i
;
500 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
501 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
502 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
505 * Dump sets start/finish for the fastpath
507 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
508 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
510 printf (" { %d, %d }, /* %c */\n",
511 setRangeStart
[setRangeChar
- 'a'],
512 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
516 while (fgets (line
, sizeof (line
), stdin
))
517 fputs (line
, stdout
);
520 exit (ferror (stdout
));