mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-26 18:21:04 +00:00
IGUIFont / CGUITTFont code cleanups (#15581)
This commit is contained in:
parent
0bfd9bc09e
commit
c49ff76955
12 changed files with 135 additions and 626 deletions
|
@ -788,9 +788,9 @@ void CGUIEditBox::draw()
|
|||
mbegin = font->getDimension(s.c_str()).Width;
|
||||
|
||||
// deal with kerning
|
||||
mbegin += font->getKerningWidth(
|
||||
&((*txtLine)[realmbgn - startPos]),
|
||||
realmbgn - startPos > 0 ? &((*txtLine)[realmbgn - startPos - 1]) : 0);
|
||||
mbegin += font->getKerning(
|
||||
(*txtLine)[realmbgn - startPos],
|
||||
realmbgn - startPos > 0 ? (*txtLine)[realmbgn - startPos - 1] : 0).X;
|
||||
|
||||
lineStartPos = realmbgn - startPos;
|
||||
}
|
||||
|
@ -832,7 +832,8 @@ void CGUIEditBox::draw()
|
|||
}
|
||||
s = txtLine->subString(0, CursorPos - startPos);
|
||||
charcursorpos = font->getDimension(s.c_str()).Width +
|
||||
font->getKerningWidth(CursorChar.c_str(), CursorPos - startPos > 0 ? &((*txtLine)[CursorPos - startPos - 1]) : 0);
|
||||
font->getKerning(CursorChar[0],
|
||||
CursorPos - startPos > 0 ? (*txtLine)[CursorPos - startPos - 1] : 0).X;
|
||||
|
||||
if (focus && (CursorBlinkTime == 0 || (os::Timer::getTime() - BlinkStartTime) % (2 * CursorBlinkTime) < CursorBlinkTime)) {
|
||||
setTextRect(cursorLine);
|
||||
|
@ -1194,7 +1195,7 @@ void CGUIEditBox::setTextRect(s32 line)
|
|||
d = font->getDimension(Text.c_str());
|
||||
d.Height = AbsoluteRect.getHeight();
|
||||
}
|
||||
d.Height += font->getKerningHeight();
|
||||
d.Height += font->getKerning(L'A').Y;
|
||||
|
||||
// justification
|
||||
switch (HAlign) {
|
||||
|
@ -1382,7 +1383,7 @@ void CGUIEditBox::calculateScrollPos()
|
|||
|
||||
// calculate vertical scrolling
|
||||
if (hasBrokenText) {
|
||||
irr::u32 lineHeight = font->getDimension(L"A").Height + font->getKerningHeight();
|
||||
irr::u32 lineHeight = font->getDimension(L"A").Height + font->getKerning(L'A').Y;
|
||||
// only up to 1 line fits?
|
||||
if (lineHeight >= (irr::u32)FrameRect.getHeight()) {
|
||||
VScrollPos = 0;
|
||||
|
|
|
@ -53,141 +53,6 @@ CGUIFont::~CGUIFont()
|
|||
}
|
||||
}
|
||||
|
||||
#if 0
|
||||
//! loads a font file from xml
|
||||
bool CGUIFont::load(io::IXMLReader* xml, const io::path& directory)
|
||||
{
|
||||
if (!SpriteBank)
|
||||
return false;
|
||||
|
||||
SpriteBank->clear();
|
||||
|
||||
while (xml->read())
|
||||
{
|
||||
if (io::EXN_ELEMENT == xml->getNodeType())
|
||||
{
|
||||
if (core::stringw(L"Texture") == xml->getNodeName())
|
||||
{
|
||||
// add a texture
|
||||
core::stringc fn = xml->getAttributeValue(L"filename");
|
||||
u32 i = (u32)xml->getAttributeValueAsInt(L"index");
|
||||
core::stringw alpha = xml->getAttributeValue(L"hasAlpha");
|
||||
|
||||
while (i+1 > SpriteBank->getTextureCount())
|
||||
SpriteBank->addTexture(0);
|
||||
|
||||
bool flags[3];
|
||||
pushTextureCreationFlags(flags);
|
||||
|
||||
// load texture
|
||||
io::path textureFullName = core::mergeFilename(directory, fn);
|
||||
SpriteBank->setTexture(i, Driver->getTexture(textureFullName));
|
||||
|
||||
popTextureCreationFlags(flags);
|
||||
|
||||
// couldn't load texture, abort.
|
||||
if (!SpriteBank->getTexture(i))
|
||||
{
|
||||
os::Printer::log("Unable to load all textures in the font, aborting", ELL_ERROR);
|
||||
return false;
|
||||
}
|
||||
else
|
||||
{
|
||||
// colorkey texture rather than alpha channel?
|
||||
if (alpha == core::stringw("false"))
|
||||
Driver->makeColorKeyTexture(SpriteBank->getTexture(i), core::position2di(0,0));
|
||||
}
|
||||
}
|
||||
else if (core::stringw(L"c") == xml->getNodeName())
|
||||
{
|
||||
// adding a character to this font
|
||||
SFontArea a;
|
||||
SGUISpriteFrame f;
|
||||
SGUISprite s;
|
||||
core::rect<s32> rectangle;
|
||||
|
||||
a.underhang = xml->getAttributeValueAsInt(L"u");
|
||||
a.overhang = xml->getAttributeValueAsInt(L"o");
|
||||
a.spriteno = SpriteBank->getSprites().size();
|
||||
s32 texno = xml->getAttributeValueAsInt(L"i");
|
||||
|
||||
// parse rectangle
|
||||
core::stringc rectstr = xml->getAttributeValue(L"r");
|
||||
wchar_t ch = xml->getAttributeValue(L"c")[0];
|
||||
|
||||
const c8 *c = rectstr.c_str();
|
||||
s32 val;
|
||||
val = 0;
|
||||
while (*c >= '0' && *c <= '9')
|
||||
{
|
||||
val *= 10;
|
||||
val += *c - '0';
|
||||
c++;
|
||||
}
|
||||
rectangle.UpperLeftCorner.X = val;
|
||||
while (*c == L' ' || *c == L',') c++;
|
||||
|
||||
val = 0;
|
||||
while (*c >= '0' && *c <= '9')
|
||||
{
|
||||
val *= 10;
|
||||
val += *c - '0';
|
||||
c++;
|
||||
}
|
||||
rectangle.UpperLeftCorner.Y = val;
|
||||
while (*c == L' ' || *c == L',') c++;
|
||||
|
||||
val = 0;
|
||||
while (*c >= '0' && *c <= '9')
|
||||
{
|
||||
val *= 10;
|
||||
val += *c - '0';
|
||||
c++;
|
||||
}
|
||||
rectangle.LowerRightCorner.X = val;
|
||||
while (*c == L' ' || *c == L',') c++;
|
||||
|
||||
val = 0;
|
||||
while (*c >= '0' && *c <= '9')
|
||||
{
|
||||
val *= 10;
|
||||
val += *c - '0';
|
||||
c++;
|
||||
}
|
||||
rectangle.LowerRightCorner.Y = val;
|
||||
|
||||
CharacterMap.emplace(ch, Areas.size());
|
||||
|
||||
// make frame
|
||||
f.rectNumber = SpriteBank->getPositions().size();
|
||||
f.textureNumber = texno;
|
||||
|
||||
// add frame to sprite
|
||||
s.Frames.push_back(f);
|
||||
s.frameTime = 0;
|
||||
|
||||
// add rectangle to sprite bank
|
||||
SpriteBank->getPositions().push_back(rectangle);
|
||||
a.width = rectangle.getWidth();
|
||||
|
||||
// add sprite to sprite bank
|
||||
SpriteBank->getSprites().push_back(s);
|
||||
|
||||
// add character to font
|
||||
Areas.push_back(a);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// set bad character
|
||||
WrongCharacter = getAreaFromCharacter(L' ');
|
||||
|
||||
setMaxHeight();
|
||||
|
||||
return true;
|
||||
}
|
||||
#endif
|
||||
|
||||
void CGUIFont::setMaxHeight()
|
||||
{
|
||||
if (!SpriteBank)
|
||||
|
@ -365,17 +230,15 @@ void CGUIFont::setKerningWidth(s32 kerning)
|
|||
GlobalKerningWidth = kerning;
|
||||
}
|
||||
|
||||
//! set an Pixel Offset on Drawing ( scale position on width )
|
||||
s32 CGUIFont::getKerningWidth(const wchar_t *thisLetter, const wchar_t *previousLetter) const
|
||||
core::vector2di CGUIFont::getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const
|
||||
{
|
||||
s32 ret = GlobalKerningWidth;
|
||||
core::vector2di ret(GlobalKerningWidth, GlobalKerningHeight);
|
||||
|
||||
if (thisLetter) {
|
||||
ret += Areas[getAreaFromCharacter(*thisLetter)].overhang;
|
||||
ret.X += Areas[getAreaFromCharacter(thisLetter)].overhang;
|
||||
|
||||
if (previousLetter) {
|
||||
ret += Areas[getAreaFromCharacter(*previousLetter)].underhang;
|
||||
}
|
||||
if (previousLetter)
|
||||
ret.X += Areas[getAreaFromCharacter(previousLetter)].underhang;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
@ -387,12 +250,6 @@ void CGUIFont::setKerningHeight(s32 kerning)
|
|||
GlobalKerningHeight = kerning;
|
||||
}
|
||||
|
||||
//! set an Pixel Offset on Drawing ( scale position on height )
|
||||
s32 CGUIFont::getKerningHeight() const
|
||||
{
|
||||
return GlobalKerningHeight;
|
||||
}
|
||||
|
||||
//! returns the sprite number from a given character
|
||||
u32 CGUIFont::getSpriteNoFromChar(const wchar_t *c) const
|
||||
{
|
||||
|
|
|
@ -58,8 +58,7 @@ public:
|
|||
void setKerningHeight(s32 kerning) override;
|
||||
|
||||
//! set an Pixel Offset on Drawing ( scale position on width )
|
||||
s32 getKerningWidth(const wchar_t *thisLetter = 0, const wchar_t *previousLetter = 0) const override;
|
||||
s32 getKerningHeight() const override;
|
||||
core::vector2di getKerning(const wchar_t thisLetter, const wchar_t previousLetter) const override;
|
||||
|
||||
//! gets the sprite bank
|
||||
IGUISpriteBank *getSpriteBank() const override;
|
||||
|
|
|
@ -74,10 +74,12 @@ void CGUIStaticText::draw()
|
|||
IGUIFont *font = getActiveFont();
|
||||
|
||||
if (font) {
|
||||
s32 kerningHeight = font->getKerning(L'A').Y;
|
||||
|
||||
if (!WordWrap) {
|
||||
if (VAlign == EGUIA_LOWERRIGHT) {
|
||||
frameRect.UpperLeftCorner.Y = frameRect.LowerRightCorner.Y -
|
||||
font->getDimension(L"A").Height - font->getKerningHeight();
|
||||
font->getDimension(L"A").Height - kerningHeight;
|
||||
}
|
||||
if (HAlign == EGUIA_LOWERRIGHT) {
|
||||
frameRect.UpperLeftCorner.X = frameRect.LowerRightCorner.X -
|
||||
|
@ -92,7 +94,7 @@ void CGUIStaticText::draw()
|
|||
breakText();
|
||||
|
||||
core::rect<s32> r = frameRect;
|
||||
s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
|
||||
s32 height = font->getDimension(L"A").Height + kerningHeight;
|
||||
s32 totalHeight = height * BrokenText.size();
|
||||
if (VAlign == EGUIA_CENTER) {
|
||||
r.UpperLeftCorner.Y = r.getCenter().Y - (totalHeight / 2);
|
||||
|
@ -471,7 +473,7 @@ s32 CGUIStaticText::getTextHeight() const
|
|||
return 0;
|
||||
|
||||
if (WordWrap) {
|
||||
s32 height = font->getDimension(L"A").Height + font->getKerningHeight();
|
||||
s32 height = font->getDimension(L"A").Height + font->getKerning(L'A').Y;
|
||||
return height * BrokenText.size();
|
||||
} else {
|
||||
// TODO: Text can have multiple lines which are not in BrokenText
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue