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