The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
marked-up_text.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 /** @file */
16 
17 #ifndef MARKED_UP_TEXT_HPP_INCLUDED
18 #define MARKED_UP_TEXT_HPP_INCLUDED
19 
20 
21 class CVideo;
22 struct surface;
23 #include <SDL_video.h>
24 #include <string>
26 
27 #ifdef SDL_GPU
28 #include "sdl/image.hpp"
29 #endif
30 
31 namespace font {
32 
33 /** Standard markups for color, size, font, images. */
36 
37 // some colors often used in UI
39 
40 extern const SDL_Color weapon_color,
47  race_color;
48 
49 // separator between damage-hits and range--type
51 
52 /** Parses the markup-tags at the front of a string. */
53 std::string::const_iterator parse_markup(std::string::const_iterator i1,
54  std::string::const_iterator i2,
55  int* font_size,
56  SDL_Color* color, int* style);
57 
58 /**
59  * Function to draw text on a surface.
60  *
61  * The text will be clipped to area. If the text runs outside of area
62  * horizontally, an ellipsis will be displayed at the end of it.
63  *
64  * If use_tooltips is true, then text with an ellipsis will have a tooltip
65  * set for it equivalent to the entire contents of the text.
66  *
67  * Some very basic 'markup' will be done on the text:
68  * - any line beginning in # will be displayed in BAD_COLOR (red)
69  * - any line beginning in @ will be displayed in GOOD_COLOR (green)
70  * - any line beginning in + will be displayed with size increased by 2
71  * - any line beginning in - will be displayed with size decreased by 2
72  * - any line beginning with 0x0n will be displayed in the color of side n
73  *
74  * The above special characters can be quoted using a C-style backslash.
75  *
76  * A bounding rectangle of the text is returned. If dst is nullptr, then the
77  * text will not be drawn, and a bounding rectangle only will be returned.
78  */
79 SDL_Rect draw_text(surface& dst, const SDL_Rect& area, int size,
80  const SDL_Color& color, const std::string& text,
81  int x, int y, bool use_tooltips = false, int style = 0);
82 
83 /** wrapper of the previous function, gui can also be nullptr */
84 SDL_Rect draw_text(CVideo* gui, const SDL_Rect& area, int size,
85  const SDL_Color& color, const std::string& text,
86  int x, int y, bool use_tooltips = false, int style = 0);
87 
88 #ifdef SDL_GPU
89 sdl::timage draw_text_to_texture(const SDL_Rect& area, int size,
90  const SDL_Color& color,
91  const std::string& text,
92  bool use_tooltips = false, int style = 0);
93 #endif
94 
95 /** Calculate the size of a text (in pixels) if it were to be drawn. */
96 SDL_Rect text_area(const std::string& text, int size, int style=0);
97 
98 /** Copy string, but without tags at the beginning */
99 std::string del_tags(const std::string& text);
100 
101 /**
102  * Determine if char is one of the special chars used as markup.
103  *
104  * @retval true Input-char is a markup-char.
105  * @retval false Input-char is a normal char.
106  */
107 bool is_format_char(char c);
108 
109 /**
110  * Determine if a ucs4::char_t is a CJK character
111  *
112  * @retval true Input-char is a CJK char
113  * @retval false Input-char is a not CJK char.
114  */
115 bool is_cjk_char(const ucs4::char_t ch);
116 
117 /** Create string of color-markup, such as "<255,255,0>" for yellow. */
118 std::string color2markup(const SDL_Color &color);
119 
120 /** Creates the hexadecimal string of a color, such as "#ffff00" for yellow. */
121 std::string color2hexa(const SDL_Color &color);
122 
123 /**
124  * Creates pango markup of a color.
125  * Don't forget to close it with a @c </span>.
126  */
127 std::string span_color(const SDL_Color &color);
128 
129 /**
130  * Wrap text.
131  *
132  * - If the text exceeds the specified max width, wrap it on a word basis.
133  * - If this is not possible, e.g. the word is too big to fit, wrap it on a
134  * - char basis.
135  */
136 std::string word_wrap_text(const std::string& unwrapped_text, int font_size,
137  int max_width, int max_height = -1, int max_lines = -1, bool partial_line = false);
138 
139 /**
140  * Draw text on the screen, fit text to maximum width, no markup, no tooltips.
141  *
142  * This method makes sure that the text fits within a given maximum width.
143  * If a line exceeds this width, it will be wrapped
144  * on a word basis if possible, otherwise on a char basis.
145  * This method is otherwise similar to the draw_text method,
146  * but it doesn't support special markup or tooltips.
147  *
148  * @returns A bounding rectangle of the text.
149  */
150 SDL_Rect draw_wrapped_text(CVideo* gui, const SDL_Rect& area, int font_size,
151  const SDL_Color& color, const std::string& text,
152  int x, int y, int max_width);
153 
154 } // end namespace font
155 
156 #endif // MARKED_UP_TEXT_HPP_INCLUDED
157 
const char COLOR_TEXT
const SDL_Color good_dmg_color
SDL_Rect text_area(const std::string &text, int size, int style)
Calculate the size of a text (in pixels) if it were to be drawn.
const SDL_Color unit_type_color
std::string color2hexa(const SDL_Color &color)
Creates the hexadecimal string of a color, such as "#ffff00" for yellow.
const std::string weapon_details_sep
const std::string race
Graphical text output.
const GLfloat * c
Definition: glew.h:12741
const char IMAGE
Definition: video.hpp:58
General purpose widgets.
const char LARGE_TEXT
Standard markups for color, size, font, images.
std::string word_wrap_text(const std::string &unwrapped_text, int font_size, int max_width, int max_height, int max_lines, bool partial_line)
Wrap text.
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
const char BLACK_TEXT
std::string span_color(const SDL_Color &color)
Creates pango markup of a color.
const SDL_Color inactive_details_color
const SDL_Color bad_dmg_color
const char GRAY_TEXT
const char BAD_TEXT
const char GREEN_TEXT
GLenum GLenum dst
Definition: glew.h:2392
const SDL_Color race_color
const SDL_Color weapon_details_color
GLuint color
Definition: glew.h:5801
std::string color2markup(const SDL_Color &color)
Create string of color-markup, such as "<255,255,0>" for yellow.
std::string::const_iterator parse_markup(std::string::const_iterator i1, std::string::const_iterator i2, int *font_size, SDL_Color *color, int *style)
Parses the markup-tags at the front of a string.
bool is_format_char(char c)
Determine if char is one of the special chars used as markup.
SDL_Rect draw_text(surface &dst, const SDL_Rect &area, int size, const SDL_Color &color, const std::string &txt, int x, int y, bool use_tooltips, int style)
Function to draw text on a surface.
const char NULL_MARKUP
Contains a wrapper class for the GPU_Image class.
const std::string weapon_details
SDL_Rect draw_wrapped_text(CVideo *gui, const SDL_Rect &area, int font_size, const SDL_Color &color, const std::string &text, int x, int y, int max_width)
Draw text on the screen, fit text to maximum width, no markup, no tooltips.
boost::uint32_t char_t
bool is_cjk_char(const ucs4::char_t ch)
Determine if a ucs4::char_t is a CJK character.
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
GLsizeiptr size
Definition: glew.h:1649
const char RED_TEXT
const SDL_Color weapon_color
const std::string weapon_numbers_sep
std::string del_tags(const std::string &text)
Copy string, but without tags at the beginning.
const char SMALL_TEXT
const char GOOD_TEXT
const char BOLD_TEXT
const std::string weapon
const SDL_Color inactive_ability_color
const int font_size
const std::string unit_type
GLsizei const GLcharARB ** string
Definition: glew.h:4503
const char NORMAL_TEXT