summaryrefslogtreecommitdiff
path: root/scene/resources/font.h
blob: 086226c0825d8e1b1f9387b879b5a480079d5ccc (plain)
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
/*************************************************************************/
/*  font.h                                                               */
/*************************************************************************/
/*                       This file is part of:                           */
/*                           GODOT ENGINE                                */
/*                      https://godotengine.org                          */
/*************************************************************************/
/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur.                 */
/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md).   */
/*                                                                       */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the       */
/* "Software"), to deal in the Software without restriction, including   */
/* without limitation the rights to use, copy, modify, merge, publish,   */
/* distribute, sublicense, and/or sell copies of the Software, and to    */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions:                                             */
/*                                                                       */
/* The above copyright notice and this permission notice shall be        */
/* included in all copies or substantial portions of the Software.       */
/*                                                                       */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,       */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF    */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY  */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,  */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE     */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.                */
/*************************************************************************/

#ifndef FONT_H
#define FONT_H

#include "core/io/resource.h"
#include "core/templates/lru.h"
#include "core/templates/map.h"
#include "scene/resources/texture.h"
#include "servers/text_server.h"

/*************************************************************************/

class FontData : public Resource {
	GDCLASS(FontData, Resource);

	RID rid;
	int base_size = 16;
	String path;

protected:
	static void _bind_methods();

	bool _set(const StringName &p_name, const Variant &p_value);
	bool _get(const StringName &p_name, Variant &r_ret) const;
	void _get_property_list(List<PropertyInfo> *p_list) const;

public:
	virtual RID get_rid() const override;

	void load_resource(const String &p_filename, int p_base_size = 16);
	void load_memory(const uint8_t *p_data, size_t p_size, const String &p_type, int p_base_size = 16);
	void _load_memory(const PackedByteArray &p_data, const String &p_type, int p_base_size = 16);

	void set_data_path(const String &p_path);
	String get_data_path() const;

	float get_height(int p_size) const;
	float get_ascent(int p_size) const;
	float get_descent(int p_size) const;

	Dictionary get_feature_list() const;
	Dictionary get_variation_list() const;

	void set_variation(const String &p_name, double p_value);
	double get_variation(const String &p_name) const;

	float get_underline_position(int p_size) const;
	float get_underline_thickness(int p_size) const;

	void set_antialiased(bool p_antialiased);
	bool get_antialiased() const;

	void set_distance_field_hint(bool p_distance_field);
	bool get_distance_field_hint() const;

	void set_force_autohinter(bool p_enabeld);
	bool get_force_autohinter() const;

	void set_hinting(TextServer::Hinting p_hinting);
	TextServer::Hinting get_hinting() const;

	bool has_char(char32_t p_char) const;
	String get_supported_chars() const;

	Vector2 get_glyph_advance(uint32_t p_index, int p_size) const;
	Vector2 get_glyph_kerning(uint32_t p_index_a, uint32_t p_index_b, int p_size) const;

	bool has_outline() const;
	float get_base_size() const;

	bool is_language_supported(const String &p_language) const;
	void set_language_support_override(const String &p_language, bool p_supported);
	bool get_language_support_override(const String &p_language) const;
	void remove_language_support_override(const String &p_language);
	Vector<String> get_language_support_overrides() const;

	bool is_script_supported(const String &p_script) const;
	void set_script_support_override(const String &p_script, bool p_supported);
	bool get_script_support_override(const String &p_script) const;
	void remove_script_support_override(const String &p_script);
	Vector<String> get_script_support_overrides() const;

	uint32_t get_glyph_index(char32_t p_char, char32_t p_variation_selector = 0x0000) const;

	Vector2 draw_glyph(RID p_canvas, int p_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const;
	Vector2 draw_glyph_outline(RID p_canvas, int p_size, int p_outline_size, const Vector2 &p_pos, uint32_t p_index, const Color &p_color = Color(1, 1, 1)) const;

	FontData();
	FontData(const String &p_filename, int p_base_size);
	FontData(const PackedByteArray &p_data, const String &p_type, int p_base_size);

	~FontData();
};

/*************************************************************************/

class TextLine;
class TextParagraph;

class Font : public Resource {
	GDCLASS(Font, Resource);

public:
	enum SpacingType {
		SPACING_TOP,
		SPACING_BOTTOM
	};

private:
	int spacing_top = 0;
	int spacing_bottom = 0;

	mutable LRUCache<uint64_t, Ref<TextLine>> cache;
	mutable LRUCache<uint64_t, Ref<TextParagraph>> cache_wrap;

	Vector<Ref<FontData>> data;

protected:
	static void _bind_methods();

	bool _set(const StringName &p_name, const Variant &p_value);
	bool _get(const StringName &p_name, Variant &r_ret) const;
	void _get_property_list(List<PropertyInfo> *p_list) const;

	void _data_changed();

public:
	Dictionary get_feature_list() const;

	// Font data control.
	void add_data(const Ref<FontData> &p_data);
	void set_data(int p_idx, const Ref<FontData> &p_data);
	int get_data_count() const;
	Ref<FontData> get_data(int p_idx) const;
	void remove_data(int p_idx);

	float get_height(int p_size = -1) const;
	float get_ascent(int p_size = -1) const;
	float get_descent(int p_size = -1) const;

	float get_underline_position(int p_size = -1) const;
	float get_underline_thickness(int p_size = -1) const;

	int get_spacing(int p_type) const;
	void set_spacing(int p_type, int p_value);

	// Drawing string.
	Size2 get_string_size(const String &p_text, int p_size = -1) const;
	Size2 get_multiline_string_size(const String &p_text, float p_width = -1, int p_size = -1, uint8_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND) const;

	void draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, float p_width = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint8_t p_flags = TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const;
	void draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HAlign p_align = HALIGN_LEFT, float p_width = -1, int p_max_lines = -1, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0), uint8_t p_flags = TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND | TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND) const;

	// Helper functions.
	bool has_char(char32_t p_char) const;
	String get_supported_chars() const;

	Size2 get_char_size(char32_t p_char, char32_t p_next = 0, int p_size = -1) const;
	float draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, char32_t p_next = 0, int p_size = -1, const Color &p_modulate = Color(1, 1, 1), int p_outline_size = 0, const Color &p_outline_modulate = Color(1, 1, 1, 0)) const;

	Vector<RID> get_rids() const;

	void update_changes();

	Font();
	~Font();
};

VARIANT_ENUM_CAST(Font::SpacingType);

/*************************************************************************/

class ResourceFormatLoaderFont : public ResourceFormatLoader {
public:
	virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, bool p_no_cache = false);
	virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
	virtual void get_recognized_extensions(List<String> *p_extensions) const;
	virtual bool handles_type(const String &p_type) const;
	virtual String get_resource_type(const String &p_path) const;
};

#ifndef DISABLE_DEPRECATED

class ResourceFormatLoaderCompatFont : public ResourceFormatLoader {
public:
	virtual RES load(const String &p_path, const String &p_original_path = "", Error *r_error = nullptr, bool p_use_sub_threads = false, float *r_progress = nullptr, bool p_no_cache = false);
	virtual void get_recognized_extensions_for_type(const String &p_type, List<String> *p_extensions) const;
	virtual void get_recognized_extensions(List<String> *p_extensions) const;
	virtual bool handles_type(const String &p_type) const;
	virtual String get_resource_type(const String &p_path) const;
};

#endif /* DISABLE_DEPRECATED */

#endif /* FONT_H */