HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
character.hpp
Go to the documentation of this file.
1// Copyright Take Vos 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
8#pragma once
9
10#include "../i18n/module.hpp"
11#include "../unicode/module.hpp"
12#include "../utility/module.hpp"
13#include "text_phrasing.hpp"
14#include "character_attributes.hpp"
15#include <cstdint>
16
17namespace hi { inline namespace v1 {
18
34struct character {
35 using value_type = uint64_t;
36
43 value_type _value;
44
45 constexpr character() noexcept = default;
46 constexpr character(character const&) noexcept = default;
47 constexpr character(character&&) noexcept = default;
48 constexpr character& operator=(character const&) noexcept = default;
49 constexpr character& operator=(character&&) noexcept = default;
50 constexpr friend bool operator==(character const&, character const&) noexcept = default;
51 constexpr friend auto operator<=>(character const&, character const&) noexcept = default;
52
53 constexpr character(hi::grapheme g, character_attributes attributes) noexcept :
54 _value((attributes.intrinsic() << 21) | g.intrinsic())
55 {
56 }
57
58 constexpr character(char32_t code_point, character_attributes attributes) noexcept :
59 character(hi::grapheme{code_point}, attributes)
60 {
61 }
62
63 constexpr character(char code_point, character_attributes attributes) noexcept :
64 character(hi::grapheme{code_point}, attributes)
65 {
66 }
67
68 template<character_attribute... Attributes>
69 constexpr character(hi::grapheme g, Attributes const&...attributes) noexcept :
70 character(g, character_attributes{attributes...})
71 {
72 }
73
74 template<character_attribute... Attributes>
75 constexpr character(char32_t code_point, Attributes const&...attributes) noexcept :
76 character(code_point, character_attributes{attributes...})
77 {
78 }
79
80 template<character_attribute... Attributes>
81 constexpr character(char code_point, Attributes const&...attributes) noexcept :
82 character(code_point, character_attributes{attributes...})
83 {
84 }
85
86 constexpr character(intrinsic_t, value_type value) noexcept : _value(value) {}
87
88 constexpr value_type& intrinsic() noexcept
89 {
90 return _value;
91 }
92
93 constexpr value_type const& intrinsic() const noexcept
94 {
95 return _value;
96 }
97
98 constexpr character& operator=(hi::grapheme grapheme) noexcept
99 {
100 return set_grapheme(grapheme);
101 }
102
103 constexpr character& operator=(char32_t code_point) noexcept
104 {
105 return set_grapheme(hi::grapheme{code_point});
106 }
107
108 constexpr character& operator=(char code_point) noexcept
109 {
110 return set_grapheme(hi::grapheme{code_point});
111 }
112
113 [[nodiscard]] constexpr hi::grapheme grapheme() const noexcept
114 {
115 return hi::grapheme{intrinsic_t{}, _value & 0x1f'ffff};
116 }
117
118 constexpr character& set_grapheme(hi::grapheme grapheme) noexcept
119 {
120 _value >>= 21;
121 _value <<= 21;
122 _value |= grapheme.intrinsic();
123 return *this;
124 }
125
126 [[nodiscard]] size_t size() const noexcept
127 {
128 return grapheme().size();
129 }
130
131 [[nodiscard]] char32_t operator[](size_t i) const noexcept
132 {
133 return grapheme()[i];
134 }
135
136 [[nodiscard]] constexpr character_attributes attributes() const noexcept
137 {
138 return character_attributes{intrinsic_t{}, _value >> 21};
139 }
140
141 constexpr character& set_attributes(character_attributes attributes) noexcept
142 {
143 _value &= 0x1f'ffff;
144 _value |= attributes.intrinsic() << 21;
145 return *this;
146 }
147};
148
149}} // namespace hi::v1
The text_phrasing type.
DOXYGEN BUG.
Definition algorithm.hpp:13
geometry/margins.hpp
Definition cache.hpp:11
The standard HikoGUI character type.
Definition character.hpp:34
value_type _value
[20: 0] 21-bit: grapheme.
Definition character.hpp:43
Definition character_attributes.hpp:13