misc/libfreetype/src/tools/test_afm.c
author koda
Mon, 25 Apr 2011 01:46:54 +0200
changeset 5172 88f2e05288ba
permissions -rw-r--r--
aaand let's add freetype as well while we are at it other smaller changes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
5172
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     1
/*
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     2
 * gcc -DFT2_BUILD_LIBRARY -I../../include -o test_afm test_afm.c \
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     3
 *     -L../../objs/.libs -lfreetype -lz -static
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     4
 */
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     5
#include <ft2build.h>
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     6
#include FT_FREETYPE_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     7
#include FT_INTERNAL_STREAM_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     8
#include FT_INTERNAL_POSTSCRIPT_AUX_H
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
     9
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    10
  void dump_fontinfo( AFM_FontInfo  fi )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    11
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    12
    FT_Int  i;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    13
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    14
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    15
    printf( "This AFM is for %sCID font.\n\n",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    16
            ( fi->IsCIDFont ) ? "" : "non-" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    17
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    18
    printf( "FontBBox: %.2f %.2f %.2f %.2f\n", fi->FontBBox.xMin / 65536.,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    19
                                               fi->FontBBox.yMin / 65536.,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    20
                                               fi->FontBBox.xMax / 65536.,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    21
                                               fi->FontBBox.yMax / 65536. );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    22
    printf( "Ascender: %.2f\n", fi->Ascender / 65536. );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    23
    printf( "Descender: %.2f\n\n", fi->Descender / 65536. );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    24
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    25
    if ( fi->NumTrackKern )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    26
      printf( "There are %d sets of track kernings:\n",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    27
              fi->NumTrackKern );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    28
    else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    29
      printf( "There is no track kerning.\n" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    30
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    31
    for ( i = 0; i < fi->NumTrackKern; i++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    32
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    33
      AFM_TrackKern  tk = fi->TrackKerns + i;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    34
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    35
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    36
      printf( "\t%2d: %5.2f %5.2f %5.2f %5.2f\n", tk->degree,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    37
                                                  tk->min_ptsize / 65536.,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    38
                                                  tk->min_kern / 65536.,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    39
                                                  tk->max_ptsize / 65536.,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    40
                                                  tk->max_kern / 65536. );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    41
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    42
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    43
    printf( "\n" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    44
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    45
    if ( fi->NumKernPair )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    46
      printf( "There are %d kerning pairs:\n",
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    47
              fi->NumKernPair );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    48
    else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    49
      printf( "There is no kerning pair.\n" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    50
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    51
    for ( i = 0; i < fi->NumKernPair; i++ )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    52
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    53
      AFM_KernPair  kp = fi->KernPairs + i;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    54
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    55
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    56
      printf( "\t%3d + %3d => (%4d, %4d)\n", kp->index1,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    57
                                             kp->index2,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    58
                                             kp->x,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    59
                                             kp->y );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    60
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    61
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    62
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    63
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    64
  int
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    65
  dummy_get_index( const char*  name,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    66
                   FT_Offset    len,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    67
                   void*        user_data )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    68
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    69
    if ( len )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    70
      return name[0];
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    71
    else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    72
      return 0;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    73
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    74
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    75
  FT_Error
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    76
  parse_afm( FT_Library    library,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    77
             FT_Stream     stream,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    78
             AFM_FontInfo  fi )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    79
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    80
    PSAux_Service  psaux;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    81
    AFM_ParserRec  parser;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    82
    FT_Error       error = FT_Err_Ok;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    83
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    84
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    85
    psaux = (PSAux_Service)FT_Get_Module_Interface( library, "psaux" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    86
    if ( !psaux || !psaux->afm_parser_funcs )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    87
      return -1;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    88
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    89
    error = FT_Stream_EnterFrame( stream, stream->size );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    90
    if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    91
      return error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    92
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    93
    error = psaux->afm_parser_funcs->init( &parser,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    94
                                           library->memory,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    95
                                           stream->cursor,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    96
                                           stream->limit );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    97
    if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    98
      return error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
    99
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   100
    parser.FontInfo = fi;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   101
    parser.get_index = dummy_get_index;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   102
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   103
    error = psaux->afm_parser_funcs->parse( &parser );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   104
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   105
    psaux->afm_parser_funcs->done( &parser );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   106
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   107
    return error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   108
  }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   109
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   110
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   111
  int main( int    argc,
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   112
            char** argv )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   113
  {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   114
    FT_Library       library;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   115
    FT_StreamRec     stream;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   116
    FT_Error         error = FT_Err_Ok;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   117
    AFM_FontInfoRec  fi;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   118
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   119
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   120
    if ( argc < 2 )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   121
      return FT_Err_Invalid_Argument;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   122
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   123
    error = FT_Init_FreeType( &library );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   124
    if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   125
      return error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   126
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   127
    FT_ZERO( &stream );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   128
    error = FT_Stream_Open( &stream, argv[1] );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   129
    if ( error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   130
      goto Exit;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   131
    stream.memory = library->memory;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   132
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   133
    FT_ZERO( &fi );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   134
    error = parse_afm( library, &stream, &fi );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   135
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   136
    if ( !error )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   137
    {
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   138
      FT_Memory  memory = library->memory;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   139
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   140
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   141
      dump_fontinfo( &fi );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   142
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   143
      if ( fi.KernPairs )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   144
        FT_FREE( fi.KernPairs );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   145
      if ( fi.TrackKerns )
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   146
        FT_FREE( fi.TrackKerns );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   147
    }
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   148
    else
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   149
      printf( "parse error\n" );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   150
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   151
    FT_Stream_Close( &stream );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   152
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   153
  Exit:
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   154
    FT_Done_FreeType( library );
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   155
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   156
    return error;
88f2e05288ba aaand let's add freetype as well while we are at it
koda
parents:
diff changeset
   157
  }