標籤:style blog color os io 檔案 for art
搞了幾個小時,這個頭疼的問題,我給出代碼吧。
找到
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;}