1 /*
2  * Copyright (C) 2006-2020 by the Widelands Development Team
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17  *
18  */
19 
20 #ifndef WL_GRAPHIC_TEXT_SDL_TTF_FONT_H
21 #define WL_GRAPHIC_TEXT_SDL_TTF_FONT_H
22 
23 #include <memory>
24 
25 #include <SDL_ttf.h>
26 
27 #include "graphic/text/texture_cache.h"
28 #include "graphic/texture.h"
29 
30 namespace RT {
31 
32 /**
33  * Wrapper object around a font.
34  *
35  * Fonts in our sense are defined by the general font shape (given by the font
36  * name) and the size of the font. Note that Bold and Italic are special in the
37  * regard that we expect that this is already handled by the Font File, so, the
38  * font loader directly loads DejaVuSans-Bold.ttf for example.
39  */
40 class IFont {
41 public:
42 	enum {
43 		DEFAULT = 0,
44 		BOLD = 1,
45 		ITALIC = 2,
46 		UNDERLINE = 4,
47 		SHADOW = 8,
48 	};
~IFont()49 	virtual ~IFont() {
50 	}
51 
52 	virtual void dimensions(const std::string&, int, uint16_t*, uint16_t*) = 0;
53 	virtual std::shared_ptr<const Image>
54 	render(const std::string&, const RGBColor& clr, int, TextureCache*) = 0;
55 
56 	virtual uint16_t ascent(int) const = 0;
57 	virtual TTF_Font* get_ttf_font() const = 0;
58 };
59 
60 // Implementation of a Font object using SDL_ttf.
61 class SdlTtfFont : public IFont {
62 public:
63 	SdlTtfFont(TTF_Font* ttf, const std::string& face, int ptsize, std::string* ttf_memory_block);
64 	~SdlTtfFont() override;
65 
66 	void dimensions(const std::string&, int, uint16_t* w, uint16_t* h) override;
67 	std::shared_ptr<const Image>
68 	render(const std::string&, const RGBColor& clr, int, TextureCache*) override;
69 	uint16_t ascent(int) const override;
get_ttf_font()70 	TTF_Font* get_ttf_font() const override {
71 		return font_;
72 	}
73 
74 private:
75 	void set_style(int);
76 
77 	TTF_Font* font_;
78 	int style_;
79 	const std::string font_name_;
80 	const int ptsize_;
81 	// Old version of SDLTtf seem to need to keep this around.
82 	std::unique_ptr<std::string> ttf_file_memory_block_;
83 };
84 
85 }  // namespace RT
86 
87 #endif  // end of include guard:
88