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