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