BsFontDesc.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //__________________________ Banshee Project - A modern game development toolkit _________________________________//
  2. //_____________________________________ www.banshee-project.com __________________________________________________//
  3. //________________________ Copyright (c) 2014 Marko Pintera. All rights reserved. ________________________________//
  4. #pragma once
  5. #include "BsCorePrerequisites.h"
  6. namespace BansheeEngine
  7. {
  8. /**
  9. * @brief Kerning pair representing extra or less offset between
  10. * a specific pair of characters.
  11. */
  12. struct KerningPair
  13. {
  14. UINT32 otherCharId;
  15. INT32 amount;
  16. };
  17. /**
  18. * @brief Describes a single character in a font.
  19. */
  20. struct CHAR_DESC
  21. {
  22. UINT32 charId; /**< Character ID, corresponding to a Unicode key. */
  23. UINT32 page; /**< Index of the texture the character is located on. */
  24. float uvX, uvY; /**< Texture coordinates of the character in the page texture. */
  25. float uvWidth, uvHeight; /**< Width/height of the character in texture coordinates. */
  26. UINT32 width, height; /**< Width/height of the character in pixels. */
  27. INT32 xOffset, yOffset; /**< Determines offset for the visible portion of the character in pixels. */
  28. INT32 xAdvance, yAdvance; /**< Determines how much to advance the pen after writing this character. In pixels. */
  29. Vector<KerningPair> kerningPairs; /**< Pairs that determine if certain character pairs should be closer or father together. e.g. "AV" combination */
  30. };
  31. /**
  32. * @brief Describes a font.
  33. */
  34. struct FONT_DESC
  35. {
  36. Map<UINT32, CHAR_DESC> characters; /**< All characters in the font referenced by character ID. */
  37. INT32 baselineOffset; /**< Y offset to the baseline on which the characters are placed. In pixels. */
  38. UINT32 lineHeight; /**< Height of a single line of the font. In pixels. */
  39. CHAR_DESC missingGlyph; /**< Character index to use when data for a character is missing. */
  40. UINT32 spaceWidth; /**< Determines width of the space in pixels. */
  41. };
  42. // Make CHAR_DESC serializable
  43. template<> struct RTTIPlainType<CHAR_DESC>
  44. {
  45. enum { id = TID_CHAR_DESC }; enum { hasDynamicSize = 1 };
  46. static void toMemory(const CHAR_DESC& data, char* memory)
  47. {
  48. UINT32 size = getDynamicSize(data);
  49. memcpy(memory, &size, sizeof(UINT32));
  50. memory += sizeof(UINT32);
  51. memory = rttiWriteElem(data.charId, memory);
  52. memory = rttiWriteElem(data.page, memory);
  53. memory = rttiWriteElem(data.uvX, memory);
  54. memory = rttiWriteElem(data.uvY, memory);
  55. memory = rttiWriteElem(data.uvWidth, memory);
  56. memory = rttiWriteElem(data.uvHeight, memory);
  57. memory = rttiWriteElem(data.width, memory);
  58. memory = rttiWriteElem(data.height, memory);
  59. memory = rttiWriteElem(data.xOffset, memory);
  60. memory = rttiWriteElem(data.yOffset, memory);
  61. memory = rttiWriteElem(data.xAdvance, memory);
  62. memory = rttiWriteElem(data.yAdvance, memory);
  63. memory = rttiWriteElem(data.kerningPairs, memory);
  64. }
  65. static UINT32 fromMemory(CHAR_DESC& data, char* memory)
  66. {
  67. UINT32 size;
  68. memcpy(&size, memory, sizeof(UINT32));
  69. memory += sizeof(UINT32);
  70. memory = rttiReadElem(data.charId, memory);
  71. memory = rttiReadElem(data.page, memory);
  72. memory = rttiReadElem(data.uvX, memory);
  73. memory = rttiReadElem(data.uvY, memory);
  74. memory = rttiReadElem(data.uvWidth, memory);
  75. memory = rttiReadElem(data.uvHeight, memory);
  76. memory = rttiReadElem(data.width, memory);
  77. memory = rttiReadElem(data.height, memory);
  78. memory = rttiReadElem(data.xOffset, memory);
  79. memory = rttiReadElem(data.yOffset, memory);
  80. memory = rttiReadElem(data.xAdvance, memory);
  81. memory = rttiReadElem(data.yAdvance, memory);
  82. memory = rttiReadElem(data.kerningPairs, memory);
  83. return size;
  84. }
  85. static UINT32 getDynamicSize(const CHAR_DESC& data)
  86. {
  87. UINT64 dataSize = sizeof(data.charId)
  88. + sizeof(data.page)
  89. + sizeof(data.uvX)
  90. + sizeof(data.uvY)
  91. + sizeof(data.uvWidth)
  92. + sizeof(data.uvHeight)
  93. + sizeof(data.width)
  94. + sizeof(data.height)
  95. + sizeof(data.xOffset)
  96. + sizeof(data.yOffset)
  97. + sizeof(data.xAdvance)
  98. + sizeof(data.yAdvance)
  99. + RTTIPlainType<Vector<KerningPair>>::getDynamicSize(data.kerningPairs);
  100. dataSize += sizeof(UINT32);
  101. return (UINT32)dataSize;
  102. }
  103. };
  104. // Make FONT_DESC serializable
  105. template<> struct RTTIPlainType<FONT_DESC>
  106. {
  107. enum { id = TID_FONT_DESC }; enum { hasDynamicSize = 1 };
  108. static void toMemory(const FONT_DESC& data, char* memory)
  109. {
  110. UINT32 size = getDynamicSize(data);
  111. memcpy(memory, &size, sizeof(UINT32));
  112. memory += sizeof(UINT32);
  113. RTTIPlainType<Map<UINT32, CHAR_DESC>>::toMemory(data.characters, memory);
  114. rttiWriteElem(data.baselineOffset, memory);
  115. rttiWriteElem(data.lineHeight, memory);
  116. rttiWriteElem(data.missingGlyph, memory);
  117. rttiWriteElem(data.spaceWidth, memory);
  118. }
  119. static UINT32 fromMemory(FONT_DESC& data, char* memory)
  120. {
  121. UINT32 size;
  122. memcpy(&size, memory, sizeof(UINT32));
  123. memory += sizeof(UINT32);
  124. RTTIPlainType<Map<UINT32, CHAR_DESC>>::fromMemory(data.characters, memory);
  125. rttiReadElem(data.baselineOffset, memory);
  126. rttiReadElem(data.lineHeight, memory);
  127. rttiReadElem(data.missingGlyph, memory);
  128. rttiReadElem(data.spaceWidth, memory);
  129. return size;
  130. }
  131. static UINT32 getDynamicSize(const FONT_DESC& data)
  132. {
  133. UINT64 dataSize = sizeof(UINT32);
  134. dataSize += RTTIPlainType<Map<UINT32, CHAR_DESC>>::getDynamicSize(data.characters);
  135. dataSize += rttiGetElemSize(data.baselineOffset);
  136. dataSize += rttiGetElemSize(data.lineHeight);
  137. dataSize += rttiGetElemSize(data.missingGlyph);
  138. dataSize += rttiGetElemSize(data.spaceWidth);
  139. return (UINT32)dataSize;
  140. }
  141. };
  142. }