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/module.hpp"
12#include <cstdint>
13#include <optional>
14#include <bit>
15
16namespace hi::inline v1 {
17
27enum class phrasing : uint8_t {
30 regular = 0,
31
35 emphesis = 1,
36
40 strong = 2,
41
46 code = 3,
47
51 abbreviation = 4,
52
56 quote = 5,
57
62 keyboard = 6,
63
67 highlight = 7,
68
72 math = 8,
73
77 example = 9,
78
82 unarticulated = 10,
83
87 title = 11,
88
92 success = 12,
93
97 warning = 13,
98
102 error = 14,
103};
104
105// clang-format off
106constexpr auto phrasing_metadata = enum_metadata{
107 phrasing::regular, "regular",
108 phrasing::emphesis, "emphesis",
109 phrasing::strong, "strong",
110 phrasing::code, "code",
111 phrasing::abbreviation, "abbreviation",
112 phrasing::quote, "quote",
113 phrasing::keyboard, "keyboard",
114 phrasing::highlight, "highlight",
115 phrasing::math, "math",
116 phrasing::example, "example",
117 phrasing::unarticulated, "unarticulated",
118 phrasing::title, "title",
119 phrasing::success, "success",
120 phrasing::warning, "warning",
121 phrasing::error, "error",
122};
123// clang-format on
124
125// clang-format off
126[[nodiscard]] constexpr std::optional<phrasing> to_phrasing(char c)
127{
128 switch (c) {
129 case 'r': return phrasing::regular;
130 case 'e': return phrasing::emphesis;
131 case 's': return phrasing::strong;
132 case 'c': return phrasing::code;
133 case 'a': return phrasing::abbreviation;
134 case 'q': return phrasing::quote;
135 case 'k': return phrasing::keyboard;
136 case 'h': return phrasing::highlight;
137 case 'm': return phrasing::math;
138 case 'x': return phrasing::example;
139 case 'u': return phrasing::unarticulated;
140 case 't': return phrasing::title;
141 case 'S': return phrasing::success;
142 case 'W': return phrasing::warning;
143 case 'E': return phrasing::error;
144 default:
145 return std::nullopt;
146 }
147}
148// clang-format on
149
150enum class phrasing_mask : uint16_t {
151 regular = 1 << std::to_underlying(phrasing::regular),
152 emphesis = 1 << std::to_underlying(phrasing::emphesis),
153 strong = 1 << std::to_underlying(phrasing::strong),
154 code = 1 << std::to_underlying(phrasing::code),
155 abbreviation = 1 << std::to_underlying(phrasing::abbreviation),
156 quote = 1 << std::to_underlying(phrasing::quote),
157 keyboard = 1 << std::to_underlying(phrasing::keyboard),
158 highlight = 1 << std::to_underlying(phrasing::highlight),
159 math = 1 << std::to_underlying(phrasing::math),
160 example = 1 << std::to_underlying(phrasing::example),
161 unarticulated = 1 << std::to_underlying(phrasing::unarticulated),
162 title = 1 << std::to_underlying(phrasing::title),
163 success = 1 << std::to_underlying(phrasing::success),
164 warning = 1 << std::to_underlying(phrasing::warning),
165 error = 1 << std::to_underlying(phrasing::error),
166
168 title | success | warning | error
169};
170
171static_assert(
172 std::bit_width(phrasing_metadata.size() - 1) <= sizeof(std::underlying_type_t<phrasing_mask>) * CHAR_BIT,
173 "All phrasings must fit the phrasing_mask.");
174
175[[nodiscard]] constexpr phrasing_mask operator&(phrasing_mask const& lhs, phrasing_mask const& rhs) noexcept
176{
177 return static_cast<phrasing_mask>(std::to_underlying(lhs) & std::to_underlying(rhs));
178}
179
180[[nodiscard]] constexpr phrasing_mask operator|(phrasing_mask const& lhs, phrasing_mask const& rhs) noexcept
181{
182 return static_cast<phrasing_mask>(std::to_underlying(lhs) | std::to_underlying(rhs));
183}
184
185[[nodiscard]] constexpr phrasing_mask to_phrasing_mask(phrasing const& rhs) noexcept
186{
187 hi_axiom(std::to_underlying(rhs) < sizeof(phrasing_mask) * CHAR_BIT);
188 return static_cast<phrasing_mask>(1 << std::to_underlying(rhs));
189}
190
191[[nodiscard]] constexpr phrasing_mask to_phrasing_mask(std::string const& str)
192{
193 auto r = phrasing_mask{};
194
195 for (hilet c : str) {
196 if (c == '*') {
197 r = phrasing_mask::all;
198 } else if (hilet p = to_phrasing(c)) {
199 r = r | to_phrasing_mask(*p);
200 } else {
201 throw parse_error(std::format("Unknown character '{}' in text-phrasing-mask", c));
202 }
203 }
204
205 return r;
206}
207
208[[nodiscard]] constexpr bool all(phrasing_mask const& rhs) noexcept
209{
210 return (rhs & phrasing_mask::all) == phrasing_mask::all;
211}
212
213[[nodiscard]] constexpr bool to_bool(phrasing_mask const& rhs) noexcept
214{
215 return to_bool(std::to_underlying(rhs));
216}
217
224[[nodiscard]] constexpr bool matches(phrasing_mask const& lhs, phrasing const& rhs) noexcept
225{
226 return to_bool(lhs & to_phrasing_mask(rhs));
227}
228
229} // namespace hi::inline v1
#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
@ keyboard
The gui_event has keyboard data.
phrasing
Phrasing.
Definition phrasing.hpp:27
@ 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.
@ regular
Regular, normal text.
@ 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:13
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:224