src/video/font.hpp

Go to the documentation of this file.
00001 //  SuperTux
00002 //  Copyright (C) 2006 Matthias Braun <matze@braunis.de>,
00003 //                     Ingo Ruhnke <grumbel@gmx.de>
00004 //
00005 //  This program is free software: you can redistribute it and/or modify
00006 //  it under the terms of the GNU General Public License as published by
00007 //  the Free Software Foundation, either version 3 of the License, or
00008 //  (at your option) any later version.
00009 //
00010 //  This program is distributed in the hope that it will be useful,
00011 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 //  GNU General Public License for more details.
00014 //
00015 //  You should have received a copy of the GNU General Public License
00016 //  along with this program.  If not, see <http://www.gnu.org/licenses/>.
00017 
00018 #ifndef HEADER_SUPERTUX_VIDEO_FONT_HPP
00019 #define HEADER_SUPERTUX_VIDEO_FONT_HPP
00020 
00021 #include <stdint.h>
00022 #include <string>
00023 
00024 #include "math/rectf.hpp"
00025 #include "math/vector.hpp"
00026 #include "video/color.hpp"
00027 #include "video/surface.hpp"
00028 #include "video/texture.hpp"
00029 
00030 class Renderer;
00031 
00032 enum FontAlignment {
00033   ALIGN_LEFT,
00034   ALIGN_CENTER,
00035   ALIGN_RIGHT
00036 };
00037 
00038 class Font
00039 {
00040 public:
00041   enum GlyphWidth {
00042     FIXED,
00043     VARIABLE
00044   };
00045 
00046 public:
00053   Font(GlyphWidth glyph_width, const std::string& fontfile, int shadowsize = 2);
00054   ~Font();
00055 
00061   float get_text_width(const std::string& text) const;
00062 
00067   float get_text_height(const std::string& text) const;
00068 
00072   float get_height() const;
00073 
00077   static std::string wrap_to_chars(const std::string& text, int max_chars, std::string* overflow);
00078 
00082   std::string wrap_to_width(const std::string& text, float width, std::string* overflow);
00083 
00086   void draw(Renderer *renderer, const std::string& text, const Vector& pos,
00087             FontAlignment alignment = ALIGN_LEFT,
00088             DrawingEffect drawing_effect = NO_EFFECT,
00089             Color color = Color(1.0,1.0,1.0),
00090             float alpha = 1.0f) const;
00091 
00092 private:
00093   friend class DrawingContext;
00094 
00095   void draw_text(Renderer *renderer, const std::string& text, const Vector& pos,
00096                  DrawingEffect drawing_effect = NO_EFFECT,
00097                  Color color = Color(1.0,1.0,1.0),
00098                  float alpha = 1.0f) const;
00099 
00100   void draw_chars(Renderer *renderer, bool nonshadow, const std::string& text,
00101                   const Vector& position, DrawingEffect drawing_effect, Color color,
00102                   float alpha) const;
00103 
00104   void loadFontFile(const std::string &filename);
00105   void loadFontSurface(const std::string &glyphimage,
00106                        const std::string &shadowimage,
00107                        const std::vector<std::string> &chars,
00108                        GlyphWidth glyph_width,
00109                        int char_width);
00110 private:
00111   struct Glyph {
00114     float advance;
00115 
00117     Vector offset;
00118 
00120     int surface_idx;
00121 
00123     Rectf rect;
00124 
00125     Glyph() :
00126       advance(),
00127       offset(),
00128       surface_idx(),
00129       rect()
00130     {}
00131   };
00132 
00133 private:
00134   GlyphWidth glyph_width;
00135 
00136   std::vector<SurfacePtr>  glyph_surfaces;
00137   std::vector<SurfacePtr>  shadow_surfaces;
00138   int char_height;
00139   int shadowsize;
00140 
00142   std::vector<Glyph> glyphs;
00143 };
00144 
00145 #endif
00146 
00147 /* EOF */

Generated on Mon Jun 9 03:38:24 2014 for SuperTux by  doxygen 1.5.1