Font.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /**
  2. * Copyright (c) 2006-2015 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 <unordered_map>
  24. #include <string>
  25. #include <vector>
  26. // LOVE
  27. #include "common/config.h"
  28. #include "common/Object.h"
  29. #include "common/Matrix.h"
  30. #include "common/Vector.h"
  31. #include "font/Rasterizer.h"
  32. #include "graphics/Texture.h"
  33. #include "graphics/Volatile.h"
  34. #include "GLBuffer.h"
  35. #include "OpenGL.h"
  36. namespace love
  37. {
  38. namespace graphics
  39. {
  40. namespace opengl
  41. {
  42. class Font : public Object, public Volatile
  43. {
  44. public:
  45. typedef std::vector<uint32> Codepoints;
  46. enum AlignMode
  47. {
  48. ALIGN_LEFT,
  49. ALIGN_CENTER,
  50. ALIGN_RIGHT,
  51. ALIGN_JUSTIFY,
  52. ALIGN_MAX_ENUM
  53. };
  54. struct GlyphVertex
  55. {
  56. float x, y;
  57. float s, t;
  58. };
  59. struct TextInfo
  60. {
  61. int width;
  62. int height;
  63. };
  64. // Used to determine when to change textures in the generated vertex array.
  65. struct DrawCommand
  66. {
  67. GLuint texture;
  68. int startvertex;
  69. int vertexcount;
  70. };
  71. Font(love::font::Rasterizer *r, const Texture::Filter &filter = Texture::getDefaultFilter());
  72. virtual ~Font();
  73. std::vector<DrawCommand> generateVertices(const Codepoints &str, std::vector<GlyphVertex> &vertices, float extra_spacing = 0.0f, Vector offset = {}, TextInfo *info = nullptr);
  74. std::vector<DrawCommand> generateVertices(const std::string &text, std::vector<GlyphVertex> &vertices, float extra_spacing = 0.0f, Vector offset = Vector(), TextInfo *info = nullptr);
  75. std::vector<DrawCommand> generateVerticesFormatted(const std::string &text, float wrap, AlignMode align, std::vector<GlyphVertex> &vertices, TextInfo *info = nullptr);
  76. void drawVertices(const std::vector<DrawCommand> &drawcommands);
  77. /**
  78. * Prints the text at the designated position with rotation and scaling.
  79. *
  80. * @param text A string.
  81. * @param x The x-coordinate.
  82. * @param y The y-coordinate.
  83. * @param angle The amount of rotation.
  84. * @param sx Scale along the x axis.
  85. * @param sy Scale along the y axis.
  86. * @param ox The origin offset along the x-axis.
  87. * @param oy The origin offset along the y-axis.
  88. * @param kx Shear along the x axis.
  89. * @param ky Shear along the y axis.
  90. **/
  91. void print(const std::string &text, float x, float y, 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);
  92. void printf(const std::string &text, float x, float y, float wrap, AlignMode align, 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);
  93. /**
  94. * Returns the height of the font.
  95. **/
  96. float getHeight() const;
  97. /**
  98. * Returns the width of the passed string.
  99. *
  100. * @param str A string of text.
  101. **/
  102. int getWidth(const std::string &str);
  103. /**
  104. * Returns the width of the passed character.
  105. *
  106. * @param character A character.
  107. **/
  108. int getWidth(char character);
  109. /**
  110. * Returns the maximal width of a wrapped string
  111. * and optionally the number of lines
  112. *
  113. * @param text The input text
  114. * @param wraplimit The number of pixels to wrap at
  115. * @param max_width Optional output of the maximum width
  116. * Returns a vector with the lines.
  117. **/
  118. void getWrap(const std::string &text, float wraplimit, std::vector<std::string> &lines, std::vector<int> *line_widths = nullptr);
  119. void getWrap(const std::string &text, float wraplimit, std::vector<Codepoints> &lines, std::vector<int> *line_widths = nullptr);
  120. /**
  121. * Sets the line height (which should be a number to multiply the font size by,
  122. * example: line height = 1.2 and size = 12 means that rendered line height = 12*1.2)
  123. * @param height The new line height.
  124. **/
  125. void setLineHeight(float height);
  126. /**
  127. * Returns the line height.
  128. **/
  129. float getLineHeight() const;
  130. void setFilter(const Texture::Filter &f);
  131. const Texture::Filter &getFilter();
  132. // Implements Volatile.
  133. bool loadVolatile();
  134. void unloadVolatile();
  135. // Extra font metrics
  136. int getAscent() const;
  137. int getDescent() const;
  138. float getBaseline() const;
  139. bool hasGlyph(uint32 glyph) const;
  140. bool hasGlyphs(const std::string &text) const;
  141. void setFallbacks(const std::vector<Font *> &fallbacks);
  142. uint32 getTextureCacheID() const;
  143. static bool getConstant(const char *in, AlignMode &out);
  144. static bool getConstant(AlignMode in, const char *&out);
  145. static int fontCount;
  146. private:
  147. enum FontType
  148. {
  149. FONT_TRUETYPE,
  150. FONT_IMAGE,
  151. FONT_UNKNOWN
  152. };
  153. struct Glyph
  154. {
  155. GLuint texture;
  156. int spacing;
  157. GlyphVertex vertices[4];
  158. };
  159. struct TextureSize
  160. {
  161. int width;
  162. int height;
  163. };
  164. TextureSize getNextTextureSize() const;
  165. void createTexture();
  166. love::font::GlyphData *getRasterizerGlyphData(uint32 glyph);
  167. const Glyph &addGlyph(uint32 glyph);
  168. const Glyph &findGlyph(uint32 glyph);
  169. float getKerning(uint32 leftglyph, uint32 rightglyph);
  170. void codepointsFromString(const std::string &str, Codepoints &codepoints);
  171. void printv(const Matrix4 &t, const std::vector<DrawCommand> &drawcommands, const std::vector<GlyphVertex> &vertices);
  172. std::vector<StrongRef<love::font::Rasterizer>> rasterizers;
  173. int height;
  174. float lineHeight;
  175. int textureWidth;
  176. int textureHeight;
  177. // vector of packed textures
  178. std::vector<GLuint> textures;
  179. // maps glyphs to glyph texture information
  180. std::unordered_map<uint32, Glyph> glyphs;
  181. // map of left/right glyph pairs to horizontal kerning.
  182. std::unordered_map<uint64, float> kerning;
  183. FontType type;
  184. Texture::Filter filter;
  185. int textureX, textureY;
  186. int rowHeight;
  187. bool useSpacesAsTab;
  188. // Index buffer used for drawing quads with GL_TRIANGLES.
  189. QuadIndices quadIndices;
  190. // ID which is incremented when the texture cache is invalidated.
  191. uint32 textureCacheID;
  192. size_t textureMemorySize;
  193. static const int TEXTURE_PADDING = 1;
  194. // This will be used if the Rasterizer doesn't have a tab character itself.
  195. static const int SPACES_PER_TAB = 4;
  196. static StringMap<AlignMode, ALIGN_MAX_ENUM>::Entry alignModeEntries[];
  197. static StringMap<AlignMode, ALIGN_MAX_ENUM> alignModes;
  198. }; // Font
  199. } // opengl
  200. } // graphics
  201. } // love
  202. #endif // LOVE_GRAPHICS_OPENGL_FONT_H