The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
font.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2016 by David White <[email protected]>
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 #ifndef FONT_HPP_INCLUDED
15 #define FONT_HPP_INCLUDED
16 
17 #include "exceptions.hpp"
18 #include "font_options.hpp"
19 
20 #include "sdl/utils.hpp"
21 #include "sdl/image.hpp"
22 
23 #include <SDL_ttf.h>
24 
25 class t_string;
26 
27 namespace font {
28 
29 //object which initializes and destroys structures needed for fonts
30 struct manager {
31  manager();
32  ~manager();
33 
34  /**
35  * Updates the font path, when initialized it sets the fontpath to
36  * game_config::path. When this path is updated, this function should be
37  * called.
38  */
39  void update_font_path() const;
40 
41  struct error : public game::error {
42  error() : game::error("Font initialization failed") {}
43  };
44 private:
45  /** Initializes the font path. */
46  void init() const;
47 
48  /** Deinitializes the font path. */
49  void deinit() const;
50 };
51 
52 //various standard colors
53 extern const SDL_Color NORMAL_COLOR, GRAY_COLOR, LOBBY_COLOR, GOOD_COLOR, BAD_COLOR,
56 
57 // font sizes, to be made theme parameters
58 const int SIZE_NORMAL = 14;
59 // automatic computation of other font sizes, to be made a default for theme-provided values
60 const int
61  SIZE_TINY = 10 * SIZE_NORMAL / 14,
62  SIZE_SMALL = 12 * SIZE_NORMAL / 14,
63 
64  SIZE_15 = 15 * SIZE_NORMAL / 14,
65  SIZE_PLUS = 16 * SIZE_NORMAL / 14,
66  SIZE_LARGE = 18 * SIZE_NORMAL / 14,
67  SIZE_TITLE = 20 * SIZE_NORMAL / 14,
68  SIZE_XLARGE = 24 * SIZE_NORMAL / 14
69  ;
70 // For arbitrary scaling:
71 // (Not used in defining the SIZE_* consts because of spurious compiler warnings.)
72 inline int relative_size(int size)
73 {
74  return (SIZE_NORMAL * size / 14);
75 }
76 
77 // Returns a SDL surface containing the text rendered in a given color.
78 surface get_rendered_text(const std::string& text, int size, const SDL_Color& color, int style=0);
79 
80 SDL_Rect draw_text_line(surface& gui_surface, const SDL_Rect& area, int size,
81  const SDL_Color& color, const std::string& text,
82  int x, int y, bool use_tooltips, int style);
83 
84 // Returns the maximum height of a font, in pixels
85 int get_max_height(int size);
86 
87 ///
88 /// Determine the width of a line of text given a certain font size.
89 /// The font type used is the default wesnoth font type.
90 ///
91 int line_width(const std::string& line, int font_size, int style=TTF_STYLE_NORMAL);
92 
93 ///
94 /// Determine the size of a line of text given a certain font size. Similar to
95 /// line_width, but for both coordinates.
96 ///
97 SDL_Rect line_size(const std::string& line, int font_size, int style=TTF_STYLE_NORMAL);
98 
99 /**
100  * If the text exceeds the specified max width, end it with an ellipsis (...)
101  */
102 std::string make_text_ellipsis(const std::string& text, int font_size, int max_width,
103  int style = TTF_STYLE_NORMAL);
104 
105 
106 bool load_font_config();
107 
108 /** Returns the currently defined fonts. */
110 
112 void cache_mode(CACHE mode);
113 
114 }
115 
116 #endif
const SDL_Color BIGMAP_COLOR
Definition: font.cpp:575
const SDL_Color BUTTON_COLOR
Definition: font.cpp:571
family_class
Font classes for get_font_families().
const SDL_Color TITLE_COLOR
Definition: font.cpp:573
const SDL_Color PETRIFIED_COLOR
Definition: font.cpp:572
Graphical text output.
const SDL_Color BLACK_COLOR
Definition: font.cpp:569
int relative_size(int size)
Definition: font.hpp:72
SDL_Rect line_size(const std::string &line, int font_size, int style)
Determine the size of a line of text given a certain font size.
Definition: font.cpp:974
CACHE
Definition: font.hpp:111
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
const int SIZE_PLUS
Definition: font.hpp:65
GLenum mode
Definition: glew.h:2390
const SDL_Color NORMAL_COLOR
Definition: font.cpp:564
const int SIZE_NORMAL
Definition: font.hpp:58
const SDL_Color GOOD_COLOR
Definition: font.cpp:567
const int SIZE_XLARGE
Definition: font.hpp:68
const SDL_Color DISABLED_COLOR
Definition: font.cpp:576
const t_string & get_font_families(family_class fclass)
Returns the currently defined fonts.
Definition: font.cpp:1110
const SDL_Color BAD_COLOR
Definition: font.cpp:568
const int SIZE_15
Definition: font.hpp:64
GLuint color
Definition: glew.h:5801
int get_max_height(int size)
Definition: font.cpp:960
bool load_font_config()
Definition: font.cpp:1048
void update_font_path() const
Updates the font path, when initialized it sets the fontpath to game_config::path.
Definition: font.cpp:386
void init() const
Initializes the font path.
Definition: font.cpp:392
void cache_mode(CACHE mode)
Definition: font.cpp:1120
Contains a wrapper class for the GPU_Image class.
const SDL_Color LABEL_COLOR
Definition: font.cpp:574
const SDL_Color LOBBY_COLOR
Definition: font.cpp:566
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
const int SIZE_TITLE
Definition: font.hpp:67
void deinit() const
Deinitializes the font path.
Definition: font.cpp:431
std::string make_text_ellipsis(const std::string &text, int font_size, int max_width, int style)
If the text exceeds the specified max width, end it with an ellipsis (...)
Definition: font.cpp:996
const int SIZE_LARGE
Definition: font.hpp:66
GLsizeiptr size
Definition: glew.h:1649
SDL_Rect draw_text_line(surface &gui_surface, const SDL_Rect &area, int size, const SDL_Color &color, const std::string &text, int x, int y, bool use_tooltips, int style)
Definition: font.cpp:892
Base class for all the errors encountered by the engine.
Definition: exceptions.hpp:27
const SDL_Color GRAY_COLOR
Definition: font.cpp:565
const SDL_Color YELLOW_COLOR
Definition: font.cpp:570
surface get_rendered_text(const std::string &str, int size, const SDL_Color &color, int style)
Definition: font.cpp:886
const int font_size
GLsizei const GLcharARB ** string
Definition: glew.h:4503
int line_width(const std::string &line, int font_size, int style)
Determine the width of a line of text given a certain font size.
Definition: font.cpp:969
const int SIZE_SMALL
Definition: font.hpp:62
const int SIZE_TINY
Definition: font.hpp:61