/modules/freetype2/src/tools/test_afm.c

http://github.com/zpao/v8monkey · C · 157 lines · 115 code · 38 blank · 4 comment · 15 complexity · 74466ef4992d9efce605a8872d54736a MD5 · raw file

  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. void dump_fontinfo( AFM_FontInfo fi )
  10. {
  11. FT_Int i;
  12. printf( "This AFM is for %sCID font.\n\n",
  13. ( fi->IsCIDFont ) ? "" : "non-" );
  14. printf( "FontBBox: %.2f %.2f %.2f %.2f\n", fi->FontBBox.xMin / 65536.,
  15. fi->FontBBox.yMin / 65536.,
  16. fi->FontBBox.xMax / 65536.,
  17. fi->FontBBox.yMax / 65536. );
  18. printf( "Ascender: %.2f\n", fi->Ascender / 65536. );
  19. printf( "Descender: %.2f\n\n", fi->Descender / 65536. );
  20. if ( fi->NumTrackKern )
  21. printf( "There are %d sets of track kernings:\n",
  22. fi->NumTrackKern );
  23. else
  24. printf( "There is no track kerning.\n" );
  25. for ( i = 0; i < fi->NumTrackKern; i++ )
  26. {
  27. AFM_TrackKern tk = fi->TrackKerns + i;
  28. printf( "\t%2d: %5.2f %5.2f %5.2f %5.2f\n", tk->degree,
  29. tk->min_ptsize / 65536.,
  30. tk->min_kern / 65536.,
  31. tk->max_ptsize / 65536.,
  32. tk->max_kern / 65536. );
  33. }
  34. printf( "\n" );
  35. if ( fi->NumKernPair )
  36. printf( "There are %d kerning pairs:\n",
  37. fi->NumKernPair );
  38. else
  39. printf( "There is no kerning pair.\n" );
  40. for ( i = 0; i < fi->NumKernPair; i++ )
  41. {
  42. AFM_KernPair kp = fi->KernPairs + i;
  43. printf( "\t%3d + %3d => (%4d, %4d)\n", kp->index1,
  44. kp->index2,
  45. kp->x,
  46. kp->y );
  47. }
  48. }
  49. int
  50. dummy_get_index( const char* name,
  51. FT_Offset len,
  52. void* user_data )
  53. {
  54. if ( len )
  55. return name[0];
  56. else
  57. return 0;
  58. }
  59. FT_Error
  60. parse_afm( FT_Library library,
  61. FT_Stream stream,
  62. AFM_FontInfo fi )
  63. {
  64. PSAux_Service psaux;
  65. AFM_ParserRec parser;
  66. FT_Error error = FT_Err_Ok;
  67. psaux = (PSAux_Service)FT_Get_Module_Interface( library, "psaux" );
  68. if ( !psaux || !psaux->afm_parser_funcs )
  69. return -1;
  70. error = FT_Stream_EnterFrame( stream, stream->size );
  71. if ( error )
  72. return error;
  73. error = psaux->afm_parser_funcs->init( &parser,
  74. library->memory,
  75. stream->cursor,
  76. stream->limit );
  77. if ( error )
  78. return error;
  79. parser.FontInfo = fi;
  80. parser.get_index = dummy_get_index;
  81. error = psaux->afm_parser_funcs->parse( &parser );
  82. psaux->afm_parser_funcs->done( &parser );
  83. return error;
  84. }
  85. int main( int argc,
  86. char** argv )
  87. {
  88. FT_Library library;
  89. FT_StreamRec stream;
  90. FT_Error error = FT_Err_Ok;
  91. AFM_FontInfoRec fi;
  92. if ( argc < 2 )
  93. return FT_Err_Invalid_Argument;
  94. error = FT_Init_FreeType( &library );
  95. if ( error )
  96. return error;
  97. FT_ZERO( &stream );
  98. error = FT_Stream_Open( &stream, argv[1] );
  99. if ( error )
  100. goto Exit;
  101. stream.memory = library->memory;
  102. FT_ZERO( &fi );
  103. error = parse_afm( library, &stream, &fi );
  104. if ( !error )
  105. {
  106. FT_Memory memory = library->memory;
  107. dump_fontinfo( &fi );
  108. if ( fi.KernPairs )
  109. FT_FREE( fi.KernPairs );
  110. if ( fi.TrackKerns )
  111. FT_FREE( fi.TrackKerns );
  112. }
  113. else
  114. printf( "parse error\n" );
  115. FT_Stream_Close( &stream );
  116. Exit:
  117. FT_Done_FreeType( library );
  118. return error;
  119. }