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/utility.hpp"
16#include "../macros.hpp"
30 unicode_line_break_class original_class = unicode_line_break_class::XX;
31 unicode_line_break_class current_class = unicode_line_break_class::XX;
32 bool is_extended_pictographic =
false;
34 unicode_east_asian_width east_asian_width = unicode_east_asian_width::A;
43 unicode_line_break_class break_class,
45 bool is_extended_pictographic,
46 unicode_east_asian_width east_asian_width) noexcept :
47 original_class(break_class),
48 current_class(break_class),
50 is_extended_pictographic(is_extended_pictographic),
51 east_asian_width(east_asian_width)
55 constexpr explicit operator unicode_line_break_class()
const noexcept
66 [[nodiscard]]
constexpr bool operator==(unicode_line_break_class rhs)
const noexcept
68 return current_class == rhs;
71 [[nodiscard]]
constexpr bool operator==(unicode_east_asian_width rhs)
const noexcept
73 return east_asian_width == rhs;
78using unicode_line_break_info_iterator = unicode_line_break_info_vector::iterator;
79using unicode_line_break_info_const_iterator = unicode_line_break_info_vector::const_iterator;
81template<
typename It,
typename ItEnd,
typename CodePo
intFunc>
83unicode_LB1(It first, ItEnd last, CodePointFunc
const& code_point_func)
noexcept
88 for (
auto it = first; it != last; ++it) {
89 hilet code_point = code_point_func(*it);
90 hilet east_asian_width = ucd_get_east_asian_width(code_point);
91 hilet break_class = ucd_get_line_break_class(code_point);
92 hilet general_category = ucd_get_general_category(code_point);
93 hilet grapheme_cluster_break = ucd_get_grapheme_cluster_break(code_point);
95 hilet resolved_break_class = [&]() {
96 switch (break_class) {
97 using enum unicode_line_break_class;
105 return is_Mn_or_Mc(general_category) ? CM : AL;
112 resolved_break_class,
113 general_category == unicode_general_category::Cn,
114 grapheme_cluster_break == unicode_grapheme_cluster_break::Extended_Pictographic,
121[[nodiscard]]
constexpr void unicode_LB2_3(unicode_break_vector& opportunities)
noexcept
123 hi_axiom(not opportunities.empty());
125 opportunities.front() = unicode_break_opportunity::no;
127 opportunities.back() = unicode_break_opportunity::mandatory;
130template<
typename MatchFunc>
131constexpr void unicode_LB_walk(
132 unicode_break_vector& opportunities,
134 MatchFunc match_func)
noexcept
142 auto cur = infos.begin();
143 hilet last = infos.end() - 1;
144 hilet last2 = infos.end();
145 auto opportunity = opportunities.begin() + 1;
147 auto cur_sp_class = XX;
148 auto cur_nu_class = XX;
149 auto prev_class = XX;
151 while (cur != last) {
152 hilet
next = cur + 1;
157 if (cur_class != SP) {
158 cur_sp_class = cur_class;
162 if (cur_nu_class == CL) {
165 }
else if (cur_nu_class == NU) {
166 if (cur_class == CL or cur_class == CP) {
168 }
else if (cur_class != NU and cur_class != SY and cur_class != IS) {
171 }
else if (cur_class == NU) {
176 if (cur->original_class == RI) {
178 }
else if (*cur != RI) {
182 if (*opportunity == unicode_break_opportunity::unassigned) {
183 *opportunity = match_func(prev_class, cur, next, next2_class, cur_sp_class, cur_nu_class, num_ri);
186 prev_class = cur_class;
195 opportunities, infos, [](hilet prev, hilet cur, hilet next, hilet next2, hilet cur_sp, hilet cur_nu, hilet num_ri) {
196 using enum unicode_break_opportunity;
200 }
else if (*cur == CR and *next == LF) {
202 }
else if (*cur == CR or *cur == LF or *cur == NL) {
204 }
else if (*next == BK or *next == CR or *next == LF or *next == NL) {
206 }
else if (*next == SP or *next == ZW) {
208 }
else if (cur_sp == ZW) {
210 }
else if (*cur == ZWJ) {
221 using enum unicode_break_opportunity;
227 auto cur = infos.begin();
228 hilet last = infos.end() - 1;
229 auto opportunity = opportunities.begin() + 1;
232 while (cur != last) {
233 hilet
next = cur + 1;
235 if ((*cur == CM or *cur == ZWJ) and X != XX) {
243 if ((*cur != BK and *cur != CR and *cur != LF and *cur != NL and *cur != SP and *cur != ZW) and
244 (*next == CM or *next == ZWJ)) {
263 for (
auto& x : infos) {
264 if (x == CM or x == ZWJ) {
273 opportunities, infos, [&](hilet prev, hilet cur, hilet next, hilet next2, hilet cur_sp, hilet cur_nu, hilet num_ri) {
274 using enum unicode_break_opportunity;
276 using enum unicode_east_asian_width;
278 if (*cur == WJ or *next == WJ) {
280 }
else if (*cur == GL) {
282 }
else if (*cur != SP and *cur != BA and *cur != HY and *next == GL) {
284 }
else if (*next == CL or *next == CP or *next == EX or *next == IS or *next == SY) {
286 }
else if (cur_sp == OP) {
288 }
else if (cur_sp == QU and *next == OP) {
290 }
else if ((cur_sp == CL or cur_sp == CP) and *next == NS) {
292 }
else if (cur_sp == B2 and *next == B2) {
294 }
else if (*cur == SP) {
296 }
else if (*cur == QU or *next == QU) {
298 }
else if (*cur == CB or *next == CB) {
300 }
else if (*cur == BB or *next == BA or *next == HY or *next == NS) {
302 }
else if (prev == HL and (*cur == HY or *cur == BA)) {
304 }
else if (*cur == SY and *next == HL) {
306 }
else if (*next == IN) {
308 }
else if ((*cur == AL or *cur == HL) and *next == NU) {
310 }
else if (*cur == NU and (*next == AL or *next == HL)) {
312 }
else if (*cur == PR and (*next == ID or *next == EB or *next == EM)) {
314 }
else if ((*cur == ID or *cur == EB or *cur == EM) and *next == PO) {
316 }
else if ((*cur == PR or *cur == PO) and (*next == AL or *next == HL)) {
318 }
else if ((*cur == AL or *cur == HL) and (*next == PR or *next == PO)) {
321 (*cur == PR or *cur == PO) and ((*next == OP and next2 == NU) or (*next == HY and next2 == NU) or *next == NU)) {
323 }
else if ((*cur == OP or *cur == HY) and *next == NU) {
325 }
else if (*cur == NU and (*next == NU or *next == SY or *next == IS)) {
327 }
else if (cur_nu == NU and (*next == NU or *next == SY or *next == IS or *next == CL or *next == CP)) {
329 }
else if ((cur_nu == NU or cur_nu == CL) and (*next == PO or *next == PR)) {
331 }
else if (*cur == JL and (*next == JL or *next == JV or *next == H2 or *next == H3)) {
333 }
else if ((*cur == JV or *cur == H2) and (*next == JV or *next == JT)) {
335 }
else if ((*cur == JT or *cur == H3) and *next == JT) {
337 }
else if ((*cur == JL or *cur == JV or *cur == JT or *cur == H2 or *cur == H3) and *next == PO) {
339 }
else if (*cur == PR and (*next == JL or *next == JV or *next == JT or *next == H2 or *next == H3)) {
341 }
else if ((*cur == AL or *cur == HL) and (*next == AL or *next == HL)) {
343 }
else if (*cur == IS and (*next == AL or *next == HL)) {
345 }
else if ((*cur == AL or *cur == HL or *cur == NU) and (*next == OP and *next != F and *next != W and *next != H)) {
347 }
else if ((*cur == CP and *cur != F and *cur != W and *cur != H) and (*next == AL or *next == HL or *next == NU)) {
349 }
else if (*cur == RI and *next == RI and (num_ri % 2) == 1) {
351 }
else if (*cur == EB and *next == EM) {
353 }
else if (cur->is_extended_pictographic and cur->is_Cn and *next == EM) {
367[[nodiscard]]
constexpr float
368unicode_LB_width(std::vector<float>::const_iterator first, std::vector<float>::const_iterator last)
noexcept
374 auto rfirst = std::make_reverse_iterator(last);
375 auto rlast = std::make_reverse_iterator(first);
377 auto it =
std::find_if(rfirst, rlast, [](hilet& width) {
381 return acc + abs(width);
393 auto max_width = 0.0f;
394 auto it = widths.
begin();
395 for (
auto length : lengths) {
409[[nodiscard]]
constexpr bool
412 auto it = widths.begin();
413 for (
auto length : lengths) {
431 for (
auto it = opportunities.begin() + 1; it != opportunities.end(); ++it) {
433 if (*it == unicode_break_opportunity::mandatory) {
451 for (
auto it = opportunities.begin() + 1; it != opportunities.end(); ++it) {
453 if (*it != unicode_break_opportunity::no) {
462[[nodiscard]]
constexpr unicode_break_const_iterator unicode_LB_fast_fit_line(
463 unicode_break_const_iterator opportunity_it,
464 std::vector<float>::const_iterator width_it,
465 float maximum_line_width)
noexcept
467 using enum unicode_break_opportunity;
470 auto end_of_line = opportunity_it;
472 width += abs(*width_it);
473 if (width > maximum_line_width) {
477 }
else if (*opportunity_it == mandatory) {
479 return opportunity_it;
481 }
else if (*opportunity_it == yes) {
483 end_of_line = opportunity_it;
492[[nodiscard]]
constexpr unicode_break_const_iterator unicode_LB_slow_fit_line(
493 unicode_break_const_iterator first,
494 unicode_break_const_iterator end_of_line,
495 std::vector<float>::const_iterator first_width,
496 float maximum_line_width)
noexcept
498 using enum unicode_break_opportunity;
501 auto it = end_of_line;
504 hilet line_width =
unicode_LB_width(first_width, first_width + num_characters);
506 if (line_width <= maximum_line_width) {
507 if (*it == mandatory) {
511 }
else if (*it == yes) {
525[[nodiscard]]
constexpr unicode_break_const_iterator
526unicode_LB_finish_fit_line(unicode_break_const_iterator first, unicode_break_const_iterator end_of_line)
noexcept
528 if (first == end_of_line) {
530 while (*end_of_line == unicode_break_opportunity::no) {
536 return end_of_line + 1;
546 float maximum_line_width)
noexcept
548 using enum unicode_break_opportunity;
551 if (widths.empty()) {
555 auto opportunity_it = opportunities.
begin() + 1;
556 auto width_it = widths.begin();
557 while (width_it != widths.end()) {
559 auto opportunity_eol = unicode_LB_fast_fit_line(opportunity_it, width_it, maximum_line_width);
560 opportunity_eol = unicode_LB_slow_fit_line(opportunity_it, opportunity_eol, width_it, maximum_line_width);
561 opportunity_eol = unicode_LB_finish_fit_line(opportunity_it, opportunity_eol);
563 hilet num_characters =
std::distance(opportunity_it, opportunity_eol);
564 r.push_back(num_characters);
565 opportunity_it += num_characters;
566 width_it += num_characters;
583 auto line_lengths = detail::unicode_LB_mandatory_lines(opportunities);
584 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
599 auto line_lengths = detail::unicode_LB_optional_lines(opportunities);
600 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
616 auto line_lengths = detail::unicode_LB_fit_lines(opportunities, char_widths, maximum_line_width);
617 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
630template<
typename It,
typename ItEnd,
typename CodePo
intFunc>
638 detail::unicode_LB2_3(r);
639 detail::unicode_LB4_8a(r,
infos);
640 detail::unicode_LB9(r,
infos);
641 detail::unicode_LB10(
infos);
642 detail::unicode_LB11_31(r,
infos);
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:368
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:426
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:446
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:597
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:581
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:543
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:410
DOXYGEN BUG.
Definition algorithm.hpp:16
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:632
unicode_line_break_class
Unicode line break class.
Definition ucd_line_break_classes.hpp:1012
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
Combined unicode_line_break_class and unicode_line_break_opportunity.
Definition unicode_line_break.hpp:29