]>
git.wh0rd.org - fontconfig.git/blob - fc-lang.c
22afe904062cada1e5a4cee2790866188f2042ec
2 * fontconfig/fc-lang/fc-lang.c
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 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18 * EVENT SHALL THE AUTHOR(S) 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))
143 included_file
= strchr (line
, ' ');
146 "invalid syntax, expected: include filename");
147 while (isspace(*included_file
))
149 included_f
= scanopen (included_file
);
151 fatal (included_file
, 0, "can't open");
152 n
= scan (included_f
, included_file
, freezer
);
155 c
= FcCharSetCreate ();
156 if (!FcCharSetMerge (c
, n
, NULL
))
157 fatal (file
, lineno
, "out of memory");
158 FcCharSetDestroy (n
);
161 if (strchr (line
, '-'))
163 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
164 fatal (file
, lineno
, "parse error");
168 if (sscanf (line
, "%x", &start
) != 1)
169 fatal (file
, lineno
, "parse error");
173 c
= FcCharSetCreate ();
174 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
176 if (!FcCharSetAddChar (c
, ucs4
))
177 fatal (file
, lineno
, "out of memory");
180 n
= FcCharSetFreeze (freezer
, c
);
181 FcCharSetDestroy (c
);
186 * Convert a file name into a name suitable for C declarations
189 get_name (char *file
)
194 dot
= strchr (file
, '.');
196 dot
= file
+ strlen(file
);
197 name
= malloc (dot
- file
+ 1);
198 strncpy (name
, file
, dot
- file
);
199 name
[dot
-file
] = '\0';
204 * Convert a C name into a language name
207 get_lang (char *name
)
209 char *lang
= malloc (strlen (name
) + 1);
213 while ((c
= *name
++))
215 if (isupper ((int) (unsigned char) c
))
216 c
= tolower ((int) (unsigned char) c
);
227 static int compare (const void *a
, const void *b
)
229 const FcChar8
*const *as
= a
, *const *bs
= b
;
230 return FcStrCmpIgnoreCase (*as
, *bs
);
233 #define MAX_LANG 1024
234 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
236 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
237 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
240 main (int argc
, char **argv
)
242 static char *files
[MAX_LANG
];
243 static const FcCharSet
*sets
[MAX_LANG
];
244 static int duplicate
[MAX_LANG
];
245 static int country
[MAX_LANG
];
246 static char *names
[MAX_LANG
];
247 static char *langs
[MAX_LANG
];
248 static int off
[MAX_LANG
];
255 int total_leaves
= 0;
257 static char line
[1024];
258 static FcChar32 map
[MAX_LANG_SET_MAP
];
259 int num_lang_set_map
;
260 int setRangeStart
[26];
262 FcChar8 setRangeChar
;
263 FcCharSetFreezer
*freezer
;
265 freezer
= FcCharSetFreezerCreate ();
267 fatal (argv
[0], 0, "out of memory");
271 if (!strcmp (argv
[argi
], "-d"))
278 fatal (argv
[0], 0, "Too many languages");
279 files
[i
++] = argv
[argi
++];
282 qsort (files
, i
, sizeof (char *), compare
);
286 f
= scanopen (files
[i
]);
288 fatal (files
[i
], 0, strerror (errno
));
289 sets
[i
] = scan (f
, files
[i
], freezer
);
290 names
[i
] = get_name (files
[i
]);
291 langs
[i
] = get_lang(names
[i
]);
292 if (strchr (langs
[i
], '-'))
293 country
[ncountry
++] = i
;
295 total_leaves
+= sets
[i
]->num
;
301 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
306 for (i
= 0; sets
[i
]; i
++)
308 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
310 for (l
= 0; l
< tl
; l
++)
311 if (leaves
[l
] == FcCharSetLeaf(sets
[i
], sl
))
314 leaves
[tl
++] = FcCharSetLeaf(sets
[i
], sl
);
319 * Scan the input until the marker is found
322 while (fgets (line
, sizeof (line
), stdin
))
324 if (!strncmp (line
, "@@@", 3))
326 fputs (line
, stdout
);
329 printf ("/* total size: %d unique leaves: %d */\n\n",
333 * Find duplicate charsets
336 for (i
= 1; sets
[i
]; i
++)
341 for (j
= 0; j
< i
; j
++)
342 if (sets
[j
] == sets
[i
])
350 for (i
= 0; sets
[i
]; i
++) {
351 if (duplicate
[i
] >= 0)
357 printf ("#define LEAF0 (%d * sizeof (FcLangCharSet))\n", nsets
);
358 printf ("#define OFF0 (LEAF0 + %d * sizeof (FcCharLeaf))\n", tl
);
359 printf ("#define NUM0 (OFF0 + %d * sizeof (intptr_t))\n", tn
);
360 printf ("#define SET(n) (n * sizeof (FcLangCharSet) + offsetof (FcLangCharSet, charset))\n");
361 printf ("#define OFF(s,o) (OFF0 + o * sizeof (intptr_t) - SET(s))\n");
362 printf ("#define NUM(s,n) (NUM0 + n * sizeof (FcChar16) - SET(s))\n");
363 printf ("#define LEAF(o,l) (LEAF0 + l * sizeof (FcCharLeaf) - (OFF0 + o * sizeof (intptr_t)))\n");
364 printf ("#define fcLangCharSets (fcLangData.langCharSets)\n");
367 printf ("static const struct {\n"
368 " FcLangCharSet langCharSets[%d];\n"
369 " FcCharLeaf leaves[%d];\n"
370 " intptr_t leaf_offsets[%d];\n"
371 " FcChar16 numbers[%d];\n"
372 "} fcLangData = {\n",
380 for (i
= 0; sets
[i
]; i
++)
382 int j
= duplicate
[i
];
387 printf (" { \"%s\", "
388 " { FC_REF_CONSTANT, %d, OFF(%d,%d), NUM(%d,%d) } }, /* %d */\n",
390 sets
[j
]->num
, i
, off
[j
], i
, off
[j
], i
);
398 for (l
= 0; l
< tl
; l
++)
400 printf (" { { /* %d */", l
);
401 for (i
= 0; i
< 256/32; i
++)
405 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
407 printf ("\n } },\n");
415 for (i
= 0; sets
[i
]; i
++)
419 if (duplicate
[i
] >= 0)
421 printf (" /* %s */\n", names
[i
]);
422 for (n
= 0; n
< sets
[i
]->num
; n
++)
426 for (l
= 0; l
< tl
; l
++)
427 if (leaves
[l
] == FcCharSetLeaf(sets
[i
], n
))
430 fatal (names
[i
], 0, "can't find leaf");
431 printf (" LEAF(%3d,%3d),", off
[i
], l
);
442 for (i
= 0; sets
[i
]; i
++)
446 if (duplicate
[i
] >= 0)
448 printf (" /* %s */\n", names
[i
]);
449 for (n
= 0; n
< sets
[i
]->num
; n
++)
453 printf (" 0x%04x,", FcCharSetNumbers (sets
[i
])[n
]);
464 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
465 num_lang_set_map
= (i
+ 31) / 32;
466 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
468 * Dump indices with country codes
473 int ncountry_ent
= 0;
475 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
476 for (c
= 0; c
< ncountry
; c
++)
481 int lang
= strchr (langs
[i
], '-') - langs
[i
];
484 for (k
= 0; k
< num_lang_set_map
; k
++)
488 for (d
= c
+ 1; d
< ncountry
; d
++)
491 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
498 for (k
= 0; k
< num_lang_set_map
; k
++)
499 printf (" 0x%08x,", map
[k
]);
500 printf (" }, /* %*.*s */\n",
501 lang
, lang
, langs
[i
]);
506 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
511 * Find ranges for each letter for faster searching
514 memset(setRangeStart
, '\0', sizeof (setRangeStart
));
515 memset(setRangeEnd
, '\0', sizeof (setRangeEnd
));
516 for (i
= 0; sets
[i
]; i
++)
518 char c
= names
[i
][0];
520 while (setRangeChar
<= c
&& c
<= 'z')
521 setRangeStart
[setRangeChar
++ - 'a'] = i
;
523 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
524 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
525 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
528 * Dump sets start/finish for the fastpath
530 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
531 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
533 printf (" { %d, %d }, /* %c */\n",
534 setRangeStart
[setRangeChar
- 'a'],
535 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
539 while (fgets (line
, sizeof (line
), stdin
))
540 fputs (line
, stdout
);
543 exit (ferror (stdout
));