1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-07-04 21:02:56 +03: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) {
std::string::size_type pos; unsigned maxWidth = 0, width = 0;
int width = 0; while (char ch = *text++) {
if (ch == '\n') {
for(unsigned x=0; x<text.length(); x++) { // New line.
//Utf8 characters maxWidth = max(width, maxWidth);
if (utf8Code(text[x]) && x+1<text.length()) { width = 0;
pos = characters.find(text.substr(x,2)); } else {
x++; std::string::size_type pos;
} else if (utf8Code(ch) && *text) {
pos = characters.find(text[x]); // 2-byte character.
if (pos == std::string::npos) { pos = characters.find(std::string(&text[-1], 2));
width += charpos[2]-charpos[1]; text++;
continue; } else {
// 1-byte character.
pos = characters.find(ch);
}
if (pos == std::string::npos) {
pos = 0;
}
width += charpos[pos * 2 + 2] - charpos[pos * 2 + 1];
} }
pos *= 2;
width += charpos[pos+2] - charpos[pos+1];
} }
return max(width, maxWidth);
return width;
} }
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);