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 mirrored;
27 unsigned char east_asian_width;
28 unsigned char normalization_check;
33 unsigned short from, to;
41 #include "unicodedata_db.h"
43 /* constants required for Hangul (de)composition */
52 #define NCOUNT (VCOUNT * TCOUNT)
54 static const UCDRecord *get_ucd_record(uint32_t code)
61 index = index0[code >> (SHIFT1+SHIFT2)] << SHIFT1;
62 offset = (code >> SHIFT2) & ((1<<SHIFT1) - 1);
63 index = index1[index + offset] << SHIFT2;
64 offset = code & ((1<<SHIFT2) - 1);
65 index = index2[index + offset];
68 return &ucd_records[index];
71 static const unsigned short *get_decomp_record(uint32_t code)
78 index = decomp_index0[code >> (DECOMP_SHIFT1+DECOMP_SHIFT2)]
80 offset = (code >> DECOMP_SHIFT2) & ((1<<DECOMP_SHIFT1) - 1);
81 index = decomp_index1[index + offset] << DECOMP_SHIFT2;
82 offset = code & ((1<<DECOMP_SHIFT2) - 1);
83 index = decomp_index2[index + offset];
86 return &decomp_data[index];
89 static int get_comp_index(uint32_t code, const Reindex *idx)
93 for (i = 0; idx[i].start; i++) {
94 const Reindex *cur = &idx[i];
95 if (code < cur->start)
97 if (code <= cur->start + cur->count) {
98 return cur->index + (code - cur->start);
105 static int compare_mp(const void *a, const void *b)
107 MirrorPair *mpa = (MirrorPair *)a;
108 MirrorPair *mpb = (MirrorPair *)b;
109 return mpa->from - mpb->from;
112 static int hangul_pair_decompose(uint32_t code, uint32_t *a, uint32_t *b)
114 int si = code - SBASE;
116 if (si < 0 || si >= SCOUNT)
121 *a = SBASE + (si / TCOUNT) * TCOUNT;
122 *b = TBASE + (si % TCOUNT);
126 *a = LBASE + (si / NCOUNT);
127 *b = VBASE + (si % NCOUNT) / TCOUNT;
132 static int hangul_pair_compose(uint32_t *code, uint32_t a, uint32_t b)
134 if (b < VBASE || b >= (TBASE + TCOUNT))
137 if ((a < LBASE || a >= (LBASE + LCOUNT))
138 && (a < SBASE || a >= (SBASE + SCOUNT)))
143 *code = a + (b - TBASE);
149 *code = SBASE + li * NCOUNT + vi * TCOUNT;
154 static uint32_t decode_utf16(const unsigned short **code_ptr)
156 const unsigned short *code = *code_ptr;
158 if ((code[0] & 0xd800) != 0xd800) {
160 return (uint32_t)code[0];
163 return 0x10000 + ((uint32_t)code[1] - 0xdc00) +
164 (((uint32_t)code[0] - 0xd800) << 10);
168 const char *ucdn_get_unicode_version(void)
170 return UNIDATA_VERSION;
173 int ucdn_get_combining_class(uint32_t code)
175 return get_ucd_record(code)->combining;
178 int ucdn_get_east_asian_width(uint32_t code)
180 return get_ucd_record(code)->east_asian_width;
183 int ucdn_get_general_category(uint32_t code)
185 return get_ucd_record(code)->category;
188 int ucdn_get_bidi_class(uint32_t code)
190 return get_ucd_record(code)->bidi_class;
193 int ucdn_get_mirrored(uint32_t code)
195 return get_ucd_record(code)->mirrored;
198 int ucdn_get_script(uint32_t code)
200 return get_ucd_record(code)->script;
203 uint32_t ucdn_mirror(uint32_t code)
208 if (get_ucd_record(code)->mirrored == 0)
212 res = bsearch(&mp, mirror_pairs, BIDI_MIRROR_LEN, sizeof(MirrorPair),
221 int ucdn_decompose(uint32_t code, uint32_t *a, uint32_t *b)
223 const unsigned short *rec;
226 if (hangul_pair_decompose(code, a, b))
229 rec = get_decomp_record(code);
232 if ((rec[0] & 0xff) != 0 || len == 0)
236 *a = decode_utf16(&rec);
238 *b = decode_utf16(&rec);
245 int ucdn_compose(uint32_t *code, uint32_t a, uint32_t b)
247 int l, r, index, indexi, offset;
249 if (hangul_pair_compose(code, a, b))
252 l = get_comp_index(a, nfc_first);
253 r = get_comp_index(b, nfc_last);
258 indexi = l * TOTAL_LAST + r;
259 index = comp_index0[indexi >> (COMP_SHIFT1+COMP_SHIFT2)] << COMP_SHIFT1;
260 offset = (indexi >> COMP_SHIFT2) & ((1<<COMP_SHIFT1) - 1);
261 index = comp_index1[index + offset] << COMP_SHIFT2;
262 offset = indexi & ((1<<COMP_SHIFT2) - 1);
263 *code = comp_data[index + offset];
268 int ucdn_compat_decompose(uint32_t code, uint32_t *decomposed)
271 const unsigned short *rec = get_decomp_record(code);
278 for (i = 0; i < len; i++)
279 decomposed[i] = decode_utf16(&rec);