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;
29 unsigned char linebreak_class;
33 unsigned short from, to;
37 unsigned short from, to;
46 #include "unicodedata_db.h"
48 /* constants required for Hangul (de)composition */
57 #define NCOUNT (VCOUNT * TCOUNT)
59 static const UCDRecord *get_ucd_record(uint32_t code)
66 index = index0[code >> (SHIFT1+SHIFT2)] << SHIFT1;
67 offset = (code >> SHIFT2) & ((1<<SHIFT1) - 1);
68 index = index1[index + offset] << SHIFT2;
69 offset = code & ((1<<SHIFT2) - 1);
70 index = index2[index + offset];
73 return &ucd_records[index];
76 static const unsigned short *get_decomp_record(uint32_t code)
83 index = decomp_index0[code >> (DECOMP_SHIFT1+DECOMP_SHIFT2)]
85 offset = (code >> DECOMP_SHIFT2) & ((1<<DECOMP_SHIFT1) - 1);
86 index = decomp_index1[index + offset] << DECOMP_SHIFT2;
87 offset = code & ((1<<DECOMP_SHIFT2) - 1);
88 index = decomp_index2[index + offset];
91 return &decomp_data[index];
94 static int get_comp_index(uint32_t code, const Reindex *idx)
98 for (i = 0; idx[i].start; i++) {
99 const Reindex *cur = &idx[i];
100 if (code < cur->start)
102 if (code <= cur->start + cur->count) {
103 return cur->index + (code - cur->start);
110 static int compare_mp(const void *a, const void *b)
112 MirrorPair *mpa = (MirrorPair *)a;
113 MirrorPair *mpb = (MirrorPair *)b;
114 return mpa->from - mpb->from;
117 static int compare_bp(const void *a, const void *b)
119 BracketPair *bpa = (BracketPair *)a;
120 BracketPair *bpb = (BracketPair *)b;
121 return bpa->from - bpb->from;
124 static BracketPair *search_bp(uint32_t code)
126 BracketPair bp = {0,0,2};
130 res = bsearch(&bp, bracket_pairs, BIDI_BRACKET_LEN, sizeof(BracketPair),
135 static int hangul_pair_decompose(uint32_t code, uint32_t *a, uint32_t *b)
137 int si = code - SBASE;
139 if (si < 0 || si >= SCOUNT)
144 *a = SBASE + (si / TCOUNT) * TCOUNT;
145 *b = TBASE + (si % TCOUNT);
149 *a = LBASE + (si / NCOUNT);
150 *b = VBASE + (si % NCOUNT) / TCOUNT;
155 static int hangul_pair_compose(uint32_t *code, uint32_t a, uint32_t b)
157 if (b < VBASE || b >= (TBASE + TCOUNT))
160 if ((a < LBASE || a >= (LBASE + LCOUNT))
161 && (a < SBASE || a >= (SBASE + SCOUNT)))
166 *code = a + (b - TBASE);
172 *code = SBASE + li * NCOUNT + vi * TCOUNT;
177 static uint32_t decode_utf16(const unsigned short **code_ptr)
179 const unsigned short *code = *code_ptr;
181 if ((code[0] & 0xd800) != 0xd800) {
183 return (uint32_t)code[0];
186 return 0x10000 + ((uint32_t)code[1] - 0xdc00) +
187 (((uint32_t)code[0] - 0xd800) << 10);
191 const char *ucdn_get_unicode_version(void)
193 return UNIDATA_VERSION;
196 int ucdn_get_combining_class(uint32_t code)
198 return get_ucd_record(code)->combining;
201 int ucdn_get_east_asian_width(uint32_t code)
203 return get_ucd_record(code)->east_asian_width;
206 int ucdn_get_general_category(uint32_t code)
208 return get_ucd_record(code)->category;
211 int ucdn_get_bidi_class(uint32_t code)
213 return get_ucd_record(code)->bidi_class;
216 int ucdn_get_mirrored(uint32_t code)
218 return get_ucd_record(code)->mirrored;
221 int ucdn_get_script(uint32_t code)
223 return get_ucd_record(code)->script;
226 int ucdn_get_linebreak_class(uint32_t code)
228 return get_ucd_record(code)->linebreak_class;
231 int ucdn_get_resolved_linebreak_class(uint32_t code)
233 const UCDRecord *record = get_ucd_record(code);
235 switch (record->linebreak_class)
237 case UCDN_LINEBREAK_CLASS_AI:
238 case UCDN_LINEBREAK_CLASS_SG:
239 case UCDN_LINEBREAK_CLASS_XX:
240 return UCDN_LINEBREAK_CLASS_AL;
242 case UCDN_LINEBREAK_CLASS_SA:
243 if (record->category == UCDN_GENERAL_CATEGORY_MC ||
244 record->category == UCDN_GENERAL_CATEGORY_MN)
245 return UCDN_LINEBREAK_CLASS_CM;
246 return UCDN_LINEBREAK_CLASS_AL;
248 case UCDN_LINEBREAK_CLASS_CJ:
249 return UCDN_LINEBREAK_CLASS_NS;
251 case UCDN_LINEBREAK_CLASS_CB:
252 return UCDN_LINEBREAK_CLASS_B2;
254 case UCDN_LINEBREAK_CLASS_NL:
255 return UCDN_LINEBREAK_CLASS_BK;
258 return record->linebreak_class;
262 uint32_t ucdn_mirror(uint32_t code)
267 if (get_ucd_record(code)->mirrored == 0)
271 res = bsearch(&mp, mirror_pairs, BIDI_MIRROR_LEN, sizeof(MirrorPair),
280 uint32_t ucdn_paired_bracket(uint32_t code)
282 BracketPair *res = search_bp(code);
289 int ucdn_paired_bracket_type(uint32_t code)
291 BracketPair *res = search_bp(code);
293 return UCDN_BIDI_PAIRED_BRACKET_TYPE_NONE;
298 int ucdn_decompose(uint32_t code, uint32_t *a, uint32_t *b)
300 const unsigned short *rec;
303 if (hangul_pair_decompose(code, a, b))
306 rec = get_decomp_record(code);
309 if ((rec[0] & 0xff) != 0 || len == 0)
313 *a = decode_utf16(&rec);
315 *b = decode_utf16(&rec);
322 int ucdn_compose(uint32_t *code, uint32_t a, uint32_t b)
324 int l, r, index, indexi, offset;
326 if (hangul_pair_compose(code, a, b))
329 l = get_comp_index(a, nfc_first);
330 r = get_comp_index(b, nfc_last);
335 indexi = l * TOTAL_LAST + r;
336 index = comp_index0[indexi >> (COMP_SHIFT1+COMP_SHIFT2)] << COMP_SHIFT1;
337 offset = (indexi >> COMP_SHIFT2) & ((1<<COMP_SHIFT1) - 1);
338 index = comp_index1[index + offset] << COMP_SHIFT2;
339 offset = indexi & ((1<<COMP_SHIFT2) - 1);
340 *code = comp_data[index + offset];
345 int ucdn_compat_decompose(uint32_t code, uint32_t *decomposed)
348 const unsigned short *rec = get_decomp_record(code);
355 for (i = 0; i < len; i++)
356 decomposed[i] = decode_utf16(&rec);