/
font.hpp
239 lines (191 loc) · 6.55 KB
/
font.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
Copyright (C) 2003 - 2014 by David White <dave@whitevine.net>
Part of the Battle for Wesnoth Project http://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#ifndef FONT_HPP_INCLUDED
#define FONT_HPP_INCLUDED
#include "exceptions.hpp"
#include "sdl/utils.hpp"
#include "sdl/image.hpp"
#include <SDL_ttf.h>
class t_string;
namespace font {
//object which initializes and destroys structures needed for fonts
struct manager {
manager();
~manager();
/**
* Updates the font path, when initialized it sets the fontpath to
* game_config::path. When this path is updated, this function should be
* called.
*/
void update_font_path() const;
struct error : public game::error {
error() : game::error("Font initialization failed") {}
};
private:
/** Initializes the font path. */
void init() const;
/** Deinitializes the font path. */
void deinit() const;
};
//various standard colors
extern const SDL_Color NORMAL_COLOR, GRAY_COLOR, LOBBY_COLOR, GOOD_COLOR, BAD_COLOR,
BLACK_COLOR, YELLOW_COLOR, BUTTON_COLOR, BIGMAP_COLOR,
PETRIFIED_COLOR, TITLE_COLOR, DISABLED_COLOR, LABEL_COLOR;
// font sizes, to be made theme parameters
const int SIZE_NORMAL = 14;
// automatic computation of other font sizes, to be made a default for theme-provided values
const int
SIZE_TINY = 10 * SIZE_NORMAL / 14,
SIZE_SMALL = 12 * SIZE_NORMAL / 14,
SIZE_15 = 15 * SIZE_NORMAL / 14,
SIZE_PLUS = 16 * SIZE_NORMAL / 14,
SIZE_LARGE = 18 * SIZE_NORMAL / 14,
SIZE_XLARGE = 24 * SIZE_NORMAL / 14
;
// For arbitrary scaling:
// (Not used in defining the SIZE_* consts because of spurious compiler warnings.)
inline int relative_size(int size)
{
return (SIZE_NORMAL * size / 14);
}
// Returns a SDL surface containing the text rendered in a given color.
surface get_rendered_text(const std::string& text, int size, const SDL_Color& color, int style=0);
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);
// Returns the maximum height of a font, in pixels
int get_max_height(int size);
///
/// Determine the width of a line of text given a certain font size.
/// The font type used is the default wesnoth font type.
///
int line_width(const std::string& line, int font_size, int style=TTF_STYLE_NORMAL);
///
/// Determine the size of a line of text given a certain font size. Similar to
/// line_width, but for both coordinates.
///
SDL_Rect line_size(const std::string& line, int font_size, int style=TTF_STYLE_NORMAL);
/**
* If the text exceeds the specified max width, end it with an ellipsis (...)
*/
std::string make_text_ellipsis(const std::string& text, int font_size, int max_width,
int style = TTF_STYLE_NORMAL);
/// structure which will hide all current floating labels, and cause floating labels
/// instantiated after it is created to be displayed
struct floating_label_context
{
floating_label_context();
~floating_label_context();
};
enum ALIGN { LEFT_ALIGN, CENTER_ALIGN, RIGHT_ALIGN };
enum LABEL_SCROLL_MODE { ANCHOR_LABEL_SCREEN, ANCHOR_LABEL_MAP };
class floating_label
{
public:
floating_label(const std::string& text);
void set_font_size(int font_size) {font_size_ = font_size;}
// set the location on the screen to display the text.
void set_position(double xpos, double ypos){
xpos_ = xpos;
ypos_ = ypos;
}
// set the amount to move the text each frame
void set_move(double xmove, double ymove){
xmove_ = xmove;
ymove_ = ymove;
}
// set the number of frames to display the text for, or -1 to display until removed
void set_lifetime(int lifetime) {
lifetime_ = lifetime;
alpha_change_ = -255 / lifetime_;
}
void set_color(const SDL_Color& color) {color_ = color;}
void set_bg_color(const SDL_Color& bg_color) {
bgcolor_ = bg_color;
#if SDL_VERSION_ATLEAST(2,0,0)
bgalpha_ = bg_color.a;
#else
bgalpha_ = bg_color.unused;
#endif
}
void set_border_size(int border) {border_ = border;}
// set width for word wrapping (use -1 to disable it)
void set_width(int w) {width_ = w;}
void set_height(int h) { height_ = h; }
void set_clip_rect(const SDL_Rect& r) {clip_rect_ = r;}
void set_alignment(ALIGN align) {align_ = align;}
void set_scroll_mode(LABEL_SCROLL_MODE scroll) {scroll_ = scroll;}
void use_markup(bool b) {use_markup_ = b;}
void move(double xmove, double ymove);
#ifdef SDL_GPU
void draw(CVideo &video);
void undraw(CVideo &video);
#else
void draw(surface screen);
void undraw(surface screen);
#endif
#if 0
sdl::timage create_image();
#else
surface create_surface();
#endif
bool expired() const { return lifetime_ == 0; }
void show(const bool value) { visible_ = value; }
LABEL_SCROLL_MODE scroll() const { return scroll_; }
private:
int xpos(size_t width) const;
#if 0
sdl::timage img_;
#else
surface surf_, buf_;
#endif
std::string text_;
int font_size_;
SDL_Color color_, bgcolor_;
int bgalpha_;
double xpos_, ypos_, xmove_, ymove_;
int lifetime_;
int width_, height_;
SDL_Rect clip_rect_;
int alpha_change_;
bool visible_;
font::ALIGN align_;
int border_;
LABEL_SCROLL_MODE scroll_;
bool use_markup_;
};
/// add a label floating on the screen above everything else.
/// @returns a handle to the label which can be used with other label functions
int add_floating_label(const floating_label& flabel);
/// moves the floating label given by 'handle' by (xmove,ymove)
void move_floating_label(int handle, double xmove, double ymove);
/// moves all floating labels that have 'scroll_mode' set to ANCHOR_LABEL_MAP
void scroll_floating_labels(double xmove, double ymove);
/// removes the floating label given by 'handle' from the screen
void remove_floating_label(int handle);
/// hides or shows a floating label
void show_floating_label(int handle, bool show);
SDL_Rect get_floating_label_rect(int handle);
#ifdef SDL_GPU
void draw_floating_labels(CVideo &video);
void undraw_floating_labels(CVideo &video);
#else
void draw_floating_labels(surface screen);
void undraw_floating_labels(surface screen);
#endif
bool load_font_config();
/** Returns the currently defined fonts. */
const t_string& get_font_families();
enum CACHE { CACHE_LOBBY, CACHE_GAME };
void cache_mode(CACHE mode);
}
#endif