HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
unicode_text_segmentation.hpp
1// Copyright Take Vos 2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "unicode_grapheme_cluster_break.hpp"
8#include "unicode_description.hpp"
9
10namespace hi::inline v1 {
11
13 unicode_grapheme_cluster_break previous = unicode_grapheme_cluster_break::Other;
14 int RI_count = 0;
15 bool first_character = true;
16 bool in_extended_pictograph = false;
17
18 void reset() noexcept
19 {
20 previous = unicode_grapheme_cluster_break::Other;
21 RI_count = 0;
22 first_character = true;
23 in_extended_pictograph = false;
24 }
25};
26
34[[nodiscard]] bool breaks_grapheme(char32_t code_point, grapheme_break_state &state) noexcept;
35
51void wrap_lines(auto first, auto last, float max_width, auto get_width, auto get_code_point, auto set_code_point) noexcept
52{
53 using enum unicode_general_category;
54
55 auto it_at_last_space = last;
56 float width_at_last_space = 0.0;
57 float current_width = 0.0;
58
59 for (auto it = first; it != last; ++it) {
60 hilet code_point = get_code_point(*it);
61 hilet description = unicode_description::find(code_point);
62 hilet general_category = description->general_category();
63
64 if (general_category == Zp || general_category == Zl) {
65 // Reset the line on existing line and paragraph separator.
66 it_at_last_space = last;
67 width_at_last_space = 0.0f;
68 current_width = 0.0;
69 continue;
70
71 } else if (general_category == Zs) {
72 // Remember the length of the line at the end of the word.
73 it_at_last_space = it;
74 width_at_last_space = current_width;
75 }
76
77 current_width += get_width(*it);
78 if (current_width >= max_width && it_at_last_space != last) {
79 // The line is too long, replace the last space with a line separator.
80 set_code_point(*it, U'\u2028');
81 it_at_last_space = last;
82 width_at_last_space = 0.0f;
83 current_width = 0.0;
84 continue;
85 }
86 }
87}
88
89} // namespace hi::inline v1
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
Definition unicode_text_segmentation.hpp:12