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
164 float maximum_line_width,
166 float line_spacing = 1.0f,
167 float paragraph_spacing = 1.5f) noexcept;
168
189 [[nodiscard]] void layout(
191 float base_line,
192 extent2 sub_pixel_size,
193 unicode_bidi_class writing_direction,
194 hi::alignment alignment = hi::alignment{horizontal_alignment::flush, vertical_alignment::middle},
195 float line_spacing = 1.0f,
196 float paragraph_spacing = 1.5f) noexcept;
197
200 [[nodiscard]] aarectangle rectangle() const noexcept
201 {
202 return _rectangle;
203 }
204
212 [[nodiscard]] char_const_iterator get_it(size_t index) const noexcept;
213
221 [[nodiscard]] char_const_iterator get_it(text_cursor cursor) const noexcept
222 {
223 return get_it(cursor.index());
224 }
225
234 [[nodiscard]] char_const_iterator get_it(size_t column, size_t row) const noexcept;
235
243 [[nodiscard]] char_const_iterator get_it(std::pair<size_t, size_t> column_row) const noexcept
244 {
245 return get_it(column_row.first, column_row.second);
246 }
247
253 [[nodiscard]] std::pair<size_t, size_t> get_column_line(text_shaper::char_const_iterator it) const noexcept;
254
260 [[nodiscard]] std::pair<size_t, size_t> get_column_line(size_t index) const noexcept
261 {
262 return get_column_line(get_it(index));
263 }
264
270 [[nodiscard]] std::pair<size_t, size_t> get_column_line(text_cursor cursor) const noexcept
271 {
272 return get_column_line(cursor.index());
273 }
274
280 [[nodiscard]] size_t get_index(text_shaper::char_const_iterator it) const noexcept;
281
286 [[nodiscard]] text_cursor get_begin_cursor() const noexcept;
287
292 [[nodiscard]] text_cursor get_end_cursor() const noexcept;
293
299 [[nodiscard]] text_cursor get_before_cursor(size_t index) const noexcept;
300
306 [[nodiscard]] text_cursor get_after_cursor(size_t index) const noexcept;
307
313 [[nodiscard]] text_cursor get_before_cursor(text_shaper::char_const_iterator it) const noexcept
314 {
315 return get_before_cursor(get_index(it));
316 }
317
323 [[nodiscard]] text_cursor get_after_cursor(text_shaper::char_const_iterator it) const noexcept
324 {
325 return get_after_cursor(get_index(it));
326 }
327
333 [[nodiscard]] text_cursor get_left_cursor(text_shaper::char_const_iterator it) const noexcept;
334
340 [[nodiscard]] text_cursor get_right_cursor(text_shaper::char_const_iterator it) const noexcept;
341
347 [[nodiscard]] bool is_on_left(text_cursor cursor) const noexcept;
348
354 [[nodiscard]] bool is_on_right(text_cursor cursor) const noexcept;
355
361 [[nodiscard]] text_cursor get_nearest_cursor(point2 point) const noexcept;
362
365 [[nodiscard]] std::pair<text_cursor, text_cursor> select_char(text_cursor cursor) const noexcept;
366
369 [[nodiscard]] std::pair<text_cursor, text_cursor> select_word(text_cursor cursor) const noexcept;
370
374
378
382
388 [[nodiscard]] char_const_iterator move_left_char(char_const_iterator it) const noexcept;
389
395 [[nodiscard]] char_const_iterator move_right_char(char_const_iterator it) const noexcept;
396
397 [[nodiscard]] text_cursor move_left_char(text_cursor cursor, bool overwrite_mode) const noexcept;
398 [[nodiscard]] text_cursor move_right_char(text_cursor cursor, bool overwrite_mode) const noexcept;
399 [[nodiscard]] text_cursor move_down_char(text_cursor cursor, float &x) const noexcept;
400 [[nodiscard]] text_cursor move_up_char(text_cursor cursor, float &x) const noexcept;
401 [[nodiscard]] text_cursor move_left_word(text_cursor cursor, bool overwrite_mode) const noexcept;
402 [[nodiscard]] text_cursor move_right_word(text_cursor cursor, bool overwrite_mode) const noexcept;
403 [[nodiscard]] text_cursor move_begin_line(text_cursor cursor) const noexcept;
404 [[nodiscard]] text_cursor move_end_line(text_cursor cursor) const noexcept;
405 [[nodiscard]] text_cursor move_begin_sentence(text_cursor cursor) const noexcept;
406 [[nodiscard]] text_cursor move_end_sentence(text_cursor cursor) const noexcept;
407 [[nodiscard]] text_cursor move_begin_paragraph(text_cursor cursor) const noexcept;
408 [[nodiscard]] text_cursor move_end_paragraph(text_cursor cursor) const noexcept;
409 [[nodiscard]] text_cursor move_begin_document(text_cursor cursor) const noexcept;
410 [[nodiscard]] text_cursor move_end_document(text_cursor cursor) const noexcept;
411
412private:
413 font_book *_font_book = nullptr;
414
417 float _dpi_scale;
418
425 char_vector _text;
426
429 unicode_break_vector _line_break_opportunities;
430
433 std::vector<float> _line_break_widths;
434
437 unicode_break_vector _word_break_opportunities;
438
441 unicode_break_vector _sentence_break_opportunities;
442
445 unicode_script _script;
446
451 line_vector _lines;
452
455 font_metrics _initial_line_metrics;
456
459 aarectangle _rectangle;
460
471 [[nodiscard]] line_vector make_lines(
473 float base_line,
474 extent2 sub_pixel_size,
475 hi::vertical_alignment vertical_alignment,
476 unicode_bidi_class writing_direction,
477 float line_spacing,
478 float paragraph_spacing) noexcept;
479
488 void position_glyphs(
490 extent2 sub_pixel_size,
491 hi::horizontal_alignment horizontal_alignment,
492 unicode_bidi_class writing_direction) noexcept;
493
496 void resolve_script() noexcept;
497
498 [[nodiscard]] std::pair<text_cursor, text_cursor>
499 get_selection_from_break(text_cursor cursor, unicode_break_vector const &break_opportunities) const noexcept;
500};
501
502} // 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
Definition text_cursor.hpp:18
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:260
char_const_iterator get_it(text_cursor cursor) const noexcept
Get the character at the cursor.
Definition text_shaper.hpp:221
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:323
aarectangle rectangle() const noexcept
The rectangle used when laying out the text.
Definition text_shaper.hpp:200
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.
std::pair< aarectangle, float > bounding_rectangle(float maximum_line_width, vertical_alignment alignment, float line_spacing=1.0f, float paragraph_spacing=1.5f) noexcept
Get bounding rectangle.
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:243
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:270
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:18
T begin(T... args)
T end(T... args)