The Battle for Wesnoth  1.13.10+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
sdl_ttf.hpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2003 - 2017 by David White <dave@whitevine.net>
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 
15 #pragma once
16 
17 #include "constants.hpp"
18 #include "font_id.hpp"
19 #include "font_description.hpp"
20 #include "color.hpp"
21 
22 #include <string>
23 
24 #include <SDL_ttf.h>
25 
26 class surface;
27 
28 namespace font {
29 
30 // Returns a SDL surface containing the text rendered in a given color.
31 surface get_rendered_text(const std::string& text, int size, const color_t& color, int style=0);
32 
33 SDL_Rect draw_text_line(surface& gui_surface, const SDL_Rect& area, int size,
34  const color_t& color, const std::string& text,
35  int x, int y, bool use_tooltips, int style);
36 
37 // Returns the maximum height of a font, in pixels
38 int get_max_height(int size);
39 
40 ///
41 /// Determine the width of a line of text given a certain font size.
42 /// The font type used is the default wesnoth font type.
43 ///
44 int line_width(const std::string& line, int font_size, int style=TTF_STYLE_NORMAL);
45 
46 ///
47 /// Determine the size of a line of text given a certain font size. Similar to
48 /// line_width, but for both coordinates.
49 ///
50 SDL_Rect line_size(const std::string& line, int font_size, int style=TTF_STYLE_NORMAL);
51 
52 /**
53  * If the text exceeds the specified max width, end it with an ellipsis (...)
54  */
55 std::string make_text_ellipsis(const std::string& text, int font_size, int max_width,
56  int style = TTF_STYLE_NORMAL);
57 
58 
60 void cache_mode(CACHE mode);
61 
62 
63 
64 
65 /***
66  * RAII object which initializes and destroys SDL_TTF, and manages caches of open fonts
67  */
68 struct sdl_ttf {
69  sdl_ttf();
70  ~sdl_ttf();
71 
72  sdl_ttf(const sdl_ttf &) = delete;
73  sdl_ttf & operator = (const sdl_ttf &) = delete;
74 
75  // Load a font
76  static TTF_Font * get_font(font_id);
77 
78  // Set the list of fonts
79  static void set_font_list(const std::vector<subset_descriptor>& fontlist);
80 
81  // Split a utf8 string into text_chunks
82  static std::vector<text_chunk> split_text(const std::string & utf8_text);
83 
84 };
85 
86 
87 } // end namespace font
std::vector< char_t > string
Note: Specific to sdl_ttf.
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: sdl_ttf.cpp:421
CACHE
Definition: sdl_ttf.hpp:59
static std::vector< text_chunk > split_text(const std::string &utf8_text)
Definition: sdl_ttf.cpp:554
sdl_ttf & operator=(const sdl_ttf &)=delete
int get_max_height(int size)
Definition: sdl_ttf.cpp:407
size_t size(const utf8::string &str)
Length in characters of a UTF-8 string.
Definition: unicode.cpp:86
void cache_mode(CACHE mode)
Definition: sdl_ttf.cpp:469
static void set_font_list(const std::vector< subset_descriptor > &fontlist)
Definition: sdl_ttf.cpp:515
surface get_rendered_text(const std::string &str, int size, const color_t &color, int style)
Definition: sdl_ttf.cpp:333
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: sdl_ttf.cpp:443
SDL_Rect draw_text_line(surface &gui_surface, const SDL_Rect &area, int size, const color_t &color, const std::string &text, int x, int y, bool use_tooltips, int style)
Definition: sdl_ttf.cpp:339
const int font_size
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: sdl_ttf.cpp:416
static TTF_Font * get_font(font_id)
Definition: sdl_ttf.cpp:207