]> git.tdb.fi Git - libs/gl.git/blob - source/builders/font.cpp
Use a placeholder texture when a texture has not been loaded yet
[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 void Font::set_texture(const Texture2D &t)
14 {
15         texture = &t;
16 }
17
18 const Texture2D &Font::get_texture() const
19 {
20         if(!texture)
21                 throw logic_error("No texture");
22         return *texture;
23 }
24
25 void Font::add_glyph(const Glyph &g)
26 {
27         insert_unique(glyphs, g.code, g);
28 }
29
30 void Font::set_kerning(unsigned l, unsigned r, float d)
31 {
32         kerning[CodePair(l, r)] = d;
33 }
34
35 float Font::get_string_width(const string &str, StringCodec::Decoder &dec) const
36 {
37         float x = 0;
38
39         unsigned prev = 0;
40         for(auto i=str.begin(); i!=str.end();)
41         {
42                 unsigned c = dec.decode_char(str, i);
43                 if(prev)
44                         x += get_glyph_advance(prev, c);
45                 prev = c;
46         }
47         x += get_glyph_advance(prev);
48
49         return x;
50 }
51
52 void Font::build_string(const string &str, StringCodec::Decoder &dec, PrimitiveBuilder &bld) const
53 {
54         VertexBuilder::PushMatrix push_mtx(bld);
55
56         unsigned prev = 0;
57         unsigned next = 0;
58         for(auto i=str.begin(); (next || i!=str.end());)
59         {
60                 unsigned c = (next ? next : dec.decode_char(str, i));
61                 next = (i!=str.end() ? dec.decode_char(str, i) : 0);
62
63                 if(unsigned lig = get_ligature(c, next))
64                 {
65                         c = lig;
66                         next = 0;
67                 }
68
69                 auto j = glyphs.find(c);
70                 if(j==glyphs.end())
71                         continue;
72
73                 if(prev)
74                         bld.transform(Matrix::translation(get_glyph_advance(prev, c), 0, 0));
75
76                 create_glyph_quad(j->second, bld);
77                 prev = c;
78         }
79 }
80
81 void Font::create_glyph_quad(const Glyph &glyph, PrimitiveBuilder &bld) const
82 {
83         bld.begin(TRIANGLE_STRIP);
84         bld.texcoord(glyph.x1, glyph.y2);
85         bld.vertex(glyph.off_x, glyph.off_y+glyph.h);
86         bld.texcoord(glyph.x1, glyph.y1);
87         bld.vertex(glyph.off_x, glyph.off_y);
88         bld.texcoord(glyph.x2, glyph.y2);
89         bld.vertex(glyph.off_x+glyph.w, glyph.off_y+glyph.h);
90         bld.texcoord(glyph.x2, glyph.y1);
91         bld.vertex(glyph.off_x+glyph.w, glyph.off_y);
92         bld.end();
93 }
94
95 float Font::get_glyph_advance(unsigned code, unsigned next) const
96 {
97         auto i = glyphs.find(code);
98         if(i==glyphs.end())
99                 return 0;
100
101         float advance = i->second.advance;
102
103         if(next)
104         {
105                 auto j = kerning.find(CodePair(code, next));
106                 if(j!=kerning.end())
107                         advance += j->second;
108         }
109
110         return advance;
111 }
112
113 unsigned Font::get_ligature(unsigned code, unsigned next) const
114 {
115         auto i = ligatures.find(CodePair(code, next));
116         return (i!=ligatures.end() ? i->second : 0);
117 }
118
119
120 Font::Loader::Loader(Font &f, Collection &c):
121         DataFile::CollectionObjectLoader<Font>(f, &c)
122 {
123         add("native_size", &Font::native_size);
124         add("ascent",      &Font::ascent);
125         add("cap_height",  &Font::cap_height);
126         add("descent",     &Font::descent);
127         add("texture",     &Loader::texture);
128         add("texture",     &Loader::texture_ref);
129         add("glyph",       &Loader::glyph);
130         add("kerning",     &Loader::kerning);
131         add("ligature",    &Loader::ligature);
132         add("x_height",    &Font::x_height);
133 }
134
135 void Font::Loader::glyph(unsigned c)
136 {
137         Glyph gl;
138         gl.code = c;
139         load_sub(gl);
140         obj.glyphs.insert(GlyphMap::value_type(c, gl));
141 }
142
143 void Font::Loader::kerning(unsigned l, unsigned r, float d)
144 {
145         obj.kerning[CodePair(l, r)] = d;
146 }
147
148 void Font::Loader::ligature(unsigned l, unsigned r, unsigned g)
149 {
150         obj.ligatures[CodePair(l, r)] = g;
151 }
152
153 void Font::Loader::texture()
154 {
155         RefPtr<Texture2D> tex = new Texture2D;
156         load_sub(*tex);
157         get_collection().add(FS::basename(get_source())+".tex", tex.get());
158         obj.texture = tex.release();
159 }
160
161 void Font::Loader::texture_ref(const string &name)
162 {
163         obj.texture = &get_collection().get<Texture2D>(name);
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