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.
22 unsigned char category;
23 unsigned char combining;
24 unsigned char bidi_class;
25 unsigned char mirrored;
26 unsigned char east_asian_width;
27 unsigned char normalization_check;
32 unsigned short from, to;
40 #include "unicodedata_db.h"
42 /* constants required for Hangul (de)composition */
51 #define NCOUNT (VCOUNT * TCOUNT)
53 static const UCDRecord *get_ucd_record(uint32_t code)
60 index = index0[code >> (SHIFT1+SHIFT2)] << SHIFT1;
61 offset = (code >> SHIFT2) & ((1<<SHIFT1) - 1);
62 index = index1[index + offset] << SHIFT2;
63 offset = code & ((1<<SHIFT2) - 1);
64 index = index2[index + offset];
67 return &ucd_records[index];
70 static const unsigned short *get_decomp_record(uint32_t code)
77 index = decomp_index0[code >> (DECOMP_SHIFT1+DECOMP_SHIFT2)]
79 offset = (code >> DECOMP_SHIFT2) & ((1<<DECOMP_SHIFT1) - 1);
80 index = decomp_index1[index + offset] << DECOMP_SHIFT2;
81 offset = code & ((1<<DECOMP_SHIFT2) - 1);
82 index = decomp_index2[index + offset];
85 return &decomp_data[index];
88 static int get_comp_index(uint32_t code, const Reindex *idx)
92 for (i = 0; idx[i].start; i++) {
93 const Reindex *cur = &idx[i];
94 if (code < cur->start)
96 if (code <= cur->start + cur->count) {
97 return cur->index + (code - cur->start);
104 static int compare_mp(const void *a, const void *b)
106 MirrorPair *mpa = (MirrorPair *)a;
107 MirrorPair *mpb = (MirrorPair *)b;
108 return mpa->from - mpb->from;
111 static int hangul_pair_decompose(uint32_t code, uint32_t *a, uint32_t *b)
113 int si = code - SBASE;
115 if (si < 0 || si >= SCOUNT)
120 *a = SBASE + (si / TCOUNT) * TCOUNT;
121 *b = TBASE + (si % TCOUNT);
125 *a = LBASE + (si / NCOUNT);
126 *b = VBASE + (si % NCOUNT) / TCOUNT;
131 static int hangul_pair_compose(uint32_t *code, uint32_t a, uint32_t b)
133 if (b < VBASE || b >= (TBASE + TCOUNT))
136 if ((a < LBASE || a >= (LBASE + LCOUNT))
137 && (a < SBASE || a >= (SBASE + SCOUNT)))
142 *code = a + (b - TBASE);
148 *code = SBASE + li * NCOUNT + vi * TCOUNT;
153 static uint32_t decode_utf16(const unsigned short **code_ptr)
155 const unsigned short *code = *code_ptr;
157 if ((code[0] & 0xd800) != 0xd800) {
159 return (uint32_t)code[0];
162 return 0x10000 + ((uint32_t)code[1] - 0xdc00) +
163 (((uint32_t)code[0] - 0xd800) << 10);
167 const char *ucdn_get_unicode_version(void)
169 return UNIDATA_VERSION;
172 int ucdn_get_combining_class(uint32_t code)
174 return get_ucd_record(code)->combining;
177 int ucdn_get_east_asian_width(uint32_t code)
179 return get_ucd_record(code)->east_asian_width;
182 int ucdn_get_general_category(uint32_t code)
184 return get_ucd_record(code)->category;
187 int ucdn_get_bidi_class(uint32_t code)
189 return get_ucd_record(code)->bidi_class;
192 int ucdn_get_mirrored(uint32_t code)
194 return get_ucd_record(code)->mirrored;
197 int ucdn_get_script(uint32_t code)
199 return get_ucd_record(code)->script;
202 uint32_t ucdn_mirror(uint32_t code)
207 if (get_ucd_record(code)->mirrored == 0)
211 res = bsearch(&mp, mirror_pairs, BIDI_MIRROR_LEN, sizeof(MirrorPair),
220 int ucdn_decompose(uint32_t code, uint32_t *a, uint32_t *b)
222 const unsigned short *rec;
225 if (hangul_pair_decompose(code, a, b))
228 rec = get_decomp_record(code);
231 if ((rec[0] & 0xff) != 0 || len == 0)
235 *a = decode_utf16(&rec);
237 *b = decode_utf16(&rec);
244 int ucdn_compose(uint32_t *code, uint32_t a, uint32_t b)
246 int l, r, index, indexi, offset;
248 if (hangul_pair_compose(code, a, b))
251 l = get_comp_index(a, nfc_first);
252 r = get_comp_index(b, nfc_last);
257 indexi = l * TOTAL_LAST + r;
258 index = comp_index0[indexi >> (COMP_SHIFT1+COMP_SHIFT2)] << COMP_SHIFT1;
259 offset = (indexi >> COMP_SHIFT2) & ((1<<COMP_SHIFT1) - 1);
260 index = comp_index1[index + offset] << COMP_SHIFT2;
261 offset = indexi & ((1<<COMP_SHIFT2) - 1);
262 *code = comp_data[index + offset];
267 int ucdn_compat_decompose(uint32_t code, uint32_t *decomposed)
270 const unsigned short *rec = get_decomp_record(code);
277 for (i = 0; i < len; i++)
278 decomposed[i] = decode_utf16(&rec);