WindowFonts.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (C) 2022 The V-Gears Team
  3. *
  4. * This file is part of V-Gears
  5. *
  6. * V-Gears is free software: you can redistribute it and/or modify it under
  7. * terms of the GNU General Public License as published by the Free Software
  8. * Foundation, version 3.0 (GPLv3) of the License.
  9. *
  10. * V-Gears is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. */
  15. #include <boost/test/unit_test.hpp>
  16. #include "installer/WindowFonts.h"
  17. namespace{
  18. void PutU16(std::vector<u8>& data, u32 value){
  19. data.push_back(value & 0xFF);
  20. data.push_back((value >> 8) & 0xFF);
  21. }
  22. void PutU32(std::vector<u8>& data, u32 value){
  23. PutU16(data, value & 0xFFFF);
  24. PutU16(data, value >> 16);
  25. }
  26. /**
  27. * Builds a 4 bpp TIM with one palette. Pixel (x, y) has index (x + y) % 16.
  28. */
  29. std::vector<u8> MakeTim(int width, int height){
  30. std::vector<u8> data;
  31. PutU32(data, 0x10);
  32. PutU32(data, 0x08);
  33. // Palette block: 16 colours.
  34. PutU32(data, 12 + 32);
  35. PutU16(data, 0);
  36. PutU16(data, 0);
  37. PutU16(data, 16);
  38. PutU16(data, 1);
  39. for (int i = 0; i < 16; i ++) PutU16(data, 0x7FFF);
  40. // Image block.
  41. PutU32(data, 12 + width * height / 2);
  42. PutU16(data, 0);
  43. PutU16(data, 0);
  44. PutU16(data, width / 4);
  45. PutU16(data, height);
  46. for (int y = 0; y < height; y ++){
  47. for (int x = 0; x < width; x += 2)
  48. data.push_back(((x + y) % 16) | (((x + 1 + y) % 16) << 4));
  49. }
  50. return data;
  51. }
  52. /**
  53. * Makes an index image of the size of the WINDOW.BIN textures, filled with one index.
  54. */
  55. WindowFonts::IndexImage MakeImage(u8 index){
  56. WindowFonts::IndexImage image;
  57. image.width = 256;
  58. image.height = 256;
  59. image.pixels.assign(256 * 256, index);
  60. return image;
  61. }
  62. /**
  63. * Gets a pixel of an RGBA image as a 32 bit value, 0xRRGGBBAA.
  64. */
  65. u32 Pixel(const WindowFonts::RgbaImage& image, int x, int y){
  66. const u8* p = &image.pixels[(y * image.width + x) * 4];
  67. return (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  68. }
  69. }
  70. BOOST_AUTO_TEST_CASE(WindowFontsReadTim4){
  71. const std::vector<u8> tim = MakeTim(16, 4);
  72. WindowFonts::IndexImage image;
  73. BOOST_REQUIRE(WindowFonts::ReadTim4(tim.data(), tim.size(), image));
  74. BOOST_CHECK_EQUAL(image.width, 16);
  75. BOOST_CHECK_EQUAL(image.height, 4);
  76. for (int y = 0; y < 4; y ++)
  77. for (int x = 0; x < 16; x ++) BOOST_CHECK_EQUAL(image.At(x, y), (x + y) % 16);
  78. // Outside of the image.
  79. BOOST_CHECK_EQUAL(image.At(16, 0), 0);
  80. BOOST_CHECK_EQUAL(image.At(0, -1), 0);
  81. }
  82. BOOST_AUTO_TEST_CASE(WindowFontsReadTim4Invalid){
  83. WindowFonts::IndexImage image;
  84. std::vector<u8> tim = MakeTim(16, 4);
  85. // Truncated.
  86. BOOST_CHECK(!WindowFonts::ReadTim4(tim.data(), tim.size() - 1, image));
  87. // 8 bpp.
  88. tim[4] = 0x09;
  89. BOOST_CHECK(!WindowFonts::ReadTim4(tim.data(), tim.size(), image));
  90. // Not a TIM.
  91. tim[0] = 0x11;
  92. BOOST_CHECK(!WindowFonts::ReadTim4(tim.data(), tim.size(), image));
  93. }
  94. BOOST_AUTO_TEST_CASE(WindowFontsTextFont){
  95. WindowFonts::IndexImage font = MakeImage(0);
  96. font.pixels[0] = 3;
  97. font.pixels[1] = 1;
  98. font.pixels[2] = 2;
  99. const WindowFonts::RgbaImage image = WindowFonts::BuildTextFont(font);
  100. BOOST_CHECK_EQUAL(image.width, 256);
  101. BOOST_CHECK_EQUAL(image.height, 256);
  102. BOOST_CHECK_EQUAL(Pixel(image, 0, 0), 0xE6E6E6FF);
  103. BOOST_CHECK_EQUAL(Pixel(image, 1, 0), 0x202020FF);
  104. BOOST_CHECK_EQUAL(Pixel(image, 2, 0), 0x00000000);
  105. BOOST_CHECK_EQUAL(Pixel(image, 3, 0), 0x00000000);
  106. }
  107. BOOST_AUTO_TEST_CASE(WindowFontsMenuFont){
  108. WindowFonts::IndexImage window = MakeImage(1);
  109. // Shadow on the first row of the second glyph row.
  110. for (int x = 128; x < 256; x ++) window.pixels[160 * 256 + x] = 15;
  111. const WindowFonts::RgbaImage image = WindowFonts::BuildMenuFont(window);
  112. BOOST_CHECK_EQUAL(image.width, 128);
  113. BOOST_CHECK_EQUAL(image.height, 256);
  114. // Each 8 pixel row goes to the lower half of a 16 pixel slot.
  115. BOOST_CHECK_EQUAL(Pixel(image, 0, 7), 0x00000000);
  116. BOOST_CHECK_EQUAL(Pixel(image, 0, 8), 0xE6E6E6FF);
  117. BOOST_CHECK_EQUAL(Pixel(image, 127, 15), 0xE6E6E6FF);
  118. BOOST_CHECK_EQUAL(Pixel(image, 5, 24), 0x181818FF);
  119. BOOST_CHECK_EQUAL(Pixel(image, 5, 25), 0xE6E6E6FF);
  120. BOOST_CHECK_EQUAL(Pixel(image, 0, 191), 0xE6E6E6FF);
  121. // The last four slots are empty.
  122. BOOST_CHECK_EQUAL(Pixel(image, 0, 200), 0x00000000);
  123. BOOST_CHECK_EQUAL(Pixel(image, 0, 255), 0x00000000);
  124. }
  125. BOOST_AUTO_TEST_CASE(WindowFontsDigitsFont){
  126. WindowFonts::IndexImage window = MakeImage(0);
  127. window.pixels[0 * 256 + 136] = 1; // '0'
  128. window.pixels[3 * 256 + 215] = 15; // '9', last column.
  129. window.pixels[240 * 256 + 137] = 1; // '-'
  130. window.pixels[216 * 256 + 152] = 1; // '+'
  131. window.pixels[232 * 256 + 152] = 1; // '%'
  132. window.pixels[232 * 256 + 168] = 1; // ':'
  133. window.pixels[0 * 256 + 216] = 1; // After the digits, not copied.
  134. const WindowFonts::RgbaImage image = WindowFonts::BuildDigitsFont(window);
  135. BOOST_CHECK_EQUAL(image.width, 128);
  136. BOOST_CHECK_EQUAL(image.height, 8);
  137. BOOST_CHECK_EQUAL(Pixel(image, 0, 0), 0xE6E6E6FF);
  138. BOOST_CHECK_EQUAL(Pixel(image, 79, 3), 0x181818FF);
  139. BOOST_CHECK_EQUAL(Pixel(image, 80, 0), 0x00000000);
  140. BOOST_CHECK_EQUAL(Pixel(image, 89, 0), 0xE6E6E6FF);
  141. BOOST_CHECK_EQUAL(Pixel(image, 96, 0), 0xE6E6E6FF);
  142. BOOST_CHECK_EQUAL(Pixel(image, 104, 0), 0xE6E6E6FF);
  143. BOOST_CHECK_EQUAL(Pixel(image, 112, 0), 0xE6E6E6FF);
  144. }
  145. BOOST_AUTO_TEST_CASE(WindowFontsTimerFont){
  146. WindowFonts::IndexImage window = MakeImage(2);
  147. window.pixels[80 * 256 + 144] = 13;
  148. const WindowFonts::RgbaImage image = WindowFonts::BuildTimerFont(window);
  149. BOOST_CHECK_EQUAL(image.width, 128);
  150. BOOST_CHECK_EQUAL(image.height, 48);
  151. BOOST_CHECK_EQUAL(Pixel(image, 0, 0), 0x292929FF);
  152. BOOST_CHECK_EQUAL(Pixel(image, 1, 0), 0xFF00FFFF);
  153. BOOST_CHECK_EQUAL(Pixel(image, 111, 47), 0xFF00FFFF);
  154. // Past the right edge of the texture.
  155. BOOST_CHECK_EQUAL(Pixel(image, 112, 0), 0x000000FF);
  156. }