PolyLabel.cpp 14 KB

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