]>
git.wh0rd.org - fontconfig.git/blob - fc-lang/fc-lang.c
960c56c3b108a721c87f8abf554be40da4769dfa
2 * $XFree86: xc/lib/fontconfig/fc-lang/fc-lang.c,v 1.3 2002/08/22 07:36:43 keithp Exp $
4 * Copyright © 2002 Keith Packard, member of The XFree86 Project, Inc.
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
)
51 fatal (char *file
, int lineno
, char *msg
)
53 fprintf (stderr
, "%s:%d: %s\n", file
, lineno
, msg
);
58 get_line (FILE *f
, char *line
, int *lineno
)
61 if (!fgets (line
, 1024, f
))
64 hash
= strchr (line
, '#');
67 if (line
[0] == '\0' || line
[0] == '\n' || line
[0] == '\032' || line
[0] == '\r')
68 return get_line (f
, line
, lineno
);
73 * build a single charset from a source file
75 * The file format is quite simple, either
76 * a single hex value or a pair separated with a dash
78 * Comments begin with '#'
82 scan (FILE *f
, char *file
)
90 while (get_line (f
, line
, &lineno
))
92 if (!strncmp (line
, "include", 7))
94 file
= strchr (line
, ' ');
98 if (file
[end
-1] == '\n')
100 f
= fopen (file
, "r");
102 fatal (file
, 0, "can't open");
107 if (strchr (line
, '-'))
109 if (sscanf (line
, "%x-%x", &start
, &end
) != 2)
110 fatal (file
, lineno
, "parse error");
114 if (sscanf (line
, "%x", &start
) != 1)
115 fatal (file
, lineno
, "parse error");
119 c
= FcCharSetCreate ();
120 for (ucs4
= start
; ucs4
<= end
; ucs4
++)
122 if (!FcCharSetAddChar (c
, ucs4
))
123 fatal (file
, lineno
, "out of memory");
126 n
= FcCharSetFreeze (c
);
127 FcCharSetDestroy (c
);
132 * Convert a file name into a name suitable for C declarations
135 get_name (char *file
)
140 dot
= strchr (file
, '.');
142 dot
= file
+ strlen(file
);
143 name
= malloc (dot
- file
+ 1);
144 strncpy (name
, file
, dot
- file
);
145 name
[dot
-file
] = '\0';
150 * Convert a C name into a language name
153 get_lang (char *name
)
155 char *lang
= malloc (strlen (name
) + 1);
159 while ((c
= *name
++))
173 static int compare (const void *a
, const void *b
)
175 const FcChar8
*const *as
= a
, *const *bs
= b
;
176 return FcStrCmpIgnoreCase (*as
, *bs
);
179 #define MAX_LANG 1024
180 #define MAX_LANG_SET_MAP ((MAX_LANG + 31) / 32)
182 #define BitSet(map, id) ((map)[(id)>>5] |= ((FcChar32) 1 << ((id) & 0x1f)))
183 #define BitGet(map, id) ((map)[(id)>>5] >> ((id) & 0x1f)) & 1)
186 main (int argc
, char **argv
)
188 char *files
[MAX_LANG
];
189 FcCharSet
*sets
[MAX_LANG
];
190 int duplicate
[MAX_LANG
];
191 int country
[MAX_LANG
];
192 char *names
[MAX_LANG
];
193 char *langs
[MAX_LANG
];
197 FcCharLeaf
**leaves
, **sleaves
;
198 int total_leaves
= 0;
202 FcChar32 map
[MAX_LANG_SET_MAP
];
203 int num_lang_set_map
;
208 fatal (*argv
, 0, "Too many languages");
212 qsort (files
, i
, sizeof (char *), compare
);
216 f
= fopen (files
[i
], "r");
218 fatal (files
[i
], 0, strerror (errno
));
219 sets
[i
] = scan (f
, files
[i
]);
220 names
[i
] = get_name (files
[i
]);
221 langs
[i
] = get_lang(names
[i
]);
222 if (strchr (langs
[i
], '-'))
223 country
[ncountry
++] = i
;
225 total_leaves
+= sets
[i
]->num
;
230 leaves
= malloc (total_leaves
* sizeof (FcCharLeaf
*));
235 for (i
= 0; sets
[i
]; i
++)
237 sleaves
= sets
[i
]->leaves
;
238 for (sl
= 0; sl
< sets
[i
]->num
; sl
++)
240 for (l
= 0; l
< tl
; l
++)
241 if (leaves
[l
] == sleaves
[sl
])
244 leaves
[tl
++] = sleaves
[sl
];
249 * Scan the input until the marker is found
252 while (fgets (line
, sizeof (line
), stdin
))
254 if (!strncmp (line
, "@@@", 3))
256 fputs (line
, stdout
);
259 printf ("/* total size: %d unique leaves: %d */\n\n",
264 printf ("static const FcCharLeaf leaves[%d] = {\n", tl
);
265 for (l
= 0; l
< tl
; l
++)
267 printf (" { { /* %d */", l
);
268 for (i
= 0; i
< 256/32; i
++)
272 printf (" 0x%08x,", leaves
[l
]->map
[i
]);
274 printf ("\n } },\n");
277 printf ("#define L(n) ((FcCharLeaf *) &leaves[n])\n\n");
280 * Find duplicate charsets
283 for (i
= 1; sets
[i
]; i
++)
288 for (j
= 0; j
< i
; j
++)
289 if (sets
[j
] == sets
[i
])
299 for (i
= 0; sets
[i
]; i
++)
303 if (duplicate
[i
] >= 0)
305 printf ("static const FcCharLeaf *leaves_%s[%d] = {\n",
306 names
[i
], sets
[i
]->num
);
307 for (n
= 0; n
< sets
[i
]->num
; n
++)
311 for (l
= 0; l
< tl
; l
++)
312 if (leaves
[l
] == sets
[i
]->leaves
[n
])
315 fatal (names
[i
], 0, "can't find leaf");
316 printf (" L(%3d),", l
);
325 printf ("static const FcChar16 numbers_%s[%d] = {\n",
326 names
[i
], sets
[i
]->num
);
327 for (n
= 0; n
< sets
[i
]->num
; n
++)
331 printf (" 0x%04x,", sets
[i
]->numbers
[n
]);
339 printf ("#undef L\n\n");
343 printf ("static const FcLangCharSet fcLangCharSets[] = {\n");
344 for (i
= 0; sets
[i
]; i
++)
346 int j
= duplicate
[i
];
349 printf (" { (FcChar8 *) \"%s\",\n"
350 " { FC_REF_CONSTANT, %d, "
351 "(FcCharLeaf **) leaves_%s, "
352 "(FcChar16 *) numbers_%s } },\n",
354 sets
[j
]->num
, names
[j
], names
[j
]);
357 printf ("#define NUM_LANG_CHAR_SET %d\n", i
);
358 num_lang_set_map
= (i
+ 31) / 32;
359 printf ("#define NUM_LANG_SET_MAP %d\n", num_lang_set_map
);
361 * Dump indices with country codes
365 int ncountry_ent
= 0;
367 printf ("static const FcChar32 fcLangCountrySets[][NUM_LANG_SET_MAP] = {\n");
368 for (c
= 0; c
< ncountry
; c
++)
373 int l
= strchr (langs
[i
], '-') - langs
[i
];
376 for (k
= 0; k
< num_lang_set_map
; k
++)
380 for (d
= c
+ 1; d
< ncountry
; d
++)
383 if (j
>= 0 && !strncmp (langs
[j
], langs
[i
], l
))
390 for (k
= 0; k
< num_lang_set_map
; k
++)
391 printf (" 0x%08x,", map
[k
]);
392 printf (" }, /* %*.*s */\n",
398 printf ("#define NUM_COUNTRY_SET %d\n", ncountry_ent
);
401 while (fgets (line
, sizeof (line
), stdin
))
402 fputs (line
, stdout
);
405 exit (ferror (stdout
));