+#include <SDL_image.h>
+#include <physfs.h>
+
+#include "lisp/list_iterator.hpp"
+#include "lisp/parser.hpp"
+#include "physfs/physfs_sdl.hpp"
+#include "supertux/screen.hpp"
+#include "util/file_system.hpp"
+#include "util/log.hpp"
+#include "util/utf8_iterator.hpp"
+#include "video/drawing_context.hpp"
+#include "video/drawing_request.hpp"
+#include "video/font.hpp"
+#include "video/renderer.hpp"
+
+namespace {
+
+bool vline_empty(SDL_Surface* surface, int x, int start_y, int end_y, Uint8 threshold)
+{
+ Uint8* pixels = (Uint8*)surface->pixels;
+
+ for(int y = start_y; y < end_y; ++y)
+ {
+ const Uint8& p = pixels[surface->pitch*y + x*surface->format->BytesPerPixel + 3];
+ if (p > threshold)
+ {
+ return false;
+ }
+ }
+ return true;
+}
+
+} // namespace
+
+Font::Font(GlyphWidth glyph_width_,
+ const std::string& filename,
+ int shadowsize_) :
+ glyph_width(glyph_width_),
+ glyph_surfaces(),
+ shadow_surfaces(),
+ char_height(),
+ shadowsize(shadowsize_),
+ border(0),
+ rtl(false),
+ glyphs(65536)
+{
+ for(unsigned int i=0; i<65536;i++) glyphs[i].surface_idx = -1;
+
+ const std::string fontdir = FileSystem::dirname(filename);
+ const std::string fontname = FileSystem::basename(filename);
+
+ // scan for prefix-filename in addons search path
+ char **rc = PHYSFS_enumerateFiles(fontdir.c_str());
+ for (char **i = rc; *i != NULL; i++) {
+ std::string filename_(*i);
+ if( filename_.rfind(fontname) != std::string::npos ) {
+ loadFontFile(fontdir + filename_);
+ }
+ }
+ PHYSFS_freeList(rc);
+}
+
+void
+Font::loadFontFile(const std::string &filename)
+{
+ lisp::Parser parser;
+ log_debug << "Loading font: " << filename << std::endl;
+ const lisp::Lisp* root = parser.parse(filename);
+ const lisp::Lisp* config_l = root->get_lisp("supertux-font");
+
+ if(!config_l) {
+ std::ostringstream msg;
+ msg << "Font file:" << filename << ": is not a supertux-font file";
+ throw std::runtime_error(msg.str());
+ }
+
+ int def_char_width=0;
+
+ if( !config_l->get("glyph-width",def_char_width) ) {
+ log_warning << "Font:"<< filename << ": misses default glyph-width" << std::endl;
+ }
+
+ if( !config_l->get("glyph-height",char_height) ) {
+ std::ostringstream msg;
+ msg << "Font:" << filename << ": misses glyph-height";
+ throw std::runtime_error(msg.str());
+ }
+
+ config_l->get("glyph-border", border);
+ config_l->get("rtl", rtl);