Main Page | Namespace List | Class Hierarchy | Class List | File List | Namespace Members | Class Members | Related Pages

font.h

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 Font 00049 { 00050 public: 00051 00053 class BadFont : public std::exception {}; 00054 00059 Font(const std::string& fontfilename, unsigned ptsize = 12, 00060 const Color& color = textColor(), unsigned face_index = 0) throw(BadFont); 00062 Font(const unsigned char* buffer, unsigned buf_size, unsigned ptsize = 12, 00063 const Color& color = textColor(), unsigned face_index = 0) throw(BadFont); 00064 00066 Font(const Font& f) : glyphs_(f.glyphs_) {if(glyphs_) glyphs_->ref();} 00068 Font() : glyphs_(0) {} 00069 00071 ~Font() {if(glyphs_) glyphs_->unref();} 00073 Font& operator=(const Font& f); 00074 00076 friend class FontData; 00077 00079 class Glyph : public Surface 00080 { 00081 public: 00083 void set(FontData&, const Color&, unsigned char); 00084 00086 struct Metrics 00087 { 00088 long width; 00089 long height; 00090 00091 long horiBearingX; 00092 long horiBearingY; 00093 long horiAdvance; 00094 00095 long vertBearingX; 00096 long vertBearingY; 00097 long vertAdvance; 00098 00099 long linearHoriAdvance; 00100 long linearVertAdvance; 00101 Point advance; 00102 00103 int bitmap_left; 00104 int bitmap_top; 00105 }; 00106 00108 const Metrics& metrics() const {return metrics_;} 00109 00110 private: 00111 friend class Font; 00112 // turns off SDL_SRCALPHA temporarily, since alpha->alpha blits don't do blending 00113 void copy(Surface& target, const Point& dest, const Region& destmask) const; 00114 00115 Metrics metrics_; 00116 }; 00117 00121 const Glyph &getChar(unsigned char c) const 00122 {return glyphs_ ? (*glyphs_)[c] : bad_glyph_;} 00123 00127 Surface *getString(const std::string & txt) const 00128 {Point p; return getString(txt, p);} 00130 Surface *getString(const std::string&, Point&) const; 00132 int blitString(const std::string& txt, Surface& target, const Point& pos) const 00133 {return blitString(txt, target, pos, target.rect());} 00135 int blitString(const std::string& txt, Surface& target, const Point& pos, const Region& mask, bool copy = false) const; 00137 Rect getExtents(const std::string&) const; 00138 00140 struct Metrics 00141 { 00142 unsigned short units_per_EM; 00143 short ascender; 00144 short descender; 00145 short height; 00146 00147 short max_advance_width; 00148 short max_advance_height; 00149 00150 short underline_position; 00151 short underline_thickness; 00152 00153 long xMin, xMax, yMin, yMax; 00154 }; 00155 00157 int getHeight() const {return glyphs_ ? glyphs_->metrics().height / 64: -1;} 00158 00160 const Metrics& metrics() const {return glyphs_ ? glyphs_->metrics() : bad_metrics_;} 00161 00163 bool valid() const {return glyphs_ != 0;} 00164 00166 Color color() const {return glyphs_? glyphs_->color() : Color();} 00168 void setColor(const Color&); 00169 00171 static const Font& textFont(); 00173 static const Color& textColor(); 00174 00175 struct ResLoad { 00176 std::pair<Font,bool> operator()(const std::string&); 00177 }; 00178 struct ResInval { 00179 typedef const Font& OutType; 00180 OutType operator()(const std::string&) const {return textFont();} 00181 }; 00204 static ResourceRegistry<Font,ResLoad,ResInval> registry; 00206 typedef Resource<Font> Resource; 00207 00208 private: 00212 class SurfaceTable 00213 { 00214 public: 00215 SurfaceTable(FontData& font, const Color& color) : font_(font), color_(color) {} 00216 00217 const Color& color() const {return color_;} 00218 00219 const Metrics& metrics() const; 00220 00221 const Glyph& operator[](unsigned char); 00222 00223 const FontData& font() const {return font_;} 00224 00225 void ref(); 00226 void unref(); 00227 // reference another surface table for the same font, with a different color 00228 SurfaceTable* ref(const Color&); 00229 00230 private: 00231 // unimplemented 00232 SurfaceTable(const SurfaceTable&); 00233 SurfaceTable& operator=(const SurfaceTable&); 00234 00235 FontData& font_; 00236 Color color_; 00237 Glyph glyphs_[256]; 00238 } *glyphs_; 00239 00240 class MapType : public RefMap<Color, SurfaceTable> { 00241 public: 00242 MapType(FontData& font) : font_(font) {} 00243 00244 private: 00245 // create the entries in the RefMap 00246 virtual Font::SurfaceTable* create(const Color& color) 00247 {return new Font::SurfaceTable(font_, color);} 00248 00249 FontData& font_; 00250 00251 }; 00252 00253 // for when there's no font loaded 00254 static Glyph bad_glyph_; 00255 static Metrics bad_metrics_; 00256 }; 00257 00258 } // namespace wftk 00259 00260 #endif // _FONT_H

Generated Mon Sep 6 21:58:16 2004.
Copyright © 1998-2003 by the respective authors.

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.