2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
11 #include "settings/lib/ISettingCallback.h"
12 #include "utils/GlobalsHandling.h"
19 struct StringSettingOption
;
21 class CCharsetConverter
: public ISettingCallback
26 void OnSettingChanged(const std::shared_ptr
<const CSetting
>& setting
) override
;
29 static void resetSystemCharset();
30 static void reinitCharsetsFromSettings(void);
35 * Convert UTF-8 string to UTF-32 string.
36 * No RTL logical-visual transformation is performed.
37 * @param utf8StringSrc is source UTF-8 string to convert
38 * @param utf32StringDst is output UTF-32 string, empty on any error
39 * @param failOnBadChar if set to true function will fail on invalid character,
40 * otherwise invalid character will be skipped
41 * @return true on successful conversion, false on any error
43 static bool utf8ToUtf32(const std::string
& utf8StringSrc
, std::u32string
& utf32StringDst
, bool failOnBadChar
= true);
45 * Convert UTF-8 string to UTF-32 string.
46 * No RTL logical-visual transformation is performed.
47 * @param utf8StringSrc is source UTF-8 string to convert
48 * @param failOnBadChar if set to true function will fail on invalid character,
49 * otherwise invalid character will be skipped
50 * @return converted string on successful conversion, empty string on any error
52 static std::u32string
utf8ToUtf32(const std::string
& utf8StringSrc
, bool failOnBadChar
= true);
54 * Convert UTF-8 string to UTF-32 string.
55 * RTL logical-visual transformation is optionally performed.
56 * Use it for readable text, GUI strings etc.
57 * @param utf8StringSrc is source UTF-8 string to convert
58 * @param utf32StringDst is output UTF-32 string, empty on any error
59 * @param bVisualBiDiFlip allow RTL visual-logical transformation if set to true, must be set
60 * to false is logical-visual transformation is already done
61 * @param forceLTRReadingOrder force LTR reading order
62 * @param failOnBadChar if set to true function will fail on invalid character,
63 * otherwise invalid character will be skipped
64 * @return true on successful conversion, false on any error
66 static bool utf8ToUtf32Visual(const std::string
& utf8StringSrc
, std::u32string
& utf32StringDst
, bool bVisualBiDiFlip
= false, bool forceLTRReadingOrder
= false, bool failOnBadChar
= false);
68 * Convert UTF-32 string to UTF-8 string.
69 * No RTL visual-logical transformation is performed.
70 * @param utf32StringSrc is source UTF-32 string to convert
71 * @param utf8StringDst is output UTF-8 string, empty on any error
72 * @param failOnBadChar if set to true function will fail on invalid character,
73 * otherwise invalid character will be skipped
74 * @return true on successful conversion, false on any error
76 static bool utf32ToUtf8(const std::u32string
& utf32StringSrc
, std::string
& utf8StringDst
, bool failOnBadChar
= false);
78 * Convert UTF-32 string to UTF-8 string.
79 * No RTL visual-logical transformation is performed.
80 * @param utf32StringSrc is source UTF-32 string to convert
81 * @param failOnBadChar if set to true function will fail on invalid character,
82 * otherwise invalid character will be skipped
83 * @return converted string on successful conversion, empty string on any error
85 static std::string
utf32ToUtf8(const std::u32string
& utf32StringSrc
, bool failOnBadChar
= false);
87 * Convert UTF-32 string to wchar_t string (wstring).
88 * No RTL visual-logical transformation is performed.
89 * @param utf32StringSrc is source UTF-32 string to convert
90 * @param wStringDst is output wchar_t string, empty on any error
91 * @param failOnBadChar if set to true function will fail on invalid character,
92 * otherwise invalid character will be skipped
93 * @return true on successful conversion, false on any error
95 static bool utf32ToW(const std::u32string
& utf32StringSrc
, std::wstring
& wStringDst
, bool failOnBadChar
= false);
97 * Perform logical to visual flip.
98 * @param logicalStringSrc is source string with logical characters order
99 * @param visualStringDst is output string with visual characters order, empty on any error
100 * @param forceLTRReadingOrder force LTR reading order
101 * @param visualToLogicalMap is output mapping of positions in the visual string to the logical string
102 * @return true on success, false otherwise
104 static bool utf32logicalToVisualBiDi(const std::u32string
& logicalStringSrc
,
105 std::u32string
& visualStringDst
,
106 bool forceLTRReadingOrder
= false,
107 bool failOnBadString
= false,
108 int* visualToLogicalMap
= nullptr);
110 * Strictly convert wchar_t string (wstring) to UTF-32 string.
111 * No RTL visual-logical transformation is performed.
112 * @param wStringSrc is source wchar_t string to convert
113 * @param utf32StringDst is output UTF-32 string, empty on any error
114 * @param failOnBadChar if set to true function will fail on invalid character,
115 * otherwise invalid character will be skipped
116 * @return true on successful conversion, false on any error
118 static bool wToUtf32(const std::wstring
& wStringSrc
, std::u32string
& utf32StringDst
, bool failOnBadChar
= false);
120 static bool utf8ToW(const std::string
& utf8StringSrc
, std::wstring
& wStringDst
,
121 bool bVisualBiDiFlip
= true, bool forceLTRReadingOrder
= false,
122 bool failOnBadChar
= false);
124 static bool utf16LEtoW(const std::u16string
& utf16String
, std::wstring
& wString
);
126 static bool subtitleCharsetToUtf8(const std::string
& stringSrc
, std::string
& utf8StringDst
);
128 static bool utf8ToStringCharset(const std::string
& utf8StringSrc
, std::string
& stringDst
);
130 static bool utf8ToStringCharset(std::string
& stringSrcDst
);
131 static bool utf8ToSystem(std::string
& stringSrcDst
, bool failOnBadChar
= false);
132 static bool systemToUtf8(const std::string
& sysStringSrc
, std::string
& utf8StringDst
, bool failOnBadChar
= false);
134 static bool utf8To(const std::string
& strDestCharset
, const std::string
& utf8StringSrc
, std::string
& stringDst
);
135 static bool utf8To(const std::string
& strDestCharset
, const std::string
& utf8StringSrc
, std::u16string
& utf16StringDst
);
136 static bool utf8To(const std::string
& strDestCharset
, const std::string
& utf8StringSrc
, std::u32string
& utf32StringDst
);
138 static bool ToUtf8(const std::string
& strSourceCharset
, const std::string
& stringSrc
, std::string
& utf8StringDst
, bool failOnBadChar
= false);
140 static bool wToUTF8(const std::wstring
& wStringSrc
, std::string
& utf8StringDst
, bool failOnBadChar
= false);
143 * \brief Convert UTF-16BE (u16string) string to UTF-8 string.
144 * No RTL visual-logical transformation is performed.
145 * \param utf16StringSrc Is source UTF-16BE u16string string to convert
146 * \param utf8StringDst Is output UTF-8 string, empty on any error
147 * \return True on successful conversion, false on any error
149 static bool utf16BEtoUTF8(const std::u16string
& utf16StringSrc
, std::string
& utf8StringDst
);
152 * \brief Convert UTF-16BE (string) string to UTF-8 string.
153 * No RTL visual-logical transformation is performed.
154 * \param utf16StringSrc Is source UTF-16BE string to convert
155 * \param utf8StringDst Is output UTF-8 string, empty on any error
156 * \return True on successful conversion, false on any error
158 static bool utf16BEtoUTF8(const std::string
& utf16StringSrc
, std::string
& utf8StringDst
);
160 static bool utf16LEtoUTF8(const std::u16string
& utf16StringSrc
, std::string
& utf8StringDst
);
161 static bool ucs2ToUTF8(const std::u16string
& ucs2StringSrc
, std::string
& utf8StringDst
);
164 * \brief Convert Macintosh (string) string to UTF-8 string.
165 * No RTL visual-logical transformation is performed.
166 * \param macStringSrc Is source Macintosh string to convert
167 * \param utf8StringDst Is output UTF-8 string, empty on any error
168 * \return True on successful conversion, false on any error
170 static bool MacintoshToUTF8(const std::string
& macStringSrc
, std::string
& utf8StringDst
);
172 static bool utf8logicalToVisualBiDi(const std::string
& utf8StringSrc
, std::string
& utf8StringDst
, bool failOnBadString
= false);
175 * Check if a string has RTL direction.
176 * @param utf8StringSrc the string
177 * @return true if the string is RTL, otherwise false
179 static bool utf8IsRTLBidiDirection(const std::string
& utf8String
);
181 static bool utf32ToStringCharset(const std::u32string
& utf32StringSrc
, std::string
& stringDst
);
183 static std::vector
<std::string
> getCharsetLabels();
184 static std::string
getCharsetLabelByName(const std::string
& charsetName
);
185 static std::string
getCharsetNameByLabel(const std::string
& charsetLabel
);
187 static bool unknownToUTF8(std::string
& stringSrcDst
);
188 static bool unknownToUTF8(const std::string
& stringSrc
, std::string
& utf8StringDst
, bool failOnBadChar
= false);
190 static bool toW(const std::string
& stringSrc
, std::wstring
& wStringDst
, const std::string
& enc
);
191 static bool fromW(const std::wstring
& wStringSrc
, std::string
& stringDst
, const std::string
& enc
);
193 static void SettingOptionsCharsetsFiller(const std::shared_ptr
<const CSetting
>& setting
,
194 std::vector
<StringSettingOption
>& list
,
195 std::string
& current
,
199 static void resetUserCharset(void);
200 static void resetSubtitleCharset(void);
202 static const int m_Utf8CharMinSize
, m_Utf8CharMaxSize
;
203 class CInnerConverter
;
206 XBMC_GLOBAL_REF(CCharsetConverter
,g_charsetConverter
);
207 #define g_charsetConverter XBMC_GLOBAL_USE(CCharsetConverter)