PolyLabel.cpp 6.5 KB

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