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