2
0

Font.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. /**
  68. * Vertex coordinates, UVs and indices can be computed and stored in a Text object.
  69. * For static text labels that do not change frequently, this means these computations
  70. * need not be performed every frame.
  71. */
  72. class Text
  73. {
  74. friend class Font;
  75. public:
  76. /**
  77. * Constructor.
  78. */
  79. Text(const char* text);
  80. /**
  81. * Destructor.
  82. */
  83. ~Text();
  84. /**
  85. * Get the string that will be drawn from this Text object.
  86. */
  87. const char* getText();
  88. private:
  89. std::string _text;
  90. unsigned int _vertexCount;
  91. SpriteBatch::SpriteVertex* _vertices;
  92. unsigned int _indexCount;
  93. unsigned short* _indices;
  94. Vector4 _color;
  95. };
  96. /**
  97. * Creates a font from the given bundle.
  98. *
  99. * If the 'id' parameter is NULL, it is assumed that the Bundle at 'path'
  100. * contains exactly one Font resource. If the Bundle does not meet this criteria,
  101. * NULL is returned.
  102. *
  103. * If a font for the given path has already been loaded, the existing font will be
  104. * returned with its reference count increased.
  105. *
  106. * @param path The path to a bundle file containing a font resource.
  107. * @param id An optional ID of the font resource within the bundle (NULL for the first/only resource).
  108. *
  109. * @return The specified font.
  110. */
  111. static Font* create(const char* path, const char* id = NULL);
  112. /**
  113. * Creates a font with the given characteristics from the specified glyph array and texture map.
  114. *
  115. * This method will create a new Font object regardless of whether another Font is already
  116. * created with the same attributes.
  117. *
  118. * @param family The font family name.
  119. * @param style The font style.
  120. * @param size The font size.
  121. * @param glyphs An array of font glyphs, defining each character in the font within the texture map.
  122. * @param glyphCount The number of items in the glyph array.
  123. * @param texture A texture map containing rendered glyphs.
  124. *
  125. * @return The new Font.
  126. */
  127. static Font* create(const char* family, Style style, unsigned int size, Glyph* glyphs, int glyphCount, Texture* texture);
  128. /**
  129. * Returns the font size (max height of glyphs) in pixels.
  130. */
  131. unsigned int getSize();
  132. /**
  133. * Begins text drawing for this font.
  134. */
  135. void begin();
  136. /**
  137. * Ends text batching for this font and renders all drawn text.
  138. */
  139. void end();
  140. /**
  141. * Draws the specified text in a solid color, with a scaling factor.
  142. *
  143. * @param text The text to draw.
  144. * @param x The viewport x position to draw text at.
  145. * @param y The viewport y position to draw text at.
  146. * @param color The color of text.
  147. * @param size The size to draw text (0 for default size).
  148. * @param rightToLeft Whether to draw text from right to left.
  149. */
  150. void drawText(const char* text, int x, int y, const Vector4& color, unsigned int size = 0, bool rightToLeft = false);
  151. /**
  152. * Draws the specified text within a rectangular area, with a specified alignment and scale.
  153. * Clips text outside the viewport. Optionally wraps text to fit within the width of the viewport.
  154. *
  155. * @param text The text to draw.
  156. * @param area The viewport area to draw within. Text will be clipped outside this rectangle.
  157. * @param color The color of text.
  158. * @param size The size to draw text (0 for default size).
  159. * @param justify Justification of text within the viewport.
  160. * @param wrap Wraps text to fit within the width of the viewport if true.
  161. * @param rightToLeft Whether to draw text from right to left.
  162. * @param clip A region to clip text within after applying justification to the viewport area.
  163. */
  164. void drawText(const char* text, const Rectangle& area, const Vector4& color, unsigned int size = 0,
  165. Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false, const Rectangle* clip = NULL);
  166. /**
  167. * Draw a string from a precomputed Text object.
  168. *
  169. * @param text The text to draw.
  170. */
  171. void drawText(Text* text);
  172. /**
  173. * Create an immutable Text object from a given string.
  174. * Vertex coordinates, UVs and indices will be computed and stored in the Text object.
  175. * For static text labels that do not change frequently, this means these computations
  176. * need not be performed every frame.
  177. *
  178. * @param text The text to draw.
  179. * @param area The viewport area to draw within. Text will be clipped outside this rectangle.
  180. * @param color The color of text.
  181. * @param size The size to draw text (0 for default size).
  182. * @param justify Justification of text within the viewport.
  183. * @param wrap Wraps text to fit within the width of the viewport if true.
  184. * @param rightToLeft Whether to draw text from right to left.
  185. * @param clip A region to clip text within after applying justification to the viewport area.
  186. *
  187. * @return A Text object.
  188. */
  189. Text* createText(const char* text, const Rectangle& area, const Vector4& color, unsigned int size = 0,
  190. Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false, const Rectangle* clip = NULL);
  191. /**
  192. * Measures a string's width and height without alignment, wrapping or clipping.
  193. *
  194. * @param text The text to measure.
  195. * @param size The font height to scale to.
  196. * @param widthOut Destination for the text's width.
  197. * @param heightOut Destination for the text's height.
  198. */
  199. void measureText(const char* text, unsigned int size, unsigned int* widthOut, unsigned int* heightOut);
  200. /**
  201. * Measures a string's bounding box after alignment, wrapping and clipping within a viewport.
  202. *
  203. * @param text The text to measure.
  204. * @param clip The clip rectangle.
  205. * @param size The font height to scale to.
  206. * @param out Destination rectangle to store the bounds in.
  207. * @param justify Justification of text within the viewport.
  208. * @param wrap Whether to measure text with wrapping applied.
  209. * @param ignoreClip Whether to clip 'out' to the viewport. Set false for the bounds of what would actually be drawn
  210. * within the given viewport; true for bounds that are guaranteed to fit the entire string of text.
  211. */
  212. void measureText(const char* text, const Rectangle& clip, unsigned int size, Rectangle* out,
  213. Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool ignoreClip = false);
  214. /**
  215. * Get an character index into a string corresponding to the character nearest the given location within the clip region.
  216. */
  217. int getIndexAtLocation(const char* text, const Rectangle& clip, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
  218. Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false);
  219. /**
  220. * Get the location of the character at the given index.
  221. */
  222. void getLocationAtIndex(const char* text, const Rectangle& clip, unsigned int size, Vector2* outLocation, const unsigned int destIndex,
  223. Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false);
  224. /**
  225. * Gets the sprite batch for this Font.
  226. *
  227. * @return The sprite batch for this Font.
  228. */
  229. SpriteBatch* getSpriteBatch() const;
  230. /**
  231. * Gets the Justify value from the given string.
  232. * Returns ALIGN_TOP_LEFT if the string is unrecognized.
  233. *
  234. * @param justify The string such as "ALIGN_HCENTER" or "ALIGN_VCENTER_RIGHT".
  235. *
  236. * @return The Justify value.
  237. */
  238. static Justify getJustify(const char* justify);
  239. private:
  240. /**
  241. * Constructor.
  242. */
  243. Font();
  244. /**
  245. * Constructor.
  246. */
  247. Font(const Font& copy);
  248. /**
  249. * Destructor.
  250. */
  251. ~Font();
  252. void getMeasurementInfo(const char* text, const Rectangle& area, unsigned int size, Justify justify, bool wrap, bool rightToLeft,
  253. std::vector<int>* xPositions, int* yPosition, std::vector<unsigned int>* lineLengths);
  254. int getIndexOrLocation(const char* text, const Rectangle& clip, unsigned int size, const Vector2& inLocation, Vector2* outLocation,
  255. const int destIndex = -1, Justify justify = ALIGN_TOP_LEFT, bool wrap = true, bool rightToLeft = false);
  256. unsigned int getTokenWidth(const char* token, unsigned int length, unsigned int size, float scale);
  257. unsigned int getReversedTokenLength(const char* token, const char* bufStart);
  258. int handleDelimiters(const char** token, const unsigned int size, const int iteration, const int areaX, int* xPos, int* yPos, unsigned int* lineLength,
  259. std::vector<int>::const_iterator* xPositionsIt, std::vector<int>::const_iterator xPositionsEnd, unsigned int* charIndex = NULL,
  260. const Vector2* stopAtPosition = NULL, const int currentIndex = -1, const int destIndex = -1);
  261. void addLineInfo(const Rectangle& area, int lineWidth, int lineLength, Justify hAlign,
  262. std::vector<int>* xPositions, std::vector<unsigned int>* lineLengths, bool rightToLeft);
  263. std::string _path;
  264. std::string _id;
  265. std::string _family;
  266. Style _style;
  267. unsigned int _size;
  268. Glyph* _glyphs;
  269. unsigned int _glyphCount;
  270. Texture* _texture;
  271. SpriteBatch* _batch;
  272. Rectangle _viewport;
  273. };
  274. }
  275. #endif