home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 15 / 15.iso / s / s076 / 4.img / DEMO / TTFNAME.C < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-30  |  3.7 KB  |  129 lines

  1. /* 
  2.  *    An example TrueType font file name parsing program.
  3.  *
  4.  *    Copyright 1992 Microsoft Corporation.
  5.  *
  6.  *    Although this example code is parsing the 'name' table of
  7.  *     a TrueType font, it could be readily adapted to parse any
  8.  *    other table.  See the "sfnt.h" include file for the 
  9.  *    predefined structures for the other tables in a TrueType 
  10.  *    font.  For example, the 'post' table is referenced by the
  11.  *    "sfnt_PostScriptInfo" structure.
  12.  *
  13.  *    This code could also be useful for applications which
  14.  *    wish to directly parse a TrueType font under Windows.
  15.  *    Since Windows 3.1 allows an application to gain access
  16.  *    to the raw font file via the GetFontData() API, this 
  17.  *    example code may be useful.
  18.  *
  19.  */
  20.  
  21. #define        PC_OS
  22.  
  23. #include    <stdio.h>
  24. #include    <fcntl.h>
  25. #include    <io.h>
  26. #include    <errno.h>
  27. #include    <dos.h>
  28. #include    "fscdefs.h"
  29. #include    "sfnt.h"
  30.  
  31. void DumpNameTable (char *);
  32.  
  33. main(int argc, char **argv) 
  34. {
  35.     struct find_t    FileInfo;
  36.  
  37.     if (argc < 2) {
  38.         printf("usage: ttfname *.ttf\n");
  39.         exit(-1);
  40.     }
  41.  
  42.     _dos_findfirst( argv[1], _A_NORMAL, &FileInfo );
  43.     do
  44.         DumpNameTable( FileInfo.name );
  45.     while ( !_dos_findnext( &FileInfo ) );
  46. }
  47.  
  48.  
  49.   // find and seek to the naming table
  50. void DumpNameTable (char * pszFile)
  51. {
  52.   unsigned            i;
  53.   char              namebuf[255];
  54.   int                 fp;
  55.   unsigned short      numNames;
  56.   long              curseek;
  57.   unsigned            cTables;
  58.   sfnt_OffsetTable    OffsetTable;
  59.   sfnt_DirectoryEntry Table;
  60.   sfnt_NamingTable    NamingTable;
  61.   sfnt_NameRecord     NameRecord;
  62.  
  63.   if ((fp = open (pszFile, O_RDONLY | O_BINARY)) == -1)
  64.     return;
  65.  
  66.   /* First off, read the initial directory header on the TTF.  We're only
  67.    * interested in the "numOffsets" variable to tell us how many tables
  68.    * are present in this file.  
  69.    *
  70.    * Remember to always convert from Motorola format (Big Endian to 
  71.    * Little Endian).
  72.    */
  73.   read (fp, &OffsetTable, sizeof (OffsetTable) - sizeof (sfnt_DirectoryEntry));
  74.   cTables = (int) SWAPW (OffsetTable.numOffsets);
  75.  
  76.   for ( i = 0; i < cTables && i < 40; i++)
  77.   {
  78.     if ((read (fp, &Table, sizeof (Table))) != sizeof(Table)) {
  79.     printf("Read failed on table #%d\n", i);
  80.     exit(-1);
  81.     }
  82.     if (Table.tag == tag_NamingTable)    /* defined in sfnt_en.h */
  83.     {
  84.     /* Now that we've found the entry for the name table, seek to that
  85.      * position in the file and read in the initial header for this
  86.      * particular table.  See "True Type Font Files" for information
  87.      * on this record layout.
  88.      */
  89.     lseek (fp, SWAPL (Table.offset), SEEK_SET);
  90.     read (fp, &NamingTable, sizeof (NamingTable));
  91.     numNames = SWAPW(NamingTable.count);
  92.     while (numNames--) {
  93.         read (fp, &NameRecord, sizeof (NameRecord));
  94.         curseek = tell(fp);
  95.  
  96. /* Undefine this next section if you'd like a little bit more info 
  97.  * during the parsing of this particular name table.
  98.  */
  99. #ifdef SPAM 
  100. printf("(%ld) platform=%u, specific=%u, lang=%x, name=%u (%u, %u)\n", curseek,
  101.         SWAPW(NameRecord.platformID),
  102.         SWAPW(NameRecord.specificID),
  103.         SWAPW(NameRecord.languageID),
  104.         SWAPW(NameRecord.nameID),
  105.         SWAPW(NameRecord.length),
  106.         SWAPW(NameRecord.offset));
  107. #endif
  108.         if (SWAPW(NameRecord.platformID) == 1 &&
  109.                 SWAPW(NameRecord.nameID) == 4) {
  110.             lseek (fp, SWAPW (NameRecord.offset) + 
  111.                 SWAPW(NamingTable.stringOffset) + 
  112.                 SWAPL(Table.offset), SEEK_SET);
  113.             read (fp, &namebuf, SWAPW(NameRecord.length));
  114.             namebuf[SWAPW(NameRecord.length)] = '\0';
  115.             printf("%s: FullFontName = %s\n", pszFile, namebuf);
  116.             lseek (fp, curseek, SEEK_SET);
  117.         }
  118.     }
  119.     goto cleanup;
  120.     }
  121.  
  122.   }
  123.   printf("%s: ** No name table found **\n", pszFile);
  124.  
  125. cleanup:
  126.   close (fp);
  127.   return;
  128. }
  129.