summaryrefslogtreecommitdiffstats
path: root/kernel/kls_ttf/ttf2pnm.cpp
blob: 45717311b7927959dccd9858f5012024fc70f88a (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
/****************************************************************************/
/*                                                                          */
/*  The FreeType project -- a free and portable quality TrueType renderer.  */
/*                                                                          */
/*  Copyright 1996-2000, 2003, 2004, 2005, 2006 by                          */
/*  D. Turner, R.Wilhelm, and W. Lemberg                                    */
/*                                                                          */
/*                                                                          */
/*  FTView - a simple font viewer.                                          */
/*                                                                          */
/*  This is a new version using the MiGS graphics subsystem for             */
/*  blitting and display.                                                   */
/*                                                                          */
/*  Press F1 when running this program to have a list of key-bindings       */
/*                                                                          */
/****************************************************************************/

#include <freetype/config/ftheader.h>

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

#include "ftcommon.h"
#include <math.h>

  /* the following header shouldn't be used in normal programs */
#include FT_SYNTHESIS_H

#define MAXPTSIZE      500                 /* dtp */

#ifdef CEIL
#undef CEIL
#endif
#define CEIL( x )   ( ( (x) + 63 ) >> 6 )

#define INIT_SIZE( size, start_x, start_y, step_x, step_y, x, y )        \
          do {                                                           \
            start_x = 4;                                                 \
            start_y = CEIL( size->metrics.height );                      \
            step_x  = CEIL( size->metrics.max_advance );                 \
            step_y  = CEIL( size->metrics.height ) + 4;                  \
                                                                         \
            x = start_x;                                                 \
            y = start_y;                                                 \
          } while ( 0 )

#define X_TOO_LONG( x, size, bitmap) \
          ( ( x ) + ( ( size )->metrics.max_advance >> 6 ) > bitmap->width )

#define Y_TOO_LONG( y, size, bitmap) \
          ( ( y ) >= bitmap->rows )

grBitmap *bit;

  static struct  status_
  {
    FT_Encoding  encoding;
    int          res;
    int          ptsize;            /* current point size */

    int          font_index;
    int          Num;               /* current first index */
    int          Fail;

  } status = { FT_ENCODING_NONE, 72, 24, 0, 0, 0 };

  static FTDemo_Handle*   handle;

  static FT_Error
  Render_All( int  num_indices,
              int  first_index )
  {
    int         start_x, start_y, step_x, step_y, x, y;
    int         i;
    FT_Size     size;

    error = FTDemo_Get_Size(handle, &size);

    if ( error )
    {
      /* probably a non-existent bitmap font size */
      return error;
    }

    INIT_SIZE( size, start_x, start_y, step_x, step_y, x, y );

    i = first_index;

    while ( i < num_indices )
    {
      int  gindex;

      if ( handle->encoding == FT_ENCODING_NONE )
        gindex = i;
      else
        gindex = FTDemo_Get_Index( handle, i );

      error = FTDemo_Draw_Index( handle, bit, gindex, &x, &y );

      if ( error )
        status.Fail++;
      else if ( X_TOO_LONG( x, size, bit ) )
      {
        x  = start_x;
        y += step_y;

        if ( Y_TOO_LONG( y, size, bit ) )
          break;
      }

      i++;
    }

    return FT_Err_Ok;
  }


  /*************************************************************************/
  /*************************************************************************/
  /*****                                                               *****/
  /*****                REST OF THE APPLICATION/PROGRAM                *****/
  /*****                                                               *****/
  /*************************************************************************/
  /*************************************************************************/

  static void
  event_font_change()
  {
    int      num_indices;

    status.font_index = 0;
    status.Num = 0;

    FTDemo_Set_Current_Font(handle, handle->fonts[status.font_index]);
    FTDemo_Set_Current_Pointsize(handle, status.ptsize, status.res);
    FTDemo_Update_Current_Flags(handle);

    num_indices = handle->current_font->num_indices;

    if(status.Num >= num_indices)
      status.Num = num_indices - 1;
  }


  int
  main( int    argc,
        char*  argv[] )
  {
    if(argc != 3)
        exit(1);

    /* Initialize engine */
    handle = FTDemo_New(status.encoding);

    FTDemo_Install_Font(handle, argv[1]);

    if(handle->num_fonts == 0)
      PanicZ( "could not find/open any font file" );

    bit = FTDemo_Display_New();

    if(!bit)
      PanicZ( "could not allocate display surface" );

    status.Fail = 0;

    event_font_change();

    FTDemo_Update_Current_Flags(handle);

    FTDemo_Display_Clear(bit);

    Render_All(handle->current_font->num_indices, status.Num);

    FILE *f = fopen(argv[2], "wb");

    fprintf(f, "P6\n%d %d\n255\n", bit->width, bit->rows);
    fwrite(bit->buffer, bit->width * bit->rows * 3, 1, f);

    fclose(f);

    FTDemo_Display_Done(bit);
    FTDemo_Done(handle);

    return 0;
  }