PolyLabel.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. #include "PolyLabel.h"
  20. #include "PolyFont.h"
  21. using namespace Polycode;
  22. #define NORMAL_FT_FLAGS FT_LOAD_TARGET_LIGHT
  23. ColorRange::ColorRange(Color color, unsigned int rangeStart, unsigned int rangeEnd) {
  24. this->color = color;
  25. this->rangeStart = rangeStart;
  26. this->rangeEnd = rangeEnd;
  27. }
  28. Label::Label(Font *font, const String& text, int size, int antiAliasMode, bool premultiplyAlpha) : Image() {
  29. setPixelType(Image::IMAGE_RGBA);
  30. this->font = font;
  31. this->size = size;
  32. this->premultiplyAlpha = premultiplyAlpha;
  33. imageData = NULL;
  34. this->antiAliasMode = antiAliasMode;
  35. currentTextWidth = 0;
  36. currentTextHeight = 0;
  37. setText(text);
  38. }
  39. Label::~Label() {
  40. }
  41. int Label::getTextWidth(Font *font, const String& text, int size) {
  42. FT_Vector delta;
  43. FT_UInt previous = 0;
  44. FT_UInt glyph_index;
  45. FT_GlyphSlot slot = font->getFace()->glyph;
  46. FT_Set_Pixel_Sizes(font->getFace(), 0, size);
  47. int width = 0;
  48. String actualString = text; //StringUtil::replace(text, "\t", TAB_REPLACE);
  49. for(int i=0; i< actualString.length();i++)
  50. {
  51. if(actualString[i] == '\t') {
  52. glyph_index = FT_Get_Char_Index( font->getFace(), ' ');
  53. FT_Load_Glyph( font->getFace(), glyph_index, NORMAL_FT_FLAGS );
  54. for(int k=0 ; k < 4; k++) {
  55. FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
  56. width += slot->advance.x >> 6;
  57. }
  58. } else {
  59. glyph_index = FT_Get_Char_Index( font->getFace(), actualString[i] );
  60. if(previous && glyph_index) {
  61. FT_Get_Kerning(font->getFace(), previous, glyph_index, FT_KERNING_DEFAULT, &delta);
  62. width += delta.x >> 6;
  63. }
  64. FT_Load_Glyph( font->getFace(), glyph_index, NORMAL_FT_FLAGS );
  65. FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
  66. width += slot->advance.x >> 6;
  67. }
  68. }
  69. // +5 pixels safety zone :)
  70. return width+5;
  71. }
  72. int Label::getTextHeight(Font *font, const String& text, int size) {
  73. String actualString = text; //StringUtil::replace(text, "\t", TAB_REPLACE);
  74. int height = 0;
  75. FT_UInt glyph_index;
  76. FT_GlyphSlot slot = font->getFace()->glyph;
  77. FT_Set_Pixel_Sizes(font->getFace(), 0, size);
  78. for(int i=0; i< actualString.length();i++)
  79. {
  80. glyph_index = FT_Get_Char_Index( font->getFace(), actualString[i] );
  81. FT_Load_Glyph(font->getFace(), glyph_index, NORMAL_FT_FLAGS );
  82. FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
  83. if(slot->bitmap_top > height)
  84. height = slot->bitmap_top;
  85. }
  86. return height;
  87. }
  88. Number Label::getTextWidth() const {
  89. return currentTextWidth;
  90. }
  91. Number Label::getTextHeight() const {
  92. return currentTextHeight;
  93. }
  94. Font *Label::getFont() const {
  95. return font;
  96. }
  97. const String& Label::getText() const {
  98. return text;
  99. }
  100. void Label::clearColors() {
  101. colorRanges.clear();
  102. }
  103. void Label::setColorForRange(Color color, unsigned int rangeStart, unsigned int rangeEnd) {
  104. colorRanges.push_back(ColorRange(color, rangeStart, rangeEnd));
  105. }
  106. Color Label::getColorForIndex(unsigned int index) {
  107. for(int i=0; i < colorRanges.size(); i++) {
  108. if(index >= colorRanges[i].rangeStart && index <= colorRanges[i].rangeEnd) {
  109. return colorRanges[i].color;
  110. }
  111. }
  112. return Color(1.0,1.0,1.0,1.0);
  113. }
  114. void Label::setText(const String& text) {
  115. // Logger::logw((char*)text.c_str());
  116. this->text = text;
  117. if(!font)
  118. return;
  119. if(!font->isValid())
  120. return;
  121. String actualString = text; //StringUtil::replace(text, "\t", TAB_REPLACE);
  122. int textWidth = getTextWidth(font, actualString, size);
  123. int textHeight = size+getTextHeight(font, actualString, size);
  124. currentTextHeight = 0;
  125. createEmpty(textWidth,textHeight);
  126. int penX = 0;
  127. int xoff = 0;
  128. // int x,y;
  129. FT_Vector delta;
  130. FT_UInt previous = 0;
  131. FT_UInt glyph_index;
  132. FT_GlyphSlot slot = font->getFace()->glyph;
  133. FT_Set_Pixel_Sizes(font->getFace(), 0, size);
  134. // copy the freetype data into the texture
  135. for(int i=0; i< actualString.length();i++)
  136. {
  137. if(actualString[i] == (wchar_t)'\t') {
  138. glyph_index = FT_Get_Char_Index( font->getFace(), ' ');
  139. FT_Load_Glyph( font->getFace(), glyph_index, NORMAL_FT_FLAGS );
  140. for(int k=0 ; k < 4; k++) {
  141. FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
  142. penX += slot->advance.x >> 6;
  143. currentTextWidth = penX;
  144. previous = glyph_index;
  145. }
  146. } else {
  147. glyph_index = FT_Get_Char_Index( font->getFace(), (FT_ULong)actualString[i]);
  148. if(previous && glyph_index) {
  149. FT_Get_Kerning(font->getFace(), previous, glyph_index, FT_KERNING_DEFAULT, &delta);
  150. penX += delta.x >> 6;
  151. }
  152. FT_Load_Glyph(font->getFace(), glyph_index, NORMAL_FT_FLAGS);
  153. switch(antiAliasMode) {
  154. case ANTIALIAS_FULL:
  155. FT_Render_Glyph(slot, FT_RENDER_MODE_LIGHT );
  156. break;
  157. case ANTIALIAS_NONE:
  158. FT_Render_Glyph(slot, FT_RENDER_MODE_MONO);
  159. break;
  160. }
  161. Color glyphColor = getColorForIndex(i);
  162. int lineoffset = ((size-slot->bitmap_top) * (textWidth*4));
  163. xoff = ((penX + slot->bitmap_left)*4);
  164. switch(antiAliasMode) {
  165. case ANTIALIAS_FULL:
  166. for(int j = 0; j < ((slot->bitmap.width * slot->bitmap.rows)); j++) {
  167. if(!(j%slot->bitmap.width) && j !=0)
  168. lineoffset += (textWidth*4)-(slot->bitmap.width * 4);
  169. int newVal = imageData[xoff+lineoffset+3] + slot->bitmap.buffer[j];
  170. if(newVal > 255)
  171. newVal = 255;
  172. newVal = (int)(((Number)newVal) * glyphColor.a);
  173. imageData[xoff+lineoffset+3] = newVal;
  174. if(premultiplyAlpha) {
  175. imageData[xoff+lineoffset] = (int)((255.0 * glyphColor.r) * ((Number)imageData[xoff+lineoffset+3])/255.0);
  176. imageData[xoff+lineoffset+1] = (int)((255.0 * glyphColor.g) * ((Number)imageData[xoff+lineoffset+3])/255.0);
  177. imageData[xoff+lineoffset+2] = (int)((255.0 * glyphColor.b) * ((Number)imageData[xoff+lineoffset+3])/255.0);
  178. } else {
  179. imageData[xoff+lineoffset] = (int)(255.0 * glyphColor.r);
  180. imageData[xoff+lineoffset+1] = (int)(255.0 * glyphColor.g);
  181. imageData[xoff+lineoffset+2] = (int)(255.0 * glyphColor.b);
  182. }
  183. xoff += 4;
  184. }
  185. break;
  186. case ANTIALIAS_NONE:
  187. unsigned char *src = slot->bitmap.buffer;
  188. for(int j=0; j <slot->bitmap.rows;j++) {
  189. unsigned char b;
  190. unsigned char *bptr = src;
  191. for(int k=0; k < slot->bitmap.width ; k++){
  192. if (k%8==0){ b = (*bptr++);}
  193. imageData[xoff+lineoffset] = (int)(255.0 * glyphColor.r);
  194. imageData[xoff+lineoffset+1] = (int)(255.0 * glyphColor.g);
  195. imageData[xoff+lineoffset+2] = (int)(255.0 * glyphColor.b);
  196. imageData[xoff+lineoffset+3] = b&0x80 ? 255 : 0;
  197. xoff += 4;
  198. b <<= 1;
  199. }
  200. lineoffset += (textWidth*4)-(slot->bitmap.width * 4);
  201. src += slot->bitmap.pitch;
  202. }
  203. break;
  204. }
  205. if(slot->bitmap_top > currentTextHeight)
  206. currentTextHeight = slot->bitmap_top;
  207. penX += slot->advance.x >> 6;
  208. currentTextWidth = penX;
  209. previous = glyph_index;
  210. }
  211. }
  212. }