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: ...
随机推荐
- 四个 jQuery 方法:
append() - 在被选元素的结尾插入内容 prepend() - 在被选元素的开头插入内容 after() - 在被选元素之后插入内容 before() - 在被选元素之前插入内容
- Eclipse 创建 Java 包
打开新建 Java 包向导 你可以使用新建 Java 包向导来创建 Java 包.Java 包向导打开方式有: 通过点击 "File" 菜单并选择 New > Package ...
- oracle ora-01652/oracle表空间
参考: oracel bigfile tablespace:(推荐) http://blog.chinaunix.net/uid-20779720-id-3078273.html ora-01652解 ...
- JEECMS 2.4.2 之添加新的可扩展的ftl模版文件、自定义方法
Demo: <@cms.CfgList isPage='1' league='0' recommend='0' lala='0' hot='1' memberId='0' pageNo=page ...
- ABAP小白的成长日记--------helloblog
在外企公司培训了3个月,系统的学习了ABAP,希望开通Blog以后和大家一起深入学习交流.印度人的办事效率是出奇的低,赶超国企公务员.虽然内容cover到了几乎所有R/4的内容,但是还有很多知识没有真 ...
- Android无线测试之—UiAutomator工程建立
一.环境要求: 已经搭建好了Android UiAotomator测试环境 二.新建测试工程: 1.打开eclipse,首次打开指定一个工作空间 2.新建一个Java Project,名叫:Demo1 ...
- X明X源面试题《一》
本文转载自zhangkang 今天去明源面试,面试题目如下 1 有两张表 A 学生表 ID Name age 1 李1 ...
- python学习【第七篇】python文件操作
一.文件操作过程 1. 打开文件,得到文件句柄并赋值给一个变量2. 通过句柄对文件进行操作3. 关闭文件 # 1.打开文件,得到文件句柄 f_handle = open('aa.txt', 'r', ...
- 从网上搜索到的一些关于pcap源代码,入门级的
/*pcap_1.c*/ #include <stdio.h>#include <stdlib.h>#include <pcap.h> /* 如果没有pcap的系 ...
- 《从零开始学Swift》学习笔记(Day 36)——静态方法
原创文章,欢迎转载.转载请注明:关东升的博客 静态方法与静态属性类似,Swift中定义了静态方法,也称为类型方法.静态方法的定义与静态属性类似,枚举和结构体的静态方法使用的关键字是static:类静态 ...