HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
text_shaper.hpp
1// Copyright Take Vos 2021.
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
84 [[nodiscard]] text_shaper(
85 hi::font_book &font_book,
86 gstring const &text,
87 text_style const &style,
88 float dpi_scale,
89 unicode_script script = unicode_script::Common) noexcept;
90
91 [[nodiscard]] text_shaper(
92 hi::font_book &font_book,
93 std::string_view text,
94 text_style const &style,
95 float dpi_scale,
96 unicode_script script = unicode_script::Common) noexcept;
97
98 [[nodiscard]] bool empty() const noexcept
99 {
100 return _text.empty();
101 }
102
103 [[nodiscard]] size_t size() const noexcept
104 {
105 return _text.size();
106 }
107
108 [[nodiscard]] char_iterator begin() noexcept
109 {
110 return _text.begin();
111 }
112
113 [[nodiscard]] char_const_iterator begin() const noexcept
114 {
115 return _text.begin();
116 }
117
118 [[nodiscard]] char_const_iterator cbegin() const noexcept
119 {
120 return _text.cbegin();
121 }
122
123 [[nodiscard]] char_iterator end() noexcept
124 {
125 return _text.end();
126 }
127
128 [[nodiscard]] char_const_iterator end() const noexcept
129 {
130 return _text.end();
131 }
132
133 [[nodiscard]] char_const_iterator cend() const noexcept
134 {
135 return _text.cend();
136 }
137
138 auto const &lines() const noexcept
139 {
140 return _lines;
141 }
142
163 float maximum_line_width,
165 float line_spacing = 1.0f,
166 float paragraph_spacing = 1.5f) noexcept;
167
188 [[nodiscard]] void layout(
190 float base_line,
191 extent2 sub_pixel_size,
192 unicode_bidi_class writing_direction,
193 hi::alignment alignment = hi::alignment{horizontal_alignment::flush, vertical_alignment::middle},
194 float line_spacing = 1.0f,
195 float paragraph_spacing = 1.5f) noexcept;
196
199 [[nodiscard]] aarectangle rectangle() const noexcept
200 {
201 return _rectangle;
202 }
203
211 [[nodiscard]] char_const_iterator get_it(size_t index) const noexcept;
212
220 [[nodiscard]] char_const_iterator get_it(text_cursor cursor) const noexcept
221 {
222 return get_it(cursor.index());
223 }
224
233 [[nodiscard]] char_const_iterator get_it(size_t column, size_t row) const noexcept;
234
242 [[nodiscard]] char_const_iterator get_it(std::pair<size_t, size_t> column_row) const noexcept
243 {
244 return get_it(column_row.first, column_row.second);
245 }
246
252 [[nodiscard]] std::pair<size_t, size_t> get_column_line(text_shaper::char_const_iterator it) const noexcept;
253
259 [[nodiscard]] std::pair<size_t, size_t> get_column_line(size_t index) const noexcept
260 {
261 return get_column_line(get_it(index));
262 }
263
269 [[nodiscard]] std::pair<size_t, size_t> get_column_line(text_cursor cursor) const noexcept
270 {
271 return get_column_line(cursor.index());
272 }
273
279 [[nodiscard]] size_t get_index(text_shaper::char_const_iterator it) const noexcept;
280
285 [[nodiscard]] text_cursor get_begin_cursor() const noexcept;
286
291 [[nodiscard]] text_cursor get_end_cursor() const noexcept;
292
298 [[nodiscard]] text_cursor get_before_cursor(size_t index) const noexcept;
299
305 [[nodiscard]] text_cursor get_after_cursor(size_t index) const noexcept;
306
312 [[nodiscard]] text_cursor get_before_cursor(text_shaper::char_const_iterator it) const noexcept
313 {
314 return get_before_cursor(get_index(it));
315 }
316
322 [[nodiscard]] text_cursor get_after_cursor(text_shaper::char_const_iterator it) const noexcept
323 {
324 return get_after_cursor(get_index(it));
325 }
326
332 [[nodiscard]] text_cursor get_left_cursor(text_shaper::char_const_iterator it) const noexcept;
333
339 [[nodiscard]] text_cursor get_right_cursor(text_shaper::char_const_iterator it) const noexcept;
340
346 [[nodiscard]] bool is_on_left(text_cursor cursor) const noexcept;
347
353 [[nodiscard]] bool is_on_right(text_cursor cursor) const noexcept;
354
360 [[nodiscard]] text_cursor get_nearest_cursor(point2 point) const noexcept;
361
364 [[nodiscard]] std::pair<text_cursor, text_cursor> select_char(text_cursor cursor) const noexcept;
365
368 [[nodiscard]] std::pair<text_cursor, text_cursor> select_word(text_cursor cursor) const noexcept;
369
373
377
381
387 [[nodiscard]] char_const_iterator move_left_char(char_const_iterator it) const noexcept;
388
394 [[nodiscard]] char_const_iterator move_right_char(char_const_iterator it) const noexcept;
395
396 [[nodiscard]] text_cursor move_left_char(text_cursor cursor, bool overwrite_mode) const noexcept;
397 [[nodiscard]] text_cursor move_right_char(text_cursor cursor, bool overwrite_mode) const noexcept;
398 [[nodiscard]] text_cursor move_down_char(text_cursor cursor, float &x) const noexcept;
399 [[nodiscard]] text_cursor move_up_char(text_cursor cursor, float &x) const noexcept;
400 [[nodiscard]] text_cursor move_left_word(text_cursor cursor, bool overwrite_mode) const noexcept;
401 [[nodiscard]] text_cursor move_right_word(text_cursor cursor, bool overwrite_mode) const noexcept;
402 [[nodiscard]] text_cursor move_begin_line(text_cursor cursor) const noexcept;
403 [[nodiscard]] text_cursor move_end_line(text_cursor cursor) const noexcept;
404 [[nodiscard]] text_cursor move_begin_sentence(text_cursor cursor) const noexcept;
405 [[nodiscard]] text_cursor move_end_sentence(text_cursor cursor) const noexcept;
406 [[nodiscard]] text_cursor move_begin_paragraph(text_cursor cursor) const noexcept;
407 [[nodiscard]] text_cursor move_end_paragraph(text_cursor cursor) const noexcept;
408 [[nodiscard]] text_cursor move_begin_document(text_cursor cursor) const noexcept;
409 [[nodiscard]] text_cursor move_end_document(text_cursor cursor) const noexcept;
410
411private:
412 font_book *_font_book = nullptr;
413
416 float _dpi_scale;
417
424 char_vector _text;
425
428 unicode_break_vector _line_break_opportunities;
429
432 std::vector<float> _line_break_widths;
433
436 unicode_break_vector _word_break_opportunities;
437
440 unicode_break_vector _sentence_break_opportunities;
441
444 unicode_script _script;
445
450 line_vector _lines;
451
454 font_metrics _initial_line_metrics;
455
458 aarectangle _rectangle;
459
470 [[nodiscard]] line_vector make_lines(
472 float base_line,
473 extent2 sub_pixel_size,
474 hi::vertical_alignment vertical_alignment,
475 unicode_bidi_class writing_direction,
476 float line_spacing,
477 float paragraph_spacing) noexcept;
478
487 void position_glyphs(
489 extent2 sub_pixel_size,
490 hi::horizontal_alignment horizontal_alignment,
491 unicode_bidi_class writing_direction) noexcept;
492
495 void resolve_script() noexcept;
496
497 [[nodiscard]] std::pair<text_cursor, text_cursor>
498 get_selection_from_break(text_cursor cursor, unicode_break_vector const &break_opportunities) const noexcept;
499};
500
501} // namespace hi::inline v1
vertical_alignment
Vertical alignment.
Definition alignment.hpp:17
horizontal_alignment
Definition alignment.hpp:31
STL namespace.
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
font_book keeps track of multiple fonts.
Definition font_book.hpp:30
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:259
char_const_iterator get_it(text_cursor cursor) const noexcept
Get the character at the cursor.
Definition text_shaper.hpp:220
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:322
aarectangle rectangle() const noexcept
The rectangle used when laying out the text.
Definition text_shaper.hpp:199
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:242
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:269
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)