10#include "unicode_general_category.hpp"
11#include "unicode_grapheme_cluster_break.hpp"
13#include "unicode_break_opportunity.hpp"
28namespace hi::inline
v1 {
101 bool is_extended_pictographic =
false;
103 unicode_east_asian_width east_asian_width = unicode_east_asian_width::A;
114 bool is_extended_pictographic,
115 unicode_east_asian_width east_asian_width) noexcept :
116 original_class(break_class),
117 current_class(break_class),
119 is_extended_pictographic(is_extended_pictographic),
120 east_asian_width(east_asian_width)
126 return current_class;
137 return current_class == rhs;
140 [[nodiscard]]
constexpr bool operator==(unicode_east_asian_width rhs)
const noexcept
142 return east_asian_width == rhs;
147using unicode_line_break_info_iterator = unicode_line_break_info_vector::iterator;
148using unicode_line_break_info_const_iterator = unicode_line_break_info_vector::const_iterator;
150template<
typename It,
typename ItEnd,
typename DescriptionFunc>
152unicode_LB1(It first, ItEnd last, DescriptionFunc
const& description_func)
noexcept
157 for (
auto it = first; it != last; ++it) {
158 hilet& description = description_func(*it);
159 hilet break_class = description.line_break_class();
160 hilet general_category = description.general_category();
162 hilet resolved_break_class = [&]() {
163 switch (break_class) {
172 return is_Mn_or_Mc(general_category) ? CM :
AL;
179 resolved_break_class,
180 general_category == unicode_general_category::Cn,
181 description.grapheme_cluster_break() == unicode_grapheme_cluster_break::Extended_Pictographic,
182 description.east_asian_width());
188[[nodiscard]]
constexpr void unicode_LB2_3(unicode_break_vector& opportunities)
noexcept
190 hi_axiom(not opportunities.empty());
192 opportunities.front() = unicode_break_opportunity::no;
194 opportunities.back() = unicode_break_opportunity::mandatory;
197template<
typename MatchFunc>
198constexpr void unicode_LB_walk(
199 unicode_break_vector& opportunities,
201 MatchFunc match_func)
noexcept
209 auto cur = infos.begin();
210 hilet last = infos.end() - 1;
211 hilet last2 = infos.end();
212 auto opportunity = opportunities.begin() + 1;
214 auto cur_sp_class = XX;
215 auto cur_nu_class = XX;
216 auto prev_class = XX;
218 while (cur != last) {
224 if (cur_class != SP) {
225 cur_sp_class = cur_class;
229 if (cur_nu_class == CL) {
232 }
else if (cur_nu_class == NU) {
233 if (cur_class == CL or cur_class == CP) {
235 }
else if (cur_class != NU and cur_class != SY and cur_class != IS) {
238 }
else if (cur_class == NU) {
243 if (cur->original_class == RI) {
245 }
else if (*cur != RI) {
249 if (*opportunity == unicode_break_opportunity::unassigned) {
250 *opportunity = match_func(prev_class, cur, next, next2_class, cur_sp_class, cur_nu_class, num_ri);
253 prev_class = cur_class;
263 using enum unicode_break_opportunity;
267 }
else if (*cur == CR and *next == LF) {
269 }
else if (*cur == CR or *cur == LF or *cur == NL) {
271 }
else if (*next == BK or *next == CR or *next == LF or *next == NL) {
273 }
else if (*next == SP or *next == ZW) {
275 }
else if (cur_sp == ZW) {
277 }
else if (*cur == ZWJ) {
288 using enum unicode_break_opportunity;
294 auto cur = infos.begin();
295 hilet last = infos.end() - 1;
296 auto opportunity = opportunities.begin() + 1;
299 while (cur != last) {
302 if ((*cur == CM or *cur == ZWJ) and X != XX) {
310 if ((*cur != BK and *cur != CR and *cur != LF and *cur != NL and *cur != SP and *cur != ZW) and
311 (*next == CM or *next == ZWJ)) {
330 for (
auto& x : infos) {
331 if (x == CM or x == ZWJ) {
341 using enum unicode_break_opportunity;
343 using enum unicode_east_asian_width;
345 if (*cur == WJ or *next == WJ) {
347 }
else if (*cur == GL) {
349 }
else if (*cur != SP and *cur != BA and *cur != HY and *next == GL) {
351 }
else if (*next == CL or *next == CP or *next == EX or *next == IS or *next == SY) {
353 }
else if (cur_sp == OP) {
355 }
else if (cur_sp == QU and *next == OP) {
357 }
else if ((cur_sp == CL or cur_sp == CP) and *next == NS) {
359 }
else if (cur_sp == B2 and *next == B2) {
361 }
else if (*cur == SP) {
363 }
else if (*cur == QU or *next == QU) {
365 }
else if (*cur == CB or *next == CB) {
367 }
else if (*cur == BB or *next == BA or *next == HY or *next == NS) {
369 }
else if (prev == HL and (*cur == HY or *cur == BA)) {
371 }
else if (*cur == SY and *next == HL) {
373 }
else if (*next == IN) {
375 }
else if ((*cur == AL or *cur == HL) and *next == NU) {
377 }
else if (*cur == NU and (*next == AL or *next == HL)) {
379 }
else if (*cur == PR and (*next == ID or *next == EB or *next == EM)) {
381 }
else if ((*cur == ID or *cur == EB or *cur == EM) and *next == PO) {
383 }
else if ((*cur == PR or *cur == PO) and (*next == AL or *next == HL)) {
385 }
else if ((*cur == AL or *cur == HL) and (*next == PR or *next == PO)) {
388 (*cur == PR or *cur == PO) and ((*next == OP and next2 == NU) or (*next == HY and next2 == NU) or *next == NU)) {
390 }
else if ((*cur == OP or *cur == HY) and *next == NU) {
392 }
else if (*cur == NU and (*next == NU or *next == SY or *next == IS)) {
394 }
else if (cur_nu == NU and (*next == NU or *next == SY or *next == IS or *next == CL or *next == CP)) {
396 }
else if ((cur_nu == NU or cur_nu == CL) and (*next == PO or *next == PR)) {
398 }
else if (*cur == JL and (*next == JL or *next == JV or *next == H2 or *next == H3)) {
400 }
else if ((*cur == JV or *cur == H2) and (*next == JV or *next == JT)) {
402 }
else if ((*cur == JT or *cur == H3) and *next == JT) {
404 }
else if ((*cur == JL or *cur == JV or *cur == JT or *cur == H2 or *cur == H3) and *next == PO) {
406 }
else if (*cur == PR and (*next == JL or *next == JV or *next == JT or *next == H2 or *next == H3)) {
408 }
else if ((*cur == AL or *cur == HL) and (*next == AL or *next == HL)) {
410 }
else if (*cur == IS and (*next == AL or *next == HL)) {
412 }
else if ((*cur == AL or *cur == HL or *cur == NU) and (*next == OP and *next != F and *next != W and *next != H)) {
414 }
else if ((*cur == CP and *cur != F and *cur != W and *cur != H) and (*next == AL or *next == HL or *next == NU)) {
416 }
else if (*cur == RI and *next == RI and (num_ri % 2) == 1) {
418 }
else if (*cur == EB and *next == EM) {
420 }
else if (cur->is_extended_pictographic and cur->is_Cn and *next == EM) {
434[[nodiscard]]
constexpr float
435unicode_LB_width(std::vector<float>::const_iterator first, std::vector<float>::const_iterator last)
noexcept
441 auto rfirst = std::make_reverse_iterator(last);
442 auto rlast = std::make_reverse_iterator(first);
448 return acc + abs(width);
460 auto max_width = 0.0f;
461 auto it = widths.
begin();
462 for (
auto length : lengths) {
476[[nodiscard]]
constexpr bool
479 auto it = widths.begin();
480 for (
auto length : lengths) {
498 for (
auto it = opportunities.begin() + 1; it != opportunities.end(); ++it) {
500 if (*it == unicode_break_opportunity::mandatory) {
518 for (
auto it = opportunities.begin() + 1; it != opportunities.end(); ++it) {
520 if (*it != unicode_break_opportunity::no) {
529[[nodiscard]]
constexpr unicode_break_const_iterator unicode_LB_fast_fit_line(
530 unicode_break_const_iterator opportunity_it,
531 std::vector<float>::const_iterator width_it,
532 float maximum_line_width)
noexcept
534 using enum unicode_break_opportunity;
537 auto end_of_line = opportunity_it;
539 width += abs(*width_it);
540 if (width > maximum_line_width) {
544 }
else if (*opportunity_it == mandatory) {
546 return opportunity_it;
548 }
else if (*opportunity_it == yes) {
550 end_of_line = opportunity_it;
559[[nodiscard]]
constexpr unicode_break_const_iterator unicode_LB_slow_fit_line(
560 unicode_break_const_iterator first,
561 unicode_break_const_iterator end_of_line,
562 std::vector<float>::const_iterator first_width,
563 float maximum_line_width)
noexcept
565 using enum unicode_break_opportunity;
568 auto it = end_of_line;
573 if (line_width <= maximum_line_width) {
574 if (*it == mandatory) {
578 }
else if (*it == yes) {
592[[nodiscard]]
constexpr unicode_break_const_iterator
593unicode_LB_finish_fit_line(unicode_break_const_iterator first, unicode_break_const_iterator end_of_line)
noexcept
595 if (first == end_of_line) {
597 while (*end_of_line == unicode_break_opportunity::no) {
603 return end_of_line + 1;
613 float maximum_line_width)
noexcept
615 using enum unicode_break_opportunity;
618 if (widths.empty()) {
622 auto opportunity_it = opportunities.
begin() + 1;
623 auto width_it = widths.begin();
624 while (width_it != widths.end()) {
626 auto opportunity_eol = unicode_LB_fast_fit_line(opportunity_it, width_it, maximum_line_width);
627 opportunity_eol = unicode_LB_slow_fit_line(opportunity_it, opportunity_eol, width_it, maximum_line_width);
628 opportunity_eol = unicode_LB_finish_fit_line(opportunity_it, opportunity_eol);
631 r.push_back(num_characters);
632 opportunity_it += num_characters;
633 width_it += num_characters;
650 auto line_lengths = detail::unicode_LB_mandatory_lines(opportunities);
651 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
666 auto line_lengths = detail::unicode_LB_optional_lines(opportunities);
667 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
683 auto line_lengths = detail::unicode_LB_fit_lines(opportunities, char_widths, maximum_line_width);
684 hilet width = detail::unicode_LB_width(char_widths, line_lengths);
697template<
typename It,
typename ItEnd,
typename DescriptionFunc>
698[[nodiscard]]
inline unicode_break_vector
704 auto infos = detail::unicode_LB1(first, last, description_func);
705 detail::unicode_LB2_3(r);
706 detail::unicode_LB4_8a(r, infos);
707 detail::unicode_LB9(r, infos);
708 detail::unicode_LB10(infos);
709 detail::unicode_LB11_31(r, infos);
724 auto r = detail::unicode_LB_mandatory_lines(opportunities);
725 if (detail::unicode_LB_width_check(widths, r, maximum_line_width)) {
729 r = detail::unicode_LB_fit_lines(opportunities, widths, maximum_line_width);
730 hi_axiom(detail::unicode_LB_width_check(widths, r, maximum_line_width));
Utilities to assert and bound check.
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:133
Miscellaneous math functions.
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:435
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:493
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:513
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:664
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:648
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:610
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:477
Utilities used by the HikoGUI library itself.
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:15
unicode_line_break_class
Unicode line break class.
Definition unicode_line_break.hpp:36
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:699
@ AL
Right-to-Left Arabic.
Combined unicode_line_break_class and unicode_line_break_opportunity.
Definition unicode_line_break.hpp:98