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