Text class. More...
#include <text.hpp>
Public Member Functions | |
ttext () | |
~ttext () | |
surface | render () const |
Returns the rendered 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... | |
gui2::tpoint | get_size () const |
Returns the size needed for the text. More... | |
bool | is_truncated () const |
Has the text been truncated? More... | |
unsigned | insert_text (const unsigned offset, const std::string &text) |
Inserts UTF-8 text. More... | |
bool | insert_unicode (const unsigned offset, ucs4::char_t unicode) |
Inserts a unicode char. More... | |
unsigned | insert_unicode (const unsigned offset, const ucs4::string &unicode) |
Inserts unicode text. More... | |
gui2::tpoint | get_cursor_position (const unsigned column, const unsigned line=0) const |
Gets the location for the cursor. More... | |
std::string | get_token (const gui2::tpoint &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 gui2::tpoint &position) const |
Checks if position points to a character in a link in the text, returns it if so, empty string otherwise. More... | |
gui2::tpoint | get_column_line (const gui2::tpoint &position) const |
Gets the column of line of the character at the position. More... | |
size_t | get_length () const |
Gets the length of the text in characters. More... | |
bool | set_text (const std::string &text, const bool markedup) |
Sets the text to render. More... | |
const std::string & | text () const |
ttext & | set_family_class (font::family_class fclass) |
ttext & | set_font_size (const unsigned font_size) |
ttext & | set_font_style (const unsigned font_style) |
ttext & | set_foreground_color (const Uint32 color) |
ttext & | set_foreground_color (const SDL_Color color) |
ttext & | set_maximum_width (int width) |
ttext & | set_characters_per_line (const unsigned characters_per_line) |
ttext & | set_maximum_height (int height, bool multiline) |
ttext & | set_ellipse_mode (const PangoEllipsizeMode ellipse_mode) |
ttext & | set_alignment (const PangoAlignment alignment) |
ttext & | set_maximum_length (const size_t maximum_length) |
bool | link_aware () const |
ttext & | set_link_aware (bool b) |
ttext & | set_link_color (const std::string &color) |
Static Public Attributes | |
static const unsigned | STYLE_NORMAL = TTF_STYLE_NORMAL |
The flags have the same values as the ones in SDL_TTF so it's easy to mix them for now. More... | |
static const unsigned | STYLE_BOLD = TTF_STYLE_BOLD |
Bold text. More... | |
static const unsigned | STYLE_ITALIC = TTF_STYLE_ITALIC |
Italicized text. More... | |
static const unsigned | STYLE_UNDERLINE = TTF_STYLE_UNDERLINE |
Underlined text. More... | |
Private Member Functions | |
void | recalculate (const bool force=false) const |
Recalculates the text. More... | |
void | rerender (const bool force=false) const |
Renders the text. More... | |
void | create_surface_buffer (const size_t size) const |
Creates a new buffer. More... | |
bool | set_markup (const std::string &text) |
Sets the markup'ed text. More... | |
bool | set_markup_helper (const std::string &text) |
std::string | handle_token (const std::string &token) const |
Private Attributes | |
PangoContext * | context_ |
PangoLayout * | layout_ |
PangoRectangle | rect_ |
surface | surface_ |
The surface to render upon used as a cache. More... | |
std::string | text_ |
The text to draw (stored as UTF-8). More... | |
bool | markedup_text_ |
Is the text markedup if so the markedup render routines need to be used. More... | |
bool | link_aware_ |
Are hyperlinks in the text marked-up, and will get_link return them. More... | |
std::string | 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... | |
unsigned | font_style_ |
The style of the font, this is an orred mask of the font flags. More... | |
Uint32 | foreground_color_ |
The foreground color. 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... | |
size_t | maximum_length_ |
The maximum length of the text. More... | |
bool | calculation_dirty_ |
The text has two dirty states: More... | |
size_t | length_ |
Length of the text. More... | |
bool | surface_dirty_ |
The dirty state of the surface. More... | |
unsigned char * | surface_buffer_ |
Buffer to store the image on. More... | |
Text class.
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::ttext::ttext | ( | ) |
Definition at line 91 of file text.cpp.
References alignment_, context_, ellipse_mode_, and layout_.
font::ttext::~ttext | ( | ) |
Definition at line 152 of file text.cpp.
References surface::assign(), context_, layout_, surface_, and surface_buffer_.
|
private |
Creates a new buffer.
If needed frees the other surface and then creates a new buffer and initializes the entire buffer with values 0.
NOTE eventhough we're clearly modifying function we don't change the state of the object. The const is needed so other functions can also be marked const (those also don't change the state of the object.
size | The required size of the buffer. |
Definition at line 804 of file text.cpp.
References surface::assign(), surface_, and surface_buffer_.
Referenced by rerender().
gui2::tpoint font::ttext::get_column_line | ( | const gui2::tpoint & | position | ) | const |
Gets the column of line of the character at the position.
position | The pixel position in the text area. |
Definition at line 326 of file text.cpp.
References get_cursor_position(), i, layout_, pos, recalculate(), gui2::tpoint::x, and gui2::tpoint::y.
Referenced by gui2::ttext_::get_column_line().
gui2::tpoint font::ttext::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 235 of file text.cpp.
References i, itor, layout_, and recalculate().
Referenced by get_column_line(), and gui2::ttext_::get_cursor_position().
int font::ttext::get_height | ( | ) | const |
Returns the height needed for the text.
Definition at line 185 of file text.cpp.
References get_size(), and gui2::tpoint::y.
|
inline |
Gets the length of the text in characters.
The text set is UTF-8 so the length of the string might not be the length of the text.
Definition at line 202 of file text.hpp.
References length_.
Referenced by gui2::ttext_::get_length(), gui2::ttext_::goto_end_of_data(), gui2::ttext_::handle_key_delete(), gui2::ttext_::handle_key_right_arrow(), gui2::ttext_::set_cursor(), gui2::ttext_::set_maximum_length(), and gui2::ttext_::set_value().
std::string font::ttext::get_link | ( | const gui2::tpoint & | 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 311 of file text.cpp.
References get_token(), link_aware_, and font::looks_like_url().
Referenced by gui2::tcontrol::get_label_link().
gui2::tpoint font::ttext::get_size | ( | ) | const |
Returns the size needed for the text.
Definition at line 190 of file text.cpp.
References recalculate(), and rect_.
Referenced by gui2::tcontrol::get_best_text_size(), get_height(), and get_width().
std::string font::ttext::get_token | ( | const gui2::tpoint & | 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. |
Definition at line 279 of file text.cpp.
References d, layout_, recalculate(), gui2::tpoint::x, and gui2::tpoint::y.
Referenced by gui2::tcontrol::get_label_token(), and get_link().
int font::ttext::get_width | ( | ) | const |
Returns the width needed for the text.
Definition at line 180 of file text.cpp.
References get_size(), and gui2::tpoint::x.
|
private |
Definition at line 847 of file text.cpp.
References link_color_, and font::looks_like_url().
Referenced by set_markup().
unsigned font::ttext::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 204 of file text.cpp.
References utf8::index(), utf8::insert(), length_, maximum_length_, set_text(), utf8::size(), and text_.
Referenced by gui2::ttext_::insert_char(), insert_unicode(), and gui2::ttext_::paste_selection().
bool font::ttext::insert_unicode | ( | const unsigned | offset, |
ucs4::char_t | unicode | ||
) |
unsigned font::ttext::insert_unicode | ( | const unsigned | offset, |
const ucs4::string & | unicode | ||
) |
Inserts unicode text.
offset | The position to insert the text. |
unicode | Vector with characters to insert. |
Definition at line 229 of file text.cpp.
References utf8::insert(), insert_text(), and unicode_cast().
bool font::ttext::is_truncated | ( | ) | const |
Has the text been truncated?
Definition at line 197 of file text.cpp.
References layout_, and recalculate().
Referenced by gui2::tcontrol::get_best_text_size(), and gui2::tcontrol::place().
|
inline |
Definition at line 244 of file text.hpp.
References link_aware_.
|
private |
Recalculates the text.
When the text is recalculated the surface is dirtied.
force | Recalculate even if not dirty? |
Definition at line 609 of file text.cpp.
References calculation_dirty_, characters_per_line_, context_, DBG_GUI_L, gui2::debug_truncate(), font_class_, font_size_, font_style_, font::get_font_families(), layout_, markedup_text_, maximum_height_, maximum_width_, rect_, surface_dirty_, and text_.
Referenced by get_column_line(), get_cursor_position(), get_size(), get_token(), is_truncated(), and rerender().
surface font::ttext::render | ( | ) | const |
Returns the rendered text.
Before rendering it tests whether a redraw is needed and if so it first redraws the surface before returning it.
Definition at line 166 of file text.cpp.
References rerender(), and surface_.
Referenced by font::floating_label::create_surface(), draw_text(), display::refresh_report(), storyscreen::part_ui::render_story_box(), and storyscreen::part_ui::render_title_box().
|
private |
Renders the text.
It will do a recalculation first so no need to call both.
force | Render even if not dirty? This parameter is also send to recalculate(). |
Definition at line 745 of file text.cpp.
References surface::assign(), create_surface_buffer(), font::decode_pixel(), foreground_color_, layout_, maximum_height_, maximum_width_, recalculate(), rect_, surface_, surface_buffer_, and surface_dirty_.
Referenced by render().
ttext & font::ttext::set_alignment | ( | const PangoAlignment | alignment | ) |
Definition at line 524 of file text.cpp.
References alignment_, layout_, and surface_dirty_.
Referenced by gui2::tcontrol::get_best_text_size().
ttext & font::ttext::set_characters_per_line | ( | const unsigned | characters_per_line | ) |
Definition at line 479 of file text.cpp.
References calculation_dirty_, characters_per_line_, and surface_dirty_.
Referenced by gui2::tcontrol::get_best_text_size().
ttext & font::ttext::set_ellipse_mode | ( | const PangoEllipsizeMode | ellipse_mode | ) |
Definition at line 510 of file text.cpp.
References calculation_dirty_, context_, ellipse_mode_, layout_, and surface_dirty_.
Referenced by gui2::tcontrol::get_best_text_size(), and gui2::ttext_::set_ellipse_mode().
ttext & font::ttext::set_family_class | ( | font::family_class | fclass | ) |
Definition at line 395 of file text.cpp.
References calculation_dirty_, font_class_, and surface_dirty_.
Referenced by gui2::tcontrol::get_best_text_size().
ttext & font::ttext::set_font_size | ( | const unsigned | font_size | ) |
Definition at line 406 of file text.cpp.
References calculation_dirty_, preferences::font_scaled(), font_size_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::tcontrol::get_best_text_size(), display::refresh_report(), storyscreen::part_ui::render_story_box(), storyscreen::part_ui::render_title_box(), and gui2::ttext_::set_font_size().
ttext & font::ttext::set_font_style | ( | const unsigned | font_style | ) |
Definition at line 418 of file text.cpp.
References calculation_dirty_, font_style_, and surface_dirty_.
Referenced by gui2::tcontrol::get_best_text_size(), storyscreen::part_ui::render_story_box(), storyscreen::part_ui::render_title_box(), and gui2::ttext_::set_font_style().
ttext & font::ttext::set_foreground_color | ( | const Uint32 | color | ) |
Definition at line 429 of file text.cpp.
References foreground_color_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), display::refresh_report(), storyscreen::part_ui::render_story_box(), storyscreen::part_ui::render_title_box(), and set_foreground_color().
ttext & font::ttext::set_foreground_color | ( | const SDL_Color | color | ) |
Definition at line 439 of file text.cpp.
References set_foreground_color().
ttext & font::ttext::set_link_aware | ( | bool | b | ) |
Definition at line 546 of file text.cpp.
References calculation_dirty_, link_aware_, and surface_dirty_.
Referenced by gui2::tcontrol::get_best_text_size().
ttext & font::ttext::set_link_color | ( | const std::string & | color | ) |
Definition at line 556 of file text.cpp.
References calculation_dirty_, link_color_, and surface_dirty_.
Referenced by gui2::tcontrol::get_best_text_size().
|
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. |
Definition at line 816 of file text.cpp.
References handle_token(), link_aware_, and set_markup_helper().
Referenced by set_text().
|
private |
Definition at line 856 of file text.cpp.
References _(), c, ERR_GUI_L, layout_, and set_text().
Referenced by set_markup().
Definition at line 491 of file text.cpp.
References calculation_dirty_, context_, layout_, maximum_height_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), display::refresh_report(), storyscreen::part_ui::render_story_box(), storyscreen::part_ui::render_title_box(), and gui2::ttext_::set_maximum_height().
ttext & font::ttext::set_maximum_length | ( | const size_t | maximum_length | ) |
Definition at line 533 of file text.cpp.
References length_, maximum_length_, set_text(), text_, and utf8::truncate().
Referenced by gui2::ttext_::set_maximum_length().
Definition at line 446 of file text.cpp.
References calculation_dirty_, context_, layout_, maximum_width_, and surface_dirty_.
Referenced by font::floating_label::create_surface(), gui2::tcontrol::get_best_text_size(), display::refresh_report(), storyscreen::part_ui::render_story_box(), storyscreen::part_ui::render_title_box(), and gui2::ttext_::set_maximum_width().
bool font::ttext::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 360 of file text.cpp.
References calculation_dirty_, ERR_GUI_L, layout_, length_, markedup_text_, set_markup(), surface_dirty_, text(), text_, and unicode_cast().
Referenced by font::floating_label::create_surface(), draw_text(), gui2::tcontrol::get_best_text_size(), insert_text(), display::refresh_report(), storyscreen::part_ui::render_story_box(), storyscreen::part_ui::render_title_box(), gui::button::set_label(), set_markup_helper(), set_maximum_length(), and gui2::ttext_::set_value().
|
inline |
Definition at line 220 of file text.hpp.
References text_.
Referenced by gui2::ttext_::copy_selection(), gui2::ttext_::get_value(), set_text(), gui2::ttext_::set_value(), and gui2::ttext_::text().
|
private |
The alignment of the text.
Definition at line 327 of file text.hpp.
Referenced by set_alignment(), and ttext().
|
mutableprivate |
The text has two dirty states:
Definition at line 339 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 313 of file text.hpp.
Referenced by recalculate(), and set_characters_per_line().
|
private |
Definition at line 252 of file text.hpp.
Referenced by recalculate(), set_ellipse_mode(), set_maximum_height(), set_maximum_width(), ttext(), and ~ttext().
|
private |
The way too long text is shown depends on this mode.
Definition at line 324 of file text.hpp.
Referenced by set_ellipse_mode(), and ttext().
|
private |
The font family class used.
Definition at line 276 of file text.hpp.
Referenced by recalculate(), and set_family_class().
|
private |
The font size to draw.
Definition at line 279 of file text.hpp.
Referenced by recalculate(), and set_font_size().
|
private |
The style of the font, this is an orred mask of the font flags.
Definition at line 282 of file text.hpp.
Referenced by recalculate(), and set_font_style().
|
private |
The foreground color.
Definition at line 285 of file text.hpp.
Referenced by rerender(), and set_foreground_color().
|
private |
Definition at line 253 of file text.hpp.
Referenced by get_column_line(), get_cursor_position(), get_token(), is_truncated(), recalculate(), rerender(), set_alignment(), set_ellipse_mode(), set_markup_helper(), set_maximum_height(), set_maximum_width(), set_text(), ttext(), and ~ttext().
|
mutableprivate |
Length of the text.
Definition at line 342 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 270 of file text.hpp.
Referenced by get_link(), link_aware(), set_link_aware(), and set_markup().
|
private |
The color to render links in.
Definition at line 273 of file text.hpp.
Referenced by handle_token(), and set_link_color().
|
private |
Is the text markedup if so the markedup render routines need to be used.
Definition at line 267 of file text.hpp.
Referenced by recalculate(), 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 321 of file text.hpp.
Referenced by recalculate(), rerender(), and set_maximum_height().
|
private |
The maximum length of the text.
Definition at line 330 of file text.hpp.
Referenced by 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 295 of file text.hpp.
Referenced by recalculate(), rerender(), and set_maximum_width().
|
mutableprivate |
Definition at line 254 of file text.hpp.
Referenced by get_size(), recalculate(), and rerender().
|
static |
|
static |
|
static |
The flags have the same values as the ones in SDL_TTF so it's easy to mix them for now.
To avoid including SDL_TTF in the header they're only declared here. Once SDL_TTF is removed they can be moved in the header.Normal text.
Definition at line 144 of file text.hpp.
Referenced by storyscreen::part_ui::render_story_box(), and storyscreen::part_ui::render_title_box().
|
static |
|
mutableprivate |
The surface to render upon used as a cache.
Definition at line 257 of file text.hpp.
Referenced by create_surface_buffer(), render(), rerender(), and ~ttext().
|
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.
Definition at line 373 of file text.hpp.
Referenced by create_surface_buffer(), rerender(), and ~ttext().
|
mutableprivate |
The dirty state of the surface.
Definition at line 354 of file text.hpp.
Referenced by recalculate(), rerender(), 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 264 of file text.hpp.
Referenced by insert_text(), recalculate(), set_maximum_length(), set_text(), and text().