Versions changed to 0.9.12.
[m17n/libotf.git] / src / otfopen.c
index 443b822..299c23b 100644 (file)
@@ -1,6 +1,6 @@
 /* otfopen.c -- OpenType font reader.
 
-Copyright (C) 2003, 2004, 2005, 2006, 2008
+Copyright (C) 2003, 2004, 2005, 2006, 2008, 2009, 2010
   National Institute of Advanced Industrial Science and Technology (AIST)
   Registration Number H15PRO167
 
@@ -27,7 +27,7 @@ write to the Free Software Foundation, Inc., 59 Temple Place, Suite
 #include <config.h>
 
 #include "otf.h"
-#include "otferror.h"
+#include "internal.h"
 
 #include FT_TRUETYPE_TABLES_H
 
@@ -57,6 +57,15 @@ write to the Free Software Foundation, Inc., 59 Temple Place, Suite
 
     (5) API miscellaneous
 */
+
+int debug_flag = -1;
+
+static void
+set_debug_flag ()
+{
+  debug_flag = getenv ("LIBOTF_DEBUG") != NULL;
+}
+
 \f
 /* (0) Stream handler
 
@@ -164,7 +173,8 @@ make_stream_from_ft_face (FT_Face face, const char *name)
 static void
 free_stream (OTF_Stream *stream)
 {
-  free (stream->buf);
+  if (stream->buf)
+    free (stream->buf);
   free (stream);
 }
 
@@ -199,6 +209,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);                   \
@@ -289,6 +308,16 @@ struct _OTF_TableInfo
   OTF_Stream *stream;
 };
 
+struct _OTF_ApplicationData
+{
+  char *id;
+  void *data;
+  void (*freer) (void *data);
+  struct _OTF_ApplicationData *next;
+};
+
+typedef struct _OTF_ApplicationData OTF_ApplicationData;
+
 struct OTF_InternalData
 {
   /* Information about each OTF table.  */
@@ -299,8 +328,10 @@ struct OTF_InternalData
 
   /* Records of allocated memories.  */
   OTF_MemoryRecord *memory_record;
-};
 
+  /* Root of application data chain.  */
+  OTF_ApplicationData *app_data;
+};
 
 static OTF_MemoryRecord *
 allocate_memory_record (OTF *otf)
@@ -382,13 +413,14 @@ read_head_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
 static int
 read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec)
 {
-  char *errfmt = "nameID (%d)";
+  char errfmt[256];
   int errret = -1;
   OTF_StreamState state;
   int ucs = 0;
   int ascii = 0;
   int i;
 
+  sprintf (errfmt, "nameID (%d)%%s", rec->nameID);
   if (rec->platformID == 0)
     ucs = (rec->encodingID <= 3) ? 2 : 4;
   else if (rec->platformID == 1 && rec->encodingID == 0)
@@ -398,7 +430,7 @@ read_name (OTF *otf, OTF_Stream *stream, OTF_NameRecord *rec)
           : rec->encodingID == 10 ? 4
           : 0);
 
-  OTF_MALLOC (rec->name, rec->length + 1, (void *) rec->nameID);
+  OTF_MALLOC (rec->name, rec->length + 1, "");
   SAVE_STREAM (stream, state);
   SEEK_STREAM (stream, stream->pos + rec->offset);
   READ_BYTES (stream, rec->name, rec->length);
@@ -496,17 +528,81 @@ read_name_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
 /*** (1-4) "cmap" table */
 
 static OTF_EncodingSubtable14 *
-read_cmap_uvs_table (OTF *otf, OTF_Stream *stream, int nbytes)
+read_cmap_uvs_table (OTF *otf, OTF_Stream *stream, OTF_Offset offset)
 {
   OTF_EncodingSubtable14 *sub14;
   char *errfmt = "cmap-uvs%s";
   void *errret = NULL;
-  int i;
+  unsigned nRecords;
+  unsigned i,j;
 
   OTF_MALLOC (sub14, 1, " (EncodingSubtable14)");
-  sub14->nbytes = nbytes;
-  OTF_MALLOC (sub14->data, nbytes, " (EncodingSubtable14-Data)");
-  READ_BYTES (stream, sub14->data, nbytes);
+  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;
 }
 
@@ -548,6 +644,8 @@ read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
            unicode_full_index = i;
        }
     }
+  cmap->table_index = unicode_full_index;
+
   for (i = 0; i < cmap->numTables; i++)
     {
       unsigned format;
@@ -557,7 +655,7 @@ read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
       cmap->EncodingRecord[i].subtable.format = format;
       if (format == 14)
        {
-         READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);     
+         READ_ULONG (stream, cmap->EncodingRecord[i].subtable.length);
          cmap->EncodingRecord[i].subtable.language = 0;
        }
       else
@@ -644,7 +742,7 @@ read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
              {
                unsigned off;
                unsigned rest = 2 * (segCount - j);
-               
+
                READ_USHORT (stream, off);
                if (off == 0)
                  sub4->segments[j].idRangeOffset = 0xFFFF;
@@ -732,8 +830,7 @@ read_cmap_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
          {
            cmap->EncodingRecord[i].subtable.f.f14
              = read_cmap_uvs_table (otf, stream,
-                                    cmap->EncodingRecord[i].subtable.length
-                                    - 6);
+                                    cmap->EncodingRecord[i].offset);
            break;
          }
 
@@ -757,13 +854,13 @@ 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)
                  for (c = seg->startCount; c <= seg->endCount; c++)
                    {
-                     glyph_id = c + seg->idDelta;
+                     glyph_id = (c + seg->idDelta) % 0x10000;
                      cmap->unicode_table[c] = glyph_id;
                      if (glyph_id > max_glyph_id)
                        max_glyph_id = glyph_id;
@@ -1138,22 +1235,27 @@ read_gdef_table (OTF *otf, OTF_TableInfo *table, enum OTF_ReaderFlag flag)
   OTF_GDEF *gdef;
 
   OTF_CALLOC (gdef, 1, "");
-  read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
-  if (gdef->header.GlyphClassDef)
+  if (stream->buf)
     {
-      gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
-      read_class_def_without_offset (otf, stream, &gdef->glyph_class_def);
-    }
-  if (gdef->header.AttachList)
-    read_attach_list (otf, stream, gdef->header.AttachList,
-                     &gdef->attach_list);
-  if (gdef->header.LigCaretList)
-    read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
-                        &gdef->lig_caret_list);
-  if (gdef->header.MarkAttachClassDef)
-    {
-      gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
-      read_class_def_without_offset (otf, stream, &gdef->mark_attach_class_def);
+      read_gdef_header (stream, (OTF_GDEFHeader *) &gdef->header);
+      if (gdef->header.GlyphClassDef)
+       {
+         gdef->glyph_class_def.offset = gdef->header.GlyphClassDef;
+         read_class_def_without_offset (otf, stream,
+                                        &gdef->glyph_class_def);
+       }
+      if (gdef->header.AttachList)
+       read_attach_list (otf, stream, gdef->header.AttachList,
+                         &gdef->attach_list);
+      if (gdef->header.LigCaretList)
+       read_lig_caret_list (otf, stream, gdef->header.LigCaretList,
+                            &gdef->lig_caret_list);
+      if (gdef->header.MarkAttachClassDef)
+       {
+         gdef->mark_attach_class_def.offset = gdef->header.MarkAttachClassDef;
+         read_class_def_without_offset (otf, stream,
+                                        &gdef->mark_attach_class_def);
+       }
     }
 
   *table->address = gdef;
@@ -1916,7 +2018,7 @@ read_reverse_chain1 (OTF *otf, OTF_Stream *stream, long offset,
   count = read_glyph_ids (otf, stream, &reverse_chain->Substitute, 0, -1);
   if (count <= 0)
     return -1;
-  reverse_chain->GlyphCount = count;  
+  reverse_chain->GlyphCount = count;
   return 0;
 }
 
@@ -2705,19 +2807,34 @@ read_header_part (OTF *otf, FILE *fp, FT_Face face)
       OTF_Tag gsub_tag = OTF_tag ("GSUB");
       OTF_Tag gpos_tag = OTF_tag ("GPOS");
       OTF_Stream *stream = make_stream ("Offset Table");
+      unsigned char ttctag[4];
 
       if (! stream)
        return -1;
       internal_data->header_stream = stream;
 
-      /* Size of Offset Table is 12 bytes.  */
-      if (setup_stream (stream, fp, 0, 12) < 0)
+      /* Size of Offset Table is 12 bytes.  Size of TTC header
+        (including only the an offset of the first font) is 16.  */
+      if (setup_stream (stream, fp, 0, 16) < 0)
        return -1;
+      READ_BYTES (stream, ttctag, 4);
+      if (memcmp (ttctag, "ttcf", 4) == 0)
+       {
+         /* This is a TrueType Collection file.  We extract the first font.  */
+         unsigned version, numfonts, offset;
+         READ_ULONG (stream, version);
+         READ_ULONG (stream, numfonts);
+         READ_ULONG (stream, offset); /* Offset of the first font.  */
+         if (setup_stream (stream, fp, offset, 12) < 0)
+           return -1;
+       }
+      else
+       SEEK_STREAM (stream, 0L);
       if (read_offset_table (otf, stream, &otf->offset_table) < 0)
        return -1;
-
       /* Size of each Table Directory is 16 bytes.  */
-      if (setup_stream (stream, fp, 12, 16 * otf->offset_table.numTables) < 0)
+      if (setup_stream (stream, fp, stream->pos,
+                       16 * otf->offset_table.numTables) < 0)
        return -1;
 
       OTF_CALLOC (otf->table_dirs, otf->offset_table.numTables,
@@ -2774,6 +2891,10 @@ read_header_part (OTF *otf, FILE *fp, FT_Face face)
        internal_data->table_info[OTF_TABLE_TYPE_GPOS].stream = stream;
     }
 
+  if (! internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream)
+    /* We can simulate the GDEF table.  */
+    internal_data->table_info[OTF_TABLE_TYPE_GDEF].stream
+      = make_stream ("GDEF");
   return 0;
 }
 
@@ -2835,11 +2956,14 @@ OTF_open (const char *otf_name)
   int len = strlen (otf_name);
   const char *ext = otf_name + (len - 4);
 
+  if (debug_flag < 0)
+    set_debug_flag ();
+
   if (len < 4
       || ext[0] != '.'
-      || (ext[1] != 'O' && ext[1] != 'T' && ext[1] != 'o' && ext[1] != 't')
-      || (ext[2] != 'T' && ext[2] != 't')
-      || (ext[3] != 'F' && ext[3] != 'f'))
+      || (strncasecmp (ext + 1, "otf", 3)
+         && strncasecmp (ext + 1, "ttf", 3)
+         && strncasecmp (ext + 1, "ttc", 3)))
     OTF_ERROR (OTF_ERROR_FILE, otf_name);
   fp = fopen (otf_name, "r");
   if (! fp)
@@ -2885,13 +3009,16 @@ OTF_open_ft_face (FT_Face face)
   OTF *otf;
   OTF_InternalData *internal_data;
 
+  if (debug_flag < 0)
+    set_debug_flag ();
+
   if (! FT_IS_SFNT (face))
     OTF_ERROR (OTF_ERROR_FILE, (char *) face->family_name);
   otf = calloc (1, sizeof (OTF));
   if (! otf)
     OTF_ERROR (OTF_ERROR_MEMORY, "body allocation");
   otf->filename = NULL;
-  
+
   internal_data = calloc (1, sizeof (OTF_InternalData));
   if (! internal_data)
     OTF_ERROR (OTF_ERROR_MEMORY, " (InternalData");
@@ -2919,6 +3046,7 @@ OTF_close (OTF *otf)
   if (internal_data)
     {
       OTF_MemoryRecord *memrec = internal_data->memory_record;
+      OTF_ApplicationData *app_data = internal_data->app_data;
 
       if (internal_data->header_stream)
        free_stream (internal_data->header_stream);
@@ -2927,6 +3055,10 @@ OTF_close (OTF *otf)
        if (internal_data->table_info[i].stream)
          free_stream (internal_data->table_info[i].stream);
 
+      for (; app_data; app_data = app_data->next)
+       if (app_data->data && app_data->freer)
+         app_data->freer (app_data->data);
+
       while (memrec)
        {
          OTF_MemoryRecord *next = memrec->next;
@@ -2936,6 +3068,7 @@ OTF_close (OTF *otf)
          free (memrec);
          memrec = next;
        }
+
       free (internal_data);
     }
   if (otf->filename)
@@ -3045,6 +3178,8 @@ OTF_check_features (OTF *otf, int gsubp,
 
   if (OTF_get_features (otf, gsubp) < 0)
     {
+      if (gsubp ? ! otf->gsub : ! otf->gpos)
+       return 0;
       for (i = 0; i < n_features; i++)
        {
          OTF_Tag feature = features[i];
@@ -3054,7 +3189,6 @@ OTF_check_features (OTF *otf, int gsubp,
          if ((((unsigned) feature) & 0x80000000) == 0)
            return -1;
        }
-      return 1;
     }
   if (gsubp)
     {
@@ -3131,3 +3265,44 @@ OTF_tag_name (OTF_Tag tag, char *name)
   name[3] = (char) (tag & 0xFF);
   name[4] = '\0';
 }
+
+int
+OTF_put_data (OTF *otf, char *id, void *data, void (*freer) (void *data))
+{
+  char *errfmt = "appdata %s";
+  int errret = -1;
+  OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
+  OTF_ApplicationData *app_data = internal_data->app_data;
+  int len = strlen (id) + 1;
+
+  for (; app_data; app_data = app_data->next)
+    if (memcmp (app_data->id, id, len) == 0)
+      {
+       if (app_data->data && app_data->freer)
+         app_data->freer (app_data->data);
+       break;
+      }
+  if (! app_data)
+    {
+      OTF_MALLOC (app_data, sizeof (OTF_ApplicationData), id);
+      app_data->next = internal_data->app_data;
+      internal_data->app_data = app_data;
+      OTF_MALLOC (app_data->id, len, id);
+      memcpy (app_data->id, id, len);
+    }
+  app_data->data = data;
+  app_data->freer = freer;
+  return 0;
+}
+
+void *
+OTF_get_data (OTF *otf, char *id)
+{
+  OTF_InternalData *internal_data = (OTF_InternalData *) otf->internal_data;
+  OTF_ApplicationData *app_data = internal_data->app_data;
+
+  for (; app_data; app_data = app_data->next)
+    if (strcmp (app_data->id, id) == 0)
+      return app_data->data;
+  return NULL;
+}