BitmapFont.cc 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2013 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _BITMAP_FONT_H_
  23. #include "bitmapFont/BitmapFont.h"
  24. #endif
  25. #ifndef _STRING_UNIT_H_
  26. #include "string/stringUnit.h"
  27. #endif
  28. #include <string>
  29. namespace font
  30. {
  31. BitmapFont::BitmapFont()
  32. {
  33. }
  34. bool BitmapFont::parseFont(Stream& io_rStream)
  35. {
  36. U32 numBytes = io_rStream.getStreamSize() - io_rStream.getPosition();
  37. while ((io_rStream.getStatus() != Stream::EOS) && numBytes > 0)
  38. {
  39. char Read[256];
  40. char Token[256];
  41. char *buffer = Con::getReturnBuffer(256);
  42. io_rStream.readLine((U8 *)buffer, 256);
  43. char temp[256];
  44. U32 tokenCount = StringUnit::getUnitCount(buffer, "\"");
  45. if (tokenCount > 1)
  46. {
  47. dSprintf(Token, 256, "%s", StringUnit::getUnit(buffer, 1, "\""));
  48. dSprintf(temp, 256, "tok1");
  49. dSprintf(buffer, 256, "%s", (char*)StringUnit::setUnit(buffer, 1, temp, "\""));
  50. }
  51. U32 wordCount = StringUnit::getUnitCount(buffer, " \t\n");
  52. dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, 0, " \t\n"));
  53. if (dStrcmp(Read, "info") == 0)
  54. {
  55. U32 currentWordCount = 1;
  56. while (currentWordCount < wordCount)
  57. {
  58. dSprintf(Read, 256, StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
  59. char temp[256];
  60. char Key[256];
  61. char Value[256];
  62. dSprintf(temp, 256, "%s", Read);
  63. dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
  64. dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
  65. if (dStrcmp(Value, "\"tok1\"") == 0) {
  66. dSprintf(Value, 256, "%s", Token);
  67. }
  68. if (dStrcmp(Key, "size") == 0)
  69. mSize = U16(dAtoi(Value));
  70. currentWordCount++;
  71. }
  72. }
  73. if (dStrcmp(Read, "common") == 0)
  74. {
  75. //this holds common data
  76. U32 currentWordCount = 1;
  77. while (currentWordCount < wordCount)
  78. {
  79. dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
  80. char temp[256];
  81. char Key[256];
  82. char Value[256];
  83. dSprintf(temp, 256, "%s", Read);
  84. dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
  85. dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
  86. if (dStrcmp(Value, "\"tok1\"") == 0) {
  87. dSprintf(Value, 256, "%s", Token);
  88. }
  89. if (dStrcmp(Key, "lineHeight") == 0)
  90. mLineHeight = U16(dAtoi(Value));
  91. else if (dStrcmp(Key, "base") == 0)
  92. mBaseline = U16(dAtoi(Value));
  93. else if (dStrcmp(Key, "scaleW") == 0)
  94. mWidth = U16(dAtoi(Value));
  95. else if (dStrcmp(Key, "scaleH") == 0)
  96. mHeight = U16(dAtoi(Value));
  97. else if (dStrcmp(Key, "pages") == 0)
  98. mPages = U16(dAtoi(Value));
  99. currentWordCount++;
  100. }
  101. }
  102. else if (dStrcmp(Read, "page") == 0)
  103. {
  104. //this is data for a page
  105. U32 currentWordCount = 1;
  106. while (currentWordCount < wordCount)
  107. {
  108. dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
  109. char temp[256];
  110. char Key[256];
  111. char Value[256];
  112. dSprintf(temp, 256, "%s", Read);
  113. dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
  114. dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
  115. if (dStrcmp(Value, "\"tok1\"") == 0) {
  116. dSprintf(Value, 256, "%s", Token);
  117. }
  118. //assign the correct value
  119. if (dStrcmp(Key, "file") == 0)
  120. {
  121. mPageName.push_back(StringTable->insert(Value));
  122. }
  123. currentWordCount++;
  124. }
  125. }
  126. else if (dStrcmp(Read, "char") == 0 && dStrcmp(Read, "chars") != 0)
  127. {
  128. //this is data for a character
  129. BitmapFontCharacter ci;
  130. U16 CharID = 0;
  131. U32 currentWordCount = 1;
  132. while (currentWordCount < wordCount)
  133. {
  134. dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
  135. char temp[256];
  136. char Key[256];
  137. char Value[256];
  138. dSprintf(temp, 256, "%s", Read);
  139. dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
  140. dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
  141. if (dStrcmp(Value, "\"tok1\"") == 0) {
  142. dSprintf(Value, 256, "%s", Token);
  143. }
  144. //assign the correct value
  145. if (dStrcmp(Key, "id") == 0)
  146. CharID = U32(dAtoi(Value));
  147. else if (dStrcmp(Key, "x") == 0)
  148. ci.mX = U32(dAtoi(Value));
  149. else if (dStrcmp(Key, "y") == 0)
  150. ci.mY = U32(dAtoi(Value));
  151. else if (dStrcmp(Key, "width") == 0)
  152. ci.mWidth = U32(dAtoi(Value));
  153. else if (dStrcmp(Key, "height") == 0)
  154. ci.mHeight = U32(dAtoi(Value));
  155. else if (dStrcmp(Key, "xoffset") == 0)
  156. ci.mXOffset = F32(dAtoi(Value));
  157. else if (dStrcmp(Key, "yoffset") == 0)
  158. ci.mYOffset = F32(dAtoi(Value));
  159. else if (dStrcmp(Key, "xadvance") == 0)
  160. ci.mXAdvance = F32(dAtoi(Value));
  161. else if (dStrcmp(Key, "page") == 0)
  162. ci.mPage = S32(dAtoi(Value));
  163. currentWordCount++;
  164. }
  165. ci.mCharID = CharID;
  166. ci.ProcessCharacter(mWidth, mHeight);
  167. mChar[CharID] = ci;
  168. }
  169. else if (dStrcmp(Read, "kerning") == 0 && dStrcmp(Read, "kernings") != 0)
  170. {
  171. //this is data for a kerning pair
  172. U16 first = 0;
  173. U16 second = 0;
  174. S16 amount = 0;
  175. U32 currentWordCount = 1;
  176. while (currentWordCount < wordCount)
  177. {
  178. dSprintf(Read, 256, "%s", StringUnit::getUnit(buffer, currentWordCount, " \t\n"));
  179. char temp[256];
  180. char Key[256];
  181. char Value[256];
  182. dSprintf(temp, 256, "%s", Read);
  183. dSprintf(Key, 256, "%s", StringUnit::getUnit(temp, 0, "="));
  184. dSprintf(Value, 256, "%s", StringUnit::getUnit(temp, 1, "="));
  185. if (dStrcmp(Value, "\"tok1\"") == 0) {
  186. dSprintf(Value, 256, "%s", Token);
  187. }
  188. //assign the correct value
  189. if (dStrcmp(Key, "first") == 0)
  190. first = U16(dAtoi(Value));
  191. else if (dStrcmp(Key, "second") == 0)
  192. second = U16(dAtoi(Value));
  193. else if (dStrcmp(Key, "amount") == 0)
  194. amount = S16(dAtoi(Value));
  195. currentWordCount++;
  196. }
  197. AddKerning(first, second, amount);
  198. }
  199. }
  200. return (io_rStream.getStatus() == Stream::EOS);
  201. }
  202. TextureHandle BitmapFont::LoadTexture(StringTableEntry fileName)
  203. {
  204. // Debug Profiling.
  205. PROFILE_SCOPE(FontAsset_LoadTexture);
  206. // Get image texture.
  207. TextureHandle mImageTextureHandle;
  208. mImageTextureHandle.set(fileName, TextureHandle::BitmapTexture, true, false);
  209. // Is the texture valid?
  210. if (mImageTextureHandle.IsNull())
  211. {
  212. // No, so warn.
  213. Con::warnf("FontAsset could not load texture '%s'.", fileName);
  214. return mImageTextureHandle;
  215. }
  216. //Set the filter mode. For now we'll just support bilinear. In the future we should give the user an option.
  217. mImageTextureHandle.setFilter(GL_LINEAR);
  218. return mImageTextureHandle;
  219. }
  220. }