FontFile.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 <Ogre.h>
  16. #include "common/FileSystem.h"
  17. #include "common/Logger.h"
  18. #include "FontFile.h"
  19. #include "common/TypeDefine.h"
  20. using namespace VGears;
  21. /*
  22. FontFile::FontFile(const Ogre::String& file): File(*GAMEFILESYSTEM, file){}
  23. */
  24. FontFile::FontFile(File* file): File(file){}
  25. FontFile::FontFile(File* file, const u32& offset, const u32& length):
  26. File(file, offset, length){}
  27. FontFile::FontFile(u8* buffer, const u32& offset, const u32& length):
  28. File(buffer, offset, length){}
  29. FontFile::~FontFile(void){}
  30. Surface* FontFile::GetSurface(void){
  31. int width = 512;
  32. int height = 4096;
  33. int chars = 0;
  34. int char_num = 6165;
  35. Surface* ret = CreateSurface(width, height);
  36. int x_16 = 0;
  37. int y_16 = 0;
  38. /**
  39. * CLUT colour.
  40. *
  41. * CLUT stands for Color LookUp Table.
  42. */
  43. struct ClutColor{
  44. /**
  45. * Red component in the CLUT.
  46. */
  47. u8 r;
  48. /**
  49. * Green component in the CLUT.
  50. */
  51. u8 g;
  52. /**
  53. * Blue component in the CLUT.
  54. */
  55. u8 b;
  56. /**
  57. * Alpha component in the CLUT.
  58. */
  59. u8 a;
  60. };
  61. ClutColor color;
  62. color.r = 0;
  63. color.g = 0;
  64. color.b = 0;
  65. color.a = 255;
  66. u8 data = 0;
  67. for (int chars = 0; chars < char_num; ++chars){
  68. Surface* glyth = CreateSurface(16, 16);
  69. for (int y = 0; y < 16; ++ y){
  70. data = (y < 11) ? GetU8(0xE + chars * 22 + y * 2 + 0) : 0;
  71. //LOGGER->Log(LOGGER_INFO, "%x, %02x", y, data);
  72. int j = 0;
  73. for (int i = 7; i >= 0; -- i){
  74. color.r = (((data >> i) & 0x01) == 1) ? 0 : 255;
  75. color.g = (((data >> i) & 0x01) == 1) ? 0 : 255;
  76. color.b = (((data >> i) & 0x01) == 1) ? 0 : 255;
  77. color.a = (((data >> i) & 0x01) == 1) ? 255 : 255;
  78. memcpy(
  79. glyth->pixels.data() + 64 * y + j, &color, sizeof(ClutColor)
  80. );
  81. j += 4;
  82. }
  83. data = (y < 11) ? GetU8(0xE + chars * 22 + y * 2 + 1) : 0;
  84. //LOGGER->Log(LOGGER_INFO, "%02x", data);
  85. for (int i = 7; i >= 0; --i){
  86. color.r = (((data >> i) & 0x01) == 1) ? 0 : 255;
  87. color.g = (((data >> i) & 0x01) == 1) ? 0 : 255;
  88. color.b = (((data >> i) & 0x01) == 1) ? 0 : 255;
  89. color.a = (((data >> i) & 0x01) == 1) ? 255 : 255;
  90. memcpy(
  91. glyth->pixels.data() + 64 * y + j, &color, sizeof(ClutColor)
  92. );
  93. j += 4;
  94. }
  95. }
  96. CopyToSurface(ret, x_16, y_16, glyth);
  97. delete glyth;
  98. x_16 += 16;
  99. if (x_16 == ret->width){
  100. y_16 += 16;
  101. x_16 = 0;
  102. }
  103. }
  104. Ogre::TexturePtr ptex;
  105. Ogre::HardwarePixelBufferSharedPtr buffer;
  106. ptex = Ogre::TextureManager::getSingleton().createManual(
  107. "DynaTex", "General", Ogre::TEX_TYPE_2D,
  108. ret->width, ret->height, 0, Ogre::PF_R8G8B8A8, Ogre::TU_STATIC
  109. );
  110. buffer = ptex->getBuffer(0, 0);
  111. buffer->lock(Ogre::HardwareBuffer::HBL_DISCARD);
  112. const Ogre::PixelBox& pb = buffer->getCurrentLock();
  113. for (auto y = 0; y < ret->height; ++ y){
  114. // IVV type cast
  115. auto* data = static_cast<unsigned int*> (
  116. (unsigned int *)(pb.data) + y * pb.rowPitch
  117. );
  118. for (auto x = 0; x < ret->width; ++ x){
  119. auto clut
  120. = ret->pixels[y * ret->width * 4 + x * 4 + 3]
  121. | (ret->pixels[y * ret->width * 4 + x * 4 + 2] << 8)
  122. | (ret->pixels[y * ret->width * 4 + x * 4 + 1] << 16)
  123. | (ret->pixels[y * ret->width * 4 + x * 4 + 0] << 24);
  124. data[x] = clut;
  125. }
  126. }
  127. Ogre::Image image;
  128. image.loadDynamicImage(
  129. (Ogre::uchar*) pb.data, ret->width, ret->height, Ogre::PF_R8G8B8A8
  130. );
  131. image.save("font.png");
  132. buffer->unlock();
  133. Ogre::TextureManager::getSingleton().remove("DynaTex");
  134. return ret;
  135. }