Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/scene/test_fontfile.cpp
45991 views
1
/**************************************************************************/
2
/* test_fontfile.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 "tests/test_macros.h"
32
33
TEST_FORCE_LINK(test_fontfile)
34
35
#include "scene/resources/font.h"
36
37
#include "modules/modules_enabled.gen.h" // For freetype.
38
39
namespace TestFontFile {
40
41
TEST_CASE("[FontFile] Load Dynamic Font - getters") {
42
#ifdef MODULE_FREETYPE_ENABLED
43
String test_dynamic_font = "thirdparty/fonts/NotoSansHebrew_Regular.woff2";
44
Ref<FontFile> ff;
45
ff.instantiate();
46
CHECK(ff->load_dynamic_font(test_dynamic_font) == OK);
47
48
// These properties come from the font file itself.
49
CHECK(ff->get_font_name() == "Noto Sans Hebrew");
50
CHECK(ff->get_font_style_name() == "Regular");
51
CHECK(ff->get_font_weight() == 400);
52
CHECK(ff->get_font_stretch() == 100);
53
CHECK(ff->get_opentype_features() == Dictionary());
54
55
Dictionary expected_ot_name_strings;
56
Dictionary en_dict;
57
en_dict["copyright"] = "Copyright 2024 The Noto Project Authors (https://github.com/notofonts/hebrew)";
58
en_dict["family_name"] = "Noto Sans Hebrew";
59
en_dict["subfamily_name"] = "Regular";
60
en_dict["unique_identifier"] = "3.001;GOOG;NotoSansHebrew-Regular";
61
en_dict["full_name"] = "Noto Sans Hebrew Regular";
62
en_dict["version"] = "Version 3.001";
63
en_dict["postscript_name"] = "NotoSansHebrew-Regular";
64
en_dict["trademark"] = "Noto is a trademark of Google Inc.";
65
en_dict["manufacturer"] = "Google LLC";
66
en_dict["designer"] = "Ben Nathan";
67
en_dict["description"] = "Designed by Ben Nathan";
68
en_dict["vendor_url"] = "http://www.google.com/get/noto/";
69
en_dict["designer_url"] = "https://hafontia.com/";
70
en_dict["license"] = "This Font Software is licensed under the SIL Open Font License, Version 1.1. This Font Software is distributed on an \"AS IS\" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the SIL Open Font License for the specific language, permissions and limitations governing your use of this Font Software.";
71
en_dict["license_url"] = "http://scripts.sil.org/OFL";
72
expected_ot_name_strings["en"] = en_dict;
73
CHECK(ff->get_ot_name_strings() == expected_ot_name_strings);
74
75
// These are dependent on size and potentially other state. Act as regression tests based of arbitrary small size 10 and large size 100.
76
CHECK(ff->get_height(10) == doctest::Approx((real_t)14));
77
CHECK(ff->get_ascent(10) == doctest::Approx((real_t)11));
78
CHECK(ff->get_descent(10) == doctest::Approx((real_t)3));
79
CHECK(ff->get_underline_position(10) == doctest::Approx((real_t)1.25));
80
CHECK(ff->get_underline_thickness(10) == doctest::Approx((real_t)0.5));
81
CHECK(ff->get_height(100) == doctest::Approx((real_t)137));
82
CHECK(ff->get_ascent(100) == doctest::Approx((real_t)107));
83
CHECK(ff->get_descent(100) == doctest::Approx((real_t)30));
84
CHECK(ff->get_underline_position(100) == doctest::Approx((real_t)12.5));
85
CHECK(ff->get_underline_thickness(100) == doctest::Approx((real_t)5));
86
#endif
87
}
88
89
TEST_CASE("[FontFile] Create font file and check data") {
90
// Create test instance.
91
Ref<FontFile> font_file;
92
font_file.instantiate();
93
94
#ifdef MODULE_FREETYPE_ENABLED
95
// Try to load non-existent files.
96
ERR_PRINT_OFF
97
CHECK(font_file->load_dynamic_font("") == OK);
98
CHECK_MESSAGE(font_file->get_data().is_empty() == true, "Invalid fontfile should not be loaded.");
99
100
CHECK(font_file->load_dynamic_font("thirdparty/fonts/nofonthasthisname.woff2") == OK);
101
CHECK_MESSAGE(font_file->get_data().is_empty() == true, "Invalid fontfile should not be loaded.");
102
ERR_PRINT_ON
103
104
// Load a valid file.
105
CHECK(font_file->load_dynamic_font("thirdparty/fonts/Inter_Regular.woff2") == OK);
106
107
// Check fontfile data.
108
CHECK_MESSAGE(font_file->get_data().is_empty() == false, "Fontfile should have been loaded.");
109
CHECK_MESSAGE(font_file->get_font_name() == "Inter", "Loaded correct font name.");
110
CHECK_MESSAGE(font_file->get_font_style_name() == "Regular", "Loaded correct font style.");
111
CHECK_MESSAGE(font_file->get_data().size() == 111268llu, "Whole fontfile was loaded.");
112
113
// Valid glyphs.
114
CHECK_MESSAGE(font_file->get_glyph_index(2, 'a', 0) != 0, "Glyph index for 'a' is valid.");
115
CHECK_MESSAGE(font_file->get_glyph_index(2, 'b', 0) != 0, "Glyph index for 'b' is valid.");
116
CHECK_MESSAGE(font_file->get_glyph_index(2, 0x0103, 0) != 0, "Glyph index for 'latin small letter a with breve' is valid.");
117
CHECK_MESSAGE(font_file->get_glyph_index(2, 0x03a8, 0) != 0, "Glyph index for 'Greek psi' is valid.");
118
CHECK_MESSAGE(font_file->get_glyph_index(2, 0x0416, 0) != 0, "Glyph index for 'Cyrillic zhe' is valid.");
119
CHECK_MESSAGE(font_file->get_glyph_index(2, '&', 0) != 0, "Glyph index for '&' is valid.");
120
121
// Invalid glyphs.
122
CHECK_MESSAGE(font_file->get_glyph_index(2, 0x4416, 0) == 0, "Glyph index is invalid.");
123
CHECK_MESSAGE(font_file->get_glyph_index(2, 0x5555, 0) == 0, "Glyph index is invalid.");
124
CHECK_MESSAGE(font_file->get_glyph_index(2, 0x2901, 0) == 0, "Glyph index is invalid.");
125
#endif
126
}
127
128
} // namespace TestFontFile
129
130