diff options
author | Eugenio Favalli <elvenprogrammer@gmail.com> | 2007-09-10 07:46:51 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2008-12-29 15:19:33 +0100 |
commit | 7cc4dfe0c26fa84e6b9245b6874a853405dea87a (patch) | |
tree | 34f71d91195617c11cae132b67ce6f4144ab7be5 /src/gui | |
parent | eecda4921a72acd30955c14e1fb457c5ac690a2a (diff) | |
download | mana-7cc4dfe0c26fa84e6b9245b6874a853405dea87a.tar.gz mana-7cc4dfe0c26fa84e6b9245b6874a853405dea87a.tar.bz2 mana-7cc4dfe0c26fa84e6b9245b6874a853405dea87a.tar.xz mana-7cc4dfe0c26fa84e6b9245b6874a853405dea87a.zip |
Added support for True Type Fonts.
(cherry-picked from mainline)
Diffstat (limited to 'src/gui')
-rw-r--r-- | src/gui/browserbox.cpp | 56 | ||||
-rw-r--r-- | src/gui/browserbox.h | 5 | ||||
-rw-r--r-- | src/gui/gui.cpp | 26 | ||||
-rw-r--r-- | src/gui/truetypefont.cpp | 170 | ||||
-rw-r--r-- | src/gui/truetypefont.h | 134 | ||||
-rw-r--r-- | src/gui/window.cpp | 1 |
6 files changed, 327 insertions, 65 deletions
diff --git a/src/gui/browserbox.cpp b/src/gui/browserbox.cpp index 1c549949..48ac048e 100644 --- a/src/gui/browserbox.cpp +++ b/src/gui/browserbox.cpp @@ -27,14 +27,7 @@ #include "browserbox.h" #include "linkhandler.h" - -#ifdef USE_OPENGL -#include "../configuration.h" -#include "../resources/resourcemanager.h" - -int BrowserBox::instances = 0; -gcn::ImageFont* BrowserBox::browserFont = NULL; -#endif +#include "truetypefont.h" BrowserBox::BrowserBox(unsigned int mode): gcn::Widget(), @@ -46,35 +39,10 @@ BrowserBox::BrowserBox(unsigned int mode): { setFocusable(true); addMouseListener(this); - -#ifdef USE_OPENGL - if (config.getValue("opengl", 0.0f)) { - if (instances == 0) { - browserFont = new gcn::ImageFont( - "graphics/gui/browserfont.png", - " abcdefghijklmnopqrstuvwxyz" - "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567" - "89:@!\"$%&/=?^+*#[]{}()<>_;'.,\\|-~`" - "øåáÁéÉíÍóÓúÚç륣¢¡¿àãõêñÑöüäÖÜÄßèÈÅ"); - } - setFont(browserFont); - instances++; - } -#endif } BrowserBox::~BrowserBox() { -#ifdef USE_OPENGL - instances--; - - if (instances == 0) - { - // Clean up static resource font - delete browserFont; - browserFont = NULL; - } -#endif } void BrowserBox::setLinkHandler(LinkHandler* linkHandler) @@ -103,7 +71,7 @@ void BrowserBox::addRow(const std::string &row) std::string newRow; BROWSER_LINK bLink; int idx1, idx2, idx3; - gcn::ImageFont *font = dynamic_cast<gcn::ImageFont*>(getFont()); + TrueTypeFont *font = static_cast<TrueTypeFont*>(getFont()); // Use links and user defined colors if (mUseLinksAndUserColors) @@ -177,7 +145,7 @@ void BrowserBox::addRow(const std::string &row) { unsigned int j, y = 0; unsigned int nextChar; - char hyphen = '~'; + char *hyphen = "~"; int hyphenWidth = font->getWidth(hyphen); int x = 0; @@ -186,7 +154,8 @@ void BrowserBox::addRow(const std::string &row) std::string row = *i; for (j = 0; j < row.size(); j++) { - x += font->getWidth(row.at(j)); + std::string character = row.substr(j, 1); + x += font->getWidth(character); nextChar = j + 1; // Wraping between words (at blank spaces) @@ -300,7 +269,7 @@ BrowserBox::draw(gcn::Graphics *graphics) unsigned int j; int x = 0, y = 0; int wrappedLines = 0; - gcn::ImageFont *font = dynamic_cast<gcn::ImageFont*>(getFont()); + TrueTypeFont *font = static_cast<TrueTypeFont*>(getFont()); graphics->setColor(BLACK); for (TextRowIterator i = mTextRows.begin(); i != mTextRows.end(); i++) @@ -383,22 +352,23 @@ BrowserBox::draw(gcn::Graphics *graphics) { for (x = 0; x < getWidth(); x++) { - font->drawGlyph(graphics, '-', x, y); - x += font->getWidth('-') - 2; + font->drawString(graphics, "-", x, y); + x += font->getWidth("-") - 2; } break; } // Draw each char else { - font->drawGlyph(graphics, row.at(j), x, y); - x += font->getWidth(row.at(j)); + std::string character = row.substr(j, 1); + font->drawString(graphics, character, x, y); + x += font->getWidth(character.c_str()); // Auto wrap mode if (mMode == AUTO_WRAP) { unsigned int nextChar = j + 1; - char hyphen = '~'; + char *hyphen = "~"; int hyphenWidth = font->getWidth(hyphen); // Wraping between words (at blank spaces) @@ -425,7 +395,7 @@ BrowserBox::draw(gcn::Graphics *graphics) // Wrapping looong lines (brutal force) else if ((x + 2 * hyphenWidth) > getWidth()) { - font->drawGlyph(graphics, hyphen, + font->drawString(graphics, hyphen, getWidth() - hyphenWidth, y); x = 15; // Ident in new line y += font->getHeight(); diff --git a/src/gui/browserbox.h b/src/gui/browserbox.h index 39ecbda8..465ff497 100644 --- a/src/gui/browserbox.h +++ b/src/gui/browserbox.h @@ -162,11 +162,6 @@ class BrowserBox : public gcn::Widget, public gcn::MouseListener bool mUseLinksAndUserColors; int mSelectedLink; unsigned int mMaxRows; - -#ifdef USE_OPENGL - static int instances; /**< Number of Window instances */ - static gcn::ImageFont* browserFont; -#endif }; #endif diff --git a/src/gui/gui.cpp b/src/gui/gui.cpp index 96415298..a65cb0fb 100644 --- a/src/gui/gui.cpp +++ b/src/gui/gui.cpp @@ -24,11 +24,13 @@ #include <guichan/exception.hpp> #include <guichan/image.hpp> #include <guichan/imagefont.hpp> +#include <SDL/SDL_ttf.h> // Should stay here because of Guichan being sensitive to headers order #include <guichan/sdl/sdlinput.hpp> #include "focushandler.h" +#include "truetypefont.h" #include "viewport.h" #include "window.h" #include "windowcontainer.h" @@ -106,24 +108,12 @@ Gui::Gui(Graphics *graphics): // Set global font (based on ISO-8859-15) try { - mGuiFont = new gcn::ImageFont("graphics/gui/sansserif8.png", - " !\"#$%&'()*+,-./" - "0123456789:;<=>?" - "@ABCDEFGHIJKLMNO" - "PQRSTUVWXYZ[\\]^_" - "`abcdefghijklmno" - "pqrstuvwxyz{|}~|" - " ¡¢£¤¥¦§¨©ª«¬®¯" - "°±²³´µ¶·¸¹º»¼½¾¿" - "ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏ" - "ÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞß" - "àáâãäåæçèéêëìíîï" - "ðñòóôõö÷øùúûüýþÿ" - ); + mGuiFont = new TrueTypeFont("data/fonts/dejavusans.ttf", 12); } catch (gcn::Exception e) { - logger->error("Unable to load sansserif8.png!"); + logger->error(std::string("Unable to load dejavusans.ttf: ") + + e.getMessage()); } // Set speech font @@ -136,7 +126,8 @@ Gui::Gui(Graphics *graphics): } catch (gcn::Exception e) { - logger->error("Unable to load rpgfont_wider.png!"); + logger->error(std::string("Unable to load rpgfont_wider.png: ") + + e.getMessage()); } gcn::Widget::setGlobalFont(mGuiFont); @@ -152,7 +143,8 @@ Gui::Gui(Graphics *graphics): } catch (gcn::Exception e) { - logger->error("Unable to load colored hits' fonts!"); + logger->error(std::string("Unable to load colored hits' fonts: ") + + e.getMessage()); } // Initialize mouse cursor and listen for changes to the option diff --git a/src/gui/truetypefont.cpp b/src/gui/truetypefont.cpp new file mode 100644 index 00000000..bf95ca37 --- /dev/null +++ b/src/gui/truetypefont.cpp @@ -0,0 +1,170 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include "truetypefont.h" + +#include <list> + +#include <guichan/exception.hpp> + +#include "../graphics.h" +#include "../resources/image.h" + +#define CACHE_SIZE 30 + +class TextChunk +{ + public: + TextChunk(const std::string &text, gcn::Color color) : + img(NULL), text(text), color(color) + { + } + + ~TextChunk() + { + delete img; + } + + bool operator==(const TextChunk &chunk) const + { + return ( + chunk.text == text && chunk.color == color); + } + + void generate(TTF_Font *font) + { + SDL_Color sdlCol; + sdlCol.b = color.b; + sdlCol.r = color.r; + sdlCol.g = color.g; + + SDL_Surface *surface = TTF_RenderUTF8_Blended( + font, text.c_str(), sdlCol); + + if (!surface) + { + throw "Rendering font to surface failed: " + + std::string(TTF_GetError()); + } + + img = Image::load(surface); + + SDL_FreeSurface(surface); + } + + Image *img; + std::string text; + gcn::Color color; +}; + + +// Word surfaces cache +static std::list<TextChunk> cache; +typedef std::list<TextChunk>::iterator CacheIterator; + + +TrueTypeFont::TrueTypeFont(const std::string& filename, int size) +{ + if (!TTF_WasInit() && TTF_Init() == -1) + { + throw GCN_EXCEPTION("Unable to initialize SDL_ttf: " + + std::string(TTF_GetError())); + } + + mFont = TTF_OpenFont(filename.c_str(), size); + + if (mFont == NULL) + { + throw GCN_EXCEPTION("SDLTrueTypeFont::SDLTrueTypeFont: " + + std::string(TTF_GetError())); + } +} + +TrueTypeFont::~TrueTypeFont() +{ + TTF_CloseFont(mFont); + + if (TTF_WasInit()) + { + TTF_Quit(); + } +} + +void TrueTypeFont::drawString(gcn::Graphics *graphics, + const std::string &text, + int x, int y) +{ + if (text.empty()) + { + return; + } + + Graphics *g = dynamic_cast<Graphics *>(graphics); + + if (!g) + { + throw "Not a valid graphics object!"; + } + + gcn::Color col = g->getColor(); + + TextChunk chunk(text, col); + + bool found = false; + + for (CacheIterator i = cache.begin(); i != cache.end(); i++) + { + if (chunk == (*i)) + { + // Raise priority: move it to front + cache.splice(cache.begin(), cache, i); + found = true; + break; + } + } + + // Surface not found + if (!found) + { + if (cache.size() >= CACHE_SIZE) + { + cache.pop_back(); + } + cache.push_front(chunk); + cache.front().generate(mFont); + } + + g->drawImage(cache.front().img, x, y); +} + +int TrueTypeFont::getWidth(const std::string& text) const +{ + int w, h; + TTF_SizeUTF8(mFont, text.c_str(), &w, &h); + return w; +} + +int TrueTypeFont::getHeight() const +{ + return TTF_FontHeight(mFont); +} diff --git a/src/gui/truetypefont.h b/src/gui/truetypefont.h new file mode 100644 index 00000000..e0e57cd7 --- /dev/null +++ b/src/gui/truetypefont.h @@ -0,0 +1,134 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _TMW_TRUETYPEFONT_H +#define _TMW_TRUETYPEFONT_H + +#include <string> + +#include <guichan/font.hpp> +#include <guichan/graphics.hpp> +#include <SDL/SDL_ttf.h> + +/** + * A wrapper around SDL_ttf for allowing the use of TrueType fonts. + * + * <b>NOTE:</b> This class needs SDL_ttf to be initialized. + */ +class TrueTypeFont : public gcn::Font +{ + public: + /** + * Constructor. + * + * @param filename Font filename. + * @param size Font size. + */ + TrueTypeFont(const std::string& filename, int size); + + /** + * Destructor. + */ + ~TrueTypeFont(); + + virtual int getWidth(const std::string& text) const; + + virtual int getHeight() const; + + /** + * @see Font::drawString + */ + void drawString(gcn::Graphics* graphics, const std::string& text, int x, int y); + + private: + TTF_Font *mFont; +}; + +#endif +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _TMW_TRUETYPEFONT_H +#define _TMW_TRUETYPEFONT_H + +#include <string> + +#include <guichan/font.hpp> +#include <guichan/graphics.hpp> +#include <SDL/SDL_ttf.h> + +/** + * A wrapper around SDL_ttf for allowing the use of TrueType fonts. + * + * <b>NOTE:</b> This class needs SDL_ttf to be initialized. + */ +class TrueTypeFont : public gcn::Font +{ + public: + /** + * Constructor. + * + * @param filename Font filename. + * @param size Font size. + */ + TrueTypeFont(const std::string& filename, int size); + + /** + * Destructor. + */ + ~TrueTypeFont(); + + virtual int getWidth(const std::string& text) const; + + virtual int getHeight() const; + + /** + * @see Font::drawString + */ + void drawString(gcn::Graphics* graphics, const std::string& text, int x, int y); + + private: + TTF_Font *mFont; +}; + +#endif diff --git a/src/gui/window.cpp b/src/gui/window.cpp index 602441fb..630b4ddd 100644 --- a/src/gui/window.cpp +++ b/src/gui/window.cpp @@ -184,6 +184,7 @@ void Window::draw(gcn::Graphics *graphics) // Draw title if (mShowTitle) { + g->setColor(gcn::Color(0, 0, 0)); g->setFont(getFont()); g->drawText(getCaption(), 7, 5, gcn::Graphics::LEFT); } |