+};
+
+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,
+ const std::string& shadowfile,
+ int char_width, int char_height_,
+ int shadowsize_)
+ : glyph_width(glyph_width_),
+ glyph_surface(0), shadow_glyph_surface(0),
+ char_height(char_height_),
+ shadowsize(shadowsize_)
+{
+ glyph_surface = new Surface(filename);
+ shadow_glyph_surface = new Surface(shadowfile);
+
+ first_char = 32;
+ char_count = ((int) glyph_surface->get_height() / char_height) * 16;
+
+ if (glyph_width == FIXED)
+ {
+ for(uint32_t i = 0; i < char_count; ++i)
+ {
+ float x = (i % 16) * char_width;
+ float y = (i / 16) * char_height;
+
+ Glyph glyph;
+ glyph.advance = char_width;
+ glyph.offset = Vector(0, 0);
+ glyph.rect = Rect(x, y, x + char_width, y + char_height);
+
+ glyphs.push_back(glyph);
+ shadow_glyphs.push_back(glyph);
+ }
+ }
+ else // glyph_width == VARIABLE
+ {
+ // Load the surface into RAM and scan the pixel data for characters
+ SDL_Surface* surface = IMG_Load_RW(get_physfs_SDLRWops(filename), 1);
+ if(surface == NULL) {
+ std::ostringstream msg;
+ msg << "Couldn't load image '" << filename << "' :" << SDL_GetError();
+ throw std::runtime_error(msg.str());
+ }
+
+ SDL_LockSurface(surface);
+
+ for(uint32_t i = 0; i < char_count; ++i)
+ {
+ int x = (i % 16) * char_width;
+ int y = (i / 16) * char_height;
+
+ int left = x;
+ while (left < x + char_width &&
+ vline_empty(surface, left, y, y + char_height, 64))
+ left += 1;
+
+ int right = x + char_width - 1;
+ while (right > left &&
+ vline_empty(surface, right, y, y + char_height, 64))
+ right -= 1;
+
+ Glyph glyph;
+ glyph.offset = Vector(0, 0);
+
+ if (left <= right)
+ glyph.rect = Rect(left, y, right+1, y + char_height);
+ else // glyph is completly transparent
+ glyph.rect = Rect(x, y, x + char_width, y + char_height);
+
+ glyph.advance = glyph.rect.get_width() + 1; // FIXME: might be usefull to make spacing configurable
+
+ glyphs.push_back(glyph);
+ shadow_glyphs.push_back(glyph);
+ }
+
+ SDL_UnlockSurface(surface);
+
+ SDL_FreeSurface(surface);
+ }