summaryrefslogtreecommitdiff
path: root/source/fitz/ucdn.c
blob: 8b6856a5bc1d635a12100cdf178e7a7defa76fb7 (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
/*
 * Copyright (C) 2012 Grigori Goronzy <greg@kinoho.net>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, 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 <stdio.h>
#include <stdlib.h>
#include "ucdn.h"

typedef struct {
	unsigned char category;
	unsigned char combining;
	unsigned char bidi_class;
	unsigned char mirrored;
	unsigned char east_asian_width;
	unsigned char normalization_check;
	unsigned char script;
} UCDRecord;

typedef struct {
	unsigned short from, to;
} MirrorPair;

typedef struct {
	unsigned int start;
	short count, index;
} Reindex;

#include "unicodedata_db.h"

/* constants required for Hangul (de)composition */
#define SBASE 0xAC00
#define LBASE 0x1100
#define VBASE 0x1161
#define TBASE 0x11A7
#define SCOUNT 11172
#define LCOUNT 19
#define VCOUNT 21
#define TCOUNT 28
#define NCOUNT (VCOUNT * TCOUNT)

static const UCDRecord *get_ucd_record(unsigned int code)
{
	int index, offset;

	if (code >= 0x110000)
		index = 0;
	else {
		index = index0[code >> (SHIFT1+SHIFT2)] << SHIFT1;
		offset = (code >> SHIFT2) & ((1<<SHIFT1) - 1);
		index = index1[index + offset] << SHIFT2;
		offset = code & ((1<<SHIFT2) - 1);
		index = index2[index + offset];
	}

	return &ucd_records[index];
}

static const unsigned short *get_decomp_record(unsigned int code)
{
	int index, offset;

	if (code >= 0x110000)
		index = 0;
	else {
		index = decomp_index0[code >> (DECOMP_SHIFT1+DECOMP_SHIFT2)]
			<< DECOMP_SHIFT1;
		offset = (code >> DECOMP_SHIFT2) & ((1<<DECOMP_SHIFT1) - 1);
		index = decomp_index1[index + offset] << DECOMP_SHIFT2;
		offset = code & ((1<<DECOMP_SHIFT2) - 1);
		index = decomp_index2[index + offset];
	}

	return &decomp_data[index];
}

static int get_comp_index(unsigned int code, const Reindex *idx)
{
	int i;

	for (i = 0; idx[i].start; i++) {
		const Reindex *cur = &idx[i];
		if (code < cur->start)
			return -1;
		if (code <= cur->start + cur->count) {
			return cur->index + (code - cur->start);
		}
	}

	return -1;
}

static int compare_mp(const void *a, const void *b)
{
	MirrorPair *mpa = (MirrorPair *)a;
	MirrorPair *mpb = (MirrorPair *)b;
	return mpa->from - mpb->from;
}

static int hangul_pair_decompose(unsigned int code, unsigned int *a, unsigned int *b)
{
	int si = code - SBASE;

	if (si < 0 || si >= SCOUNT)
		return 0;

	if (si % TCOUNT) {
		/* LV,T */
		*a = SBASE + (si / TCOUNT) * TCOUNT;
		*b = TBASE + (si % TCOUNT);
		return 3;
	} else {
		/* L,V */
		*a = LBASE + (si / NCOUNT);
		*b = VBASE + (si % NCOUNT) / TCOUNT;
		return 2;
	}
}

static int hangul_pair_compose(unsigned int *code, unsigned int a, unsigned int b)
{
	if (b < VBASE || b >= (TBASE + TCOUNT))
		return 0;

	if ((a < LBASE || a >= (LBASE + LCOUNT))
			&& (a < SBASE || a >= (SBASE + SCOUNT)))
		return 0;

	if (a >= SBASE) {
		/* LV,T */
		*code = a + (b - TBASE);
		return 3;
	} else {
		/* L,V */
		int li = a - LBASE;
		int vi = b - VBASE;
		*code = SBASE + li * NCOUNT + vi * TCOUNT;
		return 2;
	}
}

static unsigned int decode_utf16(const unsigned short **code_ptr)
{
	const unsigned short *code = *code_ptr;

	if ((code[0] & 0xd800) != 0xd800) {
		*code_ptr += 1;
		return (unsigned int)code[0];
	} else {
		*code_ptr += 2;
		return 0x10000 + ((unsigned int)code[1] - 0xdc00) +
			(((unsigned int)code[0] - 0xd800) << 10);
	}
}

const char *ucdn_get_unicode_version(void)
{
	return UNIDATA_VERSION;
}

int ucdn_get_combining_class(unsigned int code)
{
	return get_ucd_record(code)->combining;
}

int ucdn_get_east_asian_width(unsigned int code)
{
	return get_ucd_record(code)->east_asian_width;
}

int ucdn_get_general_category(unsigned int code)
{
	return get_ucd_record(code)->category;
}

int ucdn_get_bidi_class(unsigned int code)
{
	return get_ucd_record(code)->bidi_class;
}

int ucdn_get_mirrored(unsigned int code)
{
	return get_ucd_record(code)->mirrored;
}

int ucdn_get_script(unsigned int code)
{
	return get_ucd_record(code)->script;
}

unsigned int ucdn_mirror(unsigned int code)
{
	MirrorPair mp = {0};
	MirrorPair *res;

	if (get_ucd_record(code)->mirrored == 0)
		return code;

	mp.from = (unsigned short)code;
	res = bsearch(&mp, mirror_pairs, BIDI_MIRROR_LEN, sizeof(MirrorPair),
			compare_mp);

	if (res == NULL)
		return code;
	else
		return res->to;
}

int ucdn_decompose(unsigned int code, unsigned int *a, unsigned int *b)
{
	const unsigned short *rec;
	int len;

	if (hangul_pair_decompose(code, a, b))
		return 1;

	rec = get_decomp_record(code);
	len = rec[0] >> 8;

	if ((rec[0] & 0xff) != 0 || len == 0)
		return 0;

	rec++;
	*a = decode_utf16(&rec);
	if (len > 1)
		*b = decode_utf16(&rec);
	else
		*b = 0;

	return 1;
}

int ucdn_compose(unsigned int *code, unsigned int a, unsigned int b)
{
	int l, r, index, indexi, offset;

	if (hangul_pair_compose(code, a, b))
		return 1;

	l = get_comp_index(a, nfc_first);
	r = get_comp_index(b, nfc_last);

	if (l < 0 || r < 0)
		return 0;

	indexi = l * TOTAL_LAST + r;
	index = comp_index0[indexi >> (COMP_SHIFT1+COMP_SHIFT2)] << COMP_SHIFT1;
	offset = (indexi >> COMP_SHIFT2) & ((1<<COMP_SHIFT1) - 1);
	index = comp_index1[index + offset] << COMP_SHIFT2;
	offset = indexi & ((1<<COMP_SHIFT2) - 1);
	*code = comp_data[index + offset];

	return *code != 0;
}

int ucdn_compat_decompose(unsigned int code, unsigned int *decomposed)
{
	int i, len;
	const unsigned short *rec = get_decomp_record(code);
	len = rec[0] >> 8;

	if (len == 0)
		return 0;

	rec++;
	for (i = 0; i < len; i++)
		decomposed[i] = decode_utf16(&rec);

	return len;
}