CmTextUtility.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. private:
  65. friend class TextUtility;
  66. UINT32 mWidth;
  67. UINT32 mHeight;
  68. UINT32 mBaselineOffset;
  69. UINT32 mLineHeight;
  70. UINT32 mSpaceWidth;
  71. vector<TextWord>::type mWords;
  72. TextWord* mLastWord;
  73. void add(const CHAR_DESC& charDesc);
  74. void addSpace();
  75. void addWord(const TextWord& word);
  76. TextWord removeLastWord();
  77. void calculateBounds();
  78. };
  79. class CM_EXPORT TextData
  80. {
  81. public:
  82. ~TextData();
  83. const vector<TextLine>::type& getLines() const { return mLines; }
  84. const vector<HTexture>::type& getTexturePages() const { return mTexturePages; }
  85. const vector<UINT32>::type& getNumQuadsPerPage() const { return mQuadsPerPage; }
  86. UINT32 getWidth() const;
  87. UINT32 getHeight() const;
  88. private:
  89. friend class TextUtility;
  90. vector<UINT32>::type mQuadsPerPage;
  91. vector<TextLine>::type mLines;
  92. vector<HTexture>::type mTexturePages;
  93. };
  94. static std::shared_ptr<TextUtility::TextData> getTextData(const String& text, const HFont& font, UINT32 fontSize, UINT32 width = 0, UINT32 height = 0, bool wordWrap = false);
  95. };
  96. }