Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/scene/resources/font.cpp
9896 views
1
/**************************************************************************/
2
/* font.cpp */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#include "font.h"
32
#include "font.compat.inc"
33
34
#include "core/io/image_loader.h"
35
#include "core/templates/hash_map.h"
36
#include "core/templates/hashfuncs.h"
37
#include "scene/resources/image_texture.h"
38
#include "scene/resources/text_line.h"
39
#include "scene/resources/text_paragraph.h"
40
#include "scene/resources/theme.h"
41
#include "scene/theme/theme_db.h"
42
43
/*************************************************************************/
44
/* Font */
45
/*************************************************************************/
46
47
void Font::_bind_methods() {
48
ClassDB::bind_method(D_METHOD("set_fallbacks", "fallbacks"), &Font::set_fallbacks);
49
ClassDB::bind_method(D_METHOD("get_fallbacks"), &Font::get_fallbacks);
50
51
// Output.
52
ClassDB::bind_method(D_METHOD("find_variation", "variation_coordinates", "face_index", "strength", "transform", "spacing_top", "spacing_bottom", "spacing_space", "spacing_glyph", "baseline_offset"), &Font::find_variation, DEFVAL(0), DEFVAL(0.0), DEFVAL(Transform2D()), DEFVAL(0), DEFVAL(0), DEFVAL(0), DEFVAL(0), DEFVAL(0.0));
53
ClassDB::bind_method(D_METHOD("get_rids"), &Font::get_rids);
54
55
// Font metrics.
56
ClassDB::bind_method(D_METHOD("get_height", "font_size"), &Font::get_height, DEFVAL(DEFAULT_FONT_SIZE));
57
ClassDB::bind_method(D_METHOD("get_ascent", "font_size"), &Font::get_ascent, DEFVAL(DEFAULT_FONT_SIZE));
58
ClassDB::bind_method(D_METHOD("get_descent", "font_size"), &Font::get_descent, DEFVAL(DEFAULT_FONT_SIZE));
59
ClassDB::bind_method(D_METHOD("get_underline_position", "font_size"), &Font::get_underline_position, DEFVAL(DEFAULT_FONT_SIZE));
60
ClassDB::bind_method(D_METHOD("get_underline_thickness", "font_size"), &Font::get_underline_thickness, DEFVAL(DEFAULT_FONT_SIZE));
61
62
ClassDB::bind_method(D_METHOD("get_font_name"), &Font::get_font_name);
63
ClassDB::bind_method(D_METHOD("get_font_style_name"), &Font::get_font_style_name);
64
ClassDB::bind_method(D_METHOD("get_ot_name_strings"), &Font::get_ot_name_strings);
65
ClassDB::bind_method(D_METHOD("get_font_style"), &Font::get_font_style);
66
ClassDB::bind_method(D_METHOD("get_font_weight"), &Font::get_font_weight);
67
ClassDB::bind_method(D_METHOD("get_font_stretch"), &Font::get_font_stretch);
68
69
ClassDB::bind_method(D_METHOD("get_spacing", "spacing"), &Font::get_spacing);
70
ClassDB::bind_method(D_METHOD("get_opentype_features"), &Font::get_opentype_features);
71
72
// Drawing string.
73
ClassDB::bind_method(D_METHOD("set_cache_capacity", "single_line", "multi_line"), &Font::set_cache_capacity);
74
75
ClassDB::bind_method(D_METHOD("get_string_size", "text", "alignment", "width", "font_size", "justification_flags", "direction", "orientation"), &Font::get_string_size, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
76
ClassDB::bind_method(D_METHOD("get_multiline_string_size", "text", "alignment", "width", "font_size", "max_lines", "brk_flags", "justification_flags", "direction", "orientation"), &Font::get_multiline_string_size, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL));
77
78
ClassDB::bind_method(D_METHOD("draw_string", "canvas_item", "pos", "text", "alignment", "width", "font_size", "modulate", "justification_flags", "direction", "orientation", "oversampling"), &Font::draw_string, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL), DEFVAL(0.0));
79
ClassDB::bind_method(D_METHOD("draw_multiline_string", "canvas_item", "pos", "text", "alignment", "width", "font_size", "max_lines", "modulate", "brk_flags", "justification_flags", "direction", "orientation", "oversampling"), &Font::draw_multiline_string, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL), DEFVAL(0.0));
80
81
ClassDB::bind_method(D_METHOD("draw_string_outline", "canvas_item", "pos", "text", "alignment", "width", "font_size", "size", "modulate", "justification_flags", "direction", "orientation", "oversampling"), &Font::draw_string_outline, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL), DEFVAL(0.0));
82
ClassDB::bind_method(D_METHOD("draw_multiline_string_outline", "canvas_item", "pos", "text", "alignment", "width", "font_size", "max_lines", "size", "modulate", "brk_flags", "justification_flags", "direction", "orientation", "oversampling"), &Font::draw_multiline_string_outline, DEFVAL(HORIZONTAL_ALIGNMENT_LEFT), DEFVAL(-1), DEFVAL(DEFAULT_FONT_SIZE), DEFVAL(-1), DEFVAL(1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(TextServer::BREAK_MANDATORY | TextServer::BREAK_WORD_BOUND), DEFVAL(TextServer::JUSTIFICATION_KASHIDA | TextServer::JUSTIFICATION_WORD_BOUND), DEFVAL(TextServer::DIRECTION_AUTO), DEFVAL(TextServer::ORIENTATION_HORIZONTAL), DEFVAL(0.0));
83
84
// Drawing char.
85
ClassDB::bind_method(D_METHOD("get_char_size", "char", "font_size"), &Font::get_char_size);
86
ClassDB::bind_method(D_METHOD("draw_char", "canvas_item", "pos", "char", "font_size", "modulate", "oversampling"), &Font::draw_char, DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(0.0));
87
ClassDB::bind_method(D_METHOD("draw_char_outline", "canvas_item", "pos", "char", "font_size", "size", "modulate", "oversampling"), &Font::draw_char_outline, DEFVAL(-1), DEFVAL(Color(1.0, 1.0, 1.0)), DEFVAL(0.0));
88
89
// Helper functions.
90
ClassDB::bind_method(D_METHOD("has_char", "char"), &Font::has_char);
91
ClassDB::bind_method(D_METHOD("get_supported_chars"), &Font::get_supported_chars);
92
93
ClassDB::bind_method(D_METHOD("is_language_supported", "language"), &Font::is_language_supported);
94
ClassDB::bind_method(D_METHOD("is_script_supported", "script"), &Font::is_script_supported);
95
96
ClassDB::bind_method(D_METHOD("get_supported_feature_list"), &Font::get_supported_feature_list);
97
ClassDB::bind_method(D_METHOD("get_supported_variation_list"), &Font::get_supported_variation_list);
98
ClassDB::bind_method(D_METHOD("get_face_count"), &Font::get_face_count);
99
100
ADD_PROPERTY(PropertyInfo(Variant::ARRAY, "fallbacks", PROPERTY_HINT_ARRAY_TYPE, MAKE_RESOURCE_TYPE_HINT("Font")), "set_fallbacks", "get_fallbacks");
101
}
102
103
void Font::_update_rids_fb(const Font *p_f, int p_depth) const {
104
ERR_FAIL_COND(p_depth > MAX_FALLBACK_DEPTH);
105
if (p_f != nullptr) {
106
RID rid = p_f->_get_rid();
107
if (rid.is_valid()) {
108
rids.push_back(rid);
109
}
110
const TypedArray<Font> &_fallbacks = p_f->get_fallbacks();
111
for (int i = 0; i < _fallbacks.size(); i++) {
112
Ref<Font> fb_font = _fallbacks[i];
113
_update_rids_fb(fb_font.ptr(), p_depth + 1);
114
}
115
}
116
}
117
118
void Font::_update_rids() const {
119
rids.clear();
120
_update_rids_fb(this, 0);
121
dirty_rids = false;
122
}
123
124
void Font::_invalidate_rids() {
125
rids.clear();
126
dirty_rids = true;
127
128
cache.clear();
129
cache_wrap.clear();
130
131
emit_changed();
132
}
133
134
bool Font::_is_cyclic(const Ref<Font> &p_f, int p_depth) const {
135
ERR_FAIL_COND_V(p_depth > MAX_FALLBACK_DEPTH, true);
136
if (p_f.is_null()) {
137
return false;
138
}
139
if (p_f == this) {
140
return true;
141
}
142
for (int i = 0; i < p_f->fallbacks.size(); i++) {
143
const Ref<Font> &f = p_f->fallbacks[i];
144
if (_is_cyclic(f, p_depth + 1)) {
145
return true;
146
}
147
}
148
return false;
149
}
150
151
bool Font::_is_base_cyclic(const Ref<Font> &p_f, int p_depth) const {
152
ERR_FAIL_COND_V(p_depth > MAX_FALLBACK_DEPTH, true);
153
if (p_f.is_null()) {
154
return false;
155
}
156
if (p_f == this) {
157
return true;
158
}
159
Ref<FontVariation> fv = p_f;
160
if (fv.is_valid()) {
161
return _is_base_cyclic(fv->get_base_font(), p_depth + 1);
162
}
163
Ref<SystemFont> fs = p_f;
164
if (fs.is_valid()) {
165
return _is_base_cyclic(fs->get_base_font(), p_depth + 1);
166
}
167
return false;
168
}
169
170
void Font::reset_state() {
171
_invalidate_rids();
172
}
173
174
// Fallbacks.
175
void Font::set_fallbacks(const TypedArray<Font> &p_fallbacks) {
176
for (int i = 0; i < p_fallbacks.size(); i++) {
177
const Ref<Font> &f = p_fallbacks[i];
178
ERR_FAIL_COND_MSG(_is_cyclic(f, 0), "Cyclic font fallback.");
179
}
180
for (int i = 0; i < fallbacks.size(); i++) {
181
Ref<Font> f = fallbacks[i];
182
if (f.is_valid()) {
183
f->disconnect_changed(callable_mp(this, &Font::_invalidate_rids));
184
}
185
}
186
fallbacks = p_fallbacks;
187
for (int i = 0; i < fallbacks.size(); i++) {
188
Ref<Font> f = fallbacks[i];
189
if (f.is_valid()) {
190
f->connect_changed(callable_mp(this, &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
191
}
192
}
193
_invalidate_rids();
194
}
195
196
TypedArray<Font> Font::get_fallbacks() const {
197
return fallbacks;
198
}
199
200
// Output.
201
TypedArray<RID> Font::get_rids() const {
202
if (dirty_rids) {
203
_update_rids();
204
}
205
return rids;
206
}
207
208
// Drawing string.
209
real_t Font::get_height(int p_font_size) const {
210
if (dirty_rids) {
211
_update_rids();
212
}
213
214
real_t ret = 0.f;
215
for (int i = 0; i < rids.size(); i++) {
216
ret = MAX(ret, TS->font_get_ascent(rids.get(i), p_font_size) + TS->font_get_descent(rids.get(i), p_font_size));
217
}
218
return ret + get_spacing(TextServer::SPACING_BOTTOM) + get_spacing(TextServer::SPACING_TOP);
219
}
220
221
real_t Font::get_ascent(int p_font_size) const {
222
if (dirty_rids) {
223
_update_rids();
224
}
225
real_t ret = 0.f;
226
for (int i = 0; i < rids.size(); i++) {
227
ret = MAX(ret, TS->font_get_ascent(rids.get(i), p_font_size));
228
}
229
return ret + get_spacing(TextServer::SPACING_TOP);
230
}
231
232
real_t Font::get_descent(int p_font_size) const {
233
if (dirty_rids) {
234
_update_rids();
235
}
236
real_t ret = 0.f;
237
for (int i = 0; i < rids.size(); i++) {
238
ret = MAX(ret, TS->font_get_descent(rids.get(i), p_font_size));
239
}
240
return ret + get_spacing(TextServer::SPACING_BOTTOM);
241
}
242
243
real_t Font::get_underline_position(int p_font_size) const {
244
if (dirty_rids) {
245
_update_rids();
246
}
247
real_t ret = 0.f;
248
for (int i = 0; i < rids.size(); i++) {
249
ret = MAX(ret, TS->font_get_underline_position(rids.get(i), p_font_size));
250
}
251
return ret + get_spacing(TextServer::SPACING_TOP);
252
}
253
254
real_t Font::get_underline_thickness(int p_font_size) const {
255
if (dirty_rids) {
256
_update_rids();
257
}
258
real_t ret = 0.f;
259
for (int i = 0; i < rids.size(); i++) {
260
ret = MAX(ret, TS->font_get_underline_thickness(rids.get(i), p_font_size));
261
}
262
return ret;
263
}
264
265
String Font::get_font_name() const {
266
return TS->font_get_name(_get_rid());
267
}
268
269
Dictionary Font::get_ot_name_strings() const {
270
return TS->font_get_ot_name_strings(_get_rid());
271
}
272
273
String Font::get_font_style_name() const {
274
return TS->font_get_style_name(_get_rid());
275
}
276
277
BitField<TextServer::FontStyle> Font::get_font_style() const {
278
return TS->font_get_style(_get_rid());
279
}
280
281
int Font::get_font_weight() const {
282
return TS->font_get_weight(_get_rid());
283
}
284
285
int Font::get_font_stretch() const {
286
return TS->font_get_stretch(_get_rid());
287
}
288
289
Dictionary Font::get_opentype_features() const {
290
return Dictionary();
291
}
292
293
// Drawing string.
294
void Font::set_cache_capacity(int p_single_line, int p_multi_line) {
295
cache.set_capacity(p_single_line);
296
cache_wrap.set_capacity(p_multi_line);
297
}
298
299
Size2 Font::get_string_size(const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
300
bool fill = (p_alignment == HORIZONTAL_ALIGNMENT_FILL);
301
ShapedTextKey key = ShapedTextKey(p_text, p_font_size, fill ? p_width : 0.0, fill ? p_jst_flags : BitField(TextServer::JUSTIFICATION_NONE), TextServer::BREAK_NONE, p_direction, p_orientation);
302
303
Ref<TextLine> buffer;
304
if (cache.has(key)) {
305
buffer = cache.get(key);
306
} else {
307
buffer.instantiate();
308
buffer->set_direction(p_direction);
309
buffer->set_orientation(p_orientation);
310
buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
311
buffer->add_string(p_text, Ref<Font>(this), p_font_size);
312
cache.insert(key, buffer);
313
}
314
315
buffer->set_width(p_width);
316
buffer->set_horizontal_alignment(p_alignment);
317
if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
318
buffer->set_flags(p_jst_flags);
319
}
320
321
return buffer->get_size();
322
}
323
324
Size2 Font::get_multiline_string_size(const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation) const {
325
ShapedTextKey key = ShapedTextKey(p_text, p_font_size, p_width, p_jst_flags, p_brk_flags, p_direction, p_orientation);
326
327
Ref<TextParagraph> lines_buffer;
328
if (cache_wrap.has(key)) {
329
lines_buffer = cache_wrap.get(key);
330
} else {
331
lines_buffer.instantiate();
332
lines_buffer->set_direction(p_direction);
333
lines_buffer->set_orientation(p_orientation);
334
lines_buffer->add_string(p_text, Ref<Font>(this), p_font_size);
335
lines_buffer->set_width(p_width);
336
lines_buffer->set_break_flags(p_brk_flags);
337
lines_buffer->set_justification_flags(p_jst_flags);
338
lines_buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
339
cache_wrap.insert(key, lines_buffer);
340
}
341
342
lines_buffer->set_alignment(p_alignment);
343
lines_buffer->set_max_lines_visible(p_max_lines);
344
345
return lines_buffer->get_size();
346
}
347
348
void Font::draw_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, const Color &p_modulate, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation, float p_oversampling) const {
349
bool fill = (p_alignment == HORIZONTAL_ALIGNMENT_FILL);
350
ShapedTextKey key = ShapedTextKey(p_text, p_font_size, fill ? p_width : 0.0, fill ? p_jst_flags : BitField(TextServer::JUSTIFICATION_NONE), TextServer::BREAK_NONE, p_direction, p_orientation);
351
352
Ref<TextLine> buffer;
353
if (cache.has(key)) {
354
buffer = cache.get(key);
355
} else {
356
buffer.instantiate();
357
buffer->set_direction(p_direction);
358
buffer->set_orientation(p_orientation);
359
buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
360
buffer->add_string(p_text, Ref<Font>(this), p_font_size);
361
cache.insert(key, buffer);
362
}
363
364
Vector2 ofs = p_pos;
365
if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
366
ofs.y -= buffer->get_line_ascent();
367
} else {
368
ofs.x -= buffer->get_line_ascent();
369
}
370
371
buffer->set_width(p_width);
372
buffer->set_horizontal_alignment(p_alignment);
373
if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
374
buffer->set_flags(p_jst_flags);
375
}
376
377
buffer->draw(p_canvas_item, ofs, p_modulate, p_oversampling);
378
}
379
380
void Font::draw_multiline_string(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, const Color &p_modulate, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation, float p_oversampling) const {
381
ShapedTextKey key = ShapedTextKey(p_text, p_font_size, p_width, p_jst_flags, p_brk_flags, p_direction, p_orientation);
382
383
Ref<TextParagraph> lines_buffer;
384
if (cache_wrap.has(key)) {
385
lines_buffer = cache_wrap.get(key);
386
} else {
387
lines_buffer.instantiate();
388
lines_buffer->set_direction(p_direction);
389
lines_buffer->set_orientation(p_orientation);
390
lines_buffer->add_string(p_text, Ref<Font>(this), p_font_size);
391
lines_buffer->set_width(p_width);
392
lines_buffer->set_break_flags(p_brk_flags);
393
lines_buffer->set_justification_flags(p_jst_flags);
394
lines_buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
395
cache_wrap.insert(key, lines_buffer);
396
}
397
398
Vector2 ofs = p_pos;
399
if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
400
ofs.y -= lines_buffer->get_line_ascent(0);
401
} else {
402
ofs.x -= lines_buffer->get_line_ascent(0);
403
}
404
405
lines_buffer->set_alignment(p_alignment);
406
lines_buffer->set_max_lines_visible(p_max_lines);
407
408
lines_buffer->draw(p_canvas_item, ofs, p_modulate, p_modulate, p_oversampling);
409
}
410
411
void Font::draw_string_outline(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_size, const Color &p_modulate, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation, float p_oversampling) const {
412
bool fill = (p_alignment == HORIZONTAL_ALIGNMENT_FILL);
413
ShapedTextKey key = ShapedTextKey(p_text, p_font_size, fill ? p_width : 0.0, fill ? p_jst_flags : BitField(TextServer::JUSTIFICATION_NONE), TextServer::BREAK_NONE, p_direction, p_orientation);
414
415
Ref<TextLine> buffer;
416
if (cache.has(key)) {
417
buffer = cache.get(key);
418
} else {
419
buffer.instantiate();
420
buffer->set_direction(p_direction);
421
buffer->set_orientation(p_orientation);
422
buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
423
buffer->add_string(p_text, Ref<Font>(this), p_font_size);
424
cache.insert(key, buffer);
425
}
426
427
Vector2 ofs = p_pos;
428
if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
429
ofs.y -= buffer->get_line_ascent();
430
} else {
431
ofs.x -= buffer->get_line_ascent();
432
}
433
434
buffer->set_width(p_width);
435
buffer->set_horizontal_alignment(p_alignment);
436
if (p_alignment == HORIZONTAL_ALIGNMENT_FILL) {
437
buffer->set_flags(p_jst_flags);
438
}
439
440
buffer->draw_outline(p_canvas_item, ofs, p_size, p_modulate, p_oversampling);
441
}
442
443
void Font::draw_multiline_string_outline(RID p_canvas_item, const Point2 &p_pos, const String &p_text, HorizontalAlignment p_alignment, float p_width, int p_font_size, int p_max_lines, int p_size, const Color &p_modulate, BitField<TextServer::LineBreakFlag> p_brk_flags, BitField<TextServer::JustificationFlag> p_jst_flags, TextServer::Direction p_direction, TextServer::Orientation p_orientation, float p_oversampling) const {
444
ShapedTextKey key = ShapedTextKey(p_text, p_font_size, p_width, p_jst_flags, p_brk_flags, p_direction, p_orientation);
445
446
Ref<TextParagraph> lines_buffer;
447
if (cache_wrap.has(key)) {
448
lines_buffer = cache_wrap.get(key);
449
} else {
450
lines_buffer.instantiate();
451
lines_buffer->set_direction(p_direction);
452
lines_buffer->set_orientation(p_orientation);
453
lines_buffer->add_string(p_text, Ref<Font>(this), p_font_size);
454
lines_buffer->set_width(p_width);
455
lines_buffer->set_break_flags(p_brk_flags);
456
lines_buffer->set_justification_flags(p_jst_flags);
457
lines_buffer->set_text_overrun_behavior(TextServer::OVERRUN_TRIM_CHAR);
458
cache_wrap.insert(key, lines_buffer);
459
}
460
461
Vector2 ofs = p_pos;
462
if (p_orientation == TextServer::ORIENTATION_HORIZONTAL) {
463
ofs.y -= lines_buffer->get_line_ascent(0);
464
} else {
465
ofs.x -= lines_buffer->get_line_ascent(0);
466
}
467
468
lines_buffer->set_alignment(p_alignment);
469
lines_buffer->set_max_lines_visible(p_max_lines);
470
471
lines_buffer->draw_outline(p_canvas_item, ofs, p_size, p_modulate, p_modulate, p_oversampling);
472
}
473
474
// Drawing char.
475
Size2 Font::get_char_size(char32_t p_char, int p_font_size) const {
476
if (dirty_rids) {
477
_update_rids();
478
}
479
for (int i = 0; i < rids.size(); i++) {
480
if (TS->font_has_char(rids.get(i), p_char)) {
481
int32_t glyph = TS->font_get_glyph_index(rids.get(i), p_font_size, p_char, 0);
482
return Size2(TS->font_get_glyph_advance(rids.get(i), p_font_size, glyph).x, get_height(p_font_size));
483
}
484
}
485
return Size2();
486
}
487
488
real_t Font::draw_char(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, int p_font_size, const Color &p_modulate, float p_oversampling) const {
489
if (dirty_rids) {
490
_update_rids();
491
}
492
for (int i = 0; i < rids.size(); i++) {
493
if (TS->font_has_char(rids.get(i), p_char)) {
494
int32_t glyph = TS->font_get_glyph_index(rids.get(i), p_font_size, p_char, 0);
495
TS->font_draw_glyph(rids.get(i), p_canvas_item, p_font_size, p_pos, glyph, p_modulate, p_oversampling);
496
return TS->font_get_glyph_advance(rids.get(i), p_font_size, glyph).x;
497
}
498
}
499
return 0.f;
500
}
501
502
real_t Font::draw_char_outline(RID p_canvas_item, const Point2 &p_pos, char32_t p_char, int p_font_size, int p_size, const Color &p_modulate, float p_oversampling) const {
503
if (dirty_rids) {
504
_update_rids();
505
}
506
for (int i = 0; i < rids.size(); i++) {
507
if (TS->font_has_char(rids.get(i), p_char)) {
508
int32_t glyph = TS->font_get_glyph_index(rids.get(i), p_font_size, p_char, 0);
509
TS->font_draw_glyph_outline(rids.get(i), p_canvas_item, p_font_size, p_size, p_pos, glyph, p_modulate, p_oversampling);
510
return TS->font_get_glyph_advance(rids.get(i), p_font_size, glyph).x;
511
}
512
}
513
return 0.f;
514
}
515
516
// Helper functions.
517
bool Font::has_char(char32_t p_char) const {
518
if (dirty_rids) {
519
_update_rids();
520
}
521
for (int i = 0; i < rids.size(); i++) {
522
if (TS->font_has_char(rids.get(i), p_char)) {
523
return true;
524
}
525
}
526
return false;
527
}
528
529
String Font::get_supported_chars() const {
530
if (dirty_rids) {
531
_update_rids();
532
}
533
String chars;
534
for (int i = 0; i < rids.size(); i++) {
535
String data_chars = TS->font_get_supported_chars(rids.get(i));
536
for (int j = 0; j < data_chars.length(); j++) {
537
if (chars.find_char(data_chars[j]) == -1) {
538
chars += data_chars[j];
539
}
540
}
541
}
542
return chars;
543
}
544
545
bool Font::is_language_supported(const String &p_language) const {
546
return TS->font_is_language_supported(_get_rid(), p_language);
547
}
548
549
bool Font::is_script_supported(const String &p_script) const {
550
return TS->font_is_script_supported(_get_rid(), p_script);
551
}
552
553
Dictionary Font::get_supported_feature_list() const {
554
return TS->font_supported_feature_list(_get_rid());
555
}
556
557
Dictionary Font::get_supported_variation_list() const {
558
return TS->font_supported_variation_list(_get_rid());
559
}
560
561
int64_t Font::get_face_count() const {
562
return TS->font_get_face_count(_get_rid());
563
}
564
565
Font::Font() {
566
cache.set_capacity(64);
567
cache_wrap.set_capacity(16);
568
}
569
570
Font::~Font() {
571
}
572
573
/*************************************************************************/
574
/* FontFile */
575
/*************************************************************************/
576
577
_FORCE_INLINE_ void FontFile::_clear_cache() {
578
for (int i = 0; i < cache.size(); i++) {
579
if (cache[i].is_valid()) {
580
TS->free_rid(cache[i]);
581
cache.write[i] = RID();
582
}
583
}
584
}
585
586
_FORCE_INLINE_ void FontFile::_ensure_rid(int p_cache_index, int p_make_linked_from) const {
587
if (unlikely(p_cache_index >= cache.size())) {
588
cache.resize(p_cache_index + 1);
589
}
590
if (unlikely(!cache[p_cache_index].is_valid())) {
591
if (p_make_linked_from >= 0 && p_make_linked_from != p_cache_index && p_make_linked_from < cache.size()) {
592
cache.write[p_cache_index] = TS->create_font_linked_variation(cache[p_make_linked_from]);
593
} else {
594
cache.write[p_cache_index] = TS->create_font();
595
TS->font_set_data_ptr(cache[p_cache_index], data_ptr, data_size);
596
TS->font_set_antialiasing(cache[p_cache_index], antialiasing);
597
TS->font_set_generate_mipmaps(cache[p_cache_index], mipmaps);
598
TS->font_set_disable_embedded_bitmaps(cache[p_cache_index], disable_embedded_bitmaps);
599
TS->font_set_multichannel_signed_distance_field(cache[p_cache_index], msdf);
600
TS->font_set_msdf_pixel_range(cache[p_cache_index], msdf_pixel_range);
601
TS->font_set_msdf_size(cache[p_cache_index], msdf_size);
602
TS->font_set_fixed_size(cache[p_cache_index], fixed_size);
603
TS->font_set_fixed_size_scale_mode(cache[p_cache_index], fixed_size_scale_mode);
604
TS->font_set_force_autohinter(cache[p_cache_index], force_autohinter);
605
TS->font_set_modulate_color_glyphs(cache[p_cache_index], modulate_color_glyphs);
606
TS->font_set_allow_system_fallback(cache[p_cache_index], allow_system_fallback);
607
TS->font_set_hinting(cache[p_cache_index], hinting);
608
TS->font_set_subpixel_positioning(cache[p_cache_index], subpixel_positioning);
609
TS->font_set_keep_rounding_remainders(cache[p_cache_index], keep_rounding_remainders);
610
TS->font_set_oversampling(cache[p_cache_index], oversampling_override);
611
}
612
}
613
}
614
615
void FontFile::_convert_packed_8bit(Ref<Image> &p_source, int p_page, int p_sz) {
616
int w = p_source->get_width();
617
int h = p_source->get_height();
618
619
PackedByteArray imgdata = p_source->get_data();
620
const uint8_t *r = imgdata.ptr();
621
622
PackedByteArray imgdata_r;
623
imgdata_r.resize(w * h * 2);
624
uint8_t *wr = imgdata_r.ptrw();
625
626
PackedByteArray imgdata_g;
627
imgdata_g.resize(w * h * 2);
628
uint8_t *wg = imgdata_g.ptrw();
629
630
PackedByteArray imgdata_b;
631
imgdata_b.resize(w * h * 2);
632
uint8_t *wb = imgdata_b.ptrw();
633
634
PackedByteArray imgdata_a;
635
imgdata_a.resize(w * h * 2);
636
uint8_t *wa = imgdata_a.ptrw();
637
638
for (int i = 0; i < h; i++) {
639
for (int j = 0; j < w; j++) {
640
int ofs_src = (i * w + j) * 4;
641
int ofs_dst = (i * w + j) * 2;
642
wr[ofs_dst + 0] = 255;
643
wr[ofs_dst + 1] = r[ofs_src + 0];
644
wg[ofs_dst + 0] = 255;
645
wg[ofs_dst + 1] = r[ofs_src + 1];
646
wb[ofs_dst + 0] = 255;
647
wb[ofs_dst + 1] = r[ofs_src + 2];
648
wa[ofs_dst + 0] = 255;
649
wa[ofs_dst + 1] = r[ofs_src + 3];
650
}
651
}
652
Ref<Image> img_r = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_r));
653
set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 0, img_r);
654
Ref<Image> img_g = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_g));
655
set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 1, img_g);
656
Ref<Image> img_b = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_b));
657
set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 2, img_b);
658
Ref<Image> img_a = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_a));
659
set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 3, img_a);
660
}
661
662
void FontFile::_convert_packed_4bit(Ref<Image> &p_source, int p_page, int p_sz) {
663
int w = p_source->get_width();
664
int h = p_source->get_height();
665
666
PackedByteArray imgdata = p_source->get_data();
667
const uint8_t *r = imgdata.ptr();
668
669
PackedByteArray imgdata_r;
670
imgdata_r.resize(w * h * 2);
671
uint8_t *wr = imgdata_r.ptrw();
672
673
PackedByteArray imgdata_g;
674
imgdata_g.resize(w * h * 2);
675
uint8_t *wg = imgdata_g.ptrw();
676
677
PackedByteArray imgdata_b;
678
imgdata_b.resize(w * h * 2);
679
uint8_t *wb = imgdata_b.ptrw();
680
681
PackedByteArray imgdata_a;
682
imgdata_a.resize(w * h * 2);
683
uint8_t *wa = imgdata_a.ptrw();
684
685
PackedByteArray imgdata_ro;
686
imgdata_ro.resize(w * h * 2);
687
uint8_t *wro = imgdata_ro.ptrw();
688
689
PackedByteArray imgdata_go;
690
imgdata_go.resize(w * h * 2);
691
uint8_t *wgo = imgdata_go.ptrw();
692
693
PackedByteArray imgdata_bo;
694
imgdata_bo.resize(w * h * 2);
695
uint8_t *wbo = imgdata_bo.ptrw();
696
697
PackedByteArray imgdata_ao;
698
imgdata_ao.resize(w * h * 2);
699
uint8_t *wao = imgdata_ao.ptrw();
700
701
for (int i = 0; i < h; i++) {
702
for (int j = 0; j < w; j++) {
703
int ofs_src = (i * w + j) * 4;
704
int ofs_dst = (i * w + j) * 2;
705
wr[ofs_dst + 0] = 255;
706
wro[ofs_dst + 0] = 255;
707
if (r[ofs_src + 0] > 0x0F) {
708
wr[ofs_dst + 1] = (r[ofs_src + 0] - 0x0F) * 2;
709
wro[ofs_dst + 1] = 0;
710
} else {
711
wr[ofs_dst + 1] = 0;
712
wro[ofs_dst + 1] = r[ofs_src + 0] * 2;
713
}
714
wg[ofs_dst + 0] = 255;
715
wgo[ofs_dst + 0] = 255;
716
if (r[ofs_src + 1] > 0x0F) {
717
wg[ofs_dst + 1] = (r[ofs_src + 1] - 0x0F) * 2;
718
wgo[ofs_dst + 1] = 0;
719
} else {
720
wg[ofs_dst + 1] = 0;
721
wgo[ofs_dst + 1] = r[ofs_src + 1] * 2;
722
}
723
wb[ofs_dst + 0] = 255;
724
wbo[ofs_dst + 0] = 255;
725
if (r[ofs_src + 2] > 0x0F) {
726
wb[ofs_dst + 1] = (r[ofs_src + 2] - 0x0F) * 2;
727
wbo[ofs_dst + 1] = 0;
728
} else {
729
wb[ofs_dst + 1] = 0;
730
wbo[ofs_dst + 1] = r[ofs_src + 2] * 2;
731
}
732
wa[ofs_dst + 0] = 255;
733
wao[ofs_dst + 0] = 255;
734
if (r[ofs_src + 3] > 0x0F) {
735
wa[ofs_dst + 1] = (r[ofs_src + 3] - 0x0F) * 2;
736
wao[ofs_dst + 1] = 0;
737
} else {
738
wa[ofs_dst + 1] = 0;
739
wao[ofs_dst + 1] = r[ofs_src + 3] * 2;
740
}
741
}
742
}
743
Ref<Image> img_r = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_r));
744
set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 0, img_r);
745
Ref<Image> img_g = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_g));
746
set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 1, img_g);
747
Ref<Image> img_b = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_b));
748
set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 2, img_b);
749
Ref<Image> img_a = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_a));
750
set_texture_image(0, Vector2i(p_sz, 0), p_page * 4 + 3, img_a);
751
752
Ref<Image> img_ro = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_ro));
753
set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 0, img_ro);
754
Ref<Image> img_go = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_go));
755
set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 1, img_go);
756
Ref<Image> img_bo = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_bo));
757
set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 2, img_bo);
758
Ref<Image> img_ao = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_ao));
759
set_texture_image(0, Vector2i(p_sz, 1), p_page * 4 + 3, img_ao);
760
}
761
762
void FontFile::_convert_rgba_4bit(Ref<Image> &p_source, int p_page, int p_sz) {
763
int w = p_source->get_width();
764
int h = p_source->get_height();
765
766
PackedByteArray imgdata = p_source->get_data();
767
const uint8_t *r = imgdata.ptr();
768
769
PackedByteArray imgdata_g;
770
imgdata_g.resize(w * h * 4);
771
uint8_t *wg = imgdata_g.ptrw();
772
773
PackedByteArray imgdata_o;
774
imgdata_o.resize(w * h * 4);
775
uint8_t *wo = imgdata_o.ptrw();
776
777
for (int i = 0; i < h; i++) {
778
for (int j = 0; j < w; j++) {
779
int ofs = (i * w + j) * 4;
780
781
if (r[ofs + 0] > 0x7F) {
782
wg[ofs + 0] = r[ofs + 0];
783
wo[ofs + 0] = 0;
784
} else {
785
wg[ofs + 0] = 0;
786
wo[ofs + 0] = r[ofs + 0] * 2;
787
}
788
if (r[ofs + 1] > 0x7F) {
789
wg[ofs + 1] = r[ofs + 1];
790
wo[ofs + 1] = 0;
791
} else {
792
wg[ofs + 1] = 0;
793
wo[ofs + 1] = r[ofs + 1] * 2;
794
}
795
if (r[ofs + 2] > 0x7F) {
796
wg[ofs + 2] = r[ofs + 2];
797
wo[ofs + 2] = 0;
798
} else {
799
wg[ofs + 2] = 0;
800
wo[ofs + 2] = r[ofs + 2] * 2;
801
}
802
if (r[ofs + 3] > 0x7F) {
803
wg[ofs + 3] = r[ofs + 3];
804
wo[ofs + 3] = 0;
805
} else {
806
wg[ofs + 3] = 0;
807
wo[ofs + 3] = r[ofs + 3] * 2;
808
}
809
}
810
}
811
Ref<Image> img_g = memnew(Image(w, h, false, Image::FORMAT_RGBA8, imgdata_g));
812
set_texture_image(0, Vector2i(p_sz, 0), p_page, img_g);
813
814
Ref<Image> img_o = memnew(Image(w, h, false, Image::FORMAT_RGBA8, imgdata_o));
815
set_texture_image(0, Vector2i(p_sz, 1), p_page, img_o);
816
}
817
818
void FontFile::_convert_mono_8bit(Ref<Image> &p_source, int p_page, int p_ch, int p_sz, int p_ol) {
819
int w = p_source->get_width();
820
int h = p_source->get_height();
821
822
PackedByteArray imgdata = p_source->get_data();
823
const uint8_t *r = imgdata.ptr();
824
825
int size = 4;
826
if (p_source->get_format() == Image::FORMAT_L8) {
827
size = 1;
828
p_ch = 0;
829
}
830
831
PackedByteArray imgdata_g;
832
imgdata_g.resize(w * h * 2);
833
uint8_t *wg = imgdata_g.ptrw();
834
835
for (int i = 0; i < h; i++) {
836
for (int j = 0; j < w; j++) {
837
int ofs_src = (i * w + j) * size;
838
int ofs_dst = (i * w + j) * 2;
839
wg[ofs_dst + 0] = 255;
840
wg[ofs_dst + 1] = r[ofs_src + p_ch];
841
}
842
}
843
Ref<Image> img_g = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_g));
844
set_texture_image(0, Vector2i(p_sz, p_ol), p_page, img_g);
845
}
846
847
void FontFile::_convert_mono_4bit(Ref<Image> &p_source, int p_page, int p_ch, int p_sz, int p_ol) {
848
int w = p_source->get_width();
849
int h = p_source->get_height();
850
851
PackedByteArray imgdata = p_source->get_data();
852
const uint8_t *r = imgdata.ptr();
853
854
int size = 4;
855
if (p_source->get_format() == Image::FORMAT_L8) {
856
size = 1;
857
p_ch = 0;
858
}
859
860
PackedByteArray imgdata_g;
861
imgdata_g.resize(w * h * 2);
862
uint8_t *wg = imgdata_g.ptrw();
863
864
PackedByteArray imgdata_o;
865
imgdata_o.resize(w * h * 2);
866
uint8_t *wo = imgdata_o.ptrw();
867
868
for (int i = 0; i < h; i++) {
869
for (int j = 0; j < w; j++) {
870
int ofs_src = (i * w + j) * size;
871
int ofs_dst = (i * w + j) * 2;
872
wg[ofs_dst + 0] = 255;
873
wo[ofs_dst + 0] = 255;
874
if (r[ofs_src + p_ch] > 0x7F) {
875
wg[ofs_dst + 1] = r[ofs_src + p_ch];
876
wo[ofs_dst + 1] = 0;
877
} else {
878
wg[ofs_dst + 1] = 0;
879
wo[ofs_dst + 1] = r[ofs_src + p_ch] * 2;
880
}
881
}
882
}
883
Ref<Image> img_g = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_g));
884
set_texture_image(0, Vector2i(p_sz, 0), p_page, img_g);
885
886
Ref<Image> img_o = memnew(Image(w, h, false, Image::FORMAT_LA8, imgdata_o));
887
set_texture_image(0, Vector2i(p_sz, p_ol), p_page, img_o);
888
}
889
890
void FontFile::_bind_methods() {
891
ClassDB::bind_method(D_METHOD("load_bitmap_font", "path"), &FontFile::load_bitmap_font);
892
ClassDB::bind_method(D_METHOD("load_dynamic_font", "path"), &FontFile::load_dynamic_font);
893
894
ClassDB::bind_method(D_METHOD("set_data", "data"), &FontFile::set_data);
895
ClassDB::bind_method(D_METHOD("get_data"), &FontFile::get_data);
896
897
ClassDB::bind_method(D_METHOD("set_font_name", "name"), &FontFile::set_font_name);
898
ClassDB::bind_method(D_METHOD("set_font_style_name", "name"), &FontFile::set_font_style_name);
899
ClassDB::bind_method(D_METHOD("set_font_style", "style"), &FontFile::set_font_style);
900
ClassDB::bind_method(D_METHOD("set_font_weight", "weight"), &FontFile::set_font_weight);
901
ClassDB::bind_method(D_METHOD("set_font_stretch", "stretch"), &FontFile::set_font_stretch);
902
903
ClassDB::bind_method(D_METHOD("set_antialiasing", "antialiasing"), &FontFile::set_antialiasing);
904
ClassDB::bind_method(D_METHOD("get_antialiasing"), &FontFile::get_antialiasing);
905
906
ClassDB::bind_method(D_METHOD("set_disable_embedded_bitmaps", "disable_embedded_bitmaps"), &FontFile::set_disable_embedded_bitmaps);
907
ClassDB::bind_method(D_METHOD("get_disable_embedded_bitmaps"), &FontFile::get_disable_embedded_bitmaps);
908
909
ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "generate_mipmaps"), &FontFile::set_generate_mipmaps);
910
ClassDB::bind_method(D_METHOD("get_generate_mipmaps"), &FontFile::get_generate_mipmaps);
911
912
ClassDB::bind_method(D_METHOD("set_multichannel_signed_distance_field", "msdf"), &FontFile::set_multichannel_signed_distance_field);
913
ClassDB::bind_method(D_METHOD("is_multichannel_signed_distance_field"), &FontFile::is_multichannel_signed_distance_field);
914
915
ClassDB::bind_method(D_METHOD("set_msdf_pixel_range", "msdf_pixel_range"), &FontFile::set_msdf_pixel_range);
916
ClassDB::bind_method(D_METHOD("get_msdf_pixel_range"), &FontFile::get_msdf_pixel_range);
917
918
ClassDB::bind_method(D_METHOD("set_msdf_size", "msdf_size"), &FontFile::set_msdf_size);
919
ClassDB::bind_method(D_METHOD("get_msdf_size"), &FontFile::get_msdf_size);
920
921
ClassDB::bind_method(D_METHOD("set_fixed_size", "fixed_size"), &FontFile::set_fixed_size);
922
ClassDB::bind_method(D_METHOD("get_fixed_size"), &FontFile::get_fixed_size);
923
924
ClassDB::bind_method(D_METHOD("set_fixed_size_scale_mode", "fixed_size_scale_mode"), &FontFile::set_fixed_size_scale_mode);
925
ClassDB::bind_method(D_METHOD("get_fixed_size_scale_mode"), &FontFile::get_fixed_size_scale_mode);
926
927
ClassDB::bind_method(D_METHOD("set_allow_system_fallback", "allow_system_fallback"), &FontFile::set_allow_system_fallback);
928
ClassDB::bind_method(D_METHOD("is_allow_system_fallback"), &FontFile::is_allow_system_fallback);
929
930
ClassDB::bind_method(D_METHOD("set_force_autohinter", "force_autohinter"), &FontFile::set_force_autohinter);
931
ClassDB::bind_method(D_METHOD("is_force_autohinter"), &FontFile::is_force_autohinter);
932
933
ClassDB::bind_method(D_METHOD("set_modulate_color_glyphs", "modulate"), &FontFile::set_modulate_color_glyphs);
934
ClassDB::bind_method(D_METHOD("is_modulate_color_glyphs"), &FontFile::is_modulate_color_glyphs);
935
936
ClassDB::bind_method(D_METHOD("set_hinting", "hinting"), &FontFile::set_hinting);
937
ClassDB::bind_method(D_METHOD("get_hinting"), &FontFile::get_hinting);
938
939
ClassDB::bind_method(D_METHOD("set_subpixel_positioning", "subpixel_positioning"), &FontFile::set_subpixel_positioning);
940
ClassDB::bind_method(D_METHOD("get_subpixel_positioning"), &FontFile::get_subpixel_positioning);
941
942
ClassDB::bind_method(D_METHOD("set_keep_rounding_remainders", "keep_rounding_remainders"), &FontFile::set_keep_rounding_remainders);
943
ClassDB::bind_method(D_METHOD("get_keep_rounding_remainders"), &FontFile::get_keep_rounding_remainders);
944
945
ClassDB::bind_method(D_METHOD("set_oversampling", "oversampling"), &FontFile::set_oversampling);
946
ClassDB::bind_method(D_METHOD("get_oversampling"), &FontFile::get_oversampling);
947
948
ClassDB::bind_method(D_METHOD("get_cache_count"), &FontFile::get_cache_count);
949
ClassDB::bind_method(D_METHOD("clear_cache"), &FontFile::clear_cache);
950
ClassDB::bind_method(D_METHOD("remove_cache", "cache_index"), &FontFile::remove_cache);
951
952
ClassDB::bind_method(D_METHOD("get_size_cache_list", "cache_index"), &FontFile::get_size_cache_list);
953
ClassDB::bind_method(D_METHOD("clear_size_cache", "cache_index"), &FontFile::clear_size_cache);
954
ClassDB::bind_method(D_METHOD("remove_size_cache", "cache_index", "size"), &FontFile::remove_size_cache);
955
956
ClassDB::bind_method(D_METHOD("set_variation_coordinates", "cache_index", "variation_coordinates"), &FontFile::set_variation_coordinates);
957
ClassDB::bind_method(D_METHOD("get_variation_coordinates", "cache_index"), &FontFile::get_variation_coordinates);
958
959
ClassDB::bind_method(D_METHOD("set_embolden", "cache_index", "strength"), &FontFile::set_embolden);
960
ClassDB::bind_method(D_METHOD("get_embolden", "cache_index"), &FontFile::get_embolden);
961
962
ClassDB::bind_method(D_METHOD("set_transform", "cache_index", "transform"), &FontFile::set_transform);
963
ClassDB::bind_method(D_METHOD("get_transform", "cache_index"), &FontFile::get_transform);
964
965
ClassDB::bind_method(D_METHOD("set_extra_spacing", "cache_index", "spacing", "value"), &FontFile::set_extra_spacing);
966
ClassDB::bind_method(D_METHOD("get_extra_spacing", "cache_index", "spacing"), &FontFile::get_extra_spacing);
967
968
ClassDB::bind_method(D_METHOD("set_extra_baseline_offset", "cache_index", "baseline_offset"), &FontFile::set_extra_baseline_offset);
969
ClassDB::bind_method(D_METHOD("get_extra_baseline_offset", "cache_index"), &FontFile::get_extra_baseline_offset);
970
971
ClassDB::bind_method(D_METHOD("set_face_index", "cache_index", "face_index"), &FontFile::set_face_index);
972
ClassDB::bind_method(D_METHOD("get_face_index", "cache_index"), &FontFile::get_face_index);
973
974
ClassDB::bind_method(D_METHOD("set_cache_ascent", "cache_index", "size", "ascent"), &FontFile::set_cache_ascent);
975
ClassDB::bind_method(D_METHOD("get_cache_ascent", "cache_index", "size"), &FontFile::get_cache_ascent);
976
977
ClassDB::bind_method(D_METHOD("set_cache_descent", "cache_index", "size", "descent"), &FontFile::set_cache_descent);
978
ClassDB::bind_method(D_METHOD("get_cache_descent", "cache_index", "size"), &FontFile::get_cache_descent);
979
980
ClassDB::bind_method(D_METHOD("set_cache_underline_position", "cache_index", "size", "underline_position"), &FontFile::set_cache_underline_position);
981
ClassDB::bind_method(D_METHOD("get_cache_underline_position", "cache_index", "size"), &FontFile::get_cache_underline_position);
982
983
ClassDB::bind_method(D_METHOD("set_cache_underline_thickness", "cache_index", "size", "underline_thickness"), &FontFile::set_cache_underline_thickness);
984
ClassDB::bind_method(D_METHOD("get_cache_underline_thickness", "cache_index", "size"), &FontFile::get_cache_underline_thickness);
985
986
ClassDB::bind_method(D_METHOD("set_cache_scale", "cache_index", "size", "scale"), &FontFile::set_cache_scale);
987
ClassDB::bind_method(D_METHOD("get_cache_scale", "cache_index", "size"), &FontFile::get_cache_scale);
988
989
ClassDB::bind_method(D_METHOD("get_texture_count", "cache_index", "size"), &FontFile::get_texture_count);
990
ClassDB::bind_method(D_METHOD("clear_textures", "cache_index", "size"), &FontFile::clear_textures);
991
ClassDB::bind_method(D_METHOD("remove_texture", "cache_index", "size", "texture_index"), &FontFile::remove_texture);
992
993
ClassDB::bind_method(D_METHOD("set_texture_image", "cache_index", "size", "texture_index", "image"), &FontFile::set_texture_image);
994
ClassDB::bind_method(D_METHOD("get_texture_image", "cache_index", "size", "texture_index"), &FontFile::get_texture_image);
995
996
ClassDB::bind_method(D_METHOD("set_texture_offsets", "cache_index", "size", "texture_index", "offset"), &FontFile::set_texture_offsets);
997
ClassDB::bind_method(D_METHOD("get_texture_offsets", "cache_index", "size", "texture_index"), &FontFile::get_texture_offsets);
998
999
ClassDB::bind_method(D_METHOD("get_glyph_list", "cache_index", "size"), &FontFile::get_glyph_list);
1000
ClassDB::bind_method(D_METHOD("clear_glyphs", "cache_index", "size"), &FontFile::clear_glyphs);
1001
ClassDB::bind_method(D_METHOD("remove_glyph", "cache_index", "size", "glyph"), &FontFile::remove_glyph);
1002
1003
ClassDB::bind_method(D_METHOD("set_glyph_advance", "cache_index", "size", "glyph", "advance"), &FontFile::set_glyph_advance);
1004
ClassDB::bind_method(D_METHOD("get_glyph_advance", "cache_index", "size", "glyph"), &FontFile::get_glyph_advance);
1005
1006
ClassDB::bind_method(D_METHOD("set_glyph_offset", "cache_index", "size", "glyph", "offset"), &FontFile::set_glyph_offset);
1007
ClassDB::bind_method(D_METHOD("get_glyph_offset", "cache_index", "size", "glyph"), &FontFile::get_glyph_offset);
1008
1009
ClassDB::bind_method(D_METHOD("set_glyph_size", "cache_index", "size", "glyph", "gl_size"), &FontFile::set_glyph_size);
1010
ClassDB::bind_method(D_METHOD("get_glyph_size", "cache_index", "size", "glyph"), &FontFile::get_glyph_size);
1011
1012
ClassDB::bind_method(D_METHOD("set_glyph_uv_rect", "cache_index", "size", "glyph", "uv_rect"), &FontFile::set_glyph_uv_rect);
1013
ClassDB::bind_method(D_METHOD("get_glyph_uv_rect", "cache_index", "size", "glyph"), &FontFile::get_glyph_uv_rect);
1014
1015
ClassDB::bind_method(D_METHOD("set_glyph_texture_idx", "cache_index", "size", "glyph", "texture_idx"), &FontFile::set_glyph_texture_idx);
1016
ClassDB::bind_method(D_METHOD("get_glyph_texture_idx", "cache_index", "size", "glyph"), &FontFile::get_glyph_texture_idx);
1017
1018
ClassDB::bind_method(D_METHOD("get_kerning_list", "cache_index", "size"), &FontFile::get_kerning_list);
1019
ClassDB::bind_method(D_METHOD("clear_kerning_map", "cache_index", "size"), &FontFile::clear_kerning_map);
1020
ClassDB::bind_method(D_METHOD("remove_kerning", "cache_index", "size", "glyph_pair"), &FontFile::remove_kerning);
1021
1022
ClassDB::bind_method(D_METHOD("set_kerning", "cache_index", "size", "glyph_pair", "kerning"), &FontFile::set_kerning);
1023
ClassDB::bind_method(D_METHOD("get_kerning", "cache_index", "size", "glyph_pair"), &FontFile::get_kerning);
1024
1025
ClassDB::bind_method(D_METHOD("render_range", "cache_index", "size", "start", "end"), &FontFile::render_range);
1026
ClassDB::bind_method(D_METHOD("render_glyph", "cache_index", "size", "index"), &FontFile::render_glyph);
1027
1028
ClassDB::bind_method(D_METHOD("set_language_support_override", "language", "supported"), &FontFile::set_language_support_override);
1029
ClassDB::bind_method(D_METHOD("get_language_support_override", "language"), &FontFile::get_language_support_override);
1030
ClassDB::bind_method(D_METHOD("remove_language_support_override", "language"), &FontFile::remove_language_support_override);
1031
ClassDB::bind_method(D_METHOD("get_language_support_overrides"), &FontFile::get_language_support_overrides);
1032
1033
ClassDB::bind_method(D_METHOD("set_script_support_override", "script", "supported"), &FontFile::set_script_support_override);
1034
ClassDB::bind_method(D_METHOD("get_script_support_override", "script"), &FontFile::get_script_support_override);
1035
ClassDB::bind_method(D_METHOD("remove_script_support_override", "script"), &FontFile::remove_script_support_override);
1036
ClassDB::bind_method(D_METHOD("get_script_support_overrides"), &FontFile::get_script_support_overrides);
1037
1038
ClassDB::bind_method(D_METHOD("set_opentype_feature_overrides", "overrides"), &FontFile::set_opentype_feature_overrides);
1039
ClassDB::bind_method(D_METHOD("get_opentype_feature_overrides"), &FontFile::get_opentype_feature_overrides);
1040
1041
ClassDB::bind_method(D_METHOD("get_glyph_index", "size", "char", "variation_selector"), &FontFile::get_glyph_index);
1042
ClassDB::bind_method(D_METHOD("get_char_from_glyph_index", "size", "glyph_index"), &FontFile::get_char_from_glyph_index);
1043
1044
ADD_PROPERTY(PropertyInfo(Variant::PACKED_BYTE_ARRAY, "data", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_data", "get_data");
1045
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_generate_mipmaps", "get_generate_mipmaps");
1046
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_embedded_bitmaps", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_disable_embedded_bitmaps", "get_disable_embedded_bitmaps");
1047
ADD_PROPERTY(PropertyInfo(Variant::INT, "antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD Subpixel", PROPERTY_USAGE_STORAGE), "set_antialiasing", "get_antialiasing");
1048
ADD_PROPERTY(PropertyInfo(Variant::STRING, "font_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_font_name", "get_font_name");
1049
ADD_PROPERTY(PropertyInfo(Variant::STRING, "style_name", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_font_style_name", "get_font_style_name");
1050
ADD_PROPERTY(PropertyInfo(Variant::INT, "font_style", PROPERTY_HINT_FLAGS, "Bold,Italic,Fixed Size", PROPERTY_USAGE_STORAGE), "set_font_style", "get_font_style");
1051
ADD_PROPERTY(PropertyInfo(Variant::INT, "font_weight", PROPERTY_HINT_RANGE, "100,999,25", PROPERTY_USAGE_STORAGE), "set_font_weight", "get_font_weight");
1052
ADD_PROPERTY(PropertyInfo(Variant::INT, "font_stretch", PROPERTY_HINT_RANGE, "50,200,25", PROPERTY_USAGE_STORAGE), "set_font_stretch", "get_font_stretch");
1053
1054
ADD_PROPERTY(PropertyInfo(Variant::INT, "subpixel_positioning", PROPERTY_HINT_ENUM, "Disabled,Auto,One Half of a Pixel,One Quarter of a Pixel", PROPERTY_USAGE_STORAGE), "set_subpixel_positioning", "get_subpixel_positioning");
1055
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_rounding_remainders", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_keep_rounding_remainders", "get_keep_rounding_remainders");
1056
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_multichannel_signed_distance_field", "is_multichannel_signed_distance_field");
1057
ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_pixel_range", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_msdf_pixel_range", "get_msdf_pixel_range");
1058
ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_msdf_size", "get_msdf_size");
1059
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_system_fallback", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_allow_system_fallback", "is_allow_system_fallback");
1060
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_autohinter", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_force_autohinter", "is_force_autohinter");
1061
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "modulate_color_glyphs", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_modulate_color_glyphs", "is_modulate_color_glyphs");
1062
ADD_PROPERTY(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal", PROPERTY_USAGE_STORAGE), "set_hinting", "get_hinting");
1063
ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_fixed_size", "get_fixed_size");
1064
ADD_PROPERTY(PropertyInfo(Variant::INT, "fixed_size_scale_mode", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_fixed_size_scale_mode", "get_fixed_size_scale_mode");
1065
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "opentype_feature_overrides", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_opentype_feature_overrides", "get_opentype_feature_overrides");
1066
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "oversampling", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE), "set_oversampling", "get_oversampling");
1067
}
1068
1069
void FontFile::_validate_property(PropertyInfo &p_property) const {
1070
if (!Engine::get_singleton()->is_editor_hint()) {
1071
return;
1072
}
1073
if (p_property.name == "fallbacks") {
1074
p_property.usage &= ~PROPERTY_USAGE_EDITOR;
1075
}
1076
}
1077
1078
bool FontFile::_set(const StringName &p_name, const Variant &p_value) {
1079
Vector<String> tokens = p_name.operator String().split("/");
1080
1081
#ifndef DISABLE_DEPRECATED
1082
if (tokens.size() == 1 && tokens[0] == "font_path") {
1083
// Compatibility, DynamicFontData.
1084
load_dynamic_font(p_value);
1085
}
1086
if (tokens.size() == 1 && tokens[0] == "font_data") {
1087
// Compatibility, DynamicFont.
1088
Ref<Font> f = p_value;
1089
if (f.is_valid()) {
1090
fallbacks.push_back(f);
1091
return true;
1092
}
1093
return false;
1094
} else if (tokens.size() == 2 && tokens[0] == "fallback") {
1095
// Compatibility, DynamicFont.
1096
Ref<FontFile> f = p_value;
1097
if (f.is_valid()) {
1098
fallbacks.push_back(f);
1099
return true;
1100
}
1101
return false;
1102
} else if (tokens.size() == 1 && tokens[0] == "textures") {
1103
// Compatibility, BitmapFont.
1104
set_fixed_size(16);
1105
Array textures = p_value;
1106
for (int i = 0; i < textures.size(); i++) {
1107
Ref<ImageTexture> tex = textures[i];
1108
ERR_CONTINUE(tex.is_null());
1109
set_texture_image(0, Vector2i(16, 0), i, tex->get_image());
1110
}
1111
} else if (tokens.size() == 1 && tokens[0] == "chars") {
1112
// Compatibility, BitmapFont.
1113
set_fixed_size(16);
1114
PackedInt32Array arr = p_value;
1115
int len = arr.size();
1116
ERR_FAIL_COND_V(len % 9, false);
1117
if (!len) {
1118
return false;
1119
}
1120
int chars = len / 9;
1121
for (int i = 0; i < chars; i++) {
1122
const int32_t *char_data = &arr[i * 9];
1123
char32_t c = char_data[0];
1124
set_glyph_texture_idx(0, Vector2i(16, 0), c, char_data[1]);
1125
set_glyph_uv_rect(0, Vector2i(16, 0), c, Rect2(char_data[2], char_data[3], char_data[4], char_data[5]));
1126
set_glyph_offset(0, Vector2i(16, 0), c, Size2(char_data[6], char_data[7]));
1127
set_glyph_advance(0, 16, c, Vector2(char_data[8], 0));
1128
}
1129
} else if (tokens.size() == 1 && tokens[0] == "kernings") {
1130
// Compatibility, BitmapFont.
1131
set_fixed_size(16);
1132
PackedInt32Array arr = p_value;
1133
int len = arr.size();
1134
ERR_FAIL_COND_V(len % 3, false);
1135
if (!len) {
1136
return false;
1137
}
1138
for (int i = 0; i < len / 3; i++) {
1139
const int32_t *kern_data = &arr[i * 3];
1140
set_kerning(0, 16, Vector2i(kern_data[0], kern_data[1]), Vector2(kern_data[2], 0));
1141
}
1142
} else if (tokens.size() == 1 && tokens[0] == "height") {
1143
// Compatibility, BitmapFont.
1144
bmp_height = p_value;
1145
set_fixed_size(16);
1146
set_cache_descent(0, 16, bmp_height - bmp_ascent);
1147
} else if (tokens.size() == 1 && tokens[0] == "ascent") {
1148
// Compatibility, BitmapFont.
1149
bmp_ascent = p_value;
1150
set_fixed_size(16);
1151
set_cache_ascent(0, 16, bmp_ascent);
1152
set_cache_descent(0, 16, bmp_height - bmp_ascent);
1153
} else if (tokens.size() == 1 && tokens[0] == "fallback") {
1154
// Compatibility, BitmapFont.
1155
Ref<Font> f = p_value;
1156
if (f.is_valid()) {
1157
fallbacks.push_back(f);
1158
return true;
1159
}
1160
return false;
1161
}
1162
#endif // DISABLE_DEPRECATED
1163
1164
if (tokens.size() == 2 && tokens[0] == "language_support_override") {
1165
const String &lang_code = tokens[1];
1166
set_language_support_override(lang_code, p_value);
1167
return true;
1168
} else if (tokens.size() == 2 && tokens[0] == "script_support_override") {
1169
const String &script_code = tokens[1];
1170
set_script_support_override(script_code, p_value);
1171
return true;
1172
} else if (tokens.size() >= 3 && tokens[0] == "cache") {
1173
int cache_index = tokens[1].to_int();
1174
if (tokens.size() == 3 && tokens[2] == "variation_coordinates") {
1175
set_variation_coordinates(cache_index, p_value);
1176
return true;
1177
} else if (tokens.size() == 3 && tokens[2] == "embolden") {
1178
set_embolden(cache_index, p_value);
1179
return true;
1180
} else if (tokens.size() == 3 && tokens[2] == "face_index") {
1181
set_face_index(cache_index, p_value);
1182
return true;
1183
} else if (tokens.size() == 3 && tokens[2] == "transform") {
1184
set_transform(cache_index, p_value);
1185
return true;
1186
} else if (tokens.size() == 3 && tokens[2] == "spacing_top") {
1187
set_extra_spacing(cache_index, TextServer::SPACING_TOP, p_value);
1188
return true;
1189
} else if (tokens.size() == 3 && tokens[2] == "spacing_bottom") {
1190
set_extra_spacing(cache_index, TextServer::SPACING_BOTTOM, p_value);
1191
return true;
1192
} else if (tokens.size() == 3 && tokens[2] == "spacing_space") {
1193
set_extra_spacing(cache_index, TextServer::SPACING_SPACE, p_value);
1194
return true;
1195
} else if (tokens.size() == 3 && tokens[2] == "spacing_glyph") {
1196
set_extra_spacing(cache_index, TextServer::SPACING_GLYPH, p_value);
1197
return true;
1198
} else if (tokens.size() == 3 && tokens[2] == "baseline_offset") {
1199
set_extra_baseline_offset(cache_index, p_value);
1200
return true;
1201
}
1202
if (tokens.size() >= 5) {
1203
Vector2i sz = Vector2i(tokens[2].to_int(), tokens[3].to_int());
1204
if (tokens[4] == "ascent") {
1205
set_cache_ascent(cache_index, sz.x, p_value);
1206
return true;
1207
} else if (tokens[4] == "descent") {
1208
set_cache_descent(cache_index, sz.x, p_value);
1209
return true;
1210
} else if (tokens[4] == "underline_position") {
1211
set_cache_underline_position(cache_index, sz.x, p_value);
1212
return true;
1213
} else if (tokens[4] == "underline_thickness") {
1214
set_cache_underline_thickness(cache_index, sz.x, p_value);
1215
return true;
1216
} else if (tokens[4] == "scale") {
1217
set_cache_scale(cache_index, sz.x, p_value);
1218
return true;
1219
} else if (tokens.size() == 7 && tokens[4] == "textures") {
1220
int texture_index = tokens[5].to_int();
1221
if (tokens[6] == "image") {
1222
set_texture_image(cache_index, sz, texture_index, p_value);
1223
return true;
1224
} else if (tokens[6] == "offsets") {
1225
set_texture_offsets(cache_index, sz, texture_index, p_value);
1226
return true;
1227
}
1228
} else if (tokens.size() == 7 && tokens[4] == "glyphs") {
1229
int32_t glyph_index = tokens[5].to_int();
1230
if (tokens[6] == "advance") {
1231
set_glyph_advance(cache_index, sz.x, glyph_index, p_value);
1232
return true;
1233
} else if (tokens[6] == "offset") {
1234
set_glyph_offset(cache_index, sz, glyph_index, p_value);
1235
return true;
1236
} else if (tokens[6] == "size") {
1237
set_glyph_size(cache_index, sz, glyph_index, p_value);
1238
return true;
1239
} else if (tokens[6] == "uv_rect") {
1240
set_glyph_uv_rect(cache_index, sz, glyph_index, p_value);
1241
return true;
1242
} else if (tokens[6] == "texture_idx") {
1243
set_glyph_texture_idx(cache_index, sz, glyph_index, p_value);
1244
return true;
1245
}
1246
} else if (tokens.size() == 7 && tokens[4] == "kerning_overrides") {
1247
Vector2i gp = Vector2i(tokens[5].to_int(), tokens[6].to_int());
1248
set_kerning(cache_index, sz.x, gp, p_value);
1249
return true;
1250
}
1251
}
1252
}
1253
return false;
1254
}
1255
1256
bool FontFile::_get(const StringName &p_name, Variant &r_ret) const {
1257
Vector<String> tokens = p_name.operator String().split("/");
1258
if (tokens.size() == 2 && tokens[0] == "language_support_override") {
1259
const String &lang_code = tokens[1];
1260
r_ret = get_language_support_override(lang_code);
1261
return true;
1262
} else if (tokens.size() == 2 && tokens[0] == "script_support_override") {
1263
const String &script_code = tokens[1];
1264
r_ret = get_script_support_override(script_code);
1265
return true;
1266
} else if (tokens.size() >= 3 && tokens[0] == "cache") {
1267
int cache_index = tokens[1].to_int();
1268
if (tokens.size() == 3 && tokens[2] == "variation_coordinates") {
1269
r_ret = get_variation_coordinates(cache_index);
1270
return true;
1271
} else if (tokens.size() == 3 && tokens[2] == "embolden") {
1272
r_ret = get_embolden(cache_index);
1273
return true;
1274
} else if (tokens.size() == 3 && tokens[2] == "face_index") {
1275
r_ret = get_face_index(cache_index);
1276
return true;
1277
} else if (tokens.size() == 3 && tokens[2] == "transform") {
1278
r_ret = get_transform(cache_index);
1279
return true;
1280
} else if (tokens.size() == 3 && tokens[2] == "spacing_top") {
1281
r_ret = get_extra_spacing(cache_index, TextServer::SPACING_TOP);
1282
return true;
1283
} else if (tokens.size() == 3 && tokens[2] == "spacing_bottom") {
1284
r_ret = get_extra_spacing(cache_index, TextServer::SPACING_BOTTOM);
1285
return true;
1286
} else if (tokens.size() == 3 && tokens[2] == "spacing_space") {
1287
r_ret = get_extra_spacing(cache_index, TextServer::SPACING_SPACE);
1288
return true;
1289
} else if (tokens.size() == 3 && tokens[2] == "spacing_glyph") {
1290
r_ret = get_extra_spacing(cache_index, TextServer::SPACING_GLYPH);
1291
return true;
1292
} else if (tokens.size() == 3 && tokens[2] == "baseline_offset") {
1293
r_ret = get_extra_baseline_offset(cache_index);
1294
return true;
1295
}
1296
if (tokens.size() >= 5) {
1297
Vector2i sz = Vector2i(tokens[2].to_int(), tokens[3].to_int());
1298
if (tokens[4] == "ascent") {
1299
r_ret = get_cache_ascent(cache_index, sz.x);
1300
return true;
1301
} else if (tokens[4] == "descent") {
1302
r_ret = get_cache_descent(cache_index, sz.x);
1303
return true;
1304
} else if (tokens[4] == "underline_position") {
1305
r_ret = get_cache_underline_position(cache_index, sz.x);
1306
return true;
1307
} else if (tokens[4] == "underline_thickness") {
1308
r_ret = get_cache_underline_thickness(cache_index, sz.x);
1309
return true;
1310
} else if (tokens[4] == "scale") {
1311
r_ret = get_cache_scale(cache_index, sz.x);
1312
return true;
1313
} else if (tokens.size() == 7 && tokens[4] == "textures") {
1314
int texture_index = tokens[5].to_int();
1315
if (tokens[6] == "image") {
1316
r_ret = get_texture_image(cache_index, sz, texture_index);
1317
return true;
1318
} else if (tokens[6] == "offsets") {
1319
r_ret = get_texture_offsets(cache_index, sz, texture_index);
1320
return true;
1321
}
1322
} else if (tokens.size() == 7 && tokens[4] == "glyphs") {
1323
int32_t glyph_index = tokens[5].to_int();
1324
if (tokens[6] == "advance") {
1325
r_ret = get_glyph_advance(cache_index, sz.x, glyph_index);
1326
return true;
1327
} else if (tokens[6] == "offset") {
1328
r_ret = get_glyph_offset(cache_index, sz, glyph_index);
1329
return true;
1330
} else if (tokens[6] == "size") {
1331
r_ret = get_glyph_size(cache_index, sz, glyph_index);
1332
return true;
1333
} else if (tokens[6] == "uv_rect") {
1334
r_ret = get_glyph_uv_rect(cache_index, sz, glyph_index);
1335
return true;
1336
} else if (tokens[6] == "texture_idx") {
1337
r_ret = get_glyph_texture_idx(cache_index, sz, glyph_index);
1338
return true;
1339
}
1340
} else if (tokens.size() == 7 && tokens[4] == "kerning_overrides") {
1341
Vector2i gp = Vector2i(tokens[5].to_int(), tokens[6].to_int());
1342
r_ret = get_kerning(cache_index, sz.x, gp);
1343
return true;
1344
}
1345
}
1346
}
1347
return false;
1348
}
1349
1350
void FontFile::_get_property_list(List<PropertyInfo> *p_list) const {
1351
Vector<String> lang_over = get_language_support_overrides();
1352
for (int i = 0; i < lang_over.size(); i++) {
1353
p_list->push_back(PropertyInfo(Variant::BOOL, "language_support_override/" + lang_over[i], PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1354
}
1355
Vector<String> scr_over = get_script_support_overrides();
1356
for (int i = 0; i < scr_over.size(); i++) {
1357
p_list->push_back(PropertyInfo(Variant::BOOL, "script_support_override/" + scr_over[i], PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1358
}
1359
for (int i = 0; i < cache.size(); i++) {
1360
String prefix = "cache/" + itos(i) + "/";
1361
TypedArray<Vector2i> sizes = get_size_cache_list(i);
1362
p_list->push_back(PropertyInfo(Variant::DICTIONARY, prefix + "variation_coordinates", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1363
p_list->push_back(PropertyInfo(Variant::INT, prefix + "face_index", PROPERTY_HINT_RANGE, "0,32767,1", PROPERTY_USAGE_STORAGE));
1364
p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + "embolden", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1365
p_list->push_back(PropertyInfo(Variant::TRANSFORM2D, prefix + "transform", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1366
p_list->push_back(PropertyInfo(Variant::INT, prefix + "spacing_top", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1367
p_list->push_back(PropertyInfo(Variant::INT, prefix + "spacing_bottom", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1368
p_list->push_back(PropertyInfo(Variant::INT, prefix + "spacing_space", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1369
p_list->push_back(PropertyInfo(Variant::INT, prefix + "spacing_glyph", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1370
p_list->push_back(PropertyInfo(Variant::FLOAT, prefix + "baseline_offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1371
1372
for (int j = 0; j < sizes.size(); j++) {
1373
Vector2i sz = sizes[j];
1374
String prefix_sz = prefix + itos(sz.x) + "/" + itos(sz.y) + "/";
1375
if (sz.y == 0) {
1376
p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "ascent", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1377
p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "descent", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1378
p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "underline_position", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1379
p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "underline_thickness", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1380
p_list->push_back(PropertyInfo(Variant::FLOAT, prefix_sz + "scale", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1381
}
1382
1383
int tx_cnt = get_texture_count(i, sz);
1384
for (int k = 0; k < tx_cnt; k++) {
1385
p_list->push_back(PropertyInfo(Variant::PACKED_INT32_ARRAY, prefix_sz + "textures/" + itos(k) + "/offsets", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1386
p_list->push_back(PropertyInfo(Variant::OBJECT, prefix_sz + "textures/" + itos(k) + "/image", PROPERTY_HINT_RESOURCE_TYPE, "Image", PROPERTY_USAGE_STORAGE));
1387
}
1388
PackedInt32Array glyphs = get_glyph_list(i, sz);
1389
for (int k = 0; k < glyphs.size(); k++) {
1390
const int32_t &gl = glyphs[k];
1391
if (sz.y == 0) {
1392
p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/advance", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1393
}
1394
p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/offset", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1395
p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "glyphs/" + itos(gl) + "/size", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1396
p_list->push_back(PropertyInfo(Variant::RECT2, prefix_sz + "glyphs/" + itos(gl) + "/uv_rect", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1397
p_list->push_back(PropertyInfo(Variant::INT, prefix_sz + "glyphs/" + itos(gl) + "/texture_idx", PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1398
}
1399
if (sz.y == 0) {
1400
TypedArray<Vector2i> kerning_map = get_kerning_list(i, sz.x);
1401
for (int k = 0; k < kerning_map.size(); k++) {
1402
const Vector2i &gl_pair = kerning_map[k];
1403
p_list->push_back(PropertyInfo(Variant::VECTOR2, prefix_sz + "kerning_overrides/" + itos(gl_pair.x) + "/" + itos(gl_pair.y), PROPERTY_HINT_NONE, "", PROPERTY_USAGE_STORAGE));
1404
}
1405
}
1406
}
1407
}
1408
}
1409
1410
void FontFile::reset_state() {
1411
_clear_cache();
1412
data.clear();
1413
data_ptr = nullptr;
1414
data_size = 0;
1415
cache.clear();
1416
1417
antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
1418
mipmaps = false;
1419
disable_embedded_bitmaps = true;
1420
msdf = false;
1421
force_autohinter = false;
1422
modulate_color_glyphs = false;
1423
allow_system_fallback = true;
1424
hinting = TextServer::HINTING_LIGHT;
1425
subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_DISABLED;
1426
keep_rounding_remainders = true;
1427
oversampling_override = 0.0;
1428
msdf_pixel_range = 14;
1429
msdf_size = 128;
1430
fixed_size = 0;
1431
fixed_size_scale_mode = TextServer::FIXED_SIZE_SCALE_DISABLE;
1432
1433
Font::reset_state();
1434
}
1435
1436
/*************************************************************************/
1437
1438
// OEM encoding mapping for 0x80..0xFF range.
1439
static const char32_t _oem_to_unicode[][129] = {
1440
U"\u20ac\ufffe\u201a\ufffe\u201e\u2026\u2020\u2021\ufffe\u2030\u0160\u2039\u015a\u0164\u017d\u0179\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\ufffe\u2122\u0161\u203a\u015b\u0165\u017e\u017a\xa0\u02c7\u02d8\u0141\xa4\u0104\xa6\xa7\xa8\xa9\u015e\xab\xac\xad\xae\u017b\xb0\xb1\u02db\u0142\xb4\xb5\xb6\xb7\xb8\u0105\u015f\xbb\u013d\u02dd\u013e\u017c\u0154\xc1\xc2\u0102\xc4\u0139\u0106\xc7\u010c\xc9\u0118\xcb\u011a\xcd\xce\u010e\u0110\u0143\u0147\xd3\xd4\u0150\xd6\xd7\u0158\u016e\xda\u0170\xdc\xdd\u0162\xdf\u0155\xe1\xe2\u0103\xe4\u013a\u0107\xe7\u010d\xe9\u0119\xeb\u011b\xed\xee\u010f\u0111\u0144\u0148\xf3\xf4\u0151\xf6\xf7\u0159\u016f\xfa\u0171\xfc\xfd\u0163\u02d9", // 1250 - Latin 2
1441
U"\u0402\u0403\u201a\u0453\u201e\u2026\u2020\u2021\u20ac\u2030\u0409\u2039\u040a\u040c\u040b\u040f\u0452\u2018\u2019\u201c\u201d\u2022\u2013\u2014\ufffe\u2122\u0459\u203a\u045a\u045c\u045b\u045f\xa0\u040e\u045e\u0408\xa4\u0490\xa6\xa7\u0401\xa9\u0404\xab\xac\xad\xae\u0407\xb0\xb1\u0406\u0456\u0491\xb5\xb6\xb7\u0451\u2116\u0454\xbb\u0458\u0405\u0455\u0457\u0410\u0411\u0412\u0413\u0414\u0415\u0416\u0417\u0418\u0419\u041a\u041b\u041c\u041d\u041e\u041f\u0420\u0421\u0422\u0423\u0424\u0425\u0426\u0427\u0428\u0429\u042a\u042b\u042c\u042d\u042e\u042f\u0430\u0431\u0432\u0433\u0434\u0435\u0436\u0437\u0438\u0439\u043a\u043b\u043c\u043d\u043e\u043f\u0440\u0441\u0442\u0443\u0444\u0445\u0446\u0447\u0448\u0449\u044a\u044b\u044c\u044d\u044e\u044f", // 1251 - Cyrillic
1442
U"\u20ac\ufffe\u201a\u0192\u201e\u2026\u2020\u2021\u02c6\u2030\u0160\u2039\u0152\ufffe\u017d\ufffe\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u02dc\u2122\u0161\u203a\u0153\ufffe\u017e\u0178\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\xd0\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\xdd\xde\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\xf0\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\xfd\xfe\xff", // 1252 - Latin 1
1443
U"\u20ac\ufffe\u201a\u0192\u201e\u2026\u2020\u2021\ufffe\u2030\ufffe\u2039\ufffe\ufffe\ufffe\ufffe\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\ufffe\u2122\ufffe\u203a\ufffe\ufffe\ufffe\ufffe\xa0\u0385\u0386\xa3\xa4\xa5\xa6\xa7\xa8\xa9\ufffe\xab\xac\xad\xae\u2015\xb0\xb1\xb2\xb3\u0384\xb5\xb6\xb7\u0388\u0389\u038a\xbb\u038c\xbd\u038e\u038f\u0390\u0391\u0392\u0393\u0394\u0395\u0396\u0397\u0398\u0399\u039a\u039b\u039c\u039d\u039e\u039f\u03a0\u03a1\ufffe\u03a3\u03a4\u03a5\u03a6\u03a7\u03a8\u03a9\u03aa\u03ab\u03ac\u03ad\u03ae\u03af\u03b0\u03b1\u03b2\u03b3\u03b4\u03b5\u03b6\u03b7\u03b8\u03b9\u03ba\u03bb\u03bc\u03bd\u03be\u03bf\u03c0\u03c1\u03c2\u03c3\u03c4\u03c5\u03c6\u03c7\u03c8\u03c9\u03ca\u03cb\u03cc\u03cd\u03ce\ufffe", // 1253 - Greek
1444
U"\u20ac\ufffe\u201a\u0192\u201e\u2026\u2020\u2021\u02c6\u2030\u0160\u2039\u0152\ufffe\ufffe\ufffe\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u02dc\u2122\u0161\u203a\u0153\ufffe\ufffe\u0178\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\xc3\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\xcc\xcd\xce\xcf\u011e\xd1\xd2\xd3\xd4\xd5\xd6\xd7\xd8\xd9\xda\xdb\xdc\u0130\u015e\xdf\xe0\xe1\xe2\xe3\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\xec\xed\xee\xef\u011f\xf1\xf2\xf3\xf4\xf5\xf6\xf7\xf8\xf9\xfa\xfb\xfc\u0131\u015f\xff", // 1254 - Turkish
1445
U"\u20ac\ufffe\u201a\u0192\u201e\u2026\u2020\u2021\u02c6\u2030\ufffe\u2039\ufffe\ufffe\ufffe\ufffe\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u02dc\u2122\ufffe\u203a\ufffe\ufffe\ufffe\ufffe\xa0\xa1\xa2\xa3\u20aa\xa5\xa6\xa7\xa8\xa9\xd7\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xf7\xbb\xbc\xbd\xbe\xbf\u05b0\u05b1\u05b2\u05b3\u05b4\u05b5\u05b6\u05b7\u05b8\u05b9\ufffe\u05bb\u05bc\u05bd\u05be\u05bf\u05c0\u05c1\u05c2\u05c3\u05f0\u05f1\u05f2\u05f3\u05f4\ufffe\ufffe\ufffe\ufffe\ufffe\ufffe\ufffe\u05d0\u05d1\u05d2\u05d3\u05d4\u05d5\u05d6\u05d7\u05d8\u05d9\u05da\u05db\u05dc\u05dd\u05de\u05df\u05e0\u05e1\u05e2\u05e3\u05e4\u05e5\u05e6\u05e7\u05e8\u05e9\u05ea\ufffe\ufffe\u200e\u200f\ufffe", // 1255 - Hebrew
1446
U"\u20ac\u067e\u201a\u0192\u201e\u2026\u2020\u2021\u02c6\u2030\u0679\u2039\u0152\u0686\u0698\u0688\u06af\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u06a9\u2122\u0691\u203a\u0153\u200c\u200d\u06ba\xa0\u060c\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\u06be\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\u061b\xbb\xbc\xbd\xbe\u061f\u06c1\u0621\u0622\u0623\u0624\u0625\u0626\u0627\u0628\u0629\u062a\u062b\u062c\u062d\u062e\u062f\u0630\u0631\u0632\u0633\u0634\u0635\u0636\xd7\u0637\u0638\u0639\u063a\u0640\u0641\u0642\u0643\xe0\u0644\xe2\u0645\u0646\u0647\u0648\xe7\xe8\xe9\xea\xeb\u0649\u064a\xee\xef\u064b\u064c\u064d\u064e\xf4\u064f\u0650\xf7\u0651\xf9\u0652\xfb\xfc\u200e\u200f\u06d2", // 1256 - Arabic
1447
U"\u20ac\ufffe\u201a\ufffe\u201e\u2026\u2020\u2021\ufffe\u2030\ufffe\u2039\ufffe\xa8\u02c7\xb8\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\ufffe\u2122\ufffe\u203a\ufffe\xaf\u02db\ufffe\xa0\ufffe\xa2\xa3\xa4\ufffe\xa6\xa7\xd8\xa9\u0156\xab\xac\xad\xae\xc6\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xf8\xb9\u0157\xbb\xbc\xbd\xbe\xe6\u0104\u012e\u0100\u0106\xc4\xc5\u0118\u0112\u010c\xc9\u0179\u0116\u0122\u0136\u012a\u013b\u0160\u0143\u0145\xd3\u014c\xd5\xd6\xd7\u0172\u0141\u015a\u016a\xdc\u017b\u017d\xdf\u0105\u012f\u0101\u0107\xe4\xe5\u0119\u0113\u010d\xe9\u017a\u0117\u0123\u0137\u012b\u013c\u0161\u0144\u0146\xf3\u014d\xf5\xf6\xf7\u0173\u0142\u015b\u016b\xfc\u017c\u017e\u02d9", // 1257 - Baltic
1448
U"\u20ac\ufffe\u201a\u0192\u201e\u2026\u2020\u2021\u02c6\u2030\ufffe\u2039\u0152\ufffe\ufffe\ufffe\ufffe\u2018\u2019\u201c\u201d\u2022\u2013\u2014\u02dc\u2122\ufffe\u203a\u0153\ufffe\ufffe\u0178\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf\xc0\xc1\xc2\u0102\xc4\xc5\xc6\xc7\xc8\xc9\xca\xcb\u0300\xcd\xce\xcf\u0110\xd1\u0309\xd3\xd4\u01a0\xd6\xd7\xd8\xd9\xda\xdb\xdc\u01af\u0303\xdf\xe0\xe1\xe2\u0103\xe4\xe5\xe6\xe7\xe8\xe9\xea\xeb\u0301\xed\xee\xef\u0111\xf1\u0323\xf3\xf4\u01a1\xf6\xf7\xf8\xf9\xfa\xfb\xfc\u01b0\u20ab\xff", // 1258 - Vietnamese
1449
};
1450
1451
Error FontFile::load_bitmap_font(const String &p_path) {
1452
return _load_bitmap_font(p_path, nullptr);
1453
}
1454
1455
Error FontFile::_load_bitmap_font(const String &p_path, List<String> *r_image_files) {
1456
reset_state();
1457
1458
antialiasing = TextServer::FONT_ANTIALIASING_NONE;
1459
mipmaps = false;
1460
disable_embedded_bitmaps = true;
1461
msdf = false;
1462
force_autohinter = false;
1463
modulate_color_glyphs = false;
1464
allow_system_fallback = true;
1465
hinting = TextServer::HINTING_NONE;
1466
1467
Ref<FileAccess> f = FileAccess::open(p_path, FileAccess::READ);
1468
ERR_FAIL_COND_V_MSG(f.is_null(), ERR_CANT_CREATE, vformat("Cannot open font from file: %s.", p_path));
1469
1470
int base_size = 16;
1471
int height = 0;
1472
int ascent = 0;
1473
int outline = 0;
1474
BitField<TextServer::FontStyle> st_flags = 0;
1475
String font_name;
1476
1477
bool packed = false;
1478
uint8_t ch[4] = { 0, 0, 0, 0 }; // RGBA
1479
int first_gl_ch = -1;
1480
int first_ol_ch = -1;
1481
int first_cm_ch = -1;
1482
1483
unsigned char magic[4];
1484
f->get_buffer((unsigned char *)&magic, 4);
1485
if (magic[0] == 'B' && magic[1] == 'M' && magic[2] == 'F') {
1486
// Binary BMFont file.
1487
ERR_FAIL_COND_V_MSG(magic[3] != 3, ERR_CANT_CREATE, vformat("Version %d of BMFont is not supported (should be 3).", (int)magic[3]));
1488
1489
uint8_t block_type = f->get_8();
1490
uint32_t block_size = f->get_32();
1491
bool unicode = false;
1492
uint8_t encoding = 9;
1493
while (!f->eof_reached()) {
1494
uint64_t off = f->get_position();
1495
switch (block_type) {
1496
case 1: /* info */ {
1497
ERR_FAIL_COND_V_MSG(block_size < 15, ERR_CANT_CREATE, "Invalid BMFont info block size.");
1498
base_size = Math::abs(static_cast<int16_t>(f->get_16()));
1499
if (base_size == 0) {
1500
base_size = 16;
1501
}
1502
uint8_t flags = f->get_8();
1503
if (flags & (1 << 3)) {
1504
st_flags.set_flag(TextServer::FONT_BOLD);
1505
}
1506
if (flags & (1 << 2)) {
1507
st_flags.set_flag(TextServer::FONT_ITALIC);
1508
}
1509
unicode = (flags & 0x02);
1510
uint8_t encoding_id = f->get_8(); // non-unicode charset
1511
if (!unicode) {
1512
switch (encoding_id) {
1513
case 0x00: {
1514
encoding = 2;
1515
} break;
1516
case 0xB2: {
1517
encoding = 6;
1518
} break;
1519
case 0xBA: {
1520
encoding = 7;
1521
} break;
1522
case 0xEE: {
1523
encoding = 0;
1524
} break;
1525
case 0xA1: {
1526
encoding = 3;
1527
} break;
1528
case 0xB1: {
1529
encoding = 5;
1530
} break;
1531
case 0xCC: {
1532
encoding = 1;
1533
} break;
1534
case 0xA2: {
1535
encoding = 4;
1536
} break;
1537
case 0xA3: {
1538
encoding = 8;
1539
} break;
1540
default: {
1541
WARN_PRINT(vformat("Unknown BMFont OEM encoding %x, parsing as Unicode (should be 0x00 - Latin 1, 0xB2 - Arabic, 0xBA - Baltic, 0xEE - Latin 2, 0xA1 - Greek, 0xB1 - Hebrew, 0xCC - Cyrillic, 0xA2 - Turkish, 0xA3 - Vietnamese).", encoding_id));
1542
} break;
1543
};
1544
}
1545
f->get_16(); // stretch_h, skip
1546
f->get_8(); // aa, skip
1547
f->get_32(); // padding, skip
1548
f->get_16(); // spacing, skip
1549
outline = f->get_8();
1550
// font name
1551
PackedByteArray name_data;
1552
name_data.resize(block_size - 14);
1553
f->get_buffer(name_data.ptrw(), block_size - 14);
1554
font_name = String::utf8((const char *)name_data.ptr(), block_size - 14);
1555
set_fixed_size(base_size);
1556
} break;
1557
case 2: /* common */ {
1558
ERR_FAIL_COND_V_MSG(block_size != 15, ERR_CANT_CREATE, "Invalid BMFont common block size.");
1559
height = f->get_16();
1560
ascent = f->get_16();
1561
f->get_32(); // scale, skip
1562
f->get_16(); // pages, skip
1563
uint8_t flags = f->get_8();
1564
packed = (flags & 0x01);
1565
ch[3] = f->get_8();
1566
ch[0] = f->get_8();
1567
ch[1] = f->get_8();
1568
ch[2] = f->get_8();
1569
for (int i = 0; i < 4; i++) {
1570
if (ch[i] == 0 && first_gl_ch == -1) {
1571
first_gl_ch = i;
1572
}
1573
if (ch[i] == 1 && first_ol_ch == -1) {
1574
first_ol_ch = i;
1575
if (outline == 0) {
1576
outline = 1;
1577
}
1578
}
1579
if (ch[i] == 2 && first_cm_ch == -1) {
1580
first_cm_ch = i;
1581
}
1582
}
1583
} break;
1584
case 3: /* pages */ {
1585
int page = 0;
1586
CharString cs;
1587
char32_t c = f->get_8();
1588
while (!f->eof_reached() && f->get_position() <= off + block_size) {
1589
if (c == '\0') {
1590
String base_dir = p_path.get_base_dir();
1591
String file = base_dir.path_join(String::utf8(cs.ptr(), cs.length()));
1592
if (RenderingServer::get_singleton() != nullptr) {
1593
Ref<Image> img;
1594
img.instantiate();
1595
Error err = ImageLoader::load_image(file, img);
1596
ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, vformat("Can't load font texture: %s.", file));
1597
if (r_image_files) {
1598
r_image_files->push_back(file);
1599
}
1600
1601
if (packed) {
1602
if (ch[3] == 0) { // 4 x 8 bit monochrome, no outline
1603
outline = 0;
1604
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1605
_convert_packed_8bit(img, page, base_size);
1606
} else if ((ch[3] == 2) && (outline > 0)) { // 4 x 4 bit monochrome, gl + outline
1607
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1608
_convert_packed_4bit(img, page, base_size);
1609
} else {
1610
ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Unsupported BMFont texture format.");
1611
}
1612
} else {
1613
if ((ch[0] == 0) && (ch[1] == 0) && (ch[2] == 0) && (ch[3] == 0)) { // RGBA8 color, no outline
1614
outline = 0;
1615
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1616
set_texture_image(0, Vector2i(base_size, 0), page, img);
1617
} else if ((ch[0] == 2) && (ch[1] == 2) && (ch[2] == 2) && (ch[3] == 2) && (outline > 0)) { // RGBA4 color, gl + outline
1618
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1619
_convert_rgba_4bit(img, page, base_size);
1620
} else if ((first_gl_ch >= 0) && (first_ol_ch >= 0) && (outline > 0)) { // 1 x 8 bit monochrome, gl + outline
1621
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1622
_convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
1623
_convert_mono_8bit(img, page, first_ol_ch, base_size, 1);
1624
} else if ((first_cm_ch >= 0) && (outline > 0)) { // 1 x 4 bit monochrome, gl + outline
1625
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1626
_convert_mono_4bit(img, page, first_cm_ch, base_size, 1);
1627
} else if (first_gl_ch >= 0) { // 1 x 8 bit monochrome, no outline
1628
outline = 0;
1629
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1630
_convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
1631
} else {
1632
ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Unsupported BMFont texture format.");
1633
}
1634
}
1635
}
1636
page++;
1637
cs = "";
1638
} else {
1639
cs += c;
1640
}
1641
c = f->get_8();
1642
}
1643
} break;
1644
case 4: /* chars */ {
1645
int char_count = block_size / 20;
1646
for (int i = 0; i < char_count; i++) {
1647
Vector2 advance;
1648
Vector2 size;
1649
Vector2 offset;
1650
Rect2 uv_rect;
1651
1652
char32_t idx = f->get_32();
1653
if (!unicode && encoding < 9) {
1654
if (idx >= 0x80 && idx <= 0xFF) {
1655
idx = _oem_to_unicode[encoding][idx - 0x80];
1656
} else if (idx > 0xFF) {
1657
WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", idx));
1658
idx = 0x00;
1659
}
1660
}
1661
uv_rect.position.x = (int16_t)f->get_16();
1662
uv_rect.position.y = (int16_t)f->get_16();
1663
uv_rect.size.width = (int16_t)f->get_16();
1664
size.width = uv_rect.size.width;
1665
uv_rect.size.height = (int16_t)f->get_16();
1666
size.height = uv_rect.size.height;
1667
offset.x = (int16_t)f->get_16();
1668
offset.y = (int16_t)f->get_16() - ascent;
1669
advance.x = (int16_t)f->get_16();
1670
if (advance.x < 0) {
1671
advance.x = size.width + 1;
1672
}
1673
1674
int texture_idx = f->get_8();
1675
uint8_t channel = f->get_8();
1676
1677
int ch_off = 0;
1678
if (packed) {
1679
switch (channel) {
1680
case 1:
1681
ch_off = 2;
1682
break; // B
1683
case 2:
1684
ch_off = 1;
1685
break; // G
1686
case 4:
1687
ch_off = 0;
1688
break; // R
1689
case 8:
1690
ch_off = 3;
1691
break; // A
1692
default:
1693
ch_off = 0;
1694
break;
1695
}
1696
}
1697
set_glyph_advance(0, base_size, idx, advance);
1698
set_glyph_offset(0, Vector2i(base_size, 0), idx, offset);
1699
set_glyph_size(0, Vector2i(base_size, 0), idx, size);
1700
set_glyph_uv_rect(0, Vector2i(base_size, 0), idx, uv_rect);
1701
set_glyph_texture_idx(0, Vector2i(base_size, 0), idx, texture_idx * (packed ? 4 : 1) + ch_off);
1702
if (outline > 0) {
1703
set_glyph_offset(0, Vector2i(base_size, 1), idx, offset);
1704
set_glyph_size(0, Vector2i(base_size, 1), idx, size);
1705
set_glyph_uv_rect(0, Vector2i(base_size, 1), idx, uv_rect);
1706
set_glyph_texture_idx(0, Vector2i(base_size, 1), idx, texture_idx * (packed ? 4 : 1) + ch_off);
1707
}
1708
}
1709
} break;
1710
case 5: /* kerning */ {
1711
int pair_count = block_size / 10;
1712
for (int i = 0; i < pair_count; i++) {
1713
Vector2i kpk;
1714
kpk.x = f->get_32();
1715
kpk.y = f->get_32();
1716
if (!unicode && encoding < 9) {
1717
if (kpk.x >= 0x80 && kpk.x <= 0xFF) {
1718
kpk.x = _oem_to_unicode[encoding][kpk.x - 0x80];
1719
} else if (kpk.x > 0xFF) {
1720
WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", kpk.x));
1721
kpk.x = 0x00;
1722
}
1723
if (kpk.y >= 0x80 && kpk.y <= 0xFF) {
1724
kpk.y = _oem_to_unicode[encoding][kpk.y - 0x80];
1725
} else if (kpk.y > 0xFF) {
1726
WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", kpk.y));
1727
kpk.y = 0x00;
1728
}
1729
}
1730
set_kerning(0, base_size, kpk, Vector2((int16_t)f->get_16(), 0));
1731
}
1732
} break;
1733
default: {
1734
ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Invalid BMFont block type.");
1735
} break;
1736
}
1737
f->seek(off + block_size);
1738
block_type = f->get_8();
1739
block_size = f->get_32();
1740
}
1741
1742
} else {
1743
// Text BMFont file.
1744
f->seek(0);
1745
bool unicode = false;
1746
uint8_t encoding = 9;
1747
while (true) {
1748
String line = f->get_line();
1749
1750
int delimiter = line.find_char(' ');
1751
String type = line.substr(0, delimiter);
1752
int pos = delimiter + 1;
1753
HashMap<String, String> keys;
1754
1755
while (pos < line.size() && line[pos] == ' ') {
1756
pos++;
1757
}
1758
1759
while (pos < line.size()) {
1760
int eq = line.find_char('=', pos);
1761
if (eq == -1) {
1762
break;
1763
}
1764
String key = line.substr(pos, eq - pos);
1765
int end = -1;
1766
String value;
1767
if (line[eq + 1] == '"') {
1768
end = line.find_char('"', eq + 2);
1769
if (end == -1) {
1770
break;
1771
}
1772
value = line.substr(eq + 2, end - 1 - eq - 1);
1773
pos = end + 1;
1774
} else {
1775
end = line.find_char(' ', eq + 1);
1776
if (end == -1) {
1777
end = line.size();
1778
}
1779
value = line.substr(eq + 1, end - eq);
1780
pos = end;
1781
}
1782
1783
while (pos < line.size() && line[pos] == ' ') {
1784
pos++;
1785
}
1786
1787
keys[key] = value;
1788
}
1789
1790
if (type == "info") {
1791
if (keys.has("size")) {
1792
base_size = Math::abs(keys["size"].to_int());
1793
if (base_size == 0) {
1794
base_size = 16;
1795
}
1796
}
1797
if (keys.has("outline")) {
1798
outline = keys["outline"].to_int();
1799
}
1800
if (keys.has("bold")) {
1801
if (keys["bold"].to_int()) {
1802
st_flags.set_flag(TextServer::FONT_BOLD);
1803
}
1804
}
1805
if (keys.has("italic")) {
1806
if (keys["italic"].to_int()) {
1807
st_flags.set_flag(TextServer::FONT_ITALIC);
1808
}
1809
}
1810
if (keys.has("face")) {
1811
font_name = keys["face"];
1812
}
1813
if (keys.has("unicode")) {
1814
unicode = keys["unicode"].to_int();
1815
}
1816
if (!unicode) {
1817
if (keys.has("charset")) {
1818
String encoding_name = keys["charset"].to_upper();
1819
if (encoding_name == "" || encoding_name == "ASCII" || encoding_name == "ANSI") {
1820
encoding = 2;
1821
} else if (encoding_name == "ARABIC") {
1822
encoding = 6;
1823
} else if (encoding_name == "BALTIC") {
1824
encoding = 7;
1825
} else if (encoding_name == "EASTEUROPE") {
1826
encoding = 0;
1827
} else if (encoding_name == "GREEK") {
1828
encoding = 3;
1829
} else if (encoding_name == "HEBREW") {
1830
encoding = 5;
1831
} else if (encoding_name == "RUSSIAN") {
1832
encoding = 1;
1833
} else if (encoding_name == "TURKISH") {
1834
encoding = 4;
1835
} else if (encoding_name == "VIETNAMESE") {
1836
encoding = 8;
1837
} else {
1838
WARN_PRINT(vformat("Unknown BMFont OEM encoding %s, parsing as Unicode (should be ANSI, ASCII, ARABIC, BALTIC, EASTEUROPE, GREEK, HEBREW, RUSSIAN, TURKISH or VIETNAMESE).", encoding_name));
1839
}
1840
} else {
1841
encoding = 2;
1842
}
1843
}
1844
set_fixed_size(base_size);
1845
} else if (type == "common") {
1846
if (keys.has("lineHeight")) {
1847
height = keys["lineHeight"].to_int();
1848
}
1849
if (keys.has("base")) {
1850
ascent = keys["base"].to_int();
1851
}
1852
if (keys.has("packed")) {
1853
packed = (keys["packed"].to_int() == 1);
1854
}
1855
if (keys.has("alphaChnl")) {
1856
ch[3] = keys["alphaChnl"].to_int();
1857
}
1858
if (keys.has("redChnl")) {
1859
ch[0] = keys["redChnl"].to_int();
1860
}
1861
if (keys.has("greenChnl")) {
1862
ch[1] = keys["greenChnl"].to_int();
1863
}
1864
if (keys.has("blueChnl")) {
1865
ch[2] = keys["blueChnl"].to_int();
1866
}
1867
for (int i = 0; i < 4; i++) {
1868
if (ch[i] == 0 && first_gl_ch == -1) {
1869
first_gl_ch = i;
1870
}
1871
if (ch[i] == 1 && first_ol_ch == -1) {
1872
first_ol_ch = i;
1873
if (outline == 0) {
1874
outline = 1;
1875
}
1876
}
1877
if (ch[i] == 2 && first_cm_ch == -1) {
1878
first_cm_ch = i;
1879
}
1880
}
1881
} else if (type == "page") {
1882
int page = 0;
1883
if (keys.has("id")) {
1884
page = keys["id"].to_int();
1885
}
1886
if (keys.has("file")) {
1887
String base_dir = p_path.get_base_dir();
1888
String file = base_dir.path_join(keys["file"]);
1889
if (RenderingServer::get_singleton() != nullptr) {
1890
Ref<Image> img;
1891
img.instantiate();
1892
Error err = ImageLoader::load_image(file, img);
1893
ERR_FAIL_COND_V_MSG(err != OK, ERR_FILE_CANT_READ, vformat("Can't load font texture: %s.", file));
1894
if (r_image_files) {
1895
r_image_files->push_back(file);
1896
}
1897
1898
if (packed) {
1899
if (ch[3] == 0) { // 4 x 8 bit monochrome, no outline
1900
outline = 0;
1901
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1902
_convert_packed_8bit(img, page, base_size);
1903
} else if ((ch[3] == 2) && (outline > 0)) { // 4 x 4 bit monochrome, gl + outline
1904
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1905
_convert_packed_4bit(img, page, base_size);
1906
} else {
1907
ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Unsupported BMFont texture format.");
1908
}
1909
} else {
1910
if ((ch[3] == 0) && (ch[0] == 4) && (ch[1] == 4) && (ch[2] == 4) && img->get_format() == Image::FORMAT_RGBA8) { // might be RGBA8 color, no outline (color part of the image should be sold white, but some apps designed for Godot 3 generate color fonts with this config)
1911
outline = 0;
1912
set_texture_image(0, Vector2i(base_size, 0), page, img);
1913
} else if ((ch[0] == 0) && (ch[1] == 0) && (ch[2] == 0) && (ch[3] == 0)) { // RGBA8 color, no outline
1914
outline = 0;
1915
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1916
set_texture_image(0, Vector2i(base_size, 0), page, img);
1917
} else if ((ch[0] == 2) && (ch[1] == 2) && (ch[2] == 2) && (ch[3] == 2) && (outline > 0)) { // RGBA4 color, gl + outline
1918
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1919
_convert_rgba_4bit(img, page, base_size);
1920
} else if ((first_gl_ch >= 0) && (first_ol_ch >= 0) && (outline > 0)) { // 1 x 8 bit monochrome, gl + outline
1921
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1922
_convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
1923
_convert_mono_8bit(img, page, first_ol_ch, base_size, 1);
1924
} else if ((first_cm_ch >= 0) && (outline > 0)) { // 1 x 4 bit monochrome, gl + outline
1925
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1926
_convert_mono_4bit(img, page, first_cm_ch, base_size, 1);
1927
} else if (first_gl_ch >= 0) { // 1 x 8 bit monochrome, no outline
1928
outline = 0;
1929
ERR_FAIL_COND_V_MSG(img->get_format() != Image::FORMAT_RGBA8 && img->get_format() != Image::FORMAT_L8, ERR_FILE_CANT_READ, "Unsupported BMFont texture format.");
1930
_convert_mono_8bit(img, page, first_gl_ch, base_size, 0);
1931
} else {
1932
ERR_FAIL_V_MSG(ERR_CANT_CREATE, "Unsupported BMFont texture format.");
1933
}
1934
}
1935
}
1936
}
1937
} else if (type == "char") {
1938
char32_t idx = 0;
1939
Vector2 advance;
1940
Vector2 size;
1941
Vector2 offset;
1942
Rect2 uv_rect;
1943
int texture_idx = -1;
1944
uint8_t channel = 15;
1945
1946
if (keys.has("id")) {
1947
idx = keys["id"].to_int();
1948
if (!unicode && encoding < 9) {
1949
if (idx >= 0x80 && idx <= 0xFF) {
1950
idx = _oem_to_unicode[encoding][idx - 0x80];
1951
} else if (idx > 0xFF) {
1952
WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", idx));
1953
idx = 0x00;
1954
}
1955
}
1956
}
1957
if (keys.has("x")) {
1958
uv_rect.position.x = keys["x"].to_int();
1959
}
1960
if (keys.has("y")) {
1961
uv_rect.position.y = keys["y"].to_int();
1962
}
1963
if (keys.has("width")) {
1964
uv_rect.size.width = keys["width"].to_int();
1965
size.width = keys["width"].to_int();
1966
}
1967
if (keys.has("height")) {
1968
uv_rect.size.height = keys["height"].to_int();
1969
size.height = keys["height"].to_int();
1970
}
1971
if (keys.has("xoffset")) {
1972
offset.x = keys["xoffset"].to_int();
1973
}
1974
if (keys.has("yoffset")) {
1975
offset.y = keys["yoffset"].to_int() - ascent;
1976
}
1977
if (keys.has("page")) {
1978
texture_idx = keys["page"].to_int();
1979
}
1980
if (keys.has("xadvance")) {
1981
advance.x = keys["xadvance"].to_int();
1982
}
1983
if (advance.x < 0) {
1984
advance.x = size.width + 1;
1985
}
1986
if (keys.has("chnl")) {
1987
channel = keys["chnl"].to_int();
1988
}
1989
1990
int ch_off = 0;
1991
if (packed) {
1992
switch (channel) {
1993
case 1:
1994
ch_off = 2;
1995
break; // B
1996
case 2:
1997
ch_off = 1;
1998
break; // G
1999
case 4:
2000
ch_off = 0;
2001
break; // R
2002
case 8:
2003
ch_off = 3;
2004
break; // A
2005
default:
2006
ch_off = 0;
2007
break;
2008
}
2009
}
2010
set_glyph_advance(0, base_size, idx, advance);
2011
set_glyph_offset(0, Vector2i(base_size, 0), idx, offset);
2012
set_glyph_size(0, Vector2i(base_size, 0), idx, size);
2013
set_glyph_uv_rect(0, Vector2i(base_size, 0), idx, uv_rect);
2014
set_glyph_texture_idx(0, Vector2i(base_size, 0), idx, texture_idx * (packed ? 4 : 1) + ch_off);
2015
if (outline > 0) {
2016
set_glyph_offset(0, Vector2i(base_size, 1), idx, offset);
2017
set_glyph_size(0, Vector2i(base_size, 1), idx, size);
2018
set_glyph_uv_rect(0, Vector2i(base_size, 1), idx, uv_rect);
2019
set_glyph_texture_idx(0, Vector2i(base_size, 1), idx, texture_idx * (packed ? 4 : 1) + ch_off);
2020
}
2021
} else if (type == "kerning") {
2022
Vector2i kpk;
2023
if (keys.has("first")) {
2024
kpk.x = keys["first"].to_int();
2025
}
2026
if (keys.has("second")) {
2027
kpk.y = keys["second"].to_int();
2028
}
2029
if (!unicode && encoding < 9) {
2030
if (kpk.x >= 0x80 && kpk.x <= 0xFF) {
2031
kpk.x = _oem_to_unicode[encoding][kpk.x - 0x80];
2032
} else if (kpk.x > 0xFF) {
2033
WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", kpk.x));
2034
kpk.x = 0x00;
2035
}
2036
if (kpk.y >= 0x80 && kpk.y <= 0xFF) {
2037
kpk.y = _oem_to_unicode[encoding][kpk.y - 0x80];
2038
} else if (kpk.y > 0xFF) {
2039
WARN_PRINT(vformat("Invalid BMFont OEM character %x (should be 0x00-0xFF).", kpk.x));
2040
kpk.y = 0x00;
2041
}
2042
}
2043
if (keys.has("amount")) {
2044
set_kerning(0, base_size, kpk, Vector2(keys["amount"].to_int(), 0));
2045
}
2046
}
2047
2048
if (f->eof_reached()) {
2049
break;
2050
}
2051
}
2052
}
2053
2054
set_font_name(font_name);
2055
set_font_style(st_flags);
2056
if (st_flags & TextServer::FONT_BOLD) {
2057
set_font_weight(700);
2058
}
2059
set_cache_ascent(0, base_size, ascent);
2060
set_cache_descent(0, base_size, height - ascent);
2061
2062
return OK;
2063
}
2064
2065
Error FontFile::load_dynamic_font(const String &p_path) {
2066
reset_state();
2067
2068
Vector<uint8_t> font_data = FileAccess::get_file_as_bytes(p_path);
2069
set_data(font_data);
2070
2071
return OK;
2072
}
2073
2074
void FontFile::set_data_ptr(const uint8_t *p_data, size_t p_size) {
2075
data.clear();
2076
data_ptr = p_data;
2077
data_size = p_size;
2078
2079
for (int i = 0; i < cache.size(); i++) {
2080
if (cache[i].is_valid()) {
2081
TS->font_set_data_ptr(cache[i], data_ptr, data_size);
2082
}
2083
}
2084
}
2085
2086
void FontFile::set_data(const PackedByteArray &p_data) {
2087
data = p_data;
2088
data_ptr = data.ptr();
2089
data_size = data.size();
2090
2091
for (int i = 0; i < cache.size(); i++) {
2092
if (cache[i].is_valid()) {
2093
TS->font_set_data_ptr(cache[i], data_ptr, data_size);
2094
}
2095
}
2096
}
2097
2098
PackedByteArray FontFile::get_data() const {
2099
if (unlikely((size_t)data.size() != data_size)) {
2100
data.resize(data_size);
2101
memcpy(data.ptrw(), data_ptr, data_size);
2102
}
2103
return data;
2104
}
2105
2106
void FontFile::set_font_name(const String &p_name) {
2107
_ensure_rid(0);
2108
TS->font_set_name(cache[0], p_name);
2109
}
2110
2111
void FontFile::set_font_style_name(const String &p_name) {
2112
_ensure_rid(0);
2113
TS->font_set_style_name(cache[0], p_name);
2114
}
2115
2116
void FontFile::set_font_style(BitField<TextServer::FontStyle> p_style) {
2117
_ensure_rid(0);
2118
TS->font_set_style(cache[0], p_style);
2119
}
2120
2121
void FontFile::set_font_weight(int p_weight) {
2122
_ensure_rid(0);
2123
TS->font_set_weight(cache[0], p_weight);
2124
}
2125
2126
void FontFile::set_font_stretch(int p_stretch) {
2127
_ensure_rid(0);
2128
TS->font_set_stretch(cache[0], p_stretch);
2129
}
2130
2131
void FontFile::set_antialiasing(TextServer::FontAntialiasing p_antialiasing) {
2132
if (antialiasing != p_antialiasing) {
2133
antialiasing = p_antialiasing;
2134
for (int i = 0; i < cache.size(); i++) {
2135
_ensure_rid(i);
2136
TS->font_set_antialiasing(cache[i], antialiasing);
2137
}
2138
emit_changed();
2139
}
2140
}
2141
2142
TextServer::FontAntialiasing FontFile::get_antialiasing() const {
2143
return antialiasing;
2144
}
2145
2146
void FontFile::set_disable_embedded_bitmaps(bool p_disable_embedded_bitmaps) {
2147
if (disable_embedded_bitmaps != p_disable_embedded_bitmaps) {
2148
disable_embedded_bitmaps = p_disable_embedded_bitmaps;
2149
for (int i = 0; i < cache.size(); i++) {
2150
_ensure_rid(i);
2151
TS->font_set_disable_embedded_bitmaps(cache[i], disable_embedded_bitmaps);
2152
}
2153
emit_changed();
2154
}
2155
}
2156
2157
bool FontFile::get_disable_embedded_bitmaps() const {
2158
return disable_embedded_bitmaps;
2159
}
2160
2161
void FontFile::set_generate_mipmaps(bool p_generate_mipmaps) {
2162
if (mipmaps != p_generate_mipmaps) {
2163
mipmaps = p_generate_mipmaps;
2164
for (int i = 0; i < cache.size(); i++) {
2165
_ensure_rid(i);
2166
TS->font_set_generate_mipmaps(cache[i], mipmaps);
2167
}
2168
emit_changed();
2169
}
2170
}
2171
2172
bool FontFile::get_generate_mipmaps() const {
2173
return mipmaps;
2174
}
2175
2176
void FontFile::set_multichannel_signed_distance_field(bool p_msdf) {
2177
if (msdf != p_msdf) {
2178
msdf = p_msdf;
2179
for (int i = 0; i < cache.size(); i++) {
2180
_ensure_rid(i);
2181
TS->font_set_multichannel_signed_distance_field(cache[i], msdf);
2182
}
2183
emit_changed();
2184
}
2185
}
2186
2187
bool FontFile::is_multichannel_signed_distance_field() const {
2188
return msdf;
2189
}
2190
2191
void FontFile::set_msdf_pixel_range(int p_msdf_pixel_range) {
2192
if (msdf_pixel_range != p_msdf_pixel_range) {
2193
msdf_pixel_range = p_msdf_pixel_range;
2194
for (int i = 0; i < cache.size(); i++) {
2195
_ensure_rid(i);
2196
TS->font_set_msdf_pixel_range(cache[i], msdf_pixel_range);
2197
}
2198
emit_changed();
2199
}
2200
}
2201
2202
int FontFile::get_msdf_pixel_range() const {
2203
return msdf_pixel_range;
2204
}
2205
2206
void FontFile::set_msdf_size(int p_msdf_size) {
2207
if (msdf_size != p_msdf_size) {
2208
msdf_size = p_msdf_size;
2209
for (int i = 0; i < cache.size(); i++) {
2210
_ensure_rid(i);
2211
TS->font_set_msdf_size(cache[i], msdf_size);
2212
}
2213
emit_changed();
2214
}
2215
}
2216
2217
int FontFile::get_msdf_size() const {
2218
return msdf_size;
2219
}
2220
2221
void FontFile::set_fixed_size(int p_fixed_size) {
2222
if (fixed_size != p_fixed_size) {
2223
fixed_size = p_fixed_size;
2224
for (int i = 0; i < cache.size(); i++) {
2225
_ensure_rid(i);
2226
TS->font_set_fixed_size(cache[i], fixed_size);
2227
}
2228
emit_changed();
2229
}
2230
}
2231
2232
int FontFile::get_fixed_size() const {
2233
return fixed_size;
2234
}
2235
2236
void FontFile::set_fixed_size_scale_mode(TextServer::FixedSizeScaleMode p_fixed_size_scale_mode) {
2237
if (fixed_size_scale_mode != p_fixed_size_scale_mode) {
2238
fixed_size_scale_mode = p_fixed_size_scale_mode;
2239
for (int i = 0; i < cache.size(); i++) {
2240
_ensure_rid(i);
2241
TS->font_set_fixed_size_scale_mode(cache[i], fixed_size_scale_mode);
2242
}
2243
emit_changed();
2244
}
2245
}
2246
2247
TextServer::FixedSizeScaleMode FontFile::get_fixed_size_scale_mode() const {
2248
return fixed_size_scale_mode;
2249
}
2250
2251
void FontFile::set_allow_system_fallback(bool p_allow_system_fallback) {
2252
if (allow_system_fallback != p_allow_system_fallback) {
2253
allow_system_fallback = p_allow_system_fallback;
2254
for (int i = 0; i < cache.size(); i++) {
2255
_ensure_rid(i);
2256
TS->font_set_allow_system_fallback(cache[i], allow_system_fallback);
2257
}
2258
emit_changed();
2259
}
2260
}
2261
2262
bool FontFile::is_allow_system_fallback() const {
2263
return allow_system_fallback;
2264
}
2265
2266
void FontFile::set_force_autohinter(bool p_force_autohinter) {
2267
if (force_autohinter != p_force_autohinter) {
2268
force_autohinter = p_force_autohinter;
2269
for (int i = 0; i < cache.size(); i++) {
2270
_ensure_rid(i);
2271
TS->font_set_force_autohinter(cache[i], force_autohinter);
2272
}
2273
emit_changed();
2274
}
2275
}
2276
2277
bool FontFile::is_force_autohinter() const {
2278
return force_autohinter;
2279
}
2280
2281
void FontFile::set_modulate_color_glyphs(bool p_modulate) {
2282
if (modulate_color_glyphs != p_modulate) {
2283
modulate_color_glyphs = p_modulate;
2284
for (int i = 0; i < cache.size(); i++) {
2285
_ensure_rid(i);
2286
TS->font_set_modulate_color_glyphs(cache[i], modulate_color_glyphs);
2287
}
2288
emit_changed();
2289
}
2290
}
2291
2292
bool FontFile::is_modulate_color_glyphs() const {
2293
return modulate_color_glyphs;
2294
}
2295
2296
void FontFile::set_hinting(TextServer::Hinting p_hinting) {
2297
if (hinting != p_hinting) {
2298
hinting = p_hinting;
2299
for (int i = 0; i < cache.size(); i++) {
2300
_ensure_rid(i);
2301
TS->font_set_hinting(cache[i], hinting);
2302
}
2303
emit_changed();
2304
}
2305
}
2306
2307
TextServer::Hinting FontFile::get_hinting() const {
2308
return hinting;
2309
}
2310
2311
void FontFile::set_subpixel_positioning(TextServer::SubpixelPositioning p_subpixel) {
2312
if (subpixel_positioning != p_subpixel) {
2313
subpixel_positioning = p_subpixel;
2314
for (int i = 0; i < cache.size(); i++) {
2315
_ensure_rid(i);
2316
TS->font_set_subpixel_positioning(cache[i], subpixel_positioning);
2317
}
2318
emit_changed();
2319
}
2320
}
2321
2322
TextServer::SubpixelPositioning FontFile::get_subpixel_positioning() const {
2323
return subpixel_positioning;
2324
}
2325
2326
void FontFile::set_keep_rounding_remainders(bool p_keep_rounding_remainders) {
2327
if (keep_rounding_remainders != p_keep_rounding_remainders) {
2328
keep_rounding_remainders = p_keep_rounding_remainders;
2329
for (int i = 0; i < cache.size(); i++) {
2330
_ensure_rid(i);
2331
TS->font_set_keep_rounding_remainders(cache[i], keep_rounding_remainders);
2332
}
2333
emit_changed();
2334
}
2335
}
2336
2337
bool FontFile::get_keep_rounding_remainders() const {
2338
return keep_rounding_remainders;
2339
}
2340
2341
void FontFile::set_oversampling(real_t p_oversampling) {
2342
if (oversampling_override != p_oversampling) {
2343
oversampling_override = p_oversampling;
2344
for (int i = 0; i < cache.size(); i++) {
2345
_ensure_rid(i);
2346
TS->font_set_oversampling(cache[i], oversampling_override);
2347
}
2348
emit_changed();
2349
}
2350
}
2351
2352
real_t FontFile::get_oversampling() const {
2353
return oversampling_override;
2354
}
2355
2356
RID FontFile::find_variation(const Dictionary &p_variation_coordinates, int p_face_index, float p_strength, Transform2D p_transform, int p_spacing_top, int p_spacing_bottom, int p_spacing_space, int p_spacing_glyph, float p_baseline_offset) const {
2357
// Find existing variation cache.
2358
const Dictionary &supported_coords = get_supported_variation_list();
2359
int make_linked_from = -1;
2360
for (int i = 0; i < cache.size(); i++) {
2361
if (cache[i].is_valid()) {
2362
const Dictionary &cache_var = TS->font_get_variation_coordinates(cache[i]);
2363
bool match = true;
2364
bool match_linked = true;
2365
match = match && (TS->font_get_face_index(cache[i]) == p_face_index);
2366
match = match && (TS->font_get_embolden(cache[i]) == p_strength);
2367
match = match && (TS->font_get_transform(cache[i]) == p_transform);
2368
match_linked = match_linked && (TS->font_get_spacing(cache[i], TextServer::SPACING_TOP) == p_spacing_top);
2369
match_linked = match_linked && (TS->font_get_spacing(cache[i], TextServer::SPACING_BOTTOM) == p_spacing_bottom);
2370
match_linked = match_linked && (TS->font_get_spacing(cache[i], TextServer::SPACING_SPACE) == p_spacing_space);
2371
match_linked = match_linked && (TS->font_get_spacing(cache[i], TextServer::SPACING_GLYPH) == p_spacing_glyph);
2372
match_linked = match_linked && (TS->font_get_baseline_offset(cache[i]) == p_baseline_offset);
2373
for (const Variant *V = supported_coords.next(nullptr); V && match; V = supported_coords.next(V)) {
2374
const Vector3 &def = supported_coords[*V];
2375
2376
real_t c_v = def.z;
2377
if (cache_var.has(*V)) {
2378
real_t val = cache_var[*V];
2379
c_v = CLAMP(val, def.x, def.y);
2380
}
2381
if (cache_var.has(TS->tag_to_name(*V))) {
2382
real_t val = cache_var[TS->tag_to_name(*V)];
2383
c_v = CLAMP(val, def.x, def.y);
2384
}
2385
2386
real_t s_v = def.z;
2387
if (p_variation_coordinates.has(*V)) {
2388
real_t val = p_variation_coordinates[*V];
2389
s_v = CLAMP(val, def.x, def.y);
2390
}
2391
if (p_variation_coordinates.has(TS->tag_to_name(*V))) {
2392
real_t val = p_variation_coordinates[TS->tag_to_name(*V)];
2393
s_v = CLAMP(val, def.x, def.y);
2394
}
2395
2396
match = match && (c_v == s_v);
2397
}
2398
if (match) {
2399
if (match_linked) {
2400
return cache[i];
2401
} else {
2402
make_linked_from = i;
2403
}
2404
}
2405
}
2406
}
2407
2408
// Create new variation cache.
2409
int idx = cache.size();
2410
if (make_linked_from >= 0) {
2411
_ensure_rid(idx, make_linked_from);
2412
TS->font_set_spacing(cache[idx], TextServer::SPACING_TOP, p_spacing_top);
2413
TS->font_set_spacing(cache[idx], TextServer::SPACING_BOTTOM, p_spacing_bottom);
2414
TS->font_set_spacing(cache[idx], TextServer::SPACING_SPACE, p_spacing_space);
2415
TS->font_set_spacing(cache[idx], TextServer::SPACING_GLYPH, p_spacing_glyph);
2416
TS->font_set_baseline_offset(cache[idx], p_baseline_offset);
2417
} else {
2418
_ensure_rid(idx);
2419
TS->font_set_variation_coordinates(cache[idx], p_variation_coordinates);
2420
TS->font_set_face_index(cache[idx], p_face_index);
2421
TS->font_set_embolden(cache[idx], p_strength);
2422
TS->font_set_transform(cache[idx], p_transform);
2423
TS->font_set_spacing(cache[idx], TextServer::SPACING_TOP, p_spacing_top);
2424
TS->font_set_spacing(cache[idx], TextServer::SPACING_BOTTOM, p_spacing_bottom);
2425
TS->font_set_spacing(cache[idx], TextServer::SPACING_SPACE, p_spacing_space);
2426
TS->font_set_spacing(cache[idx], TextServer::SPACING_GLYPH, p_spacing_glyph);
2427
TS->font_set_baseline_offset(cache[idx], p_baseline_offset);
2428
}
2429
return cache[idx];
2430
}
2431
2432
RID FontFile::_get_rid() const {
2433
_ensure_rid(0);
2434
return cache[0];
2435
}
2436
2437
int FontFile::get_cache_count() const {
2438
return cache.size();
2439
}
2440
2441
void FontFile::clear_cache() {
2442
_clear_cache();
2443
cache.clear();
2444
emit_changed();
2445
}
2446
2447
void FontFile::remove_cache(int p_cache_index) {
2448
ERR_FAIL_INDEX(p_cache_index, cache.size());
2449
if (cache[p_cache_index].is_valid()) {
2450
TS->free_rid(cache.write[p_cache_index]);
2451
}
2452
cache.remove_at(p_cache_index);
2453
emit_changed();
2454
}
2455
2456
TypedArray<Vector2i> FontFile::get_size_cache_list(int p_cache_index) const {
2457
ERR_FAIL_COND_V(p_cache_index < 0, Array());
2458
_ensure_rid(p_cache_index);
2459
return TS->font_get_size_cache_list(cache[p_cache_index]);
2460
}
2461
2462
void FontFile::clear_size_cache(int p_cache_index) {
2463
ERR_FAIL_COND(p_cache_index < 0);
2464
_ensure_rid(p_cache_index);
2465
TS->font_clear_size_cache(cache[p_cache_index]);
2466
}
2467
2468
void FontFile::remove_size_cache(int p_cache_index, const Vector2i &p_size) {
2469
ERR_FAIL_COND(p_cache_index < 0);
2470
_ensure_rid(p_cache_index);
2471
TS->font_remove_size_cache(cache[p_cache_index], p_size);
2472
}
2473
2474
void FontFile::set_variation_coordinates(int p_cache_index, const Dictionary &p_variation_coordinates) {
2475
ERR_FAIL_COND(p_cache_index < 0);
2476
_ensure_rid(p_cache_index);
2477
TS->font_set_variation_coordinates(cache[p_cache_index], p_variation_coordinates);
2478
}
2479
2480
Dictionary FontFile::get_variation_coordinates(int p_cache_index) const {
2481
ERR_FAIL_COND_V(p_cache_index < 0, Dictionary());
2482
_ensure_rid(p_cache_index);
2483
return TS->font_get_variation_coordinates(cache[p_cache_index]);
2484
}
2485
2486
void FontFile::set_embolden(int p_cache_index, float p_strength) {
2487
ERR_FAIL_COND(p_cache_index < 0);
2488
_ensure_rid(p_cache_index);
2489
TS->font_set_embolden(cache[p_cache_index], p_strength);
2490
}
2491
2492
float FontFile::get_embolden(int p_cache_index) const {
2493
ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
2494
_ensure_rid(p_cache_index);
2495
return TS->font_get_embolden(cache[p_cache_index]);
2496
}
2497
2498
void FontFile::set_transform(int p_cache_index, Transform2D p_transform) {
2499
ERR_FAIL_COND(p_cache_index < 0);
2500
_ensure_rid(p_cache_index);
2501
TS->font_set_transform(cache[p_cache_index], p_transform);
2502
}
2503
2504
Transform2D FontFile::get_transform(int p_cache_index) const {
2505
ERR_FAIL_COND_V(p_cache_index < 0, Transform2D());
2506
_ensure_rid(p_cache_index);
2507
return TS->font_get_transform(cache[p_cache_index]);
2508
}
2509
2510
void FontFile::set_extra_spacing(int p_cache_index, TextServer::SpacingType p_spacing, int64_t p_value) {
2511
ERR_FAIL_COND(p_cache_index < 0);
2512
_ensure_rid(p_cache_index);
2513
TS->font_set_spacing(cache[p_cache_index], p_spacing, p_value);
2514
}
2515
2516
int64_t FontFile::get_extra_spacing(int p_cache_index, TextServer::SpacingType p_spacing) const {
2517
ERR_FAIL_COND_V(p_cache_index < 0, 0);
2518
_ensure_rid(p_cache_index);
2519
return TS->font_get_spacing(cache[p_cache_index], p_spacing);
2520
}
2521
2522
float FontFile::get_extra_baseline_offset(int p_cache_index) const {
2523
ERR_FAIL_COND_V(p_cache_index < 0, 0);
2524
_ensure_rid(p_cache_index);
2525
return TS->font_get_baseline_offset(cache[p_cache_index]);
2526
}
2527
2528
void FontFile::set_extra_baseline_offset(int p_cache_index, float p_baseline_offset) {
2529
ERR_FAIL_COND(p_cache_index < 0);
2530
_ensure_rid(p_cache_index);
2531
TS->font_set_baseline_offset(cache[p_cache_index], p_baseline_offset);
2532
}
2533
2534
void FontFile::set_face_index(int p_cache_index, int64_t p_index) {
2535
ERR_FAIL_COND(p_cache_index < 0);
2536
ERR_FAIL_COND(p_index < 0);
2537
ERR_FAIL_COND(p_index >= 0x7FFF);
2538
2539
_ensure_rid(p_cache_index);
2540
TS->font_set_face_index(cache[p_cache_index], p_index);
2541
}
2542
2543
int64_t FontFile::get_face_index(int p_cache_index) const {
2544
ERR_FAIL_COND_V(p_cache_index < 0, 0);
2545
_ensure_rid(p_cache_index);
2546
return TS->font_get_face_index(cache[p_cache_index]);
2547
}
2548
2549
void FontFile::set_cache_ascent(int p_cache_index, int p_size, real_t p_ascent) {
2550
ERR_FAIL_COND(p_cache_index < 0);
2551
_ensure_rid(p_cache_index);
2552
TS->font_set_ascent(cache[p_cache_index], p_size, p_ascent);
2553
}
2554
2555
real_t FontFile::get_cache_ascent(int p_cache_index, int p_size) const {
2556
ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
2557
_ensure_rid(p_cache_index);
2558
return TS->font_get_ascent(cache[p_cache_index], p_size);
2559
}
2560
2561
void FontFile::set_cache_descent(int p_cache_index, int p_size, real_t p_descent) {
2562
ERR_FAIL_COND(p_cache_index < 0);
2563
_ensure_rid(p_cache_index);
2564
TS->font_set_descent(cache[p_cache_index], p_size, p_descent);
2565
}
2566
2567
real_t FontFile::get_cache_descent(int p_cache_index, int p_size) const {
2568
ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
2569
_ensure_rid(p_cache_index);
2570
return TS->font_get_descent(cache[p_cache_index], p_size);
2571
}
2572
2573
void FontFile::set_cache_underline_position(int p_cache_index, int p_size, real_t p_underline_position) {
2574
ERR_FAIL_COND(p_cache_index < 0);
2575
_ensure_rid(p_cache_index);
2576
TS->font_set_underline_position(cache[p_cache_index], p_size, p_underline_position);
2577
}
2578
2579
real_t FontFile::get_cache_underline_position(int p_cache_index, int p_size) const {
2580
ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
2581
_ensure_rid(p_cache_index);
2582
return TS->font_get_underline_position(cache[p_cache_index], p_size);
2583
}
2584
2585
void FontFile::set_cache_underline_thickness(int p_cache_index, int p_size, real_t p_underline_thickness) {
2586
ERR_FAIL_COND(p_cache_index < 0);
2587
_ensure_rid(p_cache_index);
2588
TS->font_set_underline_thickness(cache[p_cache_index], p_size, p_underline_thickness);
2589
}
2590
2591
real_t FontFile::get_cache_underline_thickness(int p_cache_index, int p_size) const {
2592
ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
2593
_ensure_rid(p_cache_index);
2594
return TS->font_get_underline_thickness(cache[p_cache_index], p_size);
2595
}
2596
2597
void FontFile::set_cache_scale(int p_cache_index, int p_size, real_t p_scale) {
2598
ERR_FAIL_COND(p_cache_index < 0);
2599
_ensure_rid(p_cache_index);
2600
TS->font_set_scale(cache[p_cache_index], p_size, p_scale);
2601
}
2602
2603
real_t FontFile::get_cache_scale(int p_cache_index, int p_size) const {
2604
ERR_FAIL_COND_V(p_cache_index < 0, 0.f);
2605
_ensure_rid(p_cache_index);
2606
return TS->font_get_scale(cache[p_cache_index], p_size);
2607
}
2608
2609
int FontFile::get_texture_count(int p_cache_index, const Vector2i &p_size) const {
2610
ERR_FAIL_COND_V(p_cache_index < 0, 0);
2611
_ensure_rid(p_cache_index);
2612
return TS->font_get_texture_count(cache[p_cache_index], p_size);
2613
}
2614
2615
void FontFile::clear_textures(int p_cache_index, const Vector2i &p_size) {
2616
ERR_FAIL_COND(p_cache_index < 0);
2617
_ensure_rid(p_cache_index);
2618
TS->font_clear_textures(cache[p_cache_index], p_size);
2619
}
2620
2621
void FontFile::remove_texture(int p_cache_index, const Vector2i &p_size, int p_texture_index) {
2622
ERR_FAIL_COND(p_cache_index < 0);
2623
_ensure_rid(p_cache_index);
2624
TS->font_remove_texture(cache[p_cache_index], p_size, p_texture_index);
2625
}
2626
2627
void FontFile::set_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index, const Ref<Image> &p_image) {
2628
ERR_FAIL_COND(p_cache_index < 0);
2629
_ensure_rid(p_cache_index);
2630
TS->font_set_texture_image(cache[p_cache_index], p_size, p_texture_index, p_image);
2631
}
2632
2633
Ref<Image> FontFile::get_texture_image(int p_cache_index, const Vector2i &p_size, int p_texture_index) const {
2634
ERR_FAIL_COND_V(p_cache_index < 0, Ref<Image>());
2635
_ensure_rid(p_cache_index);
2636
return TS->font_get_texture_image(cache[p_cache_index], p_size, p_texture_index);
2637
}
2638
2639
void FontFile::set_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index, const PackedInt32Array &p_offset) {
2640
ERR_FAIL_COND(p_cache_index < 0);
2641
_ensure_rid(p_cache_index);
2642
TS->font_set_texture_offsets(cache[p_cache_index], p_size, p_texture_index, p_offset);
2643
}
2644
2645
PackedInt32Array FontFile::get_texture_offsets(int p_cache_index, const Vector2i &p_size, int p_texture_index) const {
2646
ERR_FAIL_COND_V(p_cache_index < 0, PackedInt32Array());
2647
_ensure_rid(p_cache_index);
2648
return TS->font_get_texture_offsets(cache[p_cache_index], p_size, p_texture_index);
2649
}
2650
2651
PackedInt32Array FontFile::get_glyph_list(int p_cache_index, const Vector2i &p_size) const {
2652
ERR_FAIL_COND_V(p_cache_index < 0, PackedInt32Array());
2653
_ensure_rid(p_cache_index);
2654
return TS->font_get_glyph_list(cache[p_cache_index], p_size);
2655
}
2656
2657
void FontFile::clear_glyphs(int p_cache_index, const Vector2i &p_size) {
2658
ERR_FAIL_COND(p_cache_index < 0);
2659
_ensure_rid(p_cache_index);
2660
TS->font_clear_glyphs(cache[p_cache_index], p_size);
2661
}
2662
2663
void FontFile::remove_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) {
2664
ERR_FAIL_COND(p_cache_index < 0);
2665
_ensure_rid(p_cache_index);
2666
TS->font_remove_glyph(cache[p_cache_index], p_size, p_glyph);
2667
}
2668
2669
void FontFile::set_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph, const Vector2 &p_advance) {
2670
ERR_FAIL_COND(p_cache_index < 0);
2671
_ensure_rid(p_cache_index);
2672
TS->font_set_glyph_advance(cache[p_cache_index], p_size, p_glyph, p_advance);
2673
}
2674
2675
Vector2 FontFile::get_glyph_advance(int p_cache_index, int p_size, int32_t p_glyph) const {
2676
ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
2677
_ensure_rid(p_cache_index);
2678
return TS->font_get_glyph_advance(cache[p_cache_index], p_size, p_glyph);
2679
}
2680
2681
void FontFile::set_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_offset) {
2682
ERR_FAIL_COND(p_cache_index < 0);
2683
_ensure_rid(p_cache_index);
2684
TS->font_set_glyph_offset(cache[p_cache_index], p_size, p_glyph, p_offset);
2685
}
2686
2687
Vector2 FontFile::get_glyph_offset(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
2688
ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
2689
_ensure_rid(p_cache_index);
2690
return TS->font_get_glyph_offset(cache[p_cache_index], p_size, p_glyph);
2691
}
2692
2693
void FontFile::set_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Vector2 &p_gl_size) {
2694
ERR_FAIL_COND(p_cache_index < 0);
2695
_ensure_rid(p_cache_index);
2696
TS->font_set_glyph_size(cache[p_cache_index], p_size, p_glyph, p_gl_size);
2697
}
2698
2699
Vector2 FontFile::get_glyph_size(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
2700
ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
2701
_ensure_rid(p_cache_index);
2702
return TS->font_get_glyph_size(cache[p_cache_index], p_size, p_glyph);
2703
}
2704
2705
void FontFile::set_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, const Rect2 &p_uv_rect) {
2706
ERR_FAIL_COND(p_cache_index < 0);
2707
_ensure_rid(p_cache_index);
2708
TS->font_set_glyph_uv_rect(cache[p_cache_index], p_size, p_glyph, p_uv_rect);
2709
}
2710
2711
Rect2 FontFile::get_glyph_uv_rect(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
2712
ERR_FAIL_COND_V(p_cache_index < 0, Rect2());
2713
_ensure_rid(p_cache_index);
2714
return TS->font_get_glyph_uv_rect(cache[p_cache_index], p_size, p_glyph);
2715
}
2716
2717
void FontFile::set_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph, int p_texture_idx) {
2718
ERR_FAIL_COND(p_cache_index < 0);
2719
_ensure_rid(p_cache_index);
2720
TS->font_set_glyph_texture_idx(cache[p_cache_index], p_size, p_glyph, p_texture_idx);
2721
}
2722
2723
int FontFile::get_glyph_texture_idx(int p_cache_index, const Vector2i &p_size, int32_t p_glyph) const {
2724
ERR_FAIL_COND_V(p_cache_index < 0, 0);
2725
_ensure_rid(p_cache_index);
2726
return TS->font_get_glyph_texture_idx(cache[p_cache_index], p_size, p_glyph);
2727
}
2728
2729
TypedArray<Vector2i> FontFile::get_kerning_list(int p_cache_index, int p_size) const {
2730
ERR_FAIL_COND_V(p_cache_index < 0, Array());
2731
_ensure_rid(p_cache_index);
2732
return TS->font_get_kerning_list(cache[p_cache_index], p_size);
2733
}
2734
2735
void FontFile::clear_kerning_map(int p_cache_index, int p_size) {
2736
ERR_FAIL_COND(p_cache_index < 0);
2737
_ensure_rid(p_cache_index);
2738
TS->font_clear_kerning_map(cache[p_cache_index], p_size);
2739
}
2740
2741
void FontFile::remove_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair) {
2742
ERR_FAIL_COND(p_cache_index < 0);
2743
_ensure_rid(p_cache_index);
2744
TS->font_remove_kerning(cache[p_cache_index], p_size, p_glyph_pair);
2745
}
2746
2747
void FontFile::set_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair, const Vector2 &p_kerning) {
2748
ERR_FAIL_COND(p_cache_index < 0);
2749
_ensure_rid(p_cache_index);
2750
TS->font_set_kerning(cache[p_cache_index], p_size, p_glyph_pair, p_kerning);
2751
}
2752
2753
Vector2 FontFile::get_kerning(int p_cache_index, int p_size, const Vector2i &p_glyph_pair) const {
2754
ERR_FAIL_COND_V(p_cache_index < 0, Vector2());
2755
_ensure_rid(p_cache_index);
2756
return TS->font_get_kerning(cache[p_cache_index], p_size, p_glyph_pair);
2757
}
2758
2759
void FontFile::render_range(int p_cache_index, const Vector2i &p_size, char32_t p_start, char32_t p_end) {
2760
ERR_FAIL_COND(p_cache_index < 0);
2761
_ensure_rid(p_cache_index);
2762
TS->font_render_range(cache[p_cache_index], p_size, p_start, p_end);
2763
}
2764
2765
void FontFile::render_glyph(int p_cache_index, const Vector2i &p_size, int32_t p_index) {
2766
ERR_FAIL_COND(p_cache_index < 0);
2767
_ensure_rid(p_cache_index);
2768
TS->font_render_glyph(cache[p_cache_index], p_size, p_index);
2769
}
2770
2771
void FontFile::set_language_support_override(const String &p_language, bool p_supported) {
2772
_ensure_rid(0);
2773
TS->font_set_language_support_override(cache[0], p_language, p_supported);
2774
}
2775
2776
bool FontFile::get_language_support_override(const String &p_language) const {
2777
_ensure_rid(0);
2778
return TS->font_get_language_support_override(cache[0], p_language);
2779
}
2780
2781
void FontFile::remove_language_support_override(const String &p_language) {
2782
_ensure_rid(0);
2783
TS->font_remove_language_support_override(cache[0], p_language);
2784
}
2785
2786
Vector<String> FontFile::get_language_support_overrides() const {
2787
_ensure_rid(0);
2788
return TS->font_get_language_support_overrides(cache[0]);
2789
}
2790
2791
void FontFile::set_script_support_override(const String &p_script, bool p_supported) {
2792
_ensure_rid(0);
2793
TS->font_set_script_support_override(cache[0], p_script, p_supported);
2794
}
2795
2796
bool FontFile::get_script_support_override(const String &p_script) const {
2797
_ensure_rid(0);
2798
return TS->font_get_script_support_override(cache[0], p_script);
2799
}
2800
2801
void FontFile::remove_script_support_override(const String &p_script) {
2802
_ensure_rid(0);
2803
TS->font_remove_script_support_override(cache[0], p_script);
2804
}
2805
2806
Vector<String> FontFile::get_script_support_overrides() const {
2807
_ensure_rid(0);
2808
return TS->font_get_script_support_overrides(cache[0]);
2809
}
2810
2811
void FontFile::set_opentype_feature_overrides(const Dictionary &p_overrides) {
2812
_ensure_rid(0);
2813
TS->font_set_opentype_feature_overrides(cache[0], p_overrides);
2814
}
2815
2816
Dictionary FontFile::get_opentype_feature_overrides() const {
2817
_ensure_rid(0);
2818
return TS->font_get_opentype_feature_overrides(cache[0]);
2819
}
2820
2821
int32_t FontFile::get_glyph_index(int p_size, char32_t p_char, char32_t p_variation_selector) const {
2822
_ensure_rid(0);
2823
return TS->font_get_glyph_index(cache[0], p_size, p_char, p_variation_selector);
2824
}
2825
2826
char32_t FontFile::get_char_from_glyph_index(int p_size, int32_t p_glyph_index) const {
2827
_ensure_rid(0);
2828
return TS->font_get_char_from_glyph_index(cache[0], p_size, p_glyph_index);
2829
}
2830
2831
FontFile::FontFile() {
2832
}
2833
2834
FontFile::~FontFile() {
2835
_clear_cache();
2836
}
2837
2838
/*************************************************************************/
2839
/* FontVariation */
2840
/*************************************************************************/
2841
2842
void FontVariation::_bind_methods() {
2843
ClassDB::bind_method(D_METHOD("set_base_font", "font"), &FontVariation::set_base_font);
2844
ClassDB::bind_method(D_METHOD("get_base_font"), &FontVariation::get_base_font);
2845
2846
ClassDB::bind_method(D_METHOD("set_variation_opentype", "coords"), &FontVariation::set_variation_opentype);
2847
ClassDB::bind_method(D_METHOD("get_variation_opentype"), &FontVariation::get_variation_opentype);
2848
2849
ClassDB::bind_method(D_METHOD("set_variation_embolden", "strength"), &FontVariation::set_variation_embolden);
2850
ClassDB::bind_method(D_METHOD("get_variation_embolden"), &FontVariation::get_variation_embolden);
2851
2852
ClassDB::bind_method(D_METHOD("set_variation_face_index", "face_index"), &FontVariation::set_variation_face_index);
2853
ClassDB::bind_method(D_METHOD("get_variation_face_index"), &FontVariation::get_variation_face_index);
2854
2855
ClassDB::bind_method(D_METHOD("set_variation_transform", "transform"), &FontVariation::set_variation_transform);
2856
ClassDB::bind_method(D_METHOD("get_variation_transform"), &FontVariation::get_variation_transform);
2857
2858
ClassDB::bind_method(D_METHOD("set_opentype_features", "features"), &FontVariation::set_opentype_features);
2859
2860
ClassDB::bind_method(D_METHOD("set_spacing", "spacing", "value"), &FontVariation::set_spacing);
2861
2862
ClassDB::bind_method(D_METHOD("set_baseline_offset", "baseline_offset"), &FontVariation::set_baseline_offset);
2863
ClassDB::bind_method(D_METHOD("get_baseline_offset"), &FontVariation::get_baseline_offset);
2864
2865
ADD_PROPERTY(PropertyInfo(Variant::OBJECT, "base_font", PROPERTY_HINT_RESOURCE_TYPE, "Font"), "set_base_font", "get_base_font");
2866
2867
ADD_GROUP("Variation", "variation_");
2868
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "variation_opentype"), "set_variation_opentype", "get_variation_opentype");
2869
ADD_PROPERTY(PropertyInfo(Variant::INT, "variation_face_index", PROPERTY_HINT_RANGE, "0,32767,1"), "set_variation_face_index", "get_variation_face_index");
2870
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "variation_embolden", PROPERTY_HINT_RANGE, "-2,2,0.01"), "set_variation_embolden", "get_variation_embolden");
2871
ADD_PROPERTY(PropertyInfo(Variant::TRANSFORM2D, "variation_transform", PROPERTY_HINT_NONE, "suffix:px"), "set_variation_transform", "get_variation_transform");
2872
2873
ADD_GROUP("OpenType Features", "opentype_");
2874
ADD_PROPERTY(PropertyInfo(Variant::DICTIONARY, "opentype_features"), "set_opentype_features", "get_opentype_features");
2875
2876
ADD_GROUP("Extra Spacing", "spacing_");
2877
ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_glyph", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_GLYPH);
2878
ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_space", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_SPACE);
2879
ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_top", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_TOP);
2880
ADD_PROPERTYI(PropertyInfo(Variant::INT, "spacing_bottom", PROPERTY_HINT_NONE, "suffix:px"), "set_spacing", "get_spacing", TextServer::SPACING_BOTTOM);
2881
2882
ADD_GROUP("Baseline", "baseline_");
2883
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "baseline_offset", PROPERTY_HINT_RANGE, "-2,2,0.005"), "set_baseline_offset", "get_baseline_offset");
2884
}
2885
2886
void FontVariation::_update_rids() const {
2887
Ref<Font> f = _get_base_font_or_default();
2888
2889
rids.clear();
2890
if (fallbacks.is_empty() && f.is_valid()) {
2891
RID rid = _get_rid();
2892
if (rid.is_valid()) {
2893
rids.push_back(rid);
2894
}
2895
2896
const TypedArray<Font> &base_fallbacks = f->get_fallbacks();
2897
for (int i = 0; i < base_fallbacks.size(); i++) {
2898
Ref<Font> fb_font = base_fallbacks[i];
2899
_update_rids_fb(fb_font.ptr(), 0);
2900
}
2901
} else {
2902
_update_rids_fb(this, 0);
2903
}
2904
dirty_rids = false;
2905
}
2906
2907
void FontVariation::reset_state() {
2908
if (base_font.is_valid()) {
2909
base_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
2910
base_font.unref();
2911
}
2912
2913
if (theme_font.is_valid()) {
2914
theme_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
2915
theme_font.unref();
2916
}
2917
2918
variation = Variation();
2919
opentype_features = Dictionary();
2920
2921
for (int i = 0; i < TextServer::SPACING_MAX; i++) {
2922
extra_spacing[i] = 0;
2923
}
2924
baseline_offset = 0.0;
2925
2926
Font::reset_state();
2927
}
2928
2929
void FontVariation::set_base_font(const Ref<Font> &p_font) {
2930
if (base_font != p_font) {
2931
if (base_font.is_valid()) {
2932
base_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
2933
}
2934
base_font = p_font;
2935
if (base_font.is_valid()) {
2936
base_font->connect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
2937
}
2938
_invalidate_rids();
2939
notify_property_list_changed();
2940
}
2941
}
2942
2943
Ref<Font> FontVariation::get_base_font() const {
2944
return base_font;
2945
}
2946
2947
Ref<Font> FontVariation::_get_base_font_or_default() const {
2948
if (theme_font.is_valid()) {
2949
theme_font->disconnect_changed(callable_mp(static_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids));
2950
theme_font.unref();
2951
}
2952
2953
if (base_font.is_valid()) {
2954
return base_font;
2955
}
2956
2957
StringName theme_name = "font";
2958
Vector<StringName> theme_types;
2959
ThemeDB::get_singleton()->get_native_type_dependencies(get_class_name(), theme_types);
2960
2961
ThemeContext *global_context = ThemeDB::get_singleton()->get_default_theme_context();
2962
Vector<Ref<Theme>> themes = global_context->get_themes();
2963
if (Engine::get_singleton()->is_editor_hint()) {
2964
themes.insert(0, ThemeDB::get_singleton()->get_project_theme());
2965
}
2966
2967
for (const Ref<Theme> &theme : themes) {
2968
if (theme.is_null()) {
2969
continue;
2970
}
2971
2972
for (const StringName &E : theme_types) {
2973
if (!theme->has_font(theme_name, E)) {
2974
continue;
2975
}
2976
2977
Ref<Font> f = theme->get_font(theme_name, E);
2978
if (_is_base_cyclic(f, 0)) {
2979
continue;
2980
}
2981
if (f.is_valid()) {
2982
theme_font = f;
2983
theme_font->connect_changed(callable_mp(static_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
2984
}
2985
return f;
2986
}
2987
}
2988
2989
Ref<Font> f = global_context->get_fallback_theme()->get_font(theme_name, StringName());
2990
if (!_is_base_cyclic(f, 0)) {
2991
if (f.is_valid()) {
2992
theme_font = f;
2993
theme_font->connect_changed(callable_mp(static_cast<Font *>(const_cast<FontVariation *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
2994
}
2995
return f;
2996
}
2997
2998
return Ref<Font>();
2999
}
3000
3001
void FontVariation::set_variation_opentype(const Dictionary &p_coords) {
3002
if (!variation.opentype.recursive_equal(p_coords, 1)) {
3003
variation.opentype = p_coords.duplicate();
3004
_invalidate_rids();
3005
}
3006
}
3007
3008
Dictionary FontVariation::get_variation_opentype() const {
3009
return variation.opentype.duplicate();
3010
}
3011
3012
void FontVariation::set_variation_embolden(float p_strength) {
3013
if (variation.embolden != p_strength) {
3014
variation.embolden = p_strength;
3015
_invalidate_rids();
3016
}
3017
}
3018
3019
float FontVariation::get_variation_embolden() const {
3020
return variation.embolden;
3021
}
3022
3023
void FontVariation::set_variation_transform(Transform2D p_transform) {
3024
if (variation.transform != p_transform) {
3025
variation.transform = p_transform;
3026
_invalidate_rids();
3027
}
3028
}
3029
3030
Transform2D FontVariation::get_variation_transform() const {
3031
return variation.transform;
3032
}
3033
3034
void FontVariation::set_variation_face_index(int p_face_index) {
3035
if (variation.face_index != p_face_index) {
3036
variation.face_index = p_face_index;
3037
_invalidate_rids();
3038
}
3039
}
3040
3041
int FontVariation::get_variation_face_index() const {
3042
return variation.face_index;
3043
}
3044
3045
void FontVariation::set_opentype_features(const Dictionary &p_features) {
3046
if (!opentype_features.recursive_equal(p_features, 1)) {
3047
opentype_features = p_features.duplicate();
3048
_invalidate_rids();
3049
}
3050
}
3051
3052
Dictionary FontVariation::get_opentype_features() const {
3053
return opentype_features.duplicate();
3054
}
3055
3056
void FontVariation::set_spacing(TextServer::SpacingType p_spacing, int p_value) {
3057
ERR_FAIL_INDEX((int)p_spacing, TextServer::SPACING_MAX);
3058
if (extra_spacing[p_spacing] != p_value) {
3059
extra_spacing[p_spacing] = p_value;
3060
_invalidate_rids();
3061
}
3062
}
3063
3064
int FontVariation::get_spacing(TextServer::SpacingType p_spacing) const {
3065
ERR_FAIL_INDEX_V((int)p_spacing, TextServer::SPACING_MAX, 0);
3066
return extra_spacing[p_spacing];
3067
}
3068
3069
void FontVariation::set_baseline_offset(float p_baseline_offset) {
3070
if (baseline_offset != p_baseline_offset) {
3071
baseline_offset = p_baseline_offset;
3072
_invalidate_rids();
3073
}
3074
}
3075
3076
float FontVariation::get_baseline_offset() const {
3077
return baseline_offset;
3078
}
3079
3080
RID FontVariation::find_variation(const Dictionary &p_variation_coordinates, int p_face_index, float p_strength, Transform2D p_transform, int p_spacing_top, int p_spacing_bottom, int p_spacing_space, int p_spacing_glyph, float p_baseline_offset) const {
3081
Ref<Font> f = _get_base_font_or_default();
3082
if (f.is_valid()) {
3083
return f->find_variation(p_variation_coordinates, p_face_index, p_strength, p_transform, p_spacing_top, p_spacing_bottom, p_spacing_space, p_spacing_glyph, p_baseline_offset);
3084
}
3085
return RID();
3086
}
3087
3088
RID FontVariation::_get_rid() const {
3089
Ref<Font> f = _get_base_font_or_default();
3090
if (f.is_valid()) {
3091
return f->find_variation(variation.opentype, variation.face_index, variation.embolden, variation.transform, extra_spacing[TextServer::SPACING_TOP], extra_spacing[TextServer::SPACING_BOTTOM], extra_spacing[TextServer::SPACING_SPACE], extra_spacing[TextServer::SPACING_GLYPH], baseline_offset);
3092
}
3093
return RID();
3094
}
3095
3096
FontVariation::FontVariation() {
3097
for (int i = 0; i < TextServer::SPACING_MAX; i++) {
3098
extra_spacing[i] = 0;
3099
}
3100
}
3101
3102
FontVariation::~FontVariation() {
3103
}
3104
3105
/*************************************************************************/
3106
/* SystemFont */
3107
/*************************************************************************/
3108
3109
void SystemFont::_bind_methods() {
3110
ClassDB::bind_method(D_METHOD("set_antialiasing", "antialiasing"), &SystemFont::set_antialiasing);
3111
ClassDB::bind_method(D_METHOD("get_antialiasing"), &SystemFont::get_antialiasing);
3112
3113
ClassDB::bind_method(D_METHOD("set_disable_embedded_bitmaps", "disable_embedded_bitmaps"), &SystemFont::set_disable_embedded_bitmaps);
3114
ClassDB::bind_method(D_METHOD("get_disable_embedded_bitmaps"), &SystemFont::get_disable_embedded_bitmaps);
3115
3116
ClassDB::bind_method(D_METHOD("set_generate_mipmaps", "generate_mipmaps"), &SystemFont::set_generate_mipmaps);
3117
ClassDB::bind_method(D_METHOD("get_generate_mipmaps"), &SystemFont::get_generate_mipmaps);
3118
3119
ClassDB::bind_method(D_METHOD("set_allow_system_fallback", "allow_system_fallback"), &SystemFont::set_allow_system_fallback);
3120
ClassDB::bind_method(D_METHOD("is_allow_system_fallback"), &SystemFont::is_allow_system_fallback);
3121
3122
ClassDB::bind_method(D_METHOD("set_force_autohinter", "force_autohinter"), &SystemFont::set_force_autohinter);
3123
ClassDB::bind_method(D_METHOD("is_force_autohinter"), &SystemFont::is_force_autohinter);
3124
3125
ClassDB::bind_method(D_METHOD("set_modulate_color_glyphs", "modulate"), &SystemFont::set_modulate_color_glyphs);
3126
ClassDB::bind_method(D_METHOD("is_modulate_color_glyphs"), &SystemFont::is_modulate_color_glyphs);
3127
3128
ClassDB::bind_method(D_METHOD("set_hinting", "hinting"), &SystemFont::set_hinting);
3129
ClassDB::bind_method(D_METHOD("get_hinting"), &SystemFont::get_hinting);
3130
3131
ClassDB::bind_method(D_METHOD("set_subpixel_positioning", "subpixel_positioning"), &SystemFont::set_subpixel_positioning);
3132
ClassDB::bind_method(D_METHOD("get_subpixel_positioning"), &SystemFont::get_subpixel_positioning);
3133
3134
ClassDB::bind_method(D_METHOD("set_keep_rounding_remainders", "keep_rounding_remainders"), &SystemFont::set_keep_rounding_remainders);
3135
ClassDB::bind_method(D_METHOD("get_keep_rounding_remainders"), &SystemFont::get_keep_rounding_remainders);
3136
3137
ClassDB::bind_method(D_METHOD("set_multichannel_signed_distance_field", "msdf"), &SystemFont::set_multichannel_signed_distance_field);
3138
ClassDB::bind_method(D_METHOD("is_multichannel_signed_distance_field"), &SystemFont::is_multichannel_signed_distance_field);
3139
3140
ClassDB::bind_method(D_METHOD("set_msdf_pixel_range", "msdf_pixel_range"), &SystemFont::set_msdf_pixel_range);
3141
ClassDB::bind_method(D_METHOD("get_msdf_pixel_range"), &SystemFont::get_msdf_pixel_range);
3142
3143
ClassDB::bind_method(D_METHOD("set_msdf_size", "msdf_size"), &SystemFont::set_msdf_size);
3144
ClassDB::bind_method(D_METHOD("get_msdf_size"), &SystemFont::get_msdf_size);
3145
3146
ClassDB::bind_method(D_METHOD("set_oversampling", "oversampling"), &SystemFont::set_oversampling);
3147
ClassDB::bind_method(D_METHOD("get_oversampling"), &SystemFont::get_oversampling);
3148
3149
ClassDB::bind_method(D_METHOD("get_font_names"), &SystemFont::get_font_names);
3150
ClassDB::bind_method(D_METHOD("set_font_names", "names"), &SystemFont::set_font_names);
3151
3152
ClassDB::bind_method(D_METHOD("get_font_italic"), &SystemFont::get_font_italic);
3153
ClassDB::bind_method(D_METHOD("set_font_italic", "italic"), &SystemFont::set_font_italic);
3154
ClassDB::bind_method(D_METHOD("set_font_weight", "weight"), &SystemFont::set_font_weight);
3155
ClassDB::bind_method(D_METHOD("set_font_stretch", "stretch"), &SystemFont::set_font_stretch);
3156
3157
ADD_PROPERTY(PropertyInfo(Variant::PACKED_STRING_ARRAY, "font_names"), "set_font_names", "get_font_names");
3158
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "font_italic"), "set_font_italic", "get_font_italic");
3159
ADD_PROPERTY(PropertyInfo(Variant::INT, "font_weight", PROPERTY_HINT_RANGE, "100,999,25"), "set_font_weight", "get_font_weight");
3160
ADD_PROPERTY(PropertyInfo(Variant::INT, "font_stretch", PROPERTY_HINT_RANGE, "50,200,25"), "set_font_stretch", "get_font_stretch");
3161
ADD_PROPERTY(PropertyInfo(Variant::INT, "antialiasing", PROPERTY_HINT_ENUM, "None,Grayscale,LCD Subpixel", PROPERTY_USAGE_STORAGE), "set_antialiasing", "get_antialiasing");
3162
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "generate_mipmaps"), "set_generate_mipmaps", "get_generate_mipmaps");
3163
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "disable_embedded_bitmaps"), "set_disable_embedded_bitmaps", "get_disable_embedded_bitmaps");
3164
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "allow_system_fallback"), "set_allow_system_fallback", "is_allow_system_fallback");
3165
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "force_autohinter"), "set_force_autohinter", "is_force_autohinter");
3166
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "modulate_color_glyphs"), "set_modulate_color_glyphs", "is_modulate_color_glyphs");
3167
ADD_PROPERTY(PropertyInfo(Variant::INT, "hinting", PROPERTY_HINT_ENUM, "None,Light,Normal"), "set_hinting", "get_hinting");
3168
ADD_PROPERTY(PropertyInfo(Variant::INT, "subpixel_positioning", PROPERTY_HINT_ENUM, "Disabled,Auto,One Half of a Pixel,One Quarter of a Pixel"), "set_subpixel_positioning", "get_subpixel_positioning");
3169
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "keep_rounding_remainders"), "set_keep_rounding_remainders", "get_keep_rounding_remainders");
3170
ADD_PROPERTY(PropertyInfo(Variant::BOOL, "multichannel_signed_distance_field"), "set_multichannel_signed_distance_field", "is_multichannel_signed_distance_field");
3171
ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_pixel_range"), "set_msdf_pixel_range", "get_msdf_pixel_range");
3172
ADD_PROPERTY(PropertyInfo(Variant::INT, "msdf_size"), "set_msdf_size", "get_msdf_size");
3173
ADD_PROPERTY(PropertyInfo(Variant::FLOAT, "oversampling"), "set_oversampling", "get_oversampling");
3174
}
3175
3176
void SystemFont::_update_rids() const {
3177
Ref<Font> f = _get_base_font_or_default();
3178
3179
rids.clear();
3180
if (fallbacks.is_empty() && f.is_valid()) {
3181
RID rid = _get_rid();
3182
if (rid.is_valid()) {
3183
rids.push_back(rid);
3184
}
3185
3186
const TypedArray<Font> &base_fallbacks = f->get_fallbacks();
3187
for (int i = 0; i < base_fallbacks.size(); i++) {
3188
Ref<Font> fb_font = base_fallbacks[i];
3189
_update_rids_fb(fb_font.ptr(), 0);
3190
}
3191
} else {
3192
_update_rids_fb(this, 0);
3193
}
3194
dirty_rids = false;
3195
}
3196
3197
void SystemFont::_update_base_font() {
3198
if (base_font.is_valid()) {
3199
base_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
3200
base_font.unref();
3201
}
3202
3203
face_indices.clear();
3204
ftr_weight = 0;
3205
ftr_stretch = 0;
3206
ftr_italic = 0;
3207
for (const String &E : names) {
3208
if (E.is_empty()) {
3209
continue;
3210
}
3211
3212
String path = OS::get_singleton()->get_system_font_path(E, weight, stretch, italic);
3213
if (path.is_empty()) {
3214
continue;
3215
}
3216
Ref<FontFile> file;
3217
file.instantiate();
3218
Error err = file->load_dynamic_font(path);
3219
if (err != OK) {
3220
continue;
3221
}
3222
3223
// If it's a font collection check all faces to match requested style and name.
3224
int best_score = 0;
3225
for (int i = 0; i < file->get_face_count(); i++) {
3226
int score = 0;
3227
file->set_face_index(0, i);
3228
const String n = file->get_font_name();
3229
if (n.to_upper() == E.to_upper()) {
3230
score += 80;
3231
}
3232
BitField<TextServer::FontStyle> style = file->get_font_style();
3233
int font_weight = file->get_font_weight();
3234
int font_stretch = file->get_font_stretch();
3235
score += (20 - Math::abs(font_weight - weight) / 50);
3236
score += (20 - Math::abs(font_stretch - stretch) / 10);
3237
if (bool(style & TextServer::FONT_ITALIC) == italic) {
3238
score += 30;
3239
}
3240
if (score > best_score) {
3241
face_indices.clear();
3242
}
3243
if (score >= best_score) {
3244
best_score = score;
3245
face_indices.push_back(i);
3246
}
3247
}
3248
if (face_indices.is_empty()) {
3249
face_indices.push_back(0);
3250
}
3251
file->set_face_index(0, face_indices[0]);
3252
3253
// If it's a variable font, apply weight, stretch and italic coordinates to match requested style.
3254
if (best_score != 150) {
3255
Dictionary ftr = file->get_supported_variation_list();
3256
if (ftr.has(TS->name_to_tag("width"))) {
3257
ftr_stretch = stretch;
3258
}
3259
if (ftr.has(TS->name_to_tag("weight"))) {
3260
ftr_weight = weight;
3261
}
3262
if (italic && ftr.has(TS->name_to_tag("italic"))) {
3263
ftr_italic = 1;
3264
}
3265
}
3266
3267
// Apply font rendering settings.
3268
file->set_antialiasing(antialiasing);
3269
file->set_generate_mipmaps(mipmaps);
3270
file->set_disable_embedded_bitmaps(disable_embedded_bitmaps);
3271
file->set_force_autohinter(force_autohinter);
3272
file->set_modulate_color_glyphs(modulate_color_glyphs);
3273
file->set_allow_system_fallback(allow_system_fallback);
3274
file->set_hinting(hinting);
3275
file->set_subpixel_positioning(subpixel_positioning);
3276
file->set_keep_rounding_remainders(keep_rounding_remainders);
3277
file->set_oversampling(oversampling_override);
3278
file->set_multichannel_signed_distance_field(msdf);
3279
file->set_msdf_pixel_range(msdf_pixel_range);
3280
file->set_msdf_size(msdf_size);
3281
3282
base_font = file;
3283
3284
break;
3285
}
3286
3287
if (base_font.is_valid()) {
3288
base_font->connect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
3289
}
3290
3291
_invalidate_rids();
3292
}
3293
3294
void SystemFont::reset_state() {
3295
if (base_font.is_valid()) {
3296
base_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
3297
base_font.unref();
3298
}
3299
3300
if (theme_font.is_valid()) {
3301
theme_font->disconnect_changed(callable_mp(reinterpret_cast<Font *>(this), &Font::_invalidate_rids));
3302
theme_font.unref();
3303
}
3304
3305
names.clear();
3306
face_indices.clear();
3307
ftr_weight = 0;
3308
ftr_stretch = 0;
3309
ftr_italic = 0;
3310
italic = false;
3311
weight = 400;
3312
stretch = 100;
3313
antialiasing = TextServer::FONT_ANTIALIASING_GRAY;
3314
mipmaps = false;
3315
disable_embedded_bitmaps = true;
3316
force_autohinter = false;
3317
modulate_color_glyphs = false;
3318
allow_system_fallback = true;
3319
hinting = TextServer::HINTING_LIGHT;
3320
subpixel_positioning = TextServer::SUBPIXEL_POSITIONING_DISABLED;
3321
keep_rounding_remainders = true;
3322
oversampling_override = 0.0;
3323
msdf = false;
3324
3325
Font::reset_state();
3326
}
3327
3328
Ref<Font> SystemFont::_get_base_font_or_default() const {
3329
if (theme_font.is_valid()) {
3330
theme_font->disconnect_changed(callable_mp(static_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids));
3331
theme_font.unref();
3332
}
3333
3334
if (base_font.is_valid()) {
3335
return base_font;
3336
}
3337
3338
StringName theme_name = "font";
3339
Vector<StringName> theme_types;
3340
ThemeDB::get_singleton()->get_native_type_dependencies(get_class_name(), theme_types);
3341
3342
ThemeContext *global_context = ThemeDB::get_singleton()->get_default_theme_context();
3343
for (const Ref<Theme> &theme : global_context->get_themes()) {
3344
if (theme.is_null()) {
3345
continue;
3346
}
3347
3348
for (const StringName &E : theme_types) {
3349
if (!theme->has_font(theme_name, E)) {
3350
continue;
3351
}
3352
3353
Ref<Font> f = theme->get_font(theme_name, E);
3354
if (_is_base_cyclic(f, 0)) {
3355
continue;
3356
}
3357
if (f.is_valid()) {
3358
theme_font = f;
3359
theme_font->connect_changed(callable_mp(static_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
3360
}
3361
return f;
3362
}
3363
}
3364
3365
Ref<Font> f = global_context->get_fallback_theme()->get_font(theme_name, StringName());
3366
if (!_is_base_cyclic(f, 0)) {
3367
if (f.is_valid()) {
3368
theme_font = f;
3369
theme_font->connect_changed(callable_mp(static_cast<Font *>(const_cast<SystemFont *>(this)), &Font::_invalidate_rids), CONNECT_REFERENCE_COUNTED);
3370
}
3371
return f;
3372
}
3373
3374
return Ref<Font>();
3375
}
3376
3377
void SystemFont::set_antialiasing(TextServer::FontAntialiasing p_antialiasing) {
3378
if (antialiasing != p_antialiasing) {
3379
antialiasing = p_antialiasing;
3380
if (base_font.is_valid()) {
3381
base_font->set_antialiasing(antialiasing);
3382
}
3383
emit_changed();
3384
}
3385
}
3386
3387
TextServer::FontAntialiasing SystemFont::get_antialiasing() const {
3388
return antialiasing;
3389
}
3390
3391
void SystemFont::set_disable_embedded_bitmaps(bool p_disable_embedded_bitmaps) {
3392
if (disable_embedded_bitmaps != p_disable_embedded_bitmaps) {
3393
disable_embedded_bitmaps = p_disable_embedded_bitmaps;
3394
if (base_font.is_valid()) {
3395
base_font->set_disable_embedded_bitmaps(disable_embedded_bitmaps);
3396
}
3397
emit_changed();
3398
}
3399
}
3400
3401
bool SystemFont::get_disable_embedded_bitmaps() const {
3402
return disable_embedded_bitmaps;
3403
}
3404
3405
void SystemFont::set_generate_mipmaps(bool p_generate_mipmaps) {
3406
if (mipmaps != p_generate_mipmaps) {
3407
mipmaps = p_generate_mipmaps;
3408
if (base_font.is_valid()) {
3409
base_font->set_generate_mipmaps(mipmaps);
3410
}
3411
emit_changed();
3412
}
3413
}
3414
3415
bool SystemFont::get_generate_mipmaps() const {
3416
return mipmaps;
3417
}
3418
3419
void SystemFont::set_allow_system_fallback(bool p_allow_system_fallback) {
3420
if (allow_system_fallback != p_allow_system_fallback) {
3421
allow_system_fallback = p_allow_system_fallback;
3422
if (base_font.is_valid()) {
3423
base_font->set_allow_system_fallback(allow_system_fallback);
3424
}
3425
emit_changed();
3426
}
3427
}
3428
3429
bool SystemFont::is_allow_system_fallback() const {
3430
return allow_system_fallback;
3431
}
3432
3433
void SystemFont::set_force_autohinter(bool p_force_autohinter) {
3434
if (force_autohinter != p_force_autohinter) {
3435
force_autohinter = p_force_autohinter;
3436
if (base_font.is_valid()) {
3437
base_font->set_force_autohinter(force_autohinter);
3438
}
3439
emit_changed();
3440
}
3441
}
3442
3443
bool SystemFont::is_force_autohinter() const {
3444
return force_autohinter;
3445
}
3446
3447
void SystemFont::set_modulate_color_glyphs(bool p_modulate) {
3448
if (modulate_color_glyphs != p_modulate) {
3449
modulate_color_glyphs = p_modulate;
3450
if (base_font.is_valid()) {
3451
base_font->set_modulate_color_glyphs(modulate_color_glyphs);
3452
}
3453
emit_changed();
3454
}
3455
}
3456
3457
bool SystemFont::is_modulate_color_glyphs() const {
3458
return modulate_color_glyphs;
3459
}
3460
3461
void SystemFont::set_hinting(TextServer::Hinting p_hinting) {
3462
if (hinting != p_hinting) {
3463
hinting = p_hinting;
3464
if (base_font.is_valid()) {
3465
base_font->set_hinting(hinting);
3466
}
3467
emit_changed();
3468
}
3469
}
3470
3471
TextServer::Hinting SystemFont::get_hinting() const {
3472
return hinting;
3473
}
3474
3475
void SystemFont::set_subpixel_positioning(TextServer::SubpixelPositioning p_subpixel) {
3476
if (subpixel_positioning != p_subpixel) {
3477
subpixel_positioning = p_subpixel;
3478
if (base_font.is_valid()) {
3479
base_font->set_subpixel_positioning(subpixel_positioning);
3480
}
3481
emit_changed();
3482
}
3483
}
3484
3485
TextServer::SubpixelPositioning SystemFont::get_subpixel_positioning() const {
3486
return subpixel_positioning;
3487
}
3488
3489
void SystemFont::set_keep_rounding_remainders(bool p_keep_rounding_remainders) {
3490
if (keep_rounding_remainders != p_keep_rounding_remainders) {
3491
keep_rounding_remainders = p_keep_rounding_remainders;
3492
if (base_font.is_valid()) {
3493
base_font->set_keep_rounding_remainders(keep_rounding_remainders);
3494
}
3495
emit_changed();
3496
}
3497
}
3498
3499
bool SystemFont::get_keep_rounding_remainders() const {
3500
return keep_rounding_remainders;
3501
}
3502
3503
void SystemFont::set_oversampling(real_t p_oversampling) {
3504
if (oversampling_override != p_oversampling) {
3505
oversampling_override = p_oversampling;
3506
if (base_font.is_valid()) {
3507
base_font->set_oversampling(oversampling_override);
3508
}
3509
emit_changed();
3510
}
3511
}
3512
3513
real_t SystemFont::get_oversampling() const {
3514
return oversampling_override;
3515
}
3516
3517
void SystemFont::set_multichannel_signed_distance_field(bool p_msdf) {
3518
if (msdf != p_msdf) {
3519
msdf = p_msdf;
3520
if (base_font.is_valid()) {
3521
base_font->set_multichannel_signed_distance_field(msdf);
3522
}
3523
emit_changed();
3524
}
3525
}
3526
3527
bool SystemFont::is_multichannel_signed_distance_field() const {
3528
return msdf;
3529
}
3530
3531
void SystemFont::set_msdf_pixel_range(int p_msdf_pixel_range) {
3532
if (msdf_pixel_range != p_msdf_pixel_range) {
3533
msdf_pixel_range = p_msdf_pixel_range;
3534
if (base_font.is_valid()) {
3535
base_font->set_msdf_pixel_range(msdf_pixel_range);
3536
}
3537
emit_changed();
3538
}
3539
}
3540
3541
int SystemFont::get_msdf_pixel_range() const {
3542
return msdf_pixel_range;
3543
}
3544
3545
void SystemFont::set_msdf_size(int p_msdf_size) {
3546
if (msdf_size != p_msdf_size) {
3547
msdf_size = p_msdf_size;
3548
if (base_font.is_valid()) {
3549
base_font->set_msdf_size(msdf_size);
3550
}
3551
emit_changed();
3552
}
3553
}
3554
3555
int SystemFont::get_msdf_size() const {
3556
return msdf_size;
3557
}
3558
3559
void SystemFont::set_font_names(const PackedStringArray &p_names) {
3560
if (names != p_names) {
3561
names = p_names;
3562
_update_base_font();
3563
}
3564
}
3565
3566
PackedStringArray SystemFont::get_font_names() const {
3567
return names;
3568
}
3569
3570
void SystemFont::set_font_italic(bool p_italic) {
3571
if (italic != p_italic) {
3572
italic = p_italic;
3573
_update_base_font();
3574
}
3575
}
3576
3577
bool SystemFont::get_font_italic() const {
3578
return italic;
3579
}
3580
3581
void SystemFont::set_font_weight(int p_weight) {
3582
if (weight != p_weight) {
3583
weight = CLAMP(p_weight, 100, 999);
3584
_update_base_font();
3585
}
3586
}
3587
3588
int SystemFont::get_font_weight() const {
3589
return weight;
3590
}
3591
3592
void SystemFont::set_font_stretch(int p_stretch) {
3593
if (stretch != p_stretch) {
3594
stretch = CLAMP(p_stretch, 50, 200);
3595
_update_base_font();
3596
}
3597
}
3598
3599
int SystemFont::get_font_stretch() const {
3600
return stretch;
3601
}
3602
3603
int SystemFont::get_spacing(TextServer::SpacingType p_spacing) const {
3604
if (base_font.is_valid()) {
3605
return base_font->get_spacing(p_spacing);
3606
} else {
3607
return 0;
3608
}
3609
}
3610
3611
RID SystemFont::find_variation(const Dictionary &p_variation_coordinates, int p_face_index, float p_strength, Transform2D p_transform, int p_spacing_top, int p_spacing_bottom, int p_spacing_space, int p_spacing_glyph, float p_baseline_offset) const {
3612
Ref<Font> f = _get_base_font_or_default();
3613
if (f.is_valid()) {
3614
Dictionary var = p_variation_coordinates;
3615
if (ftr_weight > 0 && !var.has(TS->name_to_tag("weight"))) {
3616
var[TS->name_to_tag("weight")] = ftr_weight;
3617
}
3618
if (ftr_stretch > 0 && !var.has(TS->name_to_tag("width"))) {
3619
var[TS->name_to_tag("width")] = ftr_stretch;
3620
}
3621
if (ftr_italic > 0 && !var.has(TS->name_to_tag("italic"))) {
3622
var[TS->name_to_tag("italic")] = ftr_italic;
3623
}
3624
3625
if (!face_indices.is_empty()) {
3626
int face_index = CLAMP(p_face_index, 0, face_indices.size() - 1);
3627
return f->find_variation(var, face_indices[face_index], p_strength, p_transform, p_spacing_top, p_spacing_bottom, p_spacing_space, p_spacing_glyph, p_baseline_offset);
3628
} else {
3629
return f->find_variation(var, 0, p_strength, p_transform, p_spacing_top, p_spacing_bottom, p_spacing_space, p_spacing_glyph, p_baseline_offset);
3630
}
3631
}
3632
return RID();
3633
}
3634
3635
RID SystemFont::_get_rid() const {
3636
Ref<Font> f = _get_base_font_or_default();
3637
if (f.is_valid()) {
3638
if (!face_indices.is_empty()) {
3639
Dictionary var;
3640
if (ftr_weight > 0) {
3641
var[TS->name_to_tag("weight")] = ftr_weight;
3642
}
3643
if (ftr_stretch > 0) {
3644
var[TS->name_to_tag("width")] = ftr_stretch;
3645
}
3646
if (ftr_italic > 0) {
3647
var[TS->name_to_tag("italic")] = ftr_italic;
3648
}
3649
return f->find_variation(var, face_indices[0]);
3650
} else {
3651
return f->_get_rid();
3652
}
3653
}
3654
return RID();
3655
}
3656
3657
int64_t SystemFont::get_face_count() const {
3658
return face_indices.size();
3659
}
3660
3661
SystemFont::SystemFont() {
3662
/* NOP */
3663
}
3664
3665
SystemFont::~SystemFont() {
3666
}
3667
3668