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: ...
随机推荐
- Python 爬虫实战2 百度贴吧帖子
爬取百度贴吧的帖子.与上一篇不同的是,这次我们需要用到文件的相关操作. 本篇目标 对百度贴吧的任意帖子进行抓取 指定是否只抓取楼主发帖内容 将抓取到的内容分析并保存到文件 1.URL格式的确定 首先, ...
- PYTHON MYSQL 的表创建和插入
import mysql.connector cnx = mysql.connector.connect(user='xx',password='xx++.',host='139.107.11.166 ...
- iOS 集成阿里百川最新版(3.1.1.96) 实现淘宝授权登录以及调用淘宝客户端商品详情页
公司最近要做第三方登录,由于是做导购项目,必不可少的有淘宝的授权登录.本来就是一个授权登录,没什么大不了的.但淘宝的无线开放业务——阿里百川更新的最新版本3.1.1.96,开发文档不是不详细,是很 ...
- Yii GridView::widget
GridView::widget文档 http://demos.krajee.com/grid GridView::widget([ 'dataProvider' => $dataProvide ...
- 线程中sleep和wait区别
sleep和wait的区别有: 1,这两个方法来自不同的类分别是Thread和Object 2,最主要是sleep方法没有释放锁,而wait方法释放了锁,使得敏感词线程可以使用同步控制块或者方法. 3 ...
- lucene中的IndexWriter.setMaxFieldLength()
lucene中的IndexWriter.setMaxFieldLength() 老版本的Lucene中,IndexWriter的maxFieldLength是指一个索引中的最大的Field个数. 这个 ...
- ubuntu虚拟化平台libvrit-bin
http://download.cirros-cloud.net/0.3.5/ 下载 cirros-0.3.5-x86_64-disk.img 因为 KVM(准确说是 Libvirt)默认不接受远程管 ...
- 【BZOJ2882】工艺 后缀自动机
[BZOJ2882]工艺 Description 小敏和小燕是一对好朋友. 他们正在玩一种神奇的游戏,叫Minecraft. 他们现在要做一个由方块构成的长条工艺品.但是方块现在是乱的,而且由于机器的 ...
- IIPP迷你项目(三)“Stopwatch: The Game”
0 本周项目说明 这一次博客是Coursera的IIPP课程第三周迷你项目的实现,基础要求是做一个秒表,能start能stop能reset,更高的要求是在此秒表的基础上完成两个小游戏,但是鉴于第二个小 ...
- J - 迷宫问题(BFS)
J - 迷宫问题 Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Descriptio ...