mirror of
https://github.com/minetest/irrlicht.git
synced 2025-06-30 23:30:27 +02:00
Reformat the code, using:
find -type f | # list all regular files grep -E '\.(h|cpp|mm)$' | # filter for source files grep -v '/mt_' | # filter out generated files grep -v '/vendor/' | # and vendored GL grep -v '/test/image_loader_test.cpp' | # and this file (has giant literals arrays) xargs -n 1 -P $(nproc) clang-format -i # reformat everything Co-authored-by: numzero <numzer0@yandex.ru>
This commit is contained in:
@ -17,21 +17,20 @@ namespace gui
|
||||
{
|
||||
|
||||
//! constructor
|
||||
CGUIFont::CGUIFont(IGUIEnvironment *env, const io::path& filename)
|
||||
: Driver(0), SpriteBank(0), Environment(env), WrongCharacter(0),
|
||||
MaxHeight(0), GlobalKerningWidth(0), GlobalKerningHeight(0)
|
||||
CGUIFont::CGUIFont(IGUIEnvironment *env, const io::path &filename) :
|
||||
Driver(0), SpriteBank(0), Environment(env), WrongCharacter(0),
|
||||
MaxHeight(0), GlobalKerningWidth(0), GlobalKerningHeight(0)
|
||||
{
|
||||
#ifdef _DEBUG
|
||||
#ifdef _DEBUG
|
||||
setDebugName("CGUIFont");
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (Environment)
|
||||
{
|
||||
if (Environment) {
|
||||
// don't grab environment, to avoid circular references
|
||||
Driver = Environment->getVideoDriver();
|
||||
|
||||
SpriteBank = Environment->getSpriteBank(filename);
|
||||
if (!SpriteBank) // could be default-font which has no file
|
||||
if (!SpriteBank) // could be default-font which has no file
|
||||
SpriteBank = Environment->addEmptySpriteBank(filename);
|
||||
if (SpriteBank)
|
||||
SpriteBank->grab();
|
||||
@ -40,18 +39,16 @@ CGUIFont::CGUIFont(IGUIEnvironment *env, const io::path& filename)
|
||||
if (Driver)
|
||||
Driver->grab();
|
||||
|
||||
setInvisibleCharacters ( L" " );
|
||||
setInvisibleCharacters(L" ");
|
||||
}
|
||||
|
||||
|
||||
//! destructor
|
||||
CGUIFont::~CGUIFont()
|
||||
{
|
||||
if (Driver)
|
||||
Driver->drop();
|
||||
|
||||
if (SpriteBank)
|
||||
{
|
||||
if (SpriteBank) {
|
||||
SpriteBank->drop();
|
||||
// TODO: spritebank still exists in gui-environment and should be removed here when it's
|
||||
// reference-count is 1. Just can't do that from here at the moment.
|
||||
@ -60,7 +57,6 @@ CGUIFont::~CGUIFont()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#if 0
|
||||
//! loads a font file from xml
|
||||
bool CGUIFont::load(io::IXMLReader* xml, const io::path& directory)
|
||||
@ -196,25 +192,23 @@ bool CGUIFont::load(io::IXMLReader* xml, const io::path& directory)
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
void CGUIFont::setMaxHeight()
|
||||
{
|
||||
if ( !SpriteBank )
|
||||
if (!SpriteBank)
|
||||
return;
|
||||
|
||||
MaxHeight = 0;
|
||||
|
||||
core::array< core::rect<s32> >& p = SpriteBank->getPositions();
|
||||
core::array<core::rect<s32>> &p = SpriteBank->getPositions();
|
||||
|
||||
for (u32 i=0; i<p.size(); ++i)
|
||||
{
|
||||
for (u32 i = 0; i < p.size(); ++i) {
|
||||
const s32 t = p[i].getHeight();
|
||||
if (t>MaxHeight)
|
||||
if (t > MaxHeight)
|
||||
MaxHeight = t;
|
||||
}
|
||||
}
|
||||
|
||||
void CGUIFont::pushTextureCreationFlags(bool(&flags)[3])
|
||||
void CGUIFont::pushTextureCreationFlags(bool (&flags)[3])
|
||||
{
|
||||
flags[0] = Driver->getTextureCreationFlag(video::ETCF_ALLOW_NON_POWER_2);
|
||||
flags[1] = Driver->getTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS);
|
||||
@ -225,7 +219,7 @@ void CGUIFont::pushTextureCreationFlags(bool(&flags)[3])
|
||||
Driver->setTextureCreationFlag(video::ETCF_ALLOW_MEMORY_COPY, true);
|
||||
}
|
||||
|
||||
void CGUIFont::popTextureCreationFlags(const bool(&flags)[3])
|
||||
void CGUIFont::popTextureCreationFlags(const bool (&flags)[3])
|
||||
{
|
||||
Driver->setTextureCreationFlag(video::ETCF_ALLOW_NON_POWER_2, flags[0]);
|
||||
Driver->setTextureCreationFlag(video::ETCF_CREATE_MIP_MAPS, flags[1]);
|
||||
@ -233,51 +227,48 @@ void CGUIFont::popTextureCreationFlags(const bool(&flags)[3])
|
||||
}
|
||||
|
||||
//! loads a font file, native file needed, for texture parsing
|
||||
bool CGUIFont::load(io::IReadFile* file)
|
||||
bool CGUIFont::load(io::IReadFile *file)
|
||||
{
|
||||
if (!Driver)
|
||||
return false;
|
||||
|
||||
return loadTexture(Driver->createImageFromFile(file),
|
||||
file->getFileName());
|
||||
file->getFileName());
|
||||
}
|
||||
|
||||
|
||||
//! loads a font file, native file needed, for texture parsing
|
||||
bool CGUIFont::load(const io::path& filename)
|
||||
bool CGUIFont::load(const io::path &filename)
|
||||
{
|
||||
if (!Driver)
|
||||
return false;
|
||||
|
||||
return loadTexture(Driver->createImageFromFile( filename ),
|
||||
filename);
|
||||
return loadTexture(Driver->createImageFromFile(filename),
|
||||
filename);
|
||||
}
|
||||
|
||||
|
||||
//! load & prepare font from ITexture
|
||||
bool CGUIFont::loadTexture(video::IImage* image, const io::path& name)
|
||||
bool CGUIFont::loadTexture(video::IImage *image, const io::path &name)
|
||||
{
|
||||
if (!image || !SpriteBank)
|
||||
return false;
|
||||
|
||||
s32 lowerRightPositions = 0;
|
||||
|
||||
video::IImage* tmpImage=image;
|
||||
bool deleteTmpImage=false;
|
||||
switch(image->getColorFormat())
|
||||
{
|
||||
video::IImage *tmpImage = image;
|
||||
bool deleteTmpImage = false;
|
||||
switch (image->getColorFormat()) {
|
||||
case video::ECF_R5G6B5:
|
||||
tmpImage = Driver->createImage(video::ECF_A1R5G5B5,image->getDimension());
|
||||
tmpImage = Driver->createImage(video::ECF_A1R5G5B5, image->getDimension());
|
||||
image->copyTo(tmpImage);
|
||||
deleteTmpImage=true;
|
||||
deleteTmpImage = true;
|
||||
break;
|
||||
case video::ECF_A1R5G5B5:
|
||||
case video::ECF_A8R8G8B8:
|
||||
break;
|
||||
case video::ECF_R8G8B8:
|
||||
tmpImage = Driver->createImage(video::ECF_A8R8G8B8,image->getDimension());
|
||||
tmpImage = Driver->createImage(video::ECF_A8R8G8B8, image->getDimension());
|
||||
image->copyTo(tmpImage);
|
||||
deleteTmpImage=true;
|
||||
deleteTmpImage = true;
|
||||
break;
|
||||
default:
|
||||
os::Printer::log("Unknown texture format provided for CGUIFont::loadTexture", ELL_ERROR);
|
||||
@ -290,14 +281,12 @@ bool CGUIFont::loadTexture(video::IImage* image, const io::path& name)
|
||||
// output warnings
|
||||
if (!lowerRightPositions || !SpriteBank->getSprites().size())
|
||||
os::Printer::log("Either no upper or lower corner pixels in the font file. If this font was made using the new font tool, please load the XML file instead. If not, the font may be corrupted.", ELL_ERROR);
|
||||
else
|
||||
if (lowerRightPositions != (s32)SpriteBank->getPositions().size())
|
||||
else if (lowerRightPositions != (s32)SpriteBank->getPositions().size())
|
||||
os::Printer::log("The amount of upper corner pixels and the lower corner pixels is not equal, font file may be corrupted.", ELL_ERROR);
|
||||
|
||||
bool ret = ( !SpriteBank->getSprites().empty() && lowerRightPositions );
|
||||
bool ret = (!SpriteBank->getSprites().empty() && lowerRightPositions);
|
||||
|
||||
if ( ret )
|
||||
{
|
||||
if (ret) {
|
||||
bool flags[3];
|
||||
pushTextureCreationFlags(flags);
|
||||
|
||||
@ -314,42 +303,34 @@ bool CGUIFont::loadTexture(video::IImage* image, const io::path& name)
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
void CGUIFont::readPositions(video::IImage* image, s32& lowerRightPositions)
|
||||
void CGUIFont::readPositions(video::IImage *image, s32 &lowerRightPositions)
|
||||
{
|
||||
if (!SpriteBank )
|
||||
if (!SpriteBank)
|
||||
return;
|
||||
|
||||
const core::dimension2d<u32> size = image->getDimension();
|
||||
|
||||
video::SColor colorTopLeft = image->getPixel(0,0);
|
||||
video::SColor colorTopLeft = image->getPixel(0, 0);
|
||||
colorTopLeft.setAlpha(255);
|
||||
image->setPixel(0,0,colorTopLeft);
|
||||
video::SColor colorLowerRight = image->getPixel(1,0);
|
||||
video::SColor colorBackGround = image->getPixel(2,0);
|
||||
image->setPixel(0, 0, colorTopLeft);
|
||||
video::SColor colorLowerRight = image->getPixel(1, 0);
|
||||
video::SColor colorBackGround = image->getPixel(2, 0);
|
||||
video::SColor colorBackGroundTransparent = 0;
|
||||
|
||||
image->setPixel(1,0,colorBackGround);
|
||||
image->setPixel(1, 0, colorBackGround);
|
||||
|
||||
// start parsing
|
||||
|
||||
core::position2d<s32> pos(0,0);
|
||||
for (pos.Y=0; pos.Y<(s32)size.Height; ++pos.Y)
|
||||
{
|
||||
for (pos.X=0; pos.X<(s32)size.Width; ++pos.X)
|
||||
{
|
||||
core::position2d<s32> pos(0, 0);
|
||||
for (pos.Y = 0; pos.Y < (s32)size.Height; ++pos.Y) {
|
||||
for (pos.X = 0; pos.X < (s32)size.Width; ++pos.X) {
|
||||
const video::SColor c = image->getPixel(pos.X, pos.Y);
|
||||
if (c == colorTopLeft)
|
||||
{
|
||||
if (c == colorTopLeft) {
|
||||
image->setPixel(pos.X, pos.Y, colorBackGroundTransparent);
|
||||
SpriteBank->getPositions().push_back(core::rect<s32>(pos, pos));
|
||||
}
|
||||
else
|
||||
if (c == colorLowerRight)
|
||||
{
|
||||
} else if (c == colorLowerRight) {
|
||||
// too many lower right points
|
||||
if (SpriteBank->getPositions().size()<=(u32)lowerRightPositions)
|
||||
{
|
||||
if (SpriteBank->getPositions().size() <= (u32)lowerRightPositions) {
|
||||
lowerRightPositions = 0;
|
||||
return;
|
||||
}
|
||||
@ -376,33 +357,27 @@ void CGUIFont::readPositions(video::IImage* image, s32& lowerRightPositions)
|
||||
CharacterMap[ch] = lowerRightPositions;
|
||||
|
||||
++lowerRightPositions;
|
||||
}
|
||||
else
|
||||
if (c == colorBackGround)
|
||||
} else if (c == colorBackGround)
|
||||
image->setPixel(pos.X, pos.Y, colorBackGroundTransparent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//! set an Pixel Offset on Drawing ( scale position on width )
|
||||
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
|
||||
s32 CGUIFont::getKerningWidth(const wchar_t *thisLetter, const wchar_t *previousLetter) const
|
||||
{
|
||||
s32 ret = GlobalKerningWidth;
|
||||
|
||||
if (thisLetter)
|
||||
{
|
||||
if (thisLetter) {
|
||||
ret += Areas[getAreaFromCharacter(*thisLetter)].overhang;
|
||||
|
||||
if (previousLetter)
|
||||
{
|
||||
if (previousLetter) {
|
||||
ret += Areas[getAreaFromCharacter(*previousLetter)].underhang;
|
||||
}
|
||||
}
|
||||
@ -410,28 +385,24 @@ s32 CGUIFont::getKerningWidth(const wchar_t* thisLetter, const wchar_t* previous
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
//! set an Pixel Offset on Drawing ( scale position on height )
|
||||
void CGUIFont::setKerningHeight(s32 kerning)
|
||||
{
|
||||
GlobalKerningHeight = kerning;
|
||||
}
|
||||
|
||||
|
||||
//! set an Pixel Offset on Drawing ( scale position on height )
|
||||
s32 CGUIFont::getKerningHeight () const
|
||||
s32 CGUIFont::getKerningHeight() const
|
||||
{
|
||||
return GlobalKerningHeight;
|
||||
}
|
||||
|
||||
|
||||
//! returns the sprite number from a given character
|
||||
u32 CGUIFont::getSpriteNoFromChar(const wchar_t *c) const
|
||||
{
|
||||
return Areas[getAreaFromCharacter(*c)].spriteno;
|
||||
}
|
||||
|
||||
|
||||
s32 CGUIFont::getAreaFromCharacter(const wchar_t c) const
|
||||
{
|
||||
auto n = CharacterMap.find(c);
|
||||
@ -441,33 +412,29 @@ s32 CGUIFont::getAreaFromCharacter(const wchar_t c) const
|
||||
return WrongCharacter;
|
||||
}
|
||||
|
||||
void CGUIFont::setInvisibleCharacters( const wchar_t *s )
|
||||
void CGUIFont::setInvisibleCharacters(const wchar_t *s)
|
||||
{
|
||||
Invisible = s;
|
||||
}
|
||||
|
||||
|
||||
//! returns the dimension of text
|
||||
core::dimension2d<u32> CGUIFont::getDimension(const wchar_t* text) const
|
||||
core::dimension2d<u32> CGUIFont::getDimension(const wchar_t *text) const
|
||||
{
|
||||
core::dimension2d<u32> dim(0, 0);
|
||||
core::dimension2d<u32> thisLine(0, MaxHeight);
|
||||
|
||||
for (const wchar_t* p = text; *p; ++p)
|
||||
{
|
||||
bool lineBreak=false;
|
||||
for (const wchar_t *p = text; *p; ++p) {
|
||||
bool lineBreak = false;
|
||||
if (*p == L'\r') // Mac or Windows breaks
|
||||
{
|
||||
lineBreak = true;
|
||||
if (p[1] == L'\n') // Windows breaks
|
||||
++p;
|
||||
}
|
||||
else if (*p == L'\n') // Unix breaks
|
||||
} else if (*p == L'\n') // Unix breaks
|
||||
{
|
||||
lineBreak = true;
|
||||
}
|
||||
if (lineBreak)
|
||||
{
|
||||
if (lineBreak) {
|
||||
dim.Height += thisLine.Height;
|
||||
if (dim.Width < thisLine.Width)
|
||||
dim.Width = thisLine.Width;
|
||||
@ -489,15 +456,14 @@ core::dimension2d<u32> CGUIFont::getDimension(const wchar_t* text) const
|
||||
}
|
||||
|
||||
//! draws some text and clips it to the specified rectangle if wanted
|
||||
void CGUIFont::draw(const core::stringw& text, const core::rect<s32>& position,
|
||||
video::SColor color,
|
||||
bool hcenter, bool vcenter, const core::rect<s32>* clip
|
||||
)
|
||||
void CGUIFont::draw(const core::stringw &text, const core::rect<s32> &position,
|
||||
video::SColor color,
|
||||
bool hcenter, bool vcenter, const core::rect<s32> *clip)
|
||||
{
|
||||
if (!Driver || !SpriteBank)
|
||||
return;
|
||||
|
||||
core::dimension2d<s32> textDimension; // NOTE: don't make this u32 or the >> later on can fail when the dimension width is < position width
|
||||
core::dimension2d<s32> textDimension; // NOTE: don't make this u32 or the >> later on can fail when the dimension width is < position width
|
||||
core::position2d<s32> offset = position.UpperLeftCorner;
|
||||
|
||||
if (hcenter || vcenter || clip)
|
||||
@ -509,8 +475,7 @@ void CGUIFont::draw(const core::stringw& text, const core::rect<s32>& position,
|
||||
if (vcenter)
|
||||
offset.Y += (position.getHeight() - textDimension.Height) >> 1;
|
||||
|
||||
if (clip)
|
||||
{
|
||||
if (clip) {
|
||||
core::rect<s32> clippedRect(offset, textDimension);
|
||||
clippedRect.clipAgainst(*clip);
|
||||
if (!clippedRect.isValid())
|
||||
@ -520,39 +485,34 @@ void CGUIFont::draw(const core::stringw& text, const core::rect<s32>& position,
|
||||
core::array<u32> indices(text.size());
|
||||
core::array<core::position2di> offsets(text.size());
|
||||
|
||||
for(u32 i = 0;i < text.size();i++)
|
||||
{
|
||||
for (u32 i = 0; i < text.size(); i++) {
|
||||
wchar_t c = text[i];
|
||||
|
||||
bool lineBreak=false;
|
||||
if ( c == L'\r') // Mac or Windows breaks
|
||||
bool lineBreak = false;
|
||||
if (c == L'\r') // Mac or Windows breaks
|
||||
{
|
||||
lineBreak = true;
|
||||
if ( text[i + 1] == L'\n') // Windows breaks
|
||||
if (text[i + 1] == L'\n') // Windows breaks
|
||||
c = text[++i];
|
||||
}
|
||||
else if ( c == L'\n') // Unix breaks
|
||||
} else if (c == L'\n') // Unix breaks
|
||||
{
|
||||
lineBreak = true;
|
||||
}
|
||||
|
||||
if (lineBreak)
|
||||
{
|
||||
if (lineBreak) {
|
||||
offset.Y += MaxHeight;
|
||||
offset.X = position.UpperLeftCorner.X;
|
||||
|
||||
if ( hcenter )
|
||||
{
|
||||
if (hcenter) {
|
||||
offset.X += (position.getWidth() - textDimension.Width) >> 1;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
SFontArea& area = Areas[getAreaFromCharacter(c)];
|
||||
SFontArea &area = Areas[getAreaFromCharacter(c)];
|
||||
|
||||
offset.X += area.underhang;
|
||||
if ( Invisible.findFirst ( c ) < 0 )
|
||||
{
|
||||
if (Invisible.findFirst(c) < 0) {
|
||||
indices.push_back(area.spriteno);
|
||||
offsets.push_back(offset);
|
||||
}
|
||||
@ -563,16 +523,14 @@ void CGUIFont::draw(const core::stringw& text, const core::rect<s32>& position,
|
||||
SpriteBank->draw2DSpriteBatch(indices, offsets, clip, color);
|
||||
}
|
||||
|
||||
|
||||
//! Calculates the index of the character in the text which is on a specific position.
|
||||
s32 CGUIFont::getCharacterFromPos(const wchar_t* text, s32 pixel_x) const
|
||||
s32 CGUIFont::getCharacterFromPos(const wchar_t *text, s32 pixel_x) const
|
||||
{
|
||||
s32 x = 0;
|
||||
s32 idx = 0;
|
||||
|
||||
while (text[idx])
|
||||
{
|
||||
const SFontArea& a = Areas[getAreaFromCharacter(text[idx])];
|
||||
while (text[idx]) {
|
||||
const SFontArea &a = Areas[getAreaFromCharacter(text[idx])];
|
||||
|
||||
x += a.width + a.overhang + a.underhang + GlobalKerningWidth;
|
||||
|
||||
@ -585,8 +543,7 @@ s32 CGUIFont::getCharacterFromPos(const wchar_t* text, s32 pixel_x) const
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
||||
IGUISpriteBank* CGUIFont::getSpriteBank() const
|
||||
IGUISpriteBank *CGUIFont::getSpriteBank() const
|
||||
{
|
||||
return SpriteBank;
|
||||
}
|
||||
|
Reference in New Issue
Block a user