HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
character_attributes.hpp
1
2#pragma once
3
4#include "../i18n/module.hpp"
5#include "text_phrasing.hpp"
6
7namespace hi { inline namespace v1 {
8
9template<typename Context>
10concept character_attribute = std::same_as<Context, iso_639> or std::same_as<Context, iso_3166> or
11 std::same_as<Context, iso_15924> or std::same_as<Context, language_tag> or std::same_as<Context, text_phrasing>;
12
14public:
15 using value_type = uint64_t;
16
17 constexpr character_attributes() noexcept = default;
18 constexpr character_attributes(character_attributes const&) noexcept = default;
19 constexpr character_attributes(character_attributes&&) noexcept = default;
20 constexpr character_attributes& operator=(character_attributes const&) noexcept = default;
21 constexpr character_attributes& operator=(character_attributes&&) noexcept = default;
22 [[nodiscard]] constexpr friend bool operator==(character_attributes const&, character_attributes const&) noexcept = default;
23
24 constexpr character_attributes(intrinsic_t, value_type value) noexcept : _value(value)
25 {
26 // 40 bits.
27 hi_axiom(_value < (value_type{1} << 40));
28 }
29
30 [[nodiscard]] constexpr value_type const& intrinsic() const
31 {
32 return _value;
33 }
34
35 [[nodiscard]] constexpr value_type& intrinsic()
36 {
37 return _value;
38 }
39
40 [[nodiscard]] constexpr hi::text_phrasing phrasing() const noexcept
41 {
42 return static_cast<hi::text_phrasing>((_value >> _phrasing_shift) & _phrasing_mask);
43 }
44
45 constexpr character_attributes& set_phrasing(hi::text_phrasing phrasing) noexcept
46 {
47 hilet phrasing_value = wide_cast<value_type>(to_underlying(phrasing));
48 hi_axiom(phrasing_value <= _phrasing_mask);
49 _value &= ~(_phrasing_mask << _phrasing_shift);
50 _value |= phrasing_value << _phrasing_shift;
51 return *this;
52 }
53
54 [[nodiscard]] constexpr iso_639 language() const noexcept
55 {
56 return iso_639{intrinsic_t{}, (_value >> _language_shift) & _language_mask};
57 }
58
59 constexpr character_attributes& set_language(iso_639 language) noexcept
60 {
61 hilet language_value = wide_cast<value_type>(language.intrinsic());
62 hi_axiom(language_value <= _language_mask);
63 _value &= ~(_language_mask << _language_shift);
64 _value |= language_value << _language_shift;
65 return *this;
66 }
67
68 [[nodiscard]] constexpr iso_15924 script() const noexcept
69 {
70 return iso_15924{intrinsic_t{}, (_value >> _script_shift) & _script_mask};
71 }
72
73 constexpr character_attributes& set_script(iso_15924 script) noexcept
74 {
75 hilet script_value = wide_cast<value_type>(script.intrinsic());
76 hi_axiom(script_value <= _script_mask);
77 _value &= ~(_script_mask << _script_shift);
78 _value |= script_value << _script_shift;
79 return *this;
80 }
81
82 [[nodiscard]] constexpr iso_3166 region() const noexcept
83 {
84 return iso_3166{intrinsic_t{}, (_value >> _region_shift) & _region_mask};
85 }
86
87 constexpr character_attributes& set_region(iso_3166 region) noexcept
88 {
89 hilet region_value = wide_cast<value_type>(region.intrinsic());
90 hi_axiom(region_value <= _region_mask);
91 _value &= ~(_region_mask << _region_shift);
92 _value |= region_value << _region_shift;
93 return *this;
94 }
95
96 constexpr character_attributes& set_language(hi::language_tag language_tag) noexcept
97 {
98 return set_language(language_tag.language).set_script(language_tag.script).set_region(language_tag.region);
99 }
100
101 template<character_attribute... Args>
102 constexpr character_attributes(Args const&...args) noexcept
103 {
104 add(args...);
105 }
106
107 constexpr void add() noexcept {}
108
109 constexpr void add(iso_639 const& arg) noexcept
110 {
111 set_language(arg);
112 }
113
114 constexpr void add(iso_15924 const& arg) noexcept
115 {
116 set_script(arg);
117 }
118
119 constexpr void add(iso_3166 const& arg) noexcept
120 {
121 set_region(arg);
122 }
123
124 constexpr void add(language_tag const &arg) noexcept
125 {
126 set_language(arg);
127 }
128
129 constexpr void add(text_phrasing const& arg) noexcept
130 {
131 set_phrasing(arg);
132 }
133
134 template<character_attribute First, character_attribute Second, character_attribute... Rest>
135 constexpr void add(First const& first, Second const& second, Rest const&...rest) noexcept
136 {
137 add(first);
138 add(second, rest...);
139 }
140
141private:
142 constexpr static auto _phrasing_mask = value_type{0xf};
143 constexpr static auto _phrasing_shift = 35U;
144 constexpr static auto _region_mask = value_type{0x3ff};
145 constexpr static auto _region_shift = 25U;
146 constexpr static auto _script_mask = value_type{0x3ff};
147 constexpr static auto _script_shift = 15U;
148 constexpr static auto _language_mask = value_type{0x7fff};
149 constexpr static auto _language_shift = 0U;
150
158 value_type _value;
159};
160
161}} // namespace hi::v1
The text_phrasing type.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
Definition character_attributes.hpp:13
Tag used in constructors to set the intrinsic value of that object.
Definition utility.hpp:242
Definition character_attributes.hpp:10