The Battle for Wesnoth  1.19.5+dev
Classes | Enumerations | Functions | Variables
font Namespace Reference

Graphical text output. More...

Classes

struct  floating_label_context
 structure which will hide all current floating labels, and cause floating labels instantiated after it is created to be displayed More...
 
class  floating_label
 
struct  error
 
struct  manager
 
struct  subset_descriptor
 
class  p_font
 Small helper class to make sure the pango font object is destroyed properly. More...
 
struct  inverse_table
 
class  pango_text
 Text class. More...
 

Enumerations

enum  ALIGN { LEFT_ALIGN , CENTER_ALIGN , RIGHT_ALIGN }
 
enum  LABEL_SCROLL_MODE { ANCHOR_LABEL_SCREEN , ANCHOR_LABEL_MAP }
 
enum  family_class { FONT_SANS_SERIF , FONT_MONOSPACE , FONT_LIGHT , FONT_SCRIPT }
 Font classes for get_font_families(). More...
 

Functions

int add_floating_label (const floating_label &flabel)
 add a label floating on the screen above everything else. More...
 
void move_floating_label (int handle, double xmove, double ymove)
 moves the floating label given by 'handle' by (xmove,ymove) More...
 
void scroll_floating_labels (double xmove, double ymove)
 moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP More...
 
void remove_floating_label (int handle, const std::chrono::milliseconds &fadeout=std::chrono::milliseconds{0})
 removes the floating label given by 'handle' from the screen More...
 
void show_floating_label (int handle, bool show)
 hides or shows a floating label More...
 
SDL_Rect get_floating_label_rect (int handle)
 
void draw_floating_labels ()
 
void update_floating_labels ()
 
int relative_size (int size)
 
bool load_font_config ()
 
const t_stringget_font_families (family_class fclass=FONT_SANS_SERIF)
 Returns the currently defined fonts. More...
 
family_class str_to_family_class (const std::string &str)
 
std::string escape_text (const std::string &text)
 Escapes the pango markup characters in a text. More...
 
std::string semi_escape_text (const std::string &text)
 
bool looks_like_url (std::string_view str)
 
std::string format_as_link (const std::string &link, color_t color)
 
texture pango_render_text (const std::string &text, int size, const color_t &color, font::pango_text::FONT_STYLE style=font::pango_text::STYLE_NORMAL, bool use_markup=false, int max_width=-1)
 Returns a SDL texture containing the rendered text. More...
 
std::pair< int, int > pango_line_size (const std::string &line, int font_size, font::pango_text::FONT_STYLE font_style=font::pango_text::STYLE_NORMAL)
 Determine the width and height of a line of text given a certain font size. More...
 
std::string pango_line_ellipsize (const std::string &text, int font_size, int max_width, font::pango_text::FONT_STYLE font_style=font::pango_text::STYLE_NORMAL)
 If the text exceeds the specified max width, end it with an ellipsis (...) More...
 
std::string pango_word_wrap (const std::string &unwrapped_text, int font_size, int max_width, int max_height=-1, int max_lines=-1, bool partial_line=false)
 Uses Pango to word wrap text. More...
 
rect pango_draw_text (bool actually_draw, const rect &area, int size, const color_t &color, const std::string &text, int x, int y, bool use_tooltips=false, pango_text::FONT_STYLE style=pango_text::STYLE_NORMAL)
 Draws text on the screen. More...
 
int pango_line_width (const std::string &line, int font_size, font::pango_text::FONT_STYLE font_style=font::pango_text::STYLE_NORMAL)
 Determine the width of a line of text given a certain font size. More...
 
color_t string_to_color (const std::string &s)
 Return the color the string represents. More...
 
void flush_texture_cache ()
 Flush the rendered text cache. More...
 
static void unpremultiply (uint8_t &value, const unsigned div)
 
static void from_cairo_format (uint32_t &c)
 Converts from cairo-format ARGB32 premultiplied alpha to plain alpha. More...
 
pango_textget_text_renderer ()
 Returns a reference to a static pango_text object. More...
 
int get_max_height (unsigned size, font::family_class fclass=font::FONT_SANS_SERIF, pango_text::FONT_STYLE style=pango_text::STYLE_NORMAL)
 Returns the maximum glyph height of a font, in pixels. More...
 
constexpr float get_line_spacing_factor ()
 

Variables

const int SIZE_NORMAL = 17
 
const int SIZE_TINY = 13 * (SIZE_NORMAL / 17)
 
const int SIZE_SMALL = 15 * (SIZE_NORMAL / 17)
 
const int SIZE_BUTTON = 16 * (SIZE_NORMAL / 17)
 
const int SIZE_BUTTON_SMALL = 14 * (SIZE_NORMAL / 17)
 
const int SIZE_15 = 15 * (SIZE_NORMAL / 17)
 
const int SIZE_PLUS = 18 * (SIZE_NORMAL / 17)
 
const int SIZE_LARGE = 20 * (SIZE_NORMAL / 17)
 
const int SIZE_TITLE = 22 * (SIZE_NORMAL / 17)
 
const int SIZE_FLOAT_LABEL = 24 * (SIZE_NORMAL / 17)
 
const int SIZE_XLARGE = 36 * (SIZE_NORMAL / 17)
 
const std::size_t max_text_line_width = 4096
 
const std::string ellipsis = "..."
 
const std::string nbsp = " "
 
const std::string unicode_minus = "-"
 
const std::string unicode_en_dash = "–"
 
const std::string unicode_em_dash = "—"
 
const std::string unicode_figure_dash = "‒"
 
const std::string unicode_multiplication_sign = "×"
 
const std::string unicode_bullet = "•"
 
const std::string weapon_numbers_sep = "×"
 
const std::string weapon_details_sep = "–"
 
const color_t NORMAL_COLOR {221, 221, 221}
 
const color_t GRAY_COLOR {136, 136, 136}
 
const color_t LOBBY_COLOR {187, 187, 187}
 
const color_t GOOD_COLOR {0 , 181, 26 }
 
const color_t BAD_COLOR {255, 0 , 0 }
 
const color_t BLACK_COLOR {0 , 0 , 0 }
 
const color_t YELLOW_COLOR {255, 255, 0 }
 
const color_t BUTTON_COLOR {186, 172, 125}
 
const color_t PETRIFIED_COLOR {160, 160, 160}
 
const color_t TITLE_COLOR {186, 172, 125}
 
const color_t LABEL_COLOR {107, 140, 255}
 
const color_t BIGMAP_COLOR {255, 255, 255}
 
const color_t BLUE_COLOR {0 , 0 , 255}
 
const color_t DISABLED_COLOR = PETRIFIED_COLOR.inverse()
 
const color_t weapon_color {245, 230, 193}
 
const color_t good_dmg_color {130, 240, 50 }
 
const color_t bad_dmg_color {250, 140, 80 }
 
const color_t weapon_details_color {196, 176, 147}
 
const color_t inactive_details_color { 86, 86, 86}
 
const color_t inactive_ability_color {146, 146, 146}
 
const color_t unit_type_color {245, 230, 193}
 
const color_t race_color {166, 146, 117}
 
static constexpr inverse_table inverse_table_
 

Detailed Description

Graphical text output.

This module is used to display and measure text. Text can optionally contain special characters, which may change specified display properties such as colour or font size. If special characters are turned on, they can be escaped, C-style, using backslashes.

Enumeration Type Documentation

◆ ALIGN

Enumerator
LEFT_ALIGN 
CENTER_ALIGN 
RIGHT_ALIGN 

Definition at line 39 of file floating_label.hpp.

◆ family_class

Font classes for get_font_families().

Enumerator
FONT_SANS_SERIF 
FONT_MONOSPACE 
FONT_LIGHT 
FONT_SCRIPT 

Definition at line 26 of file font_options.hpp.

◆ LABEL_SCROLL_MODE

Enumerator
ANCHOR_LABEL_SCREEN 
ANCHOR_LABEL_MAP 

Definition at line 41 of file floating_label.hpp.

Function Documentation

◆ add_floating_label()

int font::add_floating_label ( const floating_label flabel)

◆ draw_floating_labels()

void font::draw_floating_labels ( )

Definition at line 358 of file floating_label.cpp.

References label.

Referenced by display::expose().

◆ escape_text()

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

◆ flush_texture_cache()

void font::flush_texture_cache ( )

Flush the rendered text cache.

Definition at line 61 of file text.cpp.

Referenced by video::deinit().

◆ format_as_link()

std::string font::format_as_link ( const std::string &  link,
color_t  color 
)
inline

Definition at line 33 of file hyperlink.hpp.

References color_t::to_hex_string().

Referenced by font::pango_text::format_links().

◆ from_cairo_format()

static void font::from_cairo_format ( uint32_t &  c)
static

Converts from cairo-format ARGB32 premultiplied alpha to plain alpha.

Parameters
ca uint32 representing the color

Definition at line 866 of file text.cpp.

References b, c, g, inverse_table_, and unpremultiply().

Referenced by font::pango_text::create_surface().

◆ get_floating_label_rect()

SDL_Rect font::get_floating_label_rect ( int  handle)

◆ get_font_families()

const t_string & font::get_font_families ( family_class  fclass)

◆ get_line_spacing_factor()

constexpr float font::get_line_spacing_factor ( )
constexpr

◆ get_max_height()

int font::get_max_height ( unsigned  size,
font::family_class  fclass = font::FONT_SANS_SERIF,
pango_text::FONT_STYLE  style = pango_text::STYLE_NORMAL 
)

Returns the maximum glyph height of a font, in pixels.

Parameters
sizeDesired font size in pixels.
fclassFont family to use for measurement.
styleFont style to select the correct variant for measurement.
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 1142 of file text.cpp.

References font::pango_text::get_max_glyph_height(), get_text_renderer(), font::pango_text::set_family_class(), font::pango_text::set_font_size(), font::pango_text::set_font_style(), and utf8::size().

Referenced by gui2::rich_label::add_link(), gui::textbox::add_text_line(), gui2::rich_label::get_parsed_text(), gui::textbox::textbox(), gui2::combobox::update_offsets(), gui2::multiline_text::update_offsets(), and gui2::text_box::update_offsets().

◆ get_text_renderer()

pango_text & font::get_text_renderer ( )

Returns a reference to a static pango_text object.

Since the class is essentially a render pipeline, there's no need for individual areas of the game to own their own renderers. Not to mention it isn't a trivial class; constructing one is likely to be expensive.

Definition at line 1136 of file text.cpp.

Referenced by font::floating_label::create_texture(), gui2::text_shape::draw(), display::draw_hex(), display::draw_report(), display::draw_text_in_hex(), get_max_height(), gui2::rich_label::get_offset_from_xy(), and gui2::rich_label::get_xy_from_offset().

◆ load_font_config()

bool font::load_font_config ( )

◆ looks_like_url()

bool font::looks_like_url ( std::string_view  str)
inline

Definition at line 28 of file hyperlink.hpp.

Referenced by font::pango_text::format_links(), and font::pango_text::get_link().

◆ move_floating_label()

void font::move_floating_label ( int  handle,
double  xmove,
double  ymove 
)

◆ pango_draw_text()

rect font::pango_draw_text ( bool  actually_draw,
const rect area,
int  size,
const color_t color,
const std::string &  text,
int  x,
int  y,
bool  use_tooltips = false,
pango_text::FONT_STYLE  style = pango_text::STYLE_NORMAL 
)

Draws text on the screen.

The text will be clipped to area. If the text runs outside of area horizontally, an ellipsis will be displayed at the end of it. If area is empty, the text will not be clipped.

If use_tooltips is true, then text with an ellipsis will have a tooltip set for it equivalent to the entire contents of the text.

A bounding rectangle of the text is returned. If actually_draw is true the text will also be drawn to the screen. Otherwise only the bounding rectangle is returned.

Definition at line 139 of file sdl_ttf_compat.cpp.

References tooltips::add_tooltip(), draw::blit(), rect::empty(), FONT_SANS_SERIF, utf8::size(), and t.

Referenced by gui::button::calculate_size(), gui::button::draw_contents(), location_palette_item::draw_contents(), and display::draw_label().

◆ pango_line_ellipsize()

std::string font::pango_line_ellipsize ( const std::string &  text,
int  font_size,
int  max_width,
font::pango_text::FONT_STYLE  font_style 
)

If the text exceeds the specified max width, end it with an ellipsis (...)

Definition at line 76 of file sdl_ttf_compat.cpp.

References ellipsis, ucs4::iterator_base< string_type, update_implementation >::end(), pango_line_width(), ucs4::iterator_base< string_type, update_implementation >::substr(), and WRN_FT.

◆ pango_line_size()

std::pair< int, int > font::pango_line_size ( const std::string &  line,
int  font_size,
font::pango_text::FONT_STYLE  font_style 
)

Determine the width and height of a line of text given a certain font size.

Definition at line 60 of file sdl_ttf_compat.cpp.

References FONT_SANS_SERIF, draw::line(), and s.

Referenced by pango_line_width().

◆ pango_line_width()

int font::pango_line_width ( const std::string &  line,
int  font_size,
font::pango_text::FONT_STYLE  font_style = font::pango_text::STYLE_NORMAL 
)
inline

Determine the width of a line of text given a certain font size.

Definition at line 50 of file sdl_ttf_compat.hpp.

References draw::line(), and pango_line_size().

Referenced by gui::textbox::add_text_line(), pango_line_ellipsize(), and editor::editor_display::set_help_string().

◆ pango_render_text()

texture font::pango_render_text ( const std::string &  text,
int  size,
const color_t color,
font::pango_text::FONT_STYLE  style,
bool  use_markup,
int  max_width 
)

Returns a SDL texture containing the rendered text.

Definition at line 44 of file sdl_ttf_compat.cpp.

References FONT_SANS_SERIF, and utf8::size().

Referenced by gui::textbox::add_text_line().

◆ pango_word_wrap()

std::string font::pango_word_wrap ( const std::string &  unwrapped_text,
int  font_size,
int  max_width,
int  max_height,
int  max_lines,
bool   
)

Uses Pango to word wrap text.

Definition at line 107 of file sdl_ttf_compat.cpp.

References FONT_SANS_SERIF, draw::line(), and font::pango_text::STYLE_NORMAL.

Referenced by display_chat_manager::add_chat_message(), and gui2::rich_label::split_in_width().

◆ relative_size()

int font::relative_size ( int  size)
inline

Definition at line 30 of file constants.hpp.

References utf8::size(), and SIZE_NORMAL.

◆ remove_floating_label()

void font::remove_floating_label ( int  handle,
const std::chrono::milliseconds &  fadeout = std::chrono::milliseconds{0} 
)

◆ scroll_floating_labels()

void font::scroll_floating_labels ( double  xmove,
double  ymove 
)

moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP

Definition at line 281 of file floating_label.cpp.

References ANCHOR_LABEL_MAP, and i.

Referenced by display::scroll().

◆ semi_escape_text()

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

Definition at line 52 of file escape.hpp.

References c.

Referenced by font::pango_text::validate_markup().

◆ show_floating_label()

void font::show_floating_label ( int  handle,
bool  value 
)

hides or shows a floating label

Definition at line 311 of file floating_label.cpp.

References i.

Referenced by terrain_label::calculate_shroud().

◆ str_to_family_class()

family_class font::str_to_family_class ( const std::string &  str)
inline

Definition at line 34 of file font_options.hpp.

References FONT_LIGHT, FONT_MONOSPACE, FONT_SANS_SERIF, and FONT_SCRIPT.

◆ string_to_color()

color_t font::string_to_color ( const std::string &  s)

Return the color the string represents.

Return font::NORMAL_COLOR if the string is empty or can't be matched against any other color.

Definition at line 47 of file standard_colors.cpp.

References BAD_COLOR, BIGMAP_COLOR, BLACK_COLOR, BLUE_COLOR, color_t::from_hex_string(), GOOD_COLOR, NORMAL_COLOR, and YELLOW_COLOR.

Referenced by gui2::text_shape::draw(), and gui2::rich_label::get_parsed_text().

◆ unpremultiply()

static void font::unpremultiply ( uint8_t &  value,
const unsigned  div 
)
static

Definition at line 850 of file text.cpp.

Referenced by from_cairo_format().

◆ update_floating_labels()

void font::update_floating_labels ( )

Definition at line 375 of file floating_label.cpp.

References DBG_FT, and label.

Referenced by display::layout().

Variable Documentation

◆ BAD_COLOR

const color_t font::BAD_COLOR {255, 0 , 0 }

◆ bad_dmg_color

const color_t font::bad_dmg_color {250, 140, 80 }

◆ BIGMAP_COLOR

const color_t font::BIGMAP_COLOR {255, 255, 255}

Definition at line 32 of file standard_colors.cpp.

Referenced by string_to_color().

◆ BLACK_COLOR

const color_t font::BLACK_COLOR {0 , 0 , 0 }

Definition at line 26 of file standard_colors.cpp.

Referenced by string_to_color().

◆ BLUE_COLOR

const color_t font::BLUE_COLOR {0 , 0 , 255}

Definition at line 33 of file standard_colors.cpp.

Referenced by string_to_color().

◆ BUTTON_COLOR

const color_t font::BUTTON_COLOR {186, 172, 125}

◆ DISABLED_COLOR

const color_t font::DISABLED_COLOR = PETRIFIED_COLOR.inverse()

Definition at line 35 of file standard_colors.cpp.

◆ ellipsis

const std::string font::ellipsis = "..."

Definition at line 39 of file constants.cpp.

Referenced by utils::ellipsis_truncate(), and pango_line_ellipsize().

◆ GOOD_COLOR

const color_t font::GOOD_COLOR {0 , 181, 26 }

◆ good_dmg_color

const color_t font::good_dmg_color {130, 240, 50 }

◆ GRAY_COLOR

const color_t font::GRAY_COLOR {136, 136, 136}

◆ inactive_ability_color

const color_t font::inactive_ability_color {146, 146, 146}

Definition at line 43 of file standard_colors.cpp.

Referenced by unit_abilities().

◆ inactive_details_color

const color_t font::inactive_details_color { 86, 86, 86}

Definition at line 42 of file standard_colors.cpp.

Referenced by attack_info(), and attack_type::weapon_specials().

◆ inverse_table_

constexpr inverse_table font::inverse_table_
staticconstexpr

Definition at line 844 of file text.cpp.

Referenced by from_cairo_format().

◆ LABEL_COLOR

const color_t font::LABEL_COLOR {107, 140, 255}

◆ LOBBY_COLOR

const color_t font::LOBBY_COLOR {187, 187, 187}

Definition at line 23 of file standard_colors.cpp.

◆ max_text_line_width

const std::size_t font::max_text_line_width = 4096

Definition at line 36 of file constants.cpp.

◆ nbsp

const std::string font::nbsp = " "

Definition at line 40 of file constants.cpp.

Referenced by help::unit_topic_generator::operator()().

◆ NORMAL_COLOR

const color_t font::NORMAL_COLOR {221, 221, 221}

◆ PETRIFIED_COLOR

const color_t font::PETRIFIED_COLOR {160, 160, 160}

Definition at line 29 of file standard_colors.cpp.

◆ race_color

const color_t font::race_color {166, 146, 117}

Definition at line 45 of file standard_colors.cpp.

◆ SIZE_15

const int font::SIZE_15 = 15 * (SIZE_NORMAL / 17)

Definition at line 28 of file constants.cpp.

Referenced by display_chat_manager::add_chat_message().

◆ SIZE_BUTTON

const int font::SIZE_BUTTON = 16 * (SIZE_NORMAL / 17)

Definition at line 25 of file constants.cpp.

◆ SIZE_BUTTON_SMALL

const int font::SIZE_BUTTON_SMALL = 14 * (SIZE_NORMAL / 17)

Definition at line 26 of file constants.cpp.

Referenced by display::create_buttons().

◆ SIZE_FLOAT_LABEL

const int font::SIZE_FLOAT_LABEL = 24 * (SIZE_NORMAL / 17)

Definition at line 32 of file constants.cpp.

Referenced by display::announce(), and game_display::float_label().

◆ SIZE_LARGE

const int font::SIZE_LARGE = 20 * (SIZE_NORMAL / 17)

Definition at line 30 of file constants.cpp.

Referenced by editor::editor_display::set_help_string().

◆ SIZE_NORMAL

const int font::SIZE_NORMAL = 17

◆ SIZE_PLUS

const int font::SIZE_PLUS = 18 * (SIZE_NORMAL / 17)

Definition at line 29 of file constants.cpp.

Referenced by display::set_diagnostic().

◆ SIZE_SMALL

const int font::SIZE_SMALL = 15 * (SIZE_NORMAL / 17)

◆ SIZE_TINY

const int font::SIZE_TINY = 13 * (SIZE_NORMAL / 17)

Definition at line 23 of file constants.cpp.

Referenced by display::draw_hex().

◆ SIZE_TITLE

const int font::SIZE_TITLE = 22 * (SIZE_NORMAL / 17)

Definition at line 31 of file constants.cpp.

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

◆ SIZE_XLARGE

const int font::SIZE_XLARGE = 36 * (SIZE_NORMAL / 17)

Definition at line 33 of file constants.cpp.

◆ TITLE_COLOR

const color_t font::TITLE_COLOR {186, 172, 125}

◆ unicode_bullet

const std::string font::unicode_bullet = "•"

◆ unicode_em_dash

const std::string font::unicode_em_dash = "—"

◆ unicode_en_dash

const std::string font::unicode_en_dash = "–"

◆ unicode_figure_dash

const std::string font::unicode_figure_dash = "‒"

◆ unicode_minus

const std::string font::unicode_minus = "-"

◆ unicode_multiplication_sign

const std::string font::unicode_multiplication_sign = "×"

◆ unit_type_color

const color_t font::unit_type_color {245, 230, 193}

◆ weapon_color

const color_t font::weapon_color {245, 230, 193}

◆ weapon_details_color

const color_t font::weapon_details_color {196, 176, 147}

◆ weapon_details_sep

const std::string font::weapon_details_sep = "–"

Definition at line 50 of file constants.cpp.

Referenced by attack_info(), and gui2::unit_preview_pane::print_attack_details().

◆ weapon_numbers_sep

const std::string font::weapon_numbers_sep = "×"

◆ YELLOW_COLOR

const color_t font::YELLOW_COLOR {255, 255, 0 }