Font.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. /**
  2. * Copyright (c) 2006-2012 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. #include "GLee.h"
  32. namespace love
  33. {
  34. namespace graphics
  35. {
  36. namespace opengl
  37. {
  38. class Font : public Object, public Volatile
  39. {
  40. private:
  41. enum FontType
  42. {
  43. FONT_TRUETYPE = 1,
  44. FONT_IMAGE,
  45. FONT_UNKNOWN
  46. };
  47. struct Glyph
  48. {
  49. GLuint list;
  50. GLuint texture;
  51. int spacing;
  52. };
  53. love::font::Rasterizer * rasterizer;
  54. int height;
  55. float lineHeight;
  56. float mSpacing; // modifies the spacing by multiplying it with this value
  57. std::vector<GLuint> textures; // vector of packed textures
  58. std::map<int, Glyph *> glyphs; // maps glyphs to display lists
  59. FontType type;
  60. Image::Filter filter;
  61. static const int TEXTURE_WIDTH = 512;
  62. static const int TEXTURE_HEIGHT = 512;
  63. static const int TEXTURE_PADDING = 1;
  64. int texture_x, texture_y;
  65. int rowHeight;
  66. void createTexture();
  67. Glyph * addGlyph(int glyph);
  68. public:
  69. /**
  70. * Default constructor.
  71. *
  72. * @param data The font data to construct from.
  73. **/
  74. Font(love::font::Rasterizer * r, const Image::Filter& filter = Image::Filter());
  75. virtual ~Font();
  76. /**
  77. * Prints the text at the designated position with rotation and scaling.
  78. *
  79. * @param text A string.
  80. * @param x The x-coordinate.
  81. * @param y The y-coordinate.
  82. * @param angle The amount of rotation.
  83. * @param sx Scale along the x axis.
  84. * @param sy Scale along the y axis.
  85. * @param ox The origin offset along the x-axis.
  86. * @param oy The origin offset along the y-axis.
  87. * @param kx Shear along the x axis.
  88. * @param ky Shear along the y axis.
  89. **/
  90. void print(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);
  91. /**
  92. * Prints the character at the designated position.
  93. *
  94. * @param character A character.
  95. * @param x The x-coordinate.
  96. * @param y The y-coordinate.
  97. **/
  98. void print(char character, float x, float y);
  99. /**
  100. * Returns the height of the font.
  101. **/
  102. float getHeight() const;
  103. /**
  104. * Returns the width of the passed string.
  105. *
  106. * @param line A line of text.
  107. **/
  108. int getWidth(const std::string & line);
  109. int getWidth(const char * line);
  110. /**
  111. * Returns the width of the passed character.
  112. *
  113. * @param character A character.
  114. **/
  115. int getWidth(const char character);
  116. /**
  117. * Returns the maximal width of a wrapped string
  118. * and optionally the number of lines
  119. *
  120. * @param text The input text
  121. * @param wrap The number of pixels to wrap at
  122. * @param max_width Optional output of the maximum width
  123. * Returns a vector with the lines.
  124. **/
  125. std::vector<std::string> getWrap(const std::string text, float wrap, int * max_width = 0);
  126. /**
  127. * Sets the line height (which should be a number to multiply the font size by,
  128. * example: line height = 1.2 and size = 12 means that rendered line height = 12*1.2)
  129. * @param height The new line height.
  130. **/
  131. void setLineHeight(float height);
  132. /**
  133. * Returns the line height.
  134. **/
  135. float getLineHeight() const;
  136. /**
  137. * Sets the spacing modifier (changes the spacing between the characters the
  138. * same way that the line height does [multiplication]).
  139. * Note: The spacing must be set BEFORE the font is loaded to have any effect.
  140. * @param amount The amount of modification.
  141. **/
  142. void setSpacing(float amount);
  143. /**
  144. * Returns the spacing modifier.
  145. **/
  146. float getSpacing() const;
  147. // Implements Volatile.
  148. bool loadVolatile();
  149. void unloadVolatile();
  150. }; // Font
  151. } // opengl
  152. } // graphics
  153. } // love
  154. #endif // LOVE_GRAPHICS_OPENGL_FONT_H