]>
git.wh0rd.org - fontconfig.git/blob - fc-lang/fc-lang.c
989a823b41d9a3f8e584c4d9b92e8a271c2eecb8
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.
25 #include "fccharset.c"
27 #include "fcserialize.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 FcCacheObjectReference (void *object
)
56 FcCacheObjectDereference (void *object
)
65 return (FcChar8
*) getenv ("HOME");
69 fatal (const char *file
, int lineno
, const char *msg
)
72 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
74 fprintf (stderr
, "%s: %s\n", file
, msg
);
79 get_line (FILE *f
, char *line
, int *lineno
)
83 if (!fgets (line
, 1024, f
))
86 hash
= strchr (line
, '#');
91 while (end
> 0 && isspace (line
[end
-1]))
94 if (line
[0] == '\0' || line
[0] == '\n' || line
[0] == '\032' || line
[0] == '\r')
95 return get_line (f
, line
, lineno
);
102 scanopen (char *file
)
106 f
= fopen (file
, "r");
114 f
= fopen (path
, "r");
120 * build a single charset from a source file
122 * The file format is quite simple, either
123 * a single hex value or a pair separated with a dash
125 * Comments begin with '#'
128 static const FcCharSet
*
129 scan (FILE *f
, char *file
, FcCharSetFreezer
*freezer
)
133 int start
, end
, ucs4
;
137 while (get_line (f
, line
, &lineno
))
139 if (!strncmp (line
, "include", 7))
141 file
= strchr (line
, ' ');
144 "invalid syntax, expected: include filename");
145 while (isspace(*file
))
149 fatal (file
, 0, "can't open");
150 n
= scan (f
, file
, freezer
);
154 if (strchr (line
, '-'))
156 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
157 fatal (file
, lineno
, "parse error");
161 if (sscanf (line
, "%x", &start
) != 1)
162 fatal (file
, lineno
, "parse error");
166 c
= FcCharSetCreate ();
167 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
169 if (!FcCharSetAddChar (c
, ucs4
))
170 fatal (file
, lineno
, "out of memory");
173 n
= FcCharSetFreeze (freezer
, c
);
174 FcCharSetDestroy (c
);
179 * Convert a file name into a name suitable for C declarations
182 get_name (char *file
)
187 dot
= strchr (file
, '.');
189 dot
= file
+ strlen(file
);
190 name
= malloc (dot
- file
+ 1);
191 strncpy (name
, file
, dot
- file
);
192 name
[dot
-file
] = '\0';
197 * Convert a C name into a language name
200 get_lang (char *name
)
202 char *lang
= malloc (strlen (name
) + 1);
206 while ((c
= *name
++))
208 if (isupper ((int) (unsigned char) c
))
209 c
= tolower ((int) (unsigned char) c
);
220 static int compare (const void *a
, const void *b
)
222 const FcChar8
*const *as
= a
, *const *bs
= b
;
223 return FcStrCmpIgnoreCase (*as
, *bs
);
226 #define MAX_LANG 1024
227 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
229 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
230 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
233 main (int argc
, char **argv
)
235 static char *files
[MAX_LANG
];
236 static const FcCharSet
*sets
[MAX_LANG
];
237 static int duplicate
[MAX_LANG
];
238 static int country
[MAX_LANG
];
239 static char *names
[MAX_LANG
];
240 static char *langs
[MAX_LANG
];
241 static int off
[MAX_LANG
];
248 int total_leaves
= 0;
250 static char line
[1024];
251 static FcChar32 map
[MAX_LANG_SET_MAP
];
252 int num_lang_set_map
;
253 int setRangeStart
[26];
255 FcChar8 setRangeChar
;
256 FcCharSetFreezer
*freezer
;
258 freezer
= FcCharSetFreezerCreate ();
260 fatal (argv
[0], 0, "out of memory");
264 if (!strcmp (argv
[argi
], "-d"))
271 fatal (argv
[0], 0, "Too many languages");
272 files
[i
++] = argv
[argi
++];
275 qsort (files
, i
, sizeof (char *), compare
);
279 f
= scanopen (files
[i
]);
281 fatal (files
[i
], 0, strerror (errno
));
282 sets
[i
] = scan (f
, files
[i
], freezer
);
283 names
[i
] = get_name (files
[i
]);
284 langs
[i
] = get_lang(names
[i
]);
285 if (strchr (langs
[i
], '-'))
286 country
[ncountry
++] = i
;
288 total_leaves
+= sets
[i
]->num
;
294 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
299 for (i
= 0; sets
[i
]; i
++)
301 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
303 for (l
= 0; l
< tl
; l
++)
304 if (leaves
[l
] == FcCharSetLeaf(sets
[i
], sl
))
307 leaves
[tl
++] = FcCharSetLeaf(sets
[i
], sl
);
312 * Scan the input until the marker is found
315 while (fgets (line
, sizeof (line
), stdin
))
317 if (!strncmp (line
, "@@@", 3))
319 fputs (line
, stdout
);
322 printf ("/* total size: %d unique leaves: %d */\n\n",
326 * Find duplicate charsets
329 for (i
= 1; sets
[i
]; i
++)
334 for (j
= 0; j
< i
; j
++)
335 if (sets
[j
] == sets
[i
])
343 for (i
= 0; sets
[i
]; i
++) {
344 if (duplicate
[i
] >= 0)
350 printf ("#define LEAF0 (%d * sizeof (FcLangCharSet))\n", nsets
);
351 printf ("#define OFF0 (LEAF0 + %d * sizeof (FcCharLeaf))\n", tl
);
352 printf ("#define NUM0 (OFF0 + %d * sizeof (intptr_t))\n", tn
);
353 printf ("#define SET(n) (n * sizeof (FcLangCharSet) + offsetof (FcLangCharSet, charset))\n");
354 printf ("#define OFF(s,o) (OFF0 + o * sizeof (intptr_t) - SET(s))\n");
355 printf ("#define NUM(s,n) (NUM0 + n * sizeof (FcChar16) - SET(s))\n");
356 printf ("#define LEAF(o,l) (LEAF0 + l * sizeof (FcCharLeaf) - (OFF0 + o * sizeof (intptr_t)))\n");
357 printf ("#define fcLangCharSets (fcLangData.langCharSets)\n");
360 printf ("static const struct {\n"
361 " FcLangCharSet langCharSets[%d];\n"
362 " FcCharLeaf leaves[%d];\n"
363 " intptr_t leaf_offsets[%d];\n"
364 " FcChar16 numbers[%d];\n"
365 "} fcLangData = {\n",
373 for (i
= 0; sets
[i
]; i
++)
375 int j
= duplicate
[i
];
380 printf (" { \"%s\", "
381 " { FC_REF_CONSTANT, %d, OFF(%d,%d), NUM(%d,%d) } }, /* %d */\n",
383 sets
[j
]->num
, i
, off
[j
], i
, off
[j
], i
);
391 for (l
= 0; l
< tl
; l
++)
393 printf (" { { /* %d */", l
);
394 for (i
= 0; i
< 256/32; i
++)
398 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
400 printf ("\n } },\n");
408 for (i
= 0; sets
[i
]; i
++)
412 if (duplicate
[i
] >= 0)
414 printf (" /* %s */\n", names
[i
]);
415 for (n
= 0; n
< sets
[i
]->num
; n
++)
419 for (l
= 0; l
< tl
; l
++)
420 if (leaves
[l
] == FcCharSetLeaf(sets
[i
], n
))
423 fatal (names
[i
], 0, "can't find leaf");
424 printf (" LEAF(%3d,%3d),", off
[i
], l
);
435 for (i
= 0; sets
[i
]; i
++)
439 if (duplicate
[i
] >= 0)
441 printf (" /* %s */\n", names
[i
]);
442 for (n
= 0; n
< sets
[i
]->num
; n
++)
446 printf (" 0x%04x,", FcCharSetNumbers (sets
[i
])[n
]);
457 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
458 num_lang_set_map
= (i
+ 31) / 32;
459 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
461 * Dump indices with country codes
466 int ncountry_ent
= 0;
468 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
469 for (c
= 0; c
< ncountry
; c
++)
474 int lang
= strchr (langs
[i
], '-') - langs
[i
];
477 for (k
= 0; k
< num_lang_set_map
; k
++)
481 for (d
= c
+ 1; d
< ncountry
; d
++)
484 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
491 for (k
= 0; k
< num_lang_set_map
; k
++)
492 printf (" 0x%08x,", map
[k
]);
493 printf (" }, /* %*.*s */\n",
494 lang
, lang
, langs
[i
]);
499 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
504 * Find ranges for each letter for faster searching
507 memset(setRangeStart
, '\0', sizeof (setRangeStart
));
508 memset(setRangeEnd
, '\0', sizeof (setRangeEnd
));
509 for (i
= 0; sets
[i
]; i
++)
511 char c
= names
[i
][0];
513 while (setRangeChar
<= c
&& c
<= 'z')
514 setRangeStart
[setRangeChar
++ - 'a'] = i
;
516 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
517 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
518 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
521 * Dump sets start/finish for the fastpath
523 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
524 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
526 printf (" { %d, %d }, /* %c */\n",
527 setRangeStart
[setRangeChar
- 'a'],
528 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
532 while (fgets (line
, sizeof (line
), stdin
))
533 fputs (line
, stdout
);
536 exit (ferror (stdout
));