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#include <string_view>
17#include <format>
18
19hi_export_module(hikogui.unicode.phrasing);
20
21
22hi_export namespace hi::inline v1 {
23
33enum class phrasing : uint8_t {
36 regular = 0,
37
41 emphesis = 1,
42
46 strong = 2,
47
52 code = 3,
53
57 abbreviation = 4,
58
62 quote = 5,
63
68 keyboard = 6,
69
73 highlight = 7,
74
78 math = 8,
79
83 example = 9,
84
88 unarticulated = 10,
89
93 title = 11,
94
98 success = 12,
99
103 warning = 13,
104
108 error = 14,
109};
110
111// clang-format off
112constexpr auto phrasing_metadata = enum_metadata{
113 phrasing::regular, "regular",
114 phrasing::emphesis, "emphesis",
115 phrasing::strong, "strong",
116 phrasing::code, "code",
117 phrasing::abbreviation, "abbreviation",
118 phrasing::quote, "quote",
119 phrasing::keyboard, "keyboard",
120 phrasing::highlight, "highlight",
121 phrasing::math, "math",
122 phrasing::example, "example",
123 phrasing::unarticulated, "unarticulated",
124 phrasing::title, "title",
125 phrasing::success, "success",
126 phrasing::warning, "warning",
127 phrasing::error, "error",
128};
129// clang-format on
130
131// clang-format off
132[[nodiscard]] constexpr std::optional<phrasing> to_phrasing(char c)
133{
134 switch (c) {
135 case 'r': return phrasing::regular;
136 case 'e': return phrasing::emphesis;
137 case 's': return phrasing::strong;
138 case 'c': return phrasing::code;
139 case 'a': return phrasing::abbreviation;
140 case 'q': return phrasing::quote;
141 case 'k': return phrasing::keyboard;
142 case 'h': return phrasing::highlight;
143 case 'm': return phrasing::math;
144 case 'x': return phrasing::example;
145 case 'u': return phrasing::unarticulated;
146 case 't': return phrasing::title;
147 case 'S': return phrasing::success;
148 case 'W': return phrasing::warning;
149 case 'E': return phrasing::error;
150 default:
151 return std::nullopt;
152 }
153}
154// clang-format on
155
156enum class phrasing_mask : uint16_t {
157 regular = 1 << std::to_underlying(phrasing::regular),
158 emphesis = 1 << std::to_underlying(phrasing::emphesis),
159 strong = 1 << std::to_underlying(phrasing::strong),
160 code = 1 << std::to_underlying(phrasing::code),
161 abbreviation = 1 << std::to_underlying(phrasing::abbreviation),
162 quote = 1 << std::to_underlying(phrasing::quote),
163 keyboard = 1 << std::to_underlying(phrasing::keyboard),
164 highlight = 1 << std::to_underlying(phrasing::highlight),
165 math = 1 << std::to_underlying(phrasing::math),
166 example = 1 << std::to_underlying(phrasing::example),
167 unarticulated = 1 << std::to_underlying(phrasing::unarticulated),
168 title = 1 << std::to_underlying(phrasing::title),
169 success = 1 << std::to_underlying(phrasing::success),
170 warning = 1 << std::to_underlying(phrasing::warning),
171 error = 1 << std::to_underlying(phrasing::error),
172
174 title | success | warning | error
175};
176
177static_assert(
178 std::bit_width(phrasing_metadata.size() - 1) <= sizeof(std::underlying_type_t<phrasing_mask>) * CHAR_BIT,
179 "All phrasings must fit the phrasing_mask.");
180
181[[nodiscard]] constexpr phrasing_mask operator&(phrasing_mask const& lhs, phrasing_mask const& rhs) noexcept
182{
183 return static_cast<phrasing_mask>(std::to_underlying(lhs) & std::to_underlying(rhs));
184}
185
186[[nodiscard]] constexpr phrasing_mask operator|(phrasing_mask const& lhs, phrasing_mask const& rhs) noexcept
187{
188 return static_cast<phrasing_mask>(std::to_underlying(lhs) | std::to_underlying(rhs));
189}
190
191[[nodiscard]] constexpr phrasing_mask to_phrasing_mask(phrasing const& rhs) noexcept
192{
193 hi_axiom(std::to_underlying(rhs) < sizeof(phrasing_mask) * CHAR_BIT);
194 return static_cast<phrasing_mask>(1 << std::to_underlying(rhs));
195}
196
197[[nodiscard]] constexpr phrasing_mask to_phrasing_mask(std::string const& str)
198{
199 auto r = phrasing_mask{};
200
201 for (auto const c : str) {
202 if (c == '*') {
203 r = phrasing_mask::all;
204 } else if (auto const p = to_phrasing(c)) {
205 r = r | to_phrasing_mask(*p);
206 } else {
207 throw parse_error(std::format("Unknown character '{}' in text-phrasing-mask", c));
208 }
209 }
210
211 return r;
212}
213
214[[nodiscard]] constexpr bool all(phrasing_mask const& rhs) noexcept
215{
216 return (rhs & phrasing_mask::all) == phrasing_mask::all;
217}
218
219[[nodiscard]] constexpr bool to_bool(phrasing_mask const& rhs) noexcept
220{
221 return to_bool(std::to_underlying(rhs));
222}
223
230[[nodiscard]] constexpr bool matches(phrasing_mask const& lhs, phrasing const& rhs) noexcept
231{
232 return to_bool(lhs & to_phrasing_mask(rhs));
233}
234
235} // namespace hi::inline v1
@ keyboard
The gui_event has keyboard data.
phrasing
Phrasing.
Definition phrasing.hpp:33
@ 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_misc.hpp:20
@ 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:230