10#include "unicode_break_opportunity.hpp"
11#include "ucd_general_categories.hpp"
12#include "ucd_grapheme_cluster_breaks.hpp"
13#include "ucd_line_break_classes.hpp"
14#include "ucd_east_asian_widths.hpp"
15#include "../utility/module.hpp"
21namespace hi::inline
v1 {
27 unicode_line_break_class original_class = unicode_line_break_class::XX;
28 unicode_line_break_class current_class = unicode_line_break_class::XX;
29 bool is_extended_pictographic =
false;
31 unicode_east_asian_width east_asian_width = unicode_east_asian_width::A;
40 unicode_line_break_class break_class,
42 bool is_extended_pictographic,
43 unicode_east_asian_width east_asian_width) noexcept :
44 original_class(break_class),
45 current_class(break_class),
47 is_extended_pictographic(is_extended_pictographic),
48 east_asian_width(east_asian_width)
52 constexpr explicit operator unicode_line_break_class()
const noexcept
63 [[nodiscard]]
constexpr bool operator==(unicode_line_break_class rhs)
const noexcept
65 return current_class == rhs;
68 [[nodiscard]]
constexpr bool operator==(unicode_east_asian_width rhs)
const noexcept
70 return east_asian_width == rhs;
75using unicode_line_break_info_iterator = unicode_line_break_info_vector::iterator;
76using unicode_line_break_info_const_iterator = unicode_line_break_info_vector::const_iterator;
78template<
typename It,
typename ItEnd,
typename CodePo
intFunc>
80unicode_LB1(It first, ItEnd last, CodePointFunc
const& code_point_func)
noexcept
85 for (
auto it = first; it != last; ++it) {
86 hilet code_point = code_point_func(*it);
87 hilet east_asian_width = ucd_get_east_asian_width(code_point);
88 hilet break_class = ucd_get_line_break_class(code_point);
89 hilet general_category = ucd_get_general_category(code_point);
90 hilet grapheme_cluster_break = ucd_get_grapheme_cluster_break(code_point);
92 hilet resolved_break_class = [&]() {
93 switch (break_class) {
94 using enum unicode_line_break_class;
102 return is_Mn_or_Mc(general_category) ? CM : AL;
109 resolved_break_class,
110 general_category == unicode_general_category::Cn,
111 grapheme_cluster_break == unicode_grapheme_cluster_break::Extended_Pictographic,
118[[nodiscard]]
constexpr void unicode_LB2_3(unicode_break_vector& opportunities)
noexcept
120 hi_axiom(not opportunities.empty());
122 opportunities.front() = unicode_break_opportunity::no;
124 opportunities.back() = unicode_break_opportunity::mandatory;
127template<
typename MatchFunc>
128constexpr void unicode_LB_walk(
129 unicode_break_vector& opportunities,
131 MatchFunc match_func)
noexcept
139 auto cur = infos.begin();
140 hilet last = infos.end() - 1;
141 hilet last2 = infos.end();
142 auto opportunity = opportunities.begin() + 1;
144 auto cur_sp_class = XX;
145 auto cur_nu_class = XX;
146 auto prev_class = XX;
148 while (cur != last) {
154 if (cur_class != SP) {
155 cur_sp_class = cur_class;
159 if (cur_nu_class == CL) {
162 }
else if (cur_nu_class == NU) {
163 if (cur_class == CL or cur_class == CP) {
165 }
else if (cur_class != NU and cur_class != SY and cur_class != IS) {
168 }
else if (cur_class == NU) {
173 if (cur->original_class == RI) {
175 }
else if (*cur != RI) {
179 if (*opportunity == unicode_break_opportunity::unassigned) {
180 *opportunity = match_func(prev_class, cur, next, next2_class, cur_sp_class, cur_nu_class, num_ri);
183 prev_class = cur_class;
193 using enum unicode_break_opportunity;
197 }
else if (*cur == CR and *next == LF) {
199 }
else if (*cur == CR or *cur == LF or *cur == NL) {
201 }
else if (*next == BK or *next == CR or *next == LF or *next == NL) {
203 }
else if (*next == SP or *next == ZW) {
205 }
else if (cur_sp == ZW) {
207 }
else if (*cur == ZWJ) {
218 using enum unicode_break_opportunity;
224 auto cur = infos.begin();
225 hilet last = infos.end() - 1;
226 auto opportunity = opportunities.begin() + 1;
229 while (cur != last) {
232 if ((*cur == CM or *cur == ZWJ) and X != XX) {
240 if ((*cur != BK and *cur != CR and *cur != LF and *cur != NL and *cur != SP and *cur != ZW) and
241 (*next == CM or *next == ZWJ)) {
260 for (
auto& x : infos) {
261 if (x == CM or x == ZWJ) {
271 using enum unicode_break_opportunity;
273 using enum unicode_east_asian_width;
275 if (*cur == WJ or *next == WJ) {
277 }
else if (*cur == GL) {
279 }
else if (*cur != SP and *cur != BA and *cur != HY and *next == GL) {
281 }
else if (*next == CL or *next == CP or *next == EX or *next == IS or *next == SY) {
283 }
else if (cur_sp == OP) {
285 }
else if (cur_sp == QU and *next == OP) {
287 }
else if ((cur_sp == CL or cur_sp == CP) and *next == NS) {
289 }
else if (cur_sp == B2 and *next == B2) {
291 }
else if (*cur == SP) {
293 }
else if (*cur == QU or *next == QU) {
295 }
else if (*cur == CB or *next == CB) {
297 }
else if (*cur == BB or *next == BA or *next == HY or *next == NS) {
299 }
else if (prev == HL and (*cur == HY or *cur == BA)) {
301 }
else if (*cur == SY and *next == HL) {
303 }
else if (*next == IN) {
305 }
else if ((*cur == AL or *cur == HL) and *next == NU) {
307 }
else if (*cur == NU and (*next == AL or *next == HL)) {
309 }
else if (*cur == PR and (*next == ID or *next == EB or *next == EM)) {
311 }
else if ((*cur == ID or *cur == EB or *cur == EM) and *next == PO) {
313 }
else if ((*cur == PR or *cur == PO) and (*next == AL or *next == HL)) {
315 }
else if ((*cur == AL or *cur == HL) and (*next == PR or *next == PO)) {
318 (*cur == PR or *cur == PO) and ((*next == OP and next2 == NU) or (*next == HY and next2 == NU) or *next == NU)) {
320 }
else if ((*cur == OP or *cur == HY) and *next == NU) {
322 }
else if (*cur == NU and (*next == NU or *next == SY or *next == IS)) {
324 }
else if (cur_nu == NU and (*next == NU or *next == SY or *next == IS or *next == CL or *next == CP)) {
326 }
else if ((cur_nu == NU or cur_nu == CL) and (*next == PO or *next == PR)) {
328 }
else if (*cur == JL and (*next == JL or *next == JV or *next == H2 or *next == H3)) {
330 }
else if ((*cur == JV or *cur == H2) and (*next == JV or *next == JT)) {
332 }
else if ((*cur == JT or *cur == H3) and *next == JT) {
334 }
else if ((*cur == JL or *cur == JV or *cur == JT or *cur == H2 or *cur == H3) and *next == PO) {
336 }
else if (*cur == PR and (*next == JL or *next == JV or *next == JT or *next == H2 or *next == H3)) {
338 }
else if ((*cur == AL or *cur == HL) and (*next == AL or *next == HL)) {
340 }
else if (*cur == IS and (*next == AL or *next == HL)) {
342 }
else if ((*cur == AL or *cur == HL or *cur == NU) and (*next == OP and *next != F and *next != W and *next != H)) {
344 }
else if ((*cur == CP and *cur != F and *cur != W and *cur != H) and (*next == AL or *next == HL or *next == NU)) {
346 }
else if (*cur == RI and *next == RI and (num_ri % 2) == 1) {
348 }
else if (*cur == EB and *next == EM) {
350 }
else if (cur->is_extended_pictographic and cur->is_Cn and *next == EM) {
364[[nodiscard]]
constexpr float
365unicode_LB_width(std::vector<float>::const_iterator first, std::vector<float>::const_iterator last)
noexcept
371 auto rfirst = std::make_reverse_iterator(last);
372 auto rlast = std::make_reverse_iterator(first);
378 return acc + abs(width);
390 auto max_width = 0.0f;
391 auto it = widths.
begin();
392 for (
auto length : lengths) {
406[[nodiscard]]
constexpr bool
409 auto it = widths.begin();
410 for (
auto length : lengths) {
428 for (
auto it = opportunities.begin() + 1; it != opportunities.end(); ++it) {
430 if (*it == unicode_break_opportunity::mandatory) {
448 for (
auto it = opportunities.begin() + 1; it != opportunities.end(); ++it) {
450 if (*it != unicode_break_opportunity::no) {
459[[nodiscard]]
constexpr unicode_break_const_iterator unicode_LB_fast_fit_line(
460 unicode_break_const_iterator opportunity_it,
461 std::vector<float>::const_iterator width_it,
462 float maximum_line_width)
noexcept
464 using enum unicode_break_opportunity;
467 auto end_of_line = opportunity_it;
469 width += abs(*width_it);
470 if (width > maximum_line_width) {
474 }
else if (*opportunity_it == mandatory) {
476 return opportunity_it;
478 }
else if (*opportunity_it == yes) {
480 end_of_line = opportunity_it;
489[[nodiscard]]
constexpr unicode_break_const_iterator unicode_LB_slow_fit_line(
490 unicode_break_const_iterator first,
491 unicode_break_const_iterator end_of_line,
492 std::vector<float>::const_iterator first_width,
493 float maximum_line_width)
noexcept
495 using enum unicode_break_opportunity;
498 auto it = end_of_line;
503 if (line_width <= maximum_line_width) {
504 if (*it == mandatory) {
508 }
else if (*it == yes) {
522[[nodiscard]]
constexpr unicode_break_const_iterator
523unicode_LB_finish_fit_line(unicode_break_const_iterator first, unicode_break_const_iterator end_of_line)
noexcept
525 if (first == end_of_line) {
527 while (*end_of_line == unicode_break_opportunity::no) {
533 return end_of_line + 1;
543 float maximum_line_width)
noexcept
545 using enum unicode_break_opportunity;
548 if (widths.empty()) {
552 auto opportunity_it = opportunities.
begin() + 1;
553 auto width_it = widths.begin();
554 while (width_it != widths.end()) {
556 auto opportunity_eol = unicode_LB_fast_fit_line(opportunity_it, width_it, maximum_line_width);
557 opportunity_eol = unicode_LB_slow_fit_line(opportunity_it, opportunity_eol, width_it, maximum_line_width);
558 opportunity_eol = unicode_LB_finish_fit_line(opportunity_it, opportunity_eol);
561 r.push_back(num_characters);
562 opportunity_it += num_characters;
563 width_it += num_characters;
580 auto line_lengths = detail::unicode_LB_mandatory_lines(opportunities);
581 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
596 auto line_lengths = detail::unicode_LB_optional_lines(opportunities);
597 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
613 auto line_lengths = detail::unicode_LB_fit_lines(opportunities, char_widths, maximum_line_width);
614 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
627template<
typename It,
typename ItEnd,
typename CodePo
intFunc>
628[[nodiscard]]
inline unicode_break_vector
634 auto infos = detail::unicode_LB1(first, last, code_point_func);
635 detail::unicode_LB2_3(r);
636 detail::unicode_LB4_8a(r, infos);
637 detail::unicode_LB9(r, infos);
638 detail::unicode_LB10(infos);
639 detail::unicode_LB11_31(r, infos);
654 auto r = detail::unicode_LB_mandatory_lines(opportunities);
655 if (detail::unicode_LB_width_check(widths, r, maximum_line_width)) {
659 r = detail::unicode_LB_fit_lines(opportunities, widths, maximum_line_width);
660 hi_axiom(detail::unicode_LB_width_check(widths, r, maximum_line_width));
constexpr float unicode_LB_width(std::vector< float >::const_iterator first, std::vector< float >::const_iterator last) noexcept
Calculate the width of a line.
Definition unicode_line_break.hpp:365
constexpr std::vector< size_t > unicode_LB_mandatory_lines(unicode_break_vector const &opportunities) noexcept
Get the length of each line when broken with mandatory breaks.
Definition unicode_line_break.hpp:423
constexpr std::vector< size_t > unicode_LB_optional_lines(unicode_break_vector const &opportunities) noexcept
Get the length of each line when broken with mandatory and optional breaks.
Definition unicode_line_break.hpp:443
constexpr std::pair< float, std::vector< size_t > > unicode_LB_minimum_width(unicode_break_vector const &opportunities, std::vector< float > const &char_widths)
Get the minimum width of the text.
Definition unicode_line_break.hpp:594
constexpr std::pair< float, std::vector< size_t > > unicode_LB_maximum_width(unicode_break_vector const &opportunities, std::vector< float > const &char_widths)
Get the maximum width of the text.
Definition unicode_line_break.hpp:578
constexpr std::vector< size_t > unicode_LB_fit_lines(unicode_break_vector const &opportunities, std::vector< float > const &widths, float maximum_line_width) noexcept
Get the length of each line when broken after folding text to a maximum width.
Definition unicode_line_break.hpp:540
constexpr bool unicode_LB_width_check(std::vector< float > const &widths, std::vector< size_t > const &lengths, float maximum_line_width) noexcept
Check if all the lines in the text fit the maximum width.
Definition unicode_line_break.hpp:407
#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
unicode_break_vector unicode_line_break(It first, ItEnd last, CodePointFunc const &code_point_func) noexcept
The unicode line break algorithm UAX #14.
Definition unicode_line_break.hpp:629
unicode_line_break_class
Unicode line break class.
Definition ucd_line_break_classes.hpp:1012
Combined unicode_line_break_class and unicode_line_break_opportunity.
Definition unicode_line_break.hpp:26