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"
20hi_export_module(hikogui.unicode.unicode_word_break);
23hi_export
namespace hi::inline
v1 {
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))
46 [[nodiscard]]
constexpr bool is_skip()
const noexcept
48 return to_bool(_value & 0x40);
51 [[nodiscard]]
constexpr bool is_pictographic()
const noexcept
53 return to_bool(_value & 0x80);
56 [[nodiscard]]
constexpr friend bool
59 return (lhs._value & 0x3f) == std::to_underlying(rhs);
62 [[nodiscard]]
constexpr friend bool
67 return rhs == unicode_word_break_property::ALetter or rhs == unicode_word_break_property::Hebrew_Letter;
72 return rhs == unicode_word_break_property::MidNumLet or rhs == unicode_word_break_property::Single_Quote;
82 using enum unicode_break_opportunity;
83 using enum unicode_word_break_property;
85 hi_axiom(r.size() == infos.size() + 1);
90 for (
auto i = 1_uz; i < infos.size(); ++i) {
91 auto const prev = infos[i - 1];
92 auto const next = infos[i];
95 if (prev == CR and next == LF) {
97 }
else if (prev == Newline or prev == CR or prev == LF) {
99 }
else if (next == Newline or next == CR or next == LF) {
101 }
else if (prev == ZWJ and
next.is_pictographic()) {
103 }
else if (prev == WSegSpace and next == WSegSpace) {
114 using enum unicode_break_opportunity;
115 using enum unicode_word_break_property;
117 hi_axiom(r.size() == infos.size() + 1);
119 for (
auto i = 1_uz; i < infos.size(); ++i) {
120 auto const prev = infos[i - 1];
121 auto&
next = infos[i];
123 if ((prev != Newline and prev != CR and prev != LF) and (next == Extend or next == Format or next == ZWJ)) {
124 if (r[i] == unassigned) {
135 using enum unicode_break_opportunity;
136 using enum unicode_word_break_property;
138 hi_axiom(r.size() == infos.size() + 1);
140 for (
auto i = 0_uz; i != infos.size(); ++i) {
141 if (r[i] != unassigned) {
145 auto const&
next = infos[i];
148 hi_axiom(not
next.is_skip());
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];
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];
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];
177 auto RI_i = prev_i - 1;
178 auto RI_is_pair =
true;
179 if (prev == Regional_Indicator and next == Regional_Indicator) {
181 for (; RI_i >= 0; --RI_i) {
182 if (infos[RI_i].is_skip()) {
184 }
else if (infos[RI_i] != Regional_Indicator) {
187 RI_is_pair = not RI_is_pair;
192 if (is_AHLetter(prev) and is_AHLetter(next)) {
194 }
else if (is_AHLetter(prev) and (next == MidLetter or is_MidNumLetQ(next)) and is_AHLetter(next_next)) {
196 }
else if (is_AHLetter(prev_prev) and (prev == MidLetter or is_MidNumLetQ(prev)) and is_AHLetter(next)) {
198 }
else if (prev == Hebrew_Letter and next == Single_Quote) {
200 }
else if (prev == Hebrew_Letter and next == Double_Quote and next_next == Hebrew_Letter) {
202 }
else if (prev_prev == Hebrew_Letter and prev == Double_Quote and next == Hebrew_Letter) {
204 }
else if (prev == Numeric and next == Numeric) {
206 }
else if (is_AHLetter(prev) and next == Numeric) {
208 }
else if (prev == Numeric and is_AHLetter(next)) {
210 }
else if (prev_prev == Numeric and (prev == MidNum or is_MidNumLetQ(prev)) and next == Numeric) {
212 }
else if (prev == Numeric and (next == MidNum or is_MidNumLetQ(next)) and next_next == Numeric) {
214 }
else if (prev == Katakana and next == Katakana) {
217 (is_AHLetter(prev) or prev == Numeric or prev == Katakana or prev == ExtendNumLet) and next == ExtendNumLet) {
219 }
else if (prev == ExtendNumLet and (is_AHLetter(next) or next == Numeric or next == Katakana)) {
221 }
else if (prev == Regional_Indicator and next == Regional_Indicator and RI_is_pair) {
239template<
typename It,
typename ItEnd,
typename CodePo
intFunc>
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};
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);
276void wrap_lines(
auto first,
auto last,
float max_width,
auto get_width,
auto get_code_point,
auto set_code_point)
noexcept
278 using enum unicode_general_category;
280 auto it_at_last_space = last;
281 float width_at_last_space = 0.0;
282 float current_width = 0.0;
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);
288 if (general_category == Zp || general_category == Zl) {
290 it_at_last_space = last;
291 width_at_last_space = 0.0f;
295 }
else if (general_category == Zs) {
297 it_at_last_space = it;
298 width_at_last_space = current_width;
301 current_width += get_width(*it);
302 if (current_width >= max_width && it_at_last_space != last) {
304 set_code_point(*it, U
'\u2028');
305 it_at_last_space = last;
306 width_at_last_space = 0.0f;
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)