7#include "../utility/utility.hpp"
8#include "../i18n/i18n.hpp"
9#include "../telemetry/telemetry.hpp"
10#include "../concurrency/concurrency.hpp"
11#include "../char_maps/char_maps.hpp"
12#include "unicode_normalization.hpp"
13#include "ucd_general_categories.hpp"
14#include "ucd_canonical_combining_classes.hpp"
15#include "ucd_scripts.hpp"
16#include "phrasing.hpp"
17#include "../macros.hpp"
28#include <unordered_map>
33hi_export_module(hikogui.unicode.grapheme);
35hi_export
namespace hi::inline
v1 {
59 auto const length = *src >> 21;
62 r.resize_and_overwrite(length, [&](
char32_t *dst,
size_t count) {
80 return _table[start] >> 21;
93 return char32_t{_table[start] & 0x1f'ffff};
103 template<
typename CodePo
ints>
106 static_assert(std::is_same_v<typename std::remove_cvref_t<CodePoints>::value_type,
char32_t>);
108 hi_axiom(code_points.size() >= 2);
111 auto const lock = std::scoped_lock(_mutex);
114 if (
auto const it = _indices.find(code_points); it != _indices.end()) {
119 if (_head + code_points.size() >= _table.size()) {
123 auto const insert_index = _head;
124 _head += narrow_cast<uint32_t>(code_points.size());
127 std::copy(code_points.cbegin(), code_points.cend(), _table.begin() + insert_index);
128 _table[insert_index] |= char_cast<char32_t>(code_points.size() << 21);
131 _indices.emplace(std::forward<CodePoints>(code_points), insert_index);
137 mutable unfair_mutex _mutex = {};
150inline long_grapheme_table long_graphemes = {};
168 using value_type = uint64_t;
184 constexpr grapheme() noexcept = default;
190 constexpr
grapheme(
std::in_place_t, value_type value) : _value(value) {}
192 constexpr value_type& intrinsic() noexcept
197 constexpr value_type
const& intrinsic() const noexcept
204 constexpr grapheme(
char32_t code_point) noexcept : _value(char_cast<value_type>(code_point))
206 hi_axiom(code_point <= 0x10'ffff);
207 hi_axiom(ucd_get_canonical_combining_class(code_point) == 0,
"Single code-point must be a starter");
211 constexpr grapheme(
char ascii_char) noexcept : _value(char_cast<value_type>(ascii_char))
213 hi_axiom(ascii_char >= 0 and ascii_char <= 0x7f);
222 hi_axiom(code_point <= 0x10'ffff);
223 hi_axiom(ucd_get_canonical_combining_class(code_point) == 0,
"Single code-point must be a starter");
225 _value = char_cast<value_type>(code_point);
234 hi_axiom(ascii_char >= 0 and ascii_char <= 0x7f);
237 _value = char_cast<value_type>(ascii_char);
246 template<
typename CodePo
ints>
249 static_assert(std::is_same_v<typename std::remove_cvref_t<CodePoints>::value_type,
char32_t>);
251 hi_axiom(not code_points.empty());
252 if (code_points.size() == 1) {
253 auto const code_point = code_points.front();
254 hi_axiom(code_point <= 0x10'ffff);
255 hi_axiom(ucd_get_canonical_combining_class(code_point) == 0);
256 _value = char_cast<value_type>(code_point);
259 auto const index = detail::long_graphemes.add_grapheme(std::forward<CodePoints>(code_points));
261 _value = narrow_cast<value_type>(index + 0x11'0000);
264 [[unlikely]] hi_log_error_once(
265 "grapheme::error::too-many",
"Too many long graphemes encoded, replacing with U+fffd");
266 _value = char_cast<value_type>(U
'\ufffd');
276 constexpr explicit grapheme(std::u32string_view code_points) noexcept :
283 [[nodiscard]]
constexpr uint32_t
index() const noexcept
285 return _value & 0x1f'ffff;
288 [[nodiscard]]
constexpr iso_639 language() const noexcept
290 return iso_639{std::in_place_t{}, narrow_cast<uint16_t>((_value >> 21) & 0x7fff)};
295 hi_axiom(rhs.intrinsic() <= 0x7fff);
297 constexpr auto mask = ~(value_type{0x7fff} << 21);
299 _value |= wide_cast<value_type>(rhs.intrinsic()) << 21;
309 return ucd_get_script(starter());
319 [[nodiscard]]
constexpr iso_15924
starter_script(iso_15924 default_script)
const noexcept
321 auto const starter_script_ = starter_script();
322 if (starter_script_ == iso_15924::common() and starter_script_ == iso_15924::inherited()) {
323 return default_script;
325 return starter_script_;
329 [[nodiscard]]
constexpr iso_15924 script() const noexcept
331 return iso_15924{std::in_place_t{}, narrow_cast<uint16_t>((_value >> 36) & 0x3ff)};
341 auto const new_script = starter_script(rhs);
342 hi_axiom(new_script.intrinsic() < 1000);
344 constexpr auto mask = ~(value_type{0x3ff} << 36);
346 _value |= wide_cast<value_type>(new_script.intrinsic()) << 36;
353 set_script(starter_script());
356 [[nodiscard]]
constexpr iso_3166 region() const noexcept
358 return iso_3166{std::in_place_t{}, narrow_cast<uint16_t>((_value >> 46) & 0x3ff)};
361 constexpr void set_region(iso_3166 rhs)
noexcept
363 hi_axiom(rhs.intrinsic() < 1000);
365 constexpr auto mask = ~(value_type{0x3ff} << 46);
367 _value |= wide_cast<value_type>(rhs.intrinsic()) << 46;
374 auto const language_ = iso_639{std::in_place_t{}, narrow_cast<uint16_t>(tmp & 0x7fff)};
376 auto const script_ = iso_15924{std::in_place_t{}, narrow_cast<uint16_t>(tmp & 0x3ff)};
378 auto const region_ = iso_3166{std::in_place_t{}, narrow_cast<uint16_t>(tmp & 0x3ff)};
384 hi_axiom(rhs.region.intrinsic() < 1000);
385 hi_axiom(rhs.language.intrinsic() <= 0x7fff);
387 auto const new_script = starter_script(rhs.script);
388 hi_axiom(new_script.intrinsic() < 1000);
390 auto tmp = wide_cast<value_type>(rhs.region.intrinsic());
392 tmp |= new_script.intrinsic();
394 tmp |= rhs.language.intrinsic();
397 constexpr auto mask = ~(uint64_t{0x7'ffff'ffff} << 21);
402 [[nodiscard]]
constexpr hi::phrasing
phrasing() const noexcept
404 return static_cast<hi::phrasing
>((_value >> 56) & 0x3f);
407 constexpr void set_phrasing(hi::phrasing rhs)
noexcept
409 hi_axiom(std::to_underlying(rhs) <= 0x3f);
411 constexpr auto mask = ~(value_type{0x3f} << 56);
413 _value |=
static_cast<value_type
>(rhs) << 56;
420 if (
auto i = index(); i <= 0x10'ffff) {
423 return detail::long_graphemes.get_grapheme_size(i - 0x11'0000);
427 [[nodiscard]]
constexpr char32_t starter() const noexcept
429 if (
auto i = index(); i <= 0x10'ffff) {
430 return char_cast<char32_t>(i);
432 return detail::long_graphemes.get_grapheme_starter(i - 0x11'0000);
436 [[nodiscard]]
constexpr bool is_ascii() const noexcept
438 return index() <= 127;
445 if (
auto const i = index(); i <= 0x10'ffff) {
448 return detail::long_graphemes.get_grapheme(i - 0x11'0000);
457 return unicode_decompose(composed(), config);
466 return lhs.index() == rhs.index();
469 [[nodiscard]]
friend constexpr bool operator==(
grapheme const& lhs,
char32_t const& rhs)
noexcept
471 hi_axiom(char_cast<value_type>(rhs) <= 0x10'ffff);
472 return lhs.index() == char_cast<value_type>(rhs);
475 [[nodiscard]]
friend constexpr bool operator==(grapheme
const& lhs,
char const& rhs)
noexcept
477 hi_axiom(char_cast<value_type>(rhs) <= 0x7f);
478 return lhs.index() == char_cast<value_type>(rhs);
485 return lhs.decomposed() <=> rhs.decomposed();
488 [[nodiscard]]
friend constexpr std::strong_ordering operator<=>(
grapheme const& lhs,
char32_t const& rhs)
noexcept
493 [[nodiscard]]
friend constexpr std::strong_ordering operator<=>(grapheme
const& lhs,
char const& rhs)
noexcept
500 return hi::to_string(rhs.composed());
510 return rhs.composed();
constexpr std::u32string to_u32string(std::u32string_view rhs) noexcept
Identity conversion from UTF-32 to UTF-32.
Definition to_string.hpp:28
constexpr std::wstring to_wstring(std::u32string_view rhs) noexcept
Conversion from UTF-32 to wide-string (UTF-16/32).
Definition to_string.hpp:160
@ grapheme
The gui_event has grapheme data.
phrasing
Phrasing.
Definition phrasing.hpp:33
The HikoGUI namespace.
Definition array_generic.hpp:20
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
constexpr void set_language(It first, ItEnd last, language_tag language) noexcept
Set the language for the string.
Definition gstring.hpp:187
constexpr bool unicode_is_NFC_grapheme(It it, ItEnd last) noexcept
Check if the string of code-points is a single grapheme in NFC normal form.
Definition unicode_normalization.hpp:324
constexpr std::u32string unicode_normalize(std::u32string_view text, unicode_normalize_config config=unicode_normalize_config::NFC()) noexcept
Convert text to a Unicode composed normal form.
Definition unicode_normalization.hpp:308
ISO-3166 country code.
Definition iso_3166_intf.hpp:21
ISO-639 language code.
Definition iso_639.hpp:29
The IETF BCP 47 language tag.
Definition language_tag_intf.hpp:30
Definition grapheme.hpp:38
size_t get_grapheme_size(uint32_t start) const noexcept
Get the size of the grapheme.
Definition grapheme.hpp:78
char32_t get_grapheme_starter(uint32_t start) const noexcept
Get the starter (first) code-point of a grapheme.
Definition grapheme.hpp:91
std::u32string get_grapheme(uint32_t start) const noexcept
Get the grapheme from the table.
Definition grapheme.hpp:54
int32_t add_grapheme(CodePoints &&code_points) noexcept
Find or insert a grapheme in the table.
Definition grapheme.hpp:104
Definition grapheme.hpp:154
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:167
constexpr grapheme(std::u32string_view code_points) noexcept
Encode a grapheme from a list of code-points.
Definition grapheme.hpp:276
friend constexpr bool operator==(grapheme const &lhs, grapheme const &rhs) noexcept
Compare equivalence of two graphemes.
Definition grapheme.hpp:464
constexpr void set_script() noexcept
Get the script of the grapheme to the starter script.
Definition grapheme.hpp:351
friend constexpr std::strong_ordering operator<=>(grapheme const &lhs, grapheme const &rhs) noexcept
Compare two graphemes lexicographically.
Definition grapheme.hpp:483
constexpr iso_15924 starter_script(iso_15924 default_script) const noexcept
Get the script of the starter code-point.
Definition grapheme.hpp:319
constexpr uint32_t index() const noexcept
Get the codepoint/index part of the grapheme.
Definition grapheme.hpp:283
constexpr grapheme(char32_t code_point) noexcept
Encode a single code-point.
Definition grapheme.hpp:204
constexpr grapheme & operator=(char ascii_char) noexcept
Encode a single code-point.
Definition grapheme.hpp:232
constexpr grapheme(composed_t, CodePoints &&code_points) noexcept
Encode a grapheme from a list of code-points.
Definition grapheme.hpp:247
value_type _value
The grapheme's value.
Definition grapheme.hpp:182
constexpr std::u32string decomposed(unicode_normalize_config config=unicode_normalize_config::NFD()) const noexcept
Get a list of code-point normalized to NFD.
Definition grapheme.hpp:455
constexpr iso_15924 starter_script() const noexcept
Get the script of the starter code-point.
Definition grapheme.hpp:307
constexpr std::u32string composed() const noexcept
Get a list of code-point normalized to NFC.
Definition grapheme.hpp:443
constexpr void set_script(iso_15924 rhs) noexcept
Set the script of the grapheme.
Definition grapheme.hpp:339
constexpr grapheme & operator=(char32_t code_point) noexcept
Encode a single code-point.
Definition grapheme.hpp:220
constexpr std::size_t size() const noexcept
Return the number of code-points encoded in the grapheme.
Definition grapheme.hpp:418
Definition unicode_normalization.hpp:24