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_text & | operator= (const pango_text &)=delete |
surface & | render (const SDL_Rect &viewport) |
Returns the rendered text. More... | |
surface & | render () |
Equivalent to render(viewport), where the viewport's top-left is at (0,0) and the area is large enough to contain the full text. More... | |
int | get_width () const |
Returns the width needed for the text. More... | |
int | get_height () const |
Returns the height needed for the text. More... | |
point | get_size () const |
Returns the pixel size needed for the text. 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) |
Inserts UTF-8 text. More... | |
int | get_max_glyph_height () const |
Returns the maximum glyph height of a font, in pixels. More... | |
point | get_cursor_position (const unsigned column, const unsigned line=0) const |
Gets the location for the cursor. More... | |
std::size_t | get_maximum_length () const |
Get maximum length. More... | |
std::string | get_token (const point &position, const char *delimiters=" \\) 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... | |
std::vector< std::string > | get_lines () const |
Retrieves a list of strings with contents for each rendered line. 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_text & | set_family_class (font::family_class fclass) |
pango_text & | set_font_size (const unsigned font_size) |
pango_text & | set_font_style (const FONT_STYLE font_style) |
pango_text & | set_foreground_color (const color_t &color) |
pango_text & | set_maximum_width (int width) |
pango_text & | set_characters_per_line (const unsigned characters_per_line) |
pango_text & | set_maximum_height (int height, bool multiline) |
pango_text & | set_ellipse_mode (const PangoEllipsizeMode ellipse_mode) |
pango_text & | set_alignment (const PangoAlignment alignment) |
pango_text & | set_maximum_length (const std::size_t maximum_length) |
bool | link_aware () const |
pango_text & | set_link_aware (bool b) |
pango_text & | set_link_color (const color_t &color) |
pango_text & | set_add_outline (bool do_add) |
Private Member Functions | |
void | recalculate () const |
Recalculates the text layout. More... | |
PangoRectangle | calculate_size (PangoLayout &layout) const |
Calculates surface size. More... | |
void | rerender (const SDL_Rect &viewport) |
Renders the text. More... | |
void | render (PangoLayout &layout, const SDL_Rect &viewport, const unsigned stride) |
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... | |
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_ |
surface | surface_ |
The SDL surface to render upon used as a cache. More... | |
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... | |
bool | surface_dirty_ |
The dirty state of the surface. More... | |
SDL_Rect | rendered_viewport_ |
The area that's cached in surface_, which is the area that was rendered when surface_dirty_ was last set to false. More... | |
std::vector< uint8_t > | surface_buffer_ |
Buffer to store the image on. More... | |
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.
font::pango_text::pango_text | ( | ) |
Definition at line 44 of file text.cpp.
References alignment_, context_, ellipse_mode_, and layout_.
|
delete |
|
private |
Calculates surface size.
Definition at line 524 of file text.cpp.
References characters_per_line_, context_, DBG_GUI_L, gui2::debug_truncate(), f, font_class_, font_size_, font_style_, font::get_font_families(), markedup_text_, maximum_height_, maximum_width_, utf8::size(), STYLE_UNDERLINE, text_, and w.
Referenced by recalculate().
|
staticprivate |
|
private |
Replaces all instances of URLs in a given string with formatted links and returns the result.
Definition at line 805 of file text.cpp.
References font::format_as_link(), link_color_, and font::looks_like_url().
Referenced by set_markup().
Gets the column of line of the character at the position.
position | The pixel position in the text area. |
Definition at line 249 of file text.cpp.
References get_cursor_position(), i, utf8::index(), layout_, recalculate(), point::x, and point::y.
point font::pango_text::get_cursor_position | ( | const unsigned | column, |
const unsigned | line = 0 |
||
) | const |
Gets the location for the cursor.
column | The column offset of the cursor. |
line | The line offset of the cursor. |
Definition at line 152 of file text.cpp.
References i, layout_, and recalculate().
Referenced by get_column_line().
int font::pango_text::get_height | ( | ) | const |
Returns the height needed for the text.
Definition at line 113 of file text.cpp.
References get_size(), and point::y.
Referenced by gui2::text_shape::draw().
|
inline |
Gets the length of the text in bytes.
The text set is UTF-8 so the length of the string might not be the length of the text.
Definition at line 218 of file text.hpp.
References length_, and set_text().
Referenced by gui2::text_box_base::handle_editing(), gui2::text_box_base::handle_key_delete(), gui2::text_box_base::handle_key_right_arrow(), gui2::text_box_base::set_cursor(), gui2::text_box_base::set_maximum_length(), gui2::text_box_base::set_selection(), and gui2::text_box_base::set_value().
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.
Definition at line 888 of file text.cpp.
References i, layout_, and recalculate().
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.
position | The pixel position in the text area. |
Definition at line 234 of file text.cpp.
References get_token(), link_aware_, and font::looks_like_url().
Referenced by gui2::styled_widget::get_label_link().
int font::pango_text::get_max_glyph_height | ( | ) | const |
Returns the maximum glyph height of a font, in pixels.
Definition at line 492 of file text.cpp.
References context_, f, font_class_, font_size_, font_style_, and font::get_font_families().
Referenced by font::get_max_height().
std::size_t font::pango_text::get_maximum_length | ( | ) | const |
Get maximum length.
Definition at line 197 of file text.cpp.
References maximum_length_.
point font::pango_text::get_size | ( | ) | const |
Returns the pixel size needed for the text.
Definition at line 118 of file text.cpp.
References recalculate(), and rect_.
Referenced by gui2::styled_widget::get_best_text_size(), get_height(), and get_width().
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.
position | The pixel position in the text area. |
delimiters |
Definition at line 202 of file text.cpp.
References d, utf8::index(), layout_, recalculate(), point::x, and point::y.
Referenced by gui2::styled_widget::get_label_token(), and get_link().
int font::pango_text::get_width | ( | ) | const |
Returns the width needed for the text.
Definition at line 108 of file text.cpp.
References get_size(), and point::x.
Referenced by gui2::text_shape::draw().
unsigned font::pango_text::insert_text | ( | const unsigned | offset, |
const std::string & | text | ||
) |
Inserts UTF-8 text.
offset | The position to insert the text. |
text | The UTF-8 text to insert. |
Definition at line 132 of file text.cpp.
References utf8::index(), utf8::insert(), length_, maximum_length_, set_text(), utf8::size(), and text_.
Referenced by gui2::text_box_base::handle_editing(), gui2::text_box_base::insert_char(), and gui2::text_box_base::paste_selection().
bool font::pango_text::is_truncated | ( | ) | const |
Has the text been truncated? This happens if it exceeds max width or height.
Definition at line 125 of file text.cpp.
References layout_, and recalculate().
Referenced by gui2::styled_widget::get_best_text_size(), and gui2::styled_widget::place().
|
inline |
Definition at line 258 of file text.hpp.
References b, link_aware_, set_add_outline(), set_link_aware(), and set_link_color().
|
delete |
|
private |
Recalculates the text layout.
Definition at line 512 of file text.cpp.
References calculate_size(), calculation_dirty_, layout_, rect_, and surface_dirty_.
Referenced by get_column_line(), get_cursor_position(), get_lines(), get_size(), get_token(), is_truncated(), render(), and rerender().
surface & font::pango_text::render | ( | const SDL_Rect & | viewport | ) |
Returns the rendered text.
viewport | Only 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 94 of file text.cpp.
References rerender(), and surface_.
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), and display::refresh_report().
surface & font::pango_text::render | ( | ) |
Equivalent to render(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. 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 100 of file text.cpp.
References recalculate(), rect_, rerender(), and surface_.
Referenced by rerender().
|
private |
Definition at line 674 of file text.cpp.
References color_t::a, add_outline_, color_t::b, foreground_color_, color_t::g, color_t::r, and surface_buffer_.
|
private |
Renders the text.
It will do a recalculation first so no need to call both.
Definition at line 725 of file text.cpp.
References font::from_cairo_format(), layout_, recalculate(), render(), rendered_viewport_, surface_, surface_buffer_, and surface_dirty_.
Referenced by render().
pango_text & font::pango_text::set_add_outline | ( | bool | do_add | ) |
Definition at line 481 of file text.cpp.
References add_outline_, and surface_dirty_.
Referenced by link_aware().
pango_text & font::pango_text::set_alignment | ( | const PangoAlignment | alignment | ) |
Definition at line 436 of file text.cpp.
References alignment_, layout_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), gui2::styled_widget::get_best_text_size(), display::refresh_report(), and text().
pango_text & font::pango_text::set_characters_per_line | ( | const unsigned | characters_per_line | ) |
Definition at line 379 of file text.cpp.
References calculation_dirty_, characters_per_line_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), gui2::styled_widget::get_best_text_size(), display::refresh_report(), and text().
pango_text & font::pango_text::set_ellipse_mode | ( | const PangoEllipsizeMode | ellipse_mode | ) |
Definition at line 415 of file text.cpp.
References calculation_dirty_, ellipse_mode_, layout_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), gui2::styled_widget::get_best_text_size(), display::refresh_report(), and text().
pango_text & font::pango_text::set_family_class | ( | font::family_class | fclass | ) |
Definition at line 320 of file text.cpp.
References calculation_dirty_, font_class_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), gui2::styled_widget::get_best_text_size(), font::get_max_height(), display::refresh_report(), text(), and gui2::text_box_base::text_box_base().
pango_text & font::pango_text::set_font_size | ( | const unsigned | font_size | ) |
Definition at line 331 of file text.cpp.
References calculation_dirty_, preferences::font_scaled(), font_size_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), gui2::styled_widget::get_best_text_size(), font::get_max_height(), display::refresh_report(), and text().
pango_text & font::pango_text::set_font_style | ( | const FONT_STYLE | font_style | ) |
Definition at line 343 of file text.cpp.
References calculation_dirty_, font_style_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), gui2::styled_widget::get_best_text_size(), font::get_max_height(), and text().
pango_text & font::pango_text::set_foreground_color | ( | const color_t & | color | ) |
Definition at line 354 of file text.cpp.
References foreground_color_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), display::refresh_report(), and text().
pango_text & font::pango_text::set_link_aware | ( | bool | b | ) |
Definition at line 460 of file text.cpp.
References b, calculation_dirty_, link_aware_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), gui2::styled_widget::get_best_text_size(), link_aware(), and display::refresh_report().
pango_text & font::pango_text::set_link_color | ( | const color_t & | color | ) |
Definition at line 470 of file text.cpp.
References calculation_dirty_, link_color_, and surface_dirty_.
Referenced by gui2::text_shape::draw(), gui2::styled_widget::get_best_text_size(), and link_aware().
|
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.
text | The text to set as markup. |
layout |
Definition at line 776 of file text.cpp.
References _(), ERR_GUI_L, format_links(), link_aware_, set_text(), and validate_markup().
Referenced by set_text().
pango_text & font::pango_text::set_maximum_height | ( | int | height, |
bool | multiline | ||
) |
Definition at line 391 of file text.cpp.
References calculation_dirty_, layout_, maximum_height_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), display::refresh_report(), and text().
pango_text & font::pango_text::set_maximum_length | ( | const std::size_t | maximum_length | ) |
Definition at line 447 of file text.cpp.
References length_, maximum_length_, set_text(), text_, and utf8::truncate().
Referenced by gui2::text_box_base::set_maximum_length(), and text().
pango_text & font::pango_text::set_maximum_width | ( | int | width | ) |
Definition at line 364 of file text.cpp.
References calculation_dirty_, maximum_width_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), gui2::styled_widget::get_best_text_size(), and text().
bool font::pango_text::set_text | ( | const std::string & | text, |
const bool | markedup | ||
) |
Sets the text to render.
text | The text to render. |
markedup | Should the text be rendered with pango markup. If the markup is invalid it's rendered as text without markup. |
Definition at line 283 of file text.cpp.
References calculation_dirty_, context_, ERR_GUI_L, layout_, length_, markedup_text_, set_markup(), surface_dirty_, text(), text_, and unicode_cast().
Referenced by font::floating_label::create_surface(), gui2::text_shape::draw(), gui2::styled_widget::get_best_text_size(), get_length(), gui2::text_box_base::handle_editing(), insert_text(), display::refresh_report(), gui2::styled_widget::set_label(), set_markup(), set_maximum_length(), and gui2::text_box_base::set_value().
|
inline |
Definition at line 236 of file text.hpp.
References set_alignment(), set_characters_per_line(), set_ellipse_mode(), set_family_class(), set_font_size(), set_font_style(), set_foreground_color(), set_maximum_height(), set_maximum_length(), set_maximum_width(), and text_.
Referenced by gui2::text_box_base::copy_selection(), gui2::text_box_base::get_composition_length(), gui2::text_box_base::handle_editing(), set_text(), and gui2::text_box_base::set_value().
|
private |
Definition at line 843 of file text.cpp.
References font::semi_escape_text(), and WRN_GUI_L.
Referenced by set_markup().
|
private |
Whether to add an outline effect.
Definition at line 308 of file text.hpp.
Referenced by render(), and set_add_outline().
|
private |
The alignment of the text.
Definition at line 350 of file text.hpp.
Referenced by pango_text(), and set_alignment().
|
mutableprivate |
The text has two dirty states:
Definition at line 362 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(), and set_text().
|
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.
Definition at line 336 of file text.hpp.
Referenced by calculate_size(), and set_characters_per_line().
|
private |
Definition at line 269 of file text.hpp.
Referenced by calculate_size(), get_max_glyph_height(), pango_text(), and set_text().
|
private |
The way too long text is shown depends on this mode.
Definition at line 347 of file text.hpp.
Referenced by pango_text(), and set_ellipse_mode().
|
private |
The font family class used.
Definition at line 296 of file text.hpp.
Referenced by calculate_size(), get_max_glyph_height(), and set_family_class().
|
private |
The font size to draw.
Definition at line 299 of file text.hpp.
Referenced by calculate_size(), get_max_glyph_height(), and set_font_size().
|
private |
The style of the font, this is an orred mask of the font flags.
Definition at line 302 of file text.hpp.
Referenced by calculate_size(), get_max_glyph_height(), and set_font_style().
|
private |
The foreground color.
Definition at line 305 of file text.hpp.
Referenced by render(), and set_foreground_color().
|
private |
Definition at line 270 of file text.hpp.
Referenced by get_column_line(), get_cursor_position(), get_lines(), get_token(), is_truncated(), pango_text(), recalculate(), rerender(), set_alignment(), set_ellipse_mode(), set_maximum_height(), and set_text().
|
mutableprivate |
Length of the text.
Definition at line 365 of file text.hpp.
Referenced by get_length(), insert_text(), set_maximum_length(), and set_text().
|
private |
Are hyperlinks in the text marked-up, and will get_link return them.
Definition at line 284 of file text.hpp.
Referenced by get_link(), link_aware(), set_link_aware(), and set_markup().
|
private |
The color to render links in.
Links are formatted using pango <span> as follows:
<span underline="single" color=" + link_color_ + ">
Definition at line 293 of file text.hpp.
Referenced by format_links(), and set_link_color().
|
private |
Does the text contain pango markup? If different render routines must be used.
Definition at line 281 of file text.hpp.
Referenced by calculate_size(), and set_text().
|
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 344 of file text.hpp.
Referenced by calculate_size(), and set_maximum_height().
|
private |
The maximum length of the text.
Definition at line 353 of file text.hpp.
Referenced by get_maximum_length(), insert_text(), and set_maximum_length().
|
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 318 of file text.hpp.
Referenced by calculate_size(), and set_maximum_width().
|
mutableprivate |
Definition at line 271 of file text.hpp.
Referenced by get_size(), recalculate(), and render().
|
private |
The area that's cached in surface_, which is the area that was rendered when surface_dirty_ was last set to false.
Definition at line 379 of file text.hpp.
Referenced by rerender().
|
mutableprivate |
The SDL surface to render upon used as a cache.
Definition at line 274 of file text.hpp.
Referenced by render(), and rerender().
|
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 397 of file text.hpp.
Referenced by render(), and rerender().
|
mutableprivate |
The dirty state of the surface.
Definition at line 376 of file text.hpp.
Referenced by recalculate(), rerender(), set_add_outline(), set_alignment(), set_characters_per_line(), set_ellipse_mode(), set_family_class(), set_font_size(), set_font_style(), set_foreground_color(), set_link_aware(), set_link_color(), set_maximum_height(), set_maximum_width(), and set_text().
|
private |
The text to draw (stored as UTF-8).
Definition at line 278 of file text.hpp.
Referenced by calculate_size(), insert_text(), set_maximum_length(), set_text(), and text().