]> git.donarmstrong.com Git - lilypond.git/blobdiff - ttftool/parse.c
*** empty log message ***
[lilypond.git] / ttftool / parse.c
index e6becb5586e6a818cf9328c39c3c5b352bea83b9..cf43152b073f9437c5fa3ebea41a76870f50e797 100644 (file)
@@ -4,20 +4,25 @@
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+
 #include "types.h"
 #include "proto.h"
+#include "ttftool.h"
 
 struct TableDirectoryEntry *
-readDirectory (int fd, struct OffsetTable *ot)
+readDirectory (FILE *fd, struct OffsetTable *ot)
 {
   unsigned n;
   int i;
 
   struct TableDirectoryEntry *td;
+  
   surely_read (fd, ot, sizeof (struct OffsetTable));
   FIX_OffsetTable (*ot);
-  if (verbosity >= 2)
+  if (ttf_verbosity >= 2)
     fprintf (stderr, "%d tables\n", ot->numTables);
+
   n = sizeof (struct TableDirectoryEntry) * ot->numTables;
   td = mymalloc (n);
   surely_read (fd, td, n);
@@ -27,7 +32,7 @@ readDirectory (int fd, struct OffsetTable *ot)
 }
 
 char **
-readNamingTable (int fd)
+readNamingTable (FILE *fd)
 {
   USHORT format;
   USHORT nrecords;
@@ -38,7 +43,7 @@ readNamingTable (int fd)
   char *data;
   char **strings;
 
-  position = surely_lseek (fd, 0, SEEK_CUR);
+  position = ftell (fd);
 
   surely_read (fd, &format, sizeof (USHORT));
   FIX_UH (format);
@@ -77,7 +82,7 @@ readNamingTable (int fd)
          strings[records[i].nameID] = mymalloc (records[i].length / 2 + 1);
          unistrncpy (strings[records[i].nameID],
                      data + records[i].offset, records[i].length);
-         if (verbosity >= 2)
+         if (ttf_verbosity >= 2)
            fprintf (stderr, "%d: %s\n", records[i].nameID,
                     strings[records[i].nameID]);
        }
@@ -97,7 +102,7 @@ readNamingTable (int fd)
          strncpy (strings[id],
                   data + records[i].offset, records[i].length);
          strings[id][records[i].length] = 0;
-         if (verbosity >= 2)
+         if (ttf_verbosity >= 2)
            fprintf (stderr, "%d: %s\n", records[i].nameID,
                     strings[records[i].nameID]);
        }
@@ -109,7 +114,7 @@ readNamingTable (int fd)
 }
 
 int
-readMaxpTable (int fd)
+readMaxpTable (FILE *fd)
 {
   struct
   {
@@ -119,18 +124,18 @@ readMaxpTable (int fd)
   surely_read (fd, &data, sizeof (data));
   FIX_Fixed (data.version);
   FIX_UH (data.nglyphs);
-  if (verbosity >= 2)
+  if (ttf_verbosity >= 2)
     fprintf (stderr, "  version %d.%u\n",
             data.version.mantissa, data.version.fraction);
   return data.nglyphs;
 }
 
 void
-readHeadTable (int fd, struct HeadTable *ht)
+readHeadTable (FILE *fd, struct HeadTable *ht)
 {
   surely_read (fd, ht, sizeof (struct HeadTable));
   FIX_HeadTable (*ht);
-  if (verbosity >= 2)
+  if (ttf_verbosity >= 2)
     {
       fprintf (stderr, "  version %d.%d\n",
               ht->version.mantissa, ht->version.fraction);
@@ -139,15 +144,15 @@ readHeadTable (int fd, struct HeadTable *ht)
     }
   if (ht->magicNumber != 0x5F0F3CF5)
     ttf_error ("Bad magic number");
-  if (verbosity >= 2)
+  if (ttf_verbosity >= 2)
     fprintf (stderr, "  %d units per Em\n", ht->unitsPerEm);
 }
 
 int
-readPostTable (int fd, int nglyphs, struct PostTable *pt,
+readPostTable (FILE *fd, int root_nglyphs, struct PostTable *pt,
               struct GlyphName **gt)
 {
-  USHORT nglyphspost;
+  USHORT nglyphs;
   USHORT *glyphNameIndex;
   struct GlyphName *glyphNames;
   char **glyphNamesTemp;
@@ -156,7 +161,7 @@ readPostTable (int fd, int nglyphs, struct PostTable *pt,
 
   surely_read (fd, pt, sizeof (struct PostTable));
   FIX_PostTable (*pt);
-  if (verbosity >= 2)
+  if (ttf_verbosity >= 2)
     fprintf (stderr, "  format type %d.%u\n",
             pt->formatType.mantissa, pt->formatType.fraction);
 
@@ -167,11 +172,21 @@ readPostTable (int fd, int nglyphs, struct PostTable *pt,
     case 2:
       if (pt->formatType.fraction != 0)
        ttf_error ("Unsupported `post' table format");
-      surely_read (fd, &nglyphspost, sizeof (USHORT));
-      FIX_UH (nglyphspost);
-      if (nglyphspost != nglyphs)
-       ttf_error ("Inconsistency between `maxp' and `nglyphs' tables!");
-      if (verbosity >= 2)
+      surely_read (fd, &nglyphs, sizeof (USHORT));
+
+      /*from freetype2:
+       */
+      /*
+       UNDOCUMENTED!  The number of glyphs in this table can be smaller
+       than the value in the maxp table (cf. cyberbit.ttf).             
+       */
+      FIX_UH (nglyphs);
+      if (nglyphs > root_nglyphs)
+       {
+         fprintf (stderr, "More glyphs in 'post' table than in 'maxp' table");
+       }
+      
+      if (ttf_verbosity >= 2)
        fprintf (stderr, "  %d glyphs\n", nglyphs);
       glyphNameIndex = mymalloc (sizeof (USHORT) * nglyphs);
       surely_read (fd, glyphNameIndex, sizeof (USHORT) * nglyphs);
@@ -203,7 +218,7 @@ readPostTable (int fd, int nglyphs, struct PostTable *pt,
          glyphNamesTemp[i] = mymalloc (c + 1);
          surely_read (fd, glyphNamesTemp[i], c);
          glyphNamesTemp[i][c] = '\0';
-         if (verbosity >= 3)
+         if (ttf_verbosity >= 3)
            fprintf (stderr, "    %d: %s\n", i, glyphNamesTemp[i]);
          i++;
        }
@@ -221,7 +236,7 @@ readPostTable (int fd, int nglyphs, struct PostTable *pt,
  /*NOTREACHED*/}
 
 void *
-readLocaTable (int fd, int nglyphs, int format)
+readLocaTable (FILE *fd, int nglyphs, int format)
 {
   int i;
   switch (format)
@@ -250,13 +265,13 @@ readLocaTable (int fd, int nglyphs, int format)
  /*NOTREACHED*/}
 
 struct Box *
-readGlyfTable (int fd, int nglyphs, int format, void *loca)
+readGlyfTable (FILE *fd, int nglyphs, int format, void *loca)
 {
   int i;
   struct Box *bbox;
   off_t base, offset;
 
-  base = surely_lseek (fd, 0, SEEK_CUR);
+  base = ftell (fd);
 
   bbox = mymalloc (nglyphs * sizeof (struct Box));
   for (i = 0; i < nglyphs; i++)
@@ -273,7 +288,7 @@ readGlyfTable (int fd, int nglyphs, int format, void *loca)
 }
 
 longHorMetric *
-readHmtxTable (int fd, int nummetrics)
+readHmtxTable (FILE *fd, int nummetrics)
 {
   longHorMetric *metrics;
   int i;
@@ -288,13 +303,13 @@ readHmtxTable (int fd, int nummetrics)
 }
 
 struct HheaTable *
-readHheaTable (int fd)
+readHheaTable (FILE *fd)
 {
   struct HheaTable *hhea;
   hhea = mymalloc (sizeof (struct HheaTable));
   surely_read (fd, hhea, sizeof (struct HheaTable));
   FIX_HheaTable (*hhea);
-  if (verbosity >= 2)
+  if (ttf_verbosity >= 2)
     fprintf (stderr, "  version %d.%u\n",
             hhea->version.mantissa, hhea->version.fraction);
   if (hhea->metricDataFormat != 0)
@@ -303,7 +318,7 @@ readHheaTable (int fd)
 }
 
 int
-readKernTable (int fd, int **nkep, struct KernEntry0 ***kep)
+readKernTable (FILE *fd, int **nkep, struct KernEntry0 ***kep)
 {
   struct KernTable kt;
   struct KernSubTableHeader ksth;
@@ -314,7 +329,7 @@ readKernTable (int fd, int **nkep, struct KernEntry0 ***kep)
 
   surely_read (fd, &kt, sizeof (struct KernTable));
   FIX_KernTable (kt);
-  if (verbosity >= 2)
+  if (ttf_verbosity >= 2)
     {
       fprintf (stderr, "  version %d\n", kt.version);
       fprintf (stderr, "  %d subtables\n", kt.nTables);
@@ -326,7 +341,7 @@ readKernTable (int fd, int **nkep, struct KernEntry0 ***kep)
     {
       surely_read (fd, &ksth, sizeof (struct KernSubTableHeader));
       FIX_KernSubTableHeader (ksth);
-      if (verbosity >= 2)
+      if (ttf_verbosity >= 2)
        fprintf (stderr, "  analyzing subtable %d, version %d... ",
                 i, ksth.version);
       if ((ksth.coverage & kernHorizontal) &&
@@ -336,7 +351,7 @@ readKernTable (int fd, int **nkep, struct KernEntry0 ***kep)
        {
          surely_read (fd, &kst, sizeof (struct KernSubTable0));
          FIX_KernSubTable0 (kst);
-         if (verbosity >= 2)
+         if (ttf_verbosity >= 2)
            fprintf (stderr, "reading %d entries.\n", kst.nPairs);
          nke[i] = kst.nPairs;
          ke[i] = mymalloc (kst.nPairs * sizeof (struct KernEntry0));
@@ -346,7 +361,7 @@ readKernTable (int fd, int **nkep, struct KernEntry0 ***kep)
        }
       else
        {
-         if (verbosity >= 2)
+         if (ttf_verbosity >= 2)
            fprintf (stderr, "skipping.\n");
          surely_lseek (fd, ksth.length - sizeof (struct KernSubTableHeader),
                        SEEK_CUR);