2 * Copyright (C) 2012 Grigori Goronzy <greg@kinoho.net>
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 unsigned char category;
24 unsigned char combining;
25 unsigned char bidi_class;
26 unsigned char east_asian_width;
28 unsigned char linebreak_class;
32 unsigned short from, to;
36 unsigned short from, to;
47 /* constants required for Hangul (de)composition */
56 #define NCOUNT (VCOUNT * TCOUNT)
58 static const UCDRecord *get_ucd_record(uint32_t code)
65 index = index0[code >> (SHIFT1+SHIFT2)] << SHIFT1;
66 offset = (code >> SHIFT2) & ((1<<SHIFT1) - 1);
67 index = index1[index + offset] << SHIFT2;
68 offset = code & ((1<<SHIFT2) - 1);
69 index = index2[index + offset];
72 return &ucd_records[index];
75 static const unsigned short *get_decomp_record(uint32_t code)
82 index = decomp_index0[code >> (DECOMP_SHIFT1+DECOMP_SHIFT2)]
84 offset = (code >> DECOMP_SHIFT2) & ((1<<DECOMP_SHIFT1) - 1);
85 index = decomp_index1[index + offset] << DECOMP_SHIFT2;
86 offset = code & ((1<<DECOMP_SHIFT2) - 1);
87 index = decomp_index2[index + offset];
90 return &decomp_data[index];
93 static int compare_reindex(const void *a, const void *b)
95 Reindex *ra = (Reindex *)a;
96 Reindex *rb = (Reindex *)b;
98 if (ra->start < rb->start)
100 else if (ra->start > (rb->start + rb->count))
106 static int get_comp_index(uint32_t code, const Reindex *idx, size_t len)
109 Reindex r = {0, 0, 0};
111 res = (Reindex *) bsearch(&r, idx, len, sizeof(Reindex), compare_reindex);
114 return res->index + (code - res->start);
119 static int compare_mp(const void *a, const void *b)
121 MirrorPair *mpa = (MirrorPair *)a;
122 MirrorPair *mpb = (MirrorPair *)b;
123 return mpa->from - mpb->from;
126 static int compare_bp(const void *a, const void *b)
128 BracketPair *bpa = (BracketPair *)a;
129 BracketPair *bpb = (BracketPair *)b;
130 return bpa->from - bpb->from;
133 static BracketPair *search_bp(uint32_t code)
135 BracketPair bp = {0,0,2};
139 res = (BracketPair *) bsearch(&bp, bracket_pairs, BIDI_BRACKET_LEN,
140 sizeof(BracketPair), compare_bp);
144 static int hangul_pair_decompose(uint32_t code, uint32_t *a, uint32_t *b)
146 int si = code - SBASE;
148 if (si < 0 || si >= SCOUNT)
153 *a = SBASE + (si / TCOUNT) * TCOUNT;
154 *b = TBASE + (si % TCOUNT);
158 *a = LBASE + (si / NCOUNT);
159 *b = VBASE + (si % NCOUNT) / TCOUNT;
164 static int hangul_pair_compose(uint32_t *code, uint32_t a, uint32_t b)
166 if (a >= SBASE && a < (SBASE + SCOUNT) && b >= TBASE && b < (TBASE + TCOUNT)) {
168 *code = a + (b - TBASE);
170 } else if (a >= LBASE && a < (LBASE + LCOUNT) && b >= VBASE && b < (VBASE + VCOUNT)) {
174 *code = SBASE + li * NCOUNT + vi * TCOUNT;
181 static uint32_t decode_utf16(const unsigned short **code_ptr)
183 const unsigned short *code = *code_ptr;
185 if (code[0] < 0xd800 || code[0] > 0xdc00) {
187 return (uint32_t)code[0];
190 return 0x10000 + ((uint32_t)code[1] - 0xdc00) +
191 (((uint32_t)code[0] - 0xd800) << 10);
195 const char *ucdn_get_unicode_version(void)
197 return UNIDATA_VERSION;
200 int ucdn_get_combining_class(uint32_t code)
202 return get_ucd_record(code)->combining;
205 int ucdn_get_east_asian_width(uint32_t code)
207 return get_ucd_record(code)->east_asian_width;
210 int ucdn_get_general_category(uint32_t code)
212 return get_ucd_record(code)->category;
215 int ucdn_get_bidi_class(uint32_t code)
217 return get_ucd_record(code)->bidi_class;
220 int ucdn_get_mirrored(uint32_t code)
222 return ucdn_mirror(code) != code;
225 int ucdn_get_script(uint32_t code)
227 return get_ucd_record(code)->script;
230 int ucdn_get_linebreak_class(uint32_t code)
232 return get_ucd_record(code)->linebreak_class;
235 int ucdn_get_resolved_linebreak_class(uint32_t code)
237 const UCDRecord *record = get_ucd_record(code);
239 switch (record->linebreak_class)
241 case UCDN_LINEBREAK_CLASS_AI:
242 case UCDN_LINEBREAK_CLASS_SG:
243 case UCDN_LINEBREAK_CLASS_XX:
244 return UCDN_LINEBREAK_CLASS_AL;
246 case UCDN_LINEBREAK_CLASS_SA:
247 if (record->category == UCDN_GENERAL_CATEGORY_MC ||
248 record->category == UCDN_GENERAL_CATEGORY_MN)
249 return UCDN_LINEBREAK_CLASS_CM;
250 return UCDN_LINEBREAK_CLASS_AL;
252 case UCDN_LINEBREAK_CLASS_CJ:
253 return UCDN_LINEBREAK_CLASS_NS;
255 case UCDN_LINEBREAK_CLASS_CB:
256 return UCDN_LINEBREAK_CLASS_B2;
258 case UCDN_LINEBREAK_CLASS_NL:
259 return UCDN_LINEBREAK_CLASS_BK;
262 return record->linebreak_class;
266 uint32_t ucdn_mirror(uint32_t code)
272 res = (MirrorPair *) bsearch(&mp, mirror_pairs, BIDI_MIRROR_LEN,
273 sizeof(MirrorPair), compare_mp);
281 uint32_t ucdn_paired_bracket(uint32_t code)
283 BracketPair *res = search_bp(code);
290 int ucdn_paired_bracket_type(uint32_t code)
292 BracketPair *res = search_bp(code);
294 return UCDN_BIDI_PAIRED_BRACKET_TYPE_NONE;
299 int ucdn_decompose(uint32_t code, uint32_t *a, uint32_t *b)
301 const unsigned short *rec;
304 if (hangul_pair_decompose(code, a, b))
307 rec = get_decomp_record(code);
310 if ((rec[0] & 0xff) != 0 || len == 0)
314 *a = decode_utf16(&rec);
316 *b = decode_utf16(&rec);
323 int ucdn_compose(uint32_t *code, uint32_t a, uint32_t b)
325 int l, r, index, indexi, offset;
327 if (hangul_pair_compose(code, a, b))
330 l = get_comp_index(a, nfc_first, sizeof(nfc_first) / sizeof(Reindex));
331 r = get_comp_index(b, nfc_last, sizeof(nfc_last) / sizeof(Reindex));
336 indexi = l * TOTAL_LAST + r;
337 index = comp_index0[indexi >> (COMP_SHIFT1+COMP_SHIFT2)] << COMP_SHIFT1;
338 offset = (indexi >> COMP_SHIFT2) & ((1<<COMP_SHIFT1) - 1);
339 index = comp_index1[index + offset] << COMP_SHIFT2;
340 offset = indexi & ((1<<COMP_SHIFT2) - 1);
341 *code = comp_data[index + offset];
346 int ucdn_compat_decompose(uint32_t code, uint32_t *decomposed)
349 const unsigned short *rec = get_decomp_record(code);
356 for (i = 0; i < len; i++)
357 decomposed[i] = decode_utf16(&rec);