00001 /* 00002 libwftk - Worldforge Toolkit - a widget library 00003 Copyright (C) 2002 Malcolm Walker <malcolm@worldforge.org> 00004 Based on code copyright (C) 1999-2002 Karsten Laux 00005 00006 This library is free software; you can redistribute it and/or 00007 modify it under the terms of the GNU Lesser General Public 00008 License as published by the Free Software Foundation; either 00009 version 2.1 of the License, or (at your option) any later version. 00010 00011 This library is distributed in the hope that it will be useful, 00012 but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00014 Lesser General Public License for more details. 00015 00016 You should have received a copy of the GNU Lesser General Public 00017 License along with this library; if not, write to the 00018 Free Software Foundation, Inc., 59 Temple Place - Suite 330, 00019 Boston, MA 02111-1307, SA. 00020 */ 00021 00022 #ifndef _FONT_H 00023 #define _FONT_H 00024 00025 #include <string> 00026 #include <exception> 00027 00028 #include <wftk/surface.h> 00029 #include <wftk/color.h> 00030 #include <wftk/point.h> 00031 #include <wftk/resources.h> 00032 #include <wftk/region.h> 00033 00034 // pity we have to export this 00035 #include <wftk/ref_map.h> 00036 00037 namespace wftk { 00038 00048 class FontData; 00049 00050 class Font 00051 { 00052 public: 00053 00055 class BadFont : public std::exception {}; 00056 00061 Font(const std::string& fontfilename, unsigned ptsize = 12, 00062 const Color& color = textColor(), unsigned face_index = 0) throw(BadFont); 00064 Font(const unsigned char* buffer, unsigned buf_size, unsigned ptsize = 12, 00065 const Color& color = textColor(), unsigned face_index = 0) throw(BadFont); 00066 00068 Font(const Font& f) : glyphs_(f.glyphs_) {if(glyphs_) glyphs_->ref();} 00070 Font() : glyphs_(0) {} 00071 00073 ~Font() {if(glyphs_) glyphs_->unref();} 00075 Font& operator=(const Font& f); 00076 00078 friend class FontData; 00079 00081 class Glyph : public Surface 00082 { 00083 public: 00085 void set(FontData&, const Color&, unsigned char); 00086 00088 struct Metrics 00089 { 00090 long width; 00091 long height; 00092 00093 long horiBearingX; 00094 long horiBearingY; 00095 long horiAdvance; 00096 00097 long vertBearingX; 00098 long vertBearingY; 00099 long vertAdvance; 00100 00101 long linearHoriAdvance; 00102 long linearVertAdvance; 00103 Point advance; 00104 00105 int bitmap_left; 00106 int bitmap_top; 00107 }; 00108 00110 const Metrics& metrics() const {return metrics_;} 00111 00112 private: 00113 friend class Font; 00114 // turns off SDL_SRCALPHA temporarily, since alpha->alpha blits don't do blending 00115 void copy(Surface& target, const Point& dest, const Region& destmask) const; 00116 00117 Metrics metrics_; 00118 }; 00119 00123 const Glyph &getChar(unsigned char c) const 00124 {return glyphs_ ? (*glyphs_)[c] : bad_glyph_;} 00125 00129 Surface *getString(const std::string & txt) const 00130 {Point p; return getString(txt, p);} 00132 Surface *getString(const std::string&, Point&) const; 00134 int blitString(const std::string& txt, Surface& target, const Point& pos) const 00135 {return blitString(txt, target, pos, target.rect());} 00137 int blitString(const std::string& txt, Surface& target, const Point& pos, const Region& mask, bool copy = false) const; 00139 Rect getExtents(const std::string&) const; 00140 00142 struct Metrics 00143 { 00144 unsigned short units_per_EM; 00145 short ascender; 00146 short descender; 00147 short height; 00148 00149 short max_advance_width; 00150 short max_advance_height; 00151 00152 short underline_position; 00153 short underline_thickness; 00154 00155 long xMin, xMax, yMin, yMax; 00156 }; 00157 00159 int getHeight() const {return glyphs_ ? glyphs_->metrics().height / 64: -1;} 00160 00162 const Metrics& metrics() const {return glyphs_ ? glyphs_->metrics() : bad_metrics_;} 00163 00165 bool valid() const {return glyphs_ != 0;} 00166 00168 Color color() const {return glyphs_? glyphs_->color() : Color();} 00170 void setColor(const Color&); 00171 00173 static const Font& textFont(); 00175 static const Color& textColor(); 00176 00177 struct ResLoad { 00178 std::pair<Font,bool> operator()(const std::string&); 00179 }; 00180 struct ResInval { 00181 typedef const Font& OutType; 00182 OutType operator()(const std::string&) const {return textFont();} 00183 }; 00206 static ResourceRegistry<Font,ResLoad,ResInval> registry; 00208 typedef Resource<Font> Resource; 00209 00210 private: 00214 class SurfaceTable 00215 { 00216 public: 00217 SurfaceTable(FontData& font, const Color& color) : font_(font), color_(color) {} 00218 00219 const Color& color() const {return color_;} 00220 00221 const Metrics& metrics() const; 00222 00223 const Glyph& operator[](unsigned char); 00224 00225 const FontData& font() const {return font_;} 00226 00227 void ref(); 00228 void unref(); 00229 // reference another surface table for the same font, with a different color 00230 SurfaceTable* ref(const Color&); 00231 00232 private: 00233 // unimplemented 00234 SurfaceTable(const SurfaceTable&); 00235 SurfaceTable& operator=(const SurfaceTable&); 00236 00237 FontData& font_; 00238 Color color_; 00239 Glyph glyphs_[256]; 00240 } *glyphs_; 00241 00242 class MapType : public RefMap<Color, SurfaceTable> { 00243 public: 00244 MapType(FontData& font, bool autodelete = true) : 00245 RefMap<Color, SurfaceTable>(autodelete), font_(font) {} 00246 00247 private: 00248 // create the entries in the RefMap 00249 virtual Font::SurfaceTable* create(const Color& color) 00250 {return new Font::SurfaceTable(font_, color);} 00251 00252 FontData& font_; 00253 00254 }; 00255 00256 // for when there's no font loaded 00257 static Glyph bad_glyph_; 00258 static Metrics bad_metrics_; 00259 }; 00260 00261 } // namespace wftk 00262 00263 #endif // _FONT_H
This document is licensed under the terms of the GNU Free Documentation License and may be freely distributed under the conditions given by this license.