summaryrefslogtreecommitdiff
path: root/fc-case/fc-case.c
blob: bd28517f49233610f6e73e879300d5bcd603fb45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
 * $Id$
 *
 * Copyright © 2004 Keith Packard
 *
 * Permission to use, copy, modify, distribute, and sell this software and its
 * documentation for any purpose is hereby granted without fee, provided that
 * the above copyright notice appear in all copies and that both that
 * copyright notice and this permission notice appear in supporting
 * documentation, and that the name of Keith Packard not be used in
 * advertising or publicity pertaining to distribution of the software without
 * specific, written prior permission.  Keith Packard makes no
 * representations about the suitability of this software for any purpose.  It
 * is provided "as is" without express or implied warranty.
 *
 * KEITH PACKARD DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
 * EVENT SHALL KEITH PACKARD BE LIABLE FOR ANY SPECIAL, INDIRECT OR
 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
 * PERFORMANCE OF THIS SOFTWARE.
 */

#include "fcint.h"
#include <ctype.h>

#define MAX_OUT	    32
#define MAX_LINE    8192

/* stub definitions for declarations from fcint.h.. */
int * _fcBankId = 0, * _fcBankIdx = 0;
FcValueList ** _fcValueLists = 0;
FcPatternElt ** _fcPatternElts = 0;
int FcDebugVal = 0;

int
FcCacheBankToIndexMTF (int bank)
{
    return 0;
}
/* end stub definitions */

typedef enum _caseFoldClass { CaseFoldCommon, CaseFoldFull, CaseFoldSimple, CaseFoldTurkic } CaseFoldClass;

typedef struct _caseFoldClassMap {
    const char	    *name;
    CaseFoldClass   class;
} CaseFoldClassMap;

static const CaseFoldClassMap	caseFoldClassMap[] = {
    { "C", CaseFoldCommon },
    { "F", CaseFoldFull },
    { "S", CaseFoldSimple },
    { "T", CaseFoldTurkic },
    { 0, 0 }
};

typedef struct _caseFoldRaw {
    FcChar32	    upper;
    CaseFoldClass   class;
    int		    nout;
    FcChar32	    lower[MAX_OUT];
} CaseFoldRaw;

static void
panic (const char *reason)
{
    fprintf (stderr, "fc-case: panic %s\n", reason);
    exit (1);
}

int			maxExpand;
static FcCaseFold	*folds;
int			nfolds;

static FcCaseFold *
addFold (void)
{
    if (folds)
	folds = realloc (folds, (nfolds + 1) * sizeof (FcCaseFold));
    else
	folds = malloc (sizeof (FcCaseFold));
    if (!folds)
	panic ("out of memory");
    return &folds[nfolds++];
}

static int
ucs4_to_utf8 (FcChar32	ucs4,
	      FcChar8	dest[FC_UTF8_MAX_LEN])
{
    int	bits;
    FcChar8 *d = dest;
    
    if      (ucs4 <       0x80) {  *d++=  ucs4;                         bits= -6; }
    else if (ucs4 <      0x800) {  *d++= ((ucs4 >>  6) & 0x1F) | 0xC0;  bits=  0; }
    else if (ucs4 <    0x10000) {  *d++= ((ucs4 >> 12) & 0x0F) | 0xE0;  bits=  6; }
    else if (ucs4 <   0x200000) {  *d++= ((ucs4 >> 18) & 0x07) | 0xF0;  bits= 12; }
    else if (ucs4 <  0x4000000) {  *d++= ((ucs4 >> 24) & 0x03) | 0xF8;  bits= 18; }
    else if (ucs4 < 0x80000000) {  *d++= ((ucs4 >> 30) & 0x01) | 0xFC;  bits= 24; }
    else return 0;

    for ( ; bits >= 0; bits-= 6) {
	*d++= ((ucs4 >> bits) & 0x3F) | 0x80;
    }
    return d - dest;
}

static int
utf8_size (FcChar32 ucs4)
{
    FcChar8 utf8[FC_UTF8_MAX_LEN];
    return ucs4_to_utf8 (ucs4, utf8 );
}

static FcChar8	*foldChars;
static int	nfoldChars;
static int	maxFoldChars;
static FcChar32	minFoldChar;
static FcChar32	maxFoldChar;

static void
addChar (FcChar32 c)
{
    FcChar8	utf8[FC_UTF8_MAX_LEN];
    int		len;
    int		i;

    len = ucs4_to_utf8 (c, utf8);
    if (foldChars)
	foldChars = realloc (foldChars, (nfoldChars + len) * sizeof (FcChar8));
    else
	foldChars = malloc (sizeof (FcChar8) * len);
    if (!foldChars)
	panic ("out of memory");
    for (i = 0; i < len; i++)
	foldChars[nfoldChars + i] = utf8[i];
    nfoldChars += len;
}

static int
foldExtends (FcCaseFold *fold, CaseFoldRaw *raw)
{
    switch (fold->method) {
    case FC_CASE_FOLD_RANGE:
	if ((short) (raw->lower[0] - raw->upper) != fold->offset)
	    return 0;
	if (raw->upper != fold->upper + fold->count)
	    return 0;
	return 1;
    case FC_CASE_FOLD_EVEN_ODD:
	if ((short) (raw->lower[0] - raw->upper) != 1)
	    return 0;
	if (raw->upper != fold->upper + fold->count + 1)
	    return 0;
	return 1;
    case FC_CASE_FOLD_FULL:
	break;
    }
    return 0;
}
	    
static const char *
case_fold_method_name (FcChar16 method)
{
    switch (method) {
    case FC_CASE_FOLD_RANGE:	return "FC_CASE_FOLD_RANGE,";
    case FC_CASE_FOLD_EVEN_ODD: return "FC_CASE_FOLD_EVEN_ODD,";
    case FC_CASE_FOLD_FULL:	return "FC_CASE_FOLD_FULL,";
    default:			return "unknown";
    }
}

static void
dump (void)
{
    int	    i;
    
    printf (   "#define FC_NUM_CASE_FOLD	%d\n", nfolds);
    printf (   "#define FC_NUM_CASE_FOLD_CHARS	%d\n", nfoldChars);
    printf (   "#define FC_MAX_CASE_FOLD_CHARS	%d\n", maxFoldChars);
    printf (   "#define FC_MAX_CASE_FOLD_EXPAND	%d\n", maxExpand);
    printf (   "#define FC_MIN_FOLD_CHAR	0x%08x\n", minFoldChar);
    printf (   "#define FC_MAX_FOLD_CHAR	0x%08x\n", maxFoldChar);
    printf (   "\n");
    
    /*
     * Dump out ranges
     */
    printf ("static const FcCaseFold    fcCaseFold[FC_NUM_CASE_FOLD] = {\n");
    for (i = 0; i < nfolds; i++)
    {
	printf ("    { 0x%08x, %-22s 0x%04x, %6d },\n",
		folds[i].upper, case_fold_method_name (folds[i].method),
		folds[i].count, folds[i].offset);
    }
    printf ("};\n\n");

    /*
     * Dump out "other" values
     */

    printf ("static const FcChar8	fcCaseFoldChars[FC_NUM_CASE_FOLD_CHARS] = {\n");
    for (i = 0; i < nfoldChars; i++)
    {
	printf ("0x%02x", foldChars[i]);
	if (i != nfoldChars - 1)
	{
	    if ((i & 0xf) == 0xf) 
		printf (",\n");
	    else
		printf (",");
	}
    }
    printf ("\n};\n");
}

/*
 * Read the standard Unicode CaseFolding.txt file
 */
#define SEP "; \t\n"

static int
parseRaw (char *line, CaseFoldRaw *raw)
{
    char    *tok, *end;
    int	    i;
    
    if (!isxdigit (line[0]))
	return 0;
    /*
     * Get upper case value
     */
    tok = strtok (line, SEP);
    if (!tok || tok[0] == '#')
	return 0;
    raw->upper = strtol (tok, &end, 16);
    if (end == tok)
	return 0;
    /*
     * Get class
     */
    tok = strtok (NULL, SEP);
    if (!tok || tok[0] == '#')
	return 0;
    for (i = 0; caseFoldClassMap[i].name; i++)
	if (!strcmp (tok, caseFoldClassMap[i].name))
	{
	    raw->class = caseFoldClassMap[i].class;
	    break;
	}
    if (!caseFoldClassMap[i].name)
	return 0;
	
    /*
     * Get list of result characters
     */
    for (i = 0; i < MAX_OUT; i++)
    {
	tok = strtok (NULL, SEP);
	if (!tok || tok[0] == '#')
	    break;
	raw->lower[i] = strtol (tok, &end, 16);
	if (end == tok)
	    break;
    }
    if (i == 0)
	return 0;
    raw->nout = i;
    return 1;
}

static int
caseFoldReadRaw (FILE *in, CaseFoldRaw *raw)
{
    char    line[MAX_LINE];

    for (;;)
    {
	if (!fgets (line, sizeof (line) - 1, in))
	    return 0;
	if (parseRaw (line, raw))
	    return 1;
    }
}

int
main (int argc, char **argv)
{
    FcCaseFold		*fold = 0;
    CaseFoldRaw		raw;
    int			i;
    FILE		*caseFile;
    char		line[MAX_LINE];
    int			expand;

    if (argc != 2)
	panic ("usage: fc-case CaseFolding.txt");
    caseFile = fopen (argv[1], "r");
    if (!caseFile)
	panic ("can't open case folding file");
    
    while (caseFoldReadRaw (caseFile, &raw))
    {
	if (!minFoldChar)
	    minFoldChar = raw.upper;
	maxFoldChar = raw.upper;
	switch (raw.class) {
	case CaseFoldCommon:
	case CaseFoldFull:
	    if (raw.nout == 1)
	    {
		if (fold && foldExtends (fold, &raw))
		    fold->count = raw.upper - fold->upper + 1;
		else
		{
		    fold = addFold ();
		    fold->upper = raw.upper;
		    fold->offset = raw.lower[0] - raw.upper;
		    if (fold->offset == 1)
			fold->method = FC_CASE_FOLD_EVEN_ODD;
		    else
			fold->method = FC_CASE_FOLD_RANGE;
		    fold->count = 1;
		}
		expand = utf8_size (raw.lower[0]) - utf8_size(raw.upper);
	    }
	    else
	    {
		fold = addFold ();
		fold->upper = raw.upper;
		fold->method = FC_CASE_FOLD_FULL;
		fold->offset = nfoldChars;
		for (i = 0; i < raw.nout; i++)
		    addChar (raw.lower[i]);
		fold->count = nfoldChars - fold->offset;
		if (fold->count > maxFoldChars)
		    maxFoldChars = fold->count;
		expand = fold->count - utf8_size (raw.upper);
	    }
	    if (expand > maxExpand)
		maxExpand = expand;
	    break;
	case CaseFoldSimple:
	    break;
	case CaseFoldTurkic:
	    break;
	}
    }
    /*
     * Scan the input until the marker is found
     */
    
    while (fgets (line, sizeof (line), stdin))
    {
	if (!strncmp (line, "@@@", 3))
	    break;
	fputs (line, stdout);
    }
    
    /*
     * Dump these tables
     */
    dump ();
    
    /*
     * And flush out the rest of the input file
     */

    while (fgets (line, sizeof (line), stdin))
	fputs (line, stdout);
    
    fflush (stdout);
    exit (ferror (stdout));
}