HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_phrasing.hpp
1
2
3#pragma once
4
5#include "../required.hpp"
6#include "../cast.hpp"
7#include "../enum_metadata.hpp"
8#include <cstdint>
9
10namespace hi::inline v1 {
11
12enum class text_phrasing {
13 regular,
14 emphesis,
15 strong,
16 code,
17 abbreviation,
18 bold,
19 italic,
20 citation,
21 keyboard,
22 mark,
23 math,
24 example,
25 unarticulated,
26};
27
28// clang-format off
29constexpr auto text_phrasing_metadata = enum_metadata{
30 text_phrasing::regular, "regular",
31 text_phrasing::emphesis, "emphesis",
32 text_phrasing::strong, "strong",
33 text_phrasing::code, "code",
34 text_phrasing::abbreviation, "abbreviation",
35 text_phrasing::bold, "bold",
36 text_phrasing::italic, "italic",
37 text_phrasing::citation, "citation",
38 text_phrasing::keyboard, "keyboard",
39 text_phrasing::mark, "mark",
40 text_phrasing::math, "math",
41 text_phrasing::example, "example",
42 text_phrasing::unarticulated, "unarticulated",
43};
44// clang-format on
45
46enum class text_phrasing_mask : uint16_t {
47 none = 0,
48 regular = 1 << to_underlying(text_phrasing::regular),
49 emphesis = 1 << to_underlying(text_phrasing::emphesis),
50 strong = 1 << to_underlying(text_phrasing::strong),
51 code = 1 << to_underlying(text_phrasing::code),
52 abbreviation = 1 << to_underlying(text_phrasing::abbreviation),
53 bold = 1 << to_underlying(text_phrasing::bold),
54 italic = 1 << to_underlying(text_phrasing::italic),
55 citation = 1 << to_underlying(text_phrasing::citation),
56 keyboard = 1 << to_underlying(text_phrasing::keyboard),
57 mark = 1 << to_underlying(text_phrasing::highlight),
58 math = 1 << to_underlying(text_phrasing::math),
59 example = 1 << to_underlying(text_phrasing::example),
60 unarticulated = 1 << to_underlying(text_phrasing::unarticulated),
61
62 all = regular | emphesis | strong | code | abbreviation | bold | italic | citation | keyboard | highlight | math | example |
63 unarticulated
64};
65
66[[nodiscard]] constexpr text_phrasing_mask operator&(text_phrasing_mask const& lhs, text_phrasing_mask const& rhs) noexcept
67{
68 return static_cast<text_phrasing_mask>(to_underlying(lhs) & to_underlying(rhs));
69}
70
71[[nodiscard]] constexpr text_phrasing_mask operator|(text_phrasing_mask const& lhs, text_phrasing_mask const& rhs) noexcept
72{
73 return static_cast<text_phrasing_mask>(to_underlying(lhs) | to_underlying(rhs));
74}
75
76constexpr text_phrasing_mask &operator|=(text_phrasing_mask &lhs, text_phrasing_mask const& rhs) noexcept
77{
78 return lhs = lhs | rhs;
79}
80
81constexpr text_phrasing_mask &operator&=(text_phrasing_mask &lhs, text_phrasing_mask const& rhs) noexcept
82{
83 return lhs = lhs & rhs;
84}
85
86[[nodiscard]] constexpr text_phrasing_mask to_text_phrasing_mask(text_phrasing const& rhs) noexcept
87{
88 hi_axiom(to_underlying(rhs) < sizeof(text_phrasing_mask) * CHAR_BIT);
89 return static_cast<text_phrasing_mask>(1 << to_underlying(rhs));
90}
91
92[[nodiscard]] constexpr text_phrasing_mask to_text_phrasing_mask(char const &rhs) noexcept
93{
94 // clang-format off
95 switch (rhs) {
96 case 'r': return text_phrasing_mask::regular;
97 case 'e': return text_phrasing_mask::emphesis;
98 case 's': return text_phrasing_mask::strong;
99 case 'c': return text_phrasing_mask::code;
100 case 'a': return text_phrasing_mask::abbreviation;
101 case 'b': return text_phrasing_mask::bold;
102 case 'i': return text_phrasing_mask::italic;
103 case 'c': return text_phrasing_mask::citation;
104 case 'k': return text_phrasing_mask::keyboard;
105 case 'h': return text_phrasing_mask::highlight;
106 case 'm': return text_phrasing_mask::mark;
107 case 'x': return text_phrasing_mask::example;
108 case 'u': return text_phrasing_mask::unarticulated;
109 default: hi_no_default();
110 }
111 // clang-format on
112}
113
114[[nodiscard]] constexpr text_phrasing_mask to_text_phrasing_mask(std::string_view rhs) noexcept
115{
116 auto r = text_phrasing_mask::none;
117 for (hilet c: rhs) {
118 r |= to_text_phrasing_mask(c);
119 }
120 return r;
121}
122
123[[nodiscard]] constexpr std::string to_string(text_phrasing_mask const &rhs) noexcept
124{
125 auto r = std::string{};
126 r.reserve(std::popcount(rhs));
127
128 // clang-format off
129 if (to_bool(rhs & text_phrasing_mask::regular)) { r += 'r'; }
130 if (to_bool(rhs & text_phrasing_mask::emphesis)) { r += 'e'; }
131 if (to_bool(rhs & text_phrasing_mask::strong)) { r += 's'; }
132 if (to_bool(rhs & text_phrasing_mask::code)) { r += 'c'; }
133 if (to_bool(rhs & text_phrasing_mask::abbreviation)) { r += 'a'; }
134 if (to_bool(rhs & text_phrasing_mask::bold)) { r += 'b'; }
135 if (to_bool(rhs & text_phrasing_mask::italic)) { r += 'i'; }
136 if (to_bool(rhs & text_phrasing_mask::citation)) { r += 'q'; }
137 if (to_bool(rhs & text_phrasing_mask::keyboard)) { r += 'k'; }
138 if (to_bool(rhs & text_phrasing_mask::highlight)) { r += 'h'; }
139 if (to_bool(rhs & text_phrasing_mask::math)) { r += 'm'; }
140 if (to_bool(rhs & text_phrasing_mask::example)) { r += 'x'; }
141 if (to_bool(rhs & text_phrasing_mask::unarticulated)) { r += 'u'; }
142 // clang-format on
143
144 return r;
145}
146
147[[nodiscard]] constexpr bool all(text_phrasing_mask const& rhs) noexcept
148{
149 return (rhs & text_phrasing_mask::all) == text_phrasing_mask::all;
150}
151
152[[nodiscard]] constexpr bool to_bool(text_phrasing_mask const& rhs) noexcept
153{
154 return to_bool(to_underlying(rhs));
155}
156
157} // namespace hi::inline v1
This file includes required definitions.
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
constexpr alignment operator|(horizontal_alignment lhs, vertical_alignment rhs) noexcept
Combine vertical and horizontal alignment.
Definition alignment.hpp:200
T reserve(T... args)
T to_string(T... args)