demo-ansi.c 11.9 KB
Newer Older
Nicolas.Rougier's avatar
Nicolas.Rougier committed
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
/* ============================================================================
 * 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.
 * ============================================================================
 */
34 35 36
#include <stdarg.h>
#include <stdio.h>
#include <wchar.h>
37
#include "freetype-gl.h"
38 39 40 41 42
#include "font-manager.h"
#include "vertex-buffer.h"
#include "text-buffer.h"
#include "markup.h"
#include "shader.h"
43
#include "mat4.h"
44

45 46 47 48 49 50 51 52
#if defined(__APPLE__)
    #include <Glut/glut.h>
#elif defined(_WIN32) || defined(_WIN64)
    #include <GLUT/glut.h>
#else
    #include <GL/glut.h>
#endif

Nicolas.Rougier's avatar
Nicolas.Rougier committed
53

54
/* ------------------------------------------------------ global variables - */
55 56
text_buffer_t * buffer;
mat4   model, view, projection;
Nicolas.Rougier's avatar
Nicolas.Rougier committed
57 58


59
/* --------------------------------------------------------------- display - */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
60 61 62 63
void display( void )
{
    glClearColor(1.00,1.00,1.00,1.00);
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
64

65
    glColor4f(1.00,1.00,1.00,1.00);
66 67 68 69 70 71 72 73 74 75 76
    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 );
    }

Nicolas.Rougier's avatar
Nicolas.Rougier committed
77 78 79 80
    glutSwapBuffers( );
}


81
/* -------------------------------------------------------------- reshape - */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
82 83 84
void reshape(int width, int height)
{
    glViewport(0, 0, width, height);
85
    mat4_set_orthographic( &projection, 0, width, 0, height, -1, 1);
Nicolas.Rougier's avatar
Nicolas.Rougier committed
86 87 88
}


89
/* -------------------------------------------------------------- keyboard - */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
90 91 92 93 94 95 96 97 98
void keyboard( unsigned char key, int x, int y )
{
    if ( key == 27 )
    {
        exit( EXIT_SUCCESS );
    }
}


99
/* ----------------------------------------------------------- init_colors - */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
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;
123
    /* Default 16 colors */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
124 125 126 127
    for( i=0; i< 16; ++i )
    {
        colors[i] = defaults[i];
    }
128
    /* Color cube */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
129 130 131 132 133
    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;
    }
134
    /* Grascale ramp (24 tones) */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
135 136 137 138 139 140 141 142
    for( i=0; i<24; i++ )
    {
        vec4 color ={{i/24.0f, i/24.0f, i/24.0f, 1.0f}};
        colors[232+i] = color;
    }
}


143
/* -------------------------------------------------------- ansi_to_markup - */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
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 );
    }

Nicolas.Rougier's avatar
Nicolas.Rougier committed
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
    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;
    }

Nicolas.Rougier's avatar
Nicolas.Rougier committed
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
    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;
            }
205
            /* Set fg color (30 + x, where x is the index of the color) */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
206 207 208 209
            else if( (code >= 30) && (code < 38 ) )
            {
                markup->foreground_color = colors[code-30];
            }
210
            /* Set bg color (40 + x, where x is the index of the color) */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
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
            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;
}

258
/* ----------------------------------------------------------------- print - */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
259
void
260
print( text_buffer_t * buffer, vec2 * pen,
Nicolas.Rougier's avatar
Nicolas.Rougier committed
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
       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 );
295 296
            markup->font = font_manager_get_from_markup( buffer->manager, markup );
            text_buffer_add_text( buffer, pen, markup, text_start, text_size );
Nicolas.Rougier's avatar
Nicolas.Rougier committed
297 298 299 300 301
        }
    }
}


302
/* ------------------------------------------------------------------ main - */
Nicolas.Rougier's avatar
Nicolas.Rougier committed
303 304 305 306 307
int main( int argc, char **argv )
{
    glutInit( &argc, argv );
    glutInitWindowSize( 800, 500 );
    glutInitDisplayMode( GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH );
308
    glutCreateWindow( argv[0] );
Nicolas.Rougier's avatar
Nicolas.Rougier committed
309 310 311 312
    glutReshapeFunc( reshape );
    glutDisplayFunc( display );
    glutKeyboardFunc( keyboard );

313 314 315 316 317 318 319 320 321
    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) );

322
    buffer = text_buffer_new( LCD_FILTERING_OFF );
323 324
    vec4 black = {{0.0, 0.0, 0.0, 1.0}};
    vec4 none  = {{1.0, 1.0, 1.0, 0.0}};
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

    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;
Nicolas.Rougier's avatar
Nicolas.Rougier committed
343

344 345
    vec2 pen = {{10.0, 480.0}};
    FILE *file = fopen ( "data/256colors.txt", "r" );
Nicolas.Rougier's avatar
Nicolas.Rougier committed
346 347 348 349 350
    if ( file != NULL )
    {
        wchar_t line[1024];
        while( fgetws ( line, sizeof(line), file ) != NULL )
        {
351
            print( buffer, &pen, line, &markup );
Nicolas.Rougier's avatar
Nicolas.Rougier committed
352 353 354 355
        }
        fclose ( file );
    }

356 357 358 359
    mat4_set_identity( &projection );
    mat4_set_identity( &model );
    mat4_set_identity( &view );

Nicolas.Rougier's avatar
Nicolas.Rougier committed
360 361 362
    glutMainLoop( );
    return 0;
}