HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
Public Types | Public Member Functions
v1::text_shaper Class Reference

#include <hikogui/text/text_shaper.hpp>

Public Types

using char_vector = std::vector<text_shaper_char>
 
using char_iterator = char_vector::iterator
 
using char_const_iterator = char_vector::const_iterator
 
using char_reference = char_vector::reference
 
using char_const_reference = char_vector::const_reference
 
using line_vector = std::vector<text_shaper_line>
 
using line_iterator = line_vector::iterator
 
using line_const_iterator = line_vector::const_iterator
 

Public Member Functions

constexpr text_shaper (text_shaper const &) noexcept=default
 
constexpr text_shaper (text_shaper &&) noexcept=default
 
constexpr text_shaperoperator= (text_shaper const &) noexcept=default
 
constexpr text_shaperoperator= (text_shaper &&) noexcept=default
 
 text_shaper (gstring const &text, text_style const &style, hi::pixel_density pixel_density, hi::alignment alignment, bool left_to_right, iso_15924 script=iso_15924{"Zyyy"}) noexcept
 Construct a text_shaper with a text and alignment.
 
 text_shaper (std::string_view text, text_style const &style, hi::pixel_density pixel_density, hi::alignment alignment, bool left_to_right, iso_15924 script=iso_15924{"Zyyy"}) noexcept
 
bool empty () const noexcept
 
size_t size () const noexcept
 
char_iterator begin () noexcept
 
char_const_iterator begin () const noexcept
 
char_const_iterator cbegin () const noexcept
 
char_iterator end () noexcept
 
char_const_iterator end () const noexcept
 
char_const_iterator cend () const noexcept
 
auto const & lines () const noexcept
 
aarectangle bounding_rectangle (float maximum_line_width, float line_spacing=1.0f, float paragraph_spacing=1.5f) noexcept
 Get bounding rectangle.
 
void layout (aarectangle rectangle, float baseline, extent2 sub_pixel_size, float line_spacing=1.0f, float paragraph_spacing=1.5f) noexcept
 Layout the lines of the text.
 
aarectangle rectangle () const noexcept
 The rectangle used when laying out the text.
 
unicode_bidi_class text_direction () const noexcept
 Get the text-direction as a whole.
 
alignment resolved_alignment () const noexcept
 Get the resolved alignment of the text.
 
char_const_iterator get_it (size_t index) const noexcept
 Get the character at index in logical order.
 
char_const_iterator get_it (text_cursor cursor) const noexcept
 Get the character at the cursor.
 
char_const_iterator get_it (size_t column_nr, size_t line_nr) const noexcept
 Get the character at column and row in display order.
 
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.
 
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.
 
std::pair< size_t, size_t > get_column_line (size_t index) const noexcept
 Get the column and line of a character.
 
std::pair< size_t, size_t > get_column_line (text_cursor cursor) const noexcept
 Get the column and line of a character.
 
size_t get_index (text_shaper::char_const_iterator it) const noexcept
 Get the index of the character in logical order.
 
text_cursor get_begin_cursor () const noexcept
 Get the cursor at the beginning of the document.
 
text_cursor get_end_cursor () const noexcept
 Get the cursor at the end of the document.
 
text_cursor get_before_cursor (size_t index) const noexcept
 Get the cursor before the character in logical order.
 
text_cursor get_after_cursor (size_t index) const noexcept
 Get the cursor after the character in logical order.
 
text_cursor get_before_cursor (text_shaper::char_const_iterator it) const noexcept
 Get the cursor before the character in logical order.
 
text_cursor get_after_cursor (text_shaper::char_const_iterator it) const noexcept
 Get the cursor after the character in logical order.
 
text_cursor get_left_cursor (text_shaper::char_const_iterator it) const noexcept
 Get the cursor left of the character in display order.
 
text_cursor get_right_cursor (text_shaper::char_const_iterator it) const noexcept
 Get the cursor right of the character in display order.
 
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_nearest_cursor (point2 position) const noexcept
 find the nearest character.
 
std::pair< text_cursor, text_cursorselect_char (text_cursor cursor) const noexcept
 Get the selection for the character at the cursor.
 
std::pair< text_cursor, text_cursorselect_word (text_cursor cursor) const noexcept
 Get the selection for the word at the cursor.
 
std::pair< text_cursor, text_cursorselect_sentence (text_cursor cursor) const noexcept
 Get the selection for the sentence at the cursor.
 
std::pair< text_cursor, text_cursorselect_paragraph (text_cursor cursor) const noexcept
 Get the selection for a paragraph at the cursor.
 
std::pair< text_cursor, text_cursorselect_document (text_cursor cursor) const noexcept
 Get the selection for a paragraph at the cursor.
 
char_const_iterator move_left_char (char_const_iterator it) const noexcept
 Get the character to the left.
 
char_const_iterator move_right_char (char_const_iterator it) const noexcept
 Get the character to the right.
 
text_cursor move_left_char (text_cursor cursor, bool overwrite_mode) const noexcept
 
text_cursor move_right_char (text_cursor cursor, bool overwrite_mode) const noexcept
 
text_cursor move_down_char (text_cursor cursor, float &x) const noexcept
 
text_cursor move_up_char (text_cursor cursor, float &x) const noexcept
 
text_cursor move_left_word (text_cursor cursor, bool overwrite_mode) const noexcept
 
text_cursor move_right_word (text_cursor cursor, bool overwrite_mode) const noexcept
 
text_cursor move_begin_line (text_cursor cursor) const noexcept
 
text_cursor move_end_line (text_cursor cursor) const noexcept
 
text_cursor move_begin_sentence (text_cursor cursor) const noexcept
 
text_cursor move_end_sentence (text_cursor cursor) const noexcept
 
text_cursor move_begin_paragraph (text_cursor cursor) const noexcept
 
text_cursor move_end_paragraph (text_cursor cursor) const noexcept
 
text_cursor move_begin_document (text_cursor cursor) const noexcept
 
text_cursor move_end_document (text_cursor cursor) const noexcept
 

Detailed Description

Text shaper.

This class takes text as a set of graphemes attributed with font, size, style and color.

Steps:

  1. Load default glyphs and metrics scaled to the font-size of each glyph.
  2. Fold default glyphs to a certain width by inserting line-separators.
  3. Run unicode bidirectional algorithm.
  4. Reload glyphs and metrics of any brackets.
  5. Morph glyphs.
  6. Position glyphs including kerning and justification.

Constructor & Destructor Documentation

◆ text_shaper()

v1::text_shaper::text_shaper ( gstring const & text,
text_style const & style,
hi::pixel_density pixel_density,
hi::alignment alignment,
bool left_to_right,
iso_15924 script = iso_15924{"Zyyy"} )
inlinenoexcept

Construct a text_shaper with a text and alignment.

The constructor will load all the default glyphs for the text.

Horizontal alignment is done for each line independent of the writing direction. This allows labels to remain aligned in the same direction on the user-interface even when the labels have translations in different languages.

Label widgets should flip the alignment passed to the text shaper when the user interface is mirrored.

Text edit fields may want to change the alignment of the text depending on the dominant writing direction, for more natural typing.

Vertical alignment of the text determines what y=0 means:

  • top: y = 0 is the base-line of the first line, all other lines are at y < 0.
  • bottom: y = 0 is the base-line of the last line, all other lines are at y > 0.
  • middle, odd: y = 0 is the base-line of the middle line.
  • middle, even: y = 0 is half way between the base-lines of the middle two lines.
Parameters
textThe text as a vector of attributed graphemes. Use U+2029 as paragraph separator, and if needed U+2028 as line separator.
styleThe initial text-style to use to display the text.
pixel_densityThe pixel density of the current display.
alignmentThe alignment how to align the text.
text_directionThe default text direction when it can not be deduced from the text.
scriptThe script of the text.

Member Function Documentation

◆ bounding_rectangle()

aarectangle v1::text_shaper::bounding_rectangle ( float maximum_line_width,
float line_spacing = 1.0f,
float paragraph_spacing = 1.5f )
inlinenoexcept

Get bounding rectangle.

It will estimate the width and height based on the glyphs before glyph-morphing and kerning and fold the lines using the unicode line breaking algorithm to the max_line_width.

The alignment parameter is used to align the lines vertically:

  • top: y=0 is the base-line of the top line, with following lines below it.
  • bottom: y=0 is the base-line of the bottom line, with previous lines above it.
  • middle, odd number of lines: y=0 is the base-line of the middle line.
  • middle, even number of lines: y=0 is half-way between the base-line of the two lines in the middle.
Parameters
maximum_line_widthThe maximum line width allowed, this may be infinite to determine the natural text size without folding.
line_spacingThe scaling of the spacing between lines.
paragraph_spacingThe scaling of the spacing between paragraphs.
Returns
The rectangle surrounding the text, cap-height. The rectangle excludes ascenders & descenders, as if each line is x-height. y = 0 of the rectangle is at the base-line of the text.

◆ get_after_cursor() [1/2]

text_cursor v1::text_shaper::get_after_cursor ( size_t index) const
inlinenoexcept

Get the cursor after the character in logical order.

Parameters
indexThe index to the character or one beyond.
Returns
A cursor after the character in logical order.

◆ get_after_cursor() [2/2]

text_cursor v1::text_shaper::get_after_cursor ( text_shaper::char_const_iterator it) const
inlinenoexcept

Get the cursor after the character in logical order.

Parameters
itThe iterator to the character or end().
Returns
A cursor after the character in logical order.

◆ get_before_cursor() [1/2]

text_cursor v1::text_shaper::get_before_cursor ( size_t index) const
inlinenoexcept

Get the cursor before the character in logical order.

Parameters
indexThe index to the character or one beyond.
Returns
A cursor before the character in logical order.

◆ get_before_cursor() [2/2]

text_cursor v1::text_shaper::get_before_cursor ( text_shaper::char_const_iterator it) const
inlinenoexcept

Get the cursor before the character in logical order.

Parameters
itThe iterator to the character or end().
Returns
A cursor before the character in logical order.

◆ get_begin_cursor()

text_cursor v1::text_shaper::get_begin_cursor ( ) const
inlinenoexcept

Get the cursor at the beginning of the document.

Returns
The cursor at the beginning of the document.

◆ get_column_line() [1/3]

std::pair< size_t, size_t > v1::text_shaper::get_column_line ( size_t index) const
inlinenoexcept

Get the column and line of a character.

Parameters
indexThe index of the character in logical order.
Returns
The (column, row) packed in a std::pair.

◆ get_column_line() [2/3]

std::pair< size_t, size_t > v1::text_shaper::get_column_line ( text_cursor cursor) const
inlinenoexcept

Get the column and line of a character.

Parameters
cursorThe cursor to the character.
Returns
The (column, row) packed in a std::pair.

◆ get_column_line() [3/3]

std::pair< size_t, size_t > v1::text_shaper::get_column_line ( text_shaper::char_const_iterator it) const
inlinenoexcept

Get the column and line of a character.

Parameters
itThe iterator to the character, or end().
Returns
The (column, row) packed in a std::pair.

◆ get_end_cursor()

text_cursor v1::text_shaper::get_end_cursor ( ) const
inlinenoexcept

Get the cursor at the end of the document.

Returns
The cursor at the end of the document.

◆ get_index()

size_t v1::text_shaper::get_index ( text_shaper::char_const_iterator it) const
inlinenoexcept

Get the index of the character in logical order.

Parameters
itThe iterator to the character or end().
Returns
The index in logical order.

◆ get_it() [1/4]

char_const_iterator v1::text_shaper::get_it ( size_t column_nr,
size_t line_nr ) const
inlinenoexcept

Get the character at column and row in display order.

Note
This function checks for underflow and overflow of column and row and always returns an iterator between begin() and end() inclusive.
Parameters
column_nrThe column
line_nrThe row
Returns
Iterator to the character.

◆ get_it() [2/4]

char_const_iterator v1::text_shaper::get_it ( size_t index) const
inlinenoexcept

Get the character at index in logical order.

Note
This function checks for underflow and overflow of index and always returns an iterator between begin() and end() inclusive.
Parameters
indexThe index in the text.
Returns
Iterator to the character.

◆ get_it() [3/4]

char_const_iterator v1::text_shaper::get_it ( std::pair< size_t, size_t > column_row) const
inlinenoexcept

Get the character at column and row in display order.

Note
This function checks for underflow and overflow of column and row and always returns an iterator between begin() and end() inclusive.
Parameters
column_rowThe column, row packed in a std::pair.
Returns
Iterator to the character.

◆ get_it() [4/4]

char_const_iterator v1::text_shaper::get_it ( text_cursor cursor) const
inlinenoexcept

Get the character at the cursor.

Note
This function checks for underflow and overflow of cursor and always returns an iterator between begin() and end() inclusive.
Parameters
cursorThe cursor in the text.
Returns
Iterator to the character.

◆ get_left_cursor()

text_cursor v1::text_shaper::get_left_cursor ( text_shaper::char_const_iterator it) const
inlinenoexcept

Get the cursor left of the character in display order.

Parameters
itThe iterator to the character or end().
Returns
A cursor left of the character in display order.

◆ get_nearest_cursor()

text_cursor v1::text_shaper::get_nearest_cursor ( point2 position) const
inlinenoexcept

find the nearest character.

Parameters
positionThe point near
Returns
The text_cursor nearest to the point.

◆ get_right_cursor()

text_cursor v1::text_shaper::get_right_cursor ( text_shaper::char_const_iterator it) const
inlinenoexcept

Get the cursor right of the character in display order.

Parameters
itThe iterator to the character or end().
Returns
A cursor right of the character in display order.

◆ is_on_left()

bool v1::text_shaper::is_on_left ( text_cursor cursor) const
inlinenoexcept

Check if the cursor is on the left side of the character in display order.

Parameters
cursorThe cursor to query.
Returns
True if the cursor is on the left of the character.

◆ is_on_right()

bool v1::text_shaper::is_on_right ( text_cursor cursor) const
inlinenoexcept

Check if the cursor is on the right side of the character in display order.

Parameters
cursorThe cursor to query.
Returns
True if the cursor is on the right of the character.

◆ layout()

void v1::text_shaper::layout ( aarectangle rectangle,
float baseline,
extent2 sub_pixel_size,
float line_spacing = 1.0f,
float paragraph_spacing = 1.5f )
inlinenoexcept

Layout the lines of the text.

It will estimate the width and height based on the glyphs before glyph-morphing and kerning and fold the lines using the unicode line breaking algorithm to the width of the rectangle.

Postcondition
The lines have been laid out.
Parameters
rectangleThe rectangle to position the glyphs in.
baselineThe position of the recommended base-line.
sub_pixel_sizeThe size of a sub-pixel in device-independent-pixels.
line_spacingThe scaling of the spacing between lines (default: 1.0).
paragraph_spacingThe scaling of the spacing between paragraphs (default: 1.5).

◆ move_left_char()

char_const_iterator v1::text_shaper::move_left_char ( char_const_iterator it) const
inlinenoexcept

Get the character to the left.

Parameters
itThe iterator to the character.
Returns
The iterator to the character on the left, or empty.

◆ move_right_char()

char_const_iterator v1::text_shaper::move_right_char ( char_const_iterator it) const
inlinenoexcept

Get the character to the right.

Parameters
itThe iterator to the character.
Returns
The iterator to the character on the left, or empty.

◆ rectangle()

aarectangle v1::text_shaper::rectangle ( ) const
inlinenoexcept

The rectangle used when laying out the text.

◆ resolved_alignment()

alignment v1::text_shaper::resolved_alignment ( ) const
inlinenoexcept

Get the resolved alignment of the text.

This is the alignment when taking into account the direction of the text and the direction of the selected language.

◆ select_char()

std::pair< text_cursor, text_cursor > v1::text_shaper::select_char ( text_cursor cursor) const
inlinenoexcept

Get the selection for the character at the cursor.

◆ select_document()

std::pair< text_cursor, text_cursor > v1::text_shaper::select_document ( text_cursor cursor) const
inlinenoexcept

Get the selection for a paragraph at the cursor.

◆ select_paragraph()

std::pair< text_cursor, text_cursor > v1::text_shaper::select_paragraph ( text_cursor cursor) const
inlinenoexcept

Get the selection for a paragraph at the cursor.

◆ select_sentence()

std::pair< text_cursor, text_cursor > v1::text_shaper::select_sentence ( text_cursor cursor) const
inlinenoexcept

Get the selection for the sentence at the cursor.

◆ select_word()

std::pair< text_cursor, text_cursor > v1::text_shaper::select_word ( text_cursor cursor) const
inlinenoexcept

Get the selection for the word at the cursor.

◆ text_direction()

unicode_bidi_class v1::text_shaper::text_direction ( ) const
inlinenoexcept

Get the text-direction as a whole.


The documentation for this class was generated from the following file: