]> git.tdb.fi Git - libs/gl.git/blob - source/builders/font.cpp
1ede612a1dec64e9ab2e671a5a9398fc44e8f6dc
[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, Collection *c):
147         DataFile::CollectionObjectLoader<Font>(f, c)
148 {
149         add("native_size", &Font::native_size);
150         add("ascent",      &Font::ascent);
151         add("cap_height",  &Font::cap_height);
152         add("descent",     &Font::descent);
153         add("texture",     &Loader::texture);
154         add("texture",     &Loader::texture_ref);
155         add("glyph",       &Loader::glyph);
156         add("kerning",     &Loader::kerning);
157         add("ligature",    &Loader::ligature);
158         add("x_height",    &Font::x_height);
159 }
160
161 void Font::Loader::glyph(unsigned c)
162 {
163         Glyph gl;
164         gl.code = c;
165         load_sub(gl);
166         obj.glyphs.insert(GlyphMap::value_type(c, gl));
167 }
168
169 void Font::Loader::kerning(unsigned l, unsigned r, float d)
170 {
171         obj.kerning[CodePair(l, r)] = d;
172 }
173
174 void Font::Loader::ligature(unsigned l, unsigned r, unsigned g)
175 {
176         obj.ligatures[CodePair(l, r)] = g;
177 }
178
179 void Font::Loader::texture()
180 {
181         RefPtr<Texture2D> tex = new Texture2D;
182         load_sub(*tex);
183         get_collection().add(FS::basename(get_source())+".tex2d", tex.get());
184         obj.texture = tex.release();
185 }
186
187 void Font::Loader::texture_ref(const string &name)
188 {
189         obj.texture = &get_collection().get<Texture2D>(name);
190 }
191
192
193 Font::Glyph::Loader::Loader(Glyph &g):
194         DataFile::ObjectLoader<Glyph>(g)
195 {
196         add("texcoords", &Loader::texcoords);
197         add("size",      &Glyph::w,     &Glyph::h);
198         add("offset",    &Glyph::off_x, &Glyph::off_y);
199         add("advance",   &Glyph::advance);
200 }
201
202 void Font::Glyph::Loader::texcoords(float x1_, float y1_, float x2_, float y2_)
203 {
204         obj.x1 = x1_;
205         obj.y1 = y1_;
206         obj.x2 = x2_;
207         obj.y2 = y2_;
208 }
209
210 } // namespace GL
211 } // namespace Msp