Debian Codegraph @

lib/gtk/unicode/unicode-info.c

raw
1 /* unicode-info.c
2 *
3 * Originally a part of Gucharmap
4 *
5 * Copyright (C) 2017-2022 Jerry Casiano
6 *
7 *
8 * Copyright © 2004 Noah Levitt
9 *
10 * This program is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with this program.
22 *
23 * If not, see <http://www.gnu.org/licenses/gpl-3.0.txt>.
24 */
25
26 #include "unicode-info.h"
27
28 #include "unicode-names.h"
29 #include "unicode-blocks.h"
30 #include "unicode-nameslist.h"
31 #include "unicode-categories.h"
32 #include "unicode-versions.h"
33 #include "unicode-unihan.h"
34
35 /**
36 * SECTION: unicode-info
37 * @short_description: Unicode related utility functions
38 * @title: Unicode Information
39 * @include: unicode-info.h
40 *
41 * This widget provides a way to search for characters by name, value or
42 * other information.
43 */
44
45 /* constants for hangul (de)composition, see UAX #15 */
46 #define SBase 0xAC00
47 #define LCount 19
48 #define VCount 21
49 #define TCount 28
50 #define NCount (VCount * TCount)
51 #define SCount (LCount * NCount)
52
53 static const gchar JAMO_L_TABLE[][4] = {
54 "G", "GG", "N", "D", "DD", "R", "M", "B", "BB",
55 "S", "SS", "", "J", "JJ", "C", "K", "T", "P", "H"
56 };
57
58 static const gchar JAMO_V_TABLE[][4] = {
59 "A", "AE", "YA", "YAE", "EO", "E", "YEO", "YE",
60 "O", "WA", "WAE", "OE", "YO", "U", "WEO", "WE",
61 "WI", "YU", "EU", "YI", "I"
62 };
63
64 static const gchar JAMO_T_TABLE[][4] = {
65 "", "G", "GG", "GS", "N", "NJ", "NH", "D", "L", "LG",
66 "LM", "LB", "LS", "LT", "LP", "LH", "M", "B", "BS",
67 "S", "SS", "NG", "J", "C", "K", "T", "P", "H"
68 };
69
70 /* Compute hangul syllable name as per UAX #15 */
71 const gchar *
72 get_hangul_syllable_name (gunichar ch)
73 {
74 static gchar buf[32];
75 gint SIndex = ch - SBase;
76 gint LIndex, VIndex, TIndex;
77
78 if (SIndex < 0 || SIndex >= SCount)
79 return "";
80
81 LIndex = SIndex / NCount;
82 VIndex = (SIndex % NCount) / TCount;
83 TIndex = SIndex % TCount;
84
85 g_snprintf(buf, sizeof(buf),
86 "HANGUL SYLLABLE %s%s%s",
87 JAMO_L_TABLE[LIndex],
88 JAMO_V_TABLE[VIndex],
89 JAMO_T_TABLE[TIndex]);
90 return buf;
91 }
92
93 static const struct
94 {
95 guint32 start;
96 guint32 end;
97 }
98 CJK_Compat [] =
99 {
100 {0xf900, 0xfaff},
101 {0x2f800, 0x2fa1d}
102 };
103
104 static const struct
105 {
106 guint32 start;
107 guint32 end;
108 }
109 CJK_Unified [] =
110 {
111 {0x3400, 0x4db5},
112 {0x4e00, 0x9fea},
113 {0x20000, 0x2a6d6},
114 {0x2a700, 0x2b734},
115 {0x2b740, 0x2b81d},
116 {0x2b820, 0x2cea1},
117 {0x2ceb0, 0x2ebe0}
118 };
119
120 static gboolean
121 is_cjk_compat (gunichar ch)
122 {
123 for (guint i = 0; i < G_N_ELEMENTS(CJK_Compat); i++) {
124 if (ch >= CJK_Compat[i].start && ch <= CJK_Compat[i].end)
125 return TRUE;
126 continue;
127 }
128 return FALSE;
129 }
130
131 static gboolean
132 is_cjk_unified (gunichar ch)
133 {
134 for (guint i = 0; i < G_N_ELEMENTS(CJK_Unified); i++) {
135 if (ch >= CJK_Unified[i].start && ch <= CJK_Unified[i].end)
136 return TRUE;
137 continue;
138 }
139 return FALSE;
140 }
141
142
143 const gchar *
144 font_manager_unicode_get_codepoint_data_name (gunichar uc)
145 {
146 /* does a binary search on unicode_names */
147 gint min = 0, mid, max = G_N_ELEMENTS(unicode_names) - 1;
148
149 if (uc < unicode_names[0].index || uc > unicode_names[max].index)
150 return "";
151
152 while (max >= min) {
153 mid = (min + max) / 2;
154 if (uc > unicode_names[mid].index)
155 min = mid + 1;
156 else if (uc < unicode_names[mid].index)
157 max = mid - 1;
158 else
159 return font_manager_unicode_name_get_name(&unicode_names[mid]);
160 }
161
162 return NULL;
163 }
164
165 /**
166 * get_unicode_name:
167 * @ch: a #gunichar
168 *
169 * Returns: (transfer none) (nullable): unicode character name or %NULL
170 */
171 const gchar *
172 font_manager_unicode_get_codepoint_name (gunichar ch)
173 {
174 static gchar buf[32];
175
176 if G_UNLIKELY(is_cjk_unified(ch)) {
177 g_snprintf (buf, sizeof(buf), "CJK UNIFIED IDEOGRAPH-%04X", ch);
178 return buf;
179 } else if G_UNLIKELY(is_cjk_compat(ch)) {
180 g_snprintf (buf, sizeof(buf), "CJK COMPATIBILITY IDEOGRAPH-%04X", ch);
181 return buf;
182 } else if G_UNLIKELY(ch >= 0x17000 && ch <= 0x187ec) {
183 g_snprintf (buf, sizeof(buf), "TANGUT IDEOGRAPH-%05X", ch);
184 return buf;
185 } else if G_UNLIKELY(ch >= 0x18800 && ch <= 0x18af2) {
186 g_snprintf (buf, sizeof(buf), "TANGUT COMPONENT-%03u", ch - 0x18800 + 1);
187 return buf;
188 } else if G_UNLIKELY(ch >= 0xac00 && ch <= 0xd7af) {
189 return get_hangul_syllable_name(ch);
190 } else if G_UNLIKELY(ch >= 0xD800 && ch <= 0xDB7F)
191 return _("<Non Private Use High Surrogate>");
192 else if G_UNLIKELY(ch >= 0xDB80 && ch <= 0xDBFF)
193 return _("<Private Use High Surrogate>");
194 else if G_UNLIKELY(ch >= 0xDC00 && ch <= 0xDFFF)
195 return _("<Low Surrogate>");
196 else if G_UNLIKELY(ch >= 0xE000 && ch <= 0xF8FF)
197 return _("<Private Use>");
198 else if G_UNLIKELY(ch >= 0xF0000 && ch <= 0xFFFFD)
199 return _("<Plane 15 Private Use>");
200 else if G_UNLIKELY(ch >= 0x100000 && ch <= 0x10FFFD)
201 return _("<Plane 16 Private Use>");
202 else {
203 const gchar *x = font_manager_unicode_get_codepoint_data_name(ch);
204 if (x == NULL)
205 return _("<not assigned>");
206 else
207 return x;
208 }
209 }
210
211 /**
212 * get_unicode_category_name:
213 * @ch: a #gunichar
214 *
215 * Returns: (transfer none) (nullable): unicode category name or %NULL
216 */
217 const gchar *
218 font_manager_unicode_get_category_name (gunichar ch)
219 {
220 switch (g_unichar_type(ch)) {
221 case G_UNICODE_CONTROL:
222 return _("Other, Control");
223 case G_UNICODE_FORMAT:
224 return _("Other, Format");
225 case G_UNICODE_UNASSIGNED:
226 return _("Other, Not Assigned");
227 case G_UNICODE_PRIVATE_USE:
228 return _("Other, Private Use");
229 case G_UNICODE_SURROGATE:
230 return _("Other, Surrogate");
231 case G_UNICODE_LOWERCASE_LETTER:
232 return _("Letter, Lowercase");
233 case G_UNICODE_MODIFIER_LETTER:
234 return _("Letter, Modifier");
235 case G_UNICODE_OTHER_LETTER:
236 return _("Letter, Other");
237 case G_UNICODE_TITLECASE_LETTER:
238 return _("Letter, Titlecase");
239 case G_UNICODE_UPPERCASE_LETTER:
240 return _("Letter, Uppercase");
241 case G_UNICODE_SPACING_MARK:
242 return _("Mark, Spacing Combining");
243 case G_UNICODE_ENCLOSING_MARK:
244 return _("Mark, Enclosing");
245 case G_UNICODE_NON_SPACING_MARK:
246 return _("Mark, Non-Spacing");
247 case G_UNICODE_DECIMAL_NUMBER:
248 return _("Number, Decimal Digit");
249 case G_UNICODE_LETTER_NUMBER:
250 return _("Number, Letter");
251 case G_UNICODE_OTHER_NUMBER:
252 return _("Number, Other");
253 case G_UNICODE_CONNECT_PUNCTUATION:
254 return _("Punctuation, Connector");
255 case G_UNICODE_DASH_PUNCTUATION:
256 return _("Punctuation, Dash");
257 case G_UNICODE_CLOSE_PUNCTUATION:
258 return _("Punctuation, Close");
259 case G_UNICODE_FINAL_PUNCTUATION:
260 return _("Punctuation, Final Quote");
261 case G_UNICODE_INITIAL_PUNCTUATION:
262 return _("Punctuation, Initial Quote");
263 case G_UNICODE_OTHER_PUNCTUATION:
264 return _("Punctuation, Other");
265 case G_UNICODE_OPEN_PUNCTUATION:
266 return _("Punctuation, Open");
267 case G_UNICODE_CURRENCY_SYMBOL:
268 return _("Symbol, Currency");
269 case G_UNICODE_MODIFIER_SYMBOL:
270 return _("Symbol, Modifier");
271 case G_UNICODE_MATH_SYMBOL:
272 return _("Symbol, Math");
273 case G_UNICODE_OTHER_SYMBOL:
274 return _("Symbol, Other");
275 case G_UNICODE_LINE_SEPARATOR:
276 return _("Separator, Line");
277 case G_UNICODE_PARAGRAPH_SEPARATOR:
278 return _("Separator, Paragraph");
279 case G_UNICODE_SPACE_SEPARATOR:
280 return _("Separator, Space");
281 default:
282 return NULL;
283 }
284 }
285
286
287
288 gint
289 font_manager_unicode_get_codepoint_data_name_count (void)
290 {
291 return G_N_ELEMENTS(unicode_names);
292 }
293
294
295 FontManagerUnicodeStandard
296 font_manager_unicode_get_version (gunichar uc)
297 {
298 /* does a binary search on unicode_versions */
299 gint min = 0, mid, max = G_N_ELEMENTS (unicode_versions) - 1;
300
301 if (uc < unicode_versions[0].start || uc > unicode_versions[max].end)
302 return FONT_MANAGER_UNICODE_VERSION_UNASSIGNED;
303
304 while (max >= min)
305 {
306 mid = (min + max) / 2;
307
308 if (uc > unicode_versions[mid].end)
309 min = mid + 1;
310 else if (uc < unicode_versions[mid].start)
311 max = mid - 1;
312 else if ((uc >= unicode_versions[mid].start) && (uc <= unicode_versions[mid].end))
313 return unicode_versions[mid].version;
314 }
315
316 return FONT_MANAGER_UNICODE_VERSION_UNASSIGNED;
317 }
318
319 const gchar *
320 font_manager_unicode_version_to_string (FontManagerUnicodeStandard version)
321 {
322 g_return_val_if_fail(version >= FONT_MANAGER_UNICODE_VERSION_UNASSIGNED, NULL);
323 g_return_val_if_fail(version <= FONT_MANAGER_UNICODE_VERSION_LATEST, NULL);
324
325 if G_UNLIKELY(version == FONT_MANAGER_UNICODE_VERSION_UNASSIGNED)
326 return NULL;
327
328 return unicode_version_strings + unicode_version_string_offsets[version - 1];
329 }
330
331 gint
332 font_manager_unicode_get_unihan_count (void)
333 {
334 #if INCLUDE_UNIHAN_DATA
335 return G_N_ELEMENTS(unihan);
336 #else
337 return 0;
338 #endif
339 }
340
341 /* does a binary search; also caches most recent, since it will often be
342 * called in succession on the same character */
343 static const FontManagerNamesList *
344 get_nameslist (gunichar uc)
345 {
346 static gunichar most_recent_searched;
347 static const FontManagerNamesList *most_recent_result;
348 gint min = 0;
349 gint mid;
350 gint max = G_N_ELEMENTS (names_list) - 1;
351
352 if (uc < names_list[0].index || uc > names_list[max].index)
353 return NULL;
354
355 if (uc == most_recent_searched)
356 return most_recent_result;
357
358 most_recent_searched = uc;
359
360 while (max >= min)
361 {
362 mid = (min + max) / 2;
363 if (uc > names_list[mid].index)
364 min = mid + 1;
365 else if (uc < names_list[mid].index)
366 max = mid - 1;
367 else
368 {
369 most_recent_result = names_list + mid;
370 return names_list + mid;
371 }
372 }
373
374 most_recent_result = NULL;
375 return NULL;
376 }
377
378 /* returns newly allocated array of gunichar terminated with -1 */
379 gunichar *
380 font_manager_unicode_get_nameslist_exes (gunichar uc)
381 {
382 const FontManagerNamesList *nl;
383 gunichar *exes;
384 gunichar i, count;
385
386 nl = get_nameslist (uc);
387
388 if (nl == NULL || nl->exes_index == -1)
389 return NULL;
390
391 /* count the number of exes */
392 for (i = 0; names_list_exes[nl->exes_index + i].index == uc; i++);
393 count = i;
394
395 exes = g_malloc ((count + 1) * sizeof(gunichar));
396 for (i = 0; i < count; i++)
397 exes[i] = names_list_exes[nl->exes_index + i].value;
398 exes[count] = (gunichar)(-1);
399
400 return exes;
401 }
402
403 /**
404 * font_manager_unicode_get_nameslist_equals:
405 * @uc: a gunichar
406 *
407 * Returns: (transfer container) (array zero-terminated=1) (element-type utf8):
408 * newly allocated %NULL-terminated array of gchar*, the items are const,
409 * but the array should be freed by the caller.
410 */
411 const gchar **
412 font_manager_unicode_get_nameslist_equals (gunichar uc)
413 {
414 const FontManagerNamesList *nl;
415 const gchar **equals;
416 gunichar i, count;
417
418 nl = get_nameslist (uc);
419
420 if (nl == NULL || nl->equals_index == -1)
421 return NULL;
422
423 /* count the number of equals */
424 for (i = 0; names_list_equals[nl->equals_index + i].index == uc; i++);
425 count = i;
426
427 equals = g_malloc ((count + 1) * sizeof(gchar *));
428 for (i = 0; i < count; i++)
429 equals[i] = names_list_equals_strings + names_list_equals[nl->equals_index + i].string_index;
430 equals[count] = NULL;
431
432 return equals;
433 }
434
435 /**
436 * font_manager_unicode_get_nameslist_stars:
437 * @uc: a #gunichar
438 *
439 * Returns: (transfer container) (array zero-terminated=1) (element-type utf8):
440 * newly allocated %NULL-terminated array of gchar*, the items are const,
441 * but the array should be freed by the caller.
442 */
443 const gchar **
444 font_manager_unicode_get_nameslist_stars (gunichar uc)
445 {
446 const FontManagerNamesList *nl;
447 const gchar **stars;
448 gunichar i, count;
449
450 nl = get_nameslist (uc);
451
452 if (nl == NULL || nl->stars_index == -1)
453 return NULL;
454
455 /* count the number of stars */
456 for (i = 0; names_list_stars[nl->stars_index + i].index == uc; i++);
457 count = i;
458
459 stars = g_malloc ((count + 1) * sizeof(gchar *));
460 for (i = 0; i < count; i++)
461 stars[i] = names_list_stars_strings + names_list_stars[nl->stars_index + i].string_index;
462 stars[count] = NULL;
463
464 return stars;
465 }
466
467 /**
468 * font_manager_unicode_get_nameslist_pounds:
469 * @uc: a #gunichar
470 *
471 * Returns: (transfer container) (array zero-terminated=1) (element-type utf8):
472 * newly allocated %NULL-terminated array of gchar*, the items are const,
473 * but the array should be freed by the caller.
474 */
475 const gchar **
476 font_manager_unicode_get_nameslist_pounds (gunichar uc)
477 {
478 const FontManagerNamesList *nl;
479 const gchar **pounds;
480 gunichar i, count;
481
482 nl = get_nameslist (uc);
483
484 if (nl == NULL || nl->pounds_index == -1)
485 return NULL;
486
487 /* count the number of pounds */
488 for (i = 0; names_list_pounds[nl->pounds_index + i].index == uc; i++);
489 count = i;
490
491 pounds = g_malloc ((count + 1) * sizeof(gchar *));
492 for (i = 0; i < count; i++)
493 pounds[i] = names_list_pounds_strings + names_list_pounds[nl->pounds_index + i].string_index;
494 pounds[count] = NULL;
495
496 return pounds;
497 }
498
499 /**
500 * font_manager_unicode_get_nameslist_colons:
501 * @uc: a #gunichar
502 *
503 * Returns: (transfer container) (array zero-terminated=1) (element-type utf8):
504 * newly allocated %NULL-terminated array of gchar*, the items are const,
505 * but the array should be freed by the caller.
506 */
507 const gchar **
508 font_manager_unicode_get_nameslist_colons (gunichar uc)
509 {
510 const FontManagerNamesList *nl;
511 const gchar **colons;
512 gunichar i, count;
513
514 nl = get_nameslist (uc);
515
516 if (nl == NULL || nl->colons_index == -1)
517 return NULL;
518
519 /* count the number of colons */
520 for (i = 0; names_list_colons[nl->colons_index + i].index == uc; i++);
521 count = i;
522
523 colons = g_malloc ((count + 1) * sizeof(gchar *));
524 for (i = 0; i < count; i++)
525 colons[i] = names_list_colons_strings + names_list_colons[nl->colons_index + i].string_index;
526 colons[count] = NULL;
527
528 return colons;
529 }
530
531 /* Wrapper, in case we want to support a newer unicode version than glib */
532 gboolean
533 font_manager_unicode_unichar_validate (gunichar ch)
534 {
535 return g_unichar_validate (ch);
536 }
537
538 /**
539 * font_manager_unicode_unichar_to_printable_utf8:
540 * @uc: a unicode character
541 * @outbuf: (out caller-allocates): output buffer, must have at least 10 bytes of space.
542 * If %NULL, the length will be computed and returned, nothing will be written to @outbuf.
543 *
544 * Converts a single character to UTF-8 suitable for rendering. Check the
545 * source to see what this means. ;-)
546 *
547 * Returns: number of bytes written
548 **/
549 gint
550 font_manager_unicode_unichar_to_printable_utf8 (gunichar uc, gchar *outbuf)
551 {
552 if (! font_manager_unicode_unichar_validate(uc)
553 || (! font_manager_unicode_unichar_isgraph(uc)
554 && g_unichar_type(uc) != G_UNICODE_PRIVATE_USE))
555 return 0;
556 else
557 return g_unichar_to_utf8 (uc, outbuf);
558 }
559
560 /**
561 * font_manager_unicode_unichar_isgraph:
562 * @uc: a Unicode character
563 *
564 * Determines whether a character is printable and not a space
565 * (returns %FALSE for control characters and spaces).
566 * g_unichar_isprint() is similar, but returns %TRUE for
567 * spaces. Given some UTF-8 text, obtain a character value with
568 * g_utf8_get_char().
569 *
570 * Returns: %TRUE if @c is printable unless it's a space or control character
571 **/
572 gboolean
573 font_manager_unicode_unichar_isgraph (gunichar uc)
574 {
575 GUnicodeType t = g_unichar_type (uc);
576
577 /* From http://www.unicode.org/versions/Unicode9.0.0/ch09.pdf, p16
578 * "Unlike most other format control characters, however, they should be
579 * rendered with a visible glyph, even in circumstances where no suitable
580 * digit or sequence of digits follows them in logical order."
581 * There the standard talks about the ar signs spanning numbers, but
582 * I think this should apply to all Prepended_Concatenation_Mark format
583 * characters.
584 * Instead of parsing the corresponding data file, just hardcode the
585 * (few!) existing characters here.
586 */
587 if (t == G_UNICODE_FORMAT)
588 return (uc >= 0x0600 && uc <= 0x0605) ||
589 uc == 0x06DD || uc == 0x070F || uc == 0x08E2 || uc == 0x110BD;
590
591 return (t != G_UNICODE_CONTROL && t != G_UNICODE_SPACE_SEPARATOR);
592 }