The Battle for Wesnoth  1.19.5+dev
Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
font::pango_text Class Reference

Text class. More...

#include <text.hpp>

Public Types

enum  FONT_STYLE { STYLE_NORMAL = 0 , STYLE_BOLD = 1 , STYLE_ITALIC = 2 , STYLE_UNDERLINE = 4 }
 

Public Member Functions

 pango_text ()
 
 pango_text (const pango_text &)=delete
 
pango_textoperator= (const pango_text &)=delete
 
texture render_and_get_texture ()
 Returns the cached texture, or creates a new one otherwise. More...
 
point get_size ()
 Returns the size of the text, in drawing coordinates. More...
 
bool is_truncated () const
 Has the text been truncated? This happens if it exceeds max width or height. More...
 
unsigned insert_text (const unsigned offset, const std::string &text, const bool use_markup=false)
 Inserts UTF-8 text. More...
 
int get_max_glyph_height () const
 Returns the maximum glyph height of a font, in drawing coordinates. More...
 
point get_cursor_position (const unsigned column, const unsigned line=0) const
 Gets the location for the cursor, in drawing coordinates. More...
 
point get_cursor_pos_from_index (const unsigned offset) const
 Gets the location for the cursor, in drawing coordinates. More...
 
std::size_t get_maximum_length () const
 Get maximum length. More...
 
std::string get_token (const point &position, const char *delimiters=" \n\r\t") const
 Gets the largest collection of characters, including the token at position, and not including any characters from the delimiters set. More...
 
std::string get_link (const point &position) const
 Checks if position points to a character in a link in the text, returns it if so, empty string otherwise. More...
 
point get_column_line (const point &position) const
 Gets the column of line of the character at the position. More...
 
int xy_to_index (const point &position) const
 
std::vector< std::string > get_lines () const
 Retrieves a list of strings with contents for each rendered line. More...
 
PangoLayoutLine * get_line (int index)
 Get a specific line from the pango layout. More...
 
int get_line_num_from_offset (const unsigned offset)
 Given a byte index, find out at which line the corresponding character is located. More...
 
unsigned get_lines_count () const
 Get number of lines in the text. More...
 
std::size_t get_length () const
 Gets the length of the text in bytes. More...
 
bool set_text (const std::string &text, const bool markedup)
 Sets the text to render. More...
 
const std::string & text () const
 
pango_textset_family_class (font::family_class fclass)
 
pango_textset_font_size (unsigned font_size)
 
pango_textset_font_style (const FONT_STYLE font_style)
 
pango_textset_foreground_color (const color_t &color)
 
pango_textset_maximum_width (int width)
 
pango_textset_characters_per_line (const unsigned characters_per_line)
 
pango_textset_maximum_height (int height, bool multiline)
 
pango_textset_ellipse_mode (const PangoEllipsizeMode ellipse_mode)
 
pango_textset_alignment (const PangoAlignment alignment)
 
pango_textset_maximum_length (const std::size_t maximum_length)
 
bool link_aware () const
 
pango_textset_link_aware (bool b)
 
pango_textset_link_color (const color_t &color)
 
pango_textset_add_outline (bool do_add)
 
void add_attribute_weight (const unsigned start_offset, const unsigned end_offset, PangoWeight weight)
 Add pango font weight attribute to a specific portion of text. More...
 
void add_attribute_style (const unsigned start_offset, const unsigned end_offset, PangoStyle style)
 Add pango font style attribute to a specific portion of text, used to set italic/oblique text. More...
 
void add_attribute_underline (const unsigned start_offset, const unsigned end_offset, PangoUnderline underline)
 Add pango underline attribute to a specific portion of text. More...
 
void add_attribute_fg_color (const unsigned start_offset, const unsigned end_offset, const color_t &color)
 Add pango fg color attribute to a specific portion of text. More...
 
void add_attribute_bg_color (const unsigned start_offset, const unsigned end_offset, const color_t &color)
 Mark a specific portion of text for highlighting. More...
 
void add_attribute_size (const unsigned start_offset, const unsigned end_offset, int size)
 Add pango font size attribute to a specific portion of text. More...
 
void add_attribute_font_family (const unsigned start_offset, const unsigned end_offset, std::string family)
 Add pango font family attribute to a specific portion of text. More...
 
void clear_attribute_list ()
 Clears all attributes from the global attribute list. More...
 

Private Member Functions

texture render_texture (const SDL_Rect &viewport)
 Wrapper around render_surface which sets texture::w() and texture::h() in the same way that render_and_get_texture does. More...
 
surface render_surface (const SDL_Rect &viewport)
 Returns the rendered text. More...
 
void recalculate () const
 Recalculates the text layout. More...
 
PangoRectangle calculate_size (PangoLayout &layout) const
 Calculates surface size. More...
 
surface create_surface ()
 Equivalent to create_surface(viewport), where the viewport's top-left is at (0,0) and the area is large enough to contain the full text. More...
 
surface create_surface (const SDL_Rect &viewport)
 Renders the text to a surface that uses surface_buffer_ as its data store, the buffer will be allocated or reallocated as necessary. More...
 
void render (PangoLayout &layout, const SDL_Rect &viewport, const unsigned stride)
 This is part of create_surface(viewport). More...
 
bool set_markup (std::string_view text, PangoLayout &layout)
 Sets the markup'ed text. More...
 
bool validate_markup (std::string_view text, char **raw_text, std::string &semi_escaped) const
 
std::string format_links (std::string_view text) const
 Replaces all instances of URLs in a given string with formatted links and returns the result. More...
 
texture with_draw_scale (const texture &t) const
 Adjust a texture's draw-width and height according to pixel scale. More...
 
int to_draw_scale (int s) const
 Scale the given render-space size to draw-space, rounding up. More...
 
point to_draw_scale (const point &p) const
 Scale the given render-space point to draw-space, rounding up. More...
 
void update_pixel_scale ()
 Update pixel scale, if necessary. More...
 

Static Private Member Functions

static void copy_layout_properties (PangoLayout &src, PangoLayout &dst)
 

Private Attributes

std::unique_ptr< PangoContext, std::function< void(void *)> > context_
 
std::unique_ptr< PangoLayout, std::function< void(void *)> > layout_
 
PangoRectangle rect_
 
std::string text_
 The text to draw (stored as UTF-8). More...
 
bool markedup_text_
 Does the text contain pango markup? If different render routines must be used. More...
 
bool link_aware_
 Are hyperlinks in the text marked-up, and will get_link return them. More...
 
color_t link_color_
 The color to render links in. More...
 
font::family_class font_class_
 The font family class used. More...
 
unsigned font_size_
 The font size to draw. More...
 
FONT_STYLE font_style_
 The style of the font, this is an orred mask of the font flags. More...
 
color_t foreground_color_
 The foreground color. More...
 
bool add_outline_
 Whether to add an outline effect. More...
 
int maximum_width_
 The maximum width of the text. More...
 
unsigned characters_per_line_
 The number of characters per line. More...
 
int maximum_height_
 The maximum height of the text. More...
 
PangoEllipsizeMode ellipse_mode_
 The way too long text is shown depends on this mode. More...
 
PangoAlignment alignment_
 The alignment of the text. More...
 
std::size_t maximum_length_
 The maximum length of the text. More...
 
bool calculation_dirty_
 The text has two dirty states: More...
 
std::size_t length_
 Length of the text. More...
 
unsigned attribute_start_offset_
 
unsigned attribute_end_offset_
 
color_t highlight_color_
 
PangoAttrList * global_attribute_list_
 Global pango attribute list. More...
 
int pixel_scale_
 The pixel scale, used to render high-DPI text. More...
 
std::vector< uint8_t > surface_buffer_
 Buffer to store the image on. More...
 

Detailed Description

Text class.

This class represents text which is rendered using Pango.

It takes text, as a utf-8 std::string, plus formatting options including font and color. It provides a surface object which holds the rendered text.

Besides this, it can do some additional calculations using the font layout.

It can take an index into the text, and convert it to pixel coordinates, so that if we want to draw a cursor in an editbox, we know where to draw it.

It can also take a pixel coordinate with respect to the text layout, and translate it back to an index into the original text. This is useful if the user clicks on the text, and we want to know where to move the cursor.

The get_token method takes a pixel coordinate, which we assume represents a click position, and gets the corresponding "token" from the string. The default token delimiters are whitespace " \n\r\t". So, this returns the "word" that the user clicked on.

Finally, the get_link method represents special support for hyperlinks in text. A token "looks like a link" if it begins "http://" or "https://". If a text has link_aware enabled, then any such token is rendered with an underline and in a special color, see link_color. The get_link method calls get_token and further checks if the clicked token looks like a link.

This class stores the text to draw and uses pango with the cairo backend to render the text. See http://pango.org for more info.

Definition at line 78 of file text.hpp.

Member Enumeration Documentation

◆ FONT_STYLE

Enumerator
STYLE_NORMAL 
STYLE_BOLD 
STYLE_ITALIC 
STYLE_UNDERLINE 

Definition at line 144 of file text.hpp.

Constructor & Destructor Documentation

◆ pango_text() [1/2]

font::pango_text::pango_text ( )

◆ pango_text() [2/2]

font::pango_text::pango_text ( const pango_text )
delete

Member Function Documentation

◆ add_attribute_bg_color()

void font::pango_text::add_attribute_bg_color ( const unsigned  start_offset,
const unsigned  end_offset,
const color_t color 
)

Mark a specific portion of text for highlighting.

Used for selection box. BGColor is set in set_text(), this just marks the area to be colored. Markup not used because the user may enter their own markup or special characters

Parameters
start_offsetByte index of the cursor where selection/highlight starts
end_offsetByte index of the cursor where selection/highlight ends
colorHighlight/Background color

Definition at line 458 of file text.cpp.

References attribute_end_offset_, attribute_start_offset_, DBG_GUI_D, global_attribute_list_, and highlight_color_.

Referenced by gui2::text_shape::draw(), and gui2::text_box_base::set_highlight_area().

◆ add_attribute_fg_color()

void font::pango_text::add_attribute_fg_color ( const unsigned  start_offset,
const unsigned  end_offset,
const color_t color 
)

Add pango fg color attribute to a specific portion of text.

This changes the foreground color of the corresponding part of the text.

Parameters
start_offsetByte index of the cursor where color change starts
end_offsetByte index of the cursor where color change ends
colorForeground color

Definition at line 419 of file text.cpp.

References attribute_end_offset_, attribute_start_offset_, DBG_GUI_D, and global_attribute_list_.

Referenced by gui2::text_shape::draw().

◆ add_attribute_font_family()

void font::pango_text::add_attribute_font_family ( const unsigned  start_offset,
const unsigned  end_offset,
std::string  family 
)

Add pango font family attribute to a specific portion of text.

This changes the font family of the corresponding part of the text.

Parameters
start_offsetByte index of the cursor where size change starts
end_offsetByte index of the cursor where size change ends
familyThe font family

Definition at line 439 of file text.cpp.

References attribute_end_offset_, attribute_start_offset_, DBG_GUI_D, and global_attribute_list_.

Referenced by gui2::text_shape::draw().

◆ add_attribute_size()

void font::pango_text::add_attribute_size ( const unsigned  start_offset,
const unsigned  end_offset,
int  size 
)

Add pango font size attribute to a specific portion of text.

This changes the font size of the corresponding part of the text.

Parameters
start_offsetByte index of the cursor where size change starts
end_offsetByte index of the cursor where size change ends
sizeFont size

Definition at line 332 of file text.cpp.

References attribute_end_offset_, attribute_start_offset_, DBG_GUI_D, prefs::font_scaled(), prefs::get(), global_attribute_list_, pixel_scale_, and utf8::size().

Referenced by gui2::text_shape::draw().

◆ add_attribute_style()

void font::pango_text::add_attribute_style ( const unsigned  start_offset,
const unsigned  end_offset,
PangoStyle  style 
)

Add pango font style attribute to a specific portion of text, used to set italic/oblique text.

Parameters
start_offsetByte index of the cursor where font style change starts
end_offsetByte index of the cursor where font style change ends
stylePango font style (normal/italic/oblique)

Definition at line 369 of file text.cpp.

References attribute_end_offset_, attribute_start_offset_, DBG_GUI_D, and global_attribute_list_.

Referenced by gui2::text_shape::draw().

◆ add_attribute_underline()

void font::pango_text::add_attribute_underline ( const unsigned  start_offset,
const unsigned  end_offset,
PangoUnderline  underline 
)

Add pango underline attribute to a specific portion of text.

This adds an underline to the corresponding part of the text.

Parameters
start_offsetByte index of the cursor where underline starts
end_offsetByte index of the cursor where underline change ends
underlinePango underline style

Definition at line 388 of file text.cpp.

References attribute_end_offset_, attribute_start_offset_, DBG_GUI_D, and global_attribute_list_.

Referenced by gui2::text_shape::draw().

◆ add_attribute_weight()

void font::pango_text::add_attribute_weight ( const unsigned  start_offset,
const unsigned  end_offset,
PangoWeight  weight 
)

Add pango font weight attribute to a specific portion of text.

This changes the font weight of the corresponding part of the text.

Parameters
start_offsetByte index of the cursor where font weight change starts
end_offsetByte index of the cursor where font weight change ends
weightPango font weight

Definition at line 351 of file text.cpp.

References attribute_end_offset_, attribute_start_offset_, DBG_GUI_D, and global_attribute_list_.

Referenced by gui2::text_shape::draw().

◆ calculate_size()

PangoRectangle font::pango_text::calculate_size ( PangoLayout &  layout) const
private

◆ clear_attribute_list()

void font::pango_text::clear_attribute_list ( )

Clears all attributes from the global attribute list.

Definition at line 479 of file text.cpp.

References global_attribute_list_, and layout_.

Referenced by gui2::text_shape::draw().

◆ copy_layout_properties()

void font::pango_text::copy_layout_properties ( PangoLayout &  src,
PangoLayout &  dst 
)
staticprivate

Definition at line 1091 of file text.cpp.

References dst, and src.

◆ create_surface() [1/2]

surface font::pango_text::create_surface ( )
private

Equivalent to create_surface(viewport), where the viewport's top-left is at (0,0) and the area is large enough to contain the full text.

The top-left of the viewport will be at (0,0), regardless of the values of x and y in the rect_ member variable. If the x or y co-ordinates are non-zero, then x columns and y rows of blank space are included in the amount of memory allocated.

Definition at line 932 of file text.cpp.

References rect_.

Referenced by render_and_get_texture(), and render_surface().

◆ create_surface() [2/2]

surface font::pango_text::create_surface ( const SDL_Rect &  viewport)
private

Renders the text to a surface that uses surface_buffer_ as its data store, the buffer will be allocated or reallocated as necessary.

The surface's origin will correspond to viewport.x and viewport.y, the width and height will be at least viewport.w and viewport.h (although they may be larger).

Parameters
viewportThe area to draw, which can be a subset of the text. This rectangle's coordinates use render-space's scale.

Definition at line 937 of file text.cpp.

References DBG_FT, font::from_cairo_format(), layout_, render(), and surface_buffer_.

◆ format_links()

std::string font::pango_text::format_links ( std::string_view  text) const
private

Replaces all instances of URLs in a given string with formatted links and returns the result.

Definition at line 1015 of file text.cpp.

References font::format_as_link(), link_color_, font::looks_like_url(), and text().

Referenced by set_markup().

◆ get_column_line()

point font::pango_text::get_column_line ( const point position) const

Gets the column of line of the character at the position.

Parameters
positionThe pixel position in the text area.
Returns
A point with the x value the column and the y value the line of the character found (or last character if not found.
Todo:
There's still a bug left. When you select a text which is in the ellipses on the right side the text gets reformatted with ellipses on the left and the selected character is not the one under the cursor. Other widget toolkits don't show ellipses and have no indication more text is available. Haven't found what the best thing to do would be. Until that time leave it as is.

Definition at line 286 of file text.cpp.

References get_cursor_position(), i, utf8::index(), layout_, draw::line(), draw::point(), and recalculate().

Referenced by gui2::text_box_base::get_column_line().

◆ get_cursor_pos_from_index()

point font::pango_text::get_cursor_pos_from_index ( const unsigned  offset) const

Gets the location for the cursor, in drawing coordinates.

Parameters
offsetThe column byte index of the cursor.
Returns
The position of the top of the cursor. It the requested location is out of range 0,0 is returned.

Definition at line 225 of file text.cpp.

References layout_, draw::rect(), and to_draw_scale().

Referenced by gui2::text_box_base::get_cursor_pos_from_index(), and get_cursor_position().

◆ get_cursor_position()

point font::pango_text::get_cursor_position ( const unsigned  column,
const unsigned  line = 0 
) const

Gets the location for the cursor, in drawing coordinates.

Parameters
columnThe column character index of the cursor.
lineThe line character index of the cursor.
Returns
The position of the top of the cursor. It the requested location is out of range 0,0 is returned.

Definition at line 185 of file text.cpp.

References get_cursor_pos_from_index(), i, layout_, draw::line(), draw::point(), and recalculate().

Referenced by get_column_line(), gui2::text_box_base::get_cursor_position(), and gui2::rich_label::get_xy_from_offset().

◆ get_length()

std::size_t font::pango_text::get_length ( ) const
inline

◆ get_line()

PangoLayoutLine * font::pango_text::get_line ( int  index)

Get a specific line from the pango layout.

Parameters
indexthe line number of the line to retrieve
Returns
the PangoLayoutLine* corresponding to line number index

Definition at line 1124 of file text.cpp.

References utf8::index(), and layout_.

Referenced by gui2::text_box_base::get_line().

◆ get_line_num_from_offset()

int font::pango_text::get_line_num_from_offset ( const unsigned  offset)

Given a byte index, find out at which line the corresponding character is located.

Parameters
offsetthe byte index
Returns
the line number corresponding to the given index

Definition at line 1129 of file text.cpp.

References layout_.

Referenced by gui2::text_box_base::get_line_number().

◆ get_lines()

std::vector< std::string > font::pango_text::get_lines ( ) const

Retrieves a list of strings with contents for each rendered line.

This method is not const because it requires rendering the text.

Note
This is only intended for renderer implementation details. This is a rather expensive function because it copies everything at least once.

Definition at line 1098 of file text.cpp.

References i, draw_manager::layout(), layout_, and recalculate().

Referenced by gui2::text_box_base::get_lines().

◆ get_lines_count()

unsigned font::pango_text::get_lines_count ( ) const
inline

Get number of lines in the text.

Returns
The number of lines in the text.

Definition at line 265 of file text.hpp.

References layout_.

Referenced by gui2::text_box_base::get_lines_count().

◆ get_link()

std::string font::pango_text::get_link ( const point position) const

Checks if position points to a character in a link in the text, returns it if so, empty string otherwise.

Link-awareness must be enabled to get results.

Parameters
positionThe pixel position in the text area.
Returns
The link if one is found, the empty string otherwise.

Definition at line 271 of file text.cpp.

References get_token(), link_aware_, and font::looks_like_url().

Referenced by gui2::styled_widget::get_label_link().

◆ get_max_glyph_height()

int font::pango_text::get_max_glyph_height ( ) const

Returns the maximum glyph height of a font, in drawing coordinates.

Returns
The height of the tallest possible glyph for the selected font. More specifically, the result is the sum of the maximum ascent and descent lengths.

Definition at line 687 of file text.cpp.

References context_, f, font_class_, font_size_, font_style_, font::get_font_families(), and pixel_scale_.

Referenced by font::get_max_height().

◆ get_maximum_length()

std::size_t font::pango_text::get_maximum_length ( ) const

Get maximum length.

Returns
The maximum length of the text. The length of text should not exceed this value.

Definition at line 234 of file text.cpp.

References maximum_length_.

◆ get_size()

point font::pango_text::get_size ( )

Returns the size of the text, in drawing coordinates.

Definition at line 150 of file text.cpp.

References recalculate(), rect_, to_draw_scale(), and update_pixel_scale().

Referenced by gui2::text_shape::draw(), display::draw_report(), and gui2::styled_widget::get_best_text_size().

◆ get_token()

std::string font::pango_text::get_token ( const point position,
const char *  delimiters = " \n\r\t" 
) const

Gets the largest collection of characters, including the token at position, and not including any characters from the delimiters set.

Parameters
positionThe pixel position in the text area.
delimiters
Returns
The token containing position, and none of the delimiter characters. If position is out of bounds, it returns the empty string.

Definition at line 239 of file text.cpp.

References d, utf8::index(), layout_, and recalculate().

Referenced by gui2::styled_widget::get_label_token(), and get_link().

◆ insert_text()

unsigned font::pango_text::insert_text ( const unsigned  offset,
const std::string &  text,
const bool  use_markup = false 
)

Inserts UTF-8 text.

Parameters
offsetThe position to insert the text.
textThe UTF-8 text to insert.
use_markupIf the text is formatted or not.
Returns
The number of characters inserted.

Definition at line 165 of file text.cpp.

References utf8::index(), utf8::insert(), length_, maximum_length_, set_text(), utf8::size(), text(), and text_.

Referenced by gui2::text_box_base::handle_editing(), gui2::text_box_base::insert_char(), and gui2::text_box_base::paste_selection().

◆ is_truncated()

bool font::pango_text::is_truncated ( ) const

Has the text been truncated? This happens if it exceeds max width or height.

Definition at line 158 of file text.cpp.

References layout_, and recalculate().

Referenced by gui2::styled_widget::get_best_text_size(), and gui2::styled_widget::place().

◆ link_aware()

bool font::pango_text::link_aware ( ) const
inline

Definition at line 313 of file text.hpp.

References link_aware_.

◆ operator=()

pango_text& font::pango_text::operator= ( const pango_text )
delete

◆ recalculate()

void font::pango_text::recalculate ( ) const
private

◆ render()

void font::pango_text::render ( PangoLayout &  layout,
const SDL_Rect &  viewport,
const unsigned  stride 
)
private

This is part of create_surface(viewport).

The separation is a legacy from workarounds to the size limits of cairo_surface_t.

Definition at line 881 of file text.cpp.

References add_outline_, foreground_color_, draw_manager::layout(), and surface_buffer_.

Referenced by create_surface().

◆ render_and_get_texture()

texture font::pango_text::render_and_get_texture ( )

Returns the cached texture, or creates a new one otherwise.

texture::w() and texture::h() methods will return the expected width and height of the texture in draw space. This may differ from the real value returned by texture::get_info().

In almost all cases, use w() and h() to get the size of the rendered text for drawing.

Definition at line 118 of file text.cpp.

References create_surface(), recalculate(), update_pixel_scale(), and with_draw_scale().

Referenced by font::floating_label::create_texture(), gui2::text_shape::draw(), and display::draw_report().

◆ render_surface()

surface font::pango_text::render_surface ( const SDL_Rect &  viewport)
private

Returns the rendered text.

The viewport rect is interpreted at the scale of render-space, not drawing-space. This function has only been made private to preserve the drawing-space encapsulation.

Parameters
viewportOnly this area needs to be drawn - the returned surface's origin will correspond to viewport.x and viewport.y, the width and height will be at least viewport.w and viewport.h (although they may be larger).

Definition at line 125 of file text.cpp.

References create_surface(), recalculate(), and update_pixel_scale().

Referenced by render_texture().

◆ render_texture()

texture font::pango_text::render_texture ( const SDL_Rect &  viewport)
private

Wrapper around render_surface which sets texture::w() and texture::h() in the same way that render_and_get_texture does.

The viewport rect is interpreted at the scale of render-space, not drawing-space. This function has only been made private to preserve the drawing-space encapsulation.

Definition at line 113 of file text.cpp.

References render_surface(), and with_draw_scale().

◆ set_add_outline()

pango_text & font::pango_text::set_add_outline ( bool  do_add)

Definition at line 677 of file text.cpp.

References add_outline_.

Referenced by font::floating_label::create_texture(), and gui2::text_shape::draw().

◆ set_alignment()

pango_text & font::pango_text::set_alignment ( const PangoAlignment  alignment)

◆ set_characters_per_line()

pango_text & font::pango_text::set_characters_per_line ( const unsigned  characters_per_line)

◆ set_ellipse_mode()

pango_text & font::pango_text::set_ellipse_mode ( const PangoEllipsizeMode  ellipse_mode)

◆ set_family_class()

pango_text & font::pango_text::set_family_class ( font::family_class  fclass)

◆ set_font_size()

pango_text & font::pango_text::set_font_size ( unsigned  font_size)

◆ set_font_style()

pango_text & font::pango_text::set_font_style ( const FONT_STYLE  font_style)

◆ set_foreground_color()

pango_text & font::pango_text::set_foreground_color ( const color_t color)

◆ set_link_aware()

pango_text & font::pango_text::set_link_aware ( bool  b)

◆ set_link_color()

pango_text & font::pango_text::set_link_color ( const color_t color)

Definition at line 667 of file text.cpp.

References calculation_dirty_, and link_color_.

Referenced by gui2::text_shape::draw(), and gui2::styled_widget::get_best_text_size().

◆ set_markup()

bool font::pango_text::set_markup ( std::string_view  text,
PangoLayout &  layout 
)
private

Sets the markup'ed text.

It tries to set the text as markup. If the markup is invalid it will try a bit harder to recover from the errors and still set the markup.

Parameters
textThe text to set as markup.
layout
Returns
Whether the markup was set or an unrecoverable error occurred and the text is set as plain text with an error message.

Definition at line 982 of file text.cpp.

References format_links(), global_attribute_list_, draw_manager::layout(), link_aware_, text(), and validate_markup().

Referenced by set_text().

◆ set_maximum_height()

pango_text & font::pango_text::set_maximum_height ( int  height,
bool  multiline 
)

◆ set_maximum_length()

pango_text & font::pango_text::set_maximum_length ( const std::size_t  maximum_length)

Definition at line 645 of file text.cpp.

References length_, maximum_length_, set_text(), text_, and utf8::truncate().

Referenced by gui2::text_box_base::set_maximum_length().

◆ set_maximum_width()

pango_text & font::pango_text::set_maximum_width ( int  width)

◆ set_text()

bool font::pango_text::set_text ( const std::string &  text,
const bool  markedup 
)

Sets the text to render.

Parameters
textThe text to render.
markedupShould the text be rendered with pango markup. If the markup is invalid it's rendered as text without markup.
Returns
The status, if rendered as markup and the markup contains errors, false is returned else true.

Definition at line 484 of file text.cpp.

References calculation_dirty_, context_, ERR_GUI_L, global_attribute_list_, layout_, length_, markedup_text_, set_markup(), text(), and text_.

Referenced by font::floating_label::create_texture(), gui2::text_shape::draw(), display::draw_report(), gui2::styled_widget::get_best_text_size(), gui2::text_box_base::handle_editing(), insert_text(), gui2::styled_widget::set_label(), set_maximum_length(), and gui2::text_box_base::set_value().

◆ text()

const std::string& font::pango_text::text ( ) const
inline

◆ to_draw_scale() [1/2]

point font::pango_text::to_draw_scale ( const point p) const
private

Scale the given render-space point to draw-space, rounding up.

Definition at line 144 of file text.cpp.

References p, and to_draw_scale().

◆ to_draw_scale() [2/2]

int font::pango_text::to_draw_scale ( int  s) const
private

Scale the given render-space size to draw-space, rounding up.

Definition at line 139 of file text.cpp.

References i, and pixel_scale_.

Referenced by get_cursor_pos_from_index(), get_size(), to_draw_scale(), and with_draw_scale().

◆ update_pixel_scale()

void font::pango_text::update_pixel_scale ( )
private

Update pixel scale, if necessary.

Definition at line 707 of file text.cpp.

References calculation_dirty_, font_size_, video::get_pixel_scale(), maximum_height_, maximum_width_, and pixel_scale_.

Referenced by get_size(), render_and_get_texture(), and render_surface().

◆ validate_markup()

bool font::pango_text::validate_markup ( std::string_view  text,
char **  raw_text,
std::string &  semi_escaped 
) const
private

Definition at line 1053 of file text.cpp.

References font::semi_escape_text(), text(), and WRN_GUI_L.

Referenced by set_markup().

◆ with_draw_scale()

texture font::pango_text::with_draw_scale ( const texture t) const
private

Adjust a texture's draw-width and height according to pixel scale.

As fonts are rendered at output-scale, we need to do this just before returning the rendered texture. These attributes are stored as part of the returned texture object.

Definition at line 132 of file text.cpp.

References texture::set_draw_size(), t, and to_draw_scale().

Referenced by render_and_get_texture(), and render_texture().

◆ xy_to_index()

int font::pango_text::xy_to_index ( const point position) const

Definition at line 320 of file text.cpp.

References utf8::index(), layout_, and recalculate().

Referenced by gui2::rich_label::get_offset_from_xy().

Member Data Documentation

◆ add_outline_

bool font::pango_text::add_outline_
private

Whether to add an outline effect.

Definition at line 428 of file text.hpp.

Referenced by render(), and set_add_outline().

◆ alignment_

PangoAlignment font::pango_text::alignment_
private

The alignment of the text.

Definition at line 470 of file text.hpp.

Referenced by pango_text(), and set_alignment().

◆ attribute_end_offset_

unsigned font::pango_text::attribute_end_offset_
private

◆ attribute_start_offset_

unsigned font::pango_text::attribute_start_offset_
private

◆ calculation_dirty_

bool font::pango_text::calculation_dirty_
mutableprivate

The text has two dirty states:

  • The setting of the state and the size calculations.
  • The rendering of the surface. The dirty state of the calculations.

Definition at line 482 of file text.hpp.

Referenced by recalculate(), set_characters_per_line(), set_ellipse_mode(), set_family_class(), set_font_size(), set_font_style(), set_link_aware(), set_link_color(), set_maximum_height(), set_maximum_width(), set_text(), and update_pixel_scale().

◆ characters_per_line_

unsigned font::pango_text::characters_per_line_
private

The number of characters per line.

This can be used as an alternative of maximum_width_. The user can select a number of characters on a line for wrapping. When the value is non-zero it determines the maximum width based on the average character width.

If both maximum_width_ and characters_per_line_ are set the minimum of the two will be the maximum.

Note
Long lines are often harder to read, setting this value can automatically wrap on a number of characters regardless of the font size. Often 66 characters is considered the optimal value for a one column text.

Definition at line 456 of file text.hpp.

Referenced by calculate_size(), and set_characters_per_line().

◆ context_

std::unique_ptr<PangoContext, std::function<void(void*)> > font::pango_text::context_
private

Definition at line 393 of file text.hpp.

Referenced by calculate_size(), get_max_glyph_height(), pango_text(), and set_text().

◆ ellipse_mode_

PangoEllipsizeMode font::pango_text::ellipse_mode_
private

The way too long text is shown depends on this mode.

Definition at line 467 of file text.hpp.

Referenced by pango_text(), and set_ellipse_mode().

◆ font_class_

font::family_class font::pango_text::font_class_
private

The font family class used.

Definition at line 416 of file text.hpp.

Referenced by calculate_size(), get_max_glyph_height(), and set_family_class().

◆ font_size_

unsigned font::pango_text::font_size_
private

The font size to draw.

Definition at line 419 of file text.hpp.

Referenced by calculate_size(), get_max_glyph_height(), set_font_size(), and update_pixel_scale().

◆ font_style_

FONT_STYLE font::pango_text::font_style_
private

The style of the font, this is an orred mask of the font flags.

Definition at line 422 of file text.hpp.

Referenced by calculate_size(), get_max_glyph_height(), and set_font_style().

◆ foreground_color_

color_t font::pango_text::foreground_color_
private

The foreground color.

Definition at line 425 of file text.hpp.

Referenced by render(), and set_foreground_color().

◆ global_attribute_list_

PangoAttrList* font::pango_text::global_attribute_list_
private

Global pango attribute list.

All attributes in this list will be applied one by one to the text

Definition at line 495 of file text.hpp.

Referenced by add_attribute_bg_color(), add_attribute_fg_color(), add_attribute_font_family(), add_attribute_size(), add_attribute_style(), add_attribute_underline(), add_attribute_weight(), clear_attribute_list(), pango_text(), set_markup(), and set_text().

◆ highlight_color_

color_t font::pango_text::highlight_color_
private

Definition at line 489 of file text.hpp.

Referenced by add_attribute_bg_color().

◆ layout_

std::unique_ptr<PangoLayout, std::function<void(void*)> > font::pango_text::layout_
private

◆ length_

std::size_t font::pango_text::length_
mutableprivate

Length of the text.

Definition at line 485 of file text.hpp.

Referenced by get_length(), insert_text(), set_maximum_length(), and set_text().

◆ link_aware_

bool font::pango_text::link_aware_
private

Are hyperlinks in the text marked-up, and will get_link return them.

Definition at line 404 of file text.hpp.

Referenced by get_link(), link_aware(), set_link_aware(), and set_markup().

◆ link_color_

color_t font::pango_text::link_color_
private

The color to render links in.

Links are formatted using pango <span> as follows:

<span underline="single" color=" + link_color_ + ">

Definition at line 413 of file text.hpp.

Referenced by format_links(), and set_link_color().

◆ markedup_text_

bool font::pango_text::markedup_text_
private

Does the text contain pango markup? If different render routines must be used.

Definition at line 401 of file text.hpp.

Referenced by calculate_size(), and set_text().

◆ maximum_height_

int font::pango_text::maximum_height_
private

The maximum height of the text.

Values less or equal to 0 mean no maximum and are internally stored as -1, since that's the value pango uses for it.

Definition at line 464 of file text.hpp.

Referenced by calculate_size(), set_maximum_height(), and update_pixel_scale().

◆ maximum_length_

std::size_t font::pango_text::maximum_length_
private

The maximum length of the text.

Definition at line 473 of file text.hpp.

Referenced by get_maximum_length(), insert_text(), and set_maximum_length().

◆ maximum_width_

int font::pango_text::maximum_width_
private

The maximum width of the text.

Values less or equal to 0 mean no maximum and are internally stored as -1, since that's the value pango uses for it.

See characters_per_line_.

Definition at line 438 of file text.hpp.

Referenced by calculate_size(), set_maximum_width(), and update_pixel_scale().

◆ pixel_scale_

int font::pango_text::pixel_scale_
private

The pixel scale, used to render high-DPI text.

Definition at line 498 of file text.hpp.

Referenced by add_attribute_size(), get_max_glyph_height(), set_font_size(), set_maximum_height(), set_maximum_width(), to_draw_scale(), and update_pixel_scale().

◆ rect_

PangoRectangle font::pango_text::rect_
mutableprivate

Definition at line 395 of file text.hpp.

Referenced by create_surface(), get_size(), and recalculate().

◆ surface_buffer_

std::vector<uint8_t> font::pango_text::surface_buffer_
mutableprivate

Buffer to store the image on.

We use a cairo surface to draw on this buffer and then use the buffer as data source for the SDL_Surface. This means the buffer needs to be stored in the object, since SDL_Surface doesn't own its buffer.

Definition at line 543 of file text.hpp.

Referenced by create_surface(), and render().

◆ text_

std::string font::pango_text::text_
private

The text to draw (stored as UTF-8).

Definition at line 398 of file text.hpp.

Referenced by calculate_size(), insert_text(), set_maximum_length(), set_text(), and text().


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