*** empty log message ***
[m17n/libotf.git] / src / otfopen.c
index db35e6f..cef7e16 100644 (file)
@@ -3,7 +3,7 @@
 #include <string.h>
 
 #include "otf.h"
-#include "otfutil.h"
+#include "otferror.h"
 
 /* OTF_Stream
 
@@ -507,10 +507,10 @@ read_feature_list (OTF *otf, OTF_Stream *stream, long offset,
 
 static int read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream,
                                      long offset, unsigned type,
-                                     OTF_LookupSubTable *subtable);
+                                     OTF_LookupSubTableGSUB *subtable);
 static int read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
                                      long offset, unsigned type,
-                                     OTF_LookupSubTable *subtable);
+                                     OTF_LookupSubTableGPOS *subtable);
 
 static int
 read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
@@ -536,32 +536,28 @@ read_lookup_list (OTF *otf, OTF_Stream *stream, long offset,
       READ_UINT16 (stream, lookup->SubTableCount);
       OTF_MALLOC (lookup->SubTableOffset, lookup->SubTableCount,
                  " (SubTableOffset)");
-      OTF_CALLOC (lookup->SubTable, lookup->SubTableCount,
-                 " (SubTable)");
+      if (gsub)
+       OTF_CALLOC (lookup->SubTable.gsub, lookup->SubTableCount,
+                   " (SubTable)");
+      else
+       OTF_CALLOC (lookup->SubTable.gpos, lookup->SubTableCount,
+                   " (SubTable)");
       for (j = 0; j < lookup->SubTableCount; j++)
        READ_OFFSET (stream, lookup->SubTableOffset[j]);
-      if (gsub)
-       for (j = 0; j < lookup->SubTableCount; j++)
-         {
-           long this_offset
-             = offset + lookup->offset + lookup->SubTableOffset[j];
+      for (j = 0; j < lookup->SubTableCount; j++)
+       {
+         long this_offset
+           = offset + lookup->offset + lookup->SubTableOffset[j];
 
-           if (read_lookup_subtable_gsub (otf, stream, this_offset,
+         if (gsub
+             ? read_lookup_subtable_gsub (otf, stream, this_offset,
                                           lookup->LookupType,
-                                          lookup->SubTable + j) < 0)
-             return errret;
-         }
-      else
-       for (j = 0; j < lookup->SubTableCount; j++)
-         {
-           long this_offset
-             = offset + lookup->offset + lookup->SubTableOffset[j];
-
-           if (read_lookup_subtable_gpos (otf, stream, this_offset,
+                                          lookup->SubTable.gsub + j) < 0
+             : read_lookup_subtable_gpos (otf, stream, this_offset,
                                           lookup->LookupType,
-                                          lookup->SubTable + j) < 0)
-             return errret;
-         }
+                                          lookup->SubTable.gpos + j) < 0)
+           return errret;
+       }
     }
 
   return 0;
@@ -1042,7 +1038,7 @@ read_chain_subclass_set (OTF *otf, OTF_Stream *stream, long offset,
 
 static int 
 read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
-                          unsigned type, OTF_LookupSubTable *subtable)
+                          unsigned type, OTF_LookupSubTableGSUB *subtable)
 {
   char *errfmt = "GSUB LookupSubTable%s";
   int errret = -1;
@@ -1057,16 +1053,16 @@ read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
        {
          if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
            return -1;
-         READ_INT16 (stream, subtable->sub.gsub.single1.DeltaGlyphID);
+         READ_INT16 (stream, subtable->u.single1.DeltaGlyphID);
        }
       else if (subtable->Format == 2)
        {
          if (read_coverage (otf, stream, offset, &subtable->Coverage) < 0)
            return -1;
-         subtable->sub.gsub.single2.GlyphCount
-           = read_glyph_ids (otf, stream, &subtable->sub.gsub.single2.Substitute,
+         subtable->u.single2.GlyphCount
+           = read_glyph_ids (otf, stream, &subtable->u.single2.Substitute,
                              0);
-         if (! subtable->sub.gsub.single2.GlyphCount)
+         if (! subtable->u.single2.GlyphCount)
            return -1;
        }
       else
@@ -1077,9 +1073,9 @@ read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
       if (subtable->Format == 1)
        {
          read_coverage (otf, stream, offset, &subtable->Coverage);
-         subtable->sub.gsub.multiple1.SequenceCount
+         subtable->u.multiple1.SequenceCount
            = read_sequence (otf, stream, offset,
-                            &subtable->sub.gsub.multiple1.Sequence);
+                            &subtable->u.multiple1.Sequence);
        }
       else
        OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
@@ -1094,10 +1090,10 @@ read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
          read_coverage (otf, stream, offset, &subtable->Coverage);
          count = (read_ligature_set
                   (otf, stream, offset,
-                   &subtable->sub.gsub.ligature1.LigatureSet));
+                   &subtable->u.ligature1.LigatureSet));
          if (count < 0)
            return -1;
-         subtable->sub.gsub.ligature1.LigSetCount = (unsigned) count;
+         subtable->u.ligature1.LigSetCount = (unsigned) count;
        }
       else
        OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
@@ -1107,51 +1103,51 @@ read_lookup_subtable_gsub (OTF *otf, OTF_Stream *stream, long offset,
       if (subtable->Format == 1)
        {
          read_coverage (otf, stream, offset, &subtable->Coverage);
-         subtable->sub.gsub.chain_context1.ChainSubRuleSetCount
+         subtable->u.chain_context1.ChainSubRuleSetCount
            = (read_chain_subrule_set
               (otf, stream, offset,
-               &subtable->sub.gsub.chain_context1.ChainSubRuleSet));
+               &subtable->u.chain_context1.ChainSubRuleSet));
        }
       else if (subtable->Format == 2)
        {
          read_coverage (otf, stream, offset, &subtable->Coverage);
          read_class_def (otf, stream, offset,
-                         &subtable->sub.gsub.chain_context2.Backtrack);
+                         &subtable->u.chain_context2.Backtrack);
          read_class_def (otf, stream, offset,
-                         &subtable->sub.gsub.chain_context2.Input);
+                         &subtable->u.chain_context2.Input);
          read_class_def (otf, stream, offset,
-                         &subtable->sub.gsub.chain_context2.LookAhead);
-         subtable->sub.gsub.chain_context2.ChainSubClassSetCnt
+                         &subtable->u.chain_context2.LookAhead);
+         subtable->u.chain_context2.ChainSubClassSetCnt
            = (read_chain_subclass_set
               (otf, stream, offset,
-               &subtable->sub.gsub.chain_context2.ChainSubClassSet));
+               &subtable->u.chain_context2.ChainSubClassSet));
        }
       else if (subtable->Format == 3)
        {
          count = (read_coverage_list
                   (otf, stream, offset,
-                   &subtable->sub.gsub.chain_context3.Backtrack));
+                   &subtable->u.chain_context3.Backtrack));
          if (count < 0)
            return -1;
-         subtable->sub.gsub.chain_context3.BacktrackGlyphCount
+         subtable->u.chain_context3.BacktrackGlyphCount
            = (unsigned) count;
          count = (read_coverage_list
                   (otf, stream, offset,
-                   &subtable->sub.gsub.chain_context3.Input));
+                   &subtable->u.chain_context3.Input));
          if (count <= 0)
            return -1;
-         subtable->sub.gsub.chain_context3.InputGlyphCount
+         subtable->u.chain_context3.InputGlyphCount
            = (unsigned) count;
-         subtable->Coverage = subtable->sub.gsub.chain_context3.Input[0];
+         subtable->Coverage = subtable->u.chain_context3.Input[0];
          count = (read_coverage_list
                   (otf, stream, offset,
-                   &subtable->sub.gsub.chain_context3.LookAhead));
-         subtable->sub.gsub.chain_context3.LookaheadGlyphCount
+                   &subtable->u.chain_context3.LookAhead));
+         subtable->u.chain_context3.LookaheadGlyphCount
            = (unsigned) count;
-         subtable->sub.gsub.chain_context3.SubstCount
+         subtable->u.chain_context3.SubstCount
            = (read_subst_lookup_record
               (otf, stream,
-               &subtable->sub.gsub.chain_context3.SubstLookupRecord));
+               &subtable->u.chain_context3.SubstLookupRecord));
        }
       else
        OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
@@ -1350,7 +1346,7 @@ read_class1_record_list (OTF *otf, OTF_Stream *stream, long offset,
 static int 
 read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
                           long offset, unsigned type,
-                          OTF_LookupSubTable *subtable)
+                          OTF_LookupSubTableGPOS *subtable)
 {
   char *errfmt = "GPOS LookupSubTable%s";
   int errret = -1;
@@ -1364,14 +1360,14 @@ read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
       if (subtable->Format == 1)
        {
          read_coverage (otf, stream, offset, &subtable->Coverage);
-         subtable->sub.gsub.single1.DeltaGlyphID = READ_INT16 (stream);
+         subtable->u.single1.DeltaGlyphID = READ_INT16 (stream);
        }
       else if (subtable->Format == 2)
        {
          read_coverage (otf, stream, offset, &subtable->Coverage);
-         subtable->sub.gsub.single2.GlyphCount
+         subtable->u.single2.GlyphCount
            = read_glyph_ids (otf, stream,
-                             &subtable->sub.gsub.single2.Substitute, 0);
+                             &subtable->u.single2.Substitute, 0);
        }
       else
        OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
@@ -1387,20 +1383,20 @@ read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
        {
          SEEK_STREAM (stream, offset + 2);
          read_coverage (otf, stream, offset, &subtable->Coverage);
-         READ_UINT16 (stream, subtable->sub.gpos.pair2.ValueFormat1);
-         READ_UINT16 (stream, subtable->sub.gpos.pair2.ValueFormat2);
+         READ_UINT16 (stream, subtable->u.pair2.ValueFormat1);
+         READ_UINT16 (stream, subtable->u.pair2.ValueFormat2);
          read_class_def (otf, stream, offset,
-                         &subtable->sub.gpos.pair2.ClassDef1);
+                         &subtable->u.pair2.ClassDef1);
          read_class_def (otf, stream, offset,
-                         &subtable->sub.gpos.pair2.ClassDef2);
-         READ_UINT16 (stream, subtable->sub.gpos.pair2.Class1Count);
-         READ_UINT16 (stream, subtable->sub.gpos.pair2.Class2Count);
-         subtable->sub.gpos.pair2.Class1Record
+                         &subtable->u.pair2.ClassDef2);
+         READ_UINT16 (stream, subtable->u.pair2.Class1Count);
+         READ_UINT16 (stream, subtable->u.pair2.Class2Count);
+         subtable->u.pair2.Class1Record
            = read_class1_record_list (otf, stream, offset,
-                                      subtable->sub.gpos.pair2.Class1Count,
-                                      subtable->sub.gpos.pair2.ValueFormat1,
-                                      subtable->sub.gpos.pair2.Class2Count,
-                                      subtable->sub.gpos.pair2.ValueFormat2);
+                                      subtable->u.pair2.Class1Count,
+                                      subtable->u.pair2.ValueFormat1,
+                                      subtable->u.pair2.Class2Count,
+                                      subtable->u.pair2.ValueFormat2);
        }
       else
        OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
@@ -1411,13 +1407,13 @@ read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
        {
          read_coverage (otf, stream, offset, &subtable->Coverage);
          read_coverage (otf, stream, offset,
-                        &subtable->sub.gpos.mark_base1.BaseCoverage);
-         READ_UINT16 (stream, subtable->sub.gpos.mark_base1.ClassCount);
+                        &subtable->u.mark_base1.BaseCoverage);
+         READ_UINT16 (stream, subtable->u.mark_base1.ClassCount);
          read_mark_array (otf, stream, offset,
-                          &subtable->sub.gpos.mark_base1.MarkArray);
+                          &subtable->u.mark_base1.MarkArray);
          read_base_array (otf, stream, offset,
-                          subtable->sub.gpos.mark_base1.ClassCount,
-                          &subtable->sub.gpos.mark_base1.BaseArray);
+                          subtable->u.mark_base1.ClassCount,
+                          &subtable->u.mark_base1.BaseArray);
        }
       else
        OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
@@ -1428,45 +1424,45 @@ read_lookup_subtable_gpos (OTF *otf, OTF_Stream *stream,
       if (subtable->Format == 1)
        {
          read_coverage (otf, stream, offset,
-                        &subtable->sub.gsub.chain_context1.Coverage);
-         subtable->sub.gsub.chain_context1.ChainSubRuleSetCount
+                        &subtable->u.chain_context1.Coverage);
+         subtable->u.chain_context1.ChainSubRuleSetCount
            = (read_chain_subrule_set
               (otf, stream, offset,
-               &subtable->sub.gsub.chain_context1.ChainSubRuleSet));
+               &subtable->u.chain_context1.ChainSubRuleSet));
        }
       else if (subtable->Format == 2)
        {
          read_coverage (otf, stream, offset,
-                        &subtable->sub.gsub.chain_context2.Coverage);
+                        &subtable->u.chain_context2.Coverage);
          read_class_def (otf, stream, offset,
-                         &subtable->sub.gsub.chain_context2.Backtrack);
+                         &subtable->u.chain_context2.Backtrack);
          read_class_def (otf, stream, offset,
-                         &subtable->sub.gsub.chain_context2.Input);
+                         &subtable->u.chain_context2.Input);
          read_class_def (otf, stream, offset,
-                         &subtable->sub.gsub.chain_context2.LookAhead);
-         subtable->sub.gsub.chain_context2.ChainSubClassSetCnt
+                         &subtable->u.chain_context2.LookAhead);
+         subtable->u.chain_context2.ChainSubClassSetCnt
            = (read_chain_subclass_set
               (otf, stream, offset,
-               &subtable->sub.gsub.chain_context2.ChainSubClassSet));
+               &subtable->u.chain_context2.ChainSubClassSet));
        }
       else if (subtable->Format == 3)
        {
-         subtable->sub.gsub.chain_context3.BacktrackGlyphCount
+         subtable->u.chain_context3.BacktrackGlyphCount
            = (read_coverage_list
               (otf, stream, offset,
-               &subtable->sub.gsub.chain_context3.Backtrack));
-         subtable->sub.gsub.chain_context3.InputGlyphCount
+               &subtable->u.chain_context3.Backtrack));
+         subtable->u.chain_context3.InputGlyphCount
            = (read_coverage_list
               (otf, stream, offset,
-               &subtable->sub.gsub.chain_context3.Input));
-         subtable->sub.gsub.chain_context3.LookaheadGlyphCount
+               &subtable->u.chain_context3.Input));
+         subtable->u.chain_context3.LookaheadGlyphCount
            = (read_coverage_list
               (otf, stream, offset,
-               &subtable->sub.gsub.chain_context3.LookAhead));
-         subtable->sub.gsub.chain_context3.SubstCount
+               &subtable->u.chain_context3.LookAhead));
+         subtable->u.chain_context3.SubstCount
            = (read_subst_lookup_record
               (otf, stream,
-               &subtable->sub.gsub.chain_context3.SubstLookupRecord));
+               &subtable->u.chain_context3.SubstLookupRecord));
        }
       else
        OTF_ERROR (OTF_ERROR_TABLE, " (Invalid SubFormat)");
@@ -1941,8 +1937,6 @@ otf_close (OTF *otf)
   OTF_InternalData *internal_data = otf->internal_data;
   int i;
 
-  //if (otf->filename)
-  //free (otf->filename);
   if (internal_data)
     {
       OTF_MemoryRecord *memrec = internal_data->memory_record;
@@ -1965,6 +1959,7 @@ otf_close (OTF *otf)
        }
       free (internal_data);
     }
+  free (otf);
 }