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