Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/libmupen64plus/mupen64plus-core/src/osd/OGLFT.h
2 views
1
// -*- c++ -*-
2
/*
3
* OGLFT: A library for drawing text with OpenGL using the FreeType library
4
* Copyright (C) 2002 lignum Computing, Inc. <[email protected]>
5
* $Id: OGLFT.h,v 1.15 2003/10/01 14:41:09 allen Exp $
6
*
7
* This library is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU Lesser General Public
9
* License as published by the Free Software Foundation; either
10
* version 2.1 of the License, or (at your option) any later version.
11
*
12
* This library is distributed in the hope that it will be useful,
13
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* Lesser General Public License for more details.
16
*
17
* You should have received a copy of the GNU Lesser General Public
18
* License along with this library; if not, write
19
* Free Software Foundation, Inc.,
20
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
*/
22
23
#ifndef OGLFT_H
24
#define OGLFT_H
25
26
#include <cmath>
27
#include <map>
28
#include <list>
29
#include <vector>
30
#include <wchar.h>
31
32
#define GL_GLEXT_PROTOTYPES
33
#include <SDL_opengl.h>
34
#if defined(__MACOSX__)
35
#include <OpenGL/glu.h>
36
#elif defined(__MACOS__)
37
#include <glu.h>
38
#else
39
#include <GL/glu.h>
40
#endif
41
42
#include <ft2build.h>
43
#include FT_FREETYPE_H
44
#include FT_GLYPH_H
45
#include FT_OUTLINE_H
46
#include FT_TRIGONOMETRY_H
47
48
namespace OGLFT
49
{
50
enum Coordinates
51
{
52
X, Y, Z, W
53
};
54
55
enum ColorSpace
56
{
57
R, G, B, A
58
};
59
60
// global library functions
61
bool Init_FT(void);
62
bool Uninit_FT(void);
63
64
struct Advance
65
{
66
float dx_;
67
float dy_;
68
69
Advance ( float dx = 0, float dy = 0 ) : dx_( dx ), dy_( dy )
70
{
71
return;
72
}
73
74
Advance ( FT_Vector v )
75
{
76
dx_ = (float) (v.x / 64.);
77
dy_ = (float) (v.y / 64.);
78
}
79
80
Advance& operator+= ( const FT_Vector v )
81
{
82
dx_ += (float) (v.x / 64.);
83
dy_ += (float) (v.y / 64.);
84
return *this;
85
}
86
};
87
88
struct BBox
89
{
90
float x_min_;
91
float y_min_;
92
float x_max_;
93
float y_max_;
94
Advance advance_;
95
96
BBox () : x_min_( 0 ), y_min_( 0 ), x_max_( 0 ), y_max_( 0 )
97
{
98
return;
99
}
100
BBox ( FT_BBox ft_bbox )
101
{
102
x_min_ = (float) (ft_bbox.xMin / 64.);
103
y_min_ = (float) (ft_bbox.yMin / 64.);
104
x_max_ = (float) (ft_bbox.xMax / 64.);
105
y_max_ = (float) (ft_bbox.yMax / 64.);
106
}
107
BBox& operator*= ( double k )
108
{
109
x_min_ *= (float) k;
110
y_min_ *= (float) k;
111
x_max_ *= (float) k;
112
y_max_ *= (float) k;
113
advance_.dx_ *= (float) k;
114
advance_.dy_ *= (float) k;
115
116
return *this;
117
}
118
BBox& operator+= ( const BBox& b )
119
{
120
float new_value;
121
122
new_value = b.x_min_ + advance_.dx_;
123
if ( new_value < x_min_ ) x_min_ = new_value;
124
new_value = b.y_min_ + advance_.dy_;
125
if ( new_value < y_min_ ) y_min_ = new_value;
126
new_value = b.x_max_ + advance_.dx_;
127
if ( new_value > x_max_ ) x_max_ = new_value;
128
new_value = b.y_max_ + advance_.dy_;
129
if ( new_value > y_max_ ) y_max_ = new_value;
130
131
advance_.dx_ += b.advance_.dx_;
132
advance_.dy_ += b.advance_.dy_;
133
134
return *this;
135
}
136
};
137
typedef std::vector<GLuint> DisplayLists;
138
typedef DisplayLists::const_iterator DLCI;
139
typedef DisplayLists::iterator DLI;
140
class Face
141
{
142
public:
143
enum HorizontalJustification
144
{
145
LEFT,
146
ORIGIN,
147
CENTER,
148
RIGHT
149
};
150
151
enum VerticalJustification
152
{
153
BOTTOM,
154
BASELINE,
155
MIDDLE,
156
TOP
157
};
158
159
enum GlyphCompileMode
160
{
161
COMPILE,
162
IMMEDIATE
163
};
164
165
private:
166
struct FaceData
167
{
168
FT_Face face_;
169
bool free_on_exit_;
170
FaceData ( FT_Face face, bool free_on_exit = true )
171
: face_( face ), free_on_exit_( free_on_exit )
172
{
173
return;
174
}
175
};
176
177
protected:
178
std::vector< FaceData > faces_;
179
bool valid_;
180
enum GlyphCompileMode compile_mode_;
181
float point_size_;
182
FT_UInt resolution_;
183
bool advance_;
184
GLfloat foreground_color_[4];
185
GLfloat background_color_[4];
186
enum HorizontalJustification horizontal_justification_;
187
enum VerticalJustification vertical_justification_;
188
GLfloat string_rotation_;
189
FT_UInt rotation_reference_glyph_;
190
FT_Face rotation_reference_face_;
191
GLfloat rotation_offset_y_;
192
typedef std::map< FT_UInt, GLuint > GlyphDLists;
193
typedef GlyphDLists::const_iterator GDLCI;
194
typedef GlyphDLists::iterator GDLI;
195
GlyphDLists glyph_dlists_;
196
DisplayLists character_display_lists_;
197
198
public:
199
Face ( const char* filename, float point_size = 12, FT_UInt resolution = 100 );
200
Face ( FT_Face face, float point_size = 12, FT_UInt resolution = 100 );
201
virtual ~Face ( void );
202
bool isValid ( void ) const { return valid_; }
203
bool addAuxiliaryFace ( const char* filename );
204
bool addAuxiliaryFace ( FT_Face face );
205
void setCompileMode ( enum GlyphCompileMode compile_mode ) { compile_mode_ = compile_mode; }
206
enum GlyphCompileMode compileMode ( void ) const { return compile_mode_; }
207
void setPointSize ( float point_size );
208
float pointSize ( void ) { return point_size_; }
209
void setResolution ( FT_UInt resolution );
210
FT_UInt resolution ( void ) { return resolution_; }
211
void setAdvance ( bool advance ) { advance_ = advance; }
212
bool advance ( void ) const { return advance_; }
213
void setForegroundColor ( GLfloat red = 0.0, GLfloat green = 0.0, GLfloat blue = 0.0, GLfloat alpha = 1.0 );
214
void setForegroundColor ( const GLfloat foreground_color[4] );
215
GLfloat foregroundRed ( void ) const { return foreground_color_[R]; }
216
GLfloat foregroundGreen ( void ) const { return foreground_color_[G]; }
217
GLfloat foregroundBlue ( void ) const { return foreground_color_[B]; }
218
GLfloat foregroundAlpha ( void ) const { return foreground_color_[A]; }
219
void setBackgroundColor ( GLfloat red = 1.0, GLfloat green = 1.0, GLfloat blue = 1.0, GLfloat alpha = 0.0 );
220
void setBackgroundColor ( const GLfloat background_color[4] );
221
GLfloat backgroundRed ( void ) const { return background_color_[R]; }
222
GLfloat backgroundGreen ( void ) const { return background_color_[G]; }
223
GLfloat backgroundBlue ( void ) const { return background_color_[B]; }
224
GLfloat backgroundAlpha ( void ) const { return background_color_[A]; }
225
virtual void setCharacterRotationZ ( GLfloat character_rotation_z ) = 0;
226
virtual GLfloat characterRotationZ ( void ) const = 0;
227
void setCharacterRotationReference ( unsigned char c );
228
void setStringRotation ( GLfloat string_rotation );
229
GLfloat stringRotation ( void ) const { return string_rotation_; }
230
void setHorizontalJustification ( enum HorizontalJustification horizontal_justification )
231
{
232
horizontal_justification_ = horizontal_justification;
233
}
234
enum HorizontalJustification horizontalJustification ( void ) const { return horizontal_justification_; }
235
void setVerticalJustification ( enum VerticalJustification vertical_justification )
236
{
237
vertical_justification_ = vertical_justification;
238
}
239
240
enum VerticalJustification verticaljustification ( void ) const { return vertical_justification_; }
241
void setCharacterDisplayLists ( const DisplayLists& character_display_lists ) { character_display_lists_ = character_display_lists; }
242
DisplayLists& characterDisplayLists ( void ) { return character_display_lists_; }
243
virtual double height ( void ) const = 0;
244
virtual BBox measure ( unsigned char c ) = 0;
245
virtual BBox measure ( wchar_t c ) = 0;
246
virtual BBox measure ( const char* s );
247
virtual BBox measureRaw ( const char* s );
248
virtual BBox measure ( const wchar_t* s );
249
virtual BBox measure ( const wchar_t* format, double number );
250
virtual BBox measureRaw ( const wchar_t* s );
251
GLuint compile ( unsigned char c );
252
GLuint compile ( const wchar_t c );
253
void draw ( const char* s );
254
void draw ( const wchar_t* s );
255
void draw ( unsigned char c );
256
void draw ( const wchar_t c );
257
void draw ( GLfloat x, GLfloat y, unsigned char c );
258
void draw ( GLfloat x, GLfloat y, GLfloat z, unsigned char c );
259
void draw ( GLfloat x, GLfloat y, wchar_t c );
260
void draw ( GLfloat x, GLfloat y, GLfloat z, wchar_t c );
261
void draw ( GLfloat x, GLfloat y, const char* s, float *sizebox );
262
void draw ( GLfloat x, GLfloat y, GLfloat z, const char* s );
263
void draw ( GLfloat x, GLfloat y, const wchar_t* s );
264
void draw ( GLfloat x, GLfloat y, GLfloat z, const wchar_t* s );
265
int ascender ( void ) { return faces_.front().face_->ascender; }
266
int descender ( void ) { return faces_.front().face_->descender; }
267
BBox measure_nominal ( const char* s );
268
BBox measure_nominal ( const wchar_t* s );
269
270
protected:
271
virtual GLuint compileGlyph ( FT_Face face, FT_UInt glyph_index ) = 0;
272
virtual void renderGlyph ( FT_Face face, FT_UInt glyph_index ) = 0;
273
virtual void setCharSize ( void ) = 0;
274
virtual void clearCaches ( void ) = 0;
275
virtual void setRotationOffset ( void ) = 0;
276
277
private:
278
void init ( void );
279
};
280
281
class Raster : public Face
282
{
283
protected:
284
GLfloat character_rotation_z_;
285
public:
286
Raster ( const char* filename, float point_size = 12, FT_UInt resolution = 100 );
287
Raster ( FT_Face face, float point_size = 12, FT_UInt resolution = 100 );
288
virtual ~Raster ( void );
289
void setCharacterRotationZ ( GLfloat character_rotation_z );
290
GLfloat characterRotationZ ( void ) const { return character_rotation_z_; }
291
double height ( void ) const;
292
BBox measure ( unsigned char c );
293
BBox measure ( wchar_t c );
294
BBox measure ( const char* s ) { return Face::measure( s ); }
295
BBox measure ( const wchar_t* format, double number ) { return Face::measure( format, number ); }
296
297
private:
298
void init ( void );
299
GLuint compileGlyph ( FT_Face face, FT_UInt glyph_index );
300
void setCharSize ( void );
301
void setRotationOffset ( void );
302
void clearCaches ( void );
303
};
304
class Monochrome : public Raster
305
{
306
public:
307
Monochrome ( const char* filename, float point_size = 12, FT_UInt resolution = 100 );
308
Monochrome ( FT_Face face, float point_size = 12, FT_UInt resolution = 100 );
309
~Monochrome ( void );
310
311
private:
312
GLubyte* invertBitmap ( const FT_Bitmap& bitmap );
313
void renderGlyph ( FT_Face face, FT_UInt glyph_index );
314
};
315
316
}
317
#endif /* OGLFT_H */
318
319
320