PageRenderTime 715ms CodeModel.GetById 201ms app.highlight 320ms RepoModel.GetById 183ms app.codeStats 1ms

/src/compiler/android-ndk/jni/freetype/src/sfnt/ttload.c

http://ftk.googlecode.com/
C | 1251 lines | 722 code | 197 blank | 332 comment | 90 complexity | 3acd8afad87fc63c4e20788590ae00ed MD5 | raw file
   1/***************************************************************************/
   2/*                                                                         */
   3/*  ttload.c                                                               */
   4/*                                                                         */
   5/*    Load the basic TrueType tables, i.e., tables that can be either in   */
   6/*    TTF or OTF fonts (body).                                             */
   7/*                                                                         */
   8/*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by */
   9/*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
  10/*                                                                         */
  11/*  This file is part of the FreeType project, and may only be used,       */
  12/*  modified, and distributed under the terms of the FreeType project      */
  13/*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
  14/*  this file you indicate that you have read the license and              */
  15/*  understand and accept it fully.                                        */
  16/*                                                                         */
  17/***************************************************************************/
  18
  19
  20#include <ft2build.h>
  21#include FT_INTERNAL_DEBUG_H
  22#include FT_INTERNAL_STREAM_H
  23#include FT_TRUETYPE_TAGS_H
  24#include "ttload.h"
  25
  26#include "sferrors.h"
  27
  28
  29  /*************************************************************************/
  30  /*                                                                       */
  31  /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
  32  /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
  33  /* messages during execution.                                            */
  34  /*                                                                       */
  35#undef  FT_COMPONENT
  36#define FT_COMPONENT  trace_ttload
  37
  38
  39  /*************************************************************************/
  40  /*                                                                       */
  41  /* <Function>                                                            */
  42  /*    tt_face_lookup_table                                               */
  43  /*                                                                       */
  44  /* <Description>                                                         */
  45  /*    Looks for a TrueType table by name.                                */
  46  /*                                                                       */
  47  /* <Input>                                                               */
  48  /*    face :: A face object handle.                                      */
  49  /*                                                                       */
  50  /*    tag  :: The searched tag.                                          */
  51  /*                                                                       */
  52  /* <Return>                                                              */
  53  /*    A pointer to the table directory entry.  0 if not found.           */
  54  /*                                                                       */
  55  FT_LOCAL_DEF( TT_Table  )
  56  tt_face_lookup_table( TT_Face   face,
  57                        FT_ULong  tag  )
  58  {
  59    TT_Table  entry;
  60    TT_Table  limit;
  61#ifdef FT_DEBUG_LEVEL_TRACE
  62    FT_Bool   zero_length = FALSE;
  63#endif
  64
  65
  66    FT_TRACE4(( "tt_face_lookup_table: %08p, `%c%c%c%c' -- ",
  67                face,
  68                (FT_Char)( tag >> 24 ),
  69                (FT_Char)( tag >> 16 ),
  70                (FT_Char)( tag >> 8  ),
  71                (FT_Char)( tag       ) ));
  72
  73    entry = face->dir_tables;
  74    limit = entry + face->num_tables;
  75
  76    for ( ; entry < limit; entry++ )
  77    {
  78      /* For compatibility with Windows, we consider    */
  79      /* zero-length tables the same as missing tables. */
  80      if ( entry->Tag == tag ) {
  81        if ( entry->Length != 0 )
  82        {
  83          FT_TRACE4(( "found table.\n" ));
  84          return entry;
  85        }
  86#ifdef FT_DEBUG_LEVEL_TRACE
  87        zero_length = TRUE;
  88#endif
  89      }
  90    }
  91
  92#ifdef FT_DEBUG_LEVEL_TRACE
  93    if ( zero_length )
  94      FT_TRACE4(( "ignoring empty table\n" ));
  95    else
  96      FT_TRACE4(( "could not find table\n" ));
  97#endif
  98
  99    return NULL;
 100  }
 101
 102
 103  /*************************************************************************/
 104  /*                                                                       */
 105  /* <Function>                                                            */
 106  /*    tt_face_goto_table                                                 */
 107  /*                                                                       */
 108  /* <Description>                                                         */
 109  /*    Looks for a TrueType table by name, then seek a stream to it.      */
 110  /*                                                                       */
 111  /* <Input>                                                               */
 112  /*    face   :: A face object handle.                                    */
 113  /*                                                                       */
 114  /*    tag    :: The searched tag.                                        */
 115  /*                                                                       */
 116  /*    stream :: The stream to seek when the table is found.              */
 117  /*                                                                       */
 118  /* <Output>                                                              */
 119  /*    length :: The length of the table if found, undefined otherwise.   */
 120  /*                                                                       */
 121  /* <Return>                                                              */
 122  /*    FreeType error code.  0 means success.                             */
 123  /*                                                                       */
 124  FT_LOCAL_DEF( FT_Error )
 125  tt_face_goto_table( TT_Face    face,
 126                      FT_ULong   tag,
 127                      FT_Stream  stream,
 128                      FT_ULong*  length )
 129  {
 130    TT_Table  table;
 131    FT_Error  error;
 132
 133
 134    table = tt_face_lookup_table( face, tag );
 135    if ( table )
 136    {
 137      if ( length )
 138        *length = table->Length;
 139
 140      if ( FT_STREAM_SEEK( table->Offset ) )
 141        goto Exit;
 142    }
 143    else
 144      error = SFNT_Err_Table_Missing;
 145
 146  Exit:
 147    return error;
 148  }
 149
 150
 151  /* Here, we                                                         */
 152  /*                                                                  */
 153  /* - check that `num_tables' is valid (and adjust it if necessary)  */
 154  /*                                                                  */
 155  /* - look for a `head' table, check its size, and parse it to check */
 156  /*   whether its `magic' field is correctly set                     */
 157  /*                                                                  */
 158  /* - errors (except errors returned by stream handling)             */
 159  /*                                                                  */
 160  /*     SFNT_Err_Unknown_File_Format:                                */
 161  /*       no table is defined in directory, it is not sfnt-wrapped   */
 162  /*       data                                                       */
 163  /*     SFNT_Err_Table_Missing:                                      */
 164  /*       table directory is valid, but essential tables             */
 165  /*       (head/bhed/SING) are missing                               */
 166  /*                                                                  */
 167  static FT_Error
 168  check_table_dir( SFNT_Header  sfnt,
 169                   FT_Stream    stream )
 170  {
 171    FT_Error   error;
 172    FT_UShort  nn, valid_entries = 0;
 173    FT_UInt    has_head = 0, has_sing = 0, has_meta = 0;
 174    FT_ULong   offset = sfnt->offset + 12;
 175
 176    static const FT_Frame_Field  table_dir_entry_fields[] =
 177    {
 178#undef  FT_STRUCTURE
 179#define FT_STRUCTURE  TT_TableRec
 180
 181      FT_FRAME_START( 16 ),
 182        FT_FRAME_ULONG( Tag ),
 183        FT_FRAME_ULONG( CheckSum ),
 184        FT_FRAME_ULONG( Offset ),
 185        FT_FRAME_ULONG( Length ),
 186      FT_FRAME_END
 187    };
 188
 189
 190    if ( FT_STREAM_SEEK( offset ) )
 191      goto Exit;
 192
 193    for ( nn = 0; nn < sfnt->num_tables; nn++ )
 194    {
 195      TT_TableRec  table;
 196
 197
 198      if ( FT_STREAM_READ_FIELDS( table_dir_entry_fields, &table ) )
 199      {
 200        nn--;
 201        FT_TRACE2(( "check_table_dir:"
 202                    " can read only %d table%s in font (instead of %d)\n",
 203                    nn, nn == 1 ? "" : "s", sfnt->num_tables ));
 204        sfnt->num_tables = nn;
 205        break;
 206      }
 207
 208      /* we ignore invalid tables */
 209      if ( table.Offset + table.Length > stream->size )
 210      {
 211        FT_TRACE2(( "check_table_dir: table entry %d invalid\n", nn ));
 212        continue;
 213      }
 214      else
 215        valid_entries++;
 216
 217      if ( table.Tag == TTAG_head || table.Tag == TTAG_bhed )
 218      {
 219        FT_UInt32  magic;
 220
 221
 222#ifndef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
 223        if ( table.Tag == TTAG_head )
 224#endif
 225          has_head = 1;
 226
 227        /*
 228         * The table length should be 0x36, but certain font tools make it
 229         * 0x38, so we will just check that it is greater.
 230         *
 231         * Note that according to the specification, the table must be
 232         * padded to 32-bit lengths, but this doesn't apply to the value of
 233         * its `Length' field!
 234         *
 235         */
 236        if ( table.Length < 0x36 )
 237        {
 238          FT_TRACE2(( "check_table_dir: `head' table too small\n" ));
 239          error = SFNT_Err_Table_Missing;
 240          goto Exit;
 241        }
 242
 243        if ( FT_STREAM_SEEK( table.Offset + 12 ) ||
 244             FT_READ_ULONG( magic )              )
 245          goto Exit;
 246
 247        if ( magic != 0x5F0F3CF5UL )
 248        {
 249          FT_TRACE2(( "check_table_dir:"
 250                      " no magic number found in `head' table\n"));
 251          error = SFNT_Err_Table_Missing;
 252          goto Exit;
 253        }
 254
 255        if ( FT_STREAM_SEEK( offset + ( nn + 1 ) * 16 ) )
 256          goto Exit;
 257      }
 258      else if ( table.Tag == TTAG_SING )
 259        has_sing = 1;
 260      else if ( table.Tag == TTAG_META )
 261        has_meta = 1;
 262    }
 263
 264    sfnt->num_tables = valid_entries;
 265
 266    if ( sfnt->num_tables == 0 )
 267    {
 268      FT_TRACE2(( "check_table_dir: no tables found\n" ));
 269      error = SFNT_Err_Unknown_File_Format;
 270      goto Exit;
 271    }
 272
 273    /* if `sing' and `meta' tables are present, there is no `head' table */
 274    if ( has_head || ( has_sing && has_meta ) )
 275    {
 276      error = SFNT_Err_Ok;
 277      goto Exit;
 278    }
 279    else
 280    {
 281      FT_TRACE2(( "check_table_dir:" ));
 282#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
 283      FT_TRACE2(( " neither `head', `bhed', nor `sing' table found\n" ));
 284#else
 285      FT_TRACE2(( " neither `head' nor `sing' table found\n" ));
 286#endif
 287      error = SFNT_Err_Table_Missing;
 288    }
 289
 290  Exit:
 291    return error;
 292  }
 293
 294
 295  /*************************************************************************/
 296  /*                                                                       */
 297  /* <Function>                                                            */
 298  /*    tt_face_load_font_dir                                              */
 299  /*                                                                       */
 300  /* <Description>                                                         */
 301  /*    Loads the header of a SFNT font file.                              */
 302  /*                                                                       */
 303  /* <Input>                                                               */
 304  /*    face       :: A handle to the target face object.                  */
 305  /*                                                                       */
 306  /*    stream     :: The input stream.                                    */
 307  /*                                                                       */
 308  /* <Output>                                                              */
 309  /*    sfnt       :: The SFNT header.                                     */
 310  /*                                                                       */
 311  /* <Return>                                                              */
 312  /*    FreeType error code.  0 means success.                             */
 313  /*                                                                       */
 314  /* <Note>                                                                */
 315  /*    The stream cursor must be at the beginning of the font directory.  */
 316  /*                                                                       */
 317  FT_LOCAL_DEF( FT_Error )
 318  tt_face_load_font_dir( TT_Face    face,
 319                         FT_Stream  stream )
 320  {
 321    SFNT_HeaderRec  sfnt;
 322    FT_Error        error;
 323    FT_Memory       memory = stream->memory;
 324    TT_TableRec*    entry;
 325    FT_Int          nn;
 326
 327    static const FT_Frame_Field  offset_table_fields[] =
 328    {
 329#undef  FT_STRUCTURE
 330#define FT_STRUCTURE  SFNT_HeaderRec
 331
 332      FT_FRAME_START( 8 ),
 333        FT_FRAME_USHORT( num_tables ),
 334        FT_FRAME_USHORT( search_range ),
 335        FT_FRAME_USHORT( entry_selector ),
 336        FT_FRAME_USHORT( range_shift ),
 337      FT_FRAME_END
 338    };
 339
 340
 341    FT_TRACE2(( "tt_face_load_font_dir: %08p\n", face ));
 342
 343    /* read the offset table */
 344
 345    sfnt.offset = FT_STREAM_POS();
 346
 347    if ( FT_READ_ULONG( sfnt.format_tag )                    ||
 348         FT_STREAM_READ_FIELDS( offset_table_fields, &sfnt ) )
 349      goto Exit;
 350
 351    /* many fonts don't have these fields set correctly */
 352#if 0
 353    if ( sfnt.search_range != 1 << ( sfnt.entry_selector + 4 )        ||
 354         sfnt.search_range + sfnt.range_shift != sfnt.num_tables << 4 )
 355      return SFNT_Err_Unknown_File_Format;
 356#endif
 357
 358    /* load the table directory */
 359
 360    FT_TRACE2(( "-- Number of tables: %10u\n",    sfnt.num_tables ));
 361    FT_TRACE2(( "-- Format version:   0x%08lx\n", sfnt.format_tag ));
 362
 363    /* check first */
 364    error = check_table_dir( &sfnt, stream );
 365    if ( error )
 366    {
 367      FT_TRACE2(( "tt_face_load_font_dir:"
 368                  " invalid table directory for TrueType\n" ));
 369
 370      goto Exit;
 371    }
 372
 373    face->num_tables = sfnt.num_tables;
 374    face->format_tag = sfnt.format_tag;
 375
 376    if ( FT_QNEW_ARRAY( face->dir_tables, face->num_tables ) )
 377      goto Exit;
 378
 379    if ( FT_STREAM_SEEK( sfnt.offset + 12 )       ||
 380         FT_FRAME_ENTER( face->num_tables * 16L ) )
 381      goto Exit;
 382
 383    entry = face->dir_tables;
 384
 385    for ( nn = 0; nn < sfnt.num_tables; nn++ )
 386    {
 387      entry->Tag      = FT_GET_TAG4();
 388      entry->CheckSum = FT_GET_ULONG();
 389      entry->Offset   = FT_GET_LONG();
 390      entry->Length   = FT_GET_LONG();
 391
 392      /* ignore invalid tables */
 393      if ( entry->Offset + entry->Length > stream->size )
 394        continue;
 395      else
 396      {
 397        FT_TRACE2(( "  %c%c%c%c  -  %08lx  -  %08lx\n",
 398                    (FT_Char)( entry->Tag >> 24 ),
 399                    (FT_Char)( entry->Tag >> 16 ),
 400                    (FT_Char)( entry->Tag >> 8  ),
 401                    (FT_Char)( entry->Tag       ),
 402                    entry->Offset,
 403                    entry->Length ));
 404        entry++;
 405      }
 406    }
 407
 408    FT_FRAME_EXIT();
 409
 410    FT_TRACE2(( "table directory loaded\n\n" ));
 411
 412  Exit:
 413    return error;
 414  }
 415
 416
 417  /*************************************************************************/
 418  /*                                                                       */
 419  /* <Function>                                                            */
 420  /*    tt_face_load_any                                                   */
 421  /*                                                                       */
 422  /* <Description>                                                         */
 423  /*    Loads any font table into client memory.                           */
 424  /*                                                                       */
 425  /* <Input>                                                               */
 426  /*    face   :: The face object to look for.                             */
 427  /*                                                                       */
 428  /*    tag    :: The tag of table to load.  Use the value 0 if you want   */
 429  /*              to access the whole font file, else set this parameter   */
 430  /*              to a valid TrueType table tag that you can forge with    */
 431  /*              the MAKE_TT_TAG macro.                                   */
 432  /*                                                                       */
 433  /*    offset :: The starting offset in the table (or the file if         */
 434  /*              tag == 0).                                               */
 435  /*                                                                       */
 436  /*    length :: The address of the decision variable:                    */
 437  /*                                                                       */
 438  /*                If length == NULL:                                     */
 439  /*                  Loads the whole table.  Returns an error if          */
 440  /*                  `offset' == 0!                                       */
 441  /*                                                                       */
 442  /*                If *length == 0:                                       */
 443  /*                  Exits immediately; returning the length of the given */
 444  /*                  table or of the font file, depending on the value of */
 445  /*                  `tag'.                                               */
 446  /*                                                                       */
 447  /*                If *length != 0:                                       */
 448  /*                  Loads the next `length' bytes of table or font,      */
 449  /*                  starting at offset `offset' (in table or font too).  */
 450  /*                                                                       */
 451  /* <Output>                                                              */
 452  /*    buffer :: The address of target buffer.                            */
 453  /*                                                                       */
 454  /* <Return>                                                              */
 455  /*    FreeType error code.  0 means success.                             */
 456  /*                                                                       */
 457  FT_LOCAL_DEF( FT_Error )
 458  tt_face_load_any( TT_Face    face,
 459                    FT_ULong   tag,
 460                    FT_Long    offset,
 461                    FT_Byte*   buffer,
 462                    FT_ULong*  length )
 463  {
 464    FT_Error   error;
 465    FT_Stream  stream;
 466    TT_Table   table;
 467    FT_ULong   size;
 468
 469
 470    if ( tag != 0 )
 471    {
 472      /* look for tag in font directory */
 473      table = tt_face_lookup_table( face, tag );
 474      if ( !table )
 475      {
 476        error = SFNT_Err_Table_Missing;
 477        goto Exit;
 478      }
 479
 480      offset += table->Offset;
 481      size    = table->Length;
 482    }
 483    else
 484      /* tag == 0 -- the user wants to access the font file directly */
 485      size = face->root.stream->size;
 486
 487    if ( length && *length == 0 )
 488    {
 489      *length = size;
 490
 491      return SFNT_Err_Ok;
 492    }
 493
 494    if ( length )
 495      size = *length;
 496
 497    stream = face->root.stream;
 498    /* the `if' is syntactic sugar for picky compilers */
 499    if ( FT_STREAM_READ_AT( offset, buffer, size ) )
 500      goto Exit;
 501
 502  Exit:
 503    return error;
 504  }
 505
 506
 507  /*************************************************************************/
 508  /*                                                                       */
 509  /* <Function>                                                            */
 510  /*    tt_face_load_generic_header                                        */
 511  /*                                                                       */
 512  /* <Description>                                                         */
 513  /*    Loads the TrueType table `head' or `bhed'.                         */
 514  /*                                                                       */
 515  /* <Input>                                                               */
 516  /*    face   :: A handle to the target face object.                      */
 517  /*                                                                       */
 518  /*    stream :: The input stream.                                        */
 519  /*                                                                       */
 520  /* <Return>                                                              */
 521  /*    FreeType error code.  0 means success.                             */
 522  /*                                                                       */
 523  static FT_Error
 524  tt_face_load_generic_header( TT_Face    face,
 525                               FT_Stream  stream,
 526                               FT_ULong   tag )
 527  {
 528    FT_Error    error;
 529    TT_Header*  header;
 530
 531    static const FT_Frame_Field  header_fields[] =
 532    {
 533#undef  FT_STRUCTURE
 534#define FT_STRUCTURE  TT_Header
 535
 536      FT_FRAME_START( 54 ),
 537        FT_FRAME_ULONG ( Table_Version ),
 538        FT_FRAME_ULONG ( Font_Revision ),
 539        FT_FRAME_LONG  ( CheckSum_Adjust ),
 540        FT_FRAME_LONG  ( Magic_Number ),
 541        FT_FRAME_USHORT( Flags ),
 542        FT_FRAME_USHORT( Units_Per_EM ),
 543        FT_FRAME_LONG  ( Created[0] ),
 544        FT_FRAME_LONG  ( Created[1] ),
 545        FT_FRAME_LONG  ( Modified[0] ),
 546        FT_FRAME_LONG  ( Modified[1] ),
 547        FT_FRAME_SHORT ( xMin ),
 548        FT_FRAME_SHORT ( yMin ),
 549        FT_FRAME_SHORT ( xMax ),
 550        FT_FRAME_SHORT ( yMax ),
 551        FT_FRAME_USHORT( Mac_Style ),
 552        FT_FRAME_USHORT( Lowest_Rec_PPEM ),
 553        FT_FRAME_SHORT ( Font_Direction ),
 554        FT_FRAME_SHORT ( Index_To_Loc_Format ),
 555        FT_FRAME_SHORT ( Glyph_Data_Format ),
 556      FT_FRAME_END
 557    };
 558
 559
 560    error = face->goto_table( face, tag, stream, 0 );
 561    if ( error )
 562      goto Exit;
 563
 564    header = &face->header;
 565
 566    if ( FT_STREAM_READ_FIELDS( header_fields, header ) )
 567      goto Exit;
 568
 569    FT_TRACE3(( "Units per EM: %4u\n", header->Units_Per_EM ));
 570    FT_TRACE3(( "IndexToLoc:   %4d\n", header->Index_To_Loc_Format ));
 571
 572  Exit:
 573    return error;
 574  }
 575
 576
 577  FT_LOCAL_DEF( FT_Error )
 578  tt_face_load_head( TT_Face    face,
 579                     FT_Stream  stream )
 580  {
 581    return tt_face_load_generic_header( face, stream, TTAG_head );
 582  }
 583
 584
 585#ifdef TT_CONFIG_OPTION_EMBEDDED_BITMAPS
 586
 587  FT_LOCAL_DEF( FT_Error )
 588  tt_face_load_bhed( TT_Face    face,
 589                     FT_Stream  stream )
 590  {
 591    return tt_face_load_generic_header( face, stream, TTAG_bhed );
 592  }
 593
 594#endif /* TT_CONFIG_OPTION_EMBEDDED_BITMAPS */
 595
 596
 597  /*************************************************************************/
 598  /*                                                                       */
 599  /* <Function>                                                            */
 600  /*    tt_face_load_max_profile                                           */
 601  /*                                                                       */
 602  /* <Description>                                                         */
 603  /*    Loads the maximum profile into a face object.                      */
 604  /*                                                                       */
 605  /* <Input>                                                               */
 606  /*    face   :: A handle to the target face object.                      */
 607  /*                                                                       */
 608  /*    stream :: The input stream.                                        */
 609  /*                                                                       */
 610  /* <Return>                                                              */
 611  /*    FreeType error code.  0 means success.                             */
 612  /*                                                                       */
 613  FT_LOCAL_DEF( FT_Error )
 614  tt_face_load_maxp( TT_Face    face,
 615                     FT_Stream  stream )
 616  {
 617    FT_Error        error;
 618    TT_MaxProfile*  maxProfile = &face->max_profile;
 619
 620    const FT_Frame_Field  maxp_fields[] =
 621    {
 622#undef  FT_STRUCTURE
 623#define FT_STRUCTURE  TT_MaxProfile
 624
 625      FT_FRAME_START( 6 ),
 626        FT_FRAME_LONG  ( version ),
 627        FT_FRAME_USHORT( numGlyphs ),
 628      FT_FRAME_END
 629    };
 630
 631    const FT_Frame_Field  maxp_fields_extra[] =
 632    {
 633      FT_FRAME_START( 26 ),
 634        FT_FRAME_USHORT( maxPoints ),
 635        FT_FRAME_USHORT( maxContours ),
 636        FT_FRAME_USHORT( maxCompositePoints ),
 637        FT_FRAME_USHORT( maxCompositeContours ),
 638        FT_FRAME_USHORT( maxZones ),
 639        FT_FRAME_USHORT( maxTwilightPoints ),
 640        FT_FRAME_USHORT( maxStorage ),
 641        FT_FRAME_USHORT( maxFunctionDefs ),
 642        FT_FRAME_USHORT( maxInstructionDefs ),
 643        FT_FRAME_USHORT( maxStackElements ),
 644        FT_FRAME_USHORT( maxSizeOfInstructions ),
 645        FT_FRAME_USHORT( maxComponentElements ),
 646        FT_FRAME_USHORT( maxComponentDepth ),
 647      FT_FRAME_END
 648    };
 649
 650
 651    error = face->goto_table( face, TTAG_maxp, stream, 0 );
 652    if ( error )
 653      goto Exit;
 654
 655    if ( FT_STREAM_READ_FIELDS( maxp_fields, maxProfile ) )
 656      goto Exit;
 657
 658    maxProfile->maxPoints             = 0;
 659    maxProfile->maxContours           = 0;
 660    maxProfile->maxCompositePoints    = 0;
 661    maxProfile->maxCompositeContours  = 0;
 662    maxProfile->maxZones              = 0;
 663    maxProfile->maxTwilightPoints     = 0;
 664    maxProfile->maxStorage            = 0;
 665    maxProfile->maxFunctionDefs       = 0;
 666    maxProfile->maxInstructionDefs    = 0;
 667    maxProfile->maxStackElements      = 0;
 668    maxProfile->maxSizeOfInstructions = 0;
 669    maxProfile->maxComponentElements  = 0;
 670    maxProfile->maxComponentDepth     = 0;
 671
 672    if ( maxProfile->version >= 0x10000L )
 673    {
 674      if ( FT_STREAM_READ_FIELDS( maxp_fields_extra, maxProfile ) )
 675        goto Exit;
 676
 677      /* XXX: an adjustment that is necessary to load certain */
 678      /*      broken fonts like `Keystrokes MT' :-(           */
 679      /*                                                      */
 680      /*   We allocate 64 function entries by default when    */
 681      /*   the maxFunctionDefs field is null.                 */
 682
 683      if ( maxProfile->maxFunctionDefs == 0 )
 684        maxProfile->maxFunctionDefs = 64;
 685
 686      /* we add 4 phantom points later */
 687      if ( maxProfile->maxTwilightPoints > ( 0xFFFFU - 4 ) )
 688      {
 689        FT_TRACE0(( "tt_face_load_maxp:"
 690                    " too much twilight points in `maxp' table;\n"
 691                    "                  "
 692                    " some glyphs might be rendered incorrectly\n" ));
 693
 694        maxProfile->maxTwilightPoints = 0xFFFFU - 4;
 695      }
 696    }
 697
 698    FT_TRACE3(( "numGlyphs: %u\n", maxProfile->numGlyphs ));
 699
 700  Exit:
 701    return error;
 702  }
 703
 704
 705  /*************************************************************************/
 706  /*                                                                       */
 707  /* <Function>                                                            */
 708  /*    tt_face_load_names                                                 */
 709  /*                                                                       */
 710  /* <Description>                                                         */
 711  /*    Loads the name records.                                            */
 712  /*                                                                       */
 713  /* <Input>                                                               */
 714  /*    face   :: A handle to the target face object.                      */
 715  /*                                                                       */
 716  /*    stream :: The input stream.                                        */
 717  /*                                                                       */
 718  /* <Return>                                                              */
 719  /*    FreeType error code.  0 means success.                             */
 720  /*                                                                       */
 721  FT_LOCAL_DEF( FT_Error )
 722  tt_face_load_name( TT_Face    face,
 723                     FT_Stream  stream )
 724  {
 725    FT_Error      error;
 726    FT_Memory     memory = stream->memory;
 727    FT_ULong      table_pos, table_len;
 728    FT_ULong      storage_start, storage_limit;
 729    FT_UInt       count;
 730    TT_NameTable  table;
 731
 732    static const FT_Frame_Field  name_table_fields[] =
 733    {
 734#undef  FT_STRUCTURE
 735#define FT_STRUCTURE  TT_NameTableRec
 736
 737      FT_FRAME_START( 6 ),
 738        FT_FRAME_USHORT( format ),
 739        FT_FRAME_USHORT( numNameRecords ),
 740        FT_FRAME_USHORT( storageOffset ),
 741      FT_FRAME_END
 742    };
 743
 744    static const FT_Frame_Field  name_record_fields[] =
 745    {
 746#undef  FT_STRUCTURE
 747#define FT_STRUCTURE  TT_NameEntryRec
 748
 749      /* no FT_FRAME_START */
 750        FT_FRAME_USHORT( platformID ),
 751        FT_FRAME_USHORT( encodingID ),
 752        FT_FRAME_USHORT( languageID ),
 753        FT_FRAME_USHORT( nameID ),
 754        FT_FRAME_USHORT( stringLength ),
 755        FT_FRAME_USHORT( stringOffset ),
 756      FT_FRAME_END
 757    };
 758
 759
 760    table         = &face->name_table;
 761    table->stream = stream;
 762
 763    error = face->goto_table( face, TTAG_name, stream, &table_len );
 764    if ( error )
 765      goto Exit;
 766
 767    table_pos = FT_STREAM_POS();
 768
 769
 770    if ( FT_STREAM_READ_FIELDS( name_table_fields, table ) )
 771      goto Exit;
 772
 773    /* Some popular Asian fonts have an invalid `storageOffset' value   */
 774    /* (it should be at least "6 + 12*num_names").  However, the string */
 775    /* offsets, computed as "storageOffset + entry->stringOffset", are  */
 776    /* valid pointers within the name table...                          */
 777    /*                                                                  */
 778    /* We thus can't check `storageOffset' right now.                   */
 779    /*                                                                  */
 780    storage_start = table_pos + 6 + 12*table->numNameRecords;
 781    storage_limit = table_pos + table_len;
 782
 783    if ( storage_start > storage_limit )
 784    {
 785      FT_ERROR(( "tt_face_load_name: invalid `name' table\n" ));
 786      error = SFNT_Err_Name_Table_Missing;
 787      goto Exit;
 788    }
 789
 790    /* Allocate the array of name records. */
 791    count                 = table->numNameRecords;
 792    table->numNameRecords = 0;
 793
 794    if ( FT_NEW_ARRAY( table->names, count ) ||
 795         FT_FRAME_ENTER( count * 12 )        )
 796      goto Exit;
 797
 798    /* Load the name records and determine how much storage is needed */
 799    /* to hold the strings themselves.                                */
 800    {
 801      TT_NameEntryRec*  entry = table->names;
 802
 803
 804      for ( ; count > 0; count-- )
 805      {
 806        if ( FT_STREAM_READ_FIELDS( name_record_fields, entry ) )
 807          continue;
 808
 809        /* check that the name is not empty */
 810        if ( entry->stringLength == 0 )
 811          continue;
 812
 813        /* check that the name string is within the table */
 814        entry->stringOffset += table_pos + table->storageOffset;
 815        if ( entry->stringOffset                       < storage_start ||
 816             entry->stringOffset + entry->stringLength > storage_limit )
 817        {
 818          /* invalid entry - ignore it */
 819          entry->stringOffset = 0;
 820          entry->stringLength = 0;
 821          continue;
 822        }
 823
 824        entry++;
 825      }
 826
 827      table->numNameRecords = (FT_UInt)( entry - table->names );
 828    }
 829
 830    FT_FRAME_EXIT();
 831
 832    /* everything went well, update face->num_names */
 833    face->num_names = (FT_UShort) table->numNameRecords;
 834
 835  Exit:
 836    return error;
 837  }
 838
 839
 840  /*************************************************************************/
 841  /*                                                                       */
 842  /* <Function>                                                            */
 843  /*    tt_face_free_names                                                 */
 844  /*                                                                       */
 845  /* <Description>                                                         */
 846  /*    Frees the name records.                                            */
 847  /*                                                                       */
 848  /* <Input>                                                               */
 849  /*    face :: A handle to the target face object.                        */
 850  /*                                                                       */
 851  FT_LOCAL_DEF( void )
 852  tt_face_free_name( TT_Face  face )
 853  {
 854    FT_Memory     memory = face->root.driver->root.memory;
 855    TT_NameTable  table  = &face->name_table;
 856    TT_NameEntry  entry  = table->names;
 857    FT_UInt       count  = table->numNameRecords;
 858
 859
 860    if ( table->names )
 861    {
 862      for ( ; count > 0; count--, entry++ )
 863      {
 864        FT_FREE( entry->string );
 865        entry->stringLength = 0;
 866      }
 867
 868      /* free strings table */
 869      FT_FREE( table->names );
 870    }
 871
 872    table->numNameRecords = 0;
 873    table->format         = 0;
 874    table->storageOffset  = 0;
 875  }
 876
 877
 878  /*************************************************************************/
 879  /*                                                                       */
 880  /* <Function>                                                            */
 881  /*    tt_face_load_cmap                                                  */
 882  /*                                                                       */
 883  /* <Description>                                                         */
 884  /*    Loads the cmap directory in a face object.  The cmaps themselves   */
 885  /*    are loaded on demand in the `ttcmap.c' module.                     */
 886  /*                                                                       */
 887  /* <Input>                                                               */
 888  /*    face   :: A handle to the target face object.                      */
 889  /*                                                                       */
 890  /*    stream :: A handle to the input stream.                            */
 891  /*                                                                       */
 892  /* <Return>                                                              */
 893  /*    FreeType error code.  0 means success.                             */
 894  /*                                                                       */
 895
 896  FT_LOCAL_DEF( FT_Error )
 897  tt_face_load_cmap( TT_Face    face,
 898                     FT_Stream  stream )
 899  {
 900    FT_Error  error;
 901
 902
 903    error = face->goto_table( face, TTAG_cmap, stream, &face->cmap_size );
 904    if ( error )
 905      goto Exit;
 906
 907    if ( FT_FRAME_EXTRACT( face->cmap_size, face->cmap_table ) )
 908      face->cmap_size = 0;
 909
 910  Exit:
 911    return error;
 912  }
 913
 914
 915
 916  /*************************************************************************/
 917  /*                                                                       */
 918  /* <Function>                                                            */
 919  /*    tt_face_load_os2                                                   */
 920  /*                                                                       */
 921  /* <Description>                                                         */
 922  /*    Loads the OS2 table.                                               */
 923  /*                                                                       */
 924  /* <Input>                                                               */
 925  /*    face   :: A handle to the target face object.                      */
 926  /*                                                                       */
 927  /*    stream :: A handle to the input stream.                            */
 928  /*                                                                       */
 929  /* <Return>                                                              */
 930  /*    FreeType error code.  0 means success.                             */
 931  /*                                                                       */
 932  FT_LOCAL_DEF( FT_Error )
 933  tt_face_load_os2( TT_Face    face,
 934                    FT_Stream  stream )
 935  {
 936    FT_Error  error;
 937    TT_OS2*   os2;
 938
 939    const FT_Frame_Field  os2_fields[] =
 940    {
 941#undef  FT_STRUCTURE
 942#define FT_STRUCTURE  TT_OS2
 943
 944      FT_FRAME_START( 78 ),
 945        FT_FRAME_USHORT( version ),
 946        FT_FRAME_SHORT ( xAvgCharWidth ),
 947        FT_FRAME_USHORT( usWeightClass ),
 948        FT_FRAME_USHORT( usWidthClass ),
 949        FT_FRAME_SHORT ( fsType ),
 950        FT_FRAME_SHORT ( ySubscriptXSize ),
 951        FT_FRAME_SHORT ( ySubscriptYSize ),
 952        FT_FRAME_SHORT ( ySubscriptXOffset ),
 953        FT_FRAME_SHORT ( ySubscriptYOffset ),
 954        FT_FRAME_SHORT ( ySuperscriptXSize ),
 955        FT_FRAME_SHORT ( ySuperscriptYSize ),
 956        FT_FRAME_SHORT ( ySuperscriptXOffset ),
 957        FT_FRAME_SHORT ( ySuperscriptYOffset ),
 958        FT_FRAME_SHORT ( yStrikeoutSize ),
 959        FT_FRAME_SHORT ( yStrikeoutPosition ),
 960        FT_FRAME_SHORT ( sFamilyClass ),
 961        FT_FRAME_BYTE  ( panose[0] ),
 962        FT_FRAME_BYTE  ( panose[1] ),
 963        FT_FRAME_BYTE  ( panose[2] ),
 964        FT_FRAME_BYTE  ( panose[3] ),
 965        FT_FRAME_BYTE  ( panose[4] ),
 966        FT_FRAME_BYTE  ( panose[5] ),
 967        FT_FRAME_BYTE  ( panose[6] ),
 968        FT_FRAME_BYTE  ( panose[7] ),
 969        FT_FRAME_BYTE  ( panose[8] ),
 970        FT_FRAME_BYTE  ( panose[9] ),
 971        FT_FRAME_ULONG ( ulUnicodeRange1 ),
 972        FT_FRAME_ULONG ( ulUnicodeRange2 ),
 973        FT_FRAME_ULONG ( ulUnicodeRange3 ),
 974        FT_FRAME_ULONG ( ulUnicodeRange4 ),
 975        FT_FRAME_BYTE  ( achVendID[0] ),
 976        FT_FRAME_BYTE  ( achVendID[1] ),
 977        FT_FRAME_BYTE  ( achVendID[2] ),
 978        FT_FRAME_BYTE  ( achVendID[3] ),
 979
 980        FT_FRAME_USHORT( fsSelection ),
 981        FT_FRAME_USHORT( usFirstCharIndex ),
 982        FT_FRAME_USHORT( usLastCharIndex ),
 983        FT_FRAME_SHORT ( sTypoAscender ),
 984        FT_FRAME_SHORT ( sTypoDescender ),
 985        FT_FRAME_SHORT ( sTypoLineGap ),
 986        FT_FRAME_USHORT( usWinAscent ),
 987        FT_FRAME_USHORT( usWinDescent ),
 988      FT_FRAME_END
 989    };
 990
 991    const FT_Frame_Field  os2_fields_extra[] =
 992    {
 993      FT_FRAME_START( 8 ),
 994        FT_FRAME_ULONG( ulCodePageRange1 ),
 995        FT_FRAME_ULONG( ulCodePageRange2 ),
 996      FT_FRAME_END
 997    };
 998
 999    const FT_Frame_Field  os2_fields_extra2[] =
1000    {
1001      FT_FRAME_START( 10 ),
1002        FT_FRAME_SHORT ( sxHeight ),
1003        FT_FRAME_SHORT ( sCapHeight ),
1004        FT_FRAME_USHORT( usDefaultChar ),
1005        FT_FRAME_USHORT( usBreakChar ),
1006        FT_FRAME_USHORT( usMaxContext ),
1007      FT_FRAME_END
1008    };
1009
1010
1011    /* We now support old Mac fonts where the OS/2 table doesn't  */
1012    /* exist.  Simply put, we set the `version' field to 0xFFFF   */
1013    /* and test this value each time we need to access the table. */
1014    error = face->goto_table( face, TTAG_OS2, stream, 0 );
1015    if ( error )
1016      goto Exit;
1017
1018    os2 = &face->os2;
1019
1020    if ( FT_STREAM_READ_FIELDS( os2_fields, os2 ) )
1021      goto Exit;
1022
1023    os2->ulCodePageRange1 = 0;
1024    os2->ulCodePageRange2 = 0;
1025    os2->sxHeight         = 0;
1026    os2->sCapHeight       = 0;
1027    os2->usDefaultChar    = 0;
1028    os2->usBreakChar      = 0;
1029    os2->usMaxContext     = 0;
1030
1031    if ( os2->version >= 0x0001 )
1032    {
1033      /* only version 1 tables */
1034      if ( FT_STREAM_READ_FIELDS( os2_fields_extra, os2 ) )
1035        goto Exit;
1036
1037      if ( os2->version >= 0x0002 )
1038      {
1039        /* only version 2 tables */
1040        if ( FT_STREAM_READ_FIELDS( os2_fields_extra2, os2 ) )
1041          goto Exit;
1042      }
1043    }
1044
1045    FT_TRACE3(( "sTypoAscender:  %4d\n",   os2->sTypoAscender ));
1046    FT_TRACE3(( "sTypoDescender: %4d\n",   os2->sTypoDescender ));
1047    FT_TRACE3(( "usWinAscent:    %4u\n",   os2->usWinAscent ));
1048    FT_TRACE3(( "usWinDescent:   %4u\n",   os2->usWinDescent ));
1049    FT_TRACE3(( "fsSelection:    0x%2x\n", os2->fsSelection ));
1050
1051  Exit:
1052    return error;
1053  }
1054
1055
1056  /*************************************************************************/
1057  /*                                                                       */
1058  /* <Function>                                                            */
1059  /*    tt_face_load_postscript                                            */
1060  /*                                                                       */
1061  /* <Description>                                                         */
1062  /*    Loads the Postscript table.                                        */
1063  /*                                                                       */
1064  /* <Input>                                                               */
1065  /*    face   :: A handle to the target face object.                      */
1066  /*                                                                       */
1067  /*    stream :: A handle to the input stream.                            */
1068  /*                                                                       */
1069  /* <Return>                                                              */
1070  /*    FreeType error code.  0 means success.                             */
1071  /*                                                                       */
1072  FT_LOCAL_DEF( FT_Error )
1073  tt_face_load_post( TT_Face    face,
1074                     FT_Stream  stream )
1075  {
1076    FT_Error        error;
1077    TT_Postscript*  post = &face->postscript;
1078
1079    static const FT_Frame_Field  post_fields[] =
1080    {
1081#undef  FT_STRUCTURE
1082#define FT_STRUCTURE  TT_Postscript
1083
1084      FT_FRAME_START( 32 ),
1085        FT_FRAME_ULONG( FormatType ),
1086        FT_FRAME_ULONG( italicAngle ),
1087        FT_FRAME_SHORT( underlinePosition ),
1088        FT_FRAME_SHORT( underlineThickness ),
1089        FT_FRAME_ULONG( isFixedPitch ),
1090        FT_FRAME_ULONG( minMemType42 ),
1091        FT_FRAME_ULONG( maxMemType42 ),
1092        FT_FRAME_ULONG( minMemType1 ),
1093        FT_FRAME_ULONG( maxMemType1 ),
1094      FT_FRAME_END
1095    };
1096
1097
1098    error = face->goto_table( face, TTAG_post, stream, 0 );
1099    if ( error )
1100      return error;
1101
1102    if ( FT_STREAM_READ_FIELDS( post_fields, post ) )
1103      return error;
1104
1105    /* we don't load the glyph names, we do that in another */
1106    /* module (ttpost).                                     */
1107
1108    FT_TRACE3(( "FormatType:   0x%x\n", post->FormatType ));
1109    FT_TRACE3(( "isFixedPitch:   %s\n", post->isFixedPitch
1110                                        ? "  yes" : "   no" ));
1111
1112    return SFNT_Err_Ok;
1113  }
1114
1115
1116  /*************************************************************************/
1117  /*                                                                       */
1118  /* <Function>                                                            */
1119  /*    tt_face_load_pclt                                                  */
1120  /*                                                                       */
1121  /* <Description>                                                         */
1122  /*    Loads the PCL 5 Table.                                             */
1123  /*                                                                       */
1124  /* <Input>                                                               */
1125  /*    face   :: A handle to the target face object.                      */
1126  /*                                                                       */
1127  /*    stream :: A handle to the input stream.                            */
1128  /*                                                                       */
1129  /* <Return>                                                              */
1130  /*    FreeType error code.  0 means success.                             */
1131  /*                                                                       */
1132  FT_LOCAL_DEF( FT_Error )
1133  tt_face_load_pclt( TT_Face    face,
1134                     FT_Stream  stream )
1135  {
1136    static const FT_Frame_Field  pclt_fields[] =
1137    {
1138#undef  FT_STRUCTURE
1139#define FT_STRUCTURE  TT_PCLT
1140
1141      FT_FRAME_START( 54 ),
1142        FT_FRAME_ULONG ( Version ),
1143        FT_FRAME_ULONG ( FontNumber ),
1144        FT_FRAME_USHORT( Pitch ),
1145        FT_FRAME_USHORT( xHeight ),
1146        FT_FRAME_USHORT( Style ),
1147        FT_FRAME_USHORT( TypeFamily ),
1148        FT_FRAME_USHORT( CapHeight ),
1149        FT_FRAME_BYTES ( TypeFace, 16 ),
1150        FT_FRAME_BYTES ( CharacterComplement, 8 ),
1151        FT_FRAME_BYTES ( FileName, 6 ),
1152        FT_FRAME_CHAR  ( StrokeWeight ),
1153        FT_FRAME_CHAR  ( WidthType ),
1154        FT_FRAME_BYTE  ( SerifStyle ),
1155        FT_FRAME_BYTE  ( Reserved ),
1156      FT_FRAME_END
1157    };
1158
1159    FT_Error  error;
1160    TT_PCLT*  pclt = &face->pclt;
1161
1162
1163    /* optional table */
1164    error = face->goto_table( face, TTAG_PCLT, stream, 0 );
1165    if ( error )
1166      goto Exit;
1167
1168    if ( FT_STREAM_READ_FIELDS( pclt_fields, pclt ) )
1169      goto Exit;
1170
1171  Exit:
1172    return error;
1173  }
1174
1175
1176  /*************************************************************************/
1177  /*                                                                       */
1178  /* <Function>                                                            */
1179  /*    tt_face_load_gasp                                                  */
1180  /*                                                                       */
1181  /* <Description>                                                         */
1182  /*    Loads the `gasp' table into a face object.                         */
1183  /*                                                                       */
1184  /* <Input>                                                               */
1185  /*    face   :: A handle to the target face object.                      */
1186  /*                                                                       */
1187  /*    stream :: The input stream.                                        */
1188  /*                                                                       */
1189  /* <Return>                                                              */
1190  /*    FreeType error code.  0 means success.                             */
1191  /*                                                                       */
1192  FT_LOCAL_DEF( FT_Error )
1193  tt_face_load_gasp( TT_Face    face,
1194                     FT_Stream  stream )
1195  {
1196    FT_Error   error;
1197    FT_Memory  memory = stream->memory;
1198
1199    FT_UInt        j,num_ranges;
1200    TT_GaspRange   gaspranges;
1201
1202
1203    /* the gasp table is optional */
1204    error = face->goto_table( face, TTAG_gasp, stream, 0 );
1205    if ( error )
1206      goto Exit;
1207
1208    if ( FT_FRAME_ENTER( 4L ) )
1209      goto Exit;
1210
1211    face->gasp.version   = FT_GET_USHORT();
1212    face->gasp.numRanges = FT_GET_USHORT();
1213
1214    FT_FRAME_EXIT();
1215
1216    /* only support versions 0 and 1 of the table */
1217    if ( face->gasp.version >= 2 )
1218    {
1219      face->gasp.numRanges = 0;
1220      error = SFNT_Err_Invalid_Table;
1221      goto Exit;
1222    }
1223
1224    num_ranges = face->gasp.numRanges;
1225    FT_TRACE3(( "numRanges: %u\n", num_ranges ));
1226
1227    if ( FT_QNEW_ARRAY( gaspranges, num_ranges ) ||
1228         FT_FRAME_ENTER( num_ranges * 4L )      )
1229      goto Exit;
1230
1231    face->gasp.gaspRanges = gaspranges;
1232
1233    for ( j = 0; j < num_ranges; j++ )
1234    {
1235      gaspranges[j].maxPPEM  = FT_GET_USHORT();
1236      gaspranges[j].gaspFlag = FT_GET_USHORT();
1237
1238      FT_TRACE3(( "gaspRange %d: rangeMaxPPEM %5d, rangeGaspBehavior 0x%x\n",
1239                  j,
1240                  gaspranges[j].maxPPEM,
1241                  gaspranges[j].gaspFlag ));
1242    }
1243
1244    FT_FRAME_EXIT();
1245
1246  Exit:
1247    return error;
1248  }
1249
1250
1251/* END */