]> git.tdb.fi Git - libs/gl.git/blob - source/font.cpp
Use a RefPtr to store font texture
[libs/gl.git] / source / font.cpp
1 /* $Id$
2
3 This file is part of libmspgl
4 Copyright © 2007  Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
6 */
7
8 #include <msp/datafile/collection.h>
9 #include "bindable.h"
10 #include "gl.h"
11 #include "font.h"
12 #include "immediate.h"
13 #include "primitivetype.h"
14 #include "texture2d.h"
15
16 using namespace std;
17
18 namespace Msp {
19 namespace GL {
20
21 Font::Font():
22         native_size(1),
23         ascent(1),
24         descent(0)
25 { }
26
27 void Font::set_texture(const Texture2D &t)
28 {
29         texture = &t;
30         texture.keep();
31 }
32
33 const Texture2D &Font::get_texture() const
34 {
35         if(!texture)
36                 throw InvalidState("No texture");
37         return *texture;
38 }
39
40 void Font::add_glyph(unsigned code, float x1, float y1, float x2, float y2, float w, float h, float ox, float oy, float adv)
41 {
42         Glyph glyph;
43         glyph.code = code;
44         glyph.x1 = x1;
45         glyph.y1 = y1;
46         glyph.x2 = x2;
47         glyph.y2 = y2;
48         glyph.w = w;
49         glyph.h = h;
50         glyph.off_x = ox;
51         glyph.off_y = oy;
52         glyph.advance = adv;
53         glyphs.insert(GlyphMap::value_type(code, glyph));
54 }
55
56 float Font::get_string_width(const string &str, Codecs::Decoder &dec) const
57 {
58         float x = 0;
59
60         for(string::const_iterator i=str.begin(); i!=str.end();)
61                 x += get_glyph_advance(dec.decode_char(str, i));
62
63         return x;
64 }
65
66 void Font::draw_string(const string &str, Codecs::Decoder &dec) const
67 {
68         Immediate imm((TEXCOORD2, COLOR4_UBYTE, VERTEX2));
69         draw_string(str, dec, imm);
70 }
71
72 void Font::draw_string(const string &str, Codecs::Decoder &dec, PrimitiveBuilder &pbuilder) const
73 {
74         Bind bind_tex(get_texture(), true);
75
76         float x = 0;
77         unsigned count = 0;
78
79         pbuilder.begin(QUADS);
80         for(string::const_iterator i=str.begin(); i!=str.end();)
81         {
82                 GlyphMap::const_iterator j = glyphs.find(dec.decode_char(str, i));
83                 if(j==glyphs.end())
84                         continue;
85
86                 create_glyph_vertices(j->second, pbuilder, x);
87                 x += j->second.advance;
88                 count += 4;
89         }
90         pbuilder.end();
91 }
92
93 void Font::create_glyph_vertices(const Glyph &glyph, VertexBuilder &vbuilder, float x) const
94 {
95         vbuilder.texcoord(glyph.x1, glyph.y1);
96         vbuilder.vertex(x+glyph.off_x, glyph.off_y);
97         vbuilder.texcoord(glyph.x2, glyph.y1);
98         vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y);
99         vbuilder.texcoord(glyph.x2, glyph.y2);
100         vbuilder.vertex(x+glyph.off_x+glyph.w, glyph.off_y+glyph.h);
101         vbuilder.texcoord(glyph.x1, glyph.y2);
102         vbuilder.vertex(x+glyph.off_x, glyph.off_y+glyph.h);
103 }
104
105 float Font::get_glyph_advance(unsigned code) const
106 {
107         GlyphMap::const_iterator i = glyphs.find(code);
108         if(i==glyphs.end())
109                 return 0;
110
111         return i->second.advance;
112 }
113
114
115 Font::Loader::Loader(Font &f):
116         DataFile::CollectionObjectLoader<Font>(f, 0)
117 {
118         init();
119 }
120
121 Font::Loader::Loader(Font &f, Collection &c):
122         DataFile::CollectionObjectLoader<Font>(f, &c)
123 {
124         init();
125 }
126
127 void Font::Loader::init()
128 {
129         add("native_size", &Font::native_size);
130         add("ascent",      &Font::ascent);
131         add("descent",     &Font::descent);
132         add("texture",     &Loader::texture);
133         add("texture",     &Loader::texture_ref);
134         add("glyph",       &Loader::glyph);
135
136         // Deprecated aliases
137         add("default_size",   &Font::native_size);
138         add("texture_inline", &Loader::texture);
139 }
140
141 void Font::Loader::glyph(unsigned c)
142 {
143         Glyph gl;
144         gl.code = c;
145         load_sub(gl);
146         obj.glyphs.insert(GlyphMap::value_type(c, gl));
147 }
148
149 void Font::Loader::texture()
150 {
151         RefPtr<Texture2D> tex = new Texture2D;
152         load_sub(*tex);
153         obj.texture = tex;
154 }
155
156 void Font::Loader::texture_ref(const string &name)
157 {
158         obj.texture = get_collection().get<Texture2D>(name);
159         obj.texture.keep();
160 }
161
162
163 Font::Glyph::Loader::Loader(Glyph &g):
164         DataFile::ObjectLoader<Glyph>(g)
165 {
166         add("texcoords", &Loader::texcoords);
167         add("size",      &Glyph::w,     &Glyph::h);
168         add("offset",    &Glyph::off_x, &Glyph::off_y);
169         add("advance",   &Glyph::advance);
170 }
171
172 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)
173 {
174         obj.x1 = x1;
175         obj.y1 = y1;
176         obj.x2 = x2;
177         obj.y2 = y2;
178 }
179
180 } // namespace GL
181 } // namespace Msp