test_fontfile.h 6.7 KB

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