PolyLabel.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. //#define NORMAL_FT_FLAGS FT_LOAD_TARGET_LCD
  23. GlyphData::GlyphData() {
  24. glyphs = NULL;
  25. positions = NULL;
  26. num_glyphs = 0;
  27. trailingAdvance = 0;
  28. }
  29. GlyphData::~GlyphData() {
  30. clearData();
  31. }
  32. void GlyphData::clearData() {
  33. for(int i=0; i < num_glyphs; i++) {
  34. FT_Done_Glyph(glyphs[i]);
  35. }
  36. free(glyphs);
  37. free(positions);
  38. glyphs = NULL;
  39. positions = NULL;
  40. num_glyphs = 0;
  41. trailingAdvance = 0;
  42. }
  43. ColorRange::ColorRange(Color color, unsigned int rangeStart, unsigned int rangeEnd) {
  44. this->color = color;
  45. this->rangeStart = rangeStart;
  46. this->rangeEnd = rangeEnd;
  47. }
  48. Label::Label(Font *font, const String& text, int size, int antiAliasMode, bool premultiplyAlpha) : Image(), _optionsChanged(false) {
  49. setPixelType(Image::IMAGE_RGBA);
  50. this->font = font;
  51. this->size = size;
  52. this->premultiplyAlpha = premultiplyAlpha;
  53. imageData = NULL;
  54. this->antiAliasMode = antiAliasMode;
  55. setText(text);
  56. }
  57. Label::~Label() {
  58. }
  59. unsigned int Label::getSize() const {
  60. return size;
  61. }
  62. void Label::setSize(int newSize) {
  63. size = newSize;
  64. _optionsChanged = true;
  65. }
  66. int Label::getAntialiasMode() const {
  67. return antiAliasMode;
  68. }
  69. void Label::setAntialiasMode(int newMode) {
  70. antiAliasMode = newMode;
  71. _optionsChanged = true;
  72. }
  73. int Label::getTextWidthForString(const String& text) {
  74. if(!font)
  75. return 0;
  76. if(!font->isValid())
  77. return 0;
  78. GlyphData data;
  79. precacheGlyphs(text, &data);
  80. FT_BBox bbox;
  81. computeStringBbox(&data, &bbox);
  82. return (bbox.xMax - bbox.xMin);
  83. }
  84. int Label::getTextHeightForString(const String& text) {
  85. if(!font)
  86. return 0;
  87. if(!font->isValid())
  88. return 0;
  89. GlyphData data;
  90. precacheGlyphs(text, &data);
  91. FT_BBox bbox;
  92. computeStringBbox(&data, &bbox);
  93. return (bbox.yMax - bbox.yMin);
  94. }
  95. void Label::computeStringBbox(GlyphData *glyphData, FT_BBox *abbox) {
  96. FT_BBox bbox;
  97. /* initialize string bbox to "empty" values */
  98. bbox.xMin = bbox.yMin = 32000;
  99. bbox.xMax = bbox.yMax = -32000;
  100. /* for each glyph image, compute its bounding box, */
  101. /* translate it, and grow the string bbox */
  102. for (int n = 0; n < glyphData->num_glyphs; n++ )
  103. {
  104. FT_BBox glyph_bbox;
  105. FT_Glyph_Get_CBox( glyphData->glyphs[n], ft_glyph_bbox_pixels,
  106. &glyph_bbox );
  107. glyph_bbox.xMin += glyphData->positions[n].x;
  108. glyph_bbox.xMax += glyphData->positions[n].x;
  109. glyph_bbox.yMin += glyphData->positions[n].y;
  110. glyph_bbox.yMax += glyphData->positions[n].y;
  111. if ( glyph_bbox.xMin < bbox.xMin )
  112. bbox.xMin = glyph_bbox.xMin;
  113. if ( glyph_bbox.yMin < bbox.yMin )
  114. bbox.yMin = glyph_bbox.yMin;
  115. if ( glyph_bbox.xMax > bbox.xMax )
  116. bbox.xMax = glyph_bbox.xMax;
  117. if ( glyph_bbox.yMax > bbox.yMax )
  118. bbox.yMax = glyph_bbox.yMax;
  119. }
  120. /* check that we really grew the string bbox */
  121. if ( bbox.xMin > bbox.xMax )
  122. {
  123. bbox.xMin = 0;
  124. bbox.yMin = 0;
  125. bbox.xMax = 0;
  126. bbox.yMax = 0;
  127. }
  128. bbox.xMax += glyphData->trailingAdvance;
  129. /* return string bbox */
  130. *abbox = bbox;
  131. }
  132. Number Label::getTextWidth() const {
  133. return width;
  134. }
  135. Number Label::getTextHeight() const {
  136. return height;
  137. }
  138. Font *Label::getFont() const {
  139. return font;
  140. }
  141. void Label::setFont(Font *newFont) {
  142. if(!newFont)
  143. return;
  144. font = newFont;
  145. _optionsChanged = true;
  146. }
  147. const String& Label::getText() const {
  148. return text;
  149. }
  150. void Label::clearColors() {
  151. colorRanges.clear();
  152. _optionsChanged = true;
  153. }
  154. void Label::setColorForRange(Color color, unsigned int rangeStart, unsigned int rangeEnd) {
  155. colorRanges.push_back(ColorRange(color, rangeStart, rangeEnd));
  156. _optionsChanged = true;
  157. }
  158. Color Label::getColorForIndex(unsigned int index) {
  159. for(int i=0; i < colorRanges.size(); i++) {
  160. if(index >= colorRanges[i].rangeStart && index <= colorRanges[i].rangeEnd) {
  161. return colorRanges[i].color;
  162. }
  163. }
  164. return Color(1.0,1.0,1.0,1.0);
  165. }
  166. void Label::precacheGlyphs(String text, GlyphData *glyphData) {
  167. glyphData->clearData();
  168. std::wstring wstr = std::wstring(text.getWDataWithEncoding(String::ENCODING_UTF8));
  169. int num_chars = wstr.length();
  170. glyphData->glyphs = (FT_Glyph*) malloc(sizeof(FT_Glyph) * num_chars);
  171. glyphData->positions = (FT_Vector*) malloc(sizeof(FT_Vector) * num_chars);
  172. memset(glyphData->positions, 0, sizeof(FT_Vector) * num_chars);
  173. FT_Face face = font->getFace();
  174. FT_GlyphSlot slot = face->glyph;
  175. FT_UInt glyph_index;
  176. FT_Bool use_kerning;
  177. FT_UInt previous;
  178. FT_Error error;
  179. int pen_x = 0;
  180. int pen_y = 0;
  181. glyphData->num_glyphs = 0;
  182. use_kerning = FT_HAS_KERNING(font->getFace());
  183. previous = 0;
  184. FT_Set_Pixel_Sizes(face, 0, size);
  185. int advanceMultiplier;
  186. for(int n = 0; n < num_chars; n++ ) {
  187. if(wstr[n] == '\t') {
  188. glyph_index = FT_Get_Char_Index(face, ' ');
  189. advanceMultiplier = 4;
  190. } else {
  191. glyph_index = FT_Get_Char_Index(face, (FT_ULong)wstr[n]);
  192. advanceMultiplier = 1;
  193. }
  194. if(use_kerning && previous && glyph_index) {
  195. FT_Vector delta;
  196. FT_Get_Kerning( face, previous, glyph_index, FT_KERNING_DEFAULT, &delta);
  197. pen_x += delta.x >> 6;
  198. }
  199. glyphData->positions[glyphData->num_glyphs].x = pen_x;
  200. glyphData->positions[glyphData->num_glyphs].y = pen_y;
  201. switch(antiAliasMode) {
  202. case ANTIALIAS_FULL:
  203. case ANTIALIAS_STRONG:
  204. error = FT_Load_Glyph( face, glyph_index, FT_LOAD_TARGET_LIGHT);
  205. break;
  206. default:
  207. error = FT_Load_Glyph( face, glyph_index, FT_LOAD_DEFAULT);
  208. break;
  209. }
  210. if (error) {
  211. continue;
  212. }
  213. error = FT_Get_Glyph( face->glyph, &glyphData->glyphs[glyphData->num_glyphs] );
  214. if ( error ) {
  215. continue;
  216. }
  217. if(n == num_chars-1 && (wstr[n] == ' ' || wstr[n] == '\t')) {
  218. glyphData->trailingAdvance = (slot->advance.x >> 6) * advanceMultiplier;
  219. }
  220. pen_x += (slot->advance.x >> 6) * advanceMultiplier;
  221. previous = glyph_index;
  222. glyphData->num_glyphs++;
  223. }
  224. }
  225. int Label::getBaselineAdjust() {
  226. return baseLineAdjust;
  227. }
  228. void Label::drawGlyphBitmap(FT_Bitmap *bitmap, unsigned int x, unsigned int y, Color glyphColor) {
  229. int lineoffset = (height-y) * (width*4);
  230. int xoff = (x*4);
  231. Number alphaMultiplier = 1.0;
  232. if(antiAliasMode == ANTIALIAS_STRONG) {
  233. alphaMultiplier = 1.2;
  234. }
  235. switch(antiAliasMode) {
  236. case ANTIALIAS_FULL:
  237. case ANTIALIAS_STRONG:
  238. for(int j = 0; j < ((bitmap->width * bitmap->rows)); j++) {
  239. if(!(j % bitmap->width) && j !=0)
  240. lineoffset -= ((width*4)+(bitmap->width * 4));
  241. int newVal = imageData[xoff+lineoffset+3] + (bitmap->buffer[j] * alphaMultiplier);
  242. if(newVal > 255)
  243. newVal = 255;
  244. // newVal = (int)(((Number)newVal) * glyphColor.a);
  245. imageData[xoff+lineoffset+3] = newVal;
  246. if(premultiplyAlpha) {
  247. imageData[xoff+lineoffset] = (int)((255.0 * glyphColor.r) * ((Number)imageData[xoff+lineoffset+3])/255.0);
  248. imageData[xoff+lineoffset+1] = (int)((255.0 * glyphColor.g) * ((Number)imageData[xoff+lineoffset+3])/255.0);
  249. imageData[xoff+lineoffset+2] = (int)((255.0 * glyphColor.b) * ((Number)imageData[xoff+lineoffset+3])/255.0);
  250. } else {
  251. imageData[xoff+lineoffset] = (int)(255.0 * glyphColor.r);
  252. imageData[xoff+lineoffset+1] = (int)(255.0 * glyphColor.g);
  253. imageData[xoff+lineoffset+2] = (int)(255.0 * glyphColor.b);
  254. }
  255. xoff += 4;
  256. }
  257. break;
  258. case ANTIALIAS_NONE:
  259. unsigned char *src = bitmap->buffer;
  260. for(int j=0; j < bitmap->rows;j++) {
  261. unsigned char b;
  262. unsigned char *bptr = src;
  263. for(int k=0; k < bitmap->width ; k++){
  264. if (k%8==0){ b = (*bptr++);}
  265. imageData[xoff+lineoffset] = (int)(255.0 * glyphColor.r);
  266. imageData[xoff+lineoffset+1] = (int)(255.0 * glyphColor.g);
  267. imageData[xoff+lineoffset+2] = (int)(255.0 * glyphColor.b);
  268. imageData[xoff+lineoffset+3] = b&0x80 ? 255 : 0;
  269. xoff += 4;
  270. b <<= 1;
  271. }
  272. lineoffset -= ((width*4)+(bitmap->width * 4));
  273. src += bitmap->pitch;
  274. }
  275. break;
  276. }
  277. }
  278. void Label::renderGlyphs(GlyphData *glyphData) {
  279. bool useColorRanges = false;
  280. if(colorRanges.size() > 0) {
  281. useColorRanges = true;
  282. }
  283. Color glyphColor = Color(1.0, 1.0, 1.0, 1.0);
  284. int start_x = 0; //( ( my_target_width - string_width ) / 2 ) * 64;
  285. int start_y = 0; //( ( my_target_height - string_height ) / 2 ) * 64;
  286. FT_Error error;
  287. for (int n = 0; n < glyphData->num_glyphs; n++ ) {
  288. FT_Glyph image;
  289. FT_Vector pen;
  290. image = glyphData->glyphs[n];
  291. pen.x = (start_x + glyphData->positions[n].x) * 64;
  292. pen.y = (start_y + glyphData->positions[n].y) * 64;
  293. if(antiAliasMode == ANTIALIAS_FULL || antiAliasMode == ANTIALIAS_STRONG) {
  294. error = FT_Glyph_To_Bitmap( &image, FT_RENDER_MODE_LIGHT, &pen, 0 );
  295. } else {
  296. error = FT_Glyph_To_Bitmap( &image, FT_RENDER_MODE_MONO, &pen, 0 );
  297. }
  298. if(!error ) {
  299. if(useColorRanges) {
  300. glyphColor = getColorForIndex(n);
  301. }
  302. FT_BitmapGlyph bit = (FT_BitmapGlyph)image;
  303. drawGlyphBitmap(&bit->bitmap,
  304. bit->left - xAdjustOffset,
  305. height - bit->top + baseLineOffset, glyphColor);
  306. FT_Done_Glyph( image );
  307. }
  308. }
  309. }
  310. bool Label::optionsChanged() {
  311. return _optionsChanged;
  312. }
  313. void Label::setText(const String& text) {
  314. if(!font)
  315. return;
  316. if(!font->isValid())
  317. return;
  318. this->text = text;
  319. precacheGlyphs(text, &labelData);
  320. FT_BBox bbox;
  321. computeStringBbox(&labelData, &bbox);
  322. unsigned int textWidth = (bbox.xMax - bbox.xMin)+1;
  323. unsigned int textHeight = (bbox.yMax - bbox.yMin)+1;
  324. /*
  325. if(textWidth % 2 ){
  326. textWidth++;
  327. }
  328. if(textHeight % 2 ){
  329. textHeight++;
  330. }
  331. */
  332. baseLineOffset = bbox.yMin;
  333. xAdjustOffset = bbox.xMin;
  334. baseLineAdjust = bbox.yMax;
  335. createEmpty(textWidth,textHeight);
  336. renderGlyphs(&labelData);
  337. _optionsChanged = false;
  338. }