Mygui中文换行问题解决方案

相信大家解决了中文输入后一定会遇到如何解决中文输入的问题,中文输入换行问题是很多gui框架都存在的一个问题,这里不废话了,大家打开mygui的引擎层中的widget的textview 的头文件和源文件将其替换为:


代码如下:

/*!
@file
@author Albert Semenov
@date 09/2009
*/
#ifndef __MYGUI_TEXT_VIEW_DATA_H__
#define __MYGUI_TEXT_VIEW_DATA_H__
#include "MyGUI_Prerequest.h"
namespace MyGUI
{
class CharInfo
{
public:
CharInfo() :
mIsColour(false)
{
mMetrics.mWidth = 0.0f;
mMetrics.mHeight = 0.0f;
mMetrics.mAdvance = 0.0f;
mMetrics.mBearingX = 0.0f;
mMetrics.mBearingY = 0.0f;
}
CharInfo(
const FloatRect& _rect,
float _width,
float _height,
float _advance,
float _bearingX,
float _bearingY) :
mIsColour(false),
mUVRect(_rect)
{
mMetrics.mWidth = _width;
mMetrics.mHeight = _height;
mMetrics.mAdvance = _advance;
mMetrics.mBearingX = _bearingX;
mMetrics.mBearingY = _bearingY;
}
CharInfo(uint32 _colour) :
mIsColour(true),
mColour(_colour)
{ }
bool isColour() const
{
return mIsColour;
}
float getWidth() const
{
return mMetrics.mWidth;
}
float getHeight() const
{
return mMetrics.mHeight;
}
float getAdvance() const
{
return mMetrics.mAdvance;
}
float getBearingX() const
{
return mMetrics.mBearingX;
}
float getBearingY() const
{
return mMetrics.mBearingY;
}
const FloatRect& getUVRect() const
{
return mUVRect;
}
uint32 getColour() const
{
return mColour;
}
private:
bool mIsColour;
FloatRect mUVRect;
struct Metrics
{
float mWidth;
float mHeight;
float mAdvance;
float mBearingX;
float mBearingY;
};
union
{
Metrics mMetrics;
uint32 mColour;
};
};
typedef std::vector<CharInfo> VectorCharInfo;
//struct LineInfo
//{
// LineInfo() :
// width(0),
// offset(0),
// count(0)
// {
// }
// void clear()
// {
// width = 0;
// count = 0;
// simbols.clear();
// offset = 0;
// }
// int width;
// int offset;
// size_t count;
// VectorCharInfo simbols;
//};
struct LineInfo
{
LineInfo() :
width(0),
offset(0),
count(0),
offcount(0)
{
}
void clear()
{
offcount = 0;
width = 0;
count = 0;
simbols.clear();
offset = 0;
}
int offcount;
int width;
int offset;
size_t count;
VectorCharInfo simbols;
};
typedef std::vector<LineInfo> VectorLineInfo;
} // namespace MyGUI
#endif // __MYGUI_TEXT_VIEW_DATA_H__

上面是头文件,下面是源文件:


代码如下:

/*!
@file
@author Albert Semenov
@date 09/2010
*/
/*
This file is part of MyGUI.
MyGUI is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MyGUI is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with MyGUI. If not, see <http://www.gnu.org/licenses/>.
*/
#include "MyGUI_Precompiled.h"
#include "MyGUI_TextView.h"
namespace MyGUI
{
namespace
{
template<typename T>
void setMin(T& _var, const T& _newValue)
{
if (_newValue < _var)
_var = _newValue;
}
template<typename T>
void setMax(T& _var, const T& _newValue)
{
if (_var < _newValue)
_var = _newValue;
}
}
class RollBackPoint
{
public:
RollBackPoint() :
position(0),
count(0),
width(0),
rollback(false)
{
}
void set(size_t _position, UString::const_iterator& _space_point, size_t _count, float _width)
{
position = _position;
space_point = _space_point;
count = _count;
width = _width;
rollback = true;
}
void clear()
{
rollback = false;
}
bool empty() const
{
return !rollback;
}
float getWidth() const
{
MYGUI_DEBUG_ASSERT(rollback, "rollback point not valid");
return width;
}
size_t getCount() const
{
MYGUI_DEBUG_ASSERT(rollback, "rollback point not valid");
return count;
}
size_t getPosition() const
{
MYGUI_DEBUG_ASSERT(rollback, "rollback point not valid");
return position;
}
UString::const_iterator getTextIter() const
{
MYGUI_DEBUG_ASSERT(rollback, "rollback point not valid");
return space_point;
}
private:
size_t position;
UString::const_iterator space_point;
size_t count;
float width;
bool rollback;
};
TextView::TextView() :
mLength(0),
mFontHeight(0)
{
}
void TextView::update(const UString& _text, IFont* _font, int _height, Align _align, VertexColourType _format, int _maxWidth)
{
mFontHeight = _height;
// массив для быстрой конвертации цветов
static const char convert_colour[64] =
{
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 0, 0, 0, 0, 0,
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 10, 11, 12, 13, 14, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0
};
mViewSize.clear();
RollBackPoint roll_back;
IntSize result;
float width = 0.0f;
size_t count = 0;
mLength = 0;
mLineInfo.clear();
LineInfo line_info;
int font_height = _font->getDefaultHeight();
UString::const_iterator end = _text.end();
UString::const_iterator index = _text.begin();
/*if (index == end)
return;*/
result.height += _height;
for (; index != end; ++index)
{
Char character = *index;
// новая строка
if (character == FontCodeType::CR
|| character == FontCodeType::NEL
|| character == FontCodeType::LF)
{
if (character == FontCodeType::CR)
{
UString::const_iterator peeki = index;
++peeki;
if ((peeki != end) && (*peeki == FontCodeType::LF))
index = peeki; // skip both as one newline
}
line_info.width = (int)ceil(width);
line_info.count = count;
//mLength += line_info.count + 1;
mLength += line_info.offcount;
result.height += _height;
setMax(result.width, line_info.width);
width = 0;
count = 0;
mLineInfo.push_back(line_info);
line_info.clear();
// отменяем откат
roll_back.clear();
continue;
}
// тег
else if (character == L'#')
{
// берем следующий символ
++ index;
if (index == end)
{
--index; // это защита
continue;
}
character = *index;
// если два подряд, то рисуем один шарп, если нет то меняем цвет
if (character != L'#')
{
// парсим первый символ
uint32 colour = convert_colour[(character - 48) & 0x3F];
// и еще пять символов после шарпа
for (char i = 0; i < 5; i++)
{
++ index;
if (index == end)
{
--index; // это защита
continue;
}
colour <<= 4;
colour += convert_colour[ ((*index) - 48) & 0x3F ];
}
// если нужно, то меняем красный и синий компоненты
texture_utility::convertColour(colour, _format);
line_info.simbols.push_back( CharInfo(colour) );
continue;
}
}
GlyphInfo* info = _font->getGlyphInfo(character);
if (info == nullptr)
continue;
if (FontCodeType::Space == character)
{
roll_back.set(line_info.simbols.size(), index, count, width);
}
else if (FontCodeType::Tab == character)
{
roll_back.set(line_info.simbols.size(), index, count, width);
}
float char_width = info->width;
float char_height = info->height;
float char_advance = info->advance;
float char_bearingX = info->bearingX;
float char_bearingY = info->bearingY;
if (_height != font_height)
{
float scale = (float)_height / font_height;
char_width *= scale;
char_height *= scale;
char_advance *= scale;
char_bearingX *= scale;
char_bearingY *= scale;
}
float char_fullAdvance = char_bearingX + char_advance;
// перенос слов
if (_maxWidth != -1
&& (width + char_fullAdvance) > _maxWidth
/*&& !roll_back.empty()*/)
{
--index;
// откатываем до последнего пробела
/*width = roll_back.getWidth();
count = roll_back.getCount();
index = roll_back.getTextIter();
line_info.simbols.erase(line_info.simbols.begin() + roll_back.getPosition(), line_info.simbols.end());*/

// запоминаем место отката, как полную строку
/*line_info.width = (int)ceil(width);
line_info.count = count;
mLength += line_info.count + 1;
result.height += _height;
setMax(result.width, line_info.width);
width = 0;
count = 0;
mLineInfo.push_back(line_info);
line_info.clear();*/
// запоминаем место отката, как полную строку
line_info.width = (int)ceil(width);
line_info.count = count;
line_info.offcount = 0;
mLength += line_info.count;// + 1;
result.height += _height;
setMax(result.width, line_info.width);
width = 0;
count = 0;
mLineInfo.push_back(line_info);
line_info.clear();
// отменяем откат
roll_back.clear();
continue;
}
line_info.simbols.push_back(CharInfo(info->uvRect, char_width, char_height, char_advance, char_bearingX, char_bearingY));
width += char_fullAdvance;
count ++;
}
line_info.width = (int)ceil(width);
line_info.count = count;
mLength += line_info.count;
mLineInfo.push_back(line_info);
setMax(result.width, line_info.width);
// теперь выравниванием строки
for (VectorLineInfo::iterator line = mLineInfo.begin(); line != mLineInfo.end(); ++line)
{
if (_align.isRight())
line->offset = result.width - line->width;
else if (_align.isHCenter())
line->offset = (result.width - line->width) / 2;
}
mViewSize = result;
}
size_t TextView::getCursorPosition(const IntPoint& _value)
{
const int height = mFontHeight;
size_t result = 0;
int top = 0;
for (VectorLineInfo::const_iterator line = mLineInfo.begin(); line != mLineInfo.end(); ++line)
{
// это последняя строка
bool lastline = !(line + 1 != mLineInfo.end());
// наша строчка
if (top + height > _value.top || lastline)
{
top += height;
float left = (float)line->offset;
int count = 0;
// ищем символ
for (VectorCharInfo::const_iterator sim = line->simbols.begin(); sim != line->simbols.end(); ++sim)
{
if (sim->isColour())
continue;
float fullAdvance = sim->getAdvance() + sim->getBearingX();
if (left + fullAdvance / 2.0f > _value.left)
{
break;
}
left += fullAdvance;
count ++;
}
result += count;
break;
}
if (!lastline)
{
top += height;
result += line->count + line->offcount;

}
}
return result;
}
IntPoint TextView::getCursorPoint(size_t _position)
{
setMin(_position, mLength);
size_t position = 0;
int top = 0;
float left = 0.0f;
for (VectorLineInfo::const_iterator line = mLineInfo.begin(); line != mLineInfo.end(); ++line)
{
left = (float)line->offset;
if (position + line->count >= _position)
{
for (VectorCharInfo::const_iterator sim = line->simbols.begin(); sim != line->simbols.end(); ++sim)
{
if (sim->isColour())
continue;
if (position == _position)
break;
position ++;
left += sim->getBearingX() + sim->getAdvance();
}
break;
}
position += line->count + line->offcount;

top += mFontHeight;
}
return IntPoint((int)left, top);
}
const IntSize& TextView::getViewSize() const
{
return mViewSize;
}
size_t TextView::getTextLength() const
{
return mLength;
}
const VectorLineInfo& TextView::getData() const
{
return mLineInfo;
}
} // namespace MyGUI

修改了这两个以后,然后将edit控件改为多行支持和自动换行,这样就实现了mygui的中文自动换行问题。

(0)

相关推荐

  • Mygui中文换行问题解决方案

    相信大家解决了中文输入后一定会遇到如何解决中文输入的问题,中文输入换行问题是很多gui框架都存在的一个问题,这里不废话了,大家打开mygui的引擎层中的widget的textview 的头文件和源文件将其替换为: 复制代码 代码如下: /*! @file @author Albert Semenov @date 09/2009 */ #ifndef __MYGUI_TEXT_VIEW_DATA_H__ #define __MYGUI_TEXT_VIEW_DATA_H__ #include "My

  • MySql安装步骤图文教程及中文乱码的解决方案

    MySql Server安装步骤如下所示: 1安装MySql Server 2 安装MySqlServer管理工具 解压中文语言包,将文件复制到安装目录下覆盖 文件覆盖后,打开软件设置语言为中文(CN) 3 MySqlServer开发注意事项(C#) 1.联接字符串:"Server=localhost;Database=100;Uid=root;Pwd='root'" 2.引用MySql.Data.dll;using MySql.Data.MySqlClient; 3.使用MySqlC

  • Mysql在debian系统中不能插入中文的终极解决方案

    在debian环境下,彻底解决mysql无法插入和显示中文的问题 Linux下Mysql插入中文显示乱码解决方案 mysql -uroot -p 回车输入密码 进入mysql查看状态如下: 默认的是客户端和服务器都用了latin1,所以会乱码. 解决方案: mysql>user mydb; mysql>alter database mydb  character set utf8;! 上文提到了用临时方法更改数据库的字符集设置,显示中文,但是后来发现在有的系统下并不能成功. 比如我用的debi

  • 详解Python Matplot中文显示完美解决方案

    原因与现象 Matplot是一个功能强大的Python图表绘制库,很遗憾目前版本自带的字体库中并不支持中文字体.所以如果在绘制内容中需要显示中文,那么就会显示为方格字符. 解决办法 有一个较为完美的解决方案,通过扫描Matplot自带字体库以及系统字体库,寻找能够支持的中文字体,如果能够找到的话,就设置第一个为Matplot的字体熟悉. import matplotlib.pyplot as plt from matplotlib.font_manager import FontManager

  • pyftplib中文乱码问题解决方案

    这篇文章主要介绍了pyftplib中文乱码问题解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 采用pyftpdlib启动ftp服务端,ftp客户端在上传文件的时候,如果不指定字符编码,如果遇到中文,可能会乱码:网上找了很多资料,但是他们的客户端都是基于python实现的.还是没法解决我得问题 于是重新:FTPHandler的decode方法 def decode(self, bytes): return bytes.decode('ut

  • Sublime Text 打开Java文档中文乱码的解决方案

    问题: 打开文档后出现中文乱码如图 1.在安装插件之前,首先要安装package control这个组件,才能够给Sublime Text安装各种插件 打开Sublime Text,按 ctrl+` 或者点击菜单栏 View下的Show Console 调出console == Sublime Text2 对于Sublime Text2,输入粘贴以下代码到底部命令行,并回车 import urllib2,os,hashlib; h = 'df21e130d211cfc94d9b0905775a7

  • springboot参数传中文乱码的解决方案

    前言 本文案例来自业务部门的一个业务场景.他们的业务场景是他们部门研发了一个微服务上下文透传组件,其透传原理也挺简单的,就是通过springboot拦截器把请求参数塞进threadlocal,然后下游通过threadlocal取到值,服务之间进行feign调用时,再把threadlocal的参数塞到header头里面.这个组件一直用得好好的,突然有一天因为传的参数值是中文,导致乱码.他们通过尝试下面的各种方案,都无法解决.最后就让我们部门排查处理. 业务部门的实现思路 他们一开始的思路方向是参数

  • SpringMVC 中文乱码的解决方案

    背景 举个例子,出现中文乱码的例子:提交表单的时候. 表单 <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <form action="/c02/t1" method=&quo

  • Python利用Matplotlib绘图无法显示中文字体的解决方案

    目录 发现问题 解决方式: 总结 发现问题 我在利用Pycharm中的matplotlib库进行绘图的时候,出现了一个问题,当我将所绘的图的横,纵坐标,希望加上一个中文描述的时候,比如,'横坐标','纵坐标'类似这样的描述方法.出现了很多人都会出现的一个问题. import matplotlib.pyplot as plt squares = [1, 4, 9, 16, 25] fig, ax = plt.subplots() ax.plot(squares, linewidth=3) #设置图

  • SpringMVC Restful风格与中文乱码问题解决方案介绍

    目录 基本要点 1.定义 2.传统方式与Restful风格的区别 3.如何使用Restful风格 4.为什么要用restful 5.乱码问题 基本要点 1.定义 根据百度百科的定义,RESTFUL是一种网络应用程序的设计风格和开发方式 2.传统方式与Restful风格的区别 在我们学习restful风格之前,我们请求接口,都是使用http://localhost:8080/controller?method=add这种方式携带接口所需要的参数 而调用restful风格的接口时,我们可以改成htt

随机推荐