Charles.Forsyth20060303a828770d19 years agoHistory
829 lines · 26.9 KB · C
RawDownload
/***************************************************************************//*                                                                         *//*  sfobjs.c                                                               *//*                                                                         *//*    SFNT object management (base).                                       *//*                                                                         *//*  Copyright 1996-2001, 2002 by                                           *//*  David Turner, Robert Wilhelm, and Werner Lemberg.                      *//*                                                                         *//*  This file is part of the FreeType project, and may only be used,       *//*  modified, and distributed under the terms of the FreeType project      *//*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     *//*  this file you indicate that you have read the license and              *//*  understand and accept it fully.                                        *//*                                                                         *//***************************************************************************/  #include <ft2build.h>#include "sfobjs.h"#include "ttload.h"#include "ttcmap0.h"#include FT_INTERNAL_SFNT_H#include FT_INTERNAL_POSTSCRIPT_NAMES_H#include FT_TRUETYPE_IDS_H#include FT_TRUETYPE_TAGS_H #include "sferrors.h"    /*************************************************************************/  /*                                                                       */  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */  /* messages during execution.                                            */  /*                                                                       */#undef  FT_COMPONENT#define FT_COMPONENT  trace_sfobjs     /* convert a UTF-16 name entry to ASCII */  static FT_String*  tt_name_entry_ascii_from_utf16( TT_NameEntry  entry,                                  FT_Memory     memory )  {    FT_String*  string;    FT_UInt     len, code, n;    FT_Byte*    read = (FT_Byte*)entry->string;      len = (FT_UInt)entry->stringLength / 2;     if ( FT_MEM_NEW_ARRAY( string, len + 1 ) )      return NULL;     for ( n = 0; n < len; n++ )    {      code = FT_NEXT_USHORT( read );      if ( code < 32 || code > 127 )        code = '?';       string[n] = (char)code;    }     string[len] = 0;     return string;  }    /* convert a UCS-4 name entry to ASCII */  static FT_String*  tt_name_entry_ascii_from_ucs4( TT_NameEntry  entry,                                 FT_Memory     memory )  {    FT_String*  string;    FT_UInt     len, code, n;    FT_Byte*    read = (FT_Byte*)entry->string;      len = (FT_UInt)entry->stringLength / 4;     if ( FT_MEM_NEW_ARRAY( string, len + 1 ) )      return NULL;     for ( n = 0; n < len; n++ )    {      code = (FT_UInt)FT_NEXT_ULONG( read );      if ( code < 32 || code > 127 )        code = '?';       string[n] = (char)code;    }     string[len] = 0;     return string;  }    /* convert an Apple Roman or symbol name entry to ASCII */  static FT_String*  tt_name_entry_ascii_from_other( TT_NameEntry  entry,                                  FT_Memory     memory )  {    FT_String*  string;    FT_UInt     len, code, n;    FT_Byte*    read = (FT_Byte*)entry->string;      len = (FT_UInt)entry->stringLength;     if ( FT_MEM_NEW_ARRAY( string, len + 1 ) )      return NULL;     for ( n = 0; n < len; n++ )    {      code = *read++;      if ( code < 32 || code > 127 )        code = '?';       string[n] = (char)code;    }     string[len] = 0;     return string;  }    typedef FT_String*  (*TT_NameEntry_ConvertFunc)( TT_NameEntry  entry,                                                   FT_Memory     memory );    /*************************************************************************/  /*                                                                       */  /* <Function>                                                            */  /*    tt_face_get_name                                                   */  /*                                                                       */  /* <Description>                                                         */  /*    Returns a given ENGLISH name record in ASCII.                      */  /*                                                                       */  /* <Input>                                                               */  /*    face   :: A handle to the source face object.                      */  /*                                                                       */  /*    nameid :: The name id of the name record to return.                */  /*                                                                       */  /* <Return>                                                              */  /*    Character string.  NULL if no name is present.                     */  /*                                                                       */  static FT_String*  tt_face_get_name( TT_Face    face,                    FT_UShort  nameid )  {    FT_Memory         memory = face->root.memory;    FT_String*        result = NULL;    FT_UShort         n;    TT_NameEntryRec*  rec;    FT_Int            found_apple   = -1;    FT_Int            found_win     = -1;    FT_Int            found_unicode = -1;     TT_NameEntry_ConvertFunc  convert;      rec = face->name_table.names;    for ( n = 0; n < face->num_names; n++, rec++ )    {      /* According to the OpenType 1.3 specification, only Microsoft or  */      /* Apple platform IDs might be used in the `name' table.  The      */      /* `Unicode' platform is reserved for the `cmap' table, and the    */      /* `Iso' one is deprecated.                                        */      /*                                                                 */      /* However, the Apple TrueType specification doesn't say the same  */      /* thing and goes to suggest that all Unicode `name' table entries */      /* should be coded in UTF-16 (in big-endian format I suppose).     */      /*                                                                 */      if ( rec->nameID == nameid && rec->stringLength > 0 )      {        switch ( rec->platformID )        {        case TT_PLATFORM_APPLE_UNICODE:        case TT_PLATFORM_ISO:          /* there is `languageID' to check there.  We should use this */          /* field only as a last solution when nothing else is        */          /* available.                                                */          /*                                                           */          found_unicode = n;          break;         case TT_PLATFORM_MACINTOSH:          if ( rec->languageID == TT_MAC_LANGID_ENGLISH )            found_apple = n;           break;         case TT_PLATFORM_MICROSOFT:          /* we only take a non-English name when there is nothing */          /* else available in the font                            */          /*                                                       */          if ( found_win == -1 || ( rec->languageID & 0x3FF ) == 0x009 )          {            switch ( rec->encodingID )            {            case TT_MS_ID_SYMBOL_CS:            case TT_MS_ID_UNICODE_CS:            case TT_MS_ID_UCS_4:              found_win = n;              break;             default:              ;            }          }          break;         default:          ;        }      }    }     /* some fonts contain invalid Unicode or Macintosh formatted entries; */    /* we will thus favor names encoded in Windows formats if available   */    /*                                                                    */    convert = NULL;    if ( found_win >= 0 )    {      rec = face->name_table.names + found_win;      switch ( rec->encodingID )      {      case TT_MS_ID_UNICODE_CS:      case TT_MS_ID_SYMBOL_CS:        convert = tt_name_entry_ascii_from_utf16;        break;       case TT_MS_ID_UCS_4:        convert = tt_name_entry_ascii_from_ucs4;        break;       default:        ;      }    }    else if ( found_apple >= 0 )    {      rec     = face->name_table.names + found_apple;      convert = tt_name_entry_ascii_from_other;    }    else if ( found_unicode >= 0 )    {      rec     = face->name_table.names + found_unicode;      convert = tt_name_entry_ascii_from_utf16;    }     if ( rec && convert )    {      if ( rec->string == NULL )      {        FT_Error   error;        FT_Stream  stream = face->name_table.stream;          if ( FT_NEW_ARRAY  ( rec->string, rec->stringLength ) ||             FT_STREAM_SEEK( rec->stringOffset )              ||             FT_STREAM_READ( rec->string, rec->stringLength ) )        {          FT_FREE( rec->string );          rec->stringLength = 0;          result            = NULL;          goto Exit;        }      }       result = convert( rec, memory );    }   Exit:    return result;  }    static FT_Encoding  sfnt_find_encoding( int  platform_id,                      int  encoding_id )  {    typedef struct  TEncoding    {      int          platform_id;      int          encoding_id;      FT_Encoding  encoding;     } TEncoding;     static    const TEncoding  tt_encodings[] =    {      { TT_PLATFORM_ISO,           -1,                  FT_ENCODING_UNICODE },       { TT_PLATFORM_APPLE_UNICODE, -1,                  FT_ENCODING_UNICODE },       { TT_PLATFORM_MACINTOSH,     TT_MAC_ID_ROMAN,     FT_ENCODING_APPLE_ROMAN },       { TT_PLATFORM_MICROSOFT,     TT_MS_ID_SYMBOL_CS,  FT_ENCODING_MS_SYMBOL },      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_UCS_4,      FT_ENCODING_UNICODE },      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_UNICODE_CS, FT_ENCODING_UNICODE },      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_SJIS,       FT_ENCODING_MS_SJIS },      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_GB2312,     FT_ENCODING_MS_GB2312 },      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_BIG_5,      FT_ENCODING_MS_BIG5 },      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_WANSUNG,    FT_ENCODING_MS_WANSUNG },      { TT_PLATFORM_MICROSOFT,     TT_MS_ID_JOHAB,      FT_ENCODING_MS_JOHAB }    };     const TEncoding  *cur, *limit;      cur   = tt_encodings;    limit = cur + sizeof ( tt_encodings ) / sizeof ( tt_encodings[0] );     for ( ; cur < limit; cur++ )    {      if ( cur->platform_id == platform_id )      {        if ( cur->encoding_id == encoding_id ||             cur->encoding_id == -1          )          return cur->encoding;      }    }     return FT_ENCODING_NONE;  }    FT_LOCAL_DEF( FT_Error )  sfnt_init_face( FT_Stream      stream,                  TT_Face        face,                  FT_Int         face_index,                  FT_Int         num_params,                  FT_Parameter*  params )  {    FT_Error         error;    FT_Library       library = face->root.driver->root.library;    SFNT_Service     sfnt;    SFNT_HeaderRec   sfnt_header;     /* for now, parameters are unused */    FT_UNUSED( num_params );    FT_UNUSED( params );      sfnt = (SFNT_Service)face->sfnt;    if ( !sfnt )    {      sfnt = (SFNT_Service)FT_Get_Module_Interface( library, "sfnt" );      if ( !sfnt )      {        error = SFNT_Err_Invalid_File_Format;        goto Exit;      }       face->sfnt       = sfnt;      face->goto_table = sfnt->goto_table;    }     if ( !face->psnames )    {      face->psnames = (PSNames_Service)                        FT_Get_Module_Interface( library, "psnames" );    }     /* check that we have a valid TrueType file */    error = sfnt->load_sfnt_header( face, stream, face_index, &sfnt_header );    if ( error )      goto Exit;     face->format_tag = sfnt_header.format_tag;    face->num_tables = sfnt_header.num_tables;     /* Load font directory */    error = sfnt->load_directory( face, stream, &sfnt_header );    if ( error )      goto Exit;     face->root.num_faces = face->ttc_header.count;    if ( face->root.num_faces < 1 )      face->root.num_faces = 1;   Exit:    return error;  }  #undef  LOAD_#define LOAD_( x )  ( ( error = sfnt->load_##x( face, stream ) ) \                      != SFNT_Err_Ok )    FT_LOCAL_DEF( FT_Error )  sfnt_load_face( FT_Stream      stream,                  TT_Face        face,                  FT_Int         face_index,                  FT_Int         num_params,                  FT_Parameter*  params )  {    FT_Error      error;    FT_Bool       has_outline;    FT_Bool       is_apple_sbit;     SFNT_Service  sfnt = (SFNT_Service)face->sfnt;     FT_UNUSED( face_index );    FT_UNUSED( num_params );    FT_UNUSED( params );      /* Load tables */     /* We now support two SFNT-based bitmapped font formats.  They */    /* are recognized easily as they do not include a `glyf'       */    /* table.                                                      */    /*                                                             */    /* The first format comes from Apple, and uses a table named   */    /* `bhed' instead of `head' to store the font header (using    */    /* the same format).  It also doesn't include horizontal and   */    /* vertical metrics tables (i.e. `hhea' and `vhea' tables are  */    /* missing).                                                   */    /*                                                             */    /* The other format comes from Microsoft, and is used with     */    /* WinCE/PocketPC.  It looks like a standard TTF, except that  */    /* it doesn't contain outlines.                                */    /*                                                             */     /* do we have outlines in there? */#ifdef FT_CONFIG_OPTION_INCREMENTAL    has_outline   = FT_BOOL( face->root.internal->incremental_interface != 0 ||                             tt_face_lookup_table( face, TTAG_glyf ) != 0         ||                             tt_face_lookup_table( face, TTAG_CFF ) != 0          );#else    has_outline   = FT_BOOL( tt_face_lookup_table( face, TTAG_glyf ) != 0 ||                             tt_face_lookup_table( face, TTAG_CFF ) != 0  );#endif     is_apple_sbit = 0; #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS     /* if this font doesn't contain outlines, we try to load */    /* a `bhed' table                                        */    if ( !has_outline )      is_apple_sbit = FT_BOOL( !LOAD_( bitmap_header ) ); #endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */     /* load the font header (`head' table) if this isn't an Apple */    /* sbit font file                                             */    if ( !is_apple_sbit && LOAD_( header ) )      goto Exit;     /* the following tables are often not present in embedded TrueType */    /* fonts within PDF documents, so don't check for them.            */    (void)LOAD_( max_profile );    (void)LOAD_( charmaps );     /* the following tables are optional in PCL fonts -- */    /* don't check for errors                            */    (void)LOAD_( names );    (void)LOAD_( psnames );     /* do not load the metrics headers and tables if this is an Apple */    /* sbit font file                                                 */    if ( !is_apple_sbit )    {      /* load the `hhea' and `hmtx' tables at once */      error = sfnt->load_metrics( face, stream, 0 );      if ( error )        goto Exit;       /* try to load the `vhea' and `vmtx' tables at once */      error = sfnt->load_metrics( face, stream, 1 );      if ( error )        goto Exit;       if ( LOAD_( os2 ) )        goto Exit;    }     /* the optional tables */ #ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS     /* embedded bitmap support. */    if ( sfnt->load_sbits && LOAD_( sbits ) )    {      /* return an error if this font file has no outlines */      if ( error == SFNT_Err_Table_Missing && has_outline )        error = SFNT_Err_Ok;      else        goto Exit;    }

Lines 1–500 of 829.