1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-25 09:44:02 +00:00

Fix font bounds calculation

BitmapFont::getActualWidth() is still a bit less than getStringWidth(),
but won't fix that now since it shouldn't cause any issues... hopefully.
This commit is contained in:
fgenesis 2015-11-11 23:27:32 +01:00
parent d41375d5d2
commit 26549f84b7
11 changed files with 118 additions and 73 deletions

View file

@ -3562,7 +3562,7 @@ std::string DSQ::getUserInputString(std::string labelText, std::string t, bool a
text.resize(text.size()-1); text.resize(text.size()-1);
inputText->setText(text); inputText->setText(text);
} }
if (inputText->getWidth() < 800-60) if (inputText->getActualWidth() < 800-60)
{ {
doAlphabetInputKey(KEY_A, 'a', (char*)&map, &text, 'A'); doAlphabetInputKey(KEY_A, 'a', (char*)&map, &text, 'A');
doAlphabetInputKey(KEY_B, 'b', (char*)&map, &text, 'B'); doAlphabetInputKey(KEY_B, 'b', (char*)&map, &text, 'B');

View file

@ -10024,9 +10024,9 @@ const float helpTextScrollClickTime = -helpTextScrollSpeed;
void Game::onHelpDown() void Game::onHelpDown()
{ {
float to = helpText->offset.y - helpTextScrollClickAmount; float to = helpText->offset.y - helpTextScrollClickAmount;
if (to < -helpText->getFullHeight() + core->getVirtualHeight()) if (to < -helpText->getHeight() + core->getVirtualHeight())
{ {
to = -helpText->getFullHeight() + core->getVirtualHeight(); to = -helpText->getHeight() + core->getVirtualHeight();
} }
helpText->offset.interpolateTo(Vector(0, to), helpTextScrollClickTime); helpText->offset.interpolateTo(Vector(0, to), helpTextScrollClickTime);
} }
@ -10052,9 +10052,9 @@ void Game::update(float dt)
{ {
helpText->offset.stop(); helpText->offset.stop();
helpText->offset.y -= helpTextScrollSpeed * dt; helpText->offset.y -= helpTextScrollSpeed * dt;
if (helpText->offset.y < -helpText->getFullHeight() + core->getVirtualHeight()) if (helpText->offset.y < -helpText->getHeight() + core->getVirtualHeight())
{ {
helpText->offset.y = -helpText->getFullHeight() + core->getVirtualHeight(); helpText->offset.y = -helpText->getHeight() + core->getVirtualHeight();
} }
} }
if (isActing(ACTION_SWIMUP)) if (isActing(ACTION_SWIMUP))

View file

@ -9195,7 +9195,7 @@ luaFunc(text_setFontSize)
{ {
BaseText *txt = getText(L); BaseText *txt = getText(L);
if (txt) if (txt)
txt->setFontSize(lua_tointeger(L, 2)); txt->setFontSize(lua_tonumber(L, 2));
luaReturnNil(); luaReturnNil();
} }
@ -9203,7 +9203,7 @@ luaFunc(text_setWidth)
{ {
BaseText *txt = getText(L); BaseText *txt = getText(L);
if (txt) if (txt)
txt->setWidth(lua_tointeger(L, 2)); txt->setWidth(lua_tonumber(L, 2));
luaReturnNil(); luaReturnNil();
} }
@ -9227,6 +9227,12 @@ luaFunc(text_getStringWidth)
luaReturnNum(txt ? txt->getStringWidth(getString(L, 2)) : 0.0f); luaReturnNum(txt ? txt->getStringWidth(getString(L, 2)) : 0.0f);
} }
luaFunc(text_getActualWidth)
{
BaseText *txt = getText(L);
luaReturnNum(txt ? txt->getActualWidth() : 0.0f);
}
luaFunc(loadShader) luaFunc(loadShader)
{ {
int handle = 0; int handle = 0;
@ -10333,6 +10339,7 @@ static const struct {
luaRegister(text_setAlign), luaRegister(text_setAlign),
luaRegister(text_getHeight), luaRegister(text_getHeight),
luaRegister(text_getStringWidth), luaRegister(text_getStringWidth),
luaRegister(text_getActualWidth),
luaRegister(loadShader), luaRegister(loadShader),
luaRegister(createShader), luaRegister(createShader),

View file

@ -323,7 +323,7 @@ public:
text->setAlign(ALIGN_CENTER); text->setAlign(ALIGN_CENTER);
textBG = new RoundedRect(); textBG = new RoundedRect();
textBG->setWidthHeight(text->getWidth() + 20, 25, 10); // 30 textBG->setWidthHeight(text->getActualWidth() + 20, 25, 10); // 30
textBG->alpha = 0; textBG->alpha = 0;
textBG->followCamera = 1; textBG->followCamera = 1;
game->addRenderObject(textBG, LR_WORLDMAPHUD); game->addRenderObject(textBG, LR_WORLDMAPHUD);

View file

@ -10,12 +10,12 @@ public:
BaseText() { addType(SCO_TEXT); } BaseText() { addType(SCO_TEXT); }
virtual ~BaseText() {} virtual ~BaseText() {}
virtual void setText(const std::string& text) = 0; virtual void setText(const std::string& text) = 0;
virtual void setWidth(int width) = 0; virtual void setWidth(float width) = 0;
virtual void setFontSize(int sz) = 0; virtual void setFontSize(float sz) = 0;
virtual void setAlign(Align a) = 0; virtual void setAlign(Align a) = 0;
virtual float getHeight() = 0; virtual float getHeight() = 0; // total height
virtual float getStringWidth(const std::string& text) = 0; virtual float getStringWidth(const std::string& text) = 0; // width of string when not auto-wrapped
virtual float getActualWidth() = 0; // width of text after wrapping
}; };
#endif #endif

View file

@ -132,12 +132,12 @@ void BitmapText::setText(const std::string &text)
formatText(); formatText();
} }
void BitmapText::setWidth(int width) void BitmapText::setWidth(float width)
{ {
textWidth = width; textWidth = width;
} }
int BitmapText::getSetWidth() float BitmapText::getSetWidth()
{ {
return textWidth; return textWidth;
} }
@ -155,8 +155,9 @@ void BitmapText::formatText()
lines.clear(); lines.clear();
std::string currentLine; std::string currentLine;
int lastSpace = -1; int lastSpace = -1;
int currentWidth = 0; float currentWidth = 0;
alignWidth = 0; alignWidth = 0;
maxW = 0;
for (int i = 0; i < text.size(); i++) for (int i = 0; i < text.size(); i++)
{ {
//currentWidth += spacingMap[text[i]]*fontDrawSize; //currentWidth += spacingMap[text[i]]*fontDrawSize;
@ -174,6 +175,7 @@ void BitmapText::formatText()
text = text.substr(lastSpace+1, tsz); text = text.substr(lastSpace+1, tsz);
i = 0; i = 0;
alignWidth = currentWidth; alignWidth = currentWidth;
maxW = std::max(currentWidth, maxW);
currentWidth = 0; currentWidth = 0;
lastSpace = 0; lastSpace = 0;
continue; continue;
@ -182,8 +184,9 @@ void BitmapText::formatText()
if (text[i] == ' ') if (text[i] == ' ')
{ {
lastSpace = i; lastSpace = i;
} }
} }
maxW = std::max(currentWidth, maxW);
if (alignWidth == 0) if (alignWidth == 0)
alignWidth = currentWidth; alignWidth = currentWidth;
if (!text.empty() && (text.size() > 1 || text[0] != ' ')) if (!text.empty() && (text.size() > 1 || text[0] != ' '))
@ -248,7 +251,7 @@ void BitmapText::scrollText(const std::string &text, float scrollSpeed)
currentScrollChar = 0; currentScrollChar = 0;
} }
void BitmapText::setFontSize(int sz) void BitmapText::setFontSize(float sz)
{ {
this->fontDrawSize = sz; this->fontDrawSize = sz;
} }

View file

@ -56,10 +56,10 @@ class BitmapText : public BaseText
public: public:
BitmapText(BmpFont *bmpFont); BitmapText(BmpFont *bmpFont);
void setText(const std::string &text); void setText(const std::string &text);
void setWidth(int width); void setWidth(float width);
int getSetWidth(); // get the width that was set float getSetWidth(); // get the width that was set
void scrollText(const std::string &text, float scrollSpeed); void scrollText(const std::string &text, float scrollSpeed);
void setFontSize(int sz); void setFontSize(float sz);
bool isScrollingText(); bool isScrollingText();
void stopScrollingText(); void stopScrollingText();
bool isEmpty(); bool isEmpty();
@ -75,7 +75,8 @@ public:
virtual float getHeight(); virtual float getHeight();
void unloadDevice(); void unloadDevice();
void reloadDevice(); void reloadDevice();
virtual float getStringWidth(const std::string& text); float getStringWidth(const std::string& text);
float getActualWidth() { return maxW; }
int getNumLines(); int getNumLines();
protected: protected:
@ -89,18 +90,19 @@ protected:
int currentScrollLine; int currentScrollLine;
int currentScrollChar; int currentScrollChar;
Align align; Align align;
int alignWidth; float alignWidth;
typedef std::map<char, float> SpacingMap; typedef std::map<char, float> SpacingMap;
SpacingMap spacingMap; SpacingMap spacingMap;
void formatText(); void formatText();
int fontDrawSize; float fontDrawSize;
void onRender(); void onRender();
typedef std::vector<std::string> Lines; typedef std::vector<std::string> Lines;
Lines lines; Lines lines;
typedef std::vector<Vector> ColorIndices; typedef std::vector<Vector> ColorIndices;
std::vector<ColorIndices> colorIndices; std::vector<ColorIndices> colorIndices;
std::string text; std::string text;
int textWidth; float textWidth;
float maxW;
}; };
#endif #endif

View file

@ -26,6 +26,7 @@ DebugFont::DebugFont(int initSz, const std::string &initText)
followCamera = 1; followCamera = 1;
fontDrawSize = 5; fontDrawSize = 5;
textWidth = 800; textWidth = 800;
maxW = 0;
if (initSz) if (initSz)
{ {
setFontSize(initSz); setFontSize(initSz);
@ -37,12 +38,12 @@ DebugFont::DebugFont(int initSz, const std::string &initText)
} }
} }
void DebugFont::setWidth(int width) void DebugFont::setWidth(float width)
{ {
textWidth = width; textWidth = width;
} }
void DebugFont::setFontSize(int sz) void DebugFont::setFontSize(float sz)
{ {
fontDrawSize = sz; fontDrawSize = sz;
} }
@ -67,7 +68,12 @@ float DebugFont::getStringWidth(const std::string& text)
++c; ++c;
} }
maxchars = std::max(maxchars, c); maxchars = std::max(maxchars, c);
return float(fontDrawSize * maxchars); return fontDrawSize * maxchars * (1.4f * 0.75f);
}
float DebugFont::getActualWidth()
{
return maxW * (1.4f * 0.75f); // numbers taken from onRender()
} }
void DebugFont::formatText() void DebugFont::formatText()
@ -77,8 +83,8 @@ void DebugFont::formatText()
lines.clear(); lines.clear();
std::string currentLine; std::string currentLine;
int lastSpace = -1; int lastSpace = -1;
int currentWidth = 0; float currentWidth = 0;
int alignWidth = 0; maxW = 0;
for (int i = 0; i < text.size(); i++) for (int i = 0; i < text.size(); i++)
{ {
currentWidth += fontDrawSize; currentWidth += fontDrawSize;
@ -93,7 +99,7 @@ void DebugFont::formatText()
int tsz = text.size(); int tsz = text.size();
text = text.substr(lastSpace+1, tsz); text = text.substr(lastSpace+1, tsz);
i = 0; i = 0;
alignWidth = currentWidth; maxW = std::max(maxW, currentWidth);
currentWidth = 0; currentWidth = 0;
lastSpace = 0; lastSpace = 0;
continue; continue;
@ -104,8 +110,7 @@ void DebugFont::formatText()
lastSpace = i; lastSpace = i;
} }
} }
if (alignWidth == 0) maxW = std::max(maxW, currentWidth);
alignWidth = currentWidth;
if (!text.empty() && (text.size() > 1 || text[0] != ' ')) if (!text.empty() && (text.size() > 1 || text[0] != ' '))
{ {
lines.push_back(text); lines.push_back(text);

View file

@ -29,19 +29,21 @@ class DebugFont : public BaseText
public: public:
DebugFont(int initFontSize=0, const std::string &initText=""); DebugFont(int initFontSize=0, const std::string &initText="");
void setText(const std::string &text); void setText(const std::string &text);
void setWidth(int width); void setWidth(float width);
void setFontSize(int sz); void setFontSize(float sz);
int getNumLines() { return lines.size(); } int getNumLines() { return lines.size(); }
virtual void setAlign(Align align); virtual void setAlign(Align align);
virtual float getHeight(); virtual float getHeight();
virtual float getStringWidth(const std::string& text); virtual float getStringWidth(const std::string& text);
virtual float getActualWidth();
protected: protected:
int fontDrawSize, textWidth; float fontDrawSize, textWidth;
void formatText(); void formatText();
void onRender(); void onRender();
std::string text; std::string text;
std::vector<std::string> lines; std::vector<std::string> lines;
Align align; Align align;
float maxW;
}; };
class Quad; class Quad;

View file

@ -63,6 +63,7 @@ TTFText::TTFText(TTFFont *f) : font(f)
h = 0; h = 0;
width = 0; width = 0;
shadow = false; shadow = false;
maxW = 0;
} }
void TTFText::setText(const std::string &txt) void TTFText::setText(const std::string &txt)
@ -95,36 +96,41 @@ void TTFText::updateAlign()
} }
} }
float TTFText::getWidth()
{
return getStringWidth(originalText);
}
float TTFText::getHeight() float TTFText::getHeight()
{ {
float llx, lly, llz, urx, ury, urz; return text.size()*lineHeight;
font->font->BBox(originalText.c_str(), llx, lly, llz, urx, ury, urz);
return ury - lly;
} }
float TTFText::getStringWidth(const std::string& s) float TTFText::getStringWidth(const std::string& s)
{ {
float w = 0;
std::string cp = s;
float llx, lly, llz, urx, ury, urz; float llx, lly, llz, urx, ury, urz;
font->font->BBox(s.c_str(), llx, lly, llz, urx, ury, urz); const char *start = cp.c_str();
return urx - llx; size_t begin = 0;
for(size_t i = 0; i < cp.length(); ++i)
{
const char c = cp[i];
if(c == '\n')
{
cp[i] = 0;
const char *p = start + begin;
font->font->BBox(p, llx, lly, llz, urx, ury, urz);
w = std::max(w, urx - llx);
cp[i] = c;
begin = i + 1;
}
}
if(begin < cp.length())
{
font->font->BBox(start + begin, llx, lly, llz, urx, ury, urz);
w = std::max(w, urx - llx);
}
return w;
} }
float TTFText::getFullHeight() void TTFText::setWidth(float width)
{
/*
float llx, lly, llz, urx, ury, urz;
font->font->BBox(originalText.c_str(), llx, lly, llz, urx, ury, urz);
float diff = ury - lly;
*/
return text.size()*lineHeight;
}
void TTFText::setWidth(int width)
{ {
this->width = width; this->width = width;
@ -132,7 +138,7 @@ void TTFText::setWidth(int width)
updateFormatting(); updateFormatting();
} }
void TTFText::setFontSize(int) void TTFText::setFontSize(float)
{ {
} }
@ -142,12 +148,18 @@ void TTFText::updateFormatting()
text.clear(); text.clear();
int i=0; int i=0;
int sz = originalText.size(); int sz = originalText.size();
maxW = 0;
for (i = 0; i < sz; i++) for (i = 0; i < sz; i++)
{ {
if (originalText[i] == '\n') if (originalText[i] == '\n')
{ {
text.push_back(originalText.substr(start, i-start)); std::string part = originalText.substr(start, i-start);
text.push_back(part);
start = i+1; start = i+1;
float llx, lly, llz, urx, ury, urz;
font->font->BBox(part.c_str(), llx, lly, llz, urx, ury, urz);
float w = urx - llx;
maxW = std::max(maxW, w);
} }
else else
{ {
@ -156,25 +168,39 @@ void TTFText::updateFormatting()
lastSpace = i; lastSpace = i;
} }
float llx, lly, llz, urx, ury, urz; float llx, lly, llz, urx, ury, urz;
font->font->BBox(originalText.substr(start, i-start).c_str(), llx, lly, llz, urx, ury, urz); std::string part = originalText.substr(start, i-start);
int w = urx - llx; font->font->BBox(part.c_str(), llx, lly, llz, urx, ury, urz);
float w = urx - llx;
if (width != 0 && w >= width) if (width != 0 && w >= width)
{ {
if (lastSpace != -1) { if (lastSpace != -1)
text.push_back(originalText.substr(start, lastSpace-start)); {
part = originalText.substr(start, lastSpace-start);
i = lastSpace+1; i = lastSpace+1;
lastSpace = -1; lastSpace = -1;
start = i; start = i;
} }
else { else
text.push_back(originalText.substr(start, i-start)); part = originalText.substr(start, i-start);
}
text.push_back(part);
// recalc width of remaining text after linebreak
font->font->BBox(part.c_str(), llx, lly, llz, urx, ury, urz);
w = urx - llx;
} }
maxW = std::max(maxW, w);
} }
} }
if (i == sz) if (i == sz)
{ {
text.push_back(originalText.substr(start, i-start)); std::string part = originalText.substr(start, i-start);
text.push_back(part);
float llx, lly, llz, urx, ury, urz;
font->font->BBox(part.c_str(), llx, lly, llz, urx, ury, urz);
float w = urx - llx;
maxW = std::max(maxW, w);
} }
lineHeight = font->font->LineHeight(); lineHeight = font->font->LineHeight();
} }
@ -184,7 +210,7 @@ void TTFText::onUpdate(float dt)
RenderObject::onUpdate(dt); RenderObject::onUpdate(dt);
} }
int TTFText::getLineHeight() float TTFText::getLineHeight()
{ {
return lineHeight; return lineHeight;
} }

View file

@ -53,17 +53,16 @@ public:
TTFText(TTFFont *font); TTFText(TTFFont *font);
void setText(const std::string &txt); void setText(const std::string &txt);
void setAlign(Align align); void setAlign(Align align);
void setWidth(int width); void setWidth(float width);
float getWidth();
float getHeight(); float getHeight();
float getFullHeight(); float getActualWidth() { return maxW; }
void setFontSize(int); // dummy void setFontSize(float); // dummy
float getStringWidth(const std::string& s); float getStringWidth(const std::string& s);
bool shadow; bool shadow;
int findLine(const std::string &label); int findLine(const std::string &label);
int getLineHeight(); float getLineHeight();
protected: protected:
int width; float width;
float lineHeight; float lineHeight;
void updateAlign(); void updateAlign();
Align align; Align align;
@ -75,6 +74,7 @@ protected:
std::vector<std::string> text; std::vector<std::string> text;
TTFFont *font; TTFFont *font;
int hw,h; int hw,h;
float maxW;
}; };
#endif #endif