font.hpp

Go to the documentation of this file.
00001 /* $Id: font.hpp 53489 2012-03-10 13:07:25Z mordante $ */
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 #ifndef FONT_HPP_INCLUDED
00016 #define FONT_HPP_INCLUDED
00017 
00018 #include "exceptions.hpp"
00019 #include "SDL_ttf.h"
00020 
00021 #include "sdl_utils.hpp"
00022 
00023 class t_string;
00024 
00025 namespace font {
00026 
00027 //object which initializes and destroys structures needed for fonts
00028 struct manager {
00029     manager();
00030     ~manager();
00031 
00032     /**
00033      * Updates the font path, when initialized it sets the fontpath to
00034      * game_config::path. When this path is updated, this function should be
00035      * called.
00036      */
00037     void update_font_path() const;
00038 
00039     struct error : public game::error {
00040         error() : game::error("Font initialization failed") {}
00041     };
00042 private:
00043     /** Initializes the font path. */
00044     void init() const;
00045 
00046     /** Deinitializes the font path. */
00047     void deinit() const;
00048 };
00049 
00050 //various standard colors
00051 extern const SDL_Color NORMAL_COLOR, GRAY_COLOR, LOBBY_COLOR, GOOD_COLOR, BAD_COLOR,
00052                        BLACK_COLOR, YELLOW_COLOR, BUTTON_COLOR, BIGMAP_COLOR,
00053                        PETRIFIED_COLOR, TITLE_COLOR, DISABLED_COLOR, LABEL_COLOR;
00054 
00055 // font sizes, to be made theme parameters
00056 const int SIZE_NORMAL = 14;
00057 inline int relative_size(int size)
00058 {
00059     return (SIZE_NORMAL * size / 14);
00060 }
00061 
00062 // automatic computation of other font sizes, to be made a default for theme-provided values
00063 const int
00064     SIZE_TINY       = relative_size(10),
00065     SIZE_SMALL      = relative_size(12),
00066 
00067     SIZE_15         = relative_size(15),
00068     SIZE_PLUS       = relative_size(16),
00069     SIZE_LARGE      = relative_size(18),
00070     SIZE_XLARGE     = relative_size(24)
00071   ;
00072 
00073 // Returns a SDL surface containing the text rendered in a given color.
00074 surface get_rendered_text(const std::string& text, int size, const SDL_Color& color, int style=0);
00075 
00076 SDL_Rect draw_text_line(surface gui_surface, const SDL_Rect& area, int size,
00077                         const SDL_Color& color, const std::string& text,
00078                         int x, int y, bool use_tooltips, int style);
00079 
00080 // Returns the maximum height of a font, in pixels
00081 int get_max_height(int size);
00082 
00083 ///
00084 /// Determine the width of a line of text given a certain font size.
00085 /// The font type used is the default wesnoth font type.
00086 ///
00087 int line_width(const std::string& line, int font_size, int style=TTF_STYLE_NORMAL);
00088 
00089 ///
00090 /// Determine the size of a line of text given a certain font size. Similar to
00091 /// line_width, but for both coordinates.
00092 ///
00093 SDL_Rect line_size(const std::string& line, int font_size, int style=TTF_STYLE_NORMAL);
00094 
00095 /**
00096  * If the text excedes the specified max width, end it with an ellipsis (...)
00097  */
00098 std::string make_text_ellipsis(const std::string& text, int font_size, int max_width,
00099     int style = TTF_STYLE_NORMAL);
00100 
00101 
00102 /// structure which will hide all current floating labels, and cause floating labels
00103 /// instantiated after it is created to be displayed
00104 struct floating_label_context
00105 {
00106     floating_label_context();
00107     ~floating_label_context();
00108 };
00109 
00110 enum ALIGN { LEFT_ALIGN, CENTER_ALIGN, RIGHT_ALIGN };
00111 
00112 enum LABEL_SCROLL_MODE { ANCHOR_LABEL_SCREEN, ANCHOR_LABEL_MAP };
00113 
00114 class floating_label
00115 {
00116 public:
00117     floating_label(const std::string& text);
00118 
00119     void set_font_size(int font_size) {font_size_ = font_size;}
00120 
00121     // set the location on the screen to display the text.
00122     void set_position(double xpos, double ypos){
00123         xpos_ = xpos;
00124         ypos_ = ypos;
00125     }
00126     // set the amount to move the text each frame
00127     void set_move(double xmove, double ymove){
00128         xmove_ = xmove;
00129         ymove_ = ymove;
00130     }
00131     // set the number of frames to display the text for, or -1 to display until removed
00132     void set_lifetime(int lifetime) {
00133         lifetime_ = lifetime;
00134         alpha_change_ = -255 / lifetime_;
00135     }
00136     void set_color(const SDL_Color& color) {color_ = color;}
00137     void set_bg_color(const SDL_Color& bg_color) {
00138         bgcolor_ = bg_color;
00139         bgalpha_ = bg_color.unused;
00140     }
00141     void set_border_size(int border) {border_ = border;}
00142     // set width for word wrapping (use -1 to disable it)
00143     void set_width(int w) {width_ = w;}
00144     void set_height(int h) { height_ = h; }
00145     void set_clip_rect(const SDL_Rect& r) {clip_rect_ = r;}
00146     void set_alignment(ALIGN align) {align_ = align;}
00147     void set_scroll_mode(LABEL_SCROLL_MODE scroll) {scroll_ = scroll;}
00148     void use_markup(bool b) {use_markup_ = b;}
00149 
00150     void move(double xmove, double ymove);
00151 
00152     void draw(surface screen);
00153     void undraw(surface screen);
00154 
00155     surface create_surface();
00156 
00157     bool expired() const { return lifetime_ == 0; }
00158 
00159     void show(const bool value) { visible_ = value; }
00160 
00161     LABEL_SCROLL_MODE scroll() const { return scroll_; }
00162 
00163 private:
00164 
00165     int xpos(size_t width) const;
00166 
00167     surface surf_, buf_;
00168     std::string text_;
00169     int font_size_;
00170     SDL_Color color_, bgcolor_;
00171     int bgalpha_;
00172     double xpos_, ypos_, xmove_, ymove_;
00173     int lifetime_;
00174     int width_, height_;
00175     SDL_Rect clip_rect_;
00176     int alpha_change_;
00177     bool visible_;
00178     font::ALIGN align_;
00179     int border_;
00180     LABEL_SCROLL_MODE scroll_;
00181     bool use_markup_;
00182 };
00183 
00184 
00185 /// add a label floating on the screen above everything else.
00186 /// @returns a handle to the label which can be used with other label functions
00187 
00188 int add_floating_label(const floating_label& flabel);
00189 
00190 
00191 /// moves the floating label given by 'handle' by (xmove,ymove)
00192 void move_floating_label(int handle, double xmove, double ymove);
00193 
00194 /// moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP
00195 void scroll_floating_labels(double xmove, double ymove);
00196 
00197 /// removes the floating label given by 'handle' from the screen
00198 void remove_floating_label(int handle);
00199 
00200 /// hides or shows a floating label
00201 void show_floating_label(int handle, bool show);
00202 
00203 SDL_Rect get_floating_label_rect(int handle);
00204 
00205 void draw_floating_labels(surface screen);
00206 void undraw_floating_labels(surface screen);
00207 
00208 bool load_font_config();
00209 
00210 /** Returns the currently defined fonts. */
00211 const t_string& get_font_families();
00212 
00213 enum CACHE { CACHE_LOBBY, CACHE_GAME };
00214 void cache_mode(CACHE mode);
00215 
00216 }
00217 
00218 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated by doxygen 1.7.1 on Thu May 24 2012 01:02:36 for The Battle for Wesnoth
Gna! | Forum | Wiki | CIA | devdocs