00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
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