TestFontHandling.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 <LibGfx/BitmapFont.h>
  8. #include <LibGfx/FontDatabase.h>
  9. #include <LibTest/TestCase.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. TEST_CASE(test_fontdatabase_get_by_name)
  14. {
  15. const char* name = "Liza 10 400";
  16. auto& font_database = Gfx::FontDatabase::the();
  17. EXPECT(!font_database.get_by_name(name)->name().is_null());
  18. }
  19. TEST_CASE(test_fontdatabase_get)
  20. {
  21. auto& font_database = Gfx::FontDatabase::the();
  22. EXPECT(!font_database.get("Liza", 10, 400)->name().is_null());
  23. }
  24. TEST_CASE(test_fontdatabase_for_each_font)
  25. {
  26. auto& font_database = Gfx::FontDatabase::the();
  27. font_database.for_each_font([&](const Gfx::Font& font) {
  28. EXPECT(!font.name().is_null());
  29. EXPECT(!font.qualified_name().is_null());
  30. EXPECT(!font.family().is_null());
  31. EXPECT(font.glyph_count() > 0);
  32. });
  33. }
  34. TEST_CASE(test_default_font)
  35. {
  36. EXPECT(!Gfx::FontDatabase::default_font().name().is_null());
  37. }
  38. TEST_CASE(test_default_fixed_width_font)
  39. {
  40. EXPECT(!Gfx::FontDatabase::default_fixed_width_font().name().is_null());
  41. }
  42. TEST_CASE(test_default_bold_fixed_width_font)
  43. {
  44. EXPECT(!Gfx::FontDatabase::default_bold_fixed_width_font().name().is_null());
  45. }
  46. TEST_CASE(test_default_bold_font)
  47. {
  48. EXPECT(!Gfx::FontDatabase::default_bold_font().name().is_null());
  49. }
  50. TEST_CASE(test_clone)
  51. {
  52. u8 glyph_height = 1;
  53. u8 glyph_width = 1;
  54. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default);
  55. auto new_font = font->clone();
  56. EXPECT(!new_font->name().is_null());
  57. EXPECT(!new_font->qualified_name().is_null());
  58. EXPECT(!new_font->family().is_null());
  59. EXPECT(new_font->glyph_count() > 0);
  60. }
  61. TEST_CASE(test_set_name)
  62. {
  63. u8 glyph_height = 1;
  64. u8 glyph_width = 1;
  65. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default);
  66. const char* name = "my newly created font";
  67. font->set_name(name);
  68. EXPECT(!font->name().is_null());
  69. EXPECT(font->name().contains(name));
  70. }
  71. TEST_CASE(test_set_family)
  72. {
  73. u8 glyph_height = 1;
  74. u8 glyph_width = 1;
  75. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default);
  76. const char* family = "my newly created font family";
  77. font->set_family(family);
  78. EXPECT(!font->family().is_null());
  79. EXPECT(font->family().contains(family));
  80. }
  81. TEST_CASE(test_set_glyph_width)
  82. {
  83. u8 glyph_height = 1;
  84. u8 glyph_width = 1;
  85. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default);
  86. size_t ch = 123;
  87. font->set_glyph_width(ch, glyph_width);
  88. EXPECT(font->glyph_width(ch) == glyph_width);
  89. }
  90. TEST_CASE(test_set_glyph_spacing)
  91. {
  92. u8 glyph_height = 1;
  93. u8 glyph_width = 1;
  94. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default);
  95. u8 glyph_spacing = 8;
  96. font->set_glyph_spacing(glyph_spacing);
  97. EXPECT(font->glyph_spacing() == glyph_spacing);
  98. }
  99. TEST_CASE(test_set_type)
  100. {
  101. u8 glyph_height = 1;
  102. u8 glyph_width = 1;
  103. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default);
  104. auto type = Gfx::FontTypes::Default;
  105. font->set_type(type);
  106. EXPECT(font->type() == type);
  107. }
  108. TEST_CASE(test_width)
  109. {
  110. u8 glyph_height = 1;
  111. u8 glyph_width = 1;
  112. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default);
  113. EXPECT(font->width("A") == glyph_width);
  114. }
  115. TEST_CASE(test_glyph_or_emoji_width)
  116. {
  117. u8 glyph_height = 1;
  118. u8 glyph_width = 1;
  119. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default);
  120. font->set_type(Gfx::FontTypes::Default);
  121. EXPECT(font->glyph_or_emoji_width(0));
  122. }
  123. TEST_CASE(test_load_from_file)
  124. {
  125. auto font = Gfx::BitmapFont::load_from_file("/res/fonts/PebbletonBold14.font");
  126. EXPECT(!font->name().is_null());
  127. }
  128. TEST_CASE(test_write_to_file)
  129. {
  130. u8 glyph_height = 1;
  131. u8 glyph_width = 1;
  132. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, Gfx::FontTypes::Default);
  133. char path[] = "/tmp/new.font.XXXXXX";
  134. EXPECT(mkstemp(path) != -1);
  135. EXPECT(font->write_to_file(path));
  136. unlink(path);
  137. }