TestFontHandling.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 0";
  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, 0)->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_clone)
  35. {
  36. u8 glyph_height = 1;
  37. u8 glyph_width = 1;
  38. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  39. auto new_font = font->clone();
  40. EXPECT(!new_font->name().is_null());
  41. EXPECT(!new_font->qualified_name().is_null());
  42. EXPECT(!new_font->family().is_null());
  43. EXPECT(new_font->glyph_count() > 0);
  44. }
  45. TEST_CASE(test_set_name)
  46. {
  47. u8 glyph_height = 1;
  48. u8 glyph_width = 1;
  49. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  50. const char* name = "my newly created font";
  51. font->set_name(name);
  52. EXPECT(!font->name().is_null());
  53. EXPECT(font->name().contains(name));
  54. }
  55. TEST_CASE(test_set_family)
  56. {
  57. u8 glyph_height = 1;
  58. u8 glyph_width = 1;
  59. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  60. const char* family = "my newly created font family";
  61. font->set_family(family);
  62. EXPECT(!font->family().is_null());
  63. EXPECT(font->family().contains(family));
  64. }
  65. TEST_CASE(test_set_glyph_width)
  66. {
  67. u8 glyph_height = 1;
  68. u8 glyph_width = 1;
  69. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  70. size_t ch = 123;
  71. font->set_glyph_width(ch, glyph_width);
  72. EXPECT(font->glyph_width(ch) == glyph_width);
  73. }
  74. TEST_CASE(test_set_glyph_spacing)
  75. {
  76. u8 glyph_height = 1;
  77. u8 glyph_width = 1;
  78. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  79. u8 glyph_spacing = 8;
  80. font->set_glyph_spacing(glyph_spacing);
  81. EXPECT(font->glyph_spacing() == glyph_spacing);
  82. }
  83. TEST_CASE(test_width)
  84. {
  85. u8 glyph_height = 1;
  86. u8 glyph_width = 1;
  87. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  88. EXPECT(font->width("A") == glyph_width);
  89. }
  90. TEST_CASE(test_glyph_or_emoji_width)
  91. {
  92. u8 glyph_height = 1;
  93. u8 glyph_width = 1;
  94. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  95. EXPECT(font->glyph_or_emoji_width(0));
  96. }
  97. TEST_CASE(test_load_from_file)
  98. {
  99. auto font = Gfx::BitmapFont::load_from_file("/res/fonts/PebbletonBold14.font");
  100. EXPECT(!font->name().is_null());
  101. }
  102. TEST_CASE(test_write_to_file)
  103. {
  104. u8 glyph_height = 1;
  105. u8 glyph_width = 1;
  106. auto font = Gfx::BitmapFont::create(glyph_height, glyph_width, true, 256);
  107. char path[] = "/tmp/new.font.XXXXXX";
  108. EXPECT(mkstemp(path) != -1);
  109. EXPECT(font->write_to_file(path));
  110. unlink(path);
  111. }