注:2014年12月23日有内存/性能优化更新,内容在下面分割线后
搞了几个小时,这个头疼的问题,我给出代码吧。
找到
libcocos2d/platform/winrt/CCFreeTypeFont.cpp
(其中libcocos2d是项目名)然后将其中的函数 addWord 替换为我提供的即可。实在找不到文件的同学直接搜索吧。
需要注意的是我只简单处理了一下,所以中文下只支持UTF8字符串,非UTF8字符串会出问题。当然英文环境下任然是用默认逻辑。
我的cocos2dx版本是 3.2,如果你的版本不是这个,存在个别差异,那就自己改改吧。
希望大家拿走代码的时候在下面评论一下,好提高我分享的积极性,你懂得。
FT_Error CCFreeTypeFont::addWord(const std::string& word) { std::vector<TGlyph> glyphs; // glyphs for the word FT_BBox bbox; // bounding box containing all of the glyphs in the word int maxWidth = m_inWidth ? m_inWidth : m_windowWidth; std::string newWord; if(m_currentLine->width > 0) { newWord = ' ' + word; } else { newWord = word; } FT_Error error = initWordGlyphs(glyphs, newWord, m_currentLine->pen); if(!error) { compute_bbox(glyphs, &bbox); /*判断添加进去后整个line是否显示宽度大于设定宽度,是的话进行截取*/ if (Application::getInstance()->getCurrentLanguage() == LanguageType::CHINESE && bbox.xMax > maxWidth) { std:size_t start = 0, end = word.length(); while (true) { while (true)//这个字符比最宽还要宽,则进行截取 { end--; FT_BBox validBBox = bbox; std::vector<TGlyph> validGlyphs; FTLineInfo validLine = FTLineInfo(); validLine.width = 0; validLine.pen.x = 0; validLine.pen.y = 0; //对UTF8字符进行切割 if (end != word.length()) { while (true) { unsigned char utf8charpart = word.at(end - 1); if ((utf8charpart & 0x80) != 0 && (utf8charpart & 0xe0) != 0xc0 && (utf8charpart & 0xf0) != 0xe0 && (utf8charpart & 0xf8) != 0xf0) { end--; } else { end--; break; } } } std::string validStr = word.substr(start, end - start); FT_Error validError = initWordGlyphs(validGlyphs, validStr, validLine.pen); if (validError) break; compute_bbox(validGlyphs, &validBBox); if (validBBox.xMax <= maxWidth) { m_currentLine->glyphs.insert(validLine.glyphs.end(), validGlyphs.begin(), validGlyphs.end()); if (m_currentLine->width == 0) { m_currentLine->bbox = validBBox; } else { m_currentLine->bbox.xMax = validBBox.xMax; } break; } } start = end; end = word.length() + 1; if (start == end - 1) break; bbox = FT_BBox(); endLine(); newLine(); } } else { if (m_currentLine->width == 0 || bbox.xMax <= maxWidth) { m_currentLine->glyphs.insert(m_currentLine->glyphs.end(), glyphs.begin(), glyphs.end()); if (m_currentLine->width == 0) { m_currentLine->bbox = bbox; } else { m_currentLine->bbox.xMax = bbox.xMax; } m_currentLine->width = m_currentLine->bbox.xMax - m_currentLine->bbox.xMin; } else { endLine(); newLine(); addWord(word); } } } return error; }
========================================快乐的分割线 ========================================
谢谢 @请让我过好不好 的反馈。我仔细调试后发现造成内存占用过大的原因是中途逐字符计算换行加载成的图像没有释放掉。并且算法是先计算整个字符串长度,然后逐渐从尾部减小一个字符从新计算,当小于最大宽度时候进行换行从新执行上面的计算。这种算法性能很低,因此我改为了从第一个字符开始计算,当算的字符超过一行,则使用上个字符的位置进行换行。
其实还有优化的空间,因为逐字符计算调用的是做事情比较多余的函数,但我没有那么多时间细扣了,下面贴出代码,与分割线之前的版本类似替换系统的 addWord 函数即可:
FT_Error CCFreeTypeFont::addWord(const std::string& word) { std::vector<TGlyph> glyphs; // glyphs for the word FT_BBox bbox; // bounding box containing all of the glyphs in the word int maxWidth = m_inWidth ? m_inWidth : m_windowWidth; std::string newWord; if(m_currentLine->width > 0) { newWord = ' ' + word; } else { newWord = word; } FT_Error error = initWordGlyphs(glyphs, newWord, m_currentLine->pen); if(!error) { compute_bbox(glyphs, &bbox); /*判断添加进去后整个line是否显示宽度大于设定宽度,是的话进行截取*/ if (Application::getInstance()->getCurrentLanguage() == LanguageType::CHINESE && bbox.xMax > maxWidth) { std:size_t start = 0, end = -1, lastValidEnd = 0; while (true) { while (true)//这个字符比最宽还要宽,则进行截取 { end++; FT_BBox validBBox = bbox; std::vector<TGlyph> validGlyphs; FTLineInfo validLine; validLine.width = 0; validLine.pen.x = 0; validLine.pen.y = 0; //对UTF8字符进行切割 while (true) { end++; if (end == word.length()) break; unsigned char utf8charpart = word.at(end); if (!((utf8charpart & 0x80) != 0 && (utf8charpart & 0xe0) != 0xc0 && (utf8charpart & 0xf0) != 0xe0 && (utf8charpart & 0xf8) != 0xf0)) { break; } } std::string validStr = word.substr(start, end - start); FT_Error validError = initWordGlyphs(validGlyphs, validStr, validLine.pen); if (validError) break; compute_bbox(validGlyphs, &validBBox); if (validBBox.xMax < maxWidth && end != word.length()) { lastValidEnd = end; for (auto glyph = validGlyphs.begin(); glyph != validGlyphs.end(); ++glyph) { FT_Done_Glyph(glyph->image); } continue; } else { std::string validStr = word.substr(start, lastValidEnd - start); lastValidEnd = end; FT_Error validError = initWordGlyphs(validGlyphs, validStr, validLine.pen); if (validError) break; compute_bbox(validGlyphs, &validBBox); m_currentLine->glyphs.insert(validLine.glyphs.end(), validGlyphs.begin(), validGlyphs.end()); if (m_currentLine->width == 0) { m_currentLine->bbox = validBBox; } else { m_currentLine->bbox.xMax = validBBox.xMax; } break; } } start = lastValidEnd; end = lastValidEnd; if (end == word.length()) break; FT_BBox emptybbox; bbox = emptybbox; endLine(); newLine(); } } else { if (m_currentLine->width == 0 || bbox.xMax <= maxWidth) { m_currentLine->glyphs.insert(m_currentLine->glyphs.end(), glyphs.begin(), glyphs.end()); if (m_currentLine->width == 0) { m_currentLine->bbox = bbox; } else { m_currentLine->bbox.xMax = bbox.xMax; } m_currentLine->width = m_currentLine->bbox.xMax - m_currentLine->bbox.xMin; } else { endLine(); newLine(); addWord(word); } } } return error; }