summaryrefslogtreecommitdiff
path: root/source/fitz/ucdn.c
blob: 4e000246077e1f3a8cb97f666471f23b79a705c8 (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
/*
 * 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 "mupdf/fitz.h"
#include "mupdf/ucdn.h"

#include <stdio.h>
#include <stdlib.h>

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

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

typedef struct {
	unsigned short from, to;
	unsigned char type;
} BracketPair;

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

#include "ucdn_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(uint32_t 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(uint32_t 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 compare_reindex(const void *a, const void *b)
{
	Reindex *ra = (Reindex *)a;
	Reindex *rb = (Reindex *)b;

	if (ra->start < rb->start)
		return -1;
	else if (ra->start > (rb->start + rb->count))
		return 1;
	else
		return 0;
}

static int get_comp_index(uint32_t code, const Reindex *idx, size_t len)
{
	Reindex *res;
	Reindex r = {0, 0, 0};
	r.start = code;
	res = (Reindex *) bsearch(&r, idx, len, sizeof(Reindex), compare_reindex);

	if (res != NULL)
		return res->index + (code - res->start);
	else
		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 compare_bp(const void *a, const void *b)
{
	BracketPair *bpa = (BracketPair *)a;
	BracketPair *bpb = (BracketPair *)b;
	return bpa->from - bpb->from;
}

static BracketPair *search_bp(uint32_t code)
{
	BracketPair bp = {0,0,2};
	BracketPair *res;

	bp.from = code;
	res = (BracketPair *) bsearch(&bp, bracket_pairs, BIDI_BRACKET_LEN,
			sizeof(BracketPair), compare_bp);
	return res;
}

static int hangul_pair_decompose(uint32_t code, uint32_t *a, uint32_t *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(uint32_t *code, uint32_t a, uint32_t b)
{
	if (a >= SBASE && a < (SBASE + SCOUNT) && b >= TBASE && b < (TBASE + TCOUNT)) {
		/* LV,T */
		*code = a + (b - TBASE);
		return 3;
	} else if (a >= LBASE && a < (LBASE + LCOUNT) && b >= VBASE && b < (VBASE + VCOUNT)) {
		/* L,V */
		int li = a - LBASE;
		int vi = b - VBASE;
		*code = SBASE + li * NCOUNT + vi * TCOUNT;
		return 2;
	} else {
		return 0;
	}
}

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

	if (code[0] < 0xd800 || code[0] > 0xdc00) {
		*code_ptr += 1;
		return (uint32_t)code[0];
	} else {
		*code_ptr += 2;
		return 0x10000 + ((uint32_t)code[1] - 0xdc00) +
			(((uint32_t)code[0] - 0xd800) << 10);
	}
}

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

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

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

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

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

int ucdn_get_mirrored(uint32_t code)
{
	return ucdn_mirror(code) != code;
}

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

int ucdn_get_linebreak_class(uint32_t code)
{
	return get_ucd_record(code)->linebreak_class;
}

int ucdn_get_resolved_linebreak_class(uint32_t code)
{
	const UCDRecord *record = get_ucd_record(code);

	switch (record->linebreak_class)
	{
	case UCDN_LINEBREAK_CLASS_AI:
	case UCDN_LINEBREAK_CLASS_SG:
	case UCDN_LINEBREAK_CLASS_XX:
		return UCDN_LINEBREAK_CLASS_AL;

	case UCDN_LINEBREAK_CLASS_SA:
		if (record->category == UCDN_GENERAL_CATEGORY_MC ||
				record->category == UCDN_GENERAL_CATEGORY_MN)
			return UCDN_LINEBREAK_CLASS_CM;
		return UCDN_LINEBREAK_CLASS_AL;

	case UCDN_LINEBREAK_CLASS_CJ:
		return UCDN_LINEBREAK_CLASS_NS;

	case UCDN_LINEBREAK_CLASS_CB:
		return UCDN_LINEBREAK_CLASS_B2;

	case UCDN_LINEBREAK_CLASS_NL:
		return UCDN_LINEBREAK_CLASS_BK;

	default:
		return record->linebreak_class;
	}
}

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

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

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

uint32_t ucdn_paired_bracket(uint32_t code)
{
	BracketPair *res = search_bp(code);
	if (res == NULL)
		return code;
	else
		return res->to;
}

int ucdn_paired_bracket_type(uint32_t code)
{
	BracketPair *res = search_bp(code);
	if (res == NULL)
		return UCDN_BIDI_PAIRED_BRACKET_TYPE_NONE;
	else
		return res->type;
}

int ucdn_decompose(uint32_t code, uint32_t *a, uint32_t *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(uint32_t *code, uint32_t a, uint32_t b)
{
	int l, r, index, indexi, offset;

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

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

	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(uint32_t code, uint32_t *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;
}