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/module.hpp"
18namespace hi::inline
v1 {
30 constexpr unicode_word_break_info(unicode_word_break_property
const& word_break_property,
bool pictographic) noexcept :
31 _value(to_underlying(word_break_property) | (wide_cast<uint8_t>(pictographic) << 7))
41 [[nodiscard]]
constexpr bool is_skip()
const noexcept
43 return to_bool(_value & 0x40);
46 [[nodiscard]]
constexpr bool is_pictographic()
const noexcept
48 return to_bool(_value & 0x80);
51 [[nodiscard]]
constexpr friend bool
54 return (lhs._value & 0x3f) == to_underlying(rhs);
57 [[nodiscard]]
constexpr friend bool
62 return rhs == unicode_word_break_property::ALetter or rhs == unicode_word_break_property::Hebrew_Letter;
67 return rhs == unicode_word_break_property::MidNumLet or rhs == unicode_word_break_property::Single_Quote;
74[[nodiscard]]
inline void
77 using enum unicode_break_opportunity;
78 using enum unicode_word_break_property;
80 hi_axiom(r.size() == infos.size() + 1);
85 for (
auto i = 1_uz; i < infos.size(); ++i) {
86 hilet prev = infos[i - 1];
87 hilet next = infos[i];
90 if (prev == CR and next == LF) {
92 }
else if (prev == Newline or prev == CR or prev == LF) {
94 }
else if (next == Newline or next == CR or next == LF) {
96 }
else if (prev == ZWJ and
next.is_pictographic()) {
98 }
else if (prev == WSegSpace and next == WSegSpace) {
109 using enum unicode_break_opportunity;
110 using enum unicode_word_break_property;
112 hi_axiom(r.size() == infos.size() + 1);
114 for (
auto i = 1_uz; i < infos.size(); ++i) {
116 auto&
next = infos[i];
118 if ((prev != Newline and prev != CR and prev != LF) and (next == Extend or next == Format or next == ZWJ)) {
119 if (r[i] == unassigned) {
127[[nodiscard]]
inline void
130 using enum unicode_break_opportunity;
131 using enum unicode_word_break_property;
133 hi_axiom(r.size() == infos.size() + 1);
135 for (
auto i = 0_uz; i != infos.size(); ++i) {
136 if (r[i] != unassigned) {
145 auto prev_i = narrow_cast<ptrdiff_t>(i) - 1;
146 auto prev = unicode_word_break_info{};
147 for (; prev_i >= 0 ; --prev_i) {
148 if (not infos[prev_i].is_skip()) {
149 prev = infos[prev_i];
154 auto prev_prev_i = prev_i - 1;
155 auto prev_prev = unicode_word_break_info{};
156 for (; prev_prev_i >= 0; --prev_prev_i) {
157 if (not infos[prev_prev_i].is_skip()) {
158 prev_prev = infos[prev_prev_i];
163 auto next_next_i = i + 1;
164 auto next_next = unicode_word_break_info{};
165 for (; next_next_i != infos.size(); ++next_next_i) {
166 if (not infos[next_next_i].is_skip()) {
167 next_next = infos[next_next_i];
172 auto RI_i = prev_i - 1;
173 auto RI_is_pair =
true;
174 if (prev == Regional_Indicator and next == Regional_Indicator) {
176 for (; RI_i >= 0; --RI_i) {
177 if (infos[RI_i].is_skip()) {
179 }
else if (infos[RI_i] != Regional_Indicator) {
182 RI_is_pair = not RI_is_pair;
187 if (is_AHLetter(prev) and is_AHLetter(next)) {
189 }
else if (is_AHLetter(prev) and (next == MidLetter or is_MidNumLetQ(next)) and is_AHLetter(next_next)) {
191 }
else if (is_AHLetter(prev_prev) and (prev == MidLetter or is_MidNumLetQ(prev)) and is_AHLetter(next)) {
193 }
else if (prev == Hebrew_Letter and next == Single_Quote) {
195 }
else if (prev == Hebrew_Letter and next == Double_Quote and next_next == Hebrew_Letter) {
197 }
else if (prev_prev == Hebrew_Letter and prev == Double_Quote and next == Hebrew_Letter) {
199 }
else if (prev == Numeric and next == Numeric) {
201 }
else if (is_AHLetter(prev) and next == Numeric) {
203 }
else if (prev == Numeric and is_AHLetter(next)) {
205 }
else if (prev_prev == Numeric and (prev == MidNum or is_MidNumLetQ(prev)) and next == Numeric) {
207 }
else if (prev == Numeric and (next == MidNum or is_MidNumLetQ(next)) and next_next == Numeric) {
209 }
else if (prev == Katakana and next == Katakana) {
212 (is_AHLetter(prev) or prev == Numeric or prev == Katakana or prev == ExtendNumLet) and next == ExtendNumLet) {
214 }
else if (prev == ExtendNumLet and (is_AHLetter(next) or next == Numeric or next == Katakana)) {
216 }
else if (prev == Regional_Indicator and next == Regional_Indicator and RI_is_pair) {
234template<
typename It,
typename ItEnd,
typename CodePo
intFunc>
243 hilet code_point = code_point_func(item);
244 hilet word_break_property = ucd_get_word_break_property(code_point);
245 hilet grapheme_cluster_break = ucd_get_grapheme_cluster_break(code_point);
247 word_break_property, grapheme_cluster_break == unicode_grapheme_cluster_break::Extended_Pictographic};
250 detail::unicode_word_break_WB1_WB3d(r, infos);
251 detail::unicode_word_break_WB4(r, infos);
252 detail::unicode_word_break_WB5_WB999(r, infos);
271void wrap_lines(
auto first,
auto last,
float max_width,
auto get_width,
auto get_code_point,
auto set_code_point)
noexcept
273 using enum unicode_general_category;
275 auto it_at_last_space = last;
276 float width_at_last_space = 0.0;
277 float current_width = 0.0;
279 for (
auto it = first; it != last; ++it) {
280 hilet code_point = get_code_point(*it);
281 hilet general_category = ucd_get_general_category(code_point);
283 if (general_category == Zp || general_category == Zl) {
285 it_at_last_space = last;
286 width_at_last_space = 0.0f;
290 }
else if (general_category == Zs) {
292 it_at_last_space = it;
293 width_at_last_space = current_width;
296 current_width += get_width(*it);
297 if (current_width >= max_width && it_at_last_space != last) {
299 set_code_point(*it, U
'\u2028');
300 it_at_last_space = last;
301 width_at_last_space = 0.0f;
#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
DOXYGEN BUG.
Definition algorithm.hpp:13
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:271
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:235
Definition unicode_word_break.hpp:22
T back_inserter(T... args)