HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
unicode_word_break.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
8#pragma once
9
10#include "unicode_break_opportunity.hpp"
11#include "ucd_general_categories.hpp"
12#include "ucd_grapheme_cluster_breaks.hpp"
13#include "ucd_word_break_properties.hpp"
14#include "../utility/utility.hpp"
15#include "../macros.hpp"
16#include <algorithm>
17#include <vector>
18#include <iterator>
19
20hi_export_module(hikogui.unicode.unicode_word_break);
21
22
23hi_export namespace hi::inline v1 {
24
25namespace detail {
26
28public:
29 constexpr unicode_word_break_info() noexcept : _value(0) {}
30 constexpr unicode_word_break_info(unicode_word_break_info const&) noexcept = default;
31 constexpr unicode_word_break_info(unicode_word_break_info&&) noexcept = default;
32 constexpr unicode_word_break_info& operator=(unicode_word_break_info const&) noexcept = default;
33 constexpr unicode_word_break_info& operator=(unicode_word_break_info&&) noexcept = default;
34
35 constexpr unicode_word_break_info(unicode_word_break_property const& word_break_property, bool pictographic) noexcept :
36 _value(std::to_underlying(word_break_property) | (wide_cast<uint8_t>(pictographic) << 7))
37 {
38 }
39
40 constexpr unicode_word_break_info& make_skip() noexcept
41 {
42 _value |= 0x40;
43 return *this;
44 }
45
46 [[nodiscard]] constexpr bool is_skip() const noexcept
47 {
48 return to_bool(_value & 0x40);
49 }
50
51 [[nodiscard]] constexpr bool is_pictographic() const noexcept
52 {
53 return to_bool(_value & 0x80);
54 }
55
56 [[nodiscard]] constexpr friend bool
57 operator==(unicode_word_break_info const& lhs, unicode_word_break_property const& rhs) noexcept
58 {
59 return (lhs._value & 0x3f) == std::to_underlying(rhs);
60 }
61
62 [[nodiscard]] constexpr friend bool
63 operator==(unicode_word_break_info const&, unicode_word_break_info const&) noexcept = default;
64
65 [[nodiscard]] constexpr friend bool is_AHLetter(unicode_word_break_info const& rhs) noexcept
66 {
67 return rhs == unicode_word_break_property::ALetter or rhs == unicode_word_break_property::Hebrew_Letter;
68 }
69
70 [[nodiscard]] constexpr friend bool is_MidNumLetQ(unicode_word_break_info const& rhs) noexcept
71 {
72 return rhs == unicode_word_break_property::MidNumLet or rhs == unicode_word_break_property::Single_Quote;
73 }
74
75private:
76 uint8_t _value;
77};
78
79inline void
80unicode_word_break_WB1_WB3d(unicode_break_vector& r, std::vector<unicode_word_break_info>& infos) noexcept
81{
82 using enum unicode_break_opportunity;
83 using enum unicode_word_break_property;
84
85 hi_axiom(r.size() == infos.size() + 1);
86
87 r.front() = yes; // WB1
88 r.back() = yes; // WB2
89
90 for (auto i = 1_uz; i < infos.size(); ++i) {
91 auto const prev = infos[i - 1];
92 auto const next = infos[i];
93
94 r[i] = [&]() {
95 if (prev == CR and next == LF) {
96 return no; // WB3
97 } else if (prev == Newline or prev == CR or prev == LF) {
98 return yes; // WB3a
99 } else if (next == Newline or next == CR or next == LF) {
100 return yes; // WB3b
101 } else if (prev == ZWJ and next.is_pictographic()) {
102 return no; // WB3c
103 } else if (prev == WSegSpace and next == WSegSpace) {
104 return no; // WB3d
105 } else {
106 return unassigned;
107 }
108 }();
109 }
110}
111
112inline void unicode_word_break_WB4(unicode_break_vector& r, std::vector<unicode_word_break_info>& infos) noexcept
113{
114 using enum unicode_break_opportunity;
115 using enum unicode_word_break_property;
116
117 hi_axiom(r.size() == infos.size() + 1);
118
119 for (auto i = 1_uz; i < infos.size(); ++i) {
120 auto const prev = infos[i - 1];
121 auto& next = infos[i];
122
123 if ((prev != Newline and prev != CR and prev != LF) and (next == Extend or next == Format or next == ZWJ)) {
124 if (r[i] == unassigned) {
125 r[i] = no;
126 }
127 next.make_skip();
128 }
129 }
130}
131
132inline void
133unicode_word_break_WB5_WB999(unicode_break_vector& r, std::vector<unicode_word_break_info>& infos) noexcept
134{
135 using enum unicode_break_opportunity;
136 using enum unicode_word_break_property;
137
138 hi_axiom(r.size() == infos.size() + 1);
139
140 for (auto i = 0_uz; i != infos.size(); ++i) {
141 if (r[i] != unassigned) {
142 continue;
143 }
144
145 auto const& next = infos[i];
146
147 // WB4: (Extend | Format | ZWJ)* is assigned to no-break.
148 hi_axiom(not next.is_skip());
149
150 auto prev_i = narrow_cast<ptrdiff_t>(i) - 1;
151 auto prev = unicode_word_break_info{};
152 for (; prev_i >= 0 ; --prev_i) {
153 if (not infos[prev_i].is_skip()) {
154 prev = infos[prev_i];
155 break;
156 }
157 }
158
159 auto prev_prev_i = prev_i - 1;
160 auto prev_prev = unicode_word_break_info{};
161 for (; prev_prev_i >= 0; --prev_prev_i) {
162 if (not infos[prev_prev_i].is_skip()) {
163 prev_prev = infos[prev_prev_i];
164 break;
165 }
166 }
167
168 auto next_next_i = i + 1;
169 auto next_next = unicode_word_break_info{};
170 for (; next_next_i != infos.size(); ++next_next_i) {
171 if (not infos[next_next_i].is_skip()) {
172 next_next = infos[next_next_i];
173 break;
174 }
175 }
176
177 auto RI_i = prev_i - 1;
178 auto RI_is_pair = true;
179 if (prev == Regional_Indicator and next == Regional_Indicator) {
180 // Track back before prev, and count consecutive RI.
181 for (; RI_i >= 0; --RI_i) {
182 if (infos[RI_i].is_skip()) {
183 continue;
184 } else if (infos[RI_i] != Regional_Indicator) {
185 break;
186 }
187 RI_is_pair = not RI_is_pair;
188 }
189 }
190
191 r[i] = [&] {
192 if (is_AHLetter(prev) and is_AHLetter(next)) {
193 return no; // WB5
194 } else if (is_AHLetter(prev) and (next == MidLetter or is_MidNumLetQ(next)) and is_AHLetter(next_next)) {
195 return no; // WB6
196 } else if (is_AHLetter(prev_prev) and (prev == MidLetter or is_MidNumLetQ(prev)) and is_AHLetter(next)) {
197 return no; // WB7
198 } else if (prev == Hebrew_Letter and next == Single_Quote) {
199 return no; // WB7a
200 } else if (prev == Hebrew_Letter and next == Double_Quote and next_next == Hebrew_Letter) {
201 return no; // WB7b
202 } else if (prev_prev == Hebrew_Letter and prev == Double_Quote and next == Hebrew_Letter) {
203 return no; // WB7c
204 } else if (prev == Numeric and next == Numeric) {
205 return no; // WB8
206 } else if (is_AHLetter(prev) and next == Numeric) {
207 return no; // WB9
208 } else if (prev == Numeric and is_AHLetter(next)) {
209 return no; // WB10
210 } else if (prev_prev == Numeric and (prev == MidNum or is_MidNumLetQ(prev)) and next == Numeric) {
211 return no; // WB11
212 } else if (prev == Numeric and (next == MidNum or is_MidNumLetQ(next)) and next_next == Numeric) {
213 return no; // WB12
214 } else if (prev == Katakana and next == Katakana) {
215 return no; // WB13
216 } else if (
217 (is_AHLetter(prev) or prev == Numeric or prev == Katakana or prev == ExtendNumLet) and next == ExtendNumLet) {
218 return no; // WB13a
219 } else if (prev == ExtendNumLet and (is_AHLetter(next) or next == Numeric or next == Katakana)) {
220 return no; // WB13b
221 } else if (prev == Regional_Indicator and next == Regional_Indicator and RI_is_pair) {
222 return no; // WB15 WB16
223 } else {
224 return yes; // WB999
225 }
226 }();
227 }
228}
229
230} // namespace detail
231
239template<typename It, typename ItEnd, typename CodePointFunc>
240[[nodiscard]] inline unicode_break_vector unicode_word_break(It first, ItEnd last, CodePointFunc const& code_point_func) noexcept
241{
242 auto size = narrow_cast<size_t>(std::distance(first, last));
243 auto r = unicode_break_vector{size + 1, unicode_break_opportunity::unassigned};
244
246 infos.reserve(size);
247 std::transform(first, last, std::back_inserter(infos), [&](auto const& item) {
248 auto const code_point = code_point_func(item);
249 auto const word_break_property = ucd_get_word_break_property(code_point);
250 auto const grapheme_cluster_break = ucd_get_grapheme_cluster_break(code_point);
252 word_break_property, grapheme_cluster_break == unicode_grapheme_cluster_break::Extended_Pictographic};
253 });
254
255 detail::unicode_word_break_WB1_WB3d(r, infos);
256 detail::unicode_word_break_WB4(r, infos);
257 detail::unicode_word_break_WB5_WB999(r, infos);
258 return r;
259}
260
276void wrap_lines(auto first, auto last, float max_width, auto get_width, auto get_code_point, auto set_code_point) noexcept
277{
278 using enum unicode_general_category;
279
280 auto it_at_last_space = last;
281 float width_at_last_space = 0.0;
282 float current_width = 0.0;
283
284 for (auto it = first; it != last; ++it) {
285 auto const code_point = get_code_point(*it);
286 auto const general_category = ucd_get_general_category(code_point);
287
288 if (general_category == Zp || general_category == Zl) {
289 // Reset the line on existing line and paragraph separator.
290 it_at_last_space = last;
291 width_at_last_space = 0.0f;
292 current_width = 0.0;
293 continue;
294
295 } else if (general_category == Zs) {
296 // Remember the length of the line at the end of the word.
297 it_at_last_space = it;
298 width_at_last_space = current_width;
299 }
300
301 current_width += get_width(*it);
302 if (current_width >= max_width && it_at_last_space != last) {
303 // The line is too long, replace the last space with a line separator.
304 set_code_point(*it, U'\u2028');
305 it_at_last_space = last;
306 width_at_last_space = 0.0f;
307 current_width = 0.0;
308 continue;
309 }
310 }
311}
312
313} // namespace hi::inline v1
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
void wrap_lines(auto first, auto last, float max_width, auto get_width, auto get_code_point, auto set_code_point) noexcept
Wrap lines in text that are too wide.
Definition unicode_word_break.hpp:276
unicode_break_vector unicode_word_break(It first, ItEnd last, CodePointFunc const &code_point_func) noexcept
The unicode word break algorithm UAX#29.
Definition unicode_word_break.hpp:240
Definition unicode_word_break.hpp:27
T back_inserter(T... args)
T distance(T... args)
T next(T... args)
T prev(T... args)
T reserve(T... args)
T transform(T... args)