The Battle for Wesnoth  1.13.10+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 - 2017 by David White <dave@whitevine.net>
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 #pragma once
18 
19 #include "color.hpp"
20 
21 class CVideo;
22 class surface;
23 
24 #include <string>
26 
27 #include <SDL_rect.h>
28 
29 namespace font {
30 
31 /** Standard markups for color, size, font, images. */
34 
35 /** Parses the markup-tags at the front of a string. */
36 std::string::const_iterator parse_markup(std::string::const_iterator i1,
37  std::string::const_iterator i2,
38  int* font_size,
39  color_t* color, int* style);
40 
41 /**
42  * Function to draw text on a surface.
43  *
44  * The text will be clipped to area. If the text runs outside of area
45  * horizontally, an ellipsis will be displayed at the end of it.
46  *
47  * If use_tooltips is true, then text with an ellipsis will have a tooltip
48  * set for it equivalent to the entire contents of the text.
49  *
50  * Some very basic 'markup' will be done on the text:
51  * - any line beginning in # will be displayed in BAD_COLOR (red)
52  * - any line beginning in @ will be displayed in GOOD_COLOR (green)
53  * - any line beginning in + will be displayed with size increased by 2
54  * - any line beginning in - will be displayed with size decreased by 2
55  * - any line beginning with 0x0n will be displayed in the color of side n
56  *
57  * The above special characters can be quoted using a C-style backslash.
58  *
59  * A bounding rectangle of the text is returned. If dst is nullptr, then the
60  * text will not be drawn, and a bounding rectangle only will be returned.
61  */
62 SDL_Rect draw_text(surface& dst, const SDL_Rect& area, int size,
63  const color_t& color, const std::string& text,
64  int x, int y, bool use_tooltips = false, int style = 0);
65 
66 /** wrapper of the previous function, gui can also be nullptr */
67 SDL_Rect draw_text(CVideo* gui, const SDL_Rect& area, int size,
68  const color_t& color, const std::string& text,
69  int x, int y, bool use_tooltips = false, int style = 0);
70 
71 
72 /** Calculate the size of a text (in pixels) if it were to be drawn. */
73 SDL_Rect text_area(const std::string& text, int size, int style=0);
74 
75 /** Copy string, but without tags at the beginning */
76 std::string del_tags(const std::string& text);
77 
78 /**
79  * Determine if char is one of the special chars used as markup.
80  *
81  * @retval true Input-char is a markup-char.
82  * @retval false Input-char is a normal char.
83  */
84 bool is_format_char(char c);
85 
86 /**
87  * Determine if a ucs4::char_t is a CJK character
88  *
89  * @retval true Input-char is a CJK char
90  * @retval false Input-char is a not CJK char.
91  */
92 bool is_cjk_char(const ucs4::char_t ch);
93 
94 /**
95  * Wrap text.
96  *
97  * - If the text exceeds the specified max width, wrap it on a word basis.
98  * - If this is not possible, e.g. the word is too big to fit, wrap it on a
99  * - char basis.
100  */
101 std::string word_wrap_text(const std::string& unwrapped_text, int font_size,
102  int max_width, int max_height = -1, int max_lines = -1, bool partial_line = false);
103 
104 } // end namespace font
const char COLOR_TEXT
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.
std::vector< char_t > string
Note: Specific to sdl_ttf.
std::string::const_iterator parse_markup(std::string::const_iterator i1, std::string::const_iterator i2, int *font_size, color_t *color, int *style)
Parses the markup-tags at the front of a string.
const char IMAGE
Definition: video.hpp:29
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.
uint32_t char_t
const char BLACK_TEXT
const char GRAY_TEXT
const char BAD_TEXT
const char GREEN_TEXT
bool is_format_char(char c)
Determine if char is one of the special chars used as markup.
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
const char NULL_MARKUP
bool is_cjk_char(const ucs4::char_t ch)
Determine if a ucs4::char_t is a CJK character.
const char RED_TEXT
std::string del_tags(const std::string &text)
Copy string, but without tags at the beginning.
const char SMALL_TEXT
const char GOOD_TEXT
SDL_Rect draw_text(surface &dst, const SDL_Rect &area, int size, const color_t &color, const std::string &txt, int x, int y, bool use_tooltips, int style)
Function to draw text on a surface.
const char BOLD_TEXT
mock_char c
const int font_size
const char NORMAL_TEXT