PolyLabel.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include "PolyString.h"
  21. #include "PolyGlobals.h"
  22. #include "PolyColor.h"
  23. #include "PolyImage.h"
  24. #include "PolyFont.h"
  25. #include FT_GLYPH_H
  26. #include FT_IMAGE_H
  27. namespace Polycode {
  28. class Font;
  29. class GlyphData {
  30. public:
  31. GlyphData();
  32. ~GlyphData();
  33. void clearData();
  34. FT_Glyph *glyphs;
  35. FT_Vector *positions;
  36. FT_UInt num_glyphs;
  37. int trailingAdvance;
  38. };
  39. class ColorRange {
  40. public:
  41. ColorRange(Color color, unsigned int rangeStart, unsigned int rangeEnd);
  42. Color color;
  43. unsigned int rangeStart;
  44. unsigned int rangeEnd;
  45. };
  46. /**
  47. * An image that can render text into itself. This class is mostly used internally in SceneLabel, but can be used by itself to manually create text-based textures.
  48. */
  49. class _PolyExport Label : public Image {
  50. public:
  51. /**
  52. * Create a text label.
  53. * @param font Font to use for this label.
  54. * @param text Initial text to render.
  55. * @param size Pixel size of the text to render.
  56. * @param antiAliasMode Antialiasing mode. Can be ANTIALIAS_FULL, ANTIALIAS_NONE or ANTIALIAS_STRONG.
  57. * @param premultiplyAlpha If set to true, will premultiply alpha in the label image.
  58. * @see Font
  59. */
  60. Label(Font *font, const String& text, int size, int antiAliasMode, bool premultiplyAlpha = false);
  61. virtual ~Label();
  62. /**
  63. * Sets the text of the label.
  64. * @param text Text to set.
  65. */
  66. void setText(const String& text);
  67. /**
  68. * Returns the current text of the label.
  69. * @return Current text.
  70. */
  71. const String& getText() const;
  72. /**
  73. * Returns the pixel width for the specified string based on the current label font and size settings.
  74. * @param text Text to return width for.
  75. * @return Pixel width of specified text.
  76. */
  77. int getTextWidthForString(const String& text);
  78. /**
  79. * Returns the pixel height for the specified string based on the current label font and size settings.
  80. * @param text Text to return height for.
  81. * @return Pixel height of specified text.
  82. */
  83. int getTextHeightForString(const String& text);
  84. /**
  85. * Returns the width of the current text.
  86. * @return Width of the current text.
  87. */
  88. Number getTextWidth() const;
  89. /**
  90. * Returns the height of the current text.
  91. * @return Height of the current text.
  92. */
  93. Number getTextHeight() const;
  94. /**
  95. * Sets the color for a range of characters in the label. The colors are only applied upon the next call to setText, not the currently rendered text. This call appends the color range to a list of color ranges, so if you are calling this multiple times for the same ranges, you must call clearColors.
  96. * @param color The color to set for the specified range.
  97. * @param rangeStart Starting index of the specified range.
  98. * @param rangeEnd Ending index of the specified range.
  99. * @see clearColors
  100. */
  101. void setColorForRange(Color color, unsigned int rangeStart, unsigned int rangeEnd);
  102. /**
  103. * Clears the current label colors.
  104. * @see setColorForRange
  105. */
  106. void clearColors();
  107. /**
  108. * Returns the text color for specified character index.
  109. */
  110. Color getColorForIndex(unsigned int index);
  111. /**
  112. * Returns the premultiply alpha setting.
  113. */
  114. bool getPremultiplyAlpha() const;
  115. /**
  116. * If set to true, will premultiply alpha when text is set to the label.
  117. */
  118. void setPremultiplyAlpha(bool val);
  119. /**
  120. * Sets the Font used to render text in the label.
  121. * @see Font
  122. */
  123. void setFont(Font *newFont);
  124. /**
  125. * Returns the Font currently used to render text in the label.
  126. * @see Font
  127. */
  128. Font *getFont() const;
  129. /**
  130. * Sets the vertical pixel size of text rendered in the label.
  131. */
  132. void setSize(int newSize);
  133. /**
  134. * Return the current vertical pixel size of text rendered in the label.
  135. */
  136. unsigned int getSize() const;
  137. /**
  138. * Returns the current antialasing mode.
  139. */
  140. int getAntialiasMode() const;
  141. /**
  142. * Sets the antialiasing mode used to render text.
  143. * @param newMode Antialiasing mode. Can be ANTIALIAS_FULL, ANTIALIAS_NONE or ANTIALIAS_STRONG.
  144. */
  145. void setAntialiasMode(int newMode);
  146. static const int ANTIALIAS_FULL = 0;
  147. static const int ANTIALIAS_NONE = 1;
  148. static const int ANTIALIAS_STRONG = 2;
  149. static const int ANTIALIAS_LCD = 3;
  150. /**
  151. * Returns the pixel distance from top of image to the baseline of the rendered text.
  152. */
  153. int getBaselineAdjust();
  154. void setBackgroundColor(const Color &color);
  155. void setForegroundColor(const Color &color);
  156. void setColors(const Color &backgroundColor, const Color &foregroundColor);
  157. bool optionsChanged();
  158. protected:
  159. Color backgroundColor;
  160. Color foregroundColor;
  161. void computeStringBbox(GlyphData *glyphData, FT_BBox *abbox);
  162. void precacheGlyphs(String text, GlyphData *glyphData);
  163. void renderGlyphs(GlyphData *glyphData);
  164. void drawGlyphBitmap(FT_Bitmap *bitmap, unsigned int x, unsigned int y, const Color &glyphColor);
  165. bool _optionsChanged;
  166. GlyphData labelData;
  167. std::vector<ColorRange> colorRanges;
  168. int baseLineOffset;
  169. int xAdjustOffset;
  170. int baseLineAdjust;
  171. bool premultiplyAlpha;
  172. int antiAliasMode;
  173. int size;
  174. String text;
  175. Font *font;
  176. };
  177. }