File indexing completed on 2024-05-05 05:53:43

0001 /*
0002     SPDX-FileCopyrightText: 2022 Luis Javier Merino MorĂ¡n <ninjalj@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #ifndef HANGUL_H
0008 #define HANGUL_H
0009 
0010 #include <QtGlobal>
0011 
0012 namespace Konsole
0013 {
0014 
0015 class Character;
0016 
0017 class Hangul
0018 {
0019 public:
0020     // See HangulSyllableType.txt from the Unicode data distribution
0021     enum SyllableType {
0022         Not_Applicable,
0023         Leading_Jamo,
0024         Vowel_Jamo,
0025         Trailing_Jamo,
0026 
0027         LV_Syllable,
0028         LVT_Syllable,
0029     };
0030 
0031     enum SyllablePos {
0032         NotInSyllable,
0033         AtLeadingJamo,
0034         AtVowelJamo,
0035         AtTrailingJamo,
0036     };
0037 
0038     static int width(uint c, int widthFromTable, enum SyllablePos &syllablePos);
0039     static bool combinesWith(Character prev, uint c);
0040 
0041     static bool isHangul(const uint c)
0042     {
0043         return (c >= 0x1100 && c <= 0x11ff) || (c >= 0xa960 && c <= 0xa97f) || (c >= 0xd7b0 && c <= 0xd7ff) || (c >= 0xac00 && c <= 0xd7a3);
0044     }
0045 
0046 private:
0047     static bool isLeadingJamo(const uint c)
0048     {
0049         return (c >= 0x1100 && c <= 0x115f) || (c >= 0xa960 && c <= 0xa97f);
0050     }
0051 
0052     static bool isVowelJamo(const uint c)
0053     {
0054         return (c >= 0x1160 && c <= 0x11a7) || (c >= 0xd7b0 && c <= 0xd7c6);
0055     }
0056 
0057     static bool isTrailingJamo(const uint c)
0058     {
0059         return (c >= 0x11a8 && c <= 0x11ff) || (c >= 0xd7cb && c <= 0xd7ff);
0060     }
0061 
0062     static bool isLvSyllable(const uint c)
0063     {
0064         return (c >= 0xac00 && c <= 0xd7a3) && (c % 0x1c == 0);
0065     }
0066 
0067     static bool isLvtSyllable(const uint c)
0068     {
0069         return (c >= 0xac00 && c <= 0xd7a3) && (c % 0x1c != 0);
0070     }
0071 
0072     static SyllableType jamoType(const uint c)
0073     {
0074         // clang-format off
0075         if (isLeadingJamo(c))  return Leading_Jamo;
0076         if (isVowelJamo(c))    return Vowel_Jamo;
0077         if (isTrailingJamo(c)) return Trailing_Jamo;
0078         if (isLvSyllable(c))   return LV_Syllable;
0079         if (isLvtSyllable(c))  return LVT_Syllable;
0080         return Not_Applicable;
0081         // clang-format on
0082     }
0083 
0084     static void updateHangulSyllablePos(Hangul::SyllablePos &syllablePos, uint c);
0085     static bool validSyllableContinuation(Hangul::SyllablePos syllablePos, uint c);
0086 };
0087 
0088 }
0089 
0090 #endif // HANGUL_H