Cocos2d-x项目移植到WP8系列之八:CCLabelTTF显示中文不换行
原文链接: http://www.cnblogs.com/zouzf/p/3985330.html
在wp8平台上,CCLabeTTF显示中文不会自动换行,看了下源码,原来底层的实现是根据text的空格进行判断的,每遇到一个空格就判断是否超过label的宽度,超过就换行,但text如果是中文的话,哪来的空格给换行~~
以下实现全部参考 http://blog.csdn.net/hopingwhite/article/details/38414917 ,整理后的代码如下:
在CCFreeTypeFont.h里的CCFreeTypeFont类添加两个方法:
FT_Error addLine(const std::string& line);
void endLine_chinese();
实现如下:
void CCFreeTypeFont::endLine_chinese()
{
if(m_currentLine)
{
m_lines.push_back(m_currentLine);
compute_bbox(m_currentLine->glyphs, &m_currentLine->bbox);
m_currentLine->width = m_currentLine->bbox.xMax - m_currentLine->bbox.xMin;
m_textWidth = max(m_textWidth,m_currentLine->bbox.xMax - m_currentLine->bbox.xMin);
m_textHeight += m_lineHeight;
m_currentLine = NULL;
}
} FT_Error CCFreeTypeFont::addLine(const std::string& line)
{
wchar_t * pwszBuffer = nullptr; int num_chars = line.size();
int nBufLen = num_chars + ;
pwszBuffer = new wchar_t[nBufLen];
if (!pwszBuffer)
{
return -;
} memset(pwszBuffer, , nBufLen);
num_chars = MultiByteToWideChar(CP_UTF8, , line.c_str(), num_chars, pwszBuffer, nBufLen);
pwszBuffer[num_chars] = '\0'; int maxWidth = m_inWidth ? m_inWidth : m_windowWidth; newLine();
FT_Vector pen = m_currentLine->pen;
FT_GlyphSlot slot = m_face->glyph;
FT_UInt glyph_index;
FT_UInt previous = ;
FT_Error error = ;
unsigned int numGlyphs = ; FT_Bool useKerning = FT_HAS_KERNING(m_face); int n = ;
while (n < num_chars)
{
TGlyph glyph; /* convert character code to glyph index */
FT_ULong c = pwszBuffer[n++];
glyph_index = FT_Get_Char_Index(m_face, c); if (useKerning && previous && glyph_index)
{
FT_Vector delta;
FT_Get_Kerning(m_face, previous, glyph_index,
FT_KERNING_DEFAULT, &delta);
pen.x += delta.x >> ;
} /* store current pen position */
glyph.pos = pen;
glyph.index = glyph_index; /* load glyph image into the slot without rendering */
error = FT_Load_Glyph(m_face, glyph_index, FT_LOAD_DEFAULT);
if (error)
continue; /* ignore errors, jump to next glyph */ /* extract glyph image and store it in our table */
error = FT_Get_Glyph(m_face->glyph, &glyph.image);
if (error)
continue; /* ignore errors, jump to next glyph */ /* translate the glyph image now */
FT_Glyph_Transform(glyph.image, , &glyph.pos); /* increment pen position */
pen.x += slot->advance.x >> ; if (pen.x > maxWidth)
{
m_currentLine->pen = pen; // endLine();
endLine_chinese(); newLine();
pen = m_currentLine->pen;
previous = ;
n--;
}
else
{
m_currentLine->glyphs.push_back(glyph);
/* record current glyph index */
previous = glyph_index;
}
} if (m_currentLine)
{
m_currentLine->pen = pen; //endLine();
endLine_chinese();
} CC_SAFE_DELETE_ARRAY(pwszBuffer);
return error;
}
FT_Error CCFreeTypeFont::initGlyphs(const char* text)的实现改成如下:
FT_Error CCFreeTypeFont::initGlyphs(const char* text)
{
FT_Error error = ;
std::stringstream stringStream(text);
std::string line;
vector<std::string> lines;
vector<std::string> words; m_textWidth = ;
m_textHeight = ;
// the height of a line of text based on the max height of a glyph in the font size
m_lineHeight = ((m_face->size->metrics.ascender) >> ) - ((m_face->size->metrics.descender) >> ); m_lines.clear(); while(std::getline(stringStream, line) && !error)
{
/* newLine(); std::size_t prev = 0, pos;
while ((pos = line.find_first_of(" ", prev)) != std::string::npos)
{
if (pos > prev)
{
addWord(line.substr(prev, pos-prev));
}
prev = pos + 1;
}
if (prev < line.length())
{
addWord(line.substr(prev, std::string::npos));
}
endLine();
*/
addLine(line);
} return error;
}
再次感谢: http://blog.csdn.net/hopingwhite/article/details/38414917
原文链接: http://www.cnblogs.com/zouzf/p/3985330.html
Cocos2d-x项目移植到WP8系列之八:CCLabelTTF显示中文不换行的更多相关文章
- Cocos2d-x项目移植到WP8系列之二:开篇
原文链接: http://www.cnblogs.com/zouzf/p/3970130.html 开发环境一笔带过吧,主板和CPU要支持虚拟化技术,要开启才行,装个64位win8.1系统,win8不 ...
- Cocos2d-x项目移植到WP8系列之三:C++和C#的交互
原文链接: http://www.cnblogs.com/zouzf/p/3971021.html 上一篇提到工程使用 XAML 和 Direct3D 项目模板 是因为要涉及到C++和C#的交互,微软 ...
- Cocos2d-x项目移植到WP8系列之一:前传
原文链接: http://www.cnblogs.com/zouzf/p/3969993.html 许久没动笔了,随想一直都有动笔的想法,但拖来拖去,归根到底还是一个懒字吧 .发现人的惰性真是太强大了 ...
- Cocos2d-x项目移植到WP8系列之九:使用自定义shader
本文原链接:http://www.cnblogs.com/zouzf/p/3995132.html 有时候想得到一些例如灰度图等特殊的渲染效果,就得用到自定义shader,关于shader的一些背景知 ...
- Cocos2d-x项目移植到WP8系列之六:C#工程使用C++的DLL
原文链接: http://www.cnblogs.com/zouzf/p/3984510.html 此时,一些大问题都被解决后,整个工程基本能跑起来了,最后一个大问题是:业务层是用Lua开发的,底层的 ...
- Cocos2d-x项目移植到WP8系列之四:文件操作
原文链接: http://www.cnblogs.com/zouzf/p/3972457.html 读写文件Cocos已经用fopen fwrite来做好了,这里说的主要是文件和文件夹的创建.删除.判 ...
- Cocos2d-x项目移植到WP8系列之七:中文显示乱码
原文链接:http://www.cnblogs.com/zouzf/p/3984628.html C++和C#互调时经常会带一些参数过去例如最常见的字符串,如果字符串里有中文的话,会发现传递过去后变成 ...
- Cocos2d-x项目移植到WP8系列之五:播放MP3
原文链接: http://www.cnblogs.com/zouzf/p/3972549.html 这一块的细节还是不太了解,只是东凑西拼能跑起来而已 1.网上下载lamb库 生成需要的lib库,详情 ...
- Cocos2d-x项目移植到WP8小记
Cocos2d-x项目移植到WP8小记 作者: K.C. 日期: 10/24/2013 Date: 2013-10-24 00:33 Title: Cocos2d-x项目移植到WP8小记 Tags: ...
随机推荐
- web开发之php--- mvc 模式
http://www.cnblogs.com/archy_yu/p/4229929.html
- 关闭数据库时SHUTDOWN: waiting for active calls to complete.处理
有时候在关闭数据库时,发出shutdown immediate;命令后一直未关闭.查看ALERT日志.在等待一段时间后日志中有提示: SHUTDOWN: waiting for active call ...
- ASP.NET动态网站制作(25)-- ADO.NET(4)
前言:这节课老师主要讲网页当中内容的分页效果,自己写一个分页控件. 内容: 1.首先写出HTML代码: <div id="pager"> <%=GetPagerH ...
- NGINX快速部署
NGINX快速部署 #!/bin/bash ############################################################################## ...
- 如何在shell中处理异常(转)
似乎好像大概有句话是这么说得,好程序与坏程序之间的区别就在于它的鲁棒性,也就是在异常情况下该程序是否还是在可hold住状态,能否不死,不崩溃,或者不做出一些超出预期的事情.那要做好这些,自然而然就要学 ...
- 使用jQuery lazyload 实现图片延迟加载
下载地址 使用说明 0. 准备工作 下载jQuery和lazyload 插件(地址如上) 1. HTML 引入jQuery库和lazyload插件 <div id="imagesCon ...
- echarts之series,markLine、markPoint
一.标记线和标志点 markLine 标记线 标记线的添加,如最大值.最小值.平均值的标记线 symbol 标记的图形. data->type 特殊的标注类型,用于标注最大值最小值等. 可选: ...
- js实现文字列表无缝向上滚动
body{font-size:12px} #demo{overflow:hidden; height:80px; width:280px; margin:90px auto; position:rel ...
- 简述OSI七层协议模型、TCP/IP四层模型和五层协议之间的关系
一.OSI七层模型 OSI七层协议模型主要是:应用层(Application).表示层(Presentation).会话层(Session).传输层(Transport).网络层(Network).数 ...
- 我的Android进阶之旅------>RGB颜色查询对照表
因为兼容性问题,色阶板功能只能在IE浏览器中运行 RGB颜色对照表 #FFFFFF #FFFFF0 #FFFFE0 #FFFF00 #FFFAFA #FFFAF0 #FFF ...