Font.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. /**
  2. * Copyright (c) 2006-2013 LOVE Development Team
  3. *
  4. * This software is provided 'as-is', without any express or implied
  5. * warranty. In no event will the authors be held liable for any damages
  6. * arising from the use of this software.
  7. *
  8. * Permission is granted to anyone to use this software for any purpose,
  9. * including commercial applications, and to alter it and redistribute it
  10. * freely, subject to the following restrictions:
  11. *
  12. * 1. The origin of this software must not be misrepresented; you must not
  13. * claim that you wrote the original software. If you use this software
  14. * in a product, an acknowledgment in the product documentation would be
  15. * appreciated but is not required.
  16. * 2. Altered source versions must be plainly marked as such, and must not be
  17. * misrepresented as being the original software.
  18. * 3. This notice may not be removed or altered from any source distribution.
  19. **/
  20. #ifndef LOVE_GRAPHICS_OPENGL_FONT_H
  21. #define LOVE_GRAPHICS_OPENGL_FONT_H
  22. // STD
  23. #include <map>
  24. #include <string>
  25. #include <vector>
  26. // LOVE
  27. #include "common/Object.h"
  28. #include "font/Rasterizer.h"
  29. #include "graphics/Image.h"
  30. #include "OpenGL.h"
  31. namespace love
  32. {
  33. namespace graphics
  34. {
  35. namespace opengl
  36. {
  37. class Font : public Object, public Volatile
  38. {
  39. public:
  40. /**
  41. * Default constructor.
  42. *
  43. * @param data The font data to construct from.
  44. **/
  45. Font(love::font::Rasterizer *r, const Image::Filter &filter = Image::getDefaultFilter());
  46. virtual ~Font();
  47. /**
  48. * Prints the text at the designated position with rotation and scaling.
  49. *
  50. * @param text A string.
  51. * @param x The x-coordinate.
  52. * @param y The y-coordinate.
  53. * @param letter_spacing Additional spacing between letters.
  54. * @param angle The amount of rotation.
  55. * @param sx Scale along the x axis.
  56. * @param sy Scale along the y axis.
  57. * @param ox The origin offset along the x-axis.
  58. * @param oy The origin offset along the y-axis.
  59. * @param kx Shear along the x axis.
  60. * @param ky Shear along the y axis.
  61. **/
  62. void print(const std::string &text, float x, float y, float letter_spacing = 0.0f, float angle = 0.0f, float sx = 1.0f, float sy = 1.0f, float ox = 0.0f, float oy = 0.0f, float kx = 0.0f, float ky = 0.0f);
  63. /**
  64. * Returns the height of the font.
  65. **/
  66. float getHeight() const;
  67. /**
  68. * Returns the width of the passed string.
  69. *
  70. * @param str A string of text.
  71. **/
  72. int getWidth(const std::string &str);
  73. int getWidth(const char *str);
  74. /**
  75. * Returns the width of the passed character.
  76. *
  77. * @param character A character.
  78. **/
  79. int getWidth(unsigned int character);
  80. /**
  81. * Returns the maximal width of a wrapped string
  82. * and optionally the number of lines
  83. *
  84. * @param text The input text
  85. * @param wrap The number of pixels to wrap at
  86. * @param max_width Optional output of the maximum width
  87. * Returns a vector with the lines.
  88. **/
  89. std::vector<std::string> getWrap(const std::string &text, float wrap, int *max_width = 0);
  90. /**
  91. * Sets the line height (which should be a number to multiply the font size by,
  92. * example: line height = 1.2 and size = 12 means that rendered line height = 12*1.2)
  93. * @param height The new line height.
  94. **/
  95. void setLineHeight(float height);
  96. /**
  97. * Returns the line height.
  98. **/
  99. float getLineHeight() const;
  100. /**
  101. * Sets the spacing modifier (changes the spacing between the characters the
  102. * same way that the line height does [multiplication]).
  103. * Note: The spacing must be set BEFORE the font is loaded to have any effect.
  104. * @param amount The amount of modification.
  105. **/
  106. void setSpacing(float amount);
  107. /**
  108. * Returns the spacing modifier.
  109. **/
  110. float getSpacing() const;
  111. void setFilter(const Image::Filter &f);
  112. const Image::Filter &getFilter();
  113. // Implements Volatile.
  114. bool loadVolatile();
  115. void unloadVolatile();
  116. // Extra font metrics
  117. int getAscent() const;
  118. int getDescent() const;
  119. float getBaseline() const;
  120. private:
  121. enum FontType
  122. {
  123. FONT_TRUETYPE = 1,
  124. FONT_IMAGE,
  125. FONT_UNKNOWN
  126. };
  127. // thin wrapper for an array of 4 vertices
  128. struct GlyphQuad
  129. {
  130. vertex vertices[4];
  131. };
  132. struct Glyph
  133. {
  134. GLuint texture;
  135. int spacing;
  136. GlyphQuad quad;
  137. };
  138. // used to determine when to change textures in the vertex array generated when printing text
  139. struct GlyphArrayDrawInfo
  140. {
  141. GLuint texture;
  142. int startquad, numquads;
  143. // used when sorting with std::sort
  144. // sorts by texture first (binding textures is expensive) and relative position in memory second
  145. bool operator < (const GlyphArrayDrawInfo &other) const
  146. {
  147. if (texture != other.texture)
  148. return texture < other.texture;
  149. else
  150. return startquad < other.startquad;
  151. };
  152. };
  153. love::font::Rasterizer *rasterizer;
  154. int height;
  155. float lineHeight;
  156. float mSpacing; // modifies the spacing by multiplying it with this value
  157. int texture_size_index;
  158. int texture_width;
  159. int texture_height;
  160. std::vector<GLuint> textures; // vector of packed textures
  161. std::map<unsigned int, Glyph *> glyphs; // maps glyphs to quad information
  162. FontType type;
  163. Image::Filter filter;
  164. static const int NUM_TEXTURE_SIZES = 7;
  165. static const int TEXTURE_WIDTHS[NUM_TEXTURE_SIZES];
  166. static const int TEXTURE_HEIGHTS[NUM_TEXTURE_SIZES];
  167. static const int TEXTURE_PADDING = 1;
  168. int texture_x, texture_y;
  169. int rowHeight;
  170. bool initializeTexture(GLint format);
  171. void createTexture();
  172. Glyph *addGlyph(unsigned int glyph);
  173. Glyph *findGlyph(unsigned int glyph);
  174. }; // Font
  175. } // opengl
  176. } // graphics
  177. } // love
  178. #endif // LOVE_GRAPHICS_OPENGL_FONT_H