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