wp8中文字不能换行问题,以及在代码中加入中文无法显示的处理办法。仅供大家研究学习。
将下面代码添加到cocos2dx中 CCFreeTypeFont.cpp 文件中,
并且使用我下面提供的addword()方法。
/*
*add by Relvin
*/
//Relvin input start
int IsTextUTF8(const std::string &str)
{
//来自网络
int i;
int nBytes = 0;//{UFT8可用1-6个字节编码,ASCII用一个字节
unsigned char chr;
bool bAllAscii = true; //{如果全部都是ASCII, 说明不是UTF-8
for (i = 0; i < str.length(); i++)
{
chr = str*;
if ((chr & 0x80) != 0) // {判断是否ASCII编码,如果不是,说明有可能是UTF-8,ASCII用7位编码,但用一个字节存,最高位标记为0,o0xxxxxxx
bAllAscii = false;
if (nBytes == 0) //{如果不是ASCII码,应该是多字节符,计算字节数
{
if (chr >= 0x80)
{
if (chr >= 0xFC && chr <= 0xFD)
nBytes = 6;
else if (chr >= 0xF8)
nBytes = 5;
else if (chr >= 0xF0)
nBytes = 4;
else if (chr >= 0xE0)
nBytes = 3;
else if (chr >= 0xC0)
nBytes = 2;
else
{
return false;
}
nBytes–;
}
}
else //{多字节符的非首字节,应为 10xxxxxx
{
if ((chr & 0xC0) != 0x80)
{
return false;
}
nBytes–;
}
}
if (nBytes > 0) //{违返规则
{
return false;
}
if (bAllAscii) //如果全部都是ASCII, 说明不是UTF-8
{
return false;
}
return true;
}
std::wstring Acsi2WideByte(const std::string& strascii)
{//来自网络
int widesize = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, NULL, 0);
if (widesize == ERROR_NO_UNICODE_TRANSLATION)
{
}
if (widesize == 0)
{
}
std::vector<wchar_t> resultstring(widesize);
int convresult = MultiByteToWideChar(CP_ACP, 0, (char*)strascii.c_str(), -1, &resultstring, widesize);
if (convresult != widesize)
{
}
return std::wstring(&resultstring);
}
std::string Unicode2Utf8(const std::wstring& widestring)
{//来自网络
int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);
if (utf8size == 0)
{
}
std::vector<char> resultstring(utf8size);
int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring, utf8size, NULL, NULL);
if (convresult != utf8size)
{
}
return std::string(&resultstring);
}
std::string ASCII2UTF_8(const std::string &strAsciiCode)
{//来自网络
std::string strRet("");
//
std::wstring wstr = Acsi2WideByte(strAsciiCode);
//
strRet = Unicode2Utf8(wstr);
return strRet;
}
int is_zh_ch(char p)
{
if (p >= 0x00 && p <= 0x7f)
{
return 0;
}
else
{
return 1;
}
}
bool is_en_ch(char p)
{
if ((p >= ‘a’ && p <= ‘z’) || (p >= ‘A’ && p <= ‘Z’))
{
return true;
}
return false;
}
std::string subString(std::string &str, int start, int end)
{//来自网络
if (typeid(str) == typeid(string) && str.length() > 0)
{
int len = str.length();
string tmp = “”;
vector <string> dump;
int i = 0;
while (i < len)
{
if (is_zh_ch(str.at(i)) == 1)
{
if (i + 2 < len)
{
dump.push_back(str.substr(i, 3));
i = i + 3;
}
else
{
dump.push_back(str.substr(i, len - i));
break;
}
}
else
{
dump.push_back(str.substr(i, 1));
i = i + 1;
}
}
int iDumpSize = dump.size();
for (i = end - 1; i >= start; i--)
{
if (dump*.length() == 0)
{
break;
}
if (is_en_ch(dump*))
{
if (i + 1 > iDumpSize || !is_en_ch(dump*))
{
end = i;
break;
}
else if ((i - 1 >= start) && (!is_en_ch(dump*)))
{
end = i - 1;
break;
}
}
else
{
end = i;
break;
}
}
CCLOG("Relvin >> end = %d", end);
end = end > 0 ? end : iDumpSize;
end = end > iDumpSize ? iDumpSize : end;
CCLOG("Relvin >> end = %d", end);
if (start<0 || start>end)
return "";
for (i = start; i <= end; i++)
{
tmp += dump*;
}
str = "";
for (i = end + 1; i < iDumpSize; i++)
{
str += dump*;
}
return tmp;
}
else
{
return "";
}
}
//Relvin input end
FT_Error CCFreeTypeFont::addWord(const std::string& word)
{
std::vector 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
{
if (!IsTextUTF8(word))
{
newWord = ASCII2UTF_8(word);
}
else
{
newWord = word;
}
}
FT_Error error = initWordGlyphs(glyphs, newWord, m_currentLine->pen);
if (!error)
{
compute_bbox(glyphs, &bbox);
if (m_currentLine->width == 0 || bbox.xMax <= maxWidth)
{
if (bbox.xMax > maxWidth && glyphs.size() > 2)
{
int strEnd = 0;
for (int i = 0; i < glyphs.size(); i++)
{
if (glyphs*.pos.x >= maxWidth)
{
strEnd = i - 3;
break;
}
}
if (strEnd <= 0)
{
strEnd = glyphs.size() - 2;
}
std::string tmpString = subString(newWord, 0, strEnd);
m_currentLine->pen.x = 0;
CCLOG("Relvin >> tmpString = %s >>>>> %d", tmpString.c_str(), strEnd);
addWord(tmpString);
endLine();
newLine();
CCLOG("Relvin >> newWord = %s ", newWord.c_str());
addWord(newWord);
}
else
{
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;
}