]> git.tdb.fi Git - libs/gl.git/blob - source/builders/font.cpp
Some more cleanup of includes and forward declarations
[libs/gl.git] / source / builders / font.cpp
1 #include <msp/core/maputils.h>
2 #include <msp/datafile/collection.h>
3 #include <msp/fs/utils.h>
4 #include "font.h"
5 #include "primitivebuilder.h"
6 #include "texture2d.h"
7
8 using namespace std;
9
10 namespace Msp {
11 namespace GL {
12
13 Font::Font():
14         native_size(1),
15         ascent(1),
16         descent(0),
17         cap_height(1),
18         x_height(0.5)
19 { }
20
21 // Avoid synthesizing ~RefPtr in files including font.h
22 Font::~Font()
23 { }
24
25 void Font::set_texture(const Texture2D &t)
26 {
27         texture = &t;
28 }
29
30 const Texture2D &Font::get_texture() const
31 {
32         if(!texture)
33                 throw logic_error("No texture");
34         return *texture;
35 }
36
37 void Font::add_glyph(const Glyph &g)
38 {
39         insert_unique(glyphs, g.code, g);
40 }
41
42 void Font::set_kerning(unsigned l, unsigned r, float d)
43 {
44         kerning[CodePair(l, r)] = d;
45 }
46
47 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
48 {
49         float x = 0;
50
51         unsigned prev = 0;
52         for(auto i=str.begin(); i!=str.end();)
53         {
54                 unsigned c = dec.decode_char(str, i);
55                 if(prev)
56                         x += get_glyph_advance(prev, c);
57                 prev = c;
58         }
59         x += get_glyph_advance(prev);
60
61         return x;
62 }
63
64 void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
65 {
66         VertexBuilder::PushMatrix push_mtx(bld);
67
68         unsigned prev = 0;
69         unsigned next = 0;
70         for(auto i=str.begin(); (next || i!=str.end());)
71         {
72                 unsigned c = (next ? next : dec.decode_char(str, i));
73                 next = (i!=str.end() ? dec.decode_char(str, i) : 0);
74
75                 if(unsigned lig = get_ligature(c, next))
76                 {
77                         c = lig;
78                         next = 0;
79                 }
80
81                 auto j = glyphs.find(c);
82                 if(j==glyphs.end())
83                         continue;
84
85                 if(prev)
86                         bld.transform(Matrix::translation(get_glyph_advance(prev, c), 0, 0));
87
88                 create_glyph_quad(j->second, bld);
89                 prev = c;
90         }
91 }
92
93 void Font::create_glyph_quad(const Glyph &glyph, PrimitiveBuilder &bld) const
94 {
95         bld.begin(TRIANGLE_STRIP);
96         bld.texcoord(glyph.x1, glyph.y2);
97         bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
98         bld.texcoord(glyph.x1, glyph.y1);
99         bld.vertex(glyph.off_x, glyph.off_y);
100         bld.texcoord(glyph.x2, glyph.y2);
101         bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
102         bld.texcoord(glyph.x2, glyph.y1);
103         bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
104         bld.end();
105 }
106
107 float Font::get_glyph_advance(unsigned code, unsigned next) const
108 {
109         auto i = glyphs.find(code);
110         if(i==glyphs.end())
111                 return 0;
112
113         float advance = i->second.advance;
114
115         if(next)
116         {
117                 auto j = kerning.find(CodePair(code, next));
118                 if(j!=kerning.end())
119                         advance += j->second;
120         }
121
122         return advance;
123 }
124
125 unsigned Font::get_ligature(unsigned code, unsigned next) const
126 {
127         auto i = ligatures.find(CodePair(code, next));
128         return (i!=ligatures.end() ? i->second : 0);
129 }
130
131
132 Font::Glyph::Glyph():
133         code(0),
134         x1(0),
135         y1(0),
136         x2(1),
137         y2(1),
138         w(1),
139         h(1),
140         off_x(0),
141         off_y(0),
142         advance(1)
143 { }
144
145
146 Font::Loader::Loader(Font &f):
147         DataFile::CollectionObjectLoader<Font>(f, 0)
148 {
149         init();
150 }
151
152 Font::Loader::Loader(Font &f, Collection &c):
153         DataFile::CollectionObjectLoader<Font>(f, &c)
154 {
155         init();
156 }
157
158 void Font::Loader::init()
159 {
160         add("native_size", &Font::native_size);
161         add("ascent",      &Font::ascent);
162         add("cap_height",  &Font::cap_height);
163         add("descent",     &Font::descent);
164         add("texture",     &Loader::texture);
165         add("texture",     &Loader::texture_ref);
166         add("glyph",       &Loader::glyph);
167         add("kerning",     &Loader::kerning);
168         add("ligature",    &Loader::ligature);
169         add("x_height",    &Font::x_height);
170 }
171
172 void Font::Loader::glyph(unsigned c)
173 {
174         Glyph gl;
175         gl.code = c;
176         load_sub(gl);
177         obj.glyphs.insert(GlyphMap::value_type(c, gl));
178 }
179
180 void Font::Loader::kerning(unsigned l, unsigned r, float d)
181 {
182         obj.kerning[CodePair(l, r)] = d;
183 }
184
185 void Font::Loader::ligature(unsigned l, unsigned r, unsigned g)
186 {
187         obj.ligatures[CodePair(l, r)] = g;
188 }
189
190 void Font::Loader::texture()
191 {
192         RefPtr<Texture2D> tex = new Texture2D;
193         load_sub(*tex);
194         get_collection().add(FS::basename(get_source())+".tex2d", tex.get());
195         obj.texture = tex.release();
196 }
197
198 void Font::Loader::texture_ref(const string &name)
199 {
200         obj.texture = &get_collection().get<Texture2D>(name);
201 }
202
203
204 Font::Glyph::Loader::Loader(Glyph &g):
205         DataFile::ObjectLoader<Glyph>(g)
206 {
207         add("texcoords", &Loader::texcoords);
208         add("size",      &Glyph::w,     &Glyph::h);
209         add("offset",    &Glyph::off_x, &Glyph::off_y);
210         add("advance",   &Glyph::advance);
211 }
212
213 void Font::Glyph::Loader::texcoords(float x1_, float y1_, float x2_, float y2_)
214 {
215         obj.x1 = x1_;
216         obj.y1 = y1_;
217         obj.x2 = x2_;
218         obj.y2 = y2_;
219 }
220
221 } // namespace GL
222 } // namespace Msp