//====================================================================== // Copyright (C) 2002 Daniel Heck // // This program 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 (at your option) any later version. // // This program 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 this program; if not, write to the Free Software Foundation, Inc., // 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. //====================================================================== #include "font.hh" #include "geom.hh" #include "tools.hh" #include "video.hh" #include #include #include using namespace px; using namespace std; // // Bitmap fonts // namespace { class InvalidFont {}; class BitmapFont : public Font { vector char_rects; vector advance; Surface *surface; public: BitmapFont(Surface *s, const char *descr) throw(InvalidFont); ~BitmapFont() { delete surface; } int get_lineskip() { return surface->height() + 3; } int get_width(char c); int get_width(const char *str); int get_height(); Surface *render(const char *str); void render(const GC &gc, int x, int y, const char *str); }; } BitmapFont::BitmapFont(Surface *s, const char *descr) throw(InvalidFont) : char_rects(256), advance(256), surface(s) { // Assert(surface != 0); // Read and interpret the font description file. // expected line format: // charno xpos width xadvance FILE *fp=fopen(descr, "rt"); if (!fp) return ; //throw InvalidFont(); int c; int x=0, w=0, adv=0; while (fscanf(fp, "%d %d %d %d\n", &c, &x, &w, &adv) != EOF) { char_rects[c].x = x; char_rects[c].w = w; char_rects[c].y = 0; char_rects[c].h = s->height(); advance[c] = adv; } } int BitmapFont::get_width(char c) { return advance[int(c)]; } int BitmapFont::get_width(const char *str) { int w=0; for (const char *p = str; *p; ++p) w += get_width(*p); return w; } int BitmapFont::get_height() { return surface->height(); } Surface * BitmapFont::render(const char *str) { Surface *s = MakeSurface(get_width(str), get_height(), 16); s->set_color_key(0,0,0); render (GC(s), 0, 0, str); return s; } void BitmapFont::render(const GC &gc, int x, int y, const char *str) { for (const char *p=str; *p; ++p) { blit(gc, x, y, surface, char_rects[int(*p)]); x += get_width(*p); } } Font * px::LoadBitmapFont(const char * imgname, const char * descrname) { if (Surface *s = LoadImage(imgname)) return new BitmapFont(s, descrname); return 0; } // // TrueType fonts (using SDL_ttf if available) // namespace { class TTF_Font : public Font { public: }; }