HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_phrasing.hpp
Go to the documentation of this file.
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
15namespace hi::inline v1 {
16
22enum class text_phrasing : uint8_t {
25 regular = 0,
26
30 emphesis = 1,
31
35 strong = 2,
36
41 code = 3,
42
46 abbreviation = 4,
47
51 quote = 5,
52
57 keyboard = 6,
58
62 highlight = 7,
63
67 math = 8,
68
72 example = 9,
73
77 unarticulated = 10,
78
82 title = 11,
83
87 success = 12,
88
92 warning = 13,
93
97 error = 14,
98};
99
100// clang-format off
101constexpr auto text_phrasing_metadata = enum_metadata{
102 text_phrasing::regular, "regular",
103 text_phrasing::emphesis, "emphesis",
104 text_phrasing::strong, "strong",
105 text_phrasing::code, "code",
106 text_phrasing::abbreviation, "abbreviation",
107 text_phrasing::quote, "quote",
108 text_phrasing::keyboard, "keyboard",
109 text_phrasing::highlight, "highlight",
110 text_phrasing::math, "math",
111 text_phrasing::example, "example",
112 text_phrasing::unarticulated, "unarticulated",
113 text_phrasing::title, "title",
114 text_phrasing::success, "success",
115 text_phrasing::warning, "warning",
116 text_phrasing::error, "error",
117};
118// clang-format on
119static_assert(text_phrasing_metadata.size() <= 15, "The mask of a text_phrasing is 16-bit");
120
121// clang-format off
122[[nodiscard]] constexpr std::optional<text_phrasing> to_text_phrasing(char c)
123{
124 switch (c) {
125 case 'r': return text_phrasing::regular;
126 case 'e': return text_phrasing::emphesis;
127 case 's': return text_phrasing::strong;
128 case 'c': return text_phrasing::code;
129 case 'a': return text_phrasing::abbreviation;
130 case 'q': return text_phrasing::quote;
131 case 'k': return text_phrasing::keyboard;
132 case 'h': return text_phrasing::highlight;
133 case 'm': return text_phrasing::math;
134 case 'x': return text_phrasing::example;
135 case 'u': return text_phrasing::unarticulated;
136 case 't': return text_phrasing::title;
137 case 'S': return text_phrasing::success;
138 case 'W': return text_phrasing::warning;
139 case 'E': return text_phrasing::error;
140 default:
141 return std::nullopt;
142 }
143}
144// clang-format on
145
146enum class text_phrasing_mask : uint16_t {
147 regular = 1 << to_underlying(text_phrasing::regular),
148 emphesis = 1 << to_underlying(text_phrasing::emphesis),
149 strong = 1 << to_underlying(text_phrasing::strong),
150 code = 1 << to_underlying(text_phrasing::code),
151 abbreviation = 1 << to_underlying(text_phrasing::abbreviation),
152 quote = 1 << to_underlying(text_phrasing::quote),
153 keyboard = 1 << to_underlying(text_phrasing::keyboard),
154 highlight = 1 << to_underlying(text_phrasing::highlight),
155 math = 1 << to_underlying(text_phrasing::math),
156 example = 1 << to_underlying(text_phrasing::example),
157 unarticulated = 1 << to_underlying(text_phrasing::unarticulated),
158 title = 1 << to_underlying(text_phrasing::title),
159 success = 1 << to_underlying(text_phrasing::success),
160 warning = 1 << to_underlying(text_phrasing::warning),
161 error = 1 << to_underlying(text_phrasing::error),
162
164 title | success | warning | error
165};
166
167[[nodiscard]] constexpr text_phrasing_mask operator&(text_phrasing_mask const& lhs, text_phrasing_mask const& rhs) noexcept
168{
169 return static_cast<text_phrasing_mask>(to_underlying(lhs) & to_underlying(rhs));
170}
171
172[[nodiscard]] constexpr text_phrasing_mask operator|(text_phrasing_mask const& lhs, text_phrasing_mask const& rhs) noexcept
173{
174 return static_cast<text_phrasing_mask>(to_underlying(lhs) | to_underlying(rhs));
175}
176
177[[nodiscard]] constexpr text_phrasing_mask to_text_phrasing_mask(text_phrasing const& rhs) noexcept
178{
179 hi_axiom(to_underlying(rhs) < sizeof(text_phrasing_mask) * CHAR_BIT);
180 return static_cast<text_phrasing_mask>(1 << to_underlying(rhs));
181}
182
183[[nodiscard]] constexpr text_phrasing_mask to_text_phrasing_mask(std::string const& str)
184{
185 auto r = text_phrasing_mask{};
186
187 for (hilet c : str) {
188 if (c == '*') {
189 r = text_phrasing_mask::all;
190 } else if (hilet p = to_text_phrasing(c)) {
191 r = r | to_text_phrasing_mask(*p);
192 } else {
193 throw parse_error(std::format("Unknown character '{}' in text-phrasing-mask", c));
194 }
195 }
196
197 return r;
198}
199
200[[nodiscard]] constexpr bool all(text_phrasing_mask const& rhs) noexcept
201{
202 return (rhs & text_phrasing_mask::all) == text_phrasing_mask::all;
203}
204
205[[nodiscard]] constexpr bool to_bool(text_phrasing_mask const& rhs) noexcept
206{
207 return to_bool(to_underlying(rhs));
208}
209
216[[nodiscard]] constexpr bool matches(text_phrasing_mask const& lhs, text_phrasing const& rhs) noexcept
217{
218 return to_bool(lhs & to_text_phrasing_mask(rhs));
219}
220
221} // 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.
text_phrasing
Text phrasing.
Definition text_phrasing.hpp:22
@ 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.
@ warning
Format a warning message Often in bright yellow.
@ math
Text formatted as math.
@ code
Text is a piece of programming-code; a variable name, a function name.
@ abbreviation
An abbreviation.
@ error
Format a "bad" message Often in bright red.
@ title
Format a heading Often in bold, larger font and on a line by itself.
DOXYGEN BUG.
Definition algorithm.hpp:13
@ regular
400: Normal / Regular
constexpr bool matches(text_phrasing_mask const &lhs, text_phrasing const &rhs) noexcept
Check if the text-phrasing is included in the text-phrasing-mask.
Definition text_phrasing.hpp:216