home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: SysTools / SysTools.zip / ft-beta.zip / freetype / test / ftdump.c < prev    next >
C/C++ Source or Header  |  1997-10-06  |  11KB  |  418 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*  The FreeType project -- a free and portable quality TrueType renderer.  */
  4. /*                                                                          */
  5. /*  Copyright 1996, 1997 by                                                 */
  6. /*  D. Turner, R.Wilhelm, and W. Lemberg                                    */
  7. /*                                                                          */
  8. /*  ftdump.c : Simple TrueType font file resource profiler                  */
  9. /*                                                                          */
  10. /*  This program dumps various properties of a given font file              */
  11. /*                                                                          */
  12. /*                                                                          */
  13. /*                                                                          */
  14. /*  NOTE:  This is just a test program that is used to show off and         */
  15. /*         debug the current engine; which is still in alpha.               */
  16. /*                                                                          */
  17. /****************************************************************************/
  18.  
  19. #ifdef ARM
  20. #include "std.h"
  21. #include "graflink.h"
  22. #endif
  23.  
  24. #include "freetype.h"
  25. #include "tterror.h"  /* for Panic() */
  26.  
  27. #include "ttobjs.h"   /* We're going to access internal tables directly */
  28.  
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #include <string.h>
  32.  
  33. #ifdef ARM
  34. #include "armsup.c" /* pull in our routines */
  35. #endif
  36.  
  37.   TT_Error     error;
  38.  
  39.   TT_Face      face;
  40.   TT_Instance  instance;
  41.   TT_Glyph     glyph;
  42.  
  43.   TT_Instance_Metrics  imetrics;
  44.   TT_Glyph_Outline     outline;
  45.   TT_Glyph_Metrics     metrics;
  46.  
  47.   TT_Face_Properties   properties;
  48.  
  49.   TT_CharMap    cmap;
  50.  
  51.   int  num_glyphs;
  52.   int  ptsize;
  53.  
  54.   int  Fail;
  55.   int  Num;
  56.  
  57.  
  58.   int  flag_memory    = 1;
  59.   int  flag_names     = 1;
  60.   int  flag_encodings = 1;
  61.  
  62. extern long TTMemory_Allocated;
  63.  
  64. long   org_memory, old_memory, cur_memory;
  65.   
  66.   const char* Apple_Encodings[33] =
  67.   {
  68.     "Roman", "Japanese", "Chinese", "Korean", "Arabic", "Hebrew",
  69.     "Greek", "Russian", "RSymbol", "Devanagari", "Gurmukhi",
  70.     "Gujarati", "Oriya", "Bengali", "Tamil", "Belugu", "Kannada",
  71.     "Malayalam", "Sinhalese", "Burmese", "Khmer", "Tai", "Laotian",
  72.     "Georgian", "Armenian", "Maldivian", "Tibetan", "Mongolian",
  73.     "Geez", "Slavic", "Vietnamese", "Sindhi", "Uninterp"
  74.   };
  75.  
  76.   struct
  77.   {
  78.     long  initial_overhead;
  79.     long  face_object;
  80.     long  glyph_object;
  81.     long  first_instance;
  82.     long  second_instance;
  83.  
  84.   } memory_footprint;
  85.  
  86.  
  87.   void  Save_Memory( long* var )
  88.   {
  89.     *var = TTMemory_Allocated - old_memory;
  90.     old_memory += *var;
  91.   }
  92.  
  93.   #define FOOTPRINT(field)  Save_Memory( &memory_footprint.##field )
  94.  
  95.   static void  
  96.   Print_Mem( long val, char* string )
  97.   {
  98.     Message( "%6d bytes (%4d Kb) :  %s\n",
  99.              val,
  100.              (val+1023)/1024,
  101.              string );
  102.   }
  103.  
  104.   #define PRINT_MEM(field,string)  \
  105.             Print_Mem( memory_footprint.##field, string )
  106.  
  107.  
  108.   /* Print the memory footprint */
  109.  
  110.   void  Print_Memory()
  111.   {
  112.     /* create glyph */
  113.     error = TT_New_Glyph( face, &glyph );
  114.     if ( error )
  115.       Panic( "ERROR: could not create glyph container\n" );
  116.  
  117.     FOOTPRINT(glyph_object);
  118.  
  119.     /* create instance */
  120.     error = TT_New_Instance( face, &instance );
  121.     if ( error )
  122.       Panic( "ERROR: could not create instance\n" );
  123.  
  124.     FOOTPRINT(first_instance);
  125.  
  126.     error = TT_New_Instance( face, &instance );
  127.     if ( error )
  128.       Panic( "ERROR: could not create 2nd instance" );
  129.  
  130.     FOOTPRINT(second_instance);
  131.  
  132.     Message( "Memory footprint statistics :\n" );
  133.     Message( "-----------------------------------------------\n" );
  134.  
  135.     /* NOTE : In our current implementation, the face's execution */
  136.     /*        context object is created lazily with the first     */
  137.     /*        instance. However, all later instances share the    */
  138.     /*        the same context..                                  */
  139.  
  140.     PRINT_MEM(face_object,     "face object"     );
  141.     PRINT_MEM(glyph_object,    "glyph_object"    );
  142.     PRINT_MEM(second_instance, "instance object" );
  143.  
  144.     Print_Mem( memory_footprint.first_instance - 
  145.                memory_footprint.second_instance, "exec. context object" );
  146.  
  147.     Message( "-----------------------------------------------\n" );
  148.     Print_Mem( memory_footprint.face_object    +
  149.                memory_footprint.glyph_object   +
  150.                memory_footprint.first_instance,
  151.                "total memory use" );
  152.  
  153.     Message( "\n" );
  154.   }
  155.  
  156.   static  char  name_buffer[257];
  157.   static  int   name_len = 0;
  158.  
  159.  
  160.   static char*  LookUp_Name( int index )
  161.   {
  162.     int i, j, n;
  163.  
  164.     short  platform, encoding, language, id;
  165.     char*  string;
  166.     int    string_len;
  167.  
  168.     int    found;
  169.  
  170.     n = TT_Get_Name_Count( face );
  171.     if ( n < 0 )
  172.       return NULL;
  173.  
  174.     for ( i = 0; i < n; i++ )
  175.     {
  176.       TT_Get_Name_ID( face, i, &platform, &encoding, &language, &id );
  177.       TT_Get_Name_String( face, i, &string, &string_len );
  178.  
  179.       if ( id == index )
  180.       {
  181.  
  182.    /* The following code was inspired from Mark Leisher's ttf2bdf package */
  183.  
  184.         found = 0;
  185.  
  186.         /* Try to find a Microsoft English name */
  187.  
  188.         if ( platform == 3 )
  189.           for ( j = 1; j >= 0; j-- )
  190.             if ( encoding == j )  /* Microsoft ? */
  191.               switch (language)
  192.               {
  193.                 case 0x409:
  194.                 case 0x809:
  195.                 case 0xc09:
  196.                 case 0x1009:
  197.                 case 0x1409:
  198.                 case 0x1809: found = 1;
  199.                              break;
  200.               }
  201.  
  202.         if ( !found && platform == 0 && language == 0 )
  203.           found = 1;
  204.  
  205.         /*   
  206.          * Found a Unicode Name.                                       
  207.          */
  208.         if (found)
  209.         {
  210.           if ( string_len > 512 )
  211.             string_len = 512;
  212.   
  213.           name_len = 0;
  214.   
  215.           for ( i = 1; i < string_len; i += 2 ) 
  216.             name_buffer[name_len++] = string[i];
  217.   
  218.           name_buffer[name_len] = '\0';
  219.   
  220.           return name_buffer;
  221.         }
  222.       }
  223.     }
  224.  
  225.     /* Not found */
  226.     return NULL;
  227.   }
  228.  
  229.  
  230.   static void 
  231.   Print_Names()
  232.   {
  233.      Message( "font name table entries\n" );
  234.      Message( "-----------------------------------------------\n" );
  235.  
  236.      if ( LookUp_Name( 4 ) )
  237.        Message( "%s - ", name_buffer );
  238.  
  239.      if ( LookUp_Name( 5 ) )
  240.        Message( "%s\n\n", name_buffer );
  241.  
  242.      if ( LookUp_Name( 0 ) )
  243.        Message( "%s\n\n", name_buffer );
  244.  
  245.      if ( LookUp_Name( 7 ) )
  246.        Message( name_buffer );
  247.  
  248.      Message( "\n" );
  249.      Message( "-----------------------------------------------\n\n" );
  250.   }
  251.  
  252.  
  253.   static void
  254.   Print_Encodings()
  255.   {
  256.      int n, i;
  257.      short platform, encoding;
  258.      char  *platStr, *encoStr;
  259.  
  260.      char  tempStr[128];
  261.  
  262.      Message( "character map encodings\n" );
  263.      Message( "-----------------------------------------------\n" );
  264.  
  265.      n = TT_Get_CharMap_Count( face );
  266.      if ( n < 0 )
  267.      {
  268.        Message( "The file doesn't seem to have any encoding table\n" );
  269.        return;
  270.      }
  271.  
  272.      Message( "There are %d encodings :\n\n", n );
  273.  
  274.      for ( i = 0; i < n; i++ )
  275.      {
  276.        TT_Get_CharMap_ID( face, i, &platform, &encoding );
  277.        Message( "encoding %2d : ", i );
  278.  
  279.        platStr = encoStr = NULL;
  280.  
  281.        switch (platform)
  282.        {
  283.          case 0 :  platStr = "Apple";
  284.                    encoStr = "Unicode";
  285.                    break;
  286.  
  287.          case 1 :  platStr = "Apple";
  288.                    if (encoding < 0 || encoding > 32)
  289.                    {
  290.                      sprintf( tempStr, "Unknown %d", encoding );
  291.                      encoStr = tempStr;
  292.                    }
  293.                    else
  294.                      encoStr = (char*)Apple_Encodings[encoding];
  295.                    break;
  296.  
  297.          case 2 : platStr = "Iso";
  298.                   sprintf( tempStr, "%d", encoding );
  299.                   encoStr = tempStr;
  300.                   break;
  301.  
  302.          case 3 : platStr = "Windows";
  303.                   switch (encoding)
  304.                   {
  305.                     case 0: encoStr = "Symbol";
  306.                             break;
  307.  
  308.                     case 1: encoStr = "Unicode";
  309.                             break;
  310.  
  311.                     case 4: encoStr = "WGL4";
  312.                             break;
  313.  
  314.                     default: sprintf( tempStr, "unknown %d", encoding );
  315.                              encoStr = tempStr;
  316.                   }
  317.                   break;
  318.  
  319.          default: sprintf( tempStr, "%d - %d", platform, encoding );
  320.                   platStr = "Unknown";
  321.                   encoStr = tempStr;
  322.        }
  323.  
  324.        Message( "%s %s\n", platStr, encoStr );
  325.      }
  326.  
  327.      Message( "\n" );
  328.      Message( "-----------------------------------------------\n\n" );
  329.   }
  330.  
  331.  
  332.   int  main( int  argc, char**  argv ) 
  333.   {
  334.     int    i;
  335.     char   filename[128 + 4];
  336.     char   alt_filename[128 + 4];
  337.     char*  execname;
  338.  
  339.     execname = argv[0];
  340.  
  341.     if ( argc != 2 )
  342.     {
  343.       Message( "ftdump: simple TrueType Dumper - part of the FreeType project\n" );
  344.       Message( "-------------------------------------------------------------\n\n" );
  345.       Message( "Usage: %s fontname[.ttf|.ttc]\n\n", execname );
  346.       exit( 1 );
  347.     }
  348.  
  349.     i = strlen( argv[1] );
  350.     while ( i > 0 && argv[1][i] != '\\' )
  351.     {
  352.       if ( argv[1][i] == '.' )
  353.         i = 0;
  354.       i--;
  355.     }
  356.  
  357.     filename[128] = '\0';
  358.     alt_filename[128] = '\0';
  359.  
  360.     strncpy( filename, argv[1], 128 );
  361.     strncpy( alt_filename, argv[1], 128 );
  362.  
  363.     if ( i >= 0 )
  364.     {
  365.       strncpy( filename + strlen( filename ), ".ttf", 4 );
  366.       strncpy( alt_filename + strlen( alt_filename ), ".ttc", 4 );
  367.     }
  368.  
  369.     /* Initialize engine */
  370.  
  371.     old_memory = 0;
  372.  
  373.     if ( (error = TT_Init_FreeType()) )
  374.       Panic( "ERROR: While initializing engine, code = %d\n", error );
  375.  
  376.     FOOTPRINT(initial_overhead);
  377.   
  378.     /* Open and Load face */
  379.   
  380.     error = TT_Open_Face( filename, &face );
  381.  
  382.     if (error == TT_Err_Could_Not_Open_File)
  383.     {
  384.       strcpy( filename, alt_filename );
  385.       error = TT_Open_Face( alt_filename, &face );
  386.     }
  387.  
  388.     if ( error )
  389.       Panic( "ERROR: could not find/open %s\n", filename );
  390.  
  391.     FOOTPRINT(face_object);
  392.  
  393.     /* get face properties and allocate preload arrays */
  394.  
  395.     TT_Get_Face_Properties( face, &properties );
  396.     num_glyphs = properties.num_Glyphs;
  397.  
  398.     /* Now do various dumps */
  399.  
  400.     if (flag_names)
  401.       Print_Names();
  402.  
  403.     if (flag_encodings)
  404.       Print_Encodings();
  405.  
  406.     if (flag_memory)
  407.       Print_Memory();
  408.         
  409.     TT_Close_Face( face );
  410.  
  411.     TT_Done_FreeType();
  412.  
  413.     return 0;
  414. }
  415.  
  416.  
  417. /* End */
  418.