4 * Copyright © 2004 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.
31 /* stub definitions for declarations from fcint.h.. */
32 int * _fcBankId
= 0, * _fcBankIdx
= 0;
33 FcValueList
** _fcValueLists
= 0;
34 FcPatternElt
** _fcPatternElts
= 0;
38 FcCacheBankToIndexMTF (int bank
)
42 /* end stub definitions */
44 typedef enum _caseFoldClass
{ CaseFoldCommon
, CaseFoldFull
, CaseFoldSimple
, CaseFoldTurkic
} CaseFoldClass
;
46 typedef struct _caseFoldClassMap
{
51 static const CaseFoldClassMap caseFoldClassMap
[] = {
52 { "C", CaseFoldCommon
},
53 { "F", CaseFoldFull
},
54 { "S", CaseFoldSimple
},
55 { "T", CaseFoldTurkic
},
59 typedef struct _caseFoldRaw
{
63 FcChar32 lower
[MAX_OUT
];
67 panic (const char *reason
)
69 fprintf (stderr
, "fc-case: panic %s\n", reason
);
74 static FcCaseFold
*folds
;
81 folds
= realloc (folds
, (nfolds
+ 1) * sizeof (FcCaseFold
));
83 folds
= malloc (sizeof (FcCaseFold
));
85 panic ("out of memory");
86 return &folds
[nfolds
++];
90 ucs4_to_utf8 (FcChar32 ucs4
,
91 FcChar8 dest
[FC_UTF8_MAX_LEN
])
96 if (ucs4
< 0x80) { *d
++= ucs4
; bits
= -6; }
97 else if (ucs4
< 0x800) { *d
++= ((ucs4
>> 6) & 0x1F) | 0xC0; bits
= 0; }
98 else if (ucs4
< 0x10000) { *d
++= ((ucs4
>> 12) & 0x0F) | 0xE0; bits
= 6; }
99 else if (ucs4
< 0x200000) { *d
++= ((ucs4
>> 18) & 0x07) | 0xF0; bits
= 12; }
100 else if (ucs4
< 0x4000000) { *d
++= ((ucs4
>> 24) & 0x03) | 0xF8; bits
= 18; }
101 else if (ucs4
< 0x80000000) { *d
++= ((ucs4
>> 30) & 0x01) | 0xFC; bits
= 24; }
104 for ( ; bits
>= 0; bits
-= 6) {
105 *d
++= ((ucs4
>> bits
) & 0x3F) | 0x80;
111 utf8_size (FcChar32 ucs4
)
113 FcChar8 utf8
[FC_UTF8_MAX_LEN
];
114 return ucs4_to_utf8 (ucs4
, utf8
);
117 static FcChar8
*foldChars
;
118 static int nfoldChars
;
119 static int maxFoldChars
;
120 static FcChar32 minFoldChar
;
121 static FcChar32 maxFoldChar
;
126 FcChar8 utf8
[FC_UTF8_MAX_LEN
];
130 len
= ucs4_to_utf8 (c
, utf8
);
132 foldChars
= realloc (foldChars
, (nfoldChars
+ len
) * sizeof (FcChar8
));
134 foldChars
= malloc (sizeof (FcChar8
) * len
);
136 panic ("out of memory");
137 for (i
= 0; i
< len
; i
++)
138 foldChars
[nfoldChars
+ i
] = utf8
[i
];
143 foldExtends (FcCaseFold
*fold
, CaseFoldRaw
*raw
)
145 switch (fold
->method
) {
146 case FC_CASE_FOLD_RANGE
:
147 if ((short) (raw
->lower
[0] - raw
->upper
) != fold
->offset
)
149 if (raw
->upper
!= fold
->upper
+ fold
->count
)
152 case FC_CASE_FOLD_EVEN_ODD
:
153 if ((short) (raw
->lower
[0] - raw
->upper
) != 1)
155 if (raw
->upper
!= fold
->upper
+ fold
->count
+ 1)
158 case FC_CASE_FOLD_FULL
:
165 case_fold_method_name (FcChar16 method
)
168 case FC_CASE_FOLD_RANGE
: return "FC_CASE_FOLD_RANGE,";
169 case FC_CASE_FOLD_EVEN_ODD
: return "FC_CASE_FOLD_EVEN_ODD,";
170 case FC_CASE_FOLD_FULL
: return "FC_CASE_FOLD_FULL,";
171 default: return "unknown";
180 printf ( "#define FC_NUM_CASE_FOLD %d\n", nfolds
);
181 printf ( "#define FC_NUM_CASE_FOLD_CHARS %d\n", nfoldChars
);
182 printf ( "#define FC_MAX_CASE_FOLD_CHARS %d\n", maxFoldChars
);
183 printf ( "#define FC_MAX_CASE_FOLD_EXPAND %d\n", maxExpand
);
184 printf ( "#define FC_MIN_FOLD_CHAR 0x%08x\n", minFoldChar
);
185 printf ( "#define FC_MAX_FOLD_CHAR 0x%08x\n", maxFoldChar
);
191 printf ("static const FcCaseFold fcCaseFold[FC_NUM_CASE_FOLD] = {\n");
192 for (i
= 0; i
< nfolds
; i
++)
194 printf (" { 0x%08x, %-22s 0x%04x, %6d },\n",
195 folds
[i
].upper
, case_fold_method_name (folds
[i
].method
),
196 folds
[i
].count
, folds
[i
].offset
);
201 * Dump out "other" values
204 printf ("static const FcChar8 fcCaseFoldChars[FC_NUM_CASE_FOLD_CHARS] = {\n");
205 for (i
= 0; i
< nfoldChars
; i
++)
207 printf ("0x%02x", foldChars
[i
]);
208 if (i
!= nfoldChars
- 1)
210 if ((i
& 0xf) == 0xf)
220 * Read the standard Unicode CaseFolding.txt file
225 parseRaw (char *line
, CaseFoldRaw
*raw
)
230 if (!isxdigit (line
[0]))
233 * Get upper case value
235 tok
= strtok (line
, SEP
);
236 if (!tok
|| tok
[0] == '#')
238 raw
->upper
= strtol (tok
, &end
, 16);
244 tok
= strtok (NULL
, SEP
);
245 if (!tok
|| tok
[0] == '#')
247 for (i
= 0; caseFoldClassMap
[i
].name
; i
++)
248 if (!strcmp (tok
, caseFoldClassMap
[i
].name
))
250 raw
->class = caseFoldClassMap
[i
].class;
253 if (!caseFoldClassMap
[i
].name
)
257 * Get list of result characters
259 for (i
= 0; i
< MAX_OUT
; i
++)
261 tok
= strtok (NULL
, SEP
);
262 if (!tok
|| tok
[0] == '#')
264 raw
->lower
[i
] = strtol (tok
, &end
, 16);
275 caseFoldReadRaw (FILE *in
, CaseFoldRaw
*raw
)
281 if (!fgets (line
, sizeof (line
) - 1, in
))
283 if (parseRaw (line
, raw
))
289 main (int argc
, char **argv
)
291 FcCaseFold
*fold
= 0;
299 panic ("usage: fc-case CaseFolding.txt");
300 caseFile
= fopen (argv
[1], "r");
302 panic ("can't open case folding file");
304 while (caseFoldReadRaw (caseFile
, &raw
))
307 minFoldChar
= raw
.upper
;
308 maxFoldChar
= raw
.upper
;
314 if (fold
&& foldExtends (fold
, &raw
))
315 fold
->count
= raw
.upper
- fold
->upper
+ 1;
319 fold
->upper
= raw
.upper
;
320 fold
->offset
= raw
.lower
[0] - raw
.upper
;
321 if (fold
->offset
== 1)
322 fold
->method
= FC_CASE_FOLD_EVEN_ODD
;
324 fold
->method
= FC_CASE_FOLD_RANGE
;
327 expand
= utf8_size (raw
.lower
[0]) - utf8_size(raw
.upper
);
332 fold
->upper
= raw
.upper
;
333 fold
->method
= FC_CASE_FOLD_FULL
;
334 fold
->offset
= nfoldChars
;
335 for (i
= 0; i
< raw
.nout
; i
++)
336 addChar (raw
.lower
[i
]);
337 fold
->count
= nfoldChars
- fold
->offset
;
338 if (fold
->count
> maxFoldChars
)
339 maxFoldChars
= fold
->count
;
340 expand
= fold
->count
- utf8_size (raw
.upper
);
342 if (expand
> maxExpand
)
352 * Scan the input until the marker is found
355 while (fgets (line
, sizeof (line
), stdin
))
357 if (!strncmp (line
, "@@@", 3))
359 fputs (line
, stdout
);
368 * And flush out the rest of the input file
371 while (fgets (line
, sizeof (line
), stdin
))
372 fputs (line
, stdout
);
375 exit (ferror (stdout
));