HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
phrasing.hpp
1// Copyright Take Vos 2022.
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
9#pragma once
10
11#include "../utility/utility.hpp"
12#include "../macros.hpp"
13#include <cstdint>
14#include <optional>
15#include <bit>
16
17
18
19namespace hi::inline v1 {
20
30enum class phrasing : uint8_t {
33 regular = 0,
34
38 emphesis = 1,
39
43 strong = 2,
44
49 code = 3,
50
54 abbreviation = 4,
55
59 quote = 5,
60
65 keyboard = 6,
66
70 highlight = 7,
71
75 math = 8,
76
80 example = 9,
81
85 unarticulated = 10,
86
90 title = 11,
91
95 success = 12,
96
100 warning = 13,
101
105 error = 14,
106};
107
108// clang-format off
109constexpr auto phrasing_metadata = enum_metadata{
110 phrasing::regular, "regular",
111 phrasing::emphesis, "emphesis",
112 phrasing::strong, "strong",
113 phrasing::code, "code",
114 phrasing::abbreviation, "abbreviation",
115 phrasing::quote, "quote",
116 phrasing::keyboard, "keyboard",
117 phrasing::highlight, "highlight",
118 phrasing::math, "math",
119 phrasing::example, "example",
120 phrasing::unarticulated, "unarticulated",
121 phrasing::title, "title",
122 phrasing::success, "success",
123 phrasing::warning, "warning",
124 phrasing::error, "error",
125};
126// clang-format on
127
128// clang-format off
129[[nodiscard]] constexpr std::optional<phrasing> to_phrasing(char c)
130{
131 switch (c) {
132 case 'r': return phrasing::regular;
133 case 'e': return phrasing::emphesis;
134 case 's': return phrasing::strong;
135 case 'c': return phrasing::code;
136 case 'a': return phrasing::abbreviation;
137 case 'q': return phrasing::quote;
138 case 'k': return phrasing::keyboard;
139 case 'h': return phrasing::highlight;
140 case 'm': return phrasing::math;
141 case 'x': return phrasing::example;
142 case 'u': return phrasing::unarticulated;
143 case 't': return phrasing::title;
144 case 'S': return phrasing::success;
145 case 'W': return phrasing::warning;
146 case 'E': return phrasing::error;
147 default:
148 return std::nullopt;
149 }
150}
151// clang-format on
152
153enum class phrasing_mask : uint16_t {
154 regular = 1 << std::to_underlying(phrasing::regular),
155 emphesis = 1 << std::to_underlying(phrasing::emphesis),
156 strong = 1 << std::to_underlying(phrasing::strong),
157 code = 1 << std::to_underlying(phrasing::code),
158 abbreviation = 1 << std::to_underlying(phrasing::abbreviation),
159 quote = 1 << std::to_underlying(phrasing::quote),
160 keyboard = 1 << std::to_underlying(phrasing::keyboard),
161 highlight = 1 << std::to_underlying(phrasing::highlight),
162 math = 1 << std::to_underlying(phrasing::math),
163 example = 1 << std::to_underlying(phrasing::example),
164 unarticulated = 1 << std::to_underlying(phrasing::unarticulated),
165 title = 1 << std::to_underlying(phrasing::title),
166 success = 1 << std::to_underlying(phrasing::success),
167 warning = 1 << std::to_underlying(phrasing::warning),
168 error = 1 << std::to_underlying(phrasing::error),
169
171 title | success | warning | error
172};
173
174static_assert(
175 std::bit_width(phrasing_metadata.size() - 1) <= sizeof(std::underlying_type_t<phrasing_mask>) * CHAR_BIT,
176 "All phrasings must fit the phrasing_mask.");
177
178[[nodiscard]] constexpr phrasing_mask operator&(phrasing_mask const& lhs, phrasing_mask const& rhs) noexcept
179{
180 return static_cast<phrasing_mask>(std::to_underlying(lhs) & std::to_underlying(rhs));
181}
182
183[[nodiscard]] constexpr phrasing_mask operator|(phrasing_mask const& lhs, phrasing_mask const& rhs) noexcept
184{
185 return static_cast<phrasing_mask>(std::to_underlying(lhs) | std::to_underlying(rhs));
186}
187
188[[nodiscard]] constexpr phrasing_mask to_phrasing_mask(phrasing const& rhs) noexcept
189{
190 hi_axiom(std::to_underlying(rhs) < sizeof(phrasing_mask) * CHAR_BIT);
191 return static_cast<phrasing_mask>(1 << std::to_underlying(rhs));
192}
193
194[[nodiscard]] constexpr phrasing_mask to_phrasing_mask(std::string const& str)
195{
196 auto r = phrasing_mask{};
197
198 for (hilet c : str) {
199 if (c == '*') {
200 r = phrasing_mask::all;
201 } else if (hilet p = to_phrasing(c)) {
202 r = r | to_phrasing_mask(*p);
203 } else {
204 throw parse_error(std::format("Unknown character '{}' in text-phrasing-mask", c));
205 }
206 }
207
208 return r;
209}
210
211[[nodiscard]] constexpr bool all(phrasing_mask const& rhs) noexcept
212{
213 return (rhs & phrasing_mask::all) == phrasing_mask::all;
214}
215
216[[nodiscard]] constexpr bool to_bool(phrasing_mask const& rhs) noexcept
217{
218 return to_bool(std::to_underlying(rhs));
219}
220
227[[nodiscard]] constexpr bool matches(phrasing_mask const& lhs, phrasing const& rhs) noexcept
228{
229 return to_bool(lhs & to_phrasing_mask(rhs));
230}
231
232} // namespace hi::inline v1
@ keyboard
The gui_event has keyboard data.
phrasing
Phrasing.
Definition phrasing.hpp:30
@ example
Used in help text to show an example.
@ highlight
The text is marked or highlighted as if being marked by a highlight pen.
@ unarticulated
Unarticulated.
@ strong
Strong text; spoken louder, as if the text is not to be missed.
@ emphesis
Emphesised text; spoken as if the text is special importance, significant or promonent.
@ math
Text formatted as math.
@ code
Text is a piece of programming-code; a variable name, a function name.
@ abbreviation
An abbreviation.
@ title
Format a heading Often in bold, larger font and on a line by itself.
DOXYGEN BUG.
Definition algorithm.hpp:16
@ regular
400: Normal / Regular
constexpr bool matches(phrasing_mask const &lhs, phrasing const &rhs) noexcept
Check if the text-phrasing is included in the text-phrasing-mask.
Definition phrasing.hpp:227
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377