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
8
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
21hi_export namespace hi::inline v1 {
31enum class phrasing : uint8_t {
35
40
44 strong = 2,
45
50 code = 3,
51
56
60 quote = 5,
61
66 keyboard = 6,
67
72
76 math = 8,
77
81 example = 9,
82
87
92
96 title = 12,
97
101 success = 13,
102
107
111 error = 15,
112};
113
114// clang-format off
115constexpr auto phrasing_metadata = enum_metadata{
116 phrasing::regular, "regular",
117 phrasing::emphasis, "emphasis",
118 phrasing::strong, "strong",
119 phrasing::code, "code",
120 phrasing::abbreviation, "abbreviation",
121 phrasing::quote, "quote",
122 phrasing::keyboard, "keyboard",
123 phrasing::highlight, "highlight",
124 phrasing::math, "math",
125 phrasing::example, "example",
126 phrasing::placeholder, "placeholder",
127 phrasing::unarticulated, "unarticulated",
128 phrasing::title, "title",
129 phrasing::success, "success",
130 phrasing::warning, "warning",
131 phrasing::error, "error",
132};
133// clang-format on
134
135// clang-format off
136[[nodiscard]] constexpr std::optional<phrasing> to_phrasing(char c)
137{
138 switch (c) {
139 case 'r': return phrasing::regular;
140 case 'e': return phrasing::emphasis;
141 case 's': return phrasing::strong;
142 case 'c': return phrasing::code;
143 case 'a': return phrasing::abbreviation;
144 case 'q': return phrasing::quote;
145 case 'k': return phrasing::keyboard;
146 case 'h': return phrasing::highlight;
147 case 'm': return phrasing::math;
148 case 'x': return phrasing::example;
149 case 'p': return phrasing::placeholder;
150 case 'u': return phrasing::unarticulated;
151 case 't': return phrasing::title;
152 case 'S': return phrasing::success;
153 case 'W': return phrasing::warning;
154 case 'E': return phrasing::error;
155 default:
156 return std::nullopt;
157 }
158}
159// clang-format on
160
161enum class phrasing_mask : uint16_t {
162 regular = 1 << std::to_underlying(phrasing::regular),
163 emphasis = 1 << std::to_underlying(phrasing::emphasis),
164 strong = 1 << std::to_underlying(phrasing::strong),
165 code = 1 << std::to_underlying(phrasing::code),
166 abbreviation = 1 << std::to_underlying(phrasing::abbreviation),
167 quote = 1 << std::to_underlying(phrasing::quote),
168 keyboard = 1 << std::to_underlying(phrasing::keyboard),
169 highlight = 1 << std::to_underlying(phrasing::highlight),
170 math = 1 << std::to_underlying(phrasing::math),
171 example = 1 << std::to_underlying(phrasing::example),
172 placeholder = 1 << std::to_underlying(phrasing::placeholder),
173 unarticulated = 1 << std::to_underlying(phrasing::unarticulated),
174 title = 1 << std::to_underlying(phrasing::title),
175 success = 1 << std::to_underlying(phrasing::success),
176 warning = 1 << std::to_underlying(phrasing::warning),
177 error = 1 << std::to_underlying(phrasing::error),
178
180 unarticulated | title | success | warning | error
181};
182
183static_assert(
184 std::bit_width(phrasing_metadata.size() - 1) <= sizeof(std::underlying_type_t<phrasing_mask>) * CHAR_BIT,
185 "All phrasings must fit the phrasing_mask.");
186
187[[nodiscard]] constexpr phrasing_mask operator&(phrasing_mask const& lhs, phrasing_mask const& rhs) noexcept
188{
189 return static_cast<phrasing_mask>(std::to_underlying(lhs) & std::to_underlying(rhs));
190}
191
192[[nodiscard]] constexpr phrasing_mask operator|(phrasing_mask const& lhs, phrasing_mask const& rhs) noexcept
193{
194 return static_cast<phrasing_mask>(std::to_underlying(lhs) | std::to_underlying(rhs));
195}
196
197[[nodiscard]] constexpr phrasing_mask to_phrasing_mask(phrasing const& rhs) noexcept
198{
199 hi_axiom(std::to_underlying(rhs) < sizeof(phrasing_mask) * CHAR_BIT);
200 return static_cast<phrasing_mask>(1 << std::to_underlying(rhs));
201}
202
203[[nodiscard]] constexpr phrasing_mask to_phrasing_mask(std::string const& str)
204{
205 auto r = phrasing_mask{};
206
207 for (auto const c : str) {
208 if (c == '*') {
209 r = phrasing_mask::all;
210 } else if (auto const p = to_phrasing(c)) {
211 r = r | to_phrasing_mask(*p);
212 } else {
213 throw parse_error(std::format("Unknown character '{}' in text-phrasing-mask", c));
214 }
215 }
216
217 return r;
218}
219
220[[nodiscard]] constexpr bool all(phrasing_mask const& rhs) noexcept
221{
222 return (rhs & phrasing_mask::all) == phrasing_mask::all;
223}
224
225[[nodiscard]] constexpr bool to_bool(phrasing_mask const& rhs) noexcept
226{
227 return to_bool(std::to_underlying(rhs));
228}
229
236[[nodiscard]] constexpr bool matches(phrasing_mask const& lhs, phrasing const& rhs) noexcept
237{
238 return to_bool(lhs & to_phrasing_mask(rhs));
239}
240
241} // namespace hi::inline v1
@ keyboard
The gui_event has keyboard data.
Definition gui_event_variant.hpp:32
phrasing
Phrasing.
Definition phrasing.hpp:31
@ example
Used in help text to show an example.
Definition phrasing.hpp:81
@ highlight
The text is marked or highlighted as if being marked by a highlight pen.
Definition phrasing.hpp:71
@ unarticulated
Unarticulated.
Definition phrasing.hpp:91
@ placeholder
Placeholder for text which will be replaced by the user.
Definition phrasing.hpp:86
@ strong
Strong text; spoken louder, as if the text is not to be missed.
Definition phrasing.hpp:44
@ warning
Format a warning message Often in bright yellow.
Definition phrasing.hpp:106
@ math
Text formatted as math.
Definition phrasing.hpp:76
@ code
Text is a piece of programming-code; a variable name, a function name.
Definition phrasing.hpp:50
@ abbreviation
An abbreviation.
Definition phrasing.hpp:55
@ error
Format a "bad" message Often in bright red.
Definition phrasing.hpp:111
@ title
Format a heading Often in bold, larger font and on a line by itself.
Definition phrasing.hpp:96
@ emphasis
Emphesised text; spoken as if the text is special importance, significant or promonent.
Definition phrasing.hpp:39
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
@ regular
400: Normal / Regular
Definition font_weight.hpp:25
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:236