1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-29 13:44:05 +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; std::string::size_type pos;
int width = 0; if (utf8Code(ch) && *text) {
// 2-byte character.
for(unsigned x=0; x<text.length(); x++) { pos = characters.find(std::string(&text[-1], 2));
//Utf8 characters text++;
if (utf8Code(text[x]) && x+1<text.length()) { } else {
pos = characters.find(text.substr(x,2)); // 1-byte character.
x++; pos = characters.find(ch);
} else }
pos = characters.find(text[x]);
if (pos == std::string::npos) { if (pos == std::string::npos) {
width += charpos[2]-charpos[1]; pos = 0;
continue;
} }
width += charpos[pos * 2 + 2] - charpos[pos * 2 + 1];
pos *= 2;
width += charpos[pos+2] - charpos[pos+1];
} }
}
return width; return max(width, maxWidth);
} }
unsigned SFontPlus::getHeight() { unsigned SFontPlus::getHeight() {
@ -264,16 +267,5 @@ int ASFont::getTextWidth(const char* text) {
return font.getTextWidth(text); return font.getTextWidth(text);
} }
int ASFont::getTextWidth(const std::string& text) { int ASFont::getTextWidth(const std::string& text) {
if (text.find("\n", 0) != std::string::npos) { return font.getTextWidth(text.c_str());
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;
} }

View File

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