1 // Copyright 2015-2016 the openage authors. See copying.md for legal info.
2 
3 #pragma once
4 
5 #include <ft2build.h>
6 #include FT_FREETYPE_H
7 #include <memory>
8 #include <string>
9 #include <unordered_map>
10 
11 #include "../../error/error.h"
12 
13 namespace openage {
14 namespace renderer {
15 
16 class FreeTypeLibrary {
17 
18 public:
19 	FT_Library ft_library;
20 
21 public:
FreeTypeLibrary()22 	FreeTypeLibrary() {
23 		if (FT_Init_FreeType(&this->ft_library) != 0) {
24 			throw Error{MSG(err) << "Failed to initialize freetype library."};
25 		}
26 	}
27 
~FreeTypeLibrary()28 	~FreeTypeLibrary() {
29 		FT_Done_FreeType(this->ft_library);
30 	}
31 
32 	FreeTypeLibrary(const FreeTypeLibrary &copy) = delete;
33 
34 	FreeTypeLibrary &operator=(const FreeTypeLibrary &copy) = delete;
35 
36 	FreeTypeLibrary(FreeTypeLibrary &&other) = delete;
37 
38 	FreeTypeLibrary &operator=(FreeTypeLibrary &&other) = delete;
39 
40 };
41 
42 class Font;
43 
44 class FontManager {
45 
46 public:
47 	/**
48 	 * Gets the filepath of a particular font family and style.
49 	 *
50 	 * @param family: The font family.
51 	 * @param style: The font style.
52 	 * @returns The path to font's file.
53 	 */
54 	static std::string get_font_filename(const char *family, const char *style);
55 
56 public:
57 	FontManager();
58 
59 	~FontManager();
60 
61 	/**
62 	 * Get the freetype library instance.
63 	 */
64 	FT_Library get_ft_library();
65 
66 	/**
67 	 * Retrieves a font.
68 	 *
69 	 * @param family: The font family.
70 	 * @param style: The font style.
71 	 * @param size: The size of the font in points.
72 	 * @returns The pointer to font instance.
73 	 */
74 	Font *get_font(const char *family, const char *style, unsigned int size);
75 
76 	/**
77 	 * Retrieves a font.
78 	 *
79 	 * @param font_file: The path to font's file.
80 	 * @param size: The size of the font in points.
81 	 * @returns The pointer to font instance.
82 	 */
83 	Font *get_font(const char* font_file, unsigned int size);
84 
85 private:
86 	// The freetype library instance
87 	FreeTypeLibrary library;
88 
89 	// Font cache. the hash of font's description is used as the key
90 	std::unordered_map<size_t, std::unique_ptr<Font>> fonts;
91 
92 };
93 
94 }} // openage::renderer
95