1 #ifndef DYNAMICFONT_STB_H
2 #define DYNAMICFONT_STB_H
3 
4 #ifndef FREETYPE_ENABLED
5 
6 #include "font.h"
7 #include "io/resource_loader.h"
8 
9 #include "thirdparty/misc/stb_truetype.h"
10 
11 class DynamicFontAtSize;
12 class DynamicFont;
13 
14 class DynamicFontData : public Resource {
15 
16 	OBJ_TYPE(DynamicFontData, Resource);
17 
18 	bool valid;
19 
20 	DVector<uint8_t> font_data;
21 	DVector<uint8_t>::Read fr;
22 	const uint8_t *last_data_ptr;
23 
24 	struct KerningPairKey {
25 
26 		union {
27 			struct {
28 				uint32_t A, B;
29 			};
30 
31 			uint64_t pair;
32 		};
33 
34 		_FORCE_INLINE_ bool operator<(const KerningPairKey &p_r) const { return pair < p_r.pair; }
35 	};
36 
37 	Map<KerningPairKey, int> kerning_map;
38 
39 	Map<int, DynamicFontAtSize *> size_cache;
40 
41 	friend class DynamicFontAtSize;
42 
43 	stbtt_fontinfo info;
44 	int ascent;
45 	int descent;
46 	int linegap;
47 
48 	void lock();
49 	void unlock();
50 
51 	friend class DynamicFont;
52 
53 	Ref<DynamicFontAtSize> _get_dynamic_font_at_size(int p_size);
54 
55 public:
56 	void set_font_data(const DVector<uint8_t> &p_font);
57 	DynamicFontData();
58 	~DynamicFontData();
59 };
60 
61 class DynamicFontAtSize : public Reference {
62 
63 	OBJ_TYPE(DynamicFontAtSize, Reference);
64 
65 	int rect_margin;
66 
67 	struct CharTexture {
68 
69 		DVector<uint8_t> imgdata;
70 		int texture_size;
71 		Vector<int> offsets;
72 		Ref<ImageTexture> texture;
73 	};
74 
75 	Vector<CharTexture> textures;
76 
77 	struct Character {
78 
79 		int texture_idx;
80 		Rect2 rect;
81 		float v_align;
82 		float h_align;
83 		float advance;
84 
CharacterCharacter85 		Character() {
86 			texture_idx = 0;
87 			v_align = 0;
88 		}
89 	};
90 
91 	HashMap<CharType, Character> char_map;
92 
93 	_FORCE_INLINE_ void _update_char(CharType p_char);
94 
95 	friend class DynamicFontData;
96 	Ref<DynamicFontData> font;
97 	float scale;
98 	int size;
99 
100 protected:
101 public:
102 	float get_height() const;
103 
104 	float get_ascent() const;
105 	float get_descent() const;
106 
107 	Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
108 
109 	float draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next = 0, const Color &p_modulate = Color(1, 1, 1)) const;
110 
111 	DynamicFontAtSize();
112 	~DynamicFontAtSize();
113 };
114 
115 ///////////////
116 
117 class DynamicFont : public Font {
118 
119 	OBJ_TYPE(DynamicFont, Font);
120 
121 	Ref<DynamicFontData> data;
122 	Ref<DynamicFontAtSize> data_at_size;
123 	int size;
124 
125 protected:
126 	static void _bind_methods();
127 
128 public:
129 	void set_font_data(const Ref<DynamicFontData> &p_data);
130 	Ref<DynamicFontData> get_font_data() const;
131 
132 	void set_size(int p_size);
133 	int get_size() const;
134 
135 	virtual float get_height() const;
136 
137 	virtual float get_ascent() const;
138 	virtual float get_descent() const;
139 
140 	virtual Size2 get_char_size(CharType p_char, CharType p_next = 0) const;
141 
142 	virtual bool is_distance_field_hint() const;
143 
144 	virtual float draw_char(RID p_canvas_item, const Point2 &p_pos, CharType p_char, CharType p_next = 0, const Color &p_modulate = Color(1, 1, 1)) const;
145 
146 	DynamicFont();
147 	~DynamicFont();
148 };
149 
150 /////////////
151 
152 class ResourceFormatLoaderDynamicFont : public ResourceFormatLoader {
153 public:
154 	virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = NULL);
155 	virtual void get_recognized_extensions(List<String> *p_extensions) const;
156 	virtual bool handles_type(const String &p_type) const;
157 	virtual String get_resource_type(const String &p_path) const;
158 };
159 
160 #endif
161 #endif // DYNAMICFONT_H
162