这是篇旧文了,原文请猛戳:
http://galoisplusplus.coding....
cocos2d-x v3.2的Label实现BUG真是不少,前段时间恰好排查了几个与之相关的问题,在此记录一下。
文字换行文字换行是一个困扰我们挺长时间的问题:之前就常常有文字超过指定长度却没有换行的情况出现,后来加入韩文、泰文等“奇葩”文字后问题就更严重了。cocos2d-x引擎在v3.2后大改了这部分的实现,但由于涉及的改动太多,无法作为一个独立的patch单独apply过来,而且更新引擎版本对我们上线的游戏代价太大,也不可行。好在本渣不久前终于从各种游戏系统开发中抽出时间,完整地把这部分代码revIEw了一遍,结果发现全是Labeltextformatter::multilineText
中几行代码惹的祸,缩小了排查范围便不难fix了:
<!-- more -->
bool Labeltextformatter::multilineText(Label *theLabel){ auto limit = theLabel->_limitShowCount; auto strWhole = theLabel->_currentUTF16String; std::vector<char16_t> multiline_string; multiline_string.reserve( limit ); std::vector<char16_t> last_word; last_word.reserve( 25 ); bool isstartOfline = false,isstartOfWord = false; float startOfline = -1,startOfWord = -1; int skip = 0; int tIndex = 0; float scalsX = theLabel->getScaleX(); float linewidth = theLabel->_maxlinewidth; bool breaklineWithoutSpace = theLabel->_lineBreakWithoutSpaces; Label::LetterInfo* info = nullptr; for (int j = 0; j+skip < limit; j++) { info = & theLabel->_lettersInfo.at(j+skip); unsigned int justSkipped = 0; while (info->def.valIDDeFinition == false) { justSkipped++; tIndex = j+skip+justSkipped; if (strWhole[tIndex-1] == '\n') { StringUtils::trimUTF16Vector(last_word); last_word.push_back('\n'); multiline_string.insert(multiline_string.end(),last_word.begin(),last_word.end()); last_word.clear(); isstartOfWord = false; isstartOfline = false; startOfWord = -1; startOfline = -1; } if(tIndex < limit) { info = & theLabel->_lettersInfo.at( tIndex ); } else break; } skip += justSkipped; tIndex = j + skip; if (tIndex >= limit) break; char16_t character = strWhole[tIndex]; if (!isstartOfWord) { startOfWord = info->position.x * scalsX; isstartOfWord = true; } if (!isstartOfline) { startOfline = startOfWord; isstartOfline = true; } // 1) Whitespace. // 2) This character is non-CJK,but the last character is CJK bool isspace = StringUtils::isUnicodeSpace(character); bool isCJK = false; if(!isspace) { isCJK = StringUtils::isCJKUnicode(character); } if (isspace || (!last_word.empty() && StringUtils::isCJKUnicode(last_word.back()) && !isCJK)) { // if current character is white space,put it into the current word if (isspace) last_word.push_back(character); multiline_string.insert(multiline_string.end(),last_word.end()); last_word.clear(); isstartOfWord = false; startOfWord = -1; // put the CJK character in the last word // and put the non-CJK(ASCII) character in the current word if (!isspace) last_word.push_back(character); continue; } float posRight = (info->position.x + info->def.xAdvance) * scalsX; // Out of bounds. if (posRight - startOfline > linewidth) { if (!breaklineWithoutSpace && !isCJK) { last_word.push_back(character); int found = StringUtils::getIndexOfLastNotChar16(multiline_string,' '); if (found != -1) StringUtils::trimUTF16Vector(multiline_string); else multiline_string.clear(); if (multiline_string.size() > 0) multiline_string.push_back('\n'); isstartOfline = false; startOfline = -1; } else { StringUtils::trimUTF16Vector(last_word); //issue #8492:endless loop if not using system Font,and constrained length is less than one character wIDth if (isstartOfline && startOfWord == startOfline && last_word.size() == 0) last_word.push_back(character); else --j; last_word.push_back('\n'); multiline_string.insert(multiline_string.end(),last_word.end()); last_word.clear(); isstartOfWord = false; isstartOfline = false; startOfWord = -1; startOfline = -1; } } else { // Character is normal. last_word.push_back(character); } } multiline_string.insert(multiline_string.end(),last_word.end()); std::u16string strNew(multiline_string.begin(),multiline_string.end()); theLabel->_currentUTF16String = strNew; theLabel->computeStringNumlines(); theLabel->computeHorizontalKernings(theLabel->_currentUTF16String); return true;}描边显示不均匀
这是我们之前常常被美术大大们吐槽的地方:文字加描边后有的地方粗有的地方细,好蓝看啊...
后来本渣在网上看到大神的patch,又自我扫盲了FreeType的基础概念,总算看懂了。cocos2d-x引擎在FontFreeType::getGlyphBitmap
函数中会把不带描边的文字字形(glyph)和描边文字字形的bitmap都存到同一个数组里,在FontFreeType::renderCharat
中渲染。而描边文字字形是调用FreeType API生成的,其轮廓和不带描边的文字字形轮廓的间距并不能确保一定是我们所指定的描边大小,这个patch便是记下该间距和描边大小的offset,在拷贝bitmap时根据offset作调整。
其实cocos2d-x引擎在v3.2之后也改了这部分代码,但其实现思路却不如上述patch清晰,于是本渣便用了后者,并做了一点微小改动。
unsigned char* FontFreeType::getGlyphBitmap(unsigned short theChar,long &outWIDth,long &outHeight,Rect &outRect,int &xAdvance){ bool invalIDChar = true; unsigned char * ret = nullptr; do { if (!_FontRef) break; auto glyphIndex = FT_Get_Char_Index(_FontRef,theChar); if(!glyphIndex) break; if (_distanceFIEldEnabled) { if (FT_Load_Glyph(_FontRef,glyphIndex,FT_LOAD_RENDER | FT_LOAD_NO_HINTING | FT_LOAD_NO_autoHINT)) break; } else { if (FT_Load_Glyph(_FontRef,FT_LOAD_RENDER | FT_LOAD_NO_autoHINT)) break; } outRect.origin.x = _FontRef->glyph->metrics.horibearingX >> 6; outRect.origin.y = - (_FontRef->glyph->metrics.horibearingY >> 6); outRect.size.wIDth = (_FontRef->glyph->metrics.wIDth >> 6); outRect.size.height = (_FontRef->glyph->metrics.height >> 6); xAdvance = (static_cast<int>(_FontRef->glyph->metrics.horiAdvance >> 6)); outWIDth = _FontRef->glyph->bitmap.wIDth; outHeight = _FontRef->glyph->bitmap.rows; ret = _FontRef->glyph->bitmap.buffer; // apply patch from: http://my.oschina.net/u/1414326/blog/279456?fromerr=xX53o9Rq if (_outlinesize > 0) { auto copyBitmap = new unsigned char[outWIDth * outHeight]; memcpy(copyBitmap,ret,outWIDth * outHeight * sizeof(unsigned char)); long bitmapWIDth; long bitmapHeight; FT_BBox bBox; auto outlineBitmap = getGlyphBitmapWithOutline(theChar,bBox); if(outlineBitmap == nullptr) { ret = nullptr; delete [] copyBitmap; break; } long glyphMinX = outRect.origin.x; long glyphMaxX = outRect.origin.x + outWIDth; long glyphMinY = -outHeight - outRect.origin.y; long glyphMaxY = -outRect.origin.y; auto outlineMinX = bBox.xMin >> 6; auto outlineMaxX = bBox.xMax >> 6; auto outlineMinY = bBox.yMin >> 6; auto outlineMaxY = bBox.yMax >> 6; auto outlinewidth = outlineMaxX - outlineMinX; auto outlineHeight = outlineMaxY - outlineMinY; bitmapWIDth = outlineMaxX - outlineMinX; bitmapHeight = outlineMaxY - outlineMinY; int offsetWIDth = 0; int offsetHeight = 0; if(glyphMinX - outlineMinX != _outlinesize) { offsetWIDth = glyphMinX - outlineMinX - _outlinesize; } if(outlineMaxY - glyphMaxY != _outlinesize) { offsetHeight = outlineMaxY - glyphMaxY - _outlinesize; } long index; auto blendImage = new unsigned char[bitmapWIDth * bitmapHeight * 2]; memset(blendImage,bitmapWIDth * bitmapHeight * 2); for (int x = 0; x < bitmapWIDth; ++x) { for (int y = 0; y < bitmapHeight; ++y) { index = x + y * bitmapWIDth; blendImage[2 * index] = outlineBitmap[index]; } } long maxX = outWIDth + _outlinesize; long maxY = outHeight + _outlinesize; for (int x = _outlinesize + offsetWIDth; x < maxX + offsetWIDth & x < bitmapWIDth; ++x) { for (int y = _outlinesize + offsetHeight; y < maxY + offsetHeight & y < bitmapHeight; ++y) { index = x + y * bitmapWIDth; long index2 = x - _outlinesize - offsetWIDth + (y - _outlinesize - offsetHeight) * outWIDth; blendImage[2 * index + 1] = copyBitmap[index2]; } } outRect.origin.x = bBox.xMin >> 6; outRect.origin.y = - (bBox.yMax >> 6); xAdvance += bitmapWIDth - outRect.size.wIDth; outRect.size.wIDth = bitmapWIDth; outRect.size.height = bitmapHeight; outWIDth = bitmapWIDth; outHeight = bitmapHeight; delete [] outlineBitmap; delete [] copyBitmap; ret = blendImage; } invalIDChar = false; } while (0); if (invalIDChar) { outRect.size.wIDth = 0; outRect.size.height = 0; xAdvance = 0; return nullptr; } else { return ret; }}总结
以上是内存溢出为你收集整理的cocos2d-x v3.2 Label的一些坑全部内容,希望文章能够帮你解决cocos2d-x v3.2 Label的一些坑所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)