freetype-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Devel] [PATCH] ftdump: printing SFNT name tables


From: Flavio Stanchina
Subject: [Devel] [PATCH] ftdump: printing SFNT name tables
Date: Sat, 24 Apr 2004 18:00:40 +0200
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040401 Debian/1.6-4

Hello,
I needed to extract some bits of information from a few TrueType fonts, so I made this little patch to ftdump. The patch adds option -n to dump the SFNT table, which includes information such as version, description, copyright etc... There is a comment in Print_Name() that says
    /* XXX: Foundry?  Copyright?  Version? ... */
and I guess this patch addresses that.

The conversions I do on Unicode characters are for my own purposes; the Right Thing would be to convert all non-ASCII characters to either U+xxxx or UTF-8, but I'll leave that as an exercise to someone who
a) knows more than I do about UTF-8
b) has some time on his hands :)

I also filled in the missing usage() information.

Patch is against ftdump.c from ft2demos 2.1.7.

--
Ciao, Flavio


diff -ur ft2demos-2.1.7.orig/src/ftdump.c ft2demos-2.1.7/src/ftdump.c
--- ft2demos-2.1.7.orig/src/ftdump.c    2003-11-02 09:58:56.000000000 +0100
+++ ft2demos-2.1.7/src/ftdump.c 2004-04-17 14:12:34.000000000 +0200
@@ -5,11 +5,17 @@
 /*  Copyright 1996-2000, 2003 by                                            */
 /*  D. Turner, R.Wilhelm, and W. Lemberg                                    */
 /*                                                                          */
+/*  Added support for printing SFNT name tables                             */
+/*  Added usage information                                                 */
+/*  Flavio Stanchina (Apr 15, 2004)                                         */
+/*                                                                          */
 /****************************************************************************/
 
 
 #include <ft2build.h>
 #include FT_FREETYPE_H
+#include FT_SFNT_NAMES_H
+#include FT_TRUETYPE_IDS_H
 
   /* the following header shouldn't be used in normal programs */
 #include FT_INTERNAL_DEBUG_H
@@ -24,6 +30,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <wchar.h>
 
 
   FT_Error  error;
@@ -31,6 +38,7 @@
   int  comma_flag  = 0;
   int  debug       = 0;
   int  trace_level = 0;
+  int  name_tables = 0;
 
 
   /* PanicZ */
@@ -60,7 +68,11 @@
     fprintf( stderr, "ftdump: simple font dumper -- part of the FreeType 
project\n" );
     fprintf( stderr, 
"-----------------------------------------------------------\n" );
     fprintf( stderr, "\n" );
-    fprintf( stderr, "Usage: %s fontname[.ttf|.ttc]\n", execname );
+    fprintf( stderr, "Usage: %s [options] fontname[.ttf|.ttc]\n", execname );
+    fprintf( stderr, "\n" );
+    fprintf( stderr, "  -d        enable debug\n" );
+    fprintf( stderr, "  -l level  trace level\n" );
+    fprintf( stderr, "  -n        print SFNT name tables\n" );
     fprintf( stderr, "\n" );
 
     exit( 1 );
@@ -145,6 +157,212 @@
     }
   }
 
+  static const char *
+  platform_id( int id )
+  {
+    switch (id)
+    {
+    case TT_PLATFORM_APPLE_UNICODE: return "Apple (Unicode)";
+    case TT_PLATFORM_MACINTOSH:     return "Macintosh";
+    case TT_PLATFORM_ISO:           return "ISO (deprecated)";
+    case TT_PLATFORM_MICROSOFT:     return "Microsoft";
+    case TT_PLATFORM_CUSTOM:        return "custom";
+    case TT_PLATFORM_ADOBE:         return "Adobe";
+
+    default: return "UNKNOWN";
+    }
+  }
+
+  static const char *
+  name_id( int id )
+  {
+    switch (id)
+    {
+    case TT_NAME_ID_COPYRIGHT:      return "copyright";
+    case TT_NAME_ID_FONT_FAMILY:    return "font family";
+    case TT_NAME_ID_FONT_SUBFAMILY: return "font subfamily";
+    case TT_NAME_ID_UNIQUE_ID:      return "unique id";
+    case TT_NAME_ID_FULL_NAME:      return "full name";
+    case TT_NAME_ID_VERSION_STRING: return "version string";
+    case TT_NAME_ID_PS_NAME:        return "PostScript name";
+    case TT_NAME_ID_TRADEMARK:      return "trademark";
+
+  /* the following values are from the OpenType spec */
+    case TT_NAME_ID_MANUFACTURER: return "manufacturer";
+    case TT_NAME_ID_DESIGNER:     return "designer";
+    case TT_NAME_ID_DESCRIPTION:  return "description";
+    case TT_NAME_ID_VENDOR_URL:   return "vendor URL";
+    case TT_NAME_ID_DESIGNER_URL: return "designer URL";
+    case TT_NAME_ID_LICENSE:      return "license";
+    case TT_NAME_ID_LICENSE_URL:  return "license URL";
+  /* number 15 is reserved */
+    case TT_NAME_ID_PREFERRED_FAMILY:    return "preferred family";
+    case TT_NAME_ID_PREFERRED_SUBFAMILY: return "preferred subfamily";
+    case TT_NAME_ID_MAC_FULL_NAME:       return "MAC full name";
+
+  /* The following code is new as of 2000-01-21 */
+    case TT_NAME_ID_SAMPLE_TEXT: return "sample text";
+
+  /* This is new in OpenType 1.3 */
+    case TT_NAME_ID_CID_FINDFONT_NAME: return "CID findfont name";
+
+    default: return "UNKNOWN";
+    }
+  }
+
+  static void
+  put_ascii( FT_Byte* string, FT_UInt string_len )
+  {
+    FT_UInt i = 0;
+
+    for ( i = 0; i < string_len; i++ )
+    {
+      switch ( string[i] )
+      {
+      case '\n': fputs( "\\n", stdout ); break;
+      case '\r': fputs( "\\r", stdout ); break;
+      case '\t': fputs( "\\t", stdout ); break;
+      case '\\': fputs( "\\\\", stdout ); break;
+
+      default: putchar( string[i] ); break;
+      }
+    }
+  }
+
+  static void
+  put_unicode_be16( FT_Byte* string, FT_UInt string_len )
+  {
+    wchar_t wc;
+    FT_UInt i = 0;
+
+    for ( i = 0; i < string_len; i += 2 )
+    {
+      wc = (string[i] << 8) | string[i + 1];
+
+      switch (wc)
+      {
+      case L'\n': fputs( "\\n", stdout ); break;
+      case L'\r': fputs( "\\r", stdout ); break;
+      case L'\t': fputs( "\\t", stdout ); break;
+      case L'\\': fputs( "\\\\", stdout ); break;
+
+      case 0x00A9: fputs( "(c)", stdout ); break;
+      case 0x00AE: fputs( "(r)", stdout ); break;
+
+      case 0x2013: fputs( "--", stdout ); break;
+      case 0x2019: fputs( "\'", stdout ); break;
+
+      case 0x2122: fputs( "(tm)", stdout ); break;
+
+      default:
+        if ( wc < 256)
+          putchar( wc );
+        else
+          printf( "\\U+%04X", (int)wc );
+        break;
+      }
+    }
+  }
+
+  void
+  Print_Sfnt_Names( FT_Face  face )
+  {
+    FT_SfntName name;
+    FT_UInt num_names, i;
+
+
+    printf( "font string entries\n" );
+
+    num_names = FT_Get_Sfnt_Name_Count( face );
+    for ( i = 0; i < num_names; i++ )
+    {
+      error = FT_Get_Sfnt_Name( face, i, &name );
+      if ( error == FT_Err_Ok )
+      {
+        printf( "   %-15s [%s]: ",
+          name_id( name.name_id ),
+          platform_id( name.platform_id ) );
+
+        switch ( name.platform_id )
+        {
+        case TT_PLATFORM_APPLE_UNICODE:
+          switch ( name.encoding_id )
+          {
+          case TT_APPLE_ID_DEFAULT:
+          case TT_APPLE_ID_UNICODE_1_1:
+          case TT_APPLE_ID_ISO_10646:
+          case TT_APPLE_ID_UNICODE_2_0:
+            put_unicode_be16( name.string, name.string_len );
+            break;
+
+          default:
+            printf( "{unsupported encoding %d}", name.encoding_id );
+            break;
+          }
+          break;
+
+        case TT_PLATFORM_MACINTOSH:
+          switch ( name.encoding_id )
+          {
+          case TT_MAC_ID_ROMAN:
+            /* FIXME: convert from MacRoman to ASCII/ISO8895-1/whatever */
+            /* (MacRoman is mostly like ISO8895-1 but there are differences) */
+            put_ascii( name.string, name.string_len );
+            break;
+
+          default:
+            printf( "{unsupported encoding %d}", name.encoding_id );
+            break;
+          }
+          if ( name.language_id != TT_MAC_LANGID_ENGLISH)
+            printf( " (language=%d)", name.language_id );
+          break;
+
+        case TT_PLATFORM_ISO:
+          switch ( name.encoding_id )
+          {
+          case TT_ISO_ID_7BIT_ASCII:
+          case TT_ISO_ID_8859_1:
+            put_ascii( name.string, name.string_len );
+            break;
+
+          case TT_ISO_ID_10646:
+            put_unicode_be16( name.string, name.string_len );
+            break;
+
+          default:
+            printf( "{unsupported encoding %d}", name.encoding_id );
+            break;
+          }
+          break;
+
+        case TT_PLATFORM_MICROSOFT:
+          switch ( name.encoding_id )
+          {
+          /* I'm not sure if TT_MS_ID_SYMBOL_CS is supposed to be Unicode,
+           * but the only example I have (symbol.ttf) seems to be. */
+          case TT_MS_ID_SYMBOL_CS:
+          case TT_MS_ID_UNICODE_CS:
+            put_unicode_be16( name.string, name.string_len );
+            break;
+
+          default:
+            printf( "{unsupported encoding %d}", name.encoding_id );
+            break;
+          }
+          if ( name.language_id != TT_MS_LANGID_ENGLISH_UNITED_STATES )
+            printf( " (language=0x%04x)", name.language_id );
+          break;
+
+        default:
+          printf( "{unsupported platform}" );
+          break;
+        }
+
+        printf( "\n" );
+      }
+    }
+  }
 
   void
   Print_Fixed( FT_Face  face )
@@ -208,7 +426,7 @@
 
     while ( 1 )
     {
-      option = getopt( argc, argv, "dl:" );
+      option = getopt( argc, argv, "dl:n" );
 
       if ( option == -1 )
         break;
@@ -225,6 +443,10 @@
           usage( execname );
         break;
 
+      case 'n':
+        name_tables = 1;
+        break;
+
       default:
         usage( execname );
         break;
@@ -318,6 +540,12 @@
       printf( "\n" );
       Print_Type( face );
 
+      if ( name_tables && FT_IS_SFNT( face ) )
+      {
+        printf( "\n" );
+        Print_Sfnt_Names( face );
+      }
+
       if ( face->num_fixed_sizes )
       {
         printf( "\n" );

reply via email to

[Prev in Thread] Current Thread [Next in Thread]