Font.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. #ifndef FONT_H_
  2. #define FONT_H_
  3. #include "SpriteBatch.h"
  4. namespace gameplay
  5. {
  6. /**
  7. * Defines a font for text rendering.
  8. */
  9. class Font : public Ref
  10. {
  11. friend class Bundle;
  12. friend class TextBox;
  13. public:
  14. /**
  15. * Defines the set of allowable font styles.
  16. */
  17. enum Style
  18. {
  19. PLAIN = 0,
  20. BOLD = 1,
  21. ITALIC = 2,
  22. BOLD_ITALIC = 4
  23. };
  24. /**
  25. * Defines the set of allowable alignments when drawing text.
  26. */
  27. enum Justify
  28. {
  29. // Specify horizontal alignment, use default vertical alignment (ALIGN_TOP).
  30. ALIGN_LEFT = 0x01,
  31. ALIGN_HCENTER = 0x02,
  32. ALIGN_RIGHT = 0x04,
  33. // Specify vertical alignment, use default horizontal alignment (ALIGN_LEFT).
  34. ALIGN_TOP = 0x10,
  35. ALIGN_VCENTER = 0x20,
  36. ALIGN_BOTTOM = 0x40,
  37. // Specify both vertical and horizontal alignment.
  38. ALIGN_TOP_LEFT = ALIGN_TOP | ALIGN_LEFT,
  39. ALIGN_VCENTER_LEFT = ALIGN_VCENTER | ALIGN_LEFT,
  40. ALIGN_BOTTOM_LEFT = ALIGN_BOTTOM | ALIGN_LEFT,
  41. ALIGN_TOP_HCENTER = ALIGN_TOP | ALIGN_HCENTER,
  42. ALIGN_VCENTER_HCENTER = ALIGN_VCENTER | ALIGN_HCENTER,
  43. ALIGN_BOTTOM_HCENTER = ALIGN_BOTTOM | ALIGN_HCENTER,
  44. ALIGN_TOP_RIGHT = ALIGN_TOP | ALIGN_RIGHT,
  45. ALIGN_VCENTER_RIGHT = ALIGN_VCENTER | ALIGN_RIGHT,
  46. ALIGN_BOTTOM_RIGHT = ALIGN_BOTTOM | ALIGN_RIGHT
  47. };
  48. /**
  49. * Defines a font glyph within the texture map for a font.
  50. */
  51. class Glyph
  52. {
  53. public:
  54. /**
  55. * Glyph character code (decimal value).
  56. */
  57. unsigned int code;
  58. /**
  59. * Glyph width (in pixels).
  60. */
  61. unsigned int width;
  62. /**
  63. * Glyph texture coordinates.
  64. */
  65. float uvs[4];
  66. };
  67. class TextBatch
  68. {
  69. public:
  70. SpriteBatch::SpriteVertex* _vertices;
  71. unsigned int _vertexCount;
  72. };
  73. /**
  74. * Creates a font from the given bundle.
  75. *
  76. * If the 'id' parameter is NULL, it is assumed that the Bundle at 'path'
  77. * contains exactly one Font resource. If the Bundle does not meet this criteria,
  78. * NULL is returned.
  79. *
  80. * If a font for the given path has already been loaded, the existing font will be
  81. * returned with its reference count increased.
  82. *
  83. * @param path The path to a bundle file containing a font resource.
  84. * @param id An optional ID of the font resource within the bundle (NULL for the first/only resource).
  85. *
  86. * @return The specified font.
  87. */
  88. static Font* create(const char* path, const char* id = NULL);
  89. /**
  90. * Creates a font with the given characteristics from the specified glyph array and texture map.
  91. *
  92. * This method will create a new Font object regardless of whether another Font is already
  93. * created with the same attributes.
  94. *
  95. * @param family The font family name.
  96. * @param style The font style.
  97. * @param size The font size.
  98. * @param glyphs An array of font glyphs, defining each character in the font within the texture map.
  99. * @param glyphCount The number of items in the glyph array.
  100. * @param texture A texture map containing rendered glyphs.
  101. *
  102. * @return The new Font.
  103. */
  104. static Font* create(const char* family, Style style, unsigned int size, Glyph* glyphs, int glyphCount, Texture* texture);
  105. /**
  106. * Returns the font size (max height of glyphs) in pixels.
  107. */
  108. unsigned int getSize();
  109. /**
  110. * Begins text drawing for this font.
  111. */
  112. void begin();
  113. /**
  114. * Ends text batching for this font and renders all drawn text.
  115. */
  116. void end();
  117. /**
  118. * Compute vertex coordinates and UVs for a given string.
  119. */
  120. TextBatch* getTextBatch(const char* text, const Rectangle& area, const Vector4& color, unsigned int size = 0,
  121. Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false, const Rectangle* clip = NULL);
  122. /**
  123. * Draw a string from a precomputed StringBatch.
  124. */
  125. void drawTextBatch(TextBatch* textBatch);
  126. /**
  127. * Draws the specified text in a solid color, with a scaling factor.
  128. *
  129. * @param text The text to draw.
  130. * @param x The viewport x position to draw text at.
  131. * @param y The viewport y position to draw text at.
  132. * @param color The color of text.
  133. * @param size The size to draw text (0 for default size).
  134. * @param rightToLeft Whether to draw text from right to left.
  135. */
  136. void drawText(const char* text, int x, int y, const Vector4& color, unsigned int size = 0, bool rightToLeft = false);
  137. /**
  138. * Draws the specified text within a rectangular area, with a specified alignment and scale.
  139. * Clips text outside the viewport. Optionally wraps text to fit within the width of the viewport.
  140. *
  141. * @param text The text to draw.
  142. * @param area The viewport area to draw within. Text will be clipped outside this rectangle.
  143. * @param color The color of text.
  144. * @param size The size to draw text (0 for default size).
  145. * @param justify Justification of text within the viewport.
  146. * @param wrap Wraps text to fit within the width of the viewport if true.
  147. * @param rightToLeft Whether to draw text from right to left.
  148. * @param clip A region to clip text within after applying justification to the viewport area.
  149. */
  150. void drawText(const char* text, const Rectangle& area, const Vector4& color, unsigned int size = 0,
  151. Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false, const Rectangle* clip = NULL);
  152. /**
  153. * Measures a string's width and height without alignment, wrapping or clipping.
  154. *
  155. * @param text The text to measure.
  156. * @param size The font height to scale to.
  157. * @param widthOut Destination for the text's width.
  158. * @param heightOut Destination for the text's height.
  159. */
  160. void measureText(const char* text, unsigned int size, unsigned int* widthOut, unsigned int* heightOut);
  161. /**
  162. * Measures a string's bounding box after alignment, wrapping and clipping within a viewport.
  163. *
  164. * @param text The text to measure.
  165. * @param clip The clip rectangle.
  166. * @param size The font height to scale to.
  167. * @param out Destination rectangle to store the bounds in.
  168. * @param justify Justification of text within the viewport.
  169. * @param wrap Whether to measure text with wrapping applied.
  170. * @param ignoreClip Whether to clip 'out' to the viewport. Set false for the bounds of what would actually be drawn
  171. * within the given viewport; true for bounds that are guaranteed to fit the entire string of text.
  172. */
  173. void measureText(const char* text, const Rectangle& clip, unsigned int size, Rectangle* out,
  174. Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool ignoreClip = false);
  175. /**
  176. * Get an index into a string corresponding to the character nearest the given location within the clip region.
  177. */
  178. int getIndexAtLocation(const char* text, const Rectangle& clip, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
  179. Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false);
  180. /**
  181. * Get the location of the character at the given index.
  182. */
  183. void getLocationAtIndex(const char* text, const Rectangle& clip, unsigned int size, Vector2* outLocation, const unsigned int destIndex,
  184. Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false);
  185. /**
  186. * Gets the sprite batch for this Font.
  187. *
  188. * @return The sprite batch for this Font.
  189. */
  190. SpriteBatch* getSpriteBatch() const;
  191. /**
  192. * Gets the Justify value from the given string.
  193. * Returns ALIGN_TOP_LEFT if the string is unrecognized.
  194. *
  195. * @param justify The string such as "ALIGN_HCENTER" or "ALIGN_VCENTER_RIGHT".
  196. *
  197. * @return The Justify value.
  198. */
  199. static Justify getJustify(const char* justify);
  200. private:
  201. /**
  202. * Constructor.
  203. */
  204. Font();
  205. /**
  206. * Constructor.
  207. */
  208. Font(const Font& copy);
  209. /**
  210. * Destructor.
  211. */
  212. ~Font();
  213. void getMeasurementInfo(const char* text, const Rectangle& area, unsigned int size, Justify justify, bool wrap, bool rightToLeft,
  214. std::vector<int>* xPositions, int* yPosition, std::vector<unsigned int>* lineLengths);
  215. int getIndexOrLocation(const char* text, const Rectangle& clip, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
  216. const int destIndex = -1, Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false);
  217. unsigned int getTokenWidth(const char* token, unsigned int length, unsigned int size, float scale);
  218. unsigned int getReversedTokenLength(const char* token, const char* bufStart);
  219. int handleDelimiters(const char** token, const unsigned int size, const int iteration, const int areaX, int* xPos, int* yPos, unsigned int* lineLength,
  220. std::vector<int>::const_iterator* xPositionsIt, std::vector<int>::const_iterator xPositionsEnd, unsigned int* charIndex = NULL,
  221. const Vector2* stopAtPosition = NULL, const int currentIndex = -1, const int destIndex = -1);
  222. void addLineInfo(const Rectangle& area, int lineWidth, int lineLength, Justify hAlign,
  223. std::vector<int>* xPositions, std::vector<unsigned int>* lineLengths, bool rightToLeft);
  224. std::string _path;
  225. std::string _id;
  226. std::string _family;
  227. Style _style;
  228. unsigned int _size;
  229. Glyph* _glyphs;
  230. unsigned int _glyphCount;
  231. Texture* _texture;
  232. SpriteBatch* _batch;
  233. Rectangle _viewport;
  234. };
  235. }
  236. #endif