My favorites | Sign in
Project Home Wiki Issues Source
Checkout   Browse   Changes  
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/* ============================================================================
* 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.
* ============================================================================
*/
#include <stdarg.h>
#include <stdio.h>
#include <wchar.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;


/* --------------------------------------------------------------- display - */
void display( void )
{
glClearColor(1.00,1.00,1.00,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 );
}
}


/* ----------------------------------------------------------- init_colors - */
void
init_colors( vec4 *colors )
{
vec4 defaults[16] =
{
{{ 46/256.0f, 52/256.0f, 54/256.0f, 1.0f}},
{{204/256.0f, 0/256.0f, 0/256.0f, 1.0f}},
{{ 78/256.0f, 154/256.0f, 6/256.0f, 1.0f}},
{{196/256.0f, 160/256.0f, 0/256.0f, 1.0f}},
{{ 52/256.0f, 101/256.0f, 164/256.0f, 1.0f}},
{{117/256.0f, 80/256.0f, 123/256.0f, 1.0f}},
{{ 6/256.0f, 152/256.0f, 154/256.0f, 1.0f}},
{{211/256.0f, 215/256.0f, 207/256.0f, 1.0f}},
{{ 85/256.0f, 87/256.0f, 83/256.0f, 1.0f}},
{{239/256.0f, 41/256.0f, 41/256.0f, 1.0f}},
{{138/256.0f, 226/256.0f, 52/256.0f, 1.0f}},
{{252/256.0f, 233/256.0f, 79/256.0f, 1.0f}},
{{114/256.0f, 159/256.0f, 207/256.0f, 1.0f}},
{{173/256.0f, 127/256.0f, 168/256.0f, 1.0f}},
{{ 52/256.0f, 226/256.0f, 226/256.0f, 1.0f}},
{{238/256.0f, 238/256.0f, 236/256.0f, 1.0f}}
};
size_t i = 0;
/* Default 16 colors */
for( i=0; i< 16; ++i )
{
colors[i] = defaults[i];
}
/* Color cube */
for( i=0; i<6*6*6; i++ )
{
vec4 color = {{ (i/6/6)/5.0f, ((i/6)%6)/5.0f, (i%6)/5.0f, 1.0f}};
colors[i+16] = color;
}
/* Grascale ramp (24 tones) */
for( i=0; i<24; i++ )
{
vec4 color ={{i/24.0f, i/24.0f, i/24.0f, 1.0f}};
colors[232+i] = color;
}
}


/* -------------------------------------------------------- ansi_to_markup - */
void
ansi_to_markup( wchar_t *sequence, size_t length, markup_t *markup )
{
size_t i;
int code = 0;
int set_bg = -1;
int set_fg = -1;
vec4 none = {{0,0,0,0}};
static vec4 * colors = 0;

if( colors == 0 )
{
colors = (vec4 *) malloc( sizeof(vec4) * 256 );
init_colors( colors );
}

if( length <= 1 )
{
markup->foreground_color = colors[0];
markup->underline_color = markup->foreground_color;
markup->overline_color = markup->foreground_color;
markup->strikethrough_color = markup->foreground_color;
markup->outline_color = markup->foreground_color;
markup->background_color = none;
markup->underline = 0;
markup->overline = 0;
markup->bold = 0;
markup->italic = 0;
markup->strikethrough = 0;
return;
}

for( i=0; i<length; ++i)
{
wchar_t c = *(sequence+i);
if( c >= '0' && c <= '9' )
{
code = code * 10 + (c-'0');
}
else if( (c == ';') || (i == (length-1)) )
{
if( set_fg == 1 )
{
markup->foreground_color = colors[code];
set_fg = -1;
}
else if( set_bg == 1 )
{
markup->background_color = colors[code];
set_bg = -1;
}
else if( (set_fg == 0) && (code == 5) )
{
set_fg = 1;
code = 0;
}
else if( (set_bg == 0) && (code == 5) )
{
set_bg = 1;
code = 0;
}
/* Set fg color (30 + x, where x is the index of the color) */
else if( (code >= 30) && (code < 38 ) )
{
markup->foreground_color = colors[code-30];
}
/* Set bg color (40 + x, where x is the index of the color) */
else if( (code >= 40) && (code < 48 ) )
{
markup->background_color = colors[code-40];
}
else
{
switch (code)
{
case 0:
markup->foreground_color = colors[0];
markup->background_color = none;
markup->underline = 0;
markup->overline = 0;
markup->bold = 0;
markup->italic = 0;
markup->strikethrough = 0;
break;
case 1: markup->bold = 1; break;
case 21: markup->bold = 0; break;
case 2: markup->foreground_color.alpha = 0.5; break;
case 22: markup->foreground_color.alpha = 1.0; break;
case 3: markup->italic = 1; break;
case 23: markup->italic = 0; break;
case 4: markup->underline = 1; break;
case 24: markup->underline = 0; break;
case 8: markup->foreground_color.alpha = 0.0; break;
case 28: markup->foreground_color.alpha = 1.0; break;
case 9: markup->strikethrough = 1; break;
case 29: markup->strikethrough = 0; break;
case 53: markup->overline = 1; break;
case 55: markup->overline = 0; break;
case 39: markup->foreground_color = colors[0]; break;
case 49: markup->background_color = none; break;
case 38: set_fg = 0; break;
case 48: set_bg = 0; break;
default: break;
}
}
code = 0;
}
}
markup->underline_color = markup->foreground_color;
markup->overline_color = markup->foreground_color;
markup->strikethrough_color = markup->foreground_color;
markup->outline_color = markup->foreground_color;
}

/* ----------------------------------------------------------------- print - */
void
print( text_buffer_t * buffer, vec2 * pen,
wchar_t *text, markup_t *markup )
{
wchar_t *seq_start = text, *seq_end = text;
wchar_t *p;
for( p=text; p<(text+wcslen(text)); ++p )
{
wchar_t *start = wcsstr( p, L"\033[" );
wchar_t *end = NULL;
if( start)
{
end = wcsstr( start+1, L"m");
}
if( (start == p) && (end > start) )
{
seq_start = start+2;
seq_end = end;
p = end;
}
else
{
int seq_size = (seq_end-seq_start)+1;
wchar_t * text_start = p;
int text_size = 0;
if( start )
{
text_size = start-p;
p = start-1;
}
else
{
text_size = text+wcslen(text)-p;
p = text+wcslen(text);
}
ansi_to_markup(seq_start, seq_size, markup );
markup->font = font_manager_get_from_markup( buffer->manager, markup );
text_buffer_add_text( buffer, pen, markup, text_start, text_size );
}
}
}


/* ------------------------------------------------------------------ main - */
int main( int argc, char **argv )
{
glutInit( &argc, argv );
glutInitWindowSize( 800, 500 );
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_OFF );
vec4 black = {{0.0, 0.0, 0.0, 1.0}};
vec4 none = {{1.0, 1.0, 1.0, 0.0}};

markup_t markup;
markup.family = "fonts/VeraMono.ttf",
markup.size = 15.0;
markup.bold = 0;
markup.italic = 0;
markup.rise = 0.0;
markup.spacing = 0.0;
markup.gamma = 1.0;
markup.foreground_color = black;
markup.background_color = none;
markup.underline = 0;
markup.underline_color = black;
markup.overline = 0;
markup.overline_color = black;
markup.strikethrough = 0;
markup.strikethrough_color = black;
markup.font = 0;

vec2 pen = {{10.0, 480.0}};
FILE *file = fopen ( "data/256colors.txt", "r" );
if ( file != NULL )
{
wchar_t line[1024];
while( fgetws ( line, sizeof(line), file ) != NULL )
{
print( buffer, &pen, line, &markup );
}
fclose ( file );
}

mat4_set_identity( &projection );
mat4_set_identity( &model );
mat4_set_identity( &view );

glutMainLoop( );
return 0;
}

Change log

r219 by Nicolas.Rougier on May 2, 2013   Diff
Applied patch from  issue #56 
Go to: 
Project members, sign in to write a code review

Older revisions

r209 by Nicolas.Rougier on Apr 4, 2013   Diff
Corrected LCD rendering (was wrong,
see #48)
r199 by Nicolas.Rougier on Mar 23, 2013   Diff
Applied Christian patch and added glew
lib for windows
r193 by Nicolas.Rougier on Mar 7, 2013   Diff
Merge changes from the gl-3.0 branch
All revisions of this file

File info

Size: 12189 bytes, 362 lines
Powered by Google Project Hosting