10#include "unicode_general_category.hpp"
11#include "unicode_grapheme_cluster_break.hpp"
13#include "unicode_break_opportunity.hpp"
14#include "../utility/module.hpp"
25namespace hi::inline
v1 {
98 bool is_extended_pictographic =
false;
100 unicode_east_asian_width east_asian_width = unicode_east_asian_width::A;
111 bool is_extended_pictographic,
112 unicode_east_asian_width east_asian_width) noexcept :
113 original_class(break_class),
114 current_class(break_class),
116 is_extended_pictographic(is_extended_pictographic),
117 east_asian_width(east_asian_width)
123 return current_class;
134 return current_class == rhs;
137 [[nodiscard]]
constexpr bool operator==(unicode_east_asian_width rhs)
const noexcept
139 return east_asian_width == rhs;
144using unicode_line_break_info_iterator = unicode_line_break_info_vector::iterator;
145using unicode_line_break_info_const_iterator = unicode_line_break_info_vector::const_iterator;
147template<
typename It,
typename ItEnd,
typename DescriptionFunc>
149unicode_LB1(It first, ItEnd last, DescriptionFunc
const& description_func)
noexcept
154 for (
auto it = first; it != last; ++it) {
155 hilet& description = description_func(*it);
156 hilet break_class = description.line_break_class();
157 hilet general_category = description.general_category();
159 hilet resolved_break_class = [&]() {
160 switch (break_class) {
169 return is_Mn_or_Mc(general_category) ? CM :
AL;
176 resolved_break_class,
177 general_category == unicode_general_category::Cn,
178 description.grapheme_cluster_break() == unicode_grapheme_cluster_break::Extended_Pictographic,
179 description.east_asian_width());
185[[nodiscard]]
constexpr void unicode_LB2_3(unicode_break_vector& opportunities)
noexcept
187 hi_axiom(not opportunities.empty());
189 opportunities.front() = unicode_break_opportunity::no;
191 opportunities.back() = unicode_break_opportunity::mandatory;
194template<
typename MatchFunc>
195constexpr void unicode_LB_walk(
196 unicode_break_vector& opportunities,
198 MatchFunc match_func)
noexcept
206 auto cur = infos.begin();
207 hilet last = infos.end() - 1;
208 hilet last2 = infos.end();
209 auto opportunity = opportunities.begin() + 1;
211 auto cur_sp_class = XX;
212 auto cur_nu_class = XX;
213 auto prev_class = XX;
215 while (cur != last) {
221 if (cur_class != SP) {
222 cur_sp_class = cur_class;
226 if (cur_nu_class == CL) {
229 }
else if (cur_nu_class == NU) {
230 if (cur_class == CL or cur_class == CP) {
232 }
else if (cur_class != NU and cur_class != SY and cur_class != IS) {
235 }
else if (cur_class == NU) {
240 if (cur->original_class == RI) {
242 }
else if (*cur != RI) {
246 if (*opportunity == unicode_break_opportunity::unassigned) {
247 *opportunity = match_func(prev_class, cur, next, next2_class, cur_sp_class, cur_nu_class, num_ri);
250 prev_class = cur_class;
260 using enum unicode_break_opportunity;
264 }
else if (*cur == CR and *next == LF) {
266 }
else if (*cur == CR or *cur == LF or *cur == NL) {
268 }
else if (*next == BK or *next == CR or *next == LF or *next == NL) {
270 }
else if (*next == SP or *next == ZW) {
272 }
else if (cur_sp == ZW) {
274 }
else if (*cur == ZWJ) {
285 using enum unicode_break_opportunity;
291 auto cur = infos.begin();
292 hilet last = infos.end() - 1;
293 auto opportunity = opportunities.begin() + 1;
296 while (cur != last) {
299 if ((*cur == CM or *cur == ZWJ) and X != XX) {
307 if ((*cur != BK and *cur != CR and *cur != LF and *cur != NL and *cur != SP and *cur != ZW) and
308 (*next == CM or *next == ZWJ)) {
327 for (
auto& x : infos) {
328 if (x == CM or x == ZWJ) {
338 using enum unicode_break_opportunity;
340 using enum unicode_east_asian_width;
342 if (*cur == WJ or *next == WJ) {
344 }
else if (*cur == GL) {
346 }
else if (*cur != SP and *cur != BA and *cur != HY and *next == GL) {
348 }
else if (*next == CL or *next == CP or *next == EX or *next == IS or *next == SY) {
350 }
else if (cur_sp == OP) {
352 }
else if (cur_sp == QU and *next == OP) {
354 }
else if ((cur_sp == CL or cur_sp == CP) and *next == NS) {
356 }
else if (cur_sp == B2 and *next == B2) {
358 }
else if (*cur == SP) {
360 }
else if (*cur == QU or *next == QU) {
362 }
else if (*cur == CB or *next == CB) {
364 }
else if (*cur == BB or *next == BA or *next == HY or *next == NS) {
366 }
else if (prev == HL and (*cur == HY or *cur == BA)) {
368 }
else if (*cur == SY and *next == HL) {
370 }
else if (*next == IN) {
372 }
else if ((*cur == AL or *cur == HL) and *next == NU) {
374 }
else if (*cur == NU and (*next == AL or *next == HL)) {
376 }
else if (*cur == PR and (*next == ID or *next == EB or *next == EM)) {
378 }
else if ((*cur == ID or *cur == EB or *cur == EM) and *next == PO) {
380 }
else if ((*cur == PR or *cur == PO) and (*next == AL or *next == HL)) {
382 }
else if ((*cur == AL or *cur == HL) and (*next == PR or *next == PO)) {
385 (*cur == PR or *cur == PO) and ((*next == OP and next2 == NU) or (*next == HY and next2 == NU) or *next == NU)) {
387 }
else if ((*cur == OP or *cur == HY) and *next == NU) {
389 }
else if (*cur == NU and (*next == NU or *next == SY or *next == IS)) {
391 }
else if (cur_nu == NU and (*next == NU or *next == SY or *next == IS or *next == CL or *next == CP)) {
393 }
else if ((cur_nu == NU or cur_nu == CL) and (*next == PO or *next == PR)) {
395 }
else if (*cur == JL and (*next == JL or *next == JV or *next == H2 or *next == H3)) {
397 }
else if ((*cur == JV or *cur == H2) and (*next == JV or *next == JT)) {
399 }
else if ((*cur == JT or *cur == H3) and *next == JT) {
401 }
else if ((*cur == JL or *cur == JV or *cur == JT or *cur == H2 or *cur == H3) and *next == PO) {
403 }
else if (*cur == PR and (*next == JL or *next == JV or *next == JT or *next == H2 or *next == H3)) {
405 }
else if ((*cur == AL or *cur == HL) and (*next == AL or *next == HL)) {
407 }
else if (*cur == IS and (*next == AL or *next == HL)) {
409 }
else if ((*cur == AL or *cur == HL or *cur == NU) and (*next == OP and *next != F and *next != W and *next != H)) {
411 }
else if ((*cur == CP and *cur != F and *cur != W and *cur != H) and (*next == AL or *next == HL or *next == NU)) {
413 }
else if (*cur == RI and *next == RI and (num_ri % 2) == 1) {
415 }
else if (*cur == EB and *next == EM) {
417 }
else if (cur->is_extended_pictographic and cur->is_Cn and *next == EM) {
431[[nodiscard]]
constexpr float
432unicode_LB_width(std::vector<float>::const_iterator first, std::vector<float>::const_iterator last)
noexcept
438 auto rfirst = std::make_reverse_iterator(last);
439 auto rlast = std::make_reverse_iterator(first);
445 return acc + abs(width);
457 auto max_width = 0.0f;
458 auto it = widths.
begin();
459 for (
auto length : lengths) {
473[[nodiscard]]
constexpr bool
476 auto it = widths.begin();
477 for (
auto length : lengths) {
495 for (
auto it = opportunities.begin() + 1; it != opportunities.end(); ++it) {
497 if (*it == unicode_break_opportunity::mandatory) {
515 for (
auto it = opportunities.begin() + 1; it != opportunities.end(); ++it) {
517 if (*it != unicode_break_opportunity::no) {
526[[nodiscard]]
constexpr unicode_break_const_iterator unicode_LB_fast_fit_line(
527 unicode_break_const_iterator opportunity_it,
528 std::vector<float>::const_iterator width_it,
529 float maximum_line_width)
noexcept
531 using enum unicode_break_opportunity;
534 auto end_of_line = opportunity_it;
536 width += abs(*width_it);
537 if (width > maximum_line_width) {
541 }
else if (*opportunity_it == mandatory) {
543 return opportunity_it;
545 }
else if (*opportunity_it == yes) {
547 end_of_line = opportunity_it;
556[[nodiscard]]
constexpr unicode_break_const_iterator unicode_LB_slow_fit_line(
557 unicode_break_const_iterator first,
558 unicode_break_const_iterator end_of_line,
559 std::vector<float>::const_iterator first_width,
560 float maximum_line_width)
noexcept
562 using enum unicode_break_opportunity;
565 auto it = end_of_line;
570 if (line_width <= maximum_line_width) {
571 if (*it == mandatory) {
575 }
else if (*it == yes) {
589[[nodiscard]]
constexpr unicode_break_const_iterator
590unicode_LB_finish_fit_line(unicode_break_const_iterator first, unicode_break_const_iterator end_of_line)
noexcept
592 if (first == end_of_line) {
594 while (*end_of_line == unicode_break_opportunity::no) {
600 return end_of_line + 1;
610 float maximum_line_width)
noexcept
612 using enum unicode_break_opportunity;
615 if (widths.empty()) {
619 auto opportunity_it = opportunities.
begin() + 1;
620 auto width_it = widths.begin();
621 while (width_it != widths.end()) {
623 auto opportunity_eol = unicode_LB_fast_fit_line(opportunity_it, width_it, maximum_line_width);
624 opportunity_eol = unicode_LB_slow_fit_line(opportunity_it, opportunity_eol, width_it, maximum_line_width);
625 opportunity_eol = unicode_LB_finish_fit_line(opportunity_it, opportunity_eol);
628 r.push_back(num_characters);
629 opportunity_it += num_characters;
630 width_it += num_characters;
647 auto line_lengths = detail::unicode_LB_mandatory_lines(opportunities);
648 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
663 auto line_lengths = detail::unicode_LB_optional_lines(opportunities);
664 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
680 auto line_lengths = detail::unicode_LB_fit_lines(opportunities, char_widths, maximum_line_width);
681 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
694template<
typename It,
typename ItEnd,
typename DescriptionFunc>
695[[nodiscard]]
inline unicode_break_vector
701 auto infos = detail::unicode_LB1(first, last, description_func);
702 detail::unicode_LB2_3(r);
703 detail::unicode_LB4_8a(r, infos);
704 detail::unicode_LB9(r, infos);
705 detail::unicode_LB10(infos);
706 detail::unicode_LB11_31(r, infos);
721 auto r = detail::unicode_LB_mandatory_lines(opportunities);
722 if (detail::unicode_LB_width_check(widths, r, maximum_line_width)) {
726 r = detail::unicode_LB_fit_lines(opportunities, widths, maximum_line_width);
727 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:432
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:490
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:510
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:661
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:645
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:607
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:474
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:238
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
unicode_line_break_class
Unicode line break class.
Definition unicode_line_break.hpp:33
unicode_break_vector unicode_line_break(It first, ItEnd last, DescriptionFunc const &description_func) noexcept
The unicode line break algorithm UAX #14.
Definition unicode_line_break.hpp:696
@ AL
Right-to-Left Arabic.
Combined unicode_line_break_class and unicode_line_break_opportunity.
Definition unicode_line_break.hpp:95