summaryrefslogtreecommitdiff
path: root/Build/source/libs/freetype/freetype-1.5/test/ftview.c
blob: 71ca0d87a28a2ea82339d33c25742db53904f8ca (plain)
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
/****************************************************************************/
/*                                                                          */
/*  The FreeType project -- a free and portable quality TrueType renderer.  */
/*                                                                          */
/*  Copyright 1996-2001 by                                                  */
/*  D. Turner, R.Wilhelm, and W. Lemberg                                    */
/*                                                                          */
/*  ftview:  A simple font viewer.  Supports hinting and grayscaling.       */
/*                                                                          */
/*                                                                          */
/*  Keys:                                                                   */
/*                                                                          */
/*  x :   fine counter-clockwise rotation                                   */
/*  c :   fine clockwise rotation                                           */
/*                                                                          */
/*  v :   fast counter-clockwise rotation                                   */
/*  b :   fast clockwise rotation                                           */
/*                                                                          */
/*  + :   fast scale up                                                     */
/*  - :   fast scale down                                                   */
/*  u :   fine scale down                                                   */
/*  j :   fine scale up                                                     */
/*                                                                          */
/*  l :   go to next glyph                                                  */
/*  k :   go to previous glyph                                              */
/*                                                                          */
/*  o :   go to tenth next glyph                                            */
/*  i :   go to tenth previous glyph                                        */
/*                                                                          */
/*  0 :   go to hundredth next glyph                                        */
/*  9 :   go to hundredth previous glyph                                    */
/*                                                                          */
/*  ) :   go to 1000th next glyph                                           */
/*  ( :   go to 1000th previous glyph                                       */
/*                                                                          */
/*  } :   go to 10000th next glyph                                          */
/*  { :   go to 10000th previous glyph                                      */
/*                                                                          */
/*  n :   go to next (or last) .ttf file                                    */
/*  p :   go to previous (or first) .ttf file                               */
/*                                                                          */
/*  h :   toggle hinting                                                    */
/*                                                                          */
/*  B :   toggle sbit                                                       */
/*                                                                          */
/*  ESC :   exit                                                            */
/*                                                                          */
/*                                                                          */
/*  NOTE: This is just a test program that is used to show off and          */
/*        debug the current engine.                                         */
/*                                                                          */
/****************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "blitter.h"
#include "common.h"
#include "display.h"
#include "freetype.h"
#include "ftxsbit.h"

#include "gdriver.h"
#include "gevents.h"
#include "gmain.h"

#define  Pi         3.1415926535

#define  MAXPTSIZE  500                 /* dtp */
#define  Center_X   ( Bit.width / 2 )   /* dtp */
#define  Center_Y   ( Bit.rows  / 2 )   /* dtp */

/*
 * If Ignore_Err_Not_SBit is not defined,
 * "Fail" increase when the glyph does not have sbit and
 * sbit_flag is 1.
 */
#define  Ignore_Err_Not_SBit 1

  TT_Engine             engine;

  TT_Face               face;
  TT_Instance           instance;
  TT_Glyph              glyph;

  TT_Big_Glyph_Metrics  metrics;
  TT_Outline            outline;
  TT_Face_Properties    properties;
  TT_Instance_Metrics   imetrics;

  TT_SBit_Image*        sbit;

  int            num_glyphs;

  int            ptsize;
  int            hinted;

  char           Header[128];

  int            Rotation;
  int            Fail;
  int            Num;

  int            gray_render;
  unsigned int   sbit_flag;     /* 0 if to display outlines
				 * 1 if to use embedded bitmaps
				 * 2 if to show sbit and outline glyphs
				 */

  int            glyph_has_sbit;



  static TT_Error  Reset_Scale( int  pointSize )
  {
    TT_Error  error;
    TT_SBit_Strike strike;


    if ( (error = TT_Set_Instance_PointSize( instance, pointSize )) )
    {
      RestoreScreen();
      fprintf( stderr, "Error = 0x%x.\n", (unsigned)error );
      Panic( "Could not reset instance.\n" );
    }
    TT_Get_Instance_Metrics( instance, &imetrics );

    /* now re-allocates the small bitmap */
    if ( gray_render )
    {
      Init_Small( imetrics.x_ppem, imetrics.y_ppem );
      Clear_Small();
    }

    if ( TT_Get_SBit_Strike( face, instance, &strike ) )
      glyph_has_sbit = 0;
    else
      glyph_has_sbit = 1;

    return TT_Err_Ok;
  }


  static TT_Error  LoadTrueTypeChar( int  idx, int  hint )
  {
    int  flags;


    flags = TTLOAD_SCALE_GLYPH;
    if ( hint )
      flags |= TTLOAD_HINT_GLYPH;

    return TT_Load_Glyph( instance, glyph, idx, flags );
  }


  static TT_Error  Render_All( int  first_glyph, int  ptsize )
  {
    TT_F26Dot6  start_x, start_y, step_y, x, y;
    int         i;

    TT_Error    error = 0;


    start_x = 4;
    start_y = vio_Height - ( ( ptsize * 96 + 36 ) / 72 + 10 );

    step_y = imetrics.y_ppem + 10;

    x = start_x;
    y = start_y;

    i = first_glyph;

    while ( i < num_glyphs )
    {
      if ( ( glyph_has_sbit ) &&
           ( ( sbit_flag + 1 ) & 2 )                                  &&
           !(error = TT_Load_Glyph_Bitmap( face, instance, i, sbit )) )
      {
        Blit_Bitmap( &Bit,
                     &sbit->map,
                     gray_render ? 8 : 1,
		     (int)x + (int)(sbit->metrics.horiBearingX/64),
		     Bit.rows - (int)y - (int)(sbit->metrics.horiBearingY/64),
		     gray_palette[4] );
        metrics = sbit->metrics;
        goto Step;
      }

      if ( ( ( !glyph_has_sbit ) ||
             ( ( sbit_flag + 1 ) & 1 ) )                &&
           !(error = LoadTrueTypeChar( i, hinted )) )
      {
        TT_Get_Glyph_Outline( glyph, &outline );
        TT_Get_Glyph_Big_Metrics( glyph, &metrics );

	Render_Single_Glyph( gray_render, glyph, (int)x, (int)y );
      }
      else
      {
#ifdef  Ignore_Err_Not_SBit
        if (!(( glyph_has_sbit ) &&
             ( sbit_flag == 1 ) &&
             ( error == TT_Err_Invalid_Glyph_Index )))
#endif
        Fail++;
      }
    Step:
      x += ( metrics.horiAdvance / 64 ) + 1;

      if ( x + imetrics.x_ppem > vio_Width )
      {
        x  = start_x;
        y -= step_y;

        if ( y < 10 )
            return TT_Err_Ok;
      }
      i++;
    }
    return TT_Err_Ok;
  }


  static int  Process_Event( TEvent*  event )
  {
    switch ( event->what )
    {
    case event_Quit:                                      /* ESC or q */
      return 0;

    case event_Keyboard:
      if ( event->info == 'n' )                           /* Next file */
        return 'n';
      if ( event->info == 'p' )                           /* Previous file */
        return 'p';
      if ( event->info == 'h' )                           /* Toggle hinting */
        hinted = !hinted;
      if ( ( event->info == 'B' ) && ( glyph_has_sbit ) ) /* Toggle sbit */
        sbit_flag = ( sbit_flag + 1 ) % 3;
      break;

    case event_Rotate_Glyph:
      Rotation = ( Rotation + event->info ) & 1023;
      break;

    case event_Scale_Glyph:
      ptsize += event->info;
      if ( ptsize < 1 )         ptsize = 1;
      if ( ptsize > MAXPTSIZE ) ptsize = MAXPTSIZE;
      break;

    case event_Change_Glyph:
      Num += event->info;
      if ( Num < 0 )           Num = 0;
      if ( Num >= num_glyphs ) Num = num_glyphs - 1;
      break;
    }

    return 1;
  }


  static void  usage( char*  execname )
  {
    fprintf( stderr,
          "\n"
          "ftview: simple TrueType interpreter tester -- part of the FreeType project\n"
          "--------------------------------------------------------------------------\n"
          "\n"
          "Usage: %s [options below] ppem fontname[.ttf|.ttc] ...\n",
          execname );
    fprintf( stderr,
          "\n"
          "  -g     gray-level rendering (default: none)\n"
          "  -r R   use resolution R dpi (default: 96)\n"
          "  -B     use embedded bitmaps (default: none)\n"
          "\n" );

    exit( EXIT_FAILURE );
  }


  int  main( int  argc, char**  argv )
  {
    int    i, old_ptsize, orig_ptsize, file;
    int    XisSetup = 0;
    char   filename[128 + 4];
    char   alt_filename[128 + 4];
    char*  execname;
    int    option;
    int    res = 96;

    TT_Error  error;
    TEvent    event;

    TT_EBLC   eblc_table;


    execname = ft_basename( argv[0] );

    while ( 1 )
    {
      option = ft_getopt( argc, argv, "gr:B" );

      if ( option == -1 )
        break;

      switch ( option )
      {
      case 'g':
        gray_render = 1;
        sbit_flag   = 0;
        break;

      case 'r':
        res = atoi( ft_optarg );
        if ( res < 1 )
          usage( execname );
        break;

      case 'B':
        if (!gray_render)
#if 0
          sbit_flag  = 1;
#else
          sbit_flag  = 2;
#endif
        break;

      default:
        usage( execname );
        break;
      }
    }

    argc -= ft_optind;
    argv += ft_optind;

    if ( argc <= 1 )
      usage( execname );

    if ( sscanf( argv[0], "%d", &orig_ptsize ) != 1 )
      orig_ptsize = 64;

    file = 1;

    /* Initialize engine */

    if ( (error = TT_Init_FreeType( &engine )) )
      Panic( "Error while initializing engine, code = 0x%x.\n", error );

    if ( (error = TT_Init_SBit_Extension( engine )) )
      Panic( "Error while initializing sbit extention, code = 0x%x.\n", error );

  NewFile:
    ptsize = orig_ptsize;
    hinted = 1;

    i = strlen( argv[file] );
    while ( i > 0 && argv[file][i] != '\\' && argv[file][i] != '/' )
    {
      if ( argv[file][i] == '.' )
        i = 0;
      i--;
    }

    filename[128]     = '\0';
    alt_filename[128] = '\0';

    strncpy( filename, argv[file], 128 );
    strncpy( alt_filename, argv[file], 128 );

    if ( i >= 0 )
    {
      strncpy( filename + strlen( filename ), ".ttf", 4 );
      strncpy( alt_filename + strlen( alt_filename ), ".ttc", 4 );
    }

    /* Load face */

    error = TT_Open_Face( engine, filename, &face );

    if ( error == TT_Err_Could_Not_Open_File )
    {
      strcpy( filename, alt_filename );
      error = TT_Open_Face( engine, alt_filename, &face );
    }

    if ( error == TT_Err_Could_Not_Open_File )
      Panic( "Could not find/open %s.\n", filename );
    else if ( error )
      Panic( "Error while opening %s, error code = 0x%x.\n",
             filename, error );

    /* get face properties and allocate preload arrays */

    TT_Get_Face_Properties( face, &properties );

    num_glyphs = properties.num_Glyphs;

    glyph_has_sbit = 0;

    error = TT_Get_Face_Bitmaps( face, &eblc_table );
    if ( error == TT_Err_Ok )
      glyph_has_sbit = 1;
    if ( sbit_flag && !glyph_has_sbit )
      Panic( "%s does not have embedded bitmap glyphs.\n", filename );

    /* create glyph */

    error = TT_New_Glyph( face, &glyph );
    if ( error )
      Panic( "Could not create glyph container.\n" );

    error = TT_New_SBit_Image( &sbit );
    if ( error )
      Panic( "Could not create sbit slot.\n" );

    /* create instance */

    error = TT_New_Instance( face, &instance );
    if ( error )
      Panic( "Could not create instance for %s.\n", filename );

    error = TT_Set_Instance_Resolutions( instance, res, res );
    if ( error )
      Panic( "Could not set device resolutions." );

    if ( !XisSetup )
    {
      XisSetup = 1;

      if ( gray_render )
      {
        if ( !SetGraphScreen( Graphics_Mode_Gray ) )
          Panic( "Could not set up grayscale graphics mode.\n" );

        TT_Set_Raster_Gray_Palette( engine, virtual_palette );
      }
      else
      {
        if ( !SetGraphScreen( Graphics_Mode_Mono ) )
          Panic( "Could not set up mono graphics mode.\n" );
      }
    }

    Init_Display( gray_render );

    Reset_Scale( ptsize );

    old_ptsize = ptsize;

    Fail = 0;
    Num  = 0;

    for ( ;; )
    {
      int   key;
      char* sbit_string;
      static char* sbit_messages[4] =
        { "off", "on", " if available", "NONE" };

      sbit_string = sbit_messages[3];
      if ( glyph_has_sbit )
      {
        if (sbit_flag >= 0 && sbit_flag < 3)
          sbit_string = sbit_messages[sbit_flag];
      }

      Clear_Display();
      Render_All( Num, ptsize );
      if ( gray_render )
        Convert_To_Display_Palette();

      sprintf( Header, "%s:   Glyph: %4d  ptsize: %4d  hinting: %s  sbit: %s",
                       ft_basename( filename ), Num, ptsize,
                       hinted ? "on" : "off",
                       sbit_string );
      Display_Bitmap_On_Screen( Bit.bitmap, Bit.rows, Bit.cols );

#ifndef X11
#ifndef OS2
      Print_XY( 0, 0, Header );
#endif
#endif

      Get_Event( &event );
      if ( !( key = Process_Event( &event ) ) )
        goto End;

      if ( key == 'n' )
      {
        TT_Close_Face( face );

        if ( file < argc - 1 )
          file++;

        goto NewFile;
      }

      if ( key == 'p' )
      {
        TT_Close_Face( face );

        if ( file > 1 )
          file--;

        goto NewFile;
      }

      if ( ptsize != old_ptsize )
      {
        if ( Reset_Scale( ptsize ) )
          Panic( "Could not resize font.\n" );

        old_ptsize = ptsize;
      }
    }

  End:
    RestoreScreen();

    TT_Done_SBit_Image( sbit );
    TT_Close_Face( face );
    TT_Done_FreeType( engine );

    printf( "Execution completed successfully.\n" );
    printf( "Fails = %d\n", Fail );

    exit( EXIT_SUCCESS );      /* for safety reasons */

    return 0;       /* never reached */
}


/* End */