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 "glyph_metrics.hpp"
11#include "font_metrics.hpp"
12#include "text_style.hpp"
13#include "glyph_ids.hpp"
14#include "font.hpp"
15#include "../unicode/unicode_description.hpp"
16#include "../unicode/unicode_break_opportunity.hpp"
17#include "../unicode/grapheme.hpp"
18#include "../unicode/gstring.hpp"
19#include "../alignment.hpp"
20#include <vector>
21#include <tuple>
22
23namespace hi::inline v1 {
24class font_book;
25
40public:
42 using char_iterator = char_vector::iterator;
43 using char_const_iterator = char_vector::const_iterator;
45 using line_iterator = line_vector::iterator;
46 using line_const_iterator = line_vector::const_iterator;
47
48 constexpr text_shaper() noexcept = default;
49 constexpr text_shaper(text_shaper const &) noexcept = default;
50 constexpr text_shaper(text_shaper &&) noexcept = default;
51 constexpr text_shaper &operator=(text_shaper const &) noexcept = default;
52 constexpr text_shaper &operator=(text_shaper &&) noexcept = default;
53
81 [[nodiscard]] text_shaper(
82 hi::font_book &font_book,
83 gstring const &text,
84 text_style const &style,
85 float dpi_scale,
86 unicode_script script = unicode_script::Common) noexcept;
87
88 [[nodiscard]] text_shaper(
89 hi::font_book &font_book,
90 std::string_view text,
91 text_style const &style,
92 float dpi_scale,
93 unicode_script script = unicode_script::Common) noexcept;
94
95 [[nodiscard]] bool empty() const noexcept
96 {
97 return _text.empty();
98 }
99
100 [[nodiscard]] size_t size() const noexcept
101 {
102 return _text.size();
103 }
104
105 [[nodiscard]] char_iterator begin() noexcept
106 {
107 return _text.begin();
108 }
109
110 [[nodiscard]] char_const_iterator begin() const noexcept
111 {
112 return _text.begin();
113 }
114
115 [[nodiscard]] char_const_iterator cbegin() const noexcept
116 {
117 return _text.cbegin();
118 }
119
120 [[nodiscard]] char_iterator end() noexcept
121 {
122 return _text.end();
123 }
124
125 [[nodiscard]] char_const_iterator end() const noexcept
126 {
127 return _text.end();
128 }
129
130 [[nodiscard]] char_const_iterator cend() const noexcept
131 {
132 return _text.cend();
133 }
134
135 auto const &lines() const noexcept
136 {
137 return _lines;
138 }
139
160 float maximum_line_width,
162 float line_spacing = 1.0f,
163 float paragraph_spacing = 1.5f) noexcept;
164
185 [[nodiscard]] void layout(
187 float base_line,
188 extent2 sub_pixel_size,
189 unicode_bidi_class writing_direction,
190 hi::alignment alignment = hi::alignment{horizontal_alignment::flush, vertical_alignment::middle},
191 float line_spacing = 1.0f,
192 float paragraph_spacing = 1.5f) noexcept;
193
196 [[nodiscard]] aarectangle rectangle() const noexcept
197 {
198 return _rectangle;
199 }
200
208 [[nodiscard]] char_const_iterator get_it(size_t index) const noexcept;
209
217 [[nodiscard]] char_const_iterator get_it(text_cursor cursor) const noexcept
218 {
219 return get_it(cursor.index());
220 }
221
230 [[nodiscard]] char_const_iterator get_it(size_t column, size_t row) const noexcept;
231
239 [[nodiscard]] char_const_iterator get_it(std::pair<size_t, size_t> column_row) const noexcept
240 {
241 return get_it(column_row.first, column_row.second);
242 }
243
249 [[nodiscard]] std::pair<size_t, size_t> get_column_line(text_shaper::char_const_iterator it) const noexcept;
250
256 [[nodiscard]] std::pair<size_t, size_t> get_column_line(size_t index) const noexcept
257 {
258 return get_column_line(get_it(index));
259 }
260
266 [[nodiscard]] std::pair<size_t, size_t> get_column_line(text_cursor cursor) const noexcept
267 {
268 return get_column_line(cursor.index());
269 }
270
276 [[nodiscard]] size_t get_index(text_shaper::char_const_iterator it) const noexcept;
277
282 [[nodiscard]] text_cursor get_begin_cursor() const noexcept;
283
288 [[nodiscard]] text_cursor get_end_cursor() const noexcept;
289
295 [[nodiscard]] text_cursor get_before_cursor(size_t index) const noexcept;
296
302 [[nodiscard]] text_cursor get_after_cursor(size_t index) const noexcept;
303
309 [[nodiscard]] text_cursor get_before_cursor(text_shaper::char_const_iterator it) const noexcept
310 {
311 return get_before_cursor(get_index(it));
312 }
313
319 [[nodiscard]] text_cursor get_after_cursor(text_shaper::char_const_iterator it) const noexcept
320 {
321 return get_after_cursor(get_index(it));
322 }
323
329 [[nodiscard]] text_cursor get_left_cursor(text_shaper::char_const_iterator it) const noexcept;
330
336 [[nodiscard]] text_cursor get_right_cursor(text_shaper::char_const_iterator it) const noexcept;
337
343 [[nodiscard]] bool is_on_left(text_cursor cursor) const noexcept;
344
350 [[nodiscard]] bool is_on_right(text_cursor cursor) const noexcept;
351
357 [[nodiscard]] text_cursor get_nearest_cursor(point2 point) const noexcept;
358
361 [[nodiscard]] std::pair<text_cursor, text_cursor> select_char(text_cursor cursor) const noexcept;
362
365 [[nodiscard]] std::pair<text_cursor, text_cursor> select_word(text_cursor cursor) const noexcept;
366
370
374
378
384 [[nodiscard]] char_const_iterator move_left_char(char_const_iterator it) const noexcept;
385
391 [[nodiscard]] char_const_iterator move_right_char(char_const_iterator it) const noexcept;
392
393 [[nodiscard]] text_cursor move_left_char(text_cursor cursor, bool overwrite_mode) const noexcept;
394 [[nodiscard]] text_cursor move_right_char(text_cursor cursor, bool overwrite_mode) const noexcept;
395 [[nodiscard]] text_cursor move_down_char(text_cursor cursor, float &x) const noexcept;
396 [[nodiscard]] text_cursor move_up_char(text_cursor cursor, float &x) const noexcept;
397 [[nodiscard]] text_cursor move_left_word(text_cursor cursor, bool overwrite_mode) const noexcept;
398 [[nodiscard]] text_cursor move_right_word(text_cursor cursor, bool overwrite_mode) const noexcept;
399 [[nodiscard]] text_cursor move_begin_line(text_cursor cursor) const noexcept;
400 [[nodiscard]] text_cursor move_end_line(text_cursor cursor) const noexcept;
401 [[nodiscard]] text_cursor move_begin_sentence(text_cursor cursor) const noexcept;
402 [[nodiscard]] text_cursor move_end_sentence(text_cursor cursor) const noexcept;
403 [[nodiscard]] text_cursor move_begin_paragraph(text_cursor cursor) const noexcept;
404 [[nodiscard]] text_cursor move_end_paragraph(text_cursor cursor) const noexcept;
405 [[nodiscard]] text_cursor move_begin_document(text_cursor cursor) const noexcept;
406 [[nodiscard]] text_cursor move_end_document(text_cursor cursor) const noexcept;
407
408private:
409 font_book *_font_book = nullptr;
410
413 float _dpi_scale;
414
421 char_vector _text;
422
425 unicode_break_vector _line_break_opportunities;
426
429 std::vector<float> _line_break_widths;
430
433 unicode_break_vector _word_break_opportunities;
434
437 unicode_break_vector _sentence_break_opportunities;
438
441 unicode_script _script;
442
447 line_vector _lines;
448
451 font_metrics _initial_line_metrics;
452
455 aarectangle _rectangle;
456
467 [[nodiscard]] line_vector make_lines(
469 float base_line,
470 extent2 sub_pixel_size,
471 hi::vertical_alignment vertical_alignment,
472 unicode_bidi_class writing_direction,
473 float line_spacing,
474 float paragraph_spacing) noexcept;
475
484 void position_glyphs(
486 extent2 sub_pixel_size,
487 hi::horizontal_alignment horizontal_alignment,
488 unicode_bidi_class writing_direction) noexcept;
489
492 void resolve_script() noexcept;
493
494 [[nodiscard]] std::pair<text_cursor, text_cursor>
495 get_selection_from_break(text_cursor cursor, unicode_break_vector const &break_opportunities) const noexcept;
496};
497
498} // namespace hi::inline v1
STL namespace.
DOXYGEN BUG.
Definition algorithm.hpp:15
vertical_alignment
Vertical alignment.
Definition alignment.hpp:17
horizontal_alignment
Definition alignment.hpp:31
unicode_bidi_class
Bidirectional class Unicode Standard Annex #9: https://unicode.org/reports/tr9/.
Definition unicode_bidi_class.hpp:17
The HikoGUI namespace.
Definition ascii.hpp:19
Definition alignment.hpp:64
Class which represents an axis-aligned rectangle.
Definition axis_aligned_rectangle.hpp:20
A rectangle / parallelogram in 3D space.
Definition rectangle.hpp:20
A variant of text.
Definition label.hpp:37
font_book keeps track of multiple fonts.
Definition font_book.hpp:31
The metrics of a font.
Definition font_metrics.hpp:15
A cursor-position in text.
Definition text_cursor.hpp:25
Text shaper.
Definition text_shaper.hpp:39
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:256
char_const_iterator get_it(text_cursor cursor) const noexcept
Get the character at the cursor.
Definition text_shaper.hpp:217
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.
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:319
aarectangle rectangle() const noexcept
The rectangle used when laying out the text.
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.
aarectangle bounding_rectangle(float maximum_line_width, vertical_alignment alignment, float line_spacing=1.0f, float paragraph_spacing=1.5f) noexcept
Get bounding rectangle.
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:239
text_cursor get_begin_cursor() const noexcept
Get the cursor at the beginning of the document.
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:266
text_cursor get_right_cursor(text_shaper::char_const_iterator it) const noexcept
Get the cursor right of the character in display order.
text_shaper(hi::font_book &font_book, gstring const &text, text_style const &style, float dpi_scale, unicode_script script=unicode_script::Common) noexcept
Construct a text_shaper with a text and alignment.
char_const_iterator move_left_char(char_const_iterator it) const noexcept
Get the character to the left.
Definition text_style.hpp:175
T begin(T... args)
T end(T... args)