6 #include <msp/datafile/objectloader.h>
7 #include <msp/stringcodec/utf8.h>
8 #include "vertexarray.h"
13 class PrimitiveBuilder;
17 Stores a set of glyphs and creates strings out of them.
22 class Loader: public DataFile::CollectionObjectLoader<Font>
26 Loader(Font &, Collection &);
31 void kerning(unsigned, unsigned, float);
33 void texture_ref(const std::string &);
38 class Loader: public Msp::DataFile::ObjectLoader<Glyph>
43 void texcoords(float, float, float, float);
55 typedef std::map<unsigned, Glyph> GlyphMap;
56 typedef std::pair<unsigned, unsigned> KerningKey;
57 typedef std::map<KerningKey, float> KerningMap;
59 RefPtr<const Texture2D> texture;
72 void set_texture(const Texture2D &);
73 const Texture2D &get_texture() const;
75 /** Adds a glyph to the font. There must not be an existing glyph with the
77 void add_glyph(const Glyph &);
78 void set_kerning(unsigned, unsigned, float);
80 /** Returns the size used to generate the font texture. This serves as a
81 hint for obtaining the best quality when rendering strings. */
82 float get_native_size() const { return native_size; }
84 float get_ascent() const { return ascent; }
85 float get_descent() const { return descent; }
86 float get_cap_height() const { return cap_height; }
87 float get_x_height() const { return x_height; }
89 /** Returns the width of a string, in multiples of the font size. Scale the
90 result according to the size used in rendering. */
91 float get_string_width(const std::string &, StringCodec::Decoder &) const;
94 float get_string_width(const std::string &str) const
96 typename C::Decoder dec;
97 return get_string_width(str, dec);
100 float get_string_width(const std::string &str) const
101 { return get_string_width<StringCodec::Utf8>(str); }
103 /** Draws a string to the framebuffer with Immediate. It is drawn with size
104 1.0; set up matrices for the desired size before the call. */
105 void draw_string(const std::string &, StringCodec::Decoder &, const Color & = Color()) const;
108 void draw_string(const std::string &str, const Color &color = Color()) const
110 typename C::Decoder dec;
111 draw_string(str, dec, color);
114 void draw_string(const std::string &str, const Color &color = Color()) const
115 { draw_string<StringCodec::Utf8>(str, color); }
117 /** Builds the primitives for a string. Two-dimensional vertex and texture
118 coordinates are generated. Size 1.0 is used for building; set up the
119 builder's matrix before the call. The texture is not bound, to avoid
120 unnecessary bindings when creating meshes. */
121 void build_string(const std::string &, StringCodec::Decoder &, PrimitiveBuilder &) const;
124 void build_string(const std::string &str, PrimitiveBuilder &pb) const
126 typename C::Decoder dec;
127 build_string(str, dec, pb);
130 void build_string(const std::string &str, PrimitiveBuilder &pb) const
131 { return build_string<StringCodec::Utf8>(str, pb); }
134 void create_glyph_quad(const Glyph &, PrimitiveBuilder &) const;
135 float get_glyph_advance(unsigned, unsigned = 0) const;