TestFontHandling.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * Copyright (c) 2020, the SerenityOS developers.
  3. * Copyright (c) 2021, Brian Gianforcaro <bgianf@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/Utf8View.h>
  8. #include <LibGfx/Font/BitmapFont.h>
  9. #include <LibGfx/Font/FontDatabase.h>
  10. #include <LibTest/TestCase.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #ifdef AK_OS_SERENITY
  15. # define TEST_INPUT(x) ("/usr/Tests/LibGfx/test-inputs/" x)
  16. #else
  17. # define TEST_INPUT(x) ("test-inputs/" x)
  18. #endif
  19. TEST_CASE(test_fontdatabase_get_by_name)
  20. {
  21. Gfx::FontDatabase::set_default_fonts_lookup_path(TEST_INPUT(""));
  22. auto name = "Family 12 400 0"sv;
  23. auto& font_database = Gfx::FontDatabase::the();
  24. EXPECT(!font_database.get_by_name(name)->name().is_empty());
  25. }
  26. TEST_CASE(test_fontdatabase_get)
  27. {
  28. Gfx::FontDatabase::set_default_fonts_lookup_path(TEST_INPUT(""));
  29. auto& font_database = Gfx::FontDatabase::the();
  30. EXPECT(!font_database.get("Family"_fly_string, 12, 400, Gfx::FontWidth::Normal, 0)->name().is_empty());
  31. }
  32. TEST_CASE(test_fontdatabase_for_each_font)
  33. {
  34. Gfx::FontDatabase::set_default_fonts_lookup_path(TEST_INPUT(""));
  35. auto& font_database = Gfx::FontDatabase::the();
  36. font_database.for_each_font([&](Gfx::Font const& font) {
  37. EXPECT(!font.name().is_empty());
  38. EXPECT(!font.qualified_name().is_empty());
  39. EXPECT(!font.family().is_empty());
  40. EXPECT(font.glyph_count() > 0);
  41. });
  42. }
  43. TEST_CASE(test_clone)
  44. {
  45. u8 glyph_height = 1;
  46. u8 glyph_width = 1;
  47. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  48. auto new_font = font->clone();
  49. EXPECT(!new_font->name().is_empty());
  50. EXPECT(!new_font->qualified_name().is_empty());
  51. EXPECT(!new_font->family().is_empty());
  52. EXPECT(new_font->glyph_count() > 0);
  53. }
  54. TEST_CASE(test_set_name)
  55. {
  56. u8 glyph_height = 1;
  57. u8 glyph_width = 1;
  58. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  59. auto name = "my newly created font"_string;
  60. font->set_name(name);
  61. EXPECT(!font->name().is_empty());
  62. EXPECT(font->name().contains(name));
  63. }
  64. TEST_CASE(test_set_family)
  65. {
  66. u8 glyph_height = 1;
  67. u8 glyph_width = 1;
  68. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  69. auto family = "my newly created font family"_string;
  70. font->set_family(family);
  71. EXPECT(!font->family().is_empty());
  72. EXPECT(font->family().contains(family));
  73. }
  74. TEST_CASE(test_set_glyph_width)
  75. {
  76. u8 glyph_height = 1;
  77. u8 glyph_width = 1;
  78. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  79. size_t ch = 123;
  80. font->set_glyph_width(ch, glyph_width);
  81. EXPECT(font->glyph_width(ch) == glyph_width);
  82. }
  83. TEST_CASE(test_set_glyph_spacing)
  84. {
  85. u8 glyph_height = 1;
  86. u8 glyph_width = 1;
  87. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  88. u8 glyph_spacing = 8;
  89. font->set_glyph_spacing(glyph_spacing);
  90. EXPECT(font->glyph_spacing() == glyph_spacing);
  91. }
  92. TEST_CASE(test_width)
  93. {
  94. u8 glyph_height = 1;
  95. u8 glyph_width = 1;
  96. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  97. EXPECT(font->width("A"sv) == glyph_width);
  98. }
  99. TEST_CASE(test_glyph_or_emoji_width)
  100. {
  101. u8 glyph_height = 1;
  102. u8 glyph_width = 1;
  103. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  104. Utf8View view { " "sv };
  105. auto it = view.begin();
  106. EXPECT(font->glyph_or_emoji_width(it));
  107. }
  108. TEST_CASE(test_load_from_file)
  109. {
  110. auto font = Gfx::BitmapFont::load_from_file(TEST_INPUT("TestFont.font"sv));
  111. EXPECT(!font->name().is_empty());
  112. }
  113. TEST_CASE(test_write_to_file)
  114. {
  115. u8 glyph_height = 1;
  116. u8 glyph_width = 1;
  117. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  118. char path[] = "/tmp/new.font.XXXXXX";
  119. EXPECT(mkstemp(path) != -1);
  120. TRY_OR_FAIL(font->write_to_file(path));
  121. unlink(path);
  122. }
  123. TEST_CASE(test_character_set_masking)
  124. {
  125. auto font = TRY_OR_FAIL(Gfx::BitmapFont::try_load_from_file(TEST_INPUT("TestFont.font"sv)));
  126. auto unmasked_font = TRY_OR_FAIL(font->unmasked_character_set());
  127. EXPECT(unmasked_font->glyph_index(0x0041).value() == 0x0041);
  128. EXPECT(unmasked_font->glyph_index(0x0100).value() == 0x0100);
  129. EXPECT(unmasked_font->glyph_index(0xFFFD).value() == 0xFFFD);
  130. auto masked_font = TRY_OR_FAIL(unmasked_font->masked_character_set());
  131. EXPECT(masked_font->glyph_index(0x0041).value() == 0x0041);
  132. EXPECT(!masked_font->glyph_index(0x0100).has_value());
  133. EXPECT(masked_font->glyph_index(0xFFFD).value() == 0x1FD);
  134. }