00001 /* $Id: marked-up_text.hpp 54234 2012-05-19 20:01:40Z shadowmaster $ */ 00002 /* 00003 Copyright (C) 2003 - 2012 by David White <dave@whitevine.net> 00004 Part of the Battle for Wesnoth Project http://www.wesnoth.org/ 00005 00006 This program is free software; you can redistribute it and/or modify 00007 it under the terms of the GNU General Public License as published by 00008 the Free Software Foundation; either version 2 of the License, or 00009 (at your option) any later version. 00010 This program is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY. 00012 00013 See the COPYING file for more details. 00014 */ 00015 00016 /** @file */ 00017 00018 #ifndef MARKED_UP_TEXT_HPP_INCLUDED 00019 #define MARKED_UP_TEXT_HPP_INCLUDED 00020 00021 00022 class CVideo; 00023 struct surface; 00024 #include <SDL_video.h> 00025 #include <string> 00026 00027 namespace font { 00028 00029 /** Standard markups for color, size, font, images. */ 00030 extern const char LARGE_TEXT, SMALL_TEXT, BOLD_TEXT, NORMAL_TEXT, NULL_MARKUP, BLACK_TEXT, GRAY_TEXT, 00031 GOOD_TEXT, BAD_TEXT, GREEN_TEXT, RED_TEXT, COLOR_TEXT, IMAGE; 00032 00033 // some colors often used in UI 00034 extern const std::string weapon, weapon_details, unit_type, race; 00035 00036 extern const SDL_Color weapon_color, 00037 good_dmg_color, 00038 bad_dmg_color, 00039 weapon_details_color, 00040 unit_type_color, 00041 race_color; 00042 00043 // separator between damage-hits and range--type 00044 extern const std::string weapon_numbers_sep, weapon_details_sep; 00045 00046 /** Parses the markup-tags at the front of a string. */ 00047 std::string::const_iterator parse_markup(std::string::const_iterator i1, 00048 std::string::const_iterator i2, 00049 int* font_size, 00050 SDL_Color* color, int* style); 00051 00052 /** 00053 * Function to draw text on a surface. 00054 * 00055 * The text will be clipped to area. If the text runs outside of area 00056 * horizontally, an ellipsis will be displayed at the end of it. 00057 * 00058 * If use_tooltips is true, then text with an ellipsis will have a tooltip 00059 * set for it equivalent to the entire contents of the text. 00060 * 00061 * Some very basic 'markup' will be done on the text: 00062 * - any line beginning in # will be displayed in BAD_COLOR (red) 00063 * - any line beginning in @ will be displayed in GOOD_COLOR (green) 00064 * - any line beginning in + will be displayed with size increased by 2 00065 * - any line beginning in - will be displayed with size decreased by 2 00066 * - any line beginning with 0x0n will be displayed in the color of side n 00067 * 00068 * The above special characters can be quoted using a C-style backslash. 00069 * 00070 * A bounding rectangle of the text is returned. If dst is NULL, then the 00071 * text will not be drawn, and a bounding rectangle only will be returned. 00072 */ 00073 SDL_Rect draw_text(surface dst, const SDL_Rect& area, int size, 00074 const SDL_Color& color, const std::string& text, 00075 int x, int y, bool use_tooltips = false, int style = 0); 00076 00077 /** wrapper of the previous function, gui can also be NULL */ 00078 SDL_Rect draw_text(CVideo* gui, const SDL_Rect& area, int size, 00079 const SDL_Color& color, const std::string& text, 00080 int x, int y, bool use_tooltips = false, int style = 0); 00081 00082 /** Calculate the size of a text (in pixels) if it were to be drawn. */ 00083 SDL_Rect text_area(const std::string& text, int size, int style=0); 00084 00085 /** Copy string, but without tags at the beginning */ 00086 std::string del_tags(const std::string& text); 00087 00088 /** 00089 * Determine if char is one of the special chars used as markup. 00090 * 00091 * @retval true Input-char is a markup-char. 00092 * @retval false Input-char is a normal char. 00093 */ 00094 bool is_format_char(char c); 00095 00096 /** 00097 * Determine if a wchar_t is a CJK character 00098 * 00099 * @retval true Input-char is a CJK char 00100 * @retval false Input-char is a not CJK char. 00101 */ 00102 bool is_cjk_char(const wchar_t ch); 00103 00104 /** Create string of color-markup, such as "<255,255,0>" for yellow. */ 00105 std::string color2markup(const SDL_Color &color); 00106 00107 /** Creates the hexadecimal string of a color, such as "#ffff00" for yellow. */ 00108 std::string color2hexa(const SDL_Color &color); 00109 00110 /** 00111 * Creates pango markup of a color. 00112 * Don't forget to close it with a </span> 00113 */ 00114 std::string span_color(const SDL_Color &color); 00115 00116 /** 00117 * Wrap text. 00118 * 00119 * - If the text exceedes the specified max width, wrap it on a word basis. 00120 * - If this is not possible, e.g. the word is too big to fit, wrap it on a 00121 * - char basis. 00122 */ 00123 std::string word_wrap_text(const std::string& unwrapped_text, int font_size, 00124 int max_width, int max_height = -1, int max_lines = -1, bool partial_line = false); 00125 00126 /** 00127 * Draw text on the screen, fit text to maximum width, no markup, no tooltips. 00128 * 00129 * This method makes sure that the text fits within a given maximum width. 00130 * If a line exceedes this width, it will be wrapped 00131 * on a word basis if possible, otherwise on a char basis. 00132 * This method is otherwise similar to the draw_text method, 00133 * but it doesn't support special markup or tooltips. 00134 * 00135 * @returns A bounding rectangle of the text. 00136 */ 00137 SDL_Rect draw_wrapped_text(CVideo* gui, const SDL_Rect& area, int font_size, 00138 const SDL_Color& color, const std::string& text, 00139 int x, int y, int max_width); 00140 00141 } // end namespace font 00142 00143 #endif // MARKED_UP_TEXT_HPP_INCLUDED 00144
| Generated by doxygen 1.7.1 on Fri May 25 2012 01:03:05 for The Battle for Wesnoth | Gna! | Forum | Wiki | CIA | devdocs |