HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_shaper.hpp
1// Copyright Take Vos 2021-2022.
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 "text_shaper_char.hpp"
8#include "text_shaper_line.hpp"
9#include "text_cursor.hpp"
10#include "text_style.hpp"
11#include "../layout/box_constraints.hpp"
12#include "../font/module.hpp"
13#include "../geometry/module.hpp"
14#include "../unicode/unicode_description.hpp"
15#include "../unicode/unicode_break_opportunity.hpp"
16#include "../unicode/unicode_bidi.hpp"
17#include "../unicode/grapheme.hpp"
18#include "../unicode/gstring.hpp"
19#include <vector>
20#include <tuple>
21
22namespace hi::inline v1 {
23class font_book;
24
39public:
41 using char_iterator = char_vector::iterator;
42 using char_const_iterator = char_vector::const_iterator;
43 using char_reference = char_vector::reference;
44 using char_const_reference = char_vector::const_reference;
46 using line_iterator = line_vector::iterator;
47 using line_const_iterator = line_vector::const_iterator;
48
49 constexpr text_shaper() noexcept = default;
50 constexpr text_shaper(text_shaper const&) noexcept = default;
51 constexpr text_shaper(text_shaper&&) noexcept = default;
52 constexpr text_shaper& operator=(text_shaper const&) noexcept = default;
53 constexpr text_shaper& operator=(text_shaper&&) noexcept = default;
54
84 [[nodiscard]] text_shaper(
85 hi::font_book& font_book,
86 gstring const& text,
87 text_style const& style,
88 float dpi_scale,
89 hi::alignment alignment,
90 unicode_bidi_class text_direction,
91 unicode_script script = unicode_script::Common) noexcept;
92
93 [[nodiscard]] text_shaper(
94 hi::font_book& font_book,
95 std::string_view text,
96 text_style const& style,
97 float dpi_scale,
98 hi::alignment alignment,
99 unicode_bidi_class text_direction,
100 unicode_script script = unicode_script::Common) noexcept;
101
102 [[nodiscard]] bool empty() const noexcept
103 {
104 return _text.empty();
105 }
106
107 [[nodiscard]] size_t size() const noexcept
108 {
109 return _text.size();
110 }
111
112 [[nodiscard]] char_iterator begin() noexcept
113 {
114 return _text.begin();
115 }
116
117 [[nodiscard]] char_const_iterator begin() const noexcept
118 {
119 return _text.begin();
120 }
121
122 [[nodiscard]] char_const_iterator cbegin() const noexcept
123 {
124 return _text.cbegin();
125 }
126
127 [[nodiscard]] char_iterator end() noexcept
128 {
129 return _text.end();
130 }
131
132 [[nodiscard]] char_const_iterator end() const noexcept
133 {
134 return _text.end();
135 }
136
137 [[nodiscard]] char_const_iterator cend() const noexcept
138 {
139 return _text.cend();
140 }
141
142 auto const& lines() const noexcept
143 {
144 return _lines;
145 }
146
165 [[nodiscard]] aarectangle
166 bounding_rectangle(float maximum_line_width, float line_spacing = 1.0f, float paragraph_spacing = 1.5f) noexcept;
167
180 [[nodiscard]] void layout(
181 aarectangle rectangle,
182 float baseline,
183 extent2 sub_pixel_size,
184 float line_spacing = 1.0f,
185 float paragraph_spacing = 1.5f) noexcept;
186
189 [[nodiscard]] aarectangle rectangle() const noexcept
190 {
191 return _rectangle;
192 }
193
196 [[nodiscard]] unicode_bidi_class text_direction() const noexcept
197 {
198 return _text_direction;
199 }
200
206 [[nodiscard]] alignment resolved_alignment() const noexcept
207 {
208 return resolve(_alignment, _text_direction == unicode_bidi_class::L);
209 }
210
218 [[nodiscard]] char_const_iterator get_it(size_t index) const noexcept;
219
227 [[nodiscard]] char_const_iterator get_it(text_cursor cursor) const noexcept
228 {
229 return get_it(cursor.index());
230 }
231
240 [[nodiscard]] char_const_iterator get_it(size_t column, size_t row) const noexcept;
241
249 [[nodiscard]] char_const_iterator get_it(std::pair<size_t, size_t> column_row) const noexcept
250 {
251 return get_it(column_row.first, column_row.second);
252 }
253
259 [[nodiscard]] std::pair<size_t, size_t> get_column_line(text_shaper::char_const_iterator it) const noexcept;
260
266 [[nodiscard]] std::pair<size_t, size_t> get_column_line(size_t index) const noexcept
267 {
268 return get_column_line(get_it(index));
269 }
270
276 [[nodiscard]] std::pair<size_t, size_t> get_column_line(text_cursor cursor) const noexcept
277 {
278 return get_column_line(cursor.index());
279 }
280
286 [[nodiscard]] size_t get_index(text_shaper::char_const_iterator it) const noexcept;
287
292 [[nodiscard]] text_cursor get_begin_cursor() const noexcept;
293
298 [[nodiscard]] text_cursor get_end_cursor() const noexcept;
299
305 [[nodiscard]] text_cursor get_before_cursor(size_t index) const noexcept;
306
312 [[nodiscard]] text_cursor get_after_cursor(size_t index) const noexcept;
313
319 [[nodiscard]] text_cursor get_before_cursor(text_shaper::char_const_iterator it) const noexcept
320 {
321 return get_before_cursor(get_index(it));
322 }
323
329 [[nodiscard]] text_cursor get_after_cursor(text_shaper::char_const_iterator it) const noexcept
330 {
331 return get_after_cursor(get_index(it));
332 }
333
339 [[nodiscard]] text_cursor get_left_cursor(text_shaper::char_const_iterator it) const noexcept;
340
346 [[nodiscard]] text_cursor get_right_cursor(text_shaper::char_const_iterator it) const noexcept;
347
353 [[nodiscard]] bool is_on_left(text_cursor cursor) const noexcept;
354
360 [[nodiscard]] bool is_on_right(text_cursor cursor) const noexcept;
361
367 [[nodiscard]] text_cursor get_nearest_cursor(point2 point) const noexcept;
368
371 [[nodiscard]] std::pair<text_cursor, text_cursor> select_char(text_cursor cursor) const noexcept;
372
375 [[nodiscard]] std::pair<text_cursor, text_cursor> select_word(text_cursor cursor) const noexcept;
376
380
384
388
394 [[nodiscard]] char_const_iterator move_left_char(char_const_iterator it) const noexcept;
395
401 [[nodiscard]] char_const_iterator move_right_char(char_const_iterator it) const noexcept;
402
403 [[nodiscard]] text_cursor move_left_char(text_cursor cursor, bool overwrite_mode) const noexcept;
404 [[nodiscard]] text_cursor move_right_char(text_cursor cursor, bool overwrite_mode) const noexcept;
405 [[nodiscard]] text_cursor move_down_char(text_cursor cursor, float& x) const noexcept;
406 [[nodiscard]] text_cursor move_up_char(text_cursor cursor, float& x) const noexcept;
407 [[nodiscard]] text_cursor move_left_word(text_cursor cursor, bool overwrite_mode) const noexcept;
408 [[nodiscard]] text_cursor move_right_word(text_cursor cursor, bool overwrite_mode) const noexcept;
409 [[nodiscard]] text_cursor move_begin_line(text_cursor cursor) const noexcept;
410 [[nodiscard]] text_cursor move_end_line(text_cursor cursor) const noexcept;
411 [[nodiscard]] text_cursor move_begin_sentence(text_cursor cursor) const noexcept;
412 [[nodiscard]] text_cursor move_end_sentence(text_cursor cursor) const noexcept;
413 [[nodiscard]] text_cursor move_begin_paragraph(text_cursor cursor) const noexcept;
414 [[nodiscard]] text_cursor move_end_paragraph(text_cursor cursor) const noexcept;
415 [[nodiscard]] text_cursor move_begin_document(text_cursor cursor) const noexcept;
416 [[nodiscard]] text_cursor move_end_document(text_cursor cursor) const noexcept;
417
418private:
419 font_book *_font_book = nullptr;
420
423 float _dpi_scale;
424
431 char_vector _text;
432
433 hi::alignment _alignment;
434
437 unicode_break_vector _line_break_opportunities;
438
441 std::vector<float> _line_break_widths;
442
445 unicode_break_vector _word_break_opportunities;
446
449 unicode_break_vector _sentence_break_opportunities;
450
453 unicode_bidi_context _bidi_context;
454
457 unicode_bidi_class _text_direction;
458
461 unicode_script _script;
462
467 line_vector _lines;
468
471 font_metrics _initial_line_metrics;
472
475 aarectangle _rectangle;
476
485 [[nodiscard]] line_vector make_lines(
486 aarectangle rectangle,
487 float baseline,
488 extent2 sub_pixel_size,
489 float line_spacing,
490 float paragraph_spacing) noexcept;
491
498 void position_glyphs(aarectangle rectangle, extent2 sub_pixel_size) noexcept;
499
502 void resolve_script() noexcept;
503
504 [[nodiscard]] std::pair<text_cursor, text_cursor>
505 get_selection_from_break(text_cursor cursor, unicode_break_vector const& break_opportunities) const noexcept;
506
507 [[nodiscard]] std::pair<font_metrics, unicode_general_category>
508 get_line_metrics(text_shaper::char_const_iterator first, text_shaper::char_const_iterator last) const noexcept;
509
516 [[nodiscard]] float get_text_height(std::vector<size_t> const& lines) const noexcept;
517};
518
519} // namespace hi::inline v1
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:13
unicode_bidi_class
Bidirectional class Unicode Standard Annex #9: https://unicode.org/reports/tr9/.
Definition unicode_bidi_class.hpp:16
font_book keeps track of multiple fonts.
Definition font_book.hpp:31
The metrics of a font.
Definition font_metrics.hpp:15
Horizontal/Vertical alignment combination.
Definition alignment.hpp:231
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:20
A variant of text.
Definition label.hpp:31
A cursor-position in text.
Definition text_cursor.hpp:22
Text shaper.
Definition text_shaper.hpp:38
text_shaper(hi::font_book &font_book, gstring const &text, text_style const &style, float dpi_scale, hi::alignment alignment, unicode_bidi_class text_direction, unicode_script script=unicode_script::Common) noexcept
Construct a text_shaper with a text and alignment.
char_const_iterator get_it(size_t column, size_t row) const noexcept
Get the character at column and row in display order.
std::pair< text_cursor, text_cursor > select_paragraph(text_cursor cursor) const noexcept
Get the selection for a paragraph at the cursor.
std::pair< size_t, size_t > get_column_line(size_t index) const noexcept
Get the column and line of a character.
Definition text_shaper.hpp:266
char_const_iterator get_it(text_cursor cursor) const noexcept
Get the character at the cursor.
Definition text_shaper.hpp:227
std::pair< text_cursor, text_cursor > select_sentence(text_cursor cursor) const noexcept
Get the selection for the sentence at the cursor.
bool is_on_left(text_cursor cursor) const noexcept
Check if the cursor is on the left side of the character in display order.
bool is_on_right(text_cursor cursor) const noexcept
Check if the cursor is on the right side of the character in display order.
aarectangle bounding_rectangle(float maximum_line_width, float line_spacing=1.0f, float paragraph_spacing=1.5f) noexcept
Get bounding rectangle.
text_cursor get_after_cursor(text_shaper::char_const_iterator it) const noexcept
Get the cursor after the character in logical order.
Definition text_shaper.hpp:329
unicode_bidi_class text_direction() const noexcept
Get the text-direction as a whole.
Definition text_shaper.hpp:196
std::pair< text_cursor, text_cursor > select_char(text_cursor cursor) const noexcept
Get the selection for the character at the cursor.
std::pair< size_t, size_t > get_column_line(text_shaper::char_const_iterator it) const noexcept
Get the column and line of a character.
text_cursor get_left_cursor(text_shaper::char_const_iterator it) const noexcept
Get the cursor left of the character in display order.
std::pair< text_cursor, text_cursor > select_word(text_cursor cursor) const noexcept
Get the selection for the word at the cursor.
size_t get_index(text_shaper::char_const_iterator it) const noexcept
Get the index of the character in logical order.
char_const_iterator get_it(size_t index) const noexcept
Get the character at index in logical order.
std::pair< text_cursor, text_cursor > select_document(text_cursor cursor) const noexcept
Get the selection for a paragraph at the cursor.
text_cursor get_nearest_cursor(point2 point) const noexcept
find the nearest character.
char_const_iterator move_right_char(char_const_iterator it) const noexcept
Get the character to the right.
char_const_iterator get_it(std::pair< size_t, size_t > column_row) const noexcept
Get the character at column and row in display order.
Definition text_shaper.hpp:249
text_cursor get_begin_cursor() const noexcept
Get the cursor at the beginning of the document.
alignment resolved_alignment() const noexcept
Get the resolved alignment of the text.
Definition text_shaper.hpp:206
std::pair< size_t, size_t > get_column_line(text_cursor cursor) const noexcept
Get the column and line of a character.
Definition text_shaper.hpp:276
text_cursor get_right_cursor(text_shaper::char_const_iterator it) const noexcept
Get the cursor right of the character in display order.
char_const_iterator move_left_char(char_const_iterator it) const noexcept
Get the character to the left.
Definition text_style.hpp:173
Definition unicode_bidi.hpp:13
T begin(T... args)
T end(T... args)