WindowFonts.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. #pragma once
  16. #include <string>
  17. #include <vector>
  18. #include "common/TypeDefine.h"
  19. /**
  20. * Builds the font images used by the game from the textures in WINDOW.BIN.
  21. *
  22. * WINDOW.BIN (data/kernel) holds three gzipped files. The first one is a 4 bpp TIM with the window
  23. * graphics, including the menu font, the small digits and the timer digits. The second one is a 4 bpp
  24. * TIM with the dialog font. The third one is the width table of the dialog font.
  25. *
  26. * The images built here have the layout described by the font XML files in data/fonts, so the XML
  27. * files don't change. Only the palette indices of the TIM files are used, and they are mapped to fixed
  28. * colours, because the game tints the text itself.
  29. */
  30. namespace WindowFonts{
  31. /**
  32. * Palette indices of a 4 bpp TIM image, one byte per pixel.
  33. */
  34. struct IndexImage{
  35. /**
  36. * Image width, in pixels.
  37. */
  38. int width = 0;
  39. /**
  40. * Image height, in pixels.
  41. */
  42. int height = 0;
  43. /**
  44. * Palette index of every pixel, row by row.
  45. */
  46. std::vector<u8> pixels;
  47. /**
  48. * Gets the palette index of a pixel.
  49. *
  50. * @param[in] x X coordinate.
  51. * @param[in] y Y coordinate.
  52. * @return The palette index, or 0 if the coordinates are outside the image.
  53. */
  54. u8 At(int x, int y) const;
  55. };
  56. /**
  57. * An RGBA image, four bytes per pixel.
  58. */
  59. struct RgbaImage{
  60. /**
  61. * Image width, in pixels.
  62. */
  63. int width = 0;
  64. /**
  65. * Image height, in pixels.
  66. */
  67. int height = 0;
  68. /**
  69. * Red, green, blue and alpha of every pixel, row by row.
  70. */
  71. std::vector<u8> pixels;
  72. };
  73. /**
  74. * Reads the pixels of a 4 bpp TIM file.
  75. *
  76. * @param[in] data The TIM file.
  77. * @param[in] size Size of the TIM file, in bytes.
  78. * @param[out] image The palette indices of the image.
  79. * @return True if the data is a valid 4 bpp TIM file, false otherwise.
  80. */
  81. bool ReadTim4(const u8* data, size_t size, IndexImage& image);
  82. /**
  83. * Builds the dialog font image (ffvii_en.png) from the second TIM of WINDOW.BIN.
  84. *
  85. * @param[in] font The dialog font TIM.
  86. * @return The 256x256 font image.
  87. */
  88. RgbaImage BuildTextFont(const IndexImage& font);
  89. /**
  90. * Builds the menu font image (ffvii_menu.png) from the first TIM of WINDOW.BIN.
  91. *
  92. * The menu font is made of rows of 8 pixels. Each row is placed in a slot of 16 pixels, with the
  93. * glyphs in the lower half. The four last rows of the image are empty, the PC version doesn't have
  94. * the voiced kana that the XML expects there.
  95. *
  96. * @param[in] window The window graphics TIM.
  97. * @return The 128x256 font image.
  98. */
  99. RgbaImage BuildMenuFont(const IndexImage& window);
  100. /**
  101. * Builds the small digits font image (ffvii_digits.png) from the first TIM of WINDOW.BIN.
  102. *
  103. * @param[in] window The window graphics TIM.
  104. * @return The 128x8 font image.
  105. */
  106. RgbaImage BuildDigitsFont(const IndexImage& window);
  107. /**
  108. * Builds the timer font image (ffvii_timer.png) from the first TIM of WINDOW.BIN.
  109. *
  110. * @param[in] window The window graphics TIM.
  111. * @return The 128x48 font image.
  112. */
  113. RgbaImage BuildTimerFont(const IndexImage& window);
  114. /**
  115. * Saves an image as a PNG file.
  116. *
  117. * @param[in] image The image to save.
  118. * @param[in] file_name Path of the PNG file.
  119. */
  120. void SavePng(const RgbaImage& image, const std::string& file_name);
  121. }