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"
29 #include "gunicodeprivate.h"
32 #define CC_PART1(Page, Char) \
33 ((combining_class_table_part1[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
34 ? (combining_class_table_part1[Page] - G_UNICODE_MAX_TABLE_INDEX) \
35 : (cclass_data[combining_class_table_part1[Page]][Char]))
37 #define CC_PART2(Page, Char) \
38 ((combining_class_table_part2[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
39 ? (combining_class_table_part2[Page] - G_UNICODE_MAX_TABLE_INDEX) \
40 : (cclass_data[combining_class_table_part2[Page]][Char]))
42 #define COMBINING_CLASS(Char) \
43 (((Char) <= G_UNICODE_LAST_CHAR_PART1) \
44 ? CC_PART1 ((Char) >> 8, (Char) & 0xff) \
45 : (((Char) >= 0xe0000 && (Char) <= G_UNICODE_LAST_CHAR) \
46 ? CC_PART2 (((Char) - 0xe0000) >> 8, (Char) & 0xff) \
50 * g_unichar_combining_class:
51 * @uc: a Unicode character
53 * Determines the canonical combining class of a Unicode character.
55 * Return value: the combining class of the character
60 g_unichar_combining_class (gunichar uc)
62 return COMBINING_CLASS (uc);
65 /* constants for hangul syllable [de]composition */
73 #define NCount (VCount * TCount)
74 #define SCount (LCount * NCount)
77 * g_unicode_canonical_ordering:
78 * @string: a UCS-4 encoded string.
79 * @len: the maximum length of @string to use.
81 * Computes the canonical ordering of a string in-place.
82 * This rearranges decomposed characters in the string
83 * according to their combining classes. See the Unicode
84 * manual for more information.
87 g_unicode_canonical_ordering (gunichar *string,
97 last = COMBINING_CLASS (string[0]);
98 for (i = 0; i < len - 1; ++i)
100 int next = COMBINING_CLASS (string[i + 1]);
101 if (next != 0 && last > next)
104 /* Percolate item leftward through string. */
105 for (j = i + 1; j > 0; --j)
108 if (COMBINING_CLASS (string[j - 1]) <= next)
111 string[j] = string[j - 1];
115 /* We're re-entering the loop looking at the old
124 /* http://www.unicode.org/unicode/reports/tr15/#Hangul
125 * r should be null or have sufficient space. Calling with r == NULL will
126 * only calculate the result_len; however, a buffer with space for three
127 * characters will always be big enough. */
129 decompose_hangul (gunichar s,
133 gint SIndex = s - SBase;
135 /* not a hangul syllable */
136 if (SIndex < 0 || SIndex >= SCount)
144 gunichar L = LBase + SIndex / NCount;
145 gunichar V = VBase + (SIndex % NCount) / TCount;
146 gunichar T = TBase + SIndex % TCount;
165 /* returns a pointer to a null-terminated UTF-8 string */
167 find_decomposition (gunichar ch,
171 int end = G_N_ELEMENTS (decomp_table);
173 if (ch >= decomp_table[start].ch &&
174 ch <= decomp_table[end - 1].ch)
178 int half = (start + end) / 2;
179 if (ch == decomp_table[half].ch)
185 offset = decomp_table[half].compat_offset;
186 if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
187 offset = decomp_table[half].canon_offset;
191 offset = decomp_table[half].canon_offset;
192 if (offset == G_UNICODE_NOT_PRESENT_OFFSET)
196 return &(decomp_expansion_string[offset]);
198 else if (half == start)
200 else if (ch > decomp_table[half].ch)
211 * g_unicode_canonical_decomposition:
212 * @ch: a Unicode character.
213 * @result_len: location to store the length of the return value.
215 * Computes the canonical decomposition of a Unicode character.
217 * Return value: a newly allocated string of Unicode characters.
218 * @result_len is set to the resulting length of the string.
221 g_unicode_canonical_decomposition (gunichar ch,
228 /* Hangul syllable */
229 if (ch >= 0xac00 && ch <= 0xd7a3)
231 decompose_hangul (ch, NULL, result_len);
232 r = g_malloc (*result_len * sizeof (gunichar));
233 decompose_hangul (ch, r, result_len);
235 else if ((decomp = find_decomposition (ch, FALSE)) != NULL)
240 *result_len = g_utf8_strlen (decomp, -1);
241 r = g_malloc (*result_len * sizeof (gunichar));
243 for (p = decomp, i = 0; *p != '\0'; p = g_utf8_next_char (p), i++)
244 r[i] = g_utf8_get_char (p);
248 /* Not in our table. */
249 r = g_malloc (sizeof (gunichar));
254 /* Supposedly following the Unicode 2.1.9 table means that the
255 decompositions come out in canonical order. I haven't tested
256 this, but we rely on it here. */
260 /* L,V => LV and LV,T => LVT */
262 combine_hangul (gunichar a,
266 gint LIndex = a - LBase;
267 gint SIndex = a - SBase;
269 gint VIndex = b - VBase;
270 gint TIndex = b - TBase;
272 if (0 <= LIndex && LIndex < LCount
273 && 0 <= VIndex && VIndex < VCount)
275 *result = SBase + (LIndex * VCount + VIndex) * TCount;
278 else if (0 <= SIndex && SIndex < SCount && (SIndex % TCount) == 0
279 && 0 < TIndex && TIndex < TCount)
281 *result = a + TIndex;
288 #define CI(Page, Char) \
289 ((compose_table[Page] >= G_UNICODE_MAX_TABLE_INDEX) \
290 ? (compose_table[Page] - G_UNICODE_MAX_TABLE_INDEX) \
291 : (compose_data[compose_table[Page]][Char]))
293 #define COMPOSE_INDEX(Char) \
294 (((Char >> 8) > (COMPOSE_TABLE_LAST)) ? 0 : CI((Char) >> 8, (Char) & 0xff))
301 gushort index_a, index_b;
303 if (combine_hangul (a, b, result))
306 index_a = COMPOSE_INDEX(a);
308 if (index_a >= COMPOSE_FIRST_SINGLE_START && index_a < COMPOSE_SECOND_START)
310 if (b == compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][0])
312 *result = compose_first_single[index_a - COMPOSE_FIRST_SINGLE_START][1];
319 index_b = COMPOSE_INDEX(b);
321 if (index_b >= COMPOSE_SECOND_SINGLE_START)
323 if (a == compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][0])
325 *result = compose_second_single[index_b - COMPOSE_SECOND_SINGLE_START][1];
332 if (index_a >= COMPOSE_FIRST_START && index_a < COMPOSE_FIRST_SINGLE_START &&
333 index_b >= COMPOSE_SECOND_START && index_b < COMPOSE_SECOND_SINGLE_START)
335 gunichar res = compose_array[index_a - COMPOSE_FIRST_START][index_b - COMPOSE_SECOND_START];
348 _g_utf8_normalize_wc (const gchar *str,
356 gboolean do_compat = (mode == G_NORMALIZE_NFKC ||
357 mode == G_NORMALIZE_NFKD);
358 gboolean do_compose = (mode == G_NORMALIZE_NFC ||
359 mode == G_NORMALIZE_NFKC);
363 while ((max_len < 0 || p < str + max_len) && *p)
366 gunichar wc = g_utf8_get_char (p);
368 if (wc >= 0xac00 && wc <= 0xd7a3)
371 decompose_hangul (wc, NULL, &result_len);
376 decomp = find_decomposition (wc, do_compat);
379 n_wc += g_utf8_strlen (decomp, -1);
384 p = g_utf8_next_char (p);
387 wc_buffer = g_new (gunichar, n_wc + 1);
392 while ((max_len < 0 || p < str + max_len) && *p)
394 gunichar wc = g_utf8_get_char (p);
397 gsize old_n_wc = n_wc;
399 if (wc >= 0xac00 && wc <= 0xd7a3)
402 decompose_hangul (wc, wc_buffer + n_wc, &result_len);
407 decomp = find_decomposition (wc, do_compat);
412 for (pd = decomp; *pd != '\0'; pd = g_utf8_next_char (pd))
413 wc_buffer[n_wc++] = g_utf8_get_char (pd);
416 wc_buffer[n_wc++] = wc;
421 cc = COMBINING_CLASS (wc_buffer[old_n_wc]);
425 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
426 last_start = old_n_wc;
430 p = g_utf8_next_char (p);
435 g_unicode_canonical_ordering (wc_buffer + last_start, n_wc - last_start);
441 /* All decomposed and reordered */
443 if (do_compose && n_wc > 0)
449 for (i = 0; i < n_wc; i++)
451 int cc = COMBINING_CLASS (wc_buffer[i]);
454 (last_cc == 0 || last_cc < cc) &&
455 combine (wc_buffer[last_start], wc_buffer[i],
456 &wc_buffer[last_start]))
458 for (j = i + 1; j < n_wc; j++)
459 wc_buffer[j-1] = wc_buffer[j];
466 last_cc = COMBINING_CLASS (wc_buffer[i-1]);
485 * @str: a UTF-8 encoded string.
486 * @len: length of @str, in bytes, or -1 if @str is nul-terminated.
487 * @mode: the type of normalization to perform.
489 * Converts a string into canonical form, standardizing
490 * such issues as whether a character with an accent
491 * is represented as a base character and combining
492 * accent or as a single precomposed character. The
493 * string has to be valid UTF-8, otherwise %NULL is
494 * returned. You should generally call g_utf8_normalize()
495 * before comparing two Unicode strings.
497 * The normalization mode %G_NORMALIZE_DEFAULT only
498 * standardizes differences that do not affect the
499 * text content, such as the above-mentioned accent
500 * representation. %G_NORMALIZE_ALL also standardizes
501 * the "compatibility" characters in Unicode, such
502 * as SUPERSCRIPT THREE to the standard forms
503 * (in this case DIGIT THREE). Formatting information
504 * may be lost but for most text operations such
505 * characters should be considered the same.
507 * %G_NORMALIZE_DEFAULT_COMPOSE and %G_NORMALIZE_ALL_COMPOSE
508 * are like %G_NORMALIZE_DEFAULT and %G_NORMALIZE_ALL,
509 * but returned a result with composed forms rather
510 * than a maximally decomposed form. This is often
511 * useful if you intend to convert the string to
512 * a legacy encoding or pass it to a system with
513 * less capable Unicode handling.
515 * Return value: a newly allocated string, that is the
516 * normalized form of @str, or %NULL if @str is not
520 g_utf8_normalize (const gchar *str,
524 gunichar *result_wc = _g_utf8_normalize_wc (str, len, mode);
527 result = g_ucs4_to_utf8 (result_wc, -1, NULL, NULL, NULL);