CmTextUtility.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. #pragma once
  2. #include "CmPrerequisites.h"
  3. #include "CmFontDesc.h"
  4. #include "CmInt2.h"
  5. namespace CamelotFramework
  6. {
  7. class CM_EXPORT TextUtility
  8. {
  9. private:
  10. class TextWord
  11. {
  12. public:
  13. TextWord(bool spacer);
  14. void addChar(const CHAR_DESC& desc);
  15. void addSpace(UINT32 spaceWidth);
  16. void removeLastChar();
  17. UINT32 getWidth() const { return mWidth; }
  18. UINT32 getHeight() const { return mHeight; }
  19. bool isSpacer() const { return mSpacer; }
  20. const Vector<CHAR_DESC>::type& getChars() const { return mChars; }
  21. private:
  22. Vector<CHAR_DESC>::type mChars;
  23. UINT32 mWidth;
  24. UINT32 mHeight;
  25. bool mSpacer;
  26. UINT32 mSpaceWidth;
  27. void calculateWidth();
  28. };
  29. public:
  30. class CM_EXPORT TextLine
  31. {
  32. public:
  33. TextLine(UINT32 baselineOffset, UINT32 lineHeight, UINT32 spaceWidth);
  34. ~TextLine();
  35. UINT32 getWidth() const { return mWidth; }
  36. UINT32 getHeight() const { return mHeight; }
  37. /**
  38. * @brief Returns an offset used to separate two lines.
  39. */
  40. UINT32 getYOffset() const { return mLineHeight; }
  41. /**
  42. * @brief Gets a number quads used by all characters for every page used by this text line.
  43. *
  44. * @note One page generally corresponds to one bitmap from which the characters are read from.
  45. *
  46. * One character is represented with a single quad. Some pages might be empty.
  47. */
  48. Vector<UINT32>::type getNumQuadsPerPage() const;
  49. /**
  50. * @brief Fills the vertex/uv/index buffers for the specified page, with all the character data
  51. * needed for rendering.
  52. *
  53. * @param page The page.
  54. * @param [out] vertices Pre-allocated array where character vertices will be written.
  55. * @param [out] uvs Pre-allocated array where character uv coordinates will be written.
  56. * @param [out] indexes Pre-allocated array where character indices will be written.
  57. * @param offset Offsets the location at which the method writes to the buffers.
  58. * Counted as number of quads.
  59. * @param size Total number of quads that can fit into the specified buffers.
  60. *
  61. * @return Number of quads that were written.
  62. */
  63. UINT32 fillBuffer(UINT32 page, Vector2* vertices, Vector2* uvs, UINT32* indexes, UINT32 offset, UINT32 size) const;
  64. /**
  65. * @brief Returns the total number of characters on this line.
  66. */
  67. UINT32 getNumChars() const;
  68. /**
  69. * @brief Query if this line was created explicitly due to a newline character.
  70. * As opposed to a line that was created because a word couldn't fit on the previous line.
  71. */
  72. bool hasNewlineChar() const { return mHasNewline; }
  73. private:
  74. friend class TextUtility;
  75. UINT32 mWidth;
  76. UINT32 mHeight;
  77. UINT32 mBaselineOffset;
  78. UINT32 mLineHeight;
  79. UINT32 mSpaceWidth;
  80. Vector<TextWord>::type mWords;
  81. TextWord* mLastWord;
  82. bool mHasNewline;
  83. void add(const CHAR_DESC& charDesc);
  84. void addSpace();
  85. void addWord(const TextWord& word);
  86. void finalize(bool hasNewlineChar);
  87. TextWord removeLastWord();
  88. void calculateBounds();
  89. };
  90. class CM_EXPORT TextData
  91. {
  92. public:
  93. ~TextData();
  94. const Vector<TextLine>::type& getLines() const { return mLines; }
  95. const Vector<HTexture>::type& getTexturePages() const { return mTexturePages; }
  96. const Vector<UINT32>::type& getNumQuadsPerPage() const { return mQuadsPerPage; }
  97. UINT32 getWidth() const;
  98. UINT32 getHeight() const;
  99. private:
  100. friend class TextUtility;
  101. Vector<UINT32>::type mQuadsPerPage;
  102. Vector<TextLine>::type mLines;
  103. Vector<HTexture>::type mTexturePages;
  104. };
  105. static std::shared_ptr<TextUtility::TextData> getTextData(const WString& text, const HFont& font, UINT32 fontSize, UINT32 width = 0, UINT32 height = 0, bool wordWrap = false);
  106. private:
  107. static void addCharToPage(TextData& data, UINT32 page, const FontData& fontData);
  108. };
  109. }