1 /* decomp.c - Character decomposition.
3 * Copyright (C) 1999, 2000 Tom Tromey
4 * Copyright 2000 Red Hat, Inc.
6 * The Gnome Library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * The Gnome Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with the Gnome Library; see the file COPYING.LIB. If not,
18 * write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
27 #include "gunidecomp.h"
30 #include "gunicodeprivate.h"
33 #define CC_PART1(Page, Char) \
34 ((combining_class_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
35 ? (combining_class_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
36 : (cclass_data[combining_class_table_part1[Page]][Char]))
38 #define CC_PART2(Page, Char) \
39 ((combining_class_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
40 ? (combining_class_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
41 : (cclass_data[combining_class_table_part2[Page]][Char]))
43 #define COMBINING_CLASS(Char) \
44 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
45 ? CC_PART1 ((Char) >> 8, (Char) & 0xff) \
46 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
47 ? CC_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
51 * g_unichar_combining_class:
52 * @uc: a Unicode character
54 * Determines the canonical combining class of a Unicode character.
56 * Return value: the combining class of the character
61 g_unichar_combining_class (gunichar uc)
63 return COMBINING_CLASS (uc);
66 /* constants for hangul syllable [de]composition */
74 #define NCount (VCount * TCount)
75 #define SCount (LCount * NCount)
78 * g_unicode_canonical_ordering:
79 * @string: a UCS-4 encoded string.
80 * @len: the maximum length of @string to use.
82 * Computes the canonical ordering of a string in-place.
83 * This rearranges decomposed characters in the string
84 * according to their combining classes. See the Unicode
85 * manual for more information.
88 g_unicode_canonical_ordering (gunichar *string,
98 last = COMBINING_CLASS (string[0]);
99 for (i = 0; i < len - 1; ++i)
101 int next = COMBINING_CLASS (string[i + 1]);
102 if (next != 0 && last > next)
105 /* Percolate item leftward through string. */
106 for (j = i + 1; j > 0; --j)
109 if (COMBINING_CLASS (string[j - 1]) <= next)
112 string[j] = string[j - 1];
116 /* We're re-entering the loop looking at the old
125 /* http://www.unicode.org/unicode/reports/tr15/#Hangul
126 * r should be null or have sufficient space. Calling with r == NULL will
127 * only calculate the result_len; however, a buffer with space for three
128 * characters will always be big enough. */
130 decompose_hangul (gunichar s,
134 gint SIndex = s - SBase;
136 /* not a hangul syllable */
137 if (SIndex < 0 || SIndex >= SCount)
145 gunichar L = LBase + SIndex / NCount;
146 gunichar V = VBase + (SIndex % NCount) / TCount;
147 gunichar T = TBase + SIndex % TCount;
166 /* returns a pointer to a null-terminated UTF-8 string */
168 find_decomposition (gunichar ch,
172 int end = G_N_ELEMENTS (decomp_table);
174 if (ch >= decomp_table[start].ch &&
175 ch <= decomp_table[end - 1].ch)
179 int half = (start + end) / 2;
180 if (ch == decomp_table[half].ch)
186 offset = decomp_table[half].compat_offset;
187 if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
188 offset = decomp_table[half].canon_offset;
192 offset = decomp_table[half].canon_offset;
193 if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
197 return &(decomp_expansion_string[offset]);
199 else if (half == start)
201 else if (ch > decomp_table[half].ch)
212 * g_unicode_canonical_decomposition:
213 * @ch: a Unicode character.
214 * @result_len: location to store the length of the return value.
216 * Computes the canonical decomposition of a Unicode character.
218 * Return value: a newly allocated string of Unicode characters.
219 * @result_len is set to the resulting length of the string.
222 g_unicode_canonical_decomposition (gunichar ch,
229 /* Hangul syllable */
230 if (ch >= 0xac00 && ch <= 0xd7a3)
232 decompose_hangul (ch, NULL, result_len);
233 r = g_malloc (*result_len * sizeof (gunichar));
234 decompose_hangul (ch, r, result_len);
236 else if ((decomp = find_decomposition (ch, FALSE)) != NULL)
241 *result_len = g_utf8_strlen (decomp, -1);
242 r = g_malloc (*result_len * sizeof (gunichar));
244 for (p = decomp, i = 0; *p != '\0'; p = g_utf8_next_char (p), i++)
245 r[i] = g_utf8_get_char (p);
249 /* Not in our table. */
250 r = g_malloc (sizeof (gunichar));
258 /* L,V => LV and LV,T => LVT */
260 combine_hangul (gunichar a,
264 gint LIndex = a - LBase;
265 gint SIndex = a - SBase;
267 gint VIndex = b - VBase;
268 gint TIndex = b - TBase;
270 if (0 <= LIndex && LIndex < LCount
271 && 0 <= VIndex && VIndex < VCount)
273 *result = SBase + (LIndex * VCount + VIndex) * TCount;
276 else if (0 <= SIndex && SIndex < SCount && (SIndex % TCount) == 0
277 && 0 < TIndex && TIndex < TCount)
279 *result = a + TIndex;
286 #define CI(Page, Char) \
287 ((compose_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
288 ? (compose_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
289 : (compose_data[compose_table[Page]][Char]))
291 #define COMPOSE_INDEX(Char) \
292 (((Char >> 8) > (COMPOSE_TABLE_LAST)) ? 0 : CI((Char) >> 8, (Char) & 0xff))
299 gushort index_a, index_b;
301 if (combine_hangul (a, b, result))
304 index_a = COMPOSE_INDEX(a);
306 if (index_a >= COMPOSE_FIRST_SINGLE_START && index_a < COMPOSE_SECOND_START)
308 if (b == compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][0])
310 *result = compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][1];
317 index_b = COMPOSE_INDEX(b);
319 if (index_b >= COMPOSE_SECOND_SINGLE_START)
321 if (a == compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][0])
323 *result = compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][1];
330 if (index_a >= COMPOSE_FIRST_START && index_a < COMPOSE_FIRST_SINGLE_START &&
331 index_b >= COMPOSE_SECOND_START && index_b < COMPOSE_SECOND_SINGLE_START)
333 gunichar res = compose_array[index_a - COMPOSE_FIRST_START][index_b - COMPOSE_SECOND_START];
346 _g_utf8_normalize_wc (const gchar *str,
354 gboolean do_compat = (mode == G_NORMALIZE_NFKC ||
355 mode == G_NORMALIZE_NFKD);
356 gboolean do_compose = (mode == G_NORMALIZE_NFC ||
357 mode == G_NORMALIZE_NFKC);
361 while ((max_len < 0 || p < str + max_len) && *p)
364 gunichar wc = g_utf8_get_char (p);
366 if (wc >= 0xac00 && wc <= 0xd7a3)
369 decompose_hangul (wc, NULL, &result_len);
374 decomp = find_decomposition (wc, do_compat);
377 n_wc += g_utf8_strlen (decomp, -1);
382 p = g_utf8_next_char (p);
385 wc_buffer = g_new (gunichar, n_wc + 1);
390 while ((max_len < 0 || p < str + max_len) && *p)
392 gunichar wc = g_utf8_get_char (p);
395 gsize old_n_wc = n_wc;
397 if (wc >= 0xac00 && wc <= 0xd7a3)
400 decompose_hangul (wc, wc_buffer + n_wc, &result_len);
405 decomp = find_decomposition (wc, do_compat);
410 for (pd = decomp; *pd != '\0'; pd = g_utf8_next_char (pd))
411 wc_buffer[n_wc++] = g_utf8_get_char (pd);
414 wc_buffer[n_wc++] = wc;
419 cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
423 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
424 last_start = old_n_wc;
428 p = g_utf8_next_char (p);
433 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
439 /* All decomposed and reordered */
441 if (do_compose && n_wc > 0)
447 for (i = 0; i < n_wc; i++)
449 int cc = COMBINING_CLASS (wc_buffer[i]);
452 (last_cc == 0 || last_cc < cc) &&
453 combine (wc_buffer[last_start], wc_buffer[i],
454 &wc_buffer[last_start]))
456 for (j = i + 1; j < n_wc; j++)
457 wc_buffer[j-1] = wc_buffer[j];
464 last_cc = COMBINING_CLASS (wc_buffer[i-1]);
483 * @str: a UTF-8 encoded string.
484 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
485 * @mode: the type of normalization to perform.
487 * Converts a string into canonical form, standardizing
488 * such issues as whether a character with an accent
489 * is represented as a base character and combining
490 * accent or as a single precomposed character. The
491 * string has to be valid UTF-8, otherwise %NULL is
492 * returned. You should generally call g_utf8_normalize()
493 * before comparing two Unicode strings.
495 * The normalization mode %G_NORMALIZE_DEFAULT only
496 * standardizes differences that do not affect the
497 * text content, such as the above-mentioned accent
498 * representation. %G_NORMALIZE_ALL also standardizes
499 * the "compatibility" characters in Unicode, such
500 * as SUPERSCRIPT THREE to the standard forms
501 * (in this case DIGIT THREE). Formatting information
502 * may be lost but for most text operations such
503 * characters should be considered the same.
505 * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
506 * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
507 * but returned a result with composed forms rather
508 * than a maximally decomposed form. This is often
509 * useful if you intend to convert the string to
510 * a legacy encoding or pass it to a system with
511 * less capable Unicode handling.
513 * Return value: a newly allocated string, that is the
514 * normalized form of @str, or %NULL if @str is not
518 g_utf8_normalize (const gchar *str,
522 gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
525 result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);
532 decompose_hangul_step (gunichar ch,
541 if (SIndex < 0 || SIndex >= SCount)
542 return FALSE; /* not a hangul syllable */
544 L = LBase + SIndex / NCount;
545 V = VBase + (SIndex % NCount) / TCount;
546 T = TBase + SIndex % TCount;
553 /* split LVT -> LV,T */
556 LV = SBase + (LIndex * VCount + VIndex) * TCount;
563 /* split LV -> L,V */
572 compose_hangul_step (gunichar a,
578 /* first try L,V -> LV */
580 if (0 <= LIndex && LIndex < LCount)
585 if (0 <= VIndex && VIndex < VCount)
587 *ch = SBase + (LIndex * VCount + VIndex) * TCount;
592 /* next try LV,T -> LVT */
594 if (0 <= SIndex && SIndex < SCount && (SIndex % TCount) == 0)
599 if (0 < TIndex && TIndex < TCount)
610 * g_unichar_decompose:
611 * @ch: a Unicode character
612 * @a: return location for the first component of @ch
613 * @b: return location for the second component of @ch
615 * Performs a single decomposition step of the
616 * Unicode canonical decomposition algorithm.
618 * This function does not include compatibility
619 * decompositions. It does, however, include algorithmic
620 * Hangul Jamo decomposition, as well as 'singleton'
621 * decompositions which replace a character by a single
622 * other character. In the case of singletons *@b will
625 * If @ch is not decomposable, *@a is set to @ch and *@b
628 * Note that the way Unicode decomposition pairs are
629 * defined, it is guaranteed that @b would not decompose
630 * further, but @a may itself decompose. To get the full
631 * canonical decomposition for @ch, one would need to
632 * recursively call this function on @a. Or use
633 * g_unicode_canonical_decomposition().
635 * See <ulink url="http://unicode.org/reports/tr15/">UAX#15</ulink>
638 * Returns: %TRUE if the character could be decomposed
643 g_unichar_decompose (gunichar ch,
648 gint end = G_N_ELEMENTS (decomp_step_table);
650 if (decompose_hangul_step (ch, a, b))
653 if (ch >= decomp_step_table[start].ch &&
654 ch <= decomp_step_table[end - 1].ch)
658 gint half = (start + end) / 2;
659 const decomposition_step *p = &(decomp_step_table[half]);
666 else if (half == start)
683 * @a: a Unicode character
684 * @b: a Unicode character
685 * @ch: return location for the composed character
687 * Performs a single composition step of the
688 * Unicode canonical composition algorithm.
690 * This function does not perform algorithmic composition
691 * for Hangul characters, and does not include compatibility
692 * compositions. It does, however, include 'singleton'
693 * compositions which replace a character by a single
694 * other character. To obtain these, pass zero for @b.
696 * This function includes algorithmic Hangul Jamo composition.
698 * If @a and @b do not compose a new character, @ch is set to zero.
700 * See <ulink url="http://unicode.org/reports/tr15/">UAX#15</ulink>
703 * Returns: %TRUE if the characters could be composed
708 g_unichar_compose (gunichar a,
713 gint end = G_N_ELEMENTS (comp_step_table);
715 if (compose_hangul_step (a, b, ch))
718 if (a >= comp_step_table[start].a &&
719 a <= comp_step_table[end - 1].a)
723 gint half = (start + end) / 2;
724 const decomposition_step *p = &(comp_step_table[half]);
725 if (a == p->a && b == p->b)
730 else if (half == start)
732 else if (a > p->a || (a == p->a && b > p->b))