-
Notifications
You must be signed in to change notification settings - Fork 5
/
demo-markup.c
244 lines (217 loc) · 8.77 KB
/
demo-markup.c
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
241
242
243
244
/* ============================================================================
* Freetype GL - A C OpenGL Freetype engine
* Platform: Any
* WWW: http://code.google.com/p/freetype-gl/
* ----------------------------------------------------------------------------
* Copyright 2011,2012 Nicolas P. Rougier. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY NICOLAS P. ROUGIER ''AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL NICOLAS P. ROUGIER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are
* those of the authors and should not be interpreted as representing official
* policies, either expressed or implied, of Nicolas P. Rougier.
* ============================================================================
*
* Example demonstrating markup usage
*
* ============================================================================
*/
#include <stdarg.h>
#include <stdio.h>
#include <string.h>
#include <fontconfig/fontconfig.h>
#include "freetype-gl.h"
#include "font-manager.h"
#include "vertex-buffer.h"
#include "text-buffer.h"
#include "markup.h"
#include "shader.h"
#include "mat4.h"
#if defined(__APPLE__)
#include <Glut/glut.h>
#elif defined(_WIN32) || defined(_WIN64)
#include <GLUT/glut.h>
#else
#include <GL/glut.h>
#endif
// ------------------------------------------------------- global variables ---
text_buffer_t * buffer;
mat4 model, view, projection;
// ------------------------------------------------------ match_description ---
char *
match_description( char * family, float size, int bold, int italic )
{
#if defined _WIN32 || defined _WIN64
fprintf( stderr, "\"font_manager_match_description\" "
"not implemented for windows.\n" );
return 0;
#endif
char *filename = 0;
int weight = FC_WEIGHT_REGULAR;
int slant = FC_SLANT_ROMAN;
if ( bold )
{
weight = FC_WEIGHT_BOLD;
}
if( italic )
{
slant = FC_SLANT_ITALIC;
}
FcInit();
FcPattern *pattern = FcPatternCreate();
FcPatternAddDouble( pattern, FC_SIZE, size );
FcPatternAddInteger( pattern, FC_WEIGHT, weight );
FcPatternAddInteger( pattern, FC_SLANT, slant );
FcPatternAddString( pattern, FC_FAMILY, (FcChar8*) family );
FcConfigSubstitute( 0, pattern, FcMatchPattern );
FcDefaultSubstitute( pattern );
FcResult result;
FcPattern *match = FcFontMatch( 0, pattern, &result );
FcPatternDestroy( pattern );
if ( !match )
{
fprintf( stderr, "fontconfig error: could not match family '%s'", family );
return 0;
}
else
{
FcValue value;
FcResult result = FcPatternGet( match, FC_FILE, 0, &value );
if ( result )
{
fprintf( stderr, "fontconfig error: could not match family '%s'", family );
}
else
{
filename = strdup( (char *)(value.u.s) );
}
}
FcPatternDestroy( match );
return filename;
}
// ---------------------------------------------------------------- display ---
void display( void )
{
glClearColor(0.40,0.40,0.45,1.00);
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
glColor4f(1.00,1.00,1.00,1.00);
glUseProgram( buffer->shader );
{
glUniformMatrix4fv( glGetUniformLocation( buffer->shader, "model" ),
1, 0, model.data);
glUniformMatrix4fv( glGetUniformLocation( buffer->shader, "view" ),
1, 0, view.data);
glUniformMatrix4fv( glGetUniformLocation( buffer->shader, "projection" ),
1, 0, projection.data);
text_buffer_render( buffer );
}
glutSwapBuffers( );
}
// --------------------------------------------------------------- reshape ---
void reshape(int width, int height)
{
glViewport(0, 0, width, height);
mat4_set_orthographic( &projection, 0, width, 0, height, -1, 1);
}
// --------------------------------------------------------------- keyboard ---
void keyboard( unsigned char key, int x, int y )
{
if ( key == 27 )
{
exit( EXIT_SUCCESS );
}
}
// ------------------------------------------------------------------- main ---
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitWindowSize( 500, 220 );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutCreateWindow( argv[0] );
glutReshapeFunc( reshape );
glutDisplayFunc( display );
glutKeyboardFunc( keyboard );
GLenum err = glewInit();
if (GLEW_OK != err)
{
/* Problem: glewInit failed, something is seriously wrong. */
fprintf( stderr, "Error: %s\n", glewGetErrorString(err) );
exit( EXIT_FAILURE );
}
fprintf( stderr, "Using GLEW %s\n", glewGetString(GLEW_VERSION) );
buffer = text_buffer_new( LCD_FILTERING_ON );
vec4 black = {{0.0, 0.0, 0.0, 1.0}};
vec4 white = {{1.0, 1.0, 1.0, 1.0}};
vec4 yellow = {{1.0, 1.0, 0.0, 1.0}};
vec4 grey = {{0.5, 0.5, 0.5, 1.0}};
vec4 none = {{1.0, 1.0, 1.0, 0.0}};
char *f_normal = match_description("Droid Serif", 24, 0, 0);
char *f_bold = match_description("Droid Serif", 24, 1, 0);
char *f_italic = match_description("Droid Serif", 24, 0, 1);
char *f_japanese = match_description("Droid Sans Japanese", 18, 0, 0);
char *f_math = match_description("DejaVu Sans", 24, 0, 0);
markup_t normal = {
.family = f_normal,
.size = 24.0, .bold = 0, .italic = 0,
.rise = 0.0, .spacing = 0.0, .gamma = 2.,
.foreground_color = white, .background_color = none,
.underline = 0, .underline_color = white,
.overline = 0, .overline_color = white,
.strikethrough = 0, .strikethrough_color = white,
.font = 0,
};
markup_t highlight = normal; highlight.background_color = grey;
markup_t reverse = normal; reverse.foreground_color = black;
reverse.background_color = white;
reverse.gamma = 1.0;
markup_t overline = normal; overline.overline = 1;
markup_t underline = normal; underline.underline = 1;
markup_t small = normal; small.size = 10.0;
markup_t big = normal; big.size = 48.0;
big.italic = 1;
big.foreground_color = yellow;
markup_t bold = normal; bold.bold = 1; bold.family = f_bold;
markup_t italic = normal; italic.italic = 1; italic.family = f_italic;
markup_t japanese = normal; japanese.family = f_japanese;
japanese.size = 18.0;
markup_t math = normal; math.family = f_math;
vec2 pen = {{20, 200}};
text_buffer_printf( buffer, &pen,
&underline, L"The",
&normal, L" Quick",
&big, L" brown ",
&reverse, L" fox \n",
&italic, L"jumps over ",
&bold, L"the lazy ",
&normal, L"dog.\n",
&small, L"Now is the time for all good men "
L"to come to the aid of the party.\n",
&italic, L"Ég get etið gler án þess að meiða mig.\n",
&japanese, L"私はガラスを食べられます。 それは私を傷つけません\n",
&math, L"ℕ ⊆ ℤ ⊂ ℚ ⊂ ℝ ⊂ ℂ",
NULL );
mat4_set_identity( &projection );
mat4_set_identity( &model );
mat4_set_identity( &view );
glutMainLoop( );
return 0;
}