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