BsFontDesc.h 5.4 KB

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