From ec5d426d83eefa78f4a696cb0392d90a126b9eac Mon Sep 17 00:00:00 2001 From: Maarten ter Huurne Date: Mon, 9 May 2011 14:25:16 +0200 Subject: [PATCH] ASFont: moved SFontPlus code into ASFont source files. This is in preparation of merging the classes. --- src/Makefile.am | 4 +- src/asfont.cpp | 191 +++++++++++++++++++++++++++++++++++++++++++++ src/asfont.h | 38 ++++++++- src/gmenu2x.cpp | 1 - src/sfontplus.cpp | 192 ---------------------------------------------- src/sfontplus.h | 38 --------- 6 files changed, 227 insertions(+), 237 deletions(-) delete mode 100644 src/sfontplus.cpp delete mode 100644 src/sfontplus.h diff --git a/src/Makefile.am b/src/Makefile.am index d0b5716..ee16c15 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -8,7 +8,7 @@ gmenu2x_SOURCES = asfont.cpp button.cpp cpu.cpp dirdialog.cpp filedialog.cpp \ menusettingmultistring.cpp menusettingrgba.cpp menusettingstring.cpp \ menusettingstringbase.cpp \ messagebox.cpp selector.cpp \ - settingsdialog.cpp sfontplus.cpp surfacecollection.cpp surface.cpp \ + settingsdialog.cpp surfacecollection.cpp surface.cpp \ textdialog.cpp textmanualdialog.cpp touchscreen.cpp translator.cpp \ utilities.cpp wallpaperdialog.cpp \ browsedialog.cpp buttonbox.cpp dialog.cpp \ @@ -22,7 +22,7 @@ noinst_HEADERS = asfont.h button.h cpu.h dirdialog.h FastDelegate.h \ menusettingmultistring.h menusettingrgba.h menusettingstring.h \ menusettingstringbase.h \ messagebox.h selector.h settingsdialog.h \ - sfontplus.h surfacecollection.h surface.h textdialog.h textmanualdialog.h \ + surfacecollection.h surface.h textdialog.h textmanualdialog.h \ touchscreen.h translator.h utilities.h wallpaperdialog.h \ browsedialog.h buttonbox.h dialog.h \ imageio.h diff --git a/src/asfont.cpp b/src/asfont.cpp index 46eb776..7a8ff4d 100644 --- a/src/asfont.cpp +++ b/src/asfont.cpp @@ -1,7 +1,198 @@ #include "asfont.h" +#include "imageio.h" #include "surface.h" #include "utilities.h" +#include + +Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { + assert(x>=0); + assert(xw); + assert(y>=0); + assert(yh); + + Uint32 Bpp = surface->format->BytesPerPixel; + + // Get the pixel + switch(Bpp) { + case 1: + return *((Uint8 *)surface->pixels + y * surface->pitch + x); + break; + case 2: + return *((Uint16 *)surface->pixels + y * surface->pitch/2 + x); + break; + case 3: { // Format/endian independent + Uint8 *bits = ((Uint8 *)surface->pixels)+y*surface->pitch+x*Bpp; + Uint8 r, g, b; + r = *((bits)+surface->format->Rshift/8); + g = *((bits)+surface->format->Gshift/8); + b = *((bits)+surface->format->Bshift/8); + return SDL_MapRGB(surface->format, r, g, b); + } + break; + case 4: + return *((Uint32 *)surface->pixels + y * surface->pitch/4 + x); + break; + } + + return 0; +} + +SFontPlus::SFontPlus() { + surface = NULL; +} + +SFontPlus::SFontPlus(SDL_Surface* font) { + surface = NULL; + initFont(font); +} + +SFontPlus::SFontPlus(const std::string &font) { + surface = NULL; + initFont(font); +} + +SFontPlus::~SFontPlus() { + freeFont(); +} + +bool SFontPlus::utf8Code(unsigned char c) { + return (c>=194 && c<=198) || c==208 || c==209; + //return c>=194; +} + +void SFontPlus::initFont(const std::string &font, const std::string &characters) { + SDL_Surface *buf = loadPNG(font); + if (buf!=NULL) { + initFont( SDL_DisplayFormatAlpha(buf), characters ); + SDL_FreeSurface(buf); + } +} + +void SFontPlus::initFont(SDL_Surface *font, const std::string &characters) { + freeFont(); + this->characters = characters; + if (font==NULL) return; + surface = font; + Uint32 pink = SDL_MapRGB(surface->format, 255,0,255); +#ifdef DEBUG + bool utf8 = false; + for (unsigned x=0; x128; + if (utf8) DEBUG("%d\n", (unsigned char)characters[x]); + } +#endif + + unsigned c = 0; + + SDL_LockSurface(surface); + for (unsigned x=0; x<(unsigned)surface->w && cw && getPixel(x,0) == pink) x++; + charpos.push_back(x); + + //utf8 characters + if (c>0 && utf8Code(characters[c-1])) { + charpos.push_back(startx); + charpos.push_back(x); + c++; + } + + c++; + } + } + SDL_UnlockSurface(surface); + Uint32 colKey = getPixel(0,surface->h-1); + SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey); + std::string::size_type pos = characters.find("0")*2; + SDL_Rect srcrect = {charpos[pos], 1, charpos[pos+2] - charpos[pos], surface->h - 1}; + unsigned y = srcrect.h; + bool nonKeyFound = false; + while (y-- > 0 && !nonKeyFound) { + unsigned x = srcrect.w; + while (x-- > 0 && !nonKeyFound) + nonKeyFound = getPixel(x+srcrect.x,y+srcrect.y) != colKey; + } + lineHeight = y+1; +} + +void SFontPlus::freeFont() { + if (surface!=NULL) { + SDL_FreeSurface(surface); + surface = NULL; + } +} + +void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) { + if (text.empty()) return; + + std::string::size_type pos; + SDL_Rect srcrect, dstrect; + + // these values won't change in the loop + srcrect.y = 1; + dstrect.y = y; + srcrect.h = dstrect.h = surface->h-1; + + for(unsigned i=0; iw; i++) { + //Utf8 characters + if (utf8Code(text[i]) && i+1h - 1; +} + +unsigned SFontPlus::getLineHeight() { + return lineHeight; +} + ASFont::ASFont(SDL_Surface* font) { this->font.initFont(font); halfHeight = getHeight()/2; diff --git a/src/asfont.h b/src/asfont.h index 4ff54ff..ce54ffd 100644 --- a/src/asfont.h +++ b/src/asfont.h @@ -1,16 +1,46 @@ -//Advanced SFont by Massimiliano Torromeo (cpp wrapper around SFont) +// Based on SFont by Karl Bartel. +// Adapted to C++ by Massimiliano Torromeo. +// Refactored by Maarten ter Huurne and several others (see git log). +// License: GPL version 2 or later. #ifndef ASFONT_H #define ASFONT_H -#include "sfontplus.h" - +#include #include #include -struct SDL_Surface; class Surface; +#define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" + +class SFontPlus { +private: + Uint32 getPixel(Sint32 x, Sint32 y); + + SDL_Surface *surface; + std::vector charpos; + std::string characters; + unsigned height, lineHeight; + +public: + SFontPlus(); + SFontPlus(SDL_Surface *font); + SFontPlus(const std::string &font); + ~SFontPlus(); + + bool utf8Code(unsigned char c); + + 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 std::string &text, int x, int y); + + unsigned getTextWidth(const std::string &text); + unsigned getHeight(); + unsigned getLineHeight(); +}; class ASFont { public: diff --git a/src/gmenu2x.cpp b/src/gmenu2x.cpp index 2609b07..0870a40 100644 --- a/src/gmenu2x.cpp +++ b/src/gmenu2x.cpp @@ -48,7 +48,6 @@ #include "linkaction.h" #include "menu.h" #include "asfont.h" -#include "sfontplus.h" #include "surface.h" #include "filedialog.h" #include "gmenu2x.h" diff --git a/src/sfontplus.cpp b/src/sfontplus.cpp deleted file mode 100644 index 4059e6f..0000000 --- a/src/sfontplus.cpp +++ /dev/null @@ -1,192 +0,0 @@ -#include "sfontplus.h" -#include "imageio.h" - -#include - -Uint32 SFontPlus::getPixel(Sint32 x, Sint32 y) { - assert(x>=0); - assert(xw); - assert(y>=0); - assert(yh); - - Uint32 Bpp = surface->format->BytesPerPixel; - - // Get the pixel - switch(Bpp) { - case 1: - return *((Uint8 *)surface->pixels + y * surface->pitch + x); - break; - case 2: - return *((Uint16 *)surface->pixels + y * surface->pitch/2 + x); - break; - case 3: { // Format/endian independent - Uint8 *bits = ((Uint8 *)surface->pixels)+y*surface->pitch+x*Bpp; - Uint8 r, g, b; - r = *((bits)+surface->format->Rshift/8); - g = *((bits)+surface->format->Gshift/8); - b = *((bits)+surface->format->Bshift/8); - return SDL_MapRGB(surface->format, r, g, b); - } - break; - case 4: - return *((Uint32 *)surface->pixels + y * surface->pitch/4 + x); - break; - } - - return 0; -} - -SFontPlus::SFontPlus() { - surface = NULL; -} - -SFontPlus::SFontPlus(SDL_Surface* font) { - surface = NULL; - initFont(font); -} - -SFontPlus::SFontPlus(const std::string &font) { - surface = NULL; - initFont(font); -} - -SFontPlus::~SFontPlus() { - freeFont(); -} - -bool SFontPlus::utf8Code(unsigned char c) { - return (c>=194 && c<=198) || c==208 || c==209; - //return c>=194; -} - -void SFontPlus::initFont(const std::string &font, const std::string &characters) { - SDL_Surface *buf = loadPNG(font); - if (buf!=NULL) { - initFont( SDL_DisplayFormatAlpha(buf), characters ); - SDL_FreeSurface(buf); - } -} - -void SFontPlus::initFont(SDL_Surface *font, const std::string &characters) { - freeFont(); - this->characters = characters; - if (font==NULL) return; - surface = font; - Uint32 pink = SDL_MapRGB(surface->format, 255,0,255); -#ifdef DEBUG - bool utf8 = false; - for (unsigned x=0; x128; - if (utf8) DEBUG("%d\n", (unsigned char)characters[x]); - } -#endif - - unsigned c = 0; - - SDL_LockSurface(surface); - for (unsigned x=0; x<(unsigned)surface->w && cw && getPixel(x,0) == pink) x++; - charpos.push_back(x); - - //utf8 characters - if (c>0 && utf8Code(characters[c-1])) { - charpos.push_back(startx); - charpos.push_back(x); - c++; - } - - c++; - } - } - SDL_UnlockSurface(surface); - Uint32 colKey = getPixel(0,surface->h-1); - SDL_SetColorKey(surface, SDL_SRCCOLORKEY, colKey); - std::string::size_type pos = characters.find("0")*2; - SDL_Rect srcrect = {charpos[pos], 1, charpos[pos+2] - charpos[pos], surface->h - 1}; - unsigned y = srcrect.h; - bool nonKeyFound = false; - while (y-- > 0 && !nonKeyFound) { - unsigned x = srcrect.w; - while (x-- > 0 && !nonKeyFound) - nonKeyFound = getPixel(x+srcrect.x,y+srcrect.y) != colKey; - } - lineHeight = y+1; -} - -void SFontPlus::freeFont() { - if (surface!=NULL) { - SDL_FreeSurface(surface); - surface = NULL; - } -} - -void SFontPlus::write(SDL_Surface *s, const std::string &text, int x, int y) { - if (text.empty()) return; - - std::string::size_type pos; - SDL_Rect srcrect, dstrect; - - // these values won't change in the loop - srcrect.y = 1; - dstrect.y = y; - srcrect.h = dstrect.h = surface->h-1; - - for(unsigned i=0; iw; i++) { - //Utf8 characters - if (utf8Code(text[i]) && i+1h - 1; -} - -unsigned SFontPlus::getLineHeight() { - return lineHeight; -} diff --git a/src/sfontplus.h b/src/sfontplus.h deleted file mode 100644 index 7f6b0db..0000000 --- a/src/sfontplus.h +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef SFONTPLUS_H -#define SFONTPLUS_H - -#include -#include -#include - -#define SFONTPLUS_CHARSET "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~¡¿ÀÁÈÉÌÍÒÓÙÚÝÄËÏÖÜŸÂÊÎÔÛÅÃÕÑÆÇČĎĚĽĹŇÔŘŔŠŤŮŽàáèéìíòóùúýäëïöüÿâêîôûåãõñæçčďěľĺňôřŕšťžůðßÐÞþАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦЧШЩЪЫЬЭЮЯабвгдеёжзийклмнопрстуфхцчшщъыьэюяØøąćęłńśżźĄĆĘŁŃŚŻŹ" - -class SFontPlus { -private: - Uint32 getPixel(Sint32 x, Sint32 y); - - SDL_Surface *surface; - std::vector charpos; - std::string characters; - unsigned height, lineHeight; - -public: - SFontPlus(); - SFontPlus(SDL_Surface *font); - SFontPlus(const std::string &font); - ~SFontPlus(); - - bool utf8Code(unsigned char c); - - 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 std::string &text, int x, int y); - - unsigned getTextWidth(const std::string &text); - unsigned getHeight(); - unsigned getLineHeight(); -}; - -#endif /* SFONTPLUS_H */