Mygui Chinese Line wrapping problem Solution _c language

Source: Internet
Author: User
Tags rollback ustring
I believe that we have solved the Chinese input will encounter how to solve the problem of Chinese input, the Chinese input line is a lot of GUI framework is a problem, here is no nonsense, we open the Mygui engine layer of the widget TextView header files and source files to replace it with:
Copy Code code as follows:

/*!
@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__

The header file is above, and the following is the source file:
Copy Code code as follows:

/*!
@file
@author Albert Semenov
@date 09/2010
*/
/*
This 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 into the hope that it would be useful,
but without any WARRANTY; Without even the implied warranty of
merchantability or FITNESS for A particular purpose. The
GNU Lesser General public License for more details.
Should have received a copy of the GNU Lesser General public License
Along with Mygui. If not, */
#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 _ma Xwidth)
{
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) & 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-&GT;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

After you modify these two, you can then change the edit control to multiline support and word wrapping, which enables Mygui to be wrapped in Chinese automatically.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.