]>
git.wh0rd.org - fontconfig.git/blob - fc-lang/fc-lang.c
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 typedef struct _Entry
{
232 static int compare (const void *a
, const void *b
)
234 const Entry
const *as
= a
, *bs
= b
;
235 return FcStrCmpIgnoreCase (as
->file
, bs
->file
);
238 #define MAX_LANG 1024
239 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
241 #define BitSet(map, i) ((map)[(entries[i].id)>>5] |= ((FcChar32) 1 << ((entries[i].id) & 0x1f)))
242 #define BitGet(map, i) ((map)[(entries[i].id)>>5] >> ((entries[i].id) & 0x1f)) & 1)
245 main (int argc
, char **argv
)
247 static Entry entries
[MAX_LANG
];
248 static const FcCharSet
*sets
[MAX_LANG
];
249 static int duplicate
[MAX_LANG
];
250 static int country
[MAX_LANG
];
251 static char *names
[MAX_LANG
];
252 static char *langs
[MAX_LANG
];
253 static int off
[MAX_LANG
];
260 int total_leaves
= 0;
262 static char line
[1024];
263 static FcChar32 map
[MAX_LANG_SET_MAP
];
264 int num_lang_set_map
;
265 int setRangeStart
[26];
267 FcChar8 setRangeChar
;
268 FcCharSetFreezer
*freezer
;
270 freezer
= FcCharSetFreezerCreate ();
272 fatal (argv
[0], 0, "out of memory");
276 if (!strcmp (argv
[argi
], "-d"))
283 fatal (argv
[0], 0, "Too many languages");
285 entries
[i
].file
= argv
[argi
++];
289 qsort (entries
, i
, sizeof (Entry
), compare
);
291 while (entries
[i
].file
)
293 f
= scanopen (entries
[i
].file
);
295 fatal (entries
[i
].file
, 0, strerror (errno
));
296 sets
[i
] = scan (f
, entries
[i
].file
, freezer
);
297 names
[i
] = get_name (entries
[i
].file
);
298 langs
[i
] = get_lang(names
[i
]);
299 if (strchr (langs
[i
], '-'))
300 country
[ncountry
++] = i
;
302 total_leaves
+= sets
[i
]->num
;
308 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
313 for (i
= 0; sets
[i
]; i
++)
315 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
317 for (l
= 0; l
< tl
; l
++)
318 if (leaves
[l
] == FcCharSetLeaf(sets
[i
], sl
))
321 leaves
[tl
++] = FcCharSetLeaf(sets
[i
], sl
);
326 * Scan the input until the marker is found
329 while (fgets (line
, sizeof (line
), stdin
))
331 if (!strncmp (line
, "@@@", 3))
333 fputs (line
, stdout
);
336 printf ("/* total size: %d unique leaves: %d */\n\n",
340 * Find duplicate charsets
343 for (i
= 1; sets
[i
]; i
++)
348 for (j
= 0; j
< i
; j
++)
349 if (sets
[j
] == sets
[i
])
357 for (i
= 0; sets
[i
]; i
++) {
358 if (duplicate
[i
] >= 0)
364 printf ("#define LEAF0 (%d * sizeof (FcLangCharSet))\n", nsets
);
365 printf ("#define OFF0 (LEAF0 + %d * sizeof (FcCharLeaf))\n", tl
);
366 printf ("#define NUM0 (OFF0 + %d * sizeof (intptr_t))\n", tn
);
367 printf ("#define SET(n) (n * sizeof (FcLangCharSet) + offsetof (FcLangCharSet, charset))\n");
368 printf ("#define OFF(s,o) (OFF0 + o * sizeof (intptr_t) - SET(s))\n");
369 printf ("#define NUM(s,n) (NUM0 + n * sizeof (FcChar16) - SET(s))\n");
370 printf ("#define LEAF(o,l) (LEAF0 + l * sizeof (FcCharLeaf) - (OFF0 + o * sizeof (intptr_t)))\n");
371 printf ("#define fcLangCharSets (fcLangData.langCharSets)\n");
372 printf ("#define fcLangCharSetIndices (fcLangData.langIndices)\n");
373 printf ("#define fcLangCharSetIndicesInv (fcLangData.langIndicesInv)\n");
376 printf ("static const struct {\n"
377 " FcLangCharSet langCharSets[%d];\n"
378 " FcCharLeaf leaves[%d];\n"
379 " intptr_t leaf_offsets[%d];\n"
380 " FcChar16 numbers[%d];\n"
381 " FcChar%s langIndices[%d];\n"
382 " FcChar%s langIndicesInv[%d];\n"
383 "} fcLangData = {\n",
385 nsets
< 256 ? "8 " : "16", nsets
, nsets
< 256 ? "8 " : "16", nsets
);
392 for (i
= 0; sets
[i
]; i
++)
394 int j
= duplicate
[i
];
399 printf (" { \"%s\", "
400 " { FC_REF_CONSTANT, %d, OFF(%d,%d), NUM(%d,%d) } }, /* %d */\n",
402 sets
[j
]->num
, i
, off
[j
], i
, off
[j
], i
);
410 for (l
= 0; l
< tl
; l
++)
412 printf (" { { /* %d */", l
);
413 for (i
= 0; i
< 256/32; i
++)
417 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
419 printf ("\n } },\n");
427 for (i
= 0; sets
[i
]; i
++)
431 if (duplicate
[i
] >= 0)
433 printf (" /* %s */\n", names
[i
]);
434 for (n
= 0; n
< sets
[i
]->num
; n
++)
438 for (l
= 0; l
< tl
; l
++)
439 if (leaves
[l
] == FcCharSetLeaf(sets
[i
], n
))
442 fatal (names
[i
], 0, "can't find leaf");
443 printf (" LEAF(%3d,%3d),", off
[i
], l
);
454 for (i
= 0; sets
[i
]; i
++)
458 if (duplicate
[i
] >= 0)
460 printf (" /* %s */\n", names
[i
]);
461 for (n
= 0; n
< sets
[i
]->num
; n
++)
465 printf (" 0x%04x,", FcCharSetNumbers (sets
[i
])[n
]);
476 for (i
= 0; sets
[i
]; i
++)
478 printf (" %d, /* %s */\n", entries
[i
].id
, names
[i
]);
485 static int entries_inv
[MAX_LANG
];
486 for (i
= 0; sets
[i
]; i
++)
487 entries_inv
[entries
[i
].id
] = i
;
488 for (i
= 0; sets
[i
]; i
++)
489 printf (" %d, /* %s */\n", entries_inv
[i
], names
[entries_inv
[i
]]);
495 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
496 num_lang_set_map
= (i
+ 31) / 32;
497 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
499 * Dump indices with country codes
504 int ncountry_ent
= 0;
506 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
507 for (c
= 0; c
< ncountry
; c
++)
512 int lang
= strchr (langs
[i
], '-') - langs
[i
];
515 for (k
= 0; k
< num_lang_set_map
; k
++)
519 for (d
= c
+ 1; d
< ncountry
; d
++)
522 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], lang
+ 1))
529 for (k
= 0; k
< num_lang_set_map
; k
++)
530 printf (" 0x%08x,", map
[k
]);
531 printf (" }, /* %*.*s */\n",
532 lang
, lang
, langs
[i
]);
537 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
542 * Find ranges for each letter for faster searching
545 memset(setRangeStart
, '\0', sizeof (setRangeStart
));
546 memset(setRangeEnd
, '\0', sizeof (setRangeEnd
));
547 for (i
= 0; sets
[i
]; i
++)
549 char c
= names
[i
][0];
551 while (setRangeChar
<= c
&& c
<= 'z')
552 setRangeStart
[setRangeChar
++ - 'a'] = i
;
554 for (setRangeChar
= 'a'; setRangeChar
< 'z'; setRangeChar
++)
555 setRangeEnd
[setRangeChar
- 'a'] = setRangeStart
[setRangeChar
+1-'a'] - 1;
556 setRangeEnd
[setRangeChar
- 'a'] = i
- 1;
559 * Dump sets start/finish for the fastpath
562 printf ("static const FcLangCharSetRange fcLangCharSetRanges[] = {\n");
564 for (setRangeChar
= 'a'; setRangeChar
<= 'z' ; setRangeChar
++)
566 printf (" { %d, %d }, /* %c */\n",
567 setRangeStart
[setRangeChar
- 'a'],
568 setRangeEnd
[setRangeChar
- 'a'], setRangeChar
);
572 while (fgets (line
, sizeof (line
), stdin
))
573 fputs (line
, stdout
);
576 exit (ferror (stdout
));