The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Public Member Functions | Static Public Attributes | Private Member Functions | Private Attributes | List of all members
font::ttext Class Reference

Text class. More...

#include <text.hpp>

Inheritance diagram for font::ttext:
Inheritance graph

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::stringtext () const
 
ttextset_family_class (font::family_class fclass)
 
ttextset_font_size (const unsigned font_size)
 
ttextset_font_style (const unsigned font_style)
 
ttextset_foreground_color (const Uint32 color)
 
ttextset_foreground_color (const SDL_Color color)
 
ttextset_maximum_width (int width)
 
ttextset_characters_per_line (const unsigned characters_per_line)
 
ttextset_maximum_height (int height, bool multiline)
 
ttextset_ellipse_mode (const PangoEllipsizeMode ellipse_mode)
 
ttextset_alignment (const PangoAlignment alignment)
 
ttextset_maximum_length (const size_t maximum_length)
 
bool link_aware () const
 
ttextset_link_aware (bool b)
 
ttextset_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...
 

Detailed Description

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.

Definition at line 66 of file text.hpp.

Constructor & Destructor Documentation

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_.

Member Function Documentation

void font::ttext::create_surface_buffer ( const size_t  size) const
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.

Parameters
sizeThe 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.

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 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.

Parameters
columnThe column offset of the cursor.
lineThe line offset 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 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.

size_t font::ttext::get_length ( ) const
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.

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

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.

Parameters
positionThe pixel position in the text area.
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 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.

std::string font::ttext::handle_token ( const std::string token) const
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.

Parameters
offsetThe position to insert the text.
textThe UTF-8 text to insert.
Returns
The number of characters inserted.

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 
)

Inserts a unicode char.

Parameters
offsetThe position to insert the char.
unicodeThe character to insert.
Returns
True upon success, false otherwise.

Definition at line 224 of file text.cpp.

unsigned font::ttext::insert_unicode ( const unsigned  offset,
const ucs4::string unicode 
)

Inserts unicode text.

Parameters
offsetThe position to insert the text.
unicodeVector with characters to insert.
Returns
The number of characters inserted.

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().

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

Definition at line 244 of file text.hpp.

References link_aware_.

void font::ttext::recalculate ( const bool  force = false) const
private

Recalculates the text.

When the text is recalculated the surface is dirtied.

Parameters
forceRecalculate 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().

void font::ttext::rerender ( const bool  force = false) const
private

Renders the text.

It will do a recalculation first so no need to call both.

Parameters
forceRender 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)
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)
ttext & font::ttext::set_font_style ( const unsigned  font_style)
ttext & font::ttext::set_foreground_color ( const Uint32  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().

bool font::ttext::set_markup ( const std::string text)
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.
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 816 of file text.cpp.

References handle_token(), link_aware_, and set_markup_helper().

Referenced by set_text().

bool font::ttext::set_markup_helper ( const std::string text)
private

Definition at line 856 of file text.cpp.

References _(), c, ERR_GUI_L, layout_, and set_text().

Referenced by set_markup().

ttext & font::ttext::set_maximum_height ( int  height,
bool  multiline 
)
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().

ttext & font::ttext::set_maximum_width ( int  width)
bool font::ttext::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 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().

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

Member Data Documentation

PangoAlignment font::ttext::alignment_
private

The alignment of the text.

Definition at line 327 of file text.hpp.

Referenced by set_alignment(), and ttext().

bool font::ttext::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 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().

unsigned font::ttext::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 313 of file text.hpp.

Referenced by recalculate(), and set_characters_per_line().

PangoContext* font::ttext::context_
private
PangoEllipsizeMode font::ttext::ellipse_mode_
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().

font::family_class font::ttext::font_class_
private

The font family class used.

Definition at line 276 of file text.hpp.

Referenced by recalculate(), and set_family_class().

unsigned font::ttext::font_size_
private

The font size to draw.

Definition at line 279 of file text.hpp.

Referenced by recalculate(), and set_font_size().

unsigned font::ttext::font_style_
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().

Uint32 font::ttext::foreground_color_
private

The foreground color.

Definition at line 285 of file text.hpp.

Referenced by rerender(), and set_foreground_color().

PangoLayout* font::ttext::layout_
private
size_t font::ttext::length_
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().

bool font::ttext::link_aware_
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().

std::string font::ttext::link_color_
private

The color to render links in.

Definition at line 273 of file text.hpp.

Referenced by handle_token(), and set_link_color().

bool font::ttext::markedup_text_
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().

int font::ttext::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 321 of file text.hpp.

Referenced by recalculate(), rerender(), and set_maximum_height().

size_t font::ttext::maximum_length_
private

The maximum length of the text.

Definition at line 330 of file text.hpp.

Referenced by insert_text(), and set_maximum_length().

int font::ttext::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 295 of file text.hpp.

Referenced by recalculate(), rerender(), and set_maximum_width().

PangoRectangle font::ttext::rect_
mutableprivate

Definition at line 254 of file text.hpp.

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

const unsigned font::ttext::STYLE_BOLD = TTF_STYLE_BOLD
static

Bold text.

Definition at line 145 of file text.hpp.

const unsigned font::ttext::STYLE_ITALIC = TTF_STYLE_ITALIC
static

Italicized text.

Definition at line 146 of file text.hpp.

const unsigned font::ttext::STYLE_NORMAL = TTF_STYLE_NORMAL
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().

const unsigned font::ttext::STYLE_UNDERLINE = TTF_STYLE_UNDERLINE
static

Underlined text.

Definition at line 147 of file text.hpp.

surface font::ttext::surface_
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().

unsigned char* font::ttext::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.

Definition at line 373 of file text.hpp.

Referenced by create_surface_buffer(), rerender(), and ~ttext().

bool font::ttext::surface_dirty_
mutableprivate
std::string font::ttext::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().


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