1
0
mirror of git://projects.qi-hardware.com/gmenu2x.git synced 2024-07-02 18:31:04 +03:00

Minor cleanups in SFontPlus and ASFont.

Removed unused includes.
Avoid importing classes into default namespace in headers.
Don't use a type alias if it does not add value.
This commit is contained in:
Maarten ter Huurne 2011-05-09 04:13:11 +02:00
parent e6be835038
commit c54dec90f5
4 changed files with 49 additions and 64 deletions

View File

@ -2,10 +2,6 @@
#include "surface.h"
#include "utilities.h"
#include <iostream>
using namespace std;
ASFont::ASFont(SDL_Surface* font) {
this->font.initFont(font);
halfHeight = getHeight()/2;
@ -18,7 +14,7 @@ ASFont::ASFont(Surface* font) {
halfLineHeight = getLineHeight()/2;
}
ASFont::ASFont(const string &font) {
ASFont::ASFont(const std::string &font) {
this->font.initFont(font);
halfHeight = getHeight()/2;
halfLineHeight = getLineHeight()/2;
@ -57,7 +53,7 @@ void ASFont::write(SDL_Surface* surface, const std::string& text, int x, int y,
font.write(surface, text, x, y);
}
void ASFont::write(SDL_Surface* surface, vector<string> *text, int x, int y, const unsigned short halign, const unsigned short valign) {
void ASFont::write(SDL_Surface* surface, std::vector<std::string> *text, int x, int y, const unsigned short halign, const unsigned short valign) {
switch (valign) {
case SFontVAlignMiddle:
y -= getHalfHeight()*text->size();
@ -67,7 +63,7 @@ void ASFont::write(SDL_Surface* surface, vector<string> *text, int x, int y, con
break;
}
for (uint i=0; i<text->size(); i++) {
for (unsigned i=0; i<text->size(); i++) {
int ix = x;
switch (halign) {
case SFontHAlignCenter:
@ -83,8 +79,8 @@ void ASFont::write(SDL_Surface* surface, vector<string> *text, int x, int y, con
}
void ASFont::write(Surface* surface, const std::string& text, int x, int y, const unsigned short halign, const unsigned short valign) {
if (text.find("\n",0)!=string::npos) {
vector<string> textArr;
if (text.find("\n", 0) != std::string::npos) {
std::vector<std::string> textArr;
split(textArr,text,"\n");
write(surface->raw, &textArr, x, y, halign, valign);
} else
@ -109,16 +105,16 @@ int ASFont::getTextWidth(const char* text) {
return font.getTextWidth(text);
}
int ASFont::getTextWidth(const std::string& text) {
if (text.find("\n",0)!=string::npos) {
vector<string> textArr;
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(vector<string> *text) {
int ASFont::getTextWidth(std::vector<std::string> *text) {
int w = 0;
for (uint i=0; i<text->size(); i++)
for (unsigned i=0; i<text->size(); i++)
w = max( getTextWidth(text->at(i).c_str()), w );
return w;
}

View File

@ -3,13 +3,13 @@
#ifndef ASFONT_H
#define ASFONT_H
#include <string>
#include <vector>
#include <SDL.h>
#include "sfontplus.h"
using std::string;
using std::vector;
#include <string>
#include <vector>
struct SDL_Surface;
class Surface;
const unsigned short SFontHAlignLeft = 0;
const unsigned short SFontHAlignRight = 1;
@ -18,13 +18,11 @@ const unsigned short SFontVAlignTop = 0;
const unsigned short SFontVAlignBottom = 1;
const unsigned short SFontVAlignMiddle = 2;
class Surface;
class ASFont {
public:
ASFont(SDL_Surface* font);
ASFont(Surface* font);
ASFont(const string &font);
ASFont(const std::string &font);
~ASFont();
bool utf8Code(unsigned char c);
@ -34,11 +32,11 @@ public:
int getLineHeight();
int getHalfLineHeight();
int getTextWidth(const char* text);
int getTextWidth(const string& text);
int getTextWidth(vector<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 std::string& text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0);
void write(SDL_Surface* surface, vector<string> *text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0);
void write(SDL_Surface* surface, std::vector<std::string> *text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0);
void write(Surface* surface, const std::string& text, int x, int y, const unsigned short halign = 0, const unsigned short valign = 0);
private:

View File

@ -1,11 +1,7 @@
#include "sfontplus.h"
#include "imageio.h"
#include <cassert>
#include <iostream>
using namespace std;
Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) {
assert(x>=0);
@ -49,7 +45,7 @@ SFontPlus::SFontPlus(SDL_Surface* font) {
initFont(font);
}
SFontPlus::SFontPlus(const string &font) {
SFontPlus::SFontPlus(const std::string &font) {
surface = NULL;
initFont(font);
}
@ -63,7 +59,7 @@ bool SFontPlus::utf8Code(unsigned char c) {
//return c>=194;
}
void SFontPlus::initFont(const string &font, const string &characters) {
void SFontPlus::initFont(const std::string &font, const std::string &characters) {
SDL_Surface *buf = loadPNG(font);
if (buf!=NULL) {
initFont( SDL_DisplayFormatAlpha(buf), characters );
@ -71,7 +67,7 @@ void SFontPlus::initFont(const string &font, const string &characters) {
}
}
void SFontPlus::initFont(SDL_Surface *font, const string &characters) {
void SFontPlus::initFont(SDL_Surface *font, const std::string &characters) {
freeFont();
this->characters = characters;
if (font==NULL) return;
@ -79,22 +75,22 @@ void SFontPlus::initFont(SDL_Surface *font, const string &characters) {
Uint32 pink = SDL_MapRGB(surface->format, 255,0,255);
#ifdef DEBUG
bool utf8 = false;
for (uint x=0; x<characters.length(); x++) {
for (unsigned x=0; x<characters.length(); x++) {
if (!utf8) utf8 = (unsigned char)characters[x]>128;
if (utf8) DEBUG("%d\n", (unsigned char)characters[x]);
}
#endif
uint c = 0;
unsigned c = 0;
SDL_LockSurface(surface);
for (uint x=0; x<(uint)surface->w && c<characters.length(); x++) {
for (unsigned x=0; x<(unsigned)surface->w && c<characters.length(); x++) {
if (getPixel(x,0) == pink) {
uint startx = x;
unsigned startx = x;
charpos.push_back(x);
x++;
while (x<(uint)surface->w && getPixel(x,0) == pink) x++;
while (x<(unsigned)surface->w && getPixel(x,0) == pink) x++;
charpos.push_back(x);
//utf8 characters
@ -110,12 +106,12 @@ void SFontPlus::initFont(SDL_Surface *font, const string &characters) {
SDL_UnlockSurface(surface);
Uint32 colKey = getPixel(0,surface->h-1);
SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey);
string::size_type pos = characters.find("0")*2;
std::string::size_type pos = characters.find("0")*2;
SDL_Rect srcrect = {charpos[pos], 1, charpos[pos+2] - charpos[pos], surface->h - 1};
uint y = srcrect.h;
unsigned y = srcrect.h;
bool nonKeyFound = false;
while (y-- > 0 && !nonKeyFound) {
uint x = srcrect.w;
unsigned x = srcrect.w;
while (x-- > 0 && !nonKeyFound)
nonKeyFound = getPixel(x+srcrect.x,y+srcrect.y) != colKey;
}
@ -129,10 +125,10 @@ void SFontPlus::freeFont() {
}
}
void SFontPlus::write(SDL_Surface *s, const string &text, int x, int y) {
void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) {
if (text.empty()) return;
string::size_type pos;
std::string::size_type pos;
SDL_Rect srcrect, dstrect;
// these values won't change in the loop
@ -140,14 +136,14 @@ void SFontPlus::write(SDL_Surface *s, const string &text, int x, int y) {
dstrect.y = y;
srcrect.h = dstrect.h = surface->h-1;
for(uint i=0; i<text.length() && x<surface->w; i++) {
for(unsigned i=0; i<text.length() && x<surface->w; i++) {
//Utf8 characters
if (utf8Code(text[i]) && i+1<text.length()) {
pos = characters.find(text.substr(i,2));
i++;
} else
pos = characters.find(text[i]);
if (pos == string::npos) {
if (pos == std::string::npos) {
x += charpos[2]-charpos[1];
continue;
}
@ -164,18 +160,18 @@ void SFontPlus::write(SDL_Surface *s, const string &text, int x, int y) {
}
}
uint SFontPlus::getTextWidth(const string &text) {
string::size_type pos;
unsigned SFontPlus::getTextWidth(const std::string &text) {
std::string::size_type pos;
int width = 0;
for(uint x=0; x<text.length(); x++) {
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 (pos == string::npos) {
if (pos == std::string::npos) {
width += charpos[2]-charpos[1];
continue;
}
@ -187,10 +183,10 @@ uint SFontPlus::getTextWidth(const string &text) {
return width;
}
uint SFontPlus::getHeight() {
unsigned SFontPlus::getHeight() {
return surface->h - 1;
}
uint SFontPlus::getLineHeight() {
unsigned SFontPlus::getLineHeight() {
return lineHeight;
}

View File

@ -6,38 +6,33 @@
#include <vector>
#define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ"
#ifdef _WIN32
typedef unsigned int uint;
#endif
using std::vector;
using std::string;
class SFontPlus {
private:
Uint32 getPixel(Sint32 x, Sint32 y);
SDL_Surface *surface;
vector<uint> charpos;
string characters;
uint height, lineHeight;
std::vector<unsigned> charpos;
std::string characters;
unsigned height, lineHeight;
public:
SFontPlus();
SFontPlus(SDL_Surface *font);
SFontPlus(const string &font);
SFontPlus(const std::string &font);
~SFontPlus();
bool utf8Code(unsigned char c);
void initFont(SDL_Surface *font, const string &characters = SFONTPLUS_CHARSET);
void initFont(const string &font, const string &characters = SFONTPLUS_CHARSET);
void initFont(SDL_Surface *font, const std::string &characters = SFONTPLUS_CHARSET);
void initFont(const std::string &font, const std::string &characters = SFONTPLUS_CHARSET);
void freeFont();
void write(SDL_Surface *s, const string &text, int x, int y);
void write(SDL_Surface *s, const std::string &text, int x, int y);
uint getTextWidth(const string &text);
uint getHeight();
uint getLineHeight();
unsigned getTextWidth(const std::string &text);
unsigned getHeight();
unsigned getLineHeight();
};
#endif /* SFONTPLUS_H */