1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-11-26 03:06:15 +02:00

ASFont: merged SFontPlus class into ASFont class.

This commit is contained in:
Maarten ter Huurne 2011-05-10 02:37:10 +02:00
parent 1bc55392cd
commit b4f3cde526
2 changed files with 29 additions and 70 deletions

View File

@ -7,7 +7,7 @@
#define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" #define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ"
Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { Uint32 ASFont::getPixel(Sint32 x, Sint32 y) {
assert(x>=0); assert(x>=0);
assert(x<surface->w); assert(x<surface->w);
assert(y>=0); assert(y>=0);
@ -40,8 +40,8 @@ Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) {
return 0; return 0;
} }
SFontPlus::SFontPlus(const std::string &fontImagePath, const std::string &characters) { ASFont::ASFont(const std::string &fontImagePath) {
this->characters = characters; this->characters = SFONTPLUS_CHARSET;
SDL_Surface *buf = loadPNG(fontImagePath); SDL_Surface *buf = loadPNG(fontImagePath);
if (!buf) { if (!buf) {
@ -97,18 +97,18 @@ SFontPlus::SFontPlus(const std::string &fontImagePath, const std::string &charac
lineHeight = y+1; lineHeight = y+1;
} }
SFontPlus::~SFontPlus() { ASFont::~ASFont() {
if (surface) { if (surface) {
SDL_FreeSurface(surface); SDL_FreeSurface(surface);
} }
} }
bool SFontPlus::utf8Code(unsigned char c) { bool ASFont::utf8Code(unsigned char c) {
return (c>=194 && c<=198) || c==208 || c==209; return (c>=194 && c<=198) || c==208 || c==209;
//return c>=194; //return c>=194;
} }
void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) { void ASFont::write(SDL_Surface *s, const std::string &text, int x, int y) {
if (text.empty()) return; if (text.empty()) return;
std::string::size_type pos; std::string::size_type pos;
@ -143,8 +143,8 @@ void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) {
} }
} }
unsigned SFontPlus::getTextWidth(const char *text) { int ASFont::getTextWidth(const char *text) {
unsigned maxWidth = 0, width = 0; int maxWidth = 0, width = 0;
while (char ch = *text++) { while (char ch = *text++) {
if (ch == '\n') { if (ch == '\n') {
// New line. // New line.
@ -169,24 +169,8 @@ unsigned SFontPlus::getTextWidth(const char *text) {
return max(width, maxWidth); return max(width, maxWidth);
} }
unsigned SFontPlus::getHeight() { int ASFont::getTextWidth(const std::string& text) {
return surface->h - 1; return getTextWidth(text.c_str());
}
unsigned SFontPlus::getLineHeight() {
return lineHeight;
}
ASFont::ASFont(const std::string &fontImagePath)
: font(fontImagePath, SFONTPLUS_CHARSET)
{
}
ASFont::~ASFont() {
}
bool ASFont::utf8Code(unsigned char c) {
return font.utf8Code(c);
} }
void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) { void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign) {
@ -212,7 +196,7 @@ void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y,
break; break;
} }
font.write(surface, text, x, y); write(surface, text, x, y);
} }
void ASFont::write(SDL_Surface* surface, const std::vector<std::string> &text, int x, int y, HAlign halign, VAlign valign) { void ASFont::write(SDL_Surface* surface, const std::vector<std::string> &text, int x, int y, HAlign halign, VAlign valign) {
switch (valign) { switch (valign) {
@ -239,7 +223,7 @@ void ASFont::write(SDL_Surface* surface, const std::vector<std::string> &text, i
break; break;
} }
font.write(surface, *it, ix, y); write(surface, *it, ix, y);
y += getHeight(); y += getHeight();
} }
} }
@ -252,17 +236,3 @@ void ASFont::write(Surface* surface, const std::string& text, int x, int y, HAli
} else } else
write(surface->raw, text, x, y, halign, valign); write(surface->raw, text, x, y, halign, valign);
} }
int ASFont::getHeight() {
return font.getHeight();
}
int ASFont::getLineHeight() {
return font.getLineHeight();
}
int ASFont::getTextWidth(const char* text) {
return font.getTextWidth(text);
}
int ASFont::getTextWidth(const std::string& text) {
return font.getTextWidth(text.c_str());
}

View File

@ -12,28 +12,6 @@
class Surface; class Surface;
class SFontPlus {
public:
SFontPlus(const std::string &fontImagePath, const std::string &characters);
~SFontPlus();
bool utf8Code(unsigned char c);
void write(SDL_Surface *s, const std::string &text, int x, int y);
unsigned getTextWidth(const char *text);
unsigned getHeight();
unsigned getLineHeight();
private:
Uint32 getPixel(Sint32 x, Sint32 y);
SDL_Surface *surface;
std::vector<unsigned> charpos;
std::string characters;
unsigned height, lineHeight;
};
class ASFont { class ASFont {
public: public:
enum HAlign { HAlignLeft, HAlignRight, HAlignCenter }; enum HAlign { HAlignLeft, HAlignRight, HAlignCenter };
@ -44,17 +22,28 @@ public:
bool utf8Code(unsigned char c); bool utf8Code(unsigned char c);
int getHeight(); int getTextWidth(const char *text);
int getLineHeight();
int getTextWidth(const char* text);
int getTextWidth(const std::string& text); int getTextWidth(const std::string& text);
int getHeight() {
return surface->h - 1;
}
int getLineHeight() {
return lineHeight;
}
void write(Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop); void write(Surface* surface, const std::string& text, int x, int y, HAlign halign = HAlignLeft, VAlign valign = VAlignTop);
private: private:
void write(SDL_Surface* surface, const std::string& text, int x, int y, HAlign halign, VAlign valign); void write(SDL_Surface *s, const std::string &text, int x, int y);
void write(SDL_Surface* surface, const std::vector<std::string> &text, int x, int y, HAlign halign, VAlign valign); void write(SDL_Surface *surface, const std::string& text, int x, int y, HAlign halign, VAlign valign);
void write(SDL_Surface *surface, const std::vector<std::string> &text, int x, int y, HAlign halign, VAlign valign);
Uint32 getPixel(Sint32 x, Sint32 y);
SFontPlus font; SDL_Surface *surface;
std::vector<unsigned> charpos;
std::string characters;
int lineHeight;
}; };
#endif /* ASFONT_H */ #endif /* ASFONT_H */