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 tt {
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
36
52void wrap_lines(auto first, auto last, float max_width, auto get_width, auto get_code_point, auto set_code_point) noexcept
53{
54 using enum unicode_general_category;
55
56 auto it_at_last_space = last;
57 float width_at_last_space = 0.0;
58 float current_width = 0.0;
59
60 for (auto it = first; it != last; ++it) {
61 ttlet code_point = get_code_point(*it);
62 ttlet description = unicode_description_find(code_point);
63 ttlet general_category = description->general_category();
64
65 if (general_category == Zp || general_category == Zl) {
66 // Reset the line on existing line and paragraph separator.
67 it_at_last_space = last;
68 width_at_last_space = 0.0f;
69 current_width = 0.0;
70 continue;
71
72 } else if (general_category == Zs) {
73 // Remember the length of the line at the end of the word.
74 it_at_last_space = it;
75 width_at_last_space = current_width;
76 }
77
78 current_width += get_width(*it);
79 if (current_width >= max_width && it_at_last_space != last) {
80 // The line is too long, replace the last space with a line separator.
81 set_code_point(*it, U'\u2028');
82 it_at_last_space = last;
83 width_at_last_space = 0.0f;
84 current_width = 0.0;
85 continue;
86 }
87 }
88}
89
90}
Definition unicode_text_segmentation.hpp:12