*** empty log message ***
[m17n/libotf.git] / src / otfopen.c
index ea0445b..7e7cb34 100644 (file)
@@ -1,6 +1,6 @@
 /* otfopen.c -- OpenType font reader.
 
-Copyright (C) 2003, 2004, 2005, 2006
+Copyright (C) 2003, 2004, 2005, 2006, 2008
   National Institute of Advanced Industrial Science and Technology (AIST)
   Registration Number H15PRO167
 
@@ -91,7 +91,7 @@ typedef struct
 
 typedef long OTF_StreamState;
 
-OTF_Stream *
+static OTF_Stream *
 make_stream (const char *name)
 {
   OTF_Stream *stream;
@@ -105,7 +105,7 @@ make_stream (const char *name)
   return stream;
 }
 
-int
+static int
 setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes)
 {
   char *errfmt = "stream setup for %s";
@@ -131,7 +131,7 @@ setup_stream (OTF_Stream *stream, FILE *fp, long offset, int nbytes)
   return 0;
 }
 
-OTF_Stream *
+static OTF_Stream *
 make_stream_from_ft_face (FT_Face face, const char *name)
 {
   char *errfmt = "FT_Face stream creation for %s";
@@ -161,7 +161,7 @@ make_stream_from_ft_face (FT_Face face, const char *name)
   return stream;
 }
 
-void
+static void
 free_stream (OTF_Stream *stream)
 {
   free (stream->buf);
@@ -199,6 +199,15 @@ free_stream (OTF_Stream *stream)
     (stream)->pos += 2;                                                \
   } while (0)
 
+#define READ_UINT24(stream, var)                       \
+  do {                                                 \
+    STREAM_CHECK_SIZE ((stream), 3);                   \
+    (var) =  (((stream)->buf[(stream)->pos ] << 16)    \
+             | ((stream)->buf[(stream)->pos + 1] << 8) \
+             | (stream)->buf[(stream)->pos + 2]);      \
+    (stream)->pos += 3;                                        \
+  } while (0)
+
 #define READ_ULONG(stream, var)                                \
   do {                                                 \
     STREAM_CHECK_SIZE ((stream), 4);                   \
@@ -495,6 +504,85 @@ read_name_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
 \f
 /*** (1-4) "cmap" table */
 
+static OTF_EncodingSubtable14 *
+read_cmap_uvs_table (OTF *otf, OTF_Stream *stream, OTF_Offset offset)
+{
+  OTF_EncodingSubtable14 *sub14;
+  char *errfmt = "cmap-uvs%s";
+  void *errret = NULL;
+  unsigned nRecords;
+  unsigned i,j;
+
+  OTF_MALLOC (sub14, 1, " (EncodingSubtable14)");
+  READ_ULONG (stream, nRecords);
+  sub14->nRecords = nRecords;
+  OTF_MALLOC (sub14->Records, nRecords, "(EncodingSubtable14-Records)");
+  for (i = 0; i < sub14->nRecords; i++) 
+    {
+      unsigned varSelector=0, defaultUVSOffset, nonDefaultUVSOffset;
+
+      READ_UINT24 (stream, varSelector);
+      sub14->Records[i].varSelector = varSelector;
+      READ_ULONG (stream, defaultUVSOffset);
+      sub14->Records[i].defaultUVSOffset = defaultUVSOffset;
+      READ_ULONG (stream, nonDefaultUVSOffset);
+      sub14->Records[i].nonDefaultUVSOffset = nonDefaultUVSOffset;
+    }
+  for (i = 0; i < sub14->nRecords; i++) 
+    {
+      OTF_VariationSelectorRecord *record = &sub14->Records[i];
+      unsigned defaultUVSOffset = record->defaultUVSOffset;
+      unsigned nonDefaultUVSOffset = record->nonDefaultUVSOffset;
+
+      if (defaultUVSOffset) 
+       {
+         unsigned numUnicodeValueRanges;
+
+         SEEK_STREAM (stream, offset+defaultUVSOffset);
+         READ_ULONG (stream, numUnicodeValueRanges);
+         record->numUnicodeValueRanges = numUnicodeValueRanges;
+         OTF_MALLOC (record->unicodeValueRanges, 
+                     numUnicodeValueRanges,
+                     "(EncodingSubtable14-Records-unicodeValueRanges)");
+         for (j = 0; j < numUnicodeValueRanges; j++)
+           {
+             OTF_UnicodeValueRange *unicodeValueRange 
+               = &record->unicodeValueRanges[j];
+             unsigned startUnicodeValue;
+             char additionalCount;
+
+             READ_UINT24 (stream, startUnicodeValue);
+             unicodeValueRange->startUnicodeValue=startUnicodeValue;
+             READ_BYTES (stream, &additionalCount, 1);
+             unicodeValueRange->additionalCount
+               = (unsigned short) additionalCount;
+           }
+       }
+      if (nonDefaultUVSOffset) 
+       {
+         unsigned numUVSMappings;
+
+         SEEK_STREAM (stream, offset+nonDefaultUVSOffset);
+         READ_ULONG (stream, numUVSMappings);
+         record->numUVSMappings = numUVSMappings;
+         OTF_MALLOC (record->uvsMappings, numUVSMappings,
+                     "(EncodingSubtable14-Records-uvsMappings)");
+         for (j = 0; j < numUVSMappings; j++)
+           {
+             OTF_UVSMapping *uvsMapping = &record->uvsMappings[j];
+             unsigned unicodeValue;
+             unsigned short glyphID;
+
+             READ_UINT24 (stream, unicodeValue);
+             uvsMapping->unicodeValue = unicodeValue;
+             READ_USHORT (stream, glyphID);
+             uvsMapping->glyphID = glyphID;
+           }
+       }
+    }
+  return sub14;
+}
+
 static void *
 read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
 {
@@ -540,15 +628,23 @@ read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
       SEEK_STREAM (stream, cmap->EncodingRecord[i].offset);
       READ_USHORT (stream, format);
       cmap->EncodingRecord[i].subtable.format = format;
-      READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
-      if (format == 8 || format == 10 || format == 12)
+      if (format == 14)
        {
-         READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
-         READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
+         READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);     
+         cmap->EncodingRecord[i].subtable.language = 0;
        }
       else
        {
-         READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
+         READ_USHORT (stream, cmap->EncodingRecord[i].subtable.length);
+         if (format == 8 || format == 10 || format == 12)
+           {
+             READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
+             READ_ULONG (stream, cmap->EncodingRecord[i].subtable.language);
+           }
+         else
+           {
+             READ_USHORT (stream, cmap->EncodingRecord[i].subtable.language);
+           }
        }
       switch (format)
        {
@@ -705,6 +801,14 @@ read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
          }
          break;
 
+       case 14:
+         {
+           cmap->EncodingRecord[i].subtable.f.f14
+             = read_cmap_uvs_table (otf, stream,
+                                    cmap->EncodingRecord[i].offset);
+           break;
+         }
+
        default:
          OTF_ERROR (OTF_ERROR_TABLE, " (invalid Subtable format)");
        }
@@ -725,7 +829,7 @@ read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
 
            for (i = 0; i < segCount; i++)
              {
-               OTF_cmapSegument *seg = sub4->segments + i;
+               OTF_cmapSegment *seg = sub4->segments + i;
                int c;
 
                if (seg->idRangeOffset == 0xFFFF)
@@ -951,41 +1055,48 @@ read_device_table (OTF *otf, OTF_Stream *stream, long offset,
   READ_UINT16 (stream, table->EndSize);
   READ_UINT16 (stream, table->DeltaFormat);
   num = table->EndSize - table->StartSize + 1;
-  OTF_MALLOC (table->DeltaValue, num, "");
+  if (num > 0 && table->DeltaFormat >= 1 && table->DeltaFormat <= 3)
+    {
+      OTF_MALLOC (table->DeltaValue, num, "");
 
-  if (table->DeltaFormat == 1)
-    for (i = 0; i < num; i++)
-      {
-       if ((i % 8) == 0)
-         READ_UINT16 (stream, val);
-       intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
-       table->DeltaValue[i] = intval.int2;
-      }
-  else if (table->DeltaFormat == 2)
-    for (i = 0; i < num; i++)
-      {
-       if ((i % 4) == 0)
-         READ_UINT16 (stream, val);
-       intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
-       table->DeltaValue[i] = intval.int4;
-      }
-  else if (table->DeltaFormat == 3)
-    for (i = 0; i < num; i++)
-      {
-       if ((i % 2) == 0)
+      if (table->DeltaFormat == 1)
+       for (i = 0; i < num; i++)
          {
-           READ_UINT16 (stream, val);
-           intval.int8 = val >> 8;
-           table->DeltaValue[i] = intval.int8;
+           if ((i % 8) == 0)
+             READ_UINT16 (stream, val);
+           intval.int2 = (val >> (14 - (i % 8) * 2)) & 0x03;
+           table->DeltaValue[i] = intval.int2;
          }
-       else
+      else if (table->DeltaFormat == 2)
+       for (i = 0; i < num; i++)
          {
-           intval.int8 = val >> 8;
-           table->DeltaValue[i] = intval.int8;
+           if ((i % 4) == 0)
+             READ_UINT16 (stream, val);
+           intval.int4 = (val >> (12 - (i % 4) * 4)) & 0x0F;
+           table->DeltaValue[i] = intval.int4;
          }
-      }
+      else                             /* (table->DeltaFormat == 3) */
+       for (i = 0; i < num; i++)
+         {
+           if ((i % 2) == 0)
+             {
+               READ_UINT16 (stream, val);
+               intval.int8 = val >> 8;
+               table->DeltaValue[i] = intval.int8;
+             }
+           else
+             {
+               intval.int8 = val >> 8;
+               table->DeltaValue[i] = intval.int8;
+             }
+         }
+    }
   else
-    OTF_ERROR (OTF_ERROR_TABLE, " (Invalid format)");
+    {
+      /* Invalid DeltaFormat but several fonts has such values (bug of
+        fontforge?).  So accept it with NULL delta values.  */
+      table->DeltaValue = NULL;
+    }
   return 0;
 }
 
@@ -2604,7 +2715,7 @@ read_jstf_table (OTF_Stream *stream, long offset)
 \f
 /*** (1-11) Structure for OTF */
 
-int
+static int
 read_offset_table (OTF *otf, OTF_Stream *stream, OTF_OffsetTable *table)
 {
   int errret = -1;