1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2025-10-05 13:51:04 +00:00

Add Lua function text_getStringWidth()

This commit is contained in:
fgenesis 2015-03-08 20:43:41 +01:00
commit b70de7f94b
6 changed files with 53 additions and 1 deletions

View file

@ -14,6 +14,7 @@ public:
virtual void setFontSize(int sz) = 0;
virtual void setAlign(Align a) = 0;
virtual float getHeight() = 0;
virtual float getStringWidth(const std::string& text) = 0;
};

View file

@ -425,6 +425,30 @@ int BitmapText::getNumLines()
return lines.size();
}
float BitmapText::getStringWidth(const std::string& text)
{
std::string tmp;
int maxsize = 0;
tmp.reserve(text.length());
for (size_t i = 0; i < text.size(); i++)
{
if(text[i] == '\n')
{
std::pair<int, int> dim;
bmpFont->font.GetStringSize(tmp, &dim);
maxsize = std::max(maxsize, dim.first);
tmp.resize(0);
}
else
tmp += text[i];
}
std::pair<int, int> dim;
bmpFont->font.GetStringSize(tmp, &dim);
maxsize = std::max(maxsize, dim.first);
return maxsize * bmpFont->scale;
}
/*
BitmapText::BitmapText() : RenderObject()
{

View file

@ -75,6 +75,7 @@ public:
virtual float getHeight();
void unloadDevice();
void reloadDevice();
virtual float getStringWidth(const std::string& text);
int getNumLines();
protected:

View file

@ -52,6 +52,24 @@ float DebugFont::getHeight()
return fontDrawSize * lines.size() * 1.5f; // vspc in render()
}
float DebugFont::getStringWidth(const std::string& text)
{
int maxchars = 0;
int c = 0;
for (size_t i = 0; i < text.size(); i++)
{
if(text[i] == '\n')
{
maxchars = std::max(maxchars, c);
c = 0;
}
else
++c;
}
maxchars = std::max(maxchars, c);
return float(fontDrawSize * maxchars);
}
void DebugFont::formatText()
{
std::string text;

View file

@ -34,6 +34,7 @@ public:
int getNumLines() { return lines.size(); }
virtual void setAlign(Align align);
virtual float getHeight();
virtual float getStringWidth(const std::string& text);
protected:
int fontDrawSize, textWidth;
void formatText();