-
Notifications
You must be signed in to change notification settings - Fork 5
/
demo-atb-agg.c
executable file
·672 lines (590 loc) · 21.1 KB
/
demo-atb-agg.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
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/* ============================================================================
* 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 <math.h>
#include <stdio.h>
#include <AntTweakBar.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
// ------------------------------------------------------- typedef & struct ---
typedef enum {
VERA = 1,
VERA_MONO,
GEORGIA,
TIMES,
VERDANA,
TAHOMA,
ARIAL,
} font_family_e;
#define NUM_FONTS 7
// ------------------------------------------------------- global variables ---
TwBar *bar;
text_buffer_t * buffer;
text_buffer_t * buffer_a;
text_buffer_t * buffer_rgb;
mat4 model, view, projection;
font_family_e p_family;
float p_size;
int p_invert;
int p_kerning;
int p_hinting;
int p_lcd_filtering;
float p_gamma;
float p_interval;
float p_weight;
float p_width;
float p_faux_weight;
float p_faux_italic;
float p_primary;
float p_secondary;
float p_tertiary;
static wchar_t text[] =
L"A single pixel on a color LCD is made of three colored elements \n"
L"ordered (on various displays) either as blue, green, and red (BGR), \n"
L"or as red, green, and blue (RGB). These pixel components, sometimes \n"
L"called sub-pixels, appear as a single color to the human eye because \n"
L"of blurring by the optics and spatial integration by nerve cells in "
L"the eye.\n"
L"\n"
L"The resolution at which colored sub-pixels go unnoticed differs, \n"
L"however, with each user some users are distracted by the colored \n"
L"\"fringes\" resulting from sub-pixel rendering. Subpixel rendering \n"
L"is better suited to some display technologies than others. The \n"
L"technology is well-suited to LCDs, but less so for CRTs. In a CRT \n"
L"the light from the pixel components often spread across pixels, \n"
L"and the outputs of adjacent pixels are not perfectly independent."
L"\n";
// ----------------------------------------------------------- build_buffer ---
void
build_buffer( void )
{
vec2 pen;
texture_font_t *font;
vec4 black = {{0.0, 0.0, 0.0, 1.0}};
vec4 white = {{1.0, 1.0, 1.0, 1.0}};
vec4 none = {{1.0, 1.0, 1.0, 0.0}};
vec4 color = white;
if( p_invert )
{
color = black;
}
markup_t markup = {
.family = "Arial",
.size = 10.0,
.bold = 0,
.italic = 0,
.rise = 0.0,
.spacing = p_interval,
.gamma = p_gamma,
.foreground_color = color,
.background_color = none,
.underline = 0,
.underline_color = color,
.overline = 0,
.overline_color = color,
.strikethrough = 0,
.strikethrough_color = color,
.font = 0,
};
text_buffer_clear( buffer );
texture_atlas_t * atlas = buffer->manager->atlas;
texture_atlas_clear( atlas );
if( p_family == VERA)
{
font = texture_font_new( atlas, "fonts/Vera.ttf", p_size );
}
else if( p_family == VERA_MONO)
{
font = texture_font_new( atlas, "fonts/VeraMono.ttf", p_size );
}
else if( p_family == GEORGIA)
{
font = texture_font_new( atlas, "fonts/Georgia.ttf", p_size );
}
else if( p_family == TIMES )
{
font = texture_font_new( atlas, "fonts/Times.ttf", p_size );
}
else if( p_family == TAHOMA )
{
font = texture_font_new( atlas, "fonts/Tahoma.ttf", p_size );
}
else if( p_family == ARIAL )
{
font = texture_font_new( atlas, "fonts/Arial.ttf", p_size );
}
else if( p_family == VERDANA )
{
font = texture_font_new( atlas, "fonts/Verdana.ttf", p_size );
}
else
{
fprintf( stderr, "Error : Unknown family type\n" );
return;
}
markup.font = font;
font->hinting = p_hinting;
font->kerning = p_kerning;
font->filtering = 1;
float norm = 1.0/(p_primary + 2*p_secondary + 2*p_tertiary);
font->lcd_weights[0] = (unsigned char)(p_tertiary*norm*256);
font->lcd_weights[1] = (unsigned char)(p_secondary*norm*256);
font->lcd_weights[2] = (unsigned char)(p_primary*norm*256);
font->lcd_weights[3] = (unsigned char)(p_secondary*norm*256);
font->lcd_weights[4] = (unsigned char)(p_tertiary*norm*256);
pen.x = 10;
pen.y = 600 - font->height - 10;
text_buffer_printf( buffer, &pen, &markup, text, NULL );
// Post-processing for width and orientation
vertex_buffer_t * vbuffer = buffer->buffer;
size_t i;
for( i=0; i < vector_size( vbuffer->items ); ++i )
{
ivec4 *item = (ivec4 *) vector_get( vbuffer->items, i);
glyph_vertex_t * v0 = /* x0,y0 */
(glyph_vertex_t *) vector_get( vbuffer->vertices, item->vstart+0 );
//glyph_vertex_t * v1 = /* x0,y1 */
// (glyph_vertex_t *) vector_get( vbuffer->vertices, item->vstart+1 );
glyph_vertex_t * v2 = /* x1,y1 */
(glyph_vertex_t *) vector_get( vbuffer->vertices, item->vstart+2 );
glyph_vertex_t * v3 = /* x1,y0 */
(glyph_vertex_t *) vector_get( vbuffer->vertices, item->vstart+3 );
float x0 = v0->x, y0 = v0->y;
float x1 = v2->x, y1 = v2->y;
v2->x = v3->x = x0 + (x1-x0)*p_width;
float dy = abs(y1-y0);
float dx = tan(p_faux_italic/180.0 * M_PI) * dy;
v0->x += dx;
v0->shift = fmod(v0->shift + dx-(int)(dx),1.0);
v3->x += dx;
v3->shift = fmod(v3->shift + dx-(int)(dx),1.0);
}
texture_font_delete( font );
}
// ---------------------------------------------------------------- display ---
void display(void)
{
vec4 black = {{0.0, 0.0, 0.0, 1.0}};
vec4 white = {{1.0, 1.0, 1.0, 1.0}};
if( !p_invert )
{
glClearColor( 0, 0, 0, 1 );
buffer->base_color = white;
}
else
{
glClearColor( 1, 1, 1, 1 );
buffer->base_color = black;
}
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
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 );
}
TwDraw( );
glutSwapBuffers( );
}
// ---------------------------------------------------------------- reshape ---
void reshape( int width, int height )
{
glViewport(0, 0, width, height);
mat4_set_orthographic( &projection, 0, width, 0, height, -1, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, width, 0, height, -1, 1);
glMatrixMode(GL_MODELVIEW);
TwWindowSize( width, height );
}
// ------------------------------------------------------------------- quit ---
void reset( void )
{
p_family = VERA;
p_size = 12.0;
p_invert = 0;
p_kerning = 1;
p_hinting = 1;
p_lcd_filtering = 1;
p_gamma = 1.75;
p_interval = 0.0;
p_weight = 0.33;
p_width = 1.0;
p_faux_weight = 0.0;
p_faux_italic = 0.0;
// FT_LCD_FILTER_LIGHT
p_primary = 1.0/3.0;
p_secondary = 1.0/3.0;
p_tertiary = 0.0/3.0;
// FT_LCD_FILTER_DEFAULT
// p_primary = 3.0/9.0;
// p_secondary = 2.0/9.0;
// p_tertiary = 1.0/9.0;
build_buffer();
}
// ------------------------------------------------------------------- quit ---
void quit( void )
{
exit( EXIT_SUCCESS );
}
// -------------------------------------------------------------- terminate ---
void terminate( void )
{
TwTerminate();
}
void timer( int fps )
{
glutPostRedisplay();
glutTimerFunc( 1000.0/fps, timer, fps );
}
// --------------------------------------------------------- get/set invert ---
void TW_CALL set_invert( const void *value, void *data )
{
p_invert = *(const int *) value;
build_buffer();
}
void TW_CALL get_invert( void *value, void *data )
{
*(int *)value = p_invert;
}
// -------------------------------------------------------- get/set kerning ---
void TW_CALL set_kerning( const void *value, void *data )
{
p_kerning = *(const int *) value;
build_buffer();
}
void TW_CALL get_kerning( void *value, void *data )
{
*(int *)value = p_kerning;
}
// -------------------------------------------------------- get/set hinting ---
void TW_CALL set_hinting( const void *value, void *data )
{
p_hinting = *(const int *) value;
build_buffer();
}
void TW_CALL get_hinting( void *value, void *data )
{
*(int *)value = p_hinting;
}
// -------------------------------------------------- get/set lcd_filtering ---
void TW_CALL set_lcd_filtering( const void *value, void *data )
{
p_lcd_filtering = *(const int *) value;
if( p_lcd_filtering )
{
buffer = buffer_rgb;
}
else
{
buffer = buffer_a;
}
build_buffer();
}
void TW_CALL get_lcd_filtering( void *value, void *data )
{
*(int *)value = p_lcd_filtering;
}
// --------------------------------------------------------- get/set weight ---
void TW_CALL set_weight( const void *value, void *data )
{
p_weight = *(const float *) value;
}
void TW_CALL get_weight( void *value, void *data )
{
*(float *)value = p_weight;
}
// ---------------------------------------------------------- get/set gamma ---
void TW_CALL set_gamma( const void *value, void *data )
{
p_gamma = *(const float *) value;
build_buffer();
}
void TW_CALL get_gamma( void *value, void *data )
{
*(float *)value = p_gamma;
}
// ---------------------------------------------------------- get/set width ---
void TW_CALL set_width( const void *value, void *data )
{
p_width = *(const float *) value;
build_buffer();
}
void TW_CALL get_width( void *value, void *data )
{
*(float *)value = p_width;
}
// ------------------------------------------------------- get/set interval ---
void TW_CALL set_interval( const void *value, void *data )
{
p_interval = *(const float *) value;
build_buffer();
}
void TW_CALL get_interval( void *value, void *data )
{
*(float *)value = p_interval;
}
// ---------------------------------------------------- get/set faux_weight ---
void TW_CALL set_faux_weight( const void *value, void *data )
{
p_faux_weight = *(const float *) value;
}
void TW_CALL get_faux_weight( void *value, void *data )
{
*(float *)value = p_faux_weight;
}
// ---------------------------------------------------- get/set faux_italic ---
void TW_CALL set_faux_italic( const void *value, void *data )
{
p_faux_italic = *(const float *) value;
build_buffer();
}
void TW_CALL get_faux_italic( void *value, void *data )
{
*(float *)value = p_faux_italic;
}
// ----------------------------------------------------------- get/set size ---
void TW_CALL set_size( const void *value, void *data )
{
p_size = *(const float *) value;
build_buffer();
}
void TW_CALL get_size( void *value, void *data )
{
*(float *)value = p_size;
}
// --------------------------------------------------------- get/set family ---
void TW_CALL set_family( const void *value, void *data )
{
p_family = *(const font_family_e *) value;
build_buffer();
}
void TW_CALL get_family( void *value, void *data )
{
*(font_family_e *)value = p_family;
}
// ----------------------------------------------------------- get/set primary ---
void TW_CALL set_primary( const void *value, void *data )
{
p_primary = *(const float *) value;
build_buffer();
}
void TW_CALL get_primary( void *value, void *data )
{
*(float *)value = p_primary;
}
// ----------------------------------------------------------- get/set secondary ---
void TW_CALL set_secondary( const void *value, void *data )
{
p_secondary = *(const float *) value;
build_buffer();
}
void TW_CALL get_secondary( void *value, void *data )
{
*(float *)value = p_secondary;
}
// ----------------------------------------------------------- get/set tertiary ---
void TW_CALL set_tertiary( const void *value, void *data )
{
p_tertiary = *(const float *) value;
build_buffer();
}
void TW_CALL get_tertiary( void *value, void *data )
{
*(float *)value = p_tertiary;
}
// Main
int main(int argc, char *argv[])
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
glutInitWindowSize( 800, 600 );
glutCreateWindow( "Font rendering advanced tweaking" );
glutCreateMenu( NULL );
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) );
glutDisplayFunc( display );
glutReshapeFunc( reshape );
atexit( terminate );
TwInit( TW_OPENGL, NULL );
glutMouseFunc( (GLUTmousebuttonfun)TwEventMouseButtonGLUT );
glutMotionFunc( (GLUTmousemotionfun)TwEventMouseMotionGLUT );
glutPassiveMotionFunc( (GLUTmousemotionfun)TwEventMouseMotionGLUT );
glutKeyboardFunc( (GLUTkeyboardfun)TwEventKeyboardGLUT );
glutSpecialFunc( (GLUTspecialfun)TwEventSpecialGLUT );
TwGLUTModifiersFunc( glutGetModifiers );
// Create a new tweak bar
bar = TwNewBar("TweakBar");
TwDefine("GLOBAL "
"help = 'This example shows how to tune all font parameters.' ");
TwDefine("TweakBar "
"size = '280 400' "
"position = '500 20' "
"color = '127 127 127' "
"alpha = 240 "
"label = 'Parameters' "
"resizable = True "
"fontresizable = True "
"iconifiable = True ");
{
TwEnumVal familyEV[NUM_FONTS] = {
{VERA, "Vera"},
{VERA_MONO, "Vera Mono"},
{GEORGIA, "Georgia"},
{TIMES, "Times"},
{VERDANA, "Verdana"},
{TAHOMA, "Tahoma"},
{ARIAL, "Arial"} };
TwType family_type = TwDefineEnum("Family", familyEV, NUM_FONTS);
TwAddVarCB(bar, "Family", family_type, set_family, get_family, NULL,
"label = 'Family' "
"group = 'Font' "
"help = ' ' ");
}
TwAddVarCB(bar, "Size", TW_TYPE_FLOAT, set_size, get_size, NULL,
"label = 'Size' "
"group = 'Font' "
"min = 6.0 "
"max = 24.0 "
"step = 0.05 "
"help = ' ' ");
TwAddVarCB(bar, "LCD filtering", TW_TYPE_BOOL32, set_lcd_filtering, get_lcd_filtering, NULL,
"label = 'LCD filtering' "
"group = 'Font' "
"help = ' ' ");
// Rendering
TwAddVarCB(bar, "Kerning", TW_TYPE_BOOL32, set_kerning, get_kerning, NULL,
"label = 'Kerning' "
"group = 'Rendering' "
"help = ' ' ");
TwAddVarCB(bar, "Hinting", TW_TYPE_BOOL32, set_hinting, get_hinting, NULL,
"label = 'Hinting' "
"group = 'Rendering' "
"help = ' ' ");
// Color
TwAddVarCB(bar, "Invert", TW_TYPE_BOOL32, set_invert, get_invert, NULL,
"label = 'Invert' "
"group = 'Color' "
"help = ' ' ");
// Glyph
TwAddVarCB(bar, "Width", TW_TYPE_FLOAT, set_width, get_width, NULL,
"label = 'Width' "
"group = 'Glyph' "
"min = 0.75 "
"max = 1.25 "
"step = 0.01 "
"help = ' ' ");
TwAddVarCB(bar, "Interval", TW_TYPE_FLOAT, set_interval, get_interval, NULL,
"label = 'Spacing' "
"group = 'Glyph' "
"min = -0.2 "
"max = 0.2 "
"step = 0.01 "
"help = ' ' " );
TwAddVarCB(bar, "Faux italic", TW_TYPE_FLOAT, set_faux_italic, get_faux_italic, NULL,
"label = 'Faux italic' "
"group = 'Glyph' "
"min = -30.0 "
"max = 30.0 "
"step = 0.1 "
"help = ' ' ");
// Energy distribution
TwAddVarCB(bar, "Primary", TW_TYPE_FLOAT, set_primary, get_primary, NULL,
"label = 'Primary weight' "
"group = 'Energy distribution' "
"min = 0 "
"max = 1 "
"step = 0.01 "
"help = ' ' " );
TwAddVarCB(bar, "Secondary", TW_TYPE_FLOAT, set_secondary, get_secondary, NULL,
"label = 'Secondy weight' "
"group = 'Energy distribution' "
"min = 0 "
"max = 1 "
"step = 0.01 "
"help = ' ' " );
TwAddVarCB(bar, "Tertiary", TW_TYPE_FLOAT, set_tertiary, get_tertiary, NULL,
"label = 'Tertiary weight' "
"group = 'Energy distribution' "
"min = 0 "
"max = 1 "
"step = 0.01 "
"help = ' ' " );
TwAddSeparator(bar, "",
"group = 'Energy distribution' " );
TwAddVarCB(bar, "Gamma", TW_TYPE_FLOAT, set_gamma, get_gamma, NULL,
"label = 'Gamma correction' "
"group = 'Energy distribution' "
"min = 0.50 "
"max = 2.5 "
"step = 0.01 "
"help = ' ' ");
TwAddSeparator(bar, "", "");
TwAddButton(bar, "Reset", (TwButtonCallback) reset, NULL,
"help='Reset all parameters to default values.'");
TwAddSeparator(bar, "", "");
TwAddButton(bar, "Quit", (TwButtonCallback) quit, NULL,
"help='Quit.'");
buffer_a = text_buffer_new( 1 );
buffer_rgb = text_buffer_new( 3 );
buffer = buffer_rgb;
reset();
glutTimerFunc( 1000.0/60.0, timer, 60 );
mat4_set_identity( &projection );
mat4_set_identity( &model );
mat4_set_identity( &view );
glutMainLoop();
return EXIT_SUCCESS;
}