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