Open Source Tomb Raider Engine
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

FontTRLE.cpp 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*!
  2. * \file src/FontTRLE.cpp
  3. * \brief SDL Font implementation
  4. *
  5. * \author xythobuz
  6. */
  7. #include <algorithm>
  8. #include <fstream>
  9. #include <sstream>
  10. #include <stdexcept>
  11. #include "global.h"
  12. #include "utils/strings.h"
  13. #include "FontTRLE.h"
  14. #define SCALING 2.0f
  15. bool FontTRLE::mFontInit = false;
  16. unsigned int FontTRLE::mFontTexture = 0;
  17. int FontTRLE::offsets[106][5];
  18. void FontTRLE::shutdown() {
  19. if (mFontInit)
  20. glDeleteTextures(1, &mFontTexture);
  21. }
  22. int FontTRLE::initialize(std::string font) {
  23. assert(stringEndsWith(font, ".pc") == true);
  24. shutdown();
  25. // Load .pc file...
  26. std::ifstream file(font, std::ios::in | std::ios::binary);
  27. unsigned char *pixels = new unsigned char[256 * 256 * 4];
  28. if (!file.read((char *)pixels, 256 * 256 * 4)) {
  29. delete [] pixels;
  30. return -1;
  31. }
  32. // Fix coloring
  33. for (unsigned int i = 0; i < (256 * 256 * 4); i += 4) {
  34. float y = (0.2126f * pixels[i + 2]);
  35. y += (0.7152f * pixels[i + 1]);
  36. y += (0.0722f * pixels[i]);
  37. pixels[i] = pixels[i + 1] = pixels[i + 2] = (unsigned char)y;
  38. }
  39. // ...into GL texture
  40. glGenTextures(1, &mFontTexture);
  41. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  42. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  43. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  44. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, 256, 256, 0, GL_BGRA_EXT, GL_UNSIGNED_BYTE, pixels);
  45. delete [] pixels;
  46. // Try to load .lps file or use default glyph positions
  47. std::string lpsFile = findAndReplace(font, ".pc", ".lps");
  48. loadLPS(lpsFile);
  49. mFontInit = true;
  50. return 0;
  51. }
  52. void FontTRLE::loadLPS(std::string f) {
  53. std::ifstream file(f);
  54. if (!file) {
  55. std::copy(&defaultOffsets[0][0], &defaultOffsets[0][0] + (106 * 5), &offsets[0][0]);
  56. return;
  57. }
  58. //! \todo This is probably the worlds most unreliable parser...
  59. for (std::string line; std::getline(file, line);) {
  60. std::istringstream stream(line);
  61. int index;
  62. stream >> index;
  63. if (stream.get() != '=')
  64. continue;
  65. stream >> offsets[index][0];
  66. if (stream.get() != ',')
  67. continue;
  68. stream >> offsets[index][1];
  69. if (stream.get() != ',')
  70. continue;
  71. stream >> offsets[index][2];
  72. if (stream.get() != ',')
  73. continue;
  74. stream >> offsets[index][3];
  75. if (stream.get() != ',')
  76. continue;
  77. stream >> offsets[index][4];
  78. }
  79. }
  80. void FontTRLE::writeChar(unsigned int index, unsigned int xDraw, unsigned int yDraw,
  81. float scale, const unsigned char color[4]) {
  82. assert(mFontInit == true);
  83. int width = (int)(((float)offsets[index][2]) * scale * SCALING);
  84. int height = (int)(((float)offsets[index][3]) * scale * SCALING);
  85. int offset = (int)(((float)offsets[index][4]) * scale * SCALING);
  86. // screen coordinates
  87. int xMin = xDraw;
  88. int yMin = ((int)yDraw) + offset + (int)(10.0f * scale * SCALING);
  89. int xMax = xMin + width;
  90. int yMax = yMin + height;
  91. // texture part
  92. float txMin = ((float)offsets[index][0]) / 256.0f;
  93. float txMax = ((float)(offsets[index][0] + offsets[index][2])) / 256.0f;
  94. float tyMin = ((float)offsets[index][1]) / 256.0f;
  95. float tyMax = ((float)(offsets[index][1] + offsets[index][3])) / 256.0f;
  96. // draw
  97. glBindTexture(GL_TEXTURE_2D, mFontTexture);
  98. glColor4f(color[0] * 256.0f, color[1] * 256.0f, color[2] * 256.0f, color[3] * 256.0f);
  99. glBegin(GL_QUADS);
  100. glTexCoord2f(txMin, tyMin);
  101. glVertex2i(xMin, yMin);
  102. glTexCoord2f(txMin, tyMax);
  103. glVertex2i(xMin, yMax);
  104. glTexCoord2f(txMax, tyMax);
  105. glVertex2i(xMax, yMax);
  106. glTexCoord2f(txMax, tyMin);
  107. glVertex2i(xMax, yMin);
  108. glEnd();
  109. }
  110. unsigned int FontTRLE::widthText(float scale, std::string s) {
  111. assert(mFontInit == true);
  112. assert(s.length() > 0);
  113. unsigned int width = 0;
  114. for (unsigned int i = 0; i < s.length(); i++) {
  115. // index into offset table
  116. int index = s[i] - '!';
  117. if (index == -1) // space
  118. width += (unsigned int)(14.0f * scale * SCALING);
  119. if ((index < 0) || (index > 105))
  120. continue; // skip unprintable chars
  121. width += (float)(offsets[index][2] + 1) * scale * SCALING; // glyph width
  122. }
  123. return width;
  124. }
  125. void FontTRLE::drawText(unsigned int x, unsigned int y, float scale,
  126. const unsigned char color[4], std::string s) {
  127. assert(mFontInit == true);
  128. assert(s.length() > 0);
  129. for (unsigned int i = 0; i < s.length(); i++) {
  130. // index into offset table
  131. int index = s[i] - '!';
  132. if (index == -1) // space
  133. x += (unsigned int)(14.0f * scale * SCALING);
  134. if ((index < 0) || (index > 105))
  135. continue; // skip unprintable chars
  136. writeChar((unsigned int)index, x, y, scale, color);
  137. x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
  138. }
  139. }
  140. unsigned int FontTRLE::heightText(float scale, unsigned int maxWidth, std::string s) {
  141. assert(mFontInit == true);
  142. assert(s.length() > 0);
  143. unsigned int x = 0;
  144. unsigned int yMax = 0;
  145. unsigned int yReturn = 0;
  146. for (unsigned int i = 0; i < s.length(); i++) {
  147. // index into offset table
  148. int index = s[i] - '!';
  149. if (index == -1) // space
  150. x += (unsigned int)(14.0f * scale * SCALING);
  151. if ((index < 0) || (index > 105))
  152. continue; // skip unprintable chars
  153. if (yMax < (unsigned int)(((float)offsets[index][3]) * scale * SCALING))
  154. yMax = (unsigned int)(((float)offsets[index][3]) * scale * SCALING);
  155. x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
  156. if (x > maxWidth) {
  157. // go to the next line
  158. yReturn += yMax + 2;
  159. yMax = 0;
  160. x = (int)((float)(offsets[index][2] + 1) * scale * SCALING);
  161. }
  162. }
  163. return yReturn + yMax + 2;
  164. }
  165. void FontTRLE::drawTextWrapped(unsigned int x, unsigned int y, float scale,
  166. const unsigned char color[4], unsigned int maxWidth, std::string s) {
  167. assert(mFontInit == true);
  168. assert(s.length() > 0);
  169. unsigned int xStart = x;
  170. unsigned int yMax = 0;
  171. for (unsigned int i = 0; i < s.length(); i++) {
  172. // index into offset table
  173. int index = s[i] - '!';
  174. if (index == -1) // space
  175. x += (unsigned int)(14.0f * scale * SCALING);
  176. if ((index < 0) || (index > 105))
  177. continue; // skip unprintable chars
  178. if (yMax < (unsigned int)(((float)offsets[index][3]) * scale * SCALING))
  179. yMax = (unsigned int)(((float)offsets[index][3]) * scale * SCALING);
  180. x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
  181. if ((x - xStart) > maxWidth) {
  182. // go to the next line
  183. y += yMax + 2;
  184. yMax = 0;
  185. x = xStart;
  186. } else {
  187. x -= (int)((float)(offsets[index][2] + 1) * scale * SCALING);
  188. }
  189. writeChar((unsigned int)index, x, y, scale, color);
  190. x += (int)((float)(offsets[index][2] + 1) * scale * SCALING); // width
  191. }
  192. }
  193. int FontTRLE::defaultOffsets[106][5] = {
  194. { 174, 52, 3, 12, -11 },
  195. { 98, 58, 6, 4, -10 },
  196. { 82, 26, 13, 11, -10 },
  197. { 78, 38, 9, 13, -10 },
  198. { 214, 13, 14, 11, -9 },
  199. { 40, 26, 13, 11, -10 },
  200. { 157, 57, 5, 6, -11 },
  201. { 204, 39, 5, 15, -12 },
  202. { 34, 40, 5, 15, -12 },
  203. { 184, 59, 4, 4, -11 },
  204. { 22, 40, 10, 10, -9 },
  205. { 178, 59, 4, 4, -2 },
  206. { 106, 60, 7, 2, -4 },
  207. { 114, 60, 4, 3, -2 },
  208. { 212, 38, 8, 14, -12 },
  209. { 88, 49, 9, 9, -8 },
  210. { 200, 55, 5, 9, -8 },
  211. { 46, 52, 8, 9, -8 },
  212. { 88, 38, 7, 10, -8 },
  213. { 62, 40, 10, 10, -8 },
  214. { 142, 48, 8, 11, -9 },
  215. { 232, 50, 8, 10, -9 },
  216. { 120, 47, 8, 11, -9 },
  217. { 22, 51, 8, 10, -9 },
  218. { 110, 49, 8, 10, -8 },
  219. { 152, 57, 4, 7, -7 },
  220. { 136, 57, 4, 9, -7 },
  221. { 178, 40, 11, 9, -8 },
  222. { 210, 53, 10, 6, -7 },
  223. { 240, 40, 11, 9, -7 },
  224. { 12, 39, 9, 12, -11 },
  225. { 66, 13, 15, 13, -10 },
  226. { 130, 13, 13, 12, -11 },
  227. { 214, 25, 12, 12, -11 },
  228. { 132, 35, 10, 12, -11 },
  229. { 0, 26, 12, 12, -11 },
  230. { 14, 26, 12, 12, -11 },
  231. { 66, 27, 11, 12, -11 },
  232. { 182, 27, 11, 12, -11 },
  233. { 200, 13, 13, 12, -11 },
  234. { 222, 54, 4, 12, -11 },
  235. { 56, 52, 4, 15, -11 },
  236. { 230, 15, 12, 12, -11 },
  237. { 144, 35, 10, 12, -11 },
  238. { 48, 13, 17, 12, -11 },
  239. { 144, 13, 13, 12, -11 },
  240. { 54, 26, 11, 12, -11 },
  241. { 200, 26, 11, 12, -11 },
  242. { 240, 0, 13, 14, -11 },
  243. { 158, 13, 13, 12, -11 },
  244. { 156, 35, 10, 12, -11 },
  245. { 172, 13, 13, 12, -11 },
  246. { 98, 13, 14, 12, -11 },
  247. { 82, 13, 14, 12, -11 },
  248. { 24, 13, 22, 12, -11 },
  249. { 186, 13, 12, 13, -11 },
  250. { 114, 13, 14, 12, -11 },
  251. { 228, 28, 11, 12, -11 },
  252. { 62, 60, 5, 3, -4 },
  253. { 248, 59, 5, 3, -4 },
  254. { 88, 59, 7, 3, -4 },
  255. { 142, 60, 6, 2, -3 },
  256. { 120, 59, 7, 3, -4 },
  257. { 242, 59, 4, 4, -11 },
  258. { 98, 49, 10, 8, -7 },
  259. { 96, 35, 10, 13, -12 },
  260. { 72, 52, 8, 8, -7 },
  261. { 0, 39, 10, 11, -10 },
  262. { 164, 52, 8, 8, -7 },
  263. { 168, 38, 9, 13, -12 },
  264. { 120, 35, 11, 11, -7 },
  265. { 108, 35, 10, 13, -12 },
  266. { 194, 27, 5, 11, -10 },
  267. { 40, 51, 5, 15, -10 },
  268. { 28, 26, 11, 13, -12 },
  269. { 82, 52, 5, 12, -11 },
  270. { 96, 26, 17, 8, -7 },
  271. { 152, 48, 11, 8, -7 },
  272. { 62, 51, 9, 8, -7 },
  273. { 244, 15, 10, 12, -7 },
  274. { 52, 39, 9, 12, -7 },
  275. { 10, 52, 9, 8, -7 },
  276. { 190, 52, 8, 8, -7 },
  277. { 0, 51, 8, 10, -9 },
  278. { 178, 50, 10, 8, -7 },
  279. { 130, 48, 11, 8, -7 },
  280. { 132, 26, 17, 8, -7 },
  281. { 242, 50, 10, 8, -7 },
  282. { 40, 38, 10, 12, -7 },
  283. { 232, 41, 7, 8, -7 },
  284. { 222, 41, 8, 12, -7 },
  285. { 130, 57, 5, 8, -7 },
  286. { 194, 39, 9, 12, -10 },
  287. { 32, 56, 4, 11, -10 },
  288. { 1, 14, 22, 11, -10 },
  289. { 192, 0, 23, 13, -10 },
  290. { 168, 0, 23, 12, -10 },
  291. { 216, 0, 23, 12, -10 },
  292. { 150, 26, 17, 8, -8 },
  293. { 168, 26, 11, 11, -9 },
  294. { 114, 26, 17, 8, -8 },
  295. { 240, 28, 12, 11, -9 },
  296. { 0, 0, 40, 12, -10 },
  297. { 84, 0, 39, 11, -10 },
  298. { 42, 0, 39, 11, -10 },
  299. { 126, 0, 39, 11, -10 },
  300. };