-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathPolyGlyph.cpp
More file actions
241 lines (208 loc) · 6.49 KB
/
PolyGlyph.cpp
File metadata and controls
241 lines (208 loc) · 6.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
#include "PolyGlyph.h"
#include "assert.h"
#include <iostream>
//#include "Unicode.h"
PolyGlyph::PolyGlyph(const string &ttffilename)
{
FT_Error error;
error = FT_Init_FreeType(&m_Library);
error = FT_New_Face(m_Library, ttffilename.c_str(), 0, &m_Face);
if (error)
{
cerr<<"PolyGlyph::PolyGlyph: could not load font: "<<ttffilename<<endl;
assert(0);
}
// use 5pt at 100dpi
error = FT_Set_Char_Size(m_Face, 50 * 64, 0, 100, 0);
m_Slot = m_Face->glyph;
}
PolyGlyph::~PolyGlyph()
{
FT_Done_Face(m_Face);
FT_Done_FreeType(m_Library);
}
void PolyGlyph::ClearCache()
{
m_Cache.clear();
}
void PolyGlyph::Render(wchar_t ch, float r, float g, float b, float a,
float dx /* = 0 */, float dy /* = 0 */)
{
glPushMatrix();
glTranslatef(dx, dy, 0);
map<wchar_t,int>::iterator i = m_Cache.find(ch);
if (i!=m_Cache.end())
{
//glColor4f(1-r, 1-g, 1-b, a*0.5);
glColor4f(0,0,0,1);
glCallList(i->second+1);
glColor4f(r, g, b, a);
glCallList(i->second);
}
else
{
FT_Error error;
error = FT_Load_Char(m_Face, ch, FT_LOAD_DEFAULT);
if (error) return;
int glList = glGenLists(2);
GlyphGeometry* geo = new GlyphGeometry;
BuildGeometry(m_Slot,*geo);
glNewList(glList+1, GL_COMPILE);
RenderOutline(m_Slot);
glEndList();
glNewList(glList, GL_COMPILE);
RenderGeometry(*geo);
glTranslatef(m_Slot->metrics.horiAdvance,0,0);
glEndList();
delete geo;
m_Cache[ch]=glList;
//glColor4f(1-r, 1-g, 1-b, a*0.5);
glColor4f(0,0,0,1);
glCallList(glList+1);
glColor4f(r, g, b, a);
glCallList(glList);
}
glPopMatrix();
glTranslatef(m_Slot->metrics.horiAdvance,0,0);
}
float PolyGlyph::CharacterWidth(wchar_t ch)
{
FT_Error error;
error = FT_Load_Char(m_Face, ch, FT_LOAD_DEFAULT);
if (error) return 0;
return m_Slot->metrics.horiAdvance;
}
float PolyGlyph::CharacterHeight(wchar_t ch)
{
FT_Error error;
error = FT_Load_Char(m_Face, ch, FT_LOAD_DEFAULT);
if (error) return 0;
return m_Slot->metrics.vertAdvance;
}
void PolyGlyph::BuildGeometry(const FT_GlyphSlot glyph, GlyphGeometry &geo)
{
vector<GlyphGeometry::Vec3<double> > points;
GLUtesselator* t = gluNewTess();
#if (defined __APPLE__) && (MAC_OS_X_VERSION_MAX_ALLOWED <= MAC_OS_X_VERSION_10_4)
gluTessCallback(t, GLU_TESS_BEGIN_DATA, (GLvoid (*)(...))PolyGlyph::TessBegin);
gluTessCallback(t, GLU_TESS_VERTEX_DATA, (GLvoid (*)(...))PolyGlyph::TessVertex);
gluTessCallback(t, GLU_TESS_COMBINE_DATA, (GLvoid (*)(...))PolyGlyph::TessCombine);
gluTessCallback(t, GLU_TESS_END_DATA, (GLvoid (*)(...))PolyGlyph::TessEnd);
gluTessCallback(t, GLU_TESS_ERROR_DATA, (GLvoid (*)(...))PolyGlyph::TessError);
#else
#ifdef WIN32
gluTessCallback(t, GLU_TESS_BEGIN_DATA, (GLvoid (__stdcall *)())PolyGlyph::TessBegin);
gluTessCallback(t, GLU_TESS_VERTEX_DATA, (GLvoid (__stdcall *)())PolyGlyph::TessVertex);
gluTessCallback(t, GLU_TESS_COMBINE_DATA, (GLvoid (__stdcall *)())PolyGlyph::TessCombine);
gluTessCallback(t, GLU_TESS_END_DATA, (GLvoid (__stdcall *)())PolyGlyph::TessEnd);
gluTessCallback(t, GLU_TESS_ERROR_DATA, (GLvoid (__stdcall *)())PolyGlyph::TessError);
#else
gluTessCallback(t, GLU_TESS_BEGIN_DATA, (void (*)())PolyGlyph::TessBegin);
gluTessCallback(t, GLU_TESS_VERTEX_DATA, (void (*)())PolyGlyph::TessVertex);
gluTessCallback(t, GLU_TESS_COMBINE_DATA, (void (*)())PolyGlyph::TessCombine);
gluTessCallback(t, GLU_TESS_END_DATA, (void (*)())PolyGlyph::TessEnd);
gluTessCallback(t, GLU_TESS_ERROR_DATA, (void (*)())PolyGlyph::TessError);
#endif
#endif
gluTessProperty(t, GLU_TESS_WINDING_RULE, GLU_TESS_WINDING_NONZERO);
gluTessProperty(t, GLU_TESS_TOLERANCE, 0);
gluTessNormal(t, 0.0f, 0.0f, 1.0f);
gluTessBeginPolygon(t, &geo);
unsigned int start=0;
for(int c=0; c<glyph->outline.n_contours; c++)
{
unsigned int end = glyph->outline.contours[c]+1;
for(unsigned int p = start; p<end; p++)
{
points.push_back(GlyphGeometry::Vec3<double>(glyph->outline.points[p].x,glyph->outline.points[p].y,0));
}
start=end;
}
start=0;
for(int c=0; c<glyph->outline.n_contours; c++)
{
unsigned int end = glyph->outline.contours[c]+1;
gluTessBeginContour(t);
for(unsigned int p = start; p<end; p++)
{
gluTessVertex(t, &points[p].x,
&points[p].x);
}
start=end;
gluTessEndContour(t);
}
gluTessEndPolygon(t);
gluDeleteTess(t);
for (vector<double*>::iterator i=geo.m_CombinedVerts.begin(); i!=geo.m_CombinedVerts.end(); ++i)
{
delete[] *i;
}
geo.m_CombinedVerts.clear();
}
void __stdcall PolyGlyph::TessError( GLenum errCode, GlyphGeometry* geo)
{
geo->m_Error=errCode;
}
void __stdcall PolyGlyph::TessVertex( void* data, GlyphGeometry* geo)
{
double *ptr = (double*)data;
geo->m_Meshes[geo->m_Meshes.size()-1].m_Data.push_back(GlyphGeometry::Vec3<float>(ptr[0],ptr[1],ptr[2]));
}
void __stdcall PolyGlyph::TessCombine( double coords[3], void* vertex_data[4], float weight[4], void** outData, GlyphGeometry* geo)
{
double *out = new double[3];
out[0]=coords[0];
out[1]=coords[1];
out[2]=coords[2];
geo->m_CombinedVerts.push_back(out);
*outData=out;
}
void __stdcall PolyGlyph::TessBegin( GLenum type, GlyphGeometry* geo)
{
geo->m_Meshes.push_back(GlyphGeometry::Mesh(type));
}
void __stdcall PolyGlyph::TessEnd(GlyphGeometry* geo)
{
}
void PolyGlyph::RenderOutline(const FT_GlyphSlot glyph)
{
unsigned int start=0;
glLineWidth(2);
for(int c=0; c<glyph->outline.n_contours; c++)
{
glBegin(GL_LINE_LOOP);
unsigned int end = glyph->outline.contours[c]+1;
for(unsigned int p = start; p<end; p++)
{
glVertex3f(glyph->outline.points[p].x, glyph->outline.points[p].y, 0);
}
glEnd();
start=end;
}
}
bool g_bPolyGlyphUsesVertexArrays = true;
void PolyGlyph::RenderGeometry(const GlyphGeometry &geo)
{
if(g_bPolyGlyphUsesVertexArrays)
{
glEnableClientState(GL_VERTEX_ARRAY);
for (vector<GlyphGeometry::Mesh>::const_iterator i=geo.m_Meshes.begin(); i!=geo.m_Meshes.end(); i++)
{
glVertexPointer(3,GL_FLOAT,sizeof(float)*3,(void*)(&i->m_Data.begin()->x));
glDrawArrays(i->m_Type,0,i->m_Data.size());
}
}
else
{
// i don't like em, but display lists + glbegin are faster than vertex arrays
for (vector<GlyphGeometry::Mesh>::const_iterator i=geo.m_Meshes.begin(); i!=geo.m_Meshes.end(); i++)
{
glBegin(i->m_Type);
for (vector<GlyphGeometry::Vec3<float> >::const_iterator p=i->m_Data.begin(); p!=i->m_Data.end(); p++)
{
glVertex3f(p->x,p->y,p->z);
}
glEnd();
}
}
}