]> git.tdb.fi Git - libs/gl.git/blob - source/font.cpp
Add files
[libs/gl.git] / source / font.cpp
1 #include <GL/gl.h>
2 #include "font.h"
3 #include "texture2d.h"
4
5 using namespace std;
6
7 namespace Msp {
8 namespace GL {
9
10 Font::Font():
11         tex(0),
12         own_tex(false)
13 { }
14
15 void Font::set_texture(const Texture2D &t)
16 {
17         tex=&t;
18 }
19
20 void Font::add_glyph(wchar_t code, float x1, float y1, float x2, float y2, float w, float h, float desc, float adv)
21 {
22         Glyph glyph;
23         glyph.code=code;
24         glyph.x1=x1;
25         glyph.y1=y1;
26         glyph.x2=x2;
27         glyph.y2=y2;
28         glyph.w=w;
29         glyph.h=h;
30         glyph.descent=desc;
31         glyph.advance=adv;
32 }
33
34 float Font::get_string_width(const string &str) const
35 {
36         float x=0;
37
38         for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
39         {
40                 GlyphMap::const_iterator j=glyphs.find((unsigned char)*i);
41                 if(j==glyphs.end())
42                         continue;
43
44                 const Glyph &glyph=j->second;
45
46                 x+=glyph.advance;
47         }
48
49         return x;
50 }
51
52 void Font::draw_string(const string &str) const
53 {
54         float x=0;
55         float data[16];
56
57         prepare(data);
58
59         for(string::const_iterator i=str.begin(); i!=str.end(); ++i)
60                 draw_glyph((unsigned char)*i, data, x);
61
62         glDisableClientState(GL_TEXTURE_COORD_ARRAY);
63 }
64
65 Font::~Font()
66 {
67         if(own_tex)
68                 delete tex;
69 }
70
71 void Font::prepare(float *data) const
72 {
73         tex->bind();
74
75         glEnableClientState(GL_TEXTURE_COORD_ARRAY);
76         glEnableClientState(GL_VERTEX_ARRAY);
77         glTexCoordPointer(2, GL_FLOAT, 4*sizeof(float), data);
78         glVertexPointer(2, GL_FLOAT, 4*sizeof(float), data+2);
79 }
80
81 void Font::draw_glyph(wchar_t code, float *data, float &x) const
82 {
83         GlyphMap::const_iterator i=glyphs.find(code);
84         if(i==glyphs.end())
85                 return;
86
87         const Glyph &glyph=i->second;
88
89         data[0]=data[12]=glyph.x1;
90         data[1]=data[5]=glyph.y1;
91         data[4]=data[8]=glyph.x2;
92         data[9]=data[13]=glyph.y2;
93
94         data[2]=data[14]=x;
95         data[3]=data[7]=glyph.descent;
96         data[6]=data[10]=x+glyph.w;
97         data[11]=data[15]=glyph.h+glyph.descent;
98
99         glDrawArrays(GL_QUADS, 0, 4);
100
101         x+=glyph.advance;
102 }
103
104 Font::Loader::Loader(Font &f):
105         font(f)
106 {
107         add("texture", &Loader::texture);
108         add("glyph",   &Loader::glyph);
109 }
110
111 void Font::Loader::texture(const string &t)
112 {
113         Texture2D *tex=new Texture2D;
114         tex->parameter(GL_GENERATE_MIPMAP, 1);
115         font.tex=tex;
116         tex->image(t);
117         font.own_tex=true;
118 }
119
120 void Font::Loader::glyph(unsigned c)
121 {
122         Glyph gl;
123         gl.code=c;
124         load_sub(gl);
125         font.glyphs.insert(GlyphMap::value_type(c, gl));
126 }
127
128 Font::Glyph::Loader::Loader(Glyph &g):
129         glyph(g)
130 {
131         add("texcoords", &Loader::texcoords);
132         add("size",      &Loader::size);
133         add("descent",   &Glyph::descent);
134         add("advance",   &Glyph::advance);
135 }
136
137 void Font::Glyph::Loader::texcoords(float x1, float y1, float x2, float y2)
138 {
139         glyph.x1=x1;
140         glyph.y1=y1;
141         glyph.x2=x2;
142         glyph.y2=y2;
143 }
144
145 void Font::Glyph::Loader::size(float w, float h)
146 {
147         glyph.w=w;
148         glyph.h=h;
149 }
150
151 } // namespace GL
152 } // namespace Msp