1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-29 11:01:34 +02:00

ASFont: have a single routine for computing text width that also handles newlines.

This way the text has to be scanned only once and no copying is needed.
This commit is contained in:
Maarten ter Huurne 2011-05-10 02:08:14 +02:00
parent e4b71138b9
commit ff546cdcb0
2 changed files with 24 additions and 33 deletions

View File

@ -143,27 +143,30 @@ void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) {
}
}
unsigned SFontPlus::getTextWidth(const std::string &text) {
unsigned SFontPlus::getTextWidth(const char *text) {
unsigned maxWidth = 0, width = 0;
while (char ch = *text++) {
if (ch == '\n') {
// New line.
maxWidth = max(width, maxWidth);
width = 0;
} else {
std::string::size_type pos;
int width = 0;
for(unsigned x=0; x<text.length(); x++) {
//Utf8 characters
if (utf8Code(text[x]) && x+1<text.length()) {
pos = characters.find(text.substr(x,2));
x++;
} else
pos = characters.find(text[x]);
if (utf8Code(ch) && *text) {
// 2-byte character.
pos = characters.find(std::string(&text[-1], 2));
text++;
} else {
// 1-byte character.
pos = characters.find(ch);
}
if (pos == std::string::npos) {
width += charpos[2]-charpos[1];
continue;
pos = 0;
}
pos *= 2;
width += charpos[pos+2] - charpos[pos+1];
width += charpos[pos * 2 + 2] - charpos[pos * 2 + 1];
}
return width;
}
return max(width, maxWidth);
}
unsigned SFontPlus::getHeight() {
@ -264,16 +267,5 @@ int ASFont::getTextWidth(const char* text) {
return font.getTextWidth(text);
}
int ASFont::getTextWidth(const std::string& text) {
if (text.find("\n", 0) != std::string::npos) {
std::vector<std::string> textArr;
split(textArr,text,"\n");
return getTextWidth(&textArr);
} else
return getTextWidth(text.c_str());
}
int ASFont::getTextWidth(std::vector<std::string> *text) {
int w = 0;
for (unsigned i=0; i<text->size(); i++)
w = max( getTextWidth(text->at(i).c_str()), w );
return w;
return font.getTextWidth(text.c_str());
}

View File

@ -21,7 +21,7 @@ public:
void write(SDL_Surface *s, const std::string &text, int x, int y);
unsigned getTextWidth(const std::string &text);
unsigned getTextWidth(const char *text);
unsigned getHeight();
unsigned getLineHeight();
@ -48,7 +48,6 @@ public:
int getLineHeight();
int getTextWidth(const char* text);
int getTextWidth(const std::string& text);
int getTextWidth(std::vector<std::string> *text);
void write(SDL_Surface* surface, const char* text, int x, int y);
void write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop);
void write(SDL_Surface* surface, std::vector<std::string> *text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop);