tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

pfrload.c (28988B)


      1 /****************************************************************************
      2 *
      3 * pfrload.c
      4 *
      5 *   FreeType PFR loader (body).
      6 *
      7 * Copyright (C) 2002-2025 by
      8 * David Turner, Robert Wilhelm, and Werner Lemberg.
      9 *
     10 * This file is part of the FreeType project, and may only be used,
     11 * modified, and distributed under the terms of the FreeType project
     12 * license, LICENSE.TXT.  By continuing to use, modify, or distribute
     13 * this file you indicate that you have read the license and
     14 * understand and accept it fully.
     15 *
     16 */
     17 
     18 
     19 #include "pfrload.h"
     20 #include <freetype/internal/ftdebug.h>
     21 #include <freetype/internal/ftstream.h>
     22 
     23 #include "pfrerror.h"
     24 
     25 #undef  FT_COMPONENT
     26 #define FT_COMPONENT  pfr
     27 
     28 
     29  /*
     30   * The overall structure of a PFR file is as follows.
     31   *
     32   *   PFR header
     33   *     58 bytes (contains nPhysFonts)
     34   *
     35   *   Logical font directory (size at most 2^16 bytes)
     36   *     2 bytes (nLogFonts)
     37   *     + nLogFonts * 5 bytes
     38   *
     39   *        ==>  nLogFonts <= 13106
     40   *
     41   *   Logical font section (size at most 2^24 bytes)
     42   *     nLogFonts * logFontRecord
     43   *
     44   *     logFontRecord (size at most 2^16 bytes)
     45   *       12 bytes (fontMatrix)
     46   *       + 1 byte (flags)
     47   *       + 0-5 bytes (depending on `flags')
     48   *       + 0-(1+255*(2+255)) = 0-65536 (depending on `flags')
     49   *       + 5 bytes (physical font info)
     50   *       + 0-1 bytes (depending on PFR header)
     51   *
     52   *        ==>  minimum size 18 bytes
     53   *
     54   *   Physical font section (size at most 2^24 bytes)
     55   *     nPhysFonts * (physFontRecord
     56   *                   + nBitmapSizes * nBmapChars * bmapCharRecord)
     57   *
     58   *     physFontRecord (size at most 2^24 bytes)
     59   *       14 bytes (font info)
     60   *       + 1 byte (flags)
     61   *       + 0-2 (depending on `flags')
     62   *       + 0-? (structure too complicated to be shown here; depending on
     63   *              `flags'; contains `nBitmapSizes' and `nBmapChars')
     64   *       + 3 bytes (nAuxBytes)
     65   *       + nAuxBytes
     66   *       + 1 byte (nBlueValues)
     67   *       + 2 * nBlueValues
     68   *       + 6 bytes (hinting data)
     69   *       + 2 bytes (nCharacters)
     70   *       + nCharacters * (4-10 bytes) (depending on `flags')
     71   *
     72   *        ==>  minimum size 27 bytes
     73   *
     74   *     bmapCharRecord
     75   *       4-7 bytes
     76   *
     77   *   Glyph program strings (three possible types: simpleGps, compoundGps,
     78   *                          and bitmapGps; size at most 2^24 bytes)
     79   *     simpleGps (size at most 2^16 bytes)
     80   *       1 byte (flags)
     81   *       1-2 bytes (n[XY]orus, depending on `flags')
     82   *       0-(64+512*2) = 0-1088 bytes (depending on `n[XY]orus')
     83   *       0-? (structure too complicated to be shown here; depending on
     84   *            `flags')
     85   *       1-? glyph data (faintly resembling PS Type 1 charstrings)
     86   *
     87   *        ==>  minimum size 3 bytes
     88   *
     89   *     compoundGps (size at most 2^16 bytes)
     90   *       1 byte (nElements <= 63, flags)
     91   *       + 0-(1+255*(2+255)) = 0-65536 (depending on `flags')
     92   *       + nElements * (6-14 bytes)
     93   *
     94   *     bitmapGps (size at most 2^16 bytes)
     95   *       1 byte (flags)
     96   *       3-13 bytes (position info, depending on `flags')
     97   *       0-? bitmap data
     98   *
     99   *        ==>  minimum size 4 bytes
    100   *
    101   *   PFR trailer
    102   *       8 bytes
    103   *
    104   *
    105   * ==>  minimum size of a valid PFR:
    106   *        58 (header)
    107   *        + 2 (nLogFonts)
    108   *        + 27 (1 physFontRecord)
    109   *        + 8 (trailer)
    110   *       -----
    111   *        95 bytes
    112   *
    113   */
    114 
    115 
    116  /*************************************************************************/
    117  /*************************************************************************/
    118  /*****                                                               *****/
    119  /*****                          EXTRA ITEMS                          *****/
    120  /*****                                                               *****/
    121  /*************************************************************************/
    122  /*************************************************************************/
    123 
    124 
    125  FT_LOCAL_DEF( FT_Error )
    126  pfr_extra_items_skip( FT_Byte*  *pp,
    127                        FT_Byte*   limit )
    128  {
    129    return pfr_extra_items_parse( pp, limit, NULL, NULL );
    130  }
    131 
    132 
    133  FT_LOCAL_DEF( FT_Error )
    134  pfr_extra_items_parse( FT_Byte*       *pp,
    135                         FT_Byte*        limit,
    136                         PFR_ExtraItem   item_list,
    137                         FT_Pointer      item_data )
    138  {
    139    FT_Error  error = FT_Err_Ok;
    140    FT_Byte*  p     = *pp;
    141    FT_UInt   num_items, item_type, item_size;
    142 
    143 
    144    PFR_CHECK( 1 );
    145    num_items = PFR_NEXT_BYTE( p );
    146 
    147    for ( ; num_items > 0; num_items-- )
    148    {
    149      PFR_CHECK( 2 );
    150      item_size = PFR_NEXT_BYTE( p );
    151      item_type = PFR_NEXT_BYTE( p );
    152 
    153      PFR_CHECK( item_size );
    154 
    155      if ( item_list )
    156      {
    157        PFR_ExtraItem  extra = item_list;
    158 
    159 
    160        for ( extra = item_list; extra->parser != NULL; extra++ )
    161        {
    162          if ( extra->type == item_type )
    163          {
    164            error = extra->parser( p, p + item_size, item_data );
    165            if ( error )
    166              goto Exit;
    167 
    168            break;
    169          }
    170        }
    171      }
    172 
    173      p += item_size;
    174    }
    175 
    176  Exit:
    177    *pp = p;
    178    return error;
    179 
    180  Too_Short:
    181    FT_ERROR(( "pfr_extra_items_parse: invalid extra items table\n" ));
    182    error = FT_THROW( Invalid_Table );
    183    goto Exit;
    184  }
    185 
    186 
    187  /*************************************************************************/
    188  /*************************************************************************/
    189  /*****                                                               *****/
    190  /*****                          PFR HEADER                           *****/
    191  /*****                                                               *****/
    192  /*************************************************************************/
    193  /*************************************************************************/
    194 
    195   static const FT_Frame_Field  pfr_header_fields[] =
    196   {
    197 #undef  FT_STRUCTURE
    198 #define FT_STRUCTURE  PFR_HeaderRec
    199 
    200     FT_FRAME_START( 58 ),
    201       FT_FRAME_ULONG ( signature ),
    202       FT_FRAME_USHORT( version ),
    203       FT_FRAME_USHORT( signature2 ),
    204       FT_FRAME_USHORT( header_size ),
    205 
    206       FT_FRAME_USHORT( log_dir_size ),
    207       FT_FRAME_USHORT( log_dir_offset ),
    208 
    209       FT_FRAME_USHORT( log_font_max_size ),
    210       FT_FRAME_UOFF3 ( log_font_section_size ),
    211       FT_FRAME_UOFF3 ( log_font_section_offset ),
    212 
    213       FT_FRAME_USHORT( phy_font_max_size ),
    214       FT_FRAME_UOFF3 ( phy_font_section_size ),
    215       FT_FRAME_UOFF3 ( phy_font_section_offset ),
    216 
    217       FT_FRAME_USHORT( gps_max_size ),
    218       FT_FRAME_UOFF3 ( gps_section_size ),
    219       FT_FRAME_UOFF3 ( gps_section_offset ),
    220 
    221       FT_FRAME_BYTE  ( max_blue_values ),
    222       FT_FRAME_BYTE  ( max_x_orus ),
    223       FT_FRAME_BYTE  ( max_y_orus ),
    224 
    225       FT_FRAME_BYTE  ( phy_font_max_size_high ),
    226       FT_FRAME_BYTE  ( color_flags ),
    227 
    228       FT_FRAME_UOFF3 ( bct_max_size ),
    229       FT_FRAME_UOFF3 ( bct_set_max_size ),
    230       FT_FRAME_UOFF3 ( phy_bct_set_max_size ),
    231 
    232       FT_FRAME_USHORT( num_phy_fonts ),
    233       FT_FRAME_BYTE  ( max_vert_stem_snap ),
    234       FT_FRAME_BYTE  ( max_horz_stem_snap ),
    235       FT_FRAME_USHORT( max_chars ),
    236     FT_FRAME_END
    237   };
    238 
    239 
    240  FT_LOCAL_DEF( FT_Error )
    241  pfr_header_load( PFR_Header  header,
    242                   FT_Stream   stream )
    243  {
    244    FT_Error  error;
    245 
    246 
    247    /* read header directly */
    248    if ( !FT_STREAM_SEEK( 0 )                                &&
    249         !FT_STREAM_READ_FIELDS( pfr_header_fields, header ) )
    250    {
    251      /* make a few adjustments to the header */
    252      header->phy_font_max_size +=
    253        (FT_UInt32)header->phy_font_max_size_high << 16;
    254    }
    255 
    256    return error;
    257  }
    258 
    259 
    260  FT_LOCAL_DEF( FT_Bool )
    261  pfr_header_check( PFR_Header  header )
    262  {
    263    FT_Bool  result = 1;
    264 
    265 
    266    /* check signature and header size */
    267    if ( header->signature  != 0x50465230L ||   /* "PFR0" */
    268         header->version     > 4           ||
    269         header->header_size < 58          ||
    270         header->signature2 != 0x0D0A      )    /* CR/LF  */
    271      result = 0;
    272 
    273    return result;
    274  }
    275 
    276 
    277  /***********************************************************************/
    278  /***********************************************************************/
    279  /*****                                                             *****/
    280  /*****                    PFR LOGICAL FONTS                        *****/
    281  /*****                                                             *****/
    282  /***********************************************************************/
    283  /***********************************************************************/
    284 
    285 
    286  FT_LOCAL_DEF( FT_Error )
    287  pfr_log_font_count( FT_Stream  stream,
    288                      FT_UInt32  section_offset,
    289                      FT_Long   *acount )
    290  {
    291    FT_Error  error;
    292    FT_UInt   count;
    293    FT_UInt   result = 0;
    294 
    295 
    296    if ( FT_STREAM_SEEK( section_offset ) ||
    297         FT_READ_USHORT( count )          )
    298      goto Exit;
    299 
    300    /* check maximum value and a rough minimum size:     */
    301    /* - no more than 13106 log fonts                    */
    302    /* - we need 5 bytes for a log header record         */
    303    /* - we need at least 18 bytes for a log font record */
    304    /* - the overall size is at least 95 bytes plus the  */
    305    /*   log header and log font records                 */
    306    if ( count > ( ( 1 << 16 ) - 2 ) / 5                ||
    307         2 + count * 5 >= stream->size - section_offset ||
    308         95 + count * ( 5 + 18 ) >= stream->size        )
    309    {
    310      FT_ERROR(( "pfr_log_font_count:"
    311                 " invalid number of logical fonts\n" ));
    312      error = FT_THROW( Invalid_Table );
    313      goto Exit;
    314    }
    315 
    316    result = count;
    317 
    318  Exit:
    319    *acount = (FT_Long)result;
    320    return error;
    321  }
    322 
    323 
    324  FT_LOCAL_DEF( FT_Error )
    325  pfr_log_font_load( PFR_LogFont  log_font,
    326                     FT_Stream    stream,
    327                     FT_UInt      idx,
    328                     FT_UInt32    section_offset,
    329                     FT_Bool      size_increment )
    330  {
    331    FT_UInt    num_log_fonts;
    332    FT_UInt    flags;
    333    FT_UInt32  offset;
    334    FT_UInt32  size;
    335    FT_Error   error;
    336 
    337 
    338    if ( FT_STREAM_SEEK( section_offset ) ||
    339         FT_READ_USHORT( num_log_fonts )  )
    340      goto Exit;
    341 
    342    if ( idx >= num_log_fonts )
    343      return FT_THROW( Invalid_Argument );
    344 
    345    if ( FT_STREAM_SKIP( idx * 5 ) ||
    346         FT_READ_USHORT( size )    ||
    347         FT_READ_UOFF3 ( offset )  )
    348      goto Exit;
    349 
    350    /* save logical font size and offset */
    351    log_font->size   = size;
    352    log_font->offset = offset;
    353 
    354    /* now, check the rest of the table before loading it */
    355    {
    356      FT_Byte*  p;
    357      FT_Byte*  limit;
    358      FT_UInt   local;
    359 
    360 
    361      if ( FT_STREAM_SEEK( offset ) ||
    362           FT_FRAME_ENTER( size )   )
    363        goto Exit;
    364 
    365      p     = stream->cursor;
    366      limit = p + size;
    367 
    368      PFR_CHECK( 13 );
    369 
    370      log_font->matrix[0] = PFR_NEXT_LONG( p );
    371      log_font->matrix[1] = PFR_NEXT_LONG( p );
    372      log_font->matrix[2] = PFR_NEXT_LONG( p );
    373      log_font->matrix[3] = PFR_NEXT_LONG( p );
    374 
    375      flags = PFR_NEXT_BYTE( p );
    376 
    377      local = 0;
    378      if ( flags & PFR_LOG_STROKE )
    379      {
    380        local++;
    381        if ( flags & PFR_LOG_2BYTE_STROKE )
    382          local++;
    383 
    384        if ( ( flags & PFR_LINE_JOIN_MASK ) == PFR_LINE_JOIN_MITER )
    385          local += 3;
    386      }
    387      if ( flags & PFR_LOG_BOLD )
    388      {
    389        local++;
    390        if ( flags & PFR_LOG_2BYTE_BOLD )
    391          local++;
    392      }
    393 
    394      PFR_CHECK( local );
    395 
    396      if ( flags & PFR_LOG_STROKE )
    397      {
    398        log_font->stroke_thickness = ( flags & PFR_LOG_2BYTE_STROKE )
    399                                     ? PFR_NEXT_SHORT( p )
    400                                     : PFR_NEXT_BYTE( p );
    401 
    402        if ( ( flags & PFR_LINE_JOIN_MASK ) == PFR_LINE_JOIN_MITER )
    403          log_font->miter_limit = PFR_NEXT_LONG( p );
    404      }
    405 
    406      if ( flags & PFR_LOG_BOLD )
    407        log_font->bold_thickness = ( flags & PFR_LOG_2BYTE_BOLD )
    408                                   ? PFR_NEXT_SHORT( p )
    409                                   : PFR_NEXT_BYTE( p );
    410 
    411      if ( flags & PFR_LOG_EXTRA_ITEMS )
    412      {
    413        error = pfr_extra_items_skip( &p, limit );
    414        if ( error )
    415          goto Fail;
    416      }
    417 
    418      PFR_CHECK( 5 );
    419      log_font->phys_size   = PFR_NEXT_USHORT( p );
    420      log_font->phys_offset = PFR_NEXT_ULONG( p );
    421      if ( size_increment )
    422      {
    423        PFR_CHECK( 1 );
    424        log_font->phys_size += (FT_UInt32)PFR_NEXT_BYTE( p ) << 16;
    425      }
    426    }
    427 
    428  Fail:
    429    FT_FRAME_EXIT();
    430 
    431  Exit:
    432    return error;
    433 
    434  Too_Short:
    435    FT_ERROR(( "pfr_log_font_load: invalid logical font table\n" ));
    436    error = FT_THROW( Invalid_Table );
    437    goto Fail;
    438  }
    439 
    440 
    441  /***********************************************************************/
    442  /***********************************************************************/
    443  /*****                                                             *****/
    444  /*****                    PFR PHYSICAL FONTS                       *****/
    445  /*****                                                             *****/
    446  /***********************************************************************/
    447  /***********************************************************************/
    448 
    449 
    450  /* load bitmap strikes lists */
    451  FT_CALLBACK_DEF( FT_Error )
    452  pfr_extra_item_load_bitmap_info( FT_Byte*  p,
    453                                   FT_Byte*  limit,
    454                                   void*     phy_font_ )
    455  {
    456    PFR_PhyFont  phy_font = (PFR_PhyFont)phy_font_;
    457    FT_Memory    memory   = phy_font->memory;
    458    PFR_Strike   strike;
    459    FT_UInt      flags0;
    460    FT_UInt      n, count, size1;
    461    FT_Error     error    = FT_Err_Ok;
    462 
    463 
    464    PFR_CHECK( 5 );
    465 
    466    p     += 3;  /* skip bctSize */
    467    flags0 = PFR_NEXT_BYTE( p );
    468    count  = PFR_NEXT_BYTE( p );
    469 
    470    /* re-allocate when needed */
    471    if ( phy_font->num_strikes + count > phy_font->max_strikes )
    472    {
    473      FT_UInt  new_max = FT_PAD_CEIL( phy_font->num_strikes + count, 4 );
    474 
    475 
    476      if ( FT_RENEW_ARRAY( phy_font->strikes,
    477                           phy_font->num_strikes,
    478                           new_max ) )
    479        goto Exit;
    480 
    481      phy_font->max_strikes = new_max;
    482    }
    483 
    484    size1 = 1 + 1 + 1 + 2 + 2 + 1;
    485    if ( flags0 & PFR_STRIKE_2BYTE_XPPM )
    486      size1++;
    487 
    488    if ( flags0 & PFR_STRIKE_2BYTE_YPPM )
    489      size1++;
    490 
    491    if ( flags0 & PFR_STRIKE_3BYTE_SIZE )
    492      size1++;
    493 
    494    if ( flags0 & PFR_STRIKE_3BYTE_OFFSET )
    495      size1++;
    496 
    497    if ( flags0 & PFR_STRIKE_2BYTE_COUNT )
    498      size1++;
    499 
    500    strike = phy_font->strikes + phy_font->num_strikes;
    501 
    502    PFR_CHECK( count * size1 );
    503 
    504    for ( n = 0; n < count; n++, strike++ )
    505    {
    506      strike->x_ppm       = ( flags0 & PFR_STRIKE_2BYTE_XPPM )
    507                            ? PFR_NEXT_USHORT( p )
    508                            : PFR_NEXT_BYTE( p );
    509 
    510      strike->y_ppm       = ( flags0 & PFR_STRIKE_2BYTE_YPPM )
    511                            ? PFR_NEXT_USHORT( p )
    512                            : PFR_NEXT_BYTE( p );
    513 
    514      strike->flags       = PFR_NEXT_BYTE( p );
    515 
    516      strike->bct_size    = ( flags0 & PFR_STRIKE_3BYTE_SIZE )
    517                            ? PFR_NEXT_ULONG( p )
    518                            : PFR_NEXT_USHORT( p );
    519 
    520      strike->bct_offset  = ( flags0 & PFR_STRIKE_3BYTE_OFFSET )
    521                            ? PFR_NEXT_ULONG( p )
    522                            : PFR_NEXT_USHORT( p );
    523 
    524      strike->num_bitmaps = ( flags0 & PFR_STRIKE_2BYTE_COUNT )
    525                            ? PFR_NEXT_USHORT( p )
    526                            : PFR_NEXT_BYTE( p );
    527    }
    528 
    529    phy_font->num_strikes += count;
    530 
    531  Exit:
    532    return error;
    533 
    534  Too_Short:
    535    error = FT_THROW( Invalid_Table );
    536    FT_ERROR(( "pfr_extra_item_load_bitmap_info:"
    537               " invalid bitmap info table\n" ));
    538    goto Exit;
    539  }
    540 
    541 
    542  /* Load font ID.  This is a so-called `unique' name that is rather
    543   * long and descriptive (like `Tiresias ScreenFont v7.51').
    544   *
    545   * Note that a PFR font's family name is contained in an *undocumented*
    546   * string of the `auxiliary data' portion of a physical font record.  This
    547   * may also contain the `real' style name!
    548   *
    549   * If no family name is present, the font ID is used instead for the
    550   * family.
    551   */
    552  FT_CALLBACK_DEF( FT_Error )
    553  pfr_extra_item_load_font_id( FT_Byte*  p,
    554                               FT_Byte*  limit,
    555                               void*     phy_font_ )
    556  {
    557    PFR_PhyFont  phy_font = (PFR_PhyFont)phy_font_;
    558    FT_Error     error    = FT_Err_Ok;
    559    FT_Memory    memory   = phy_font->memory;
    560    FT_UInt      len      = (FT_UInt)( limit - p );
    561 
    562 
    563    if ( phy_font->font_id )
    564      goto Exit;
    565 
    566    if ( FT_QALLOC( phy_font->font_id, len + 1 ) )
    567      goto Exit;
    568 
    569    /* copy font ID name, and terminate it for safety */
    570    FT_MEM_COPY( phy_font->font_id, p, len );
    571    phy_font->font_id[len] = 0;
    572 
    573  Exit:
    574    return error;
    575  }
    576 
    577 
    578  /* load stem snap tables */
    579  FT_CALLBACK_DEF( FT_Error )
    580  pfr_extra_item_load_stem_snaps( FT_Byte*  p,
    581                                  FT_Byte*  limit,
    582                                  void*     phy_font_ )
    583  {
    584    PFR_PhyFont  phy_font = (PFR_PhyFont)phy_font_;
    585    FT_UInt      count, num_vert, num_horz;
    586    FT_Int*      snaps    = NULL;
    587    FT_Error     error    = FT_Err_Ok;
    588    FT_Memory    memory   = phy_font->memory;
    589 
    590 
    591    if ( phy_font->vertical.stem_snaps )
    592      goto Exit;
    593 
    594    PFR_CHECK( 1 );
    595    count = PFR_NEXT_BYTE( p );
    596 
    597    num_vert = count & 15;
    598    num_horz = count >> 4;
    599    count    = num_vert + num_horz;
    600 
    601    PFR_CHECK( count * 2 );
    602 
    603    if ( FT_QNEW_ARRAY( snaps, count ) )
    604      goto Exit;
    605 
    606    phy_font->vertical.stem_snaps   = snaps;
    607    phy_font->horizontal.stem_snaps = snaps + num_vert;
    608 
    609    for ( ; count > 0; count--, snaps++ )
    610      *snaps = FT_NEXT_SHORT( p );
    611 
    612  Exit:
    613    return error;
    614 
    615  Too_Short:
    616    error = FT_THROW( Invalid_Table );
    617    FT_ERROR(( "pfr_extra_item_load_stem_snaps:"
    618               " invalid stem snaps table\n" ));
    619    goto Exit;
    620  }
    621 
    622 
    623  /* load kerning pair data */
    624  FT_CALLBACK_DEF( FT_Error )
    625  pfr_extra_item_load_kerning_pairs( FT_Byte*  p,
    626                                     FT_Byte*  limit,
    627                                     void*     phy_font_ )
    628  {
    629    PFR_PhyFont  phy_font = (PFR_PhyFont)phy_font_;
    630    PFR_KernItem  item   = NULL;
    631    FT_Error      error  = FT_Err_Ok;
    632    FT_Memory     memory = phy_font->memory;
    633 
    634 
    635    if ( FT_NEW( item ) )
    636      goto Exit;
    637 
    638    PFR_CHECK( 4 );
    639 
    640    item->pair_count = PFR_NEXT_BYTE( p );
    641    item->base_adj   = PFR_NEXT_SHORT( p );
    642    item->flags      = PFR_NEXT_BYTE( p );
    643    item->offset     = phy_font->offset +
    644                       (FT_Offset)( p - phy_font->cursor );
    645 
    646 #ifndef PFR_CONFIG_NO_CHECKS
    647    item->pair_size = 3;
    648 
    649    if ( item->flags & PFR_KERN_2BYTE_CHAR )
    650      item->pair_size += 2;
    651 
    652    if ( item->flags & PFR_KERN_2BYTE_ADJ )
    653      item->pair_size += 1;
    654 
    655    PFR_CHECK( item->pair_count * item->pair_size );
    656 #endif
    657 
    658    /* load first and last pairs into the item to speed up */
    659    /* lookup later...                                     */
    660    if ( item->pair_count > 0 )
    661    {
    662      FT_UInt   char1, char2;
    663      FT_Byte*  q;
    664 
    665 
    666      if ( item->flags & PFR_KERN_2BYTE_CHAR )
    667      {
    668        q     = p;
    669        char1 = PFR_NEXT_USHORT( q );
    670        char2 = PFR_NEXT_USHORT( q );
    671 
    672        item->pair1 = PFR_KERN_INDEX( char1, char2 );
    673 
    674        q = p + item->pair_size * ( item->pair_count - 1 );
    675        char1 = PFR_NEXT_USHORT( q );
    676        char2 = PFR_NEXT_USHORT( q );
    677 
    678        item->pair2 = PFR_KERN_INDEX( char1, char2 );
    679      }
    680      else
    681      {
    682        q     = p;
    683        char1 = PFR_NEXT_BYTE( q );
    684        char2 = PFR_NEXT_BYTE( q );
    685 
    686        item->pair1 = PFR_KERN_INDEX( char1, char2 );
    687 
    688        q = p + item->pair_size * ( item->pair_count - 1 );
    689        char1 = PFR_NEXT_BYTE( q );
    690        char2 = PFR_NEXT_BYTE( q );
    691 
    692        item->pair2 = PFR_KERN_INDEX( char1, char2 );
    693      }
    694 
    695      /* add new item to the current list */
    696      item->next                 = NULL;
    697      *phy_font->kern_items_tail = item;
    698      phy_font->kern_items_tail  = &item->next;
    699      phy_font->num_kern_pairs  += item->pair_count;
    700    }
    701    else
    702    {
    703      /* empty item! */
    704      FT_FREE( item );
    705    }
    706 
    707  Exit:
    708    return error;
    709 
    710  Too_Short:
    711    FT_FREE( item );
    712 
    713    error = FT_THROW( Invalid_Table );
    714    FT_ERROR(( "pfr_extra_item_load_kerning_pairs:"
    715               " invalid kerning pairs table\n" ));
    716    goto Exit;
    717  }
    718 
    719 
    720  static const PFR_ExtraItemRec  pfr_phy_font_extra_items[] =
    721  {
    722    { 1, pfr_extra_item_load_bitmap_info },
    723    { 2, pfr_extra_item_load_font_id },
    724    { 3, pfr_extra_item_load_stem_snaps },
    725    { 4, pfr_extra_item_load_kerning_pairs },
    726    { 0, NULL }
    727  };
    728 
    729 
    730  /*
    731   * Load a name from the auxiliary data.  Since this extracts undocumented
    732   * strings from the font file, we need to be careful here.
    733   */
    734  static FT_Error
    735  pfr_aux_name_load( FT_Byte*     p,
    736                     FT_UInt      len,
    737                     FT_Memory    memory,
    738                     FT_String*  *astring )
    739  {
    740    FT_Error    error  = FT_Err_Ok;
    741    FT_String*  result = NULL;
    742    FT_UInt     n, ok;
    743 
    744 
    745    if ( *astring )
    746      FT_FREE( *astring );
    747 
    748    if ( len > 0 && p[len - 1] == 0 )
    749      len--;
    750 
    751    /* check that each character is ASCII  */
    752    /* for making sure not to load garbage */
    753    ok = ( len > 0 );
    754    for ( n = 0; n < len; n++ )
    755      if ( p[n] < 32 || p[n] > 127 )
    756      {
    757        ok = 0;
    758        break;
    759      }
    760 
    761    if ( ok )
    762    {
    763      if ( FT_QALLOC( result, len + 1 ) )
    764        goto Exit;
    765 
    766      FT_MEM_COPY( result, p, len );
    767      result[len] = 0;
    768    }
    769 
    770  Exit:
    771    *astring = result;
    772    return error;
    773  }
    774 
    775 
    776  FT_LOCAL_DEF( void )
    777  pfr_phy_font_done( PFR_PhyFont  phy_font,
    778                     FT_Memory    memory )
    779  {
    780    FT_FREE( phy_font->font_id );
    781    FT_FREE( phy_font->family_name );
    782    FT_FREE( phy_font->style_name );
    783 
    784    FT_FREE( phy_font->vertical.stem_snaps );
    785    phy_font->vertical.num_stem_snaps = 0;
    786 
    787    phy_font->horizontal.stem_snaps     = NULL;
    788    phy_font->horizontal.num_stem_snaps = 0;
    789 
    790    FT_FREE( phy_font->strikes );
    791    phy_font->num_strikes = 0;
    792    phy_font->max_strikes = 0;
    793 
    794    FT_FREE( phy_font->chars );
    795    phy_font->num_chars    = 0;
    796    phy_font->chars_offset = 0;
    797 
    798    FT_FREE( phy_font->blue_values );
    799    phy_font->num_blue_values = 0;
    800 
    801    {
    802      PFR_KernItem  item, next;
    803 
    804 
    805      item = phy_font->kern_items;
    806      while ( item )
    807      {
    808        next = item->next;
    809        FT_FREE( item );
    810        item = next;
    811      }
    812      phy_font->kern_items      = NULL;
    813      phy_font->kern_items_tail = NULL;
    814    }
    815 
    816    phy_font->num_kern_pairs = 0;
    817  }
    818 
    819 
    820  FT_LOCAL_DEF( FT_Error )
    821  pfr_phy_font_load( PFR_PhyFont  phy_font,
    822                     FT_Stream    stream,
    823                     FT_UInt32    offset,
    824                     FT_UInt32    size )
    825  {
    826    FT_Error   error;
    827    FT_Memory  memory = stream->memory;
    828    FT_UInt    flags;
    829    FT_ULong   num_aux;
    830    FT_Byte*   p;
    831    FT_Byte*   limit;
    832 
    833 
    834    phy_font->memory = memory;
    835    phy_font->offset = offset;
    836 
    837    phy_font->kern_items      = NULL;
    838    phy_font->kern_items_tail = &phy_font->kern_items;
    839 
    840    if ( FT_STREAM_SEEK( offset ) ||
    841         FT_FRAME_ENTER( size )   )
    842      goto Exit;
    843 
    844    phy_font->cursor = stream->cursor;
    845 
    846    p     = stream->cursor;
    847    limit = p + size;
    848 
    849    PFR_CHECK( 15 );
    850    phy_font->font_ref_number    = PFR_NEXT_USHORT( p );
    851    phy_font->outline_resolution = PFR_NEXT_USHORT( p );
    852    phy_font->metrics_resolution = PFR_NEXT_USHORT( p );
    853    phy_font->bbox.xMin          = PFR_NEXT_SHORT( p );
    854    phy_font->bbox.yMin          = PFR_NEXT_SHORT( p );
    855    phy_font->bbox.xMax          = PFR_NEXT_SHORT( p );
    856    phy_font->bbox.yMax          = PFR_NEXT_SHORT( p );
    857    phy_font->flags      = flags = PFR_NEXT_BYTE( p );
    858 
    859    if ( !phy_font->outline_resolution ||
    860         !phy_font->metrics_resolution )
    861    {
    862      error = FT_THROW( Invalid_Table );
    863      FT_ERROR(( "pfr_phy_font_load: invalid resolution\n" ));
    864      goto Fail;
    865    }
    866 
    867    /* get the standard advance for non-proportional fonts */
    868    if ( !( flags & PFR_PHY_PROPORTIONAL ) )
    869    {
    870      PFR_CHECK( 2 );
    871      phy_font->standard_advance = PFR_NEXT_SHORT( p );
    872    }
    873 
    874    /* load the extra items when present */
    875    if ( flags & PFR_PHY_EXTRA_ITEMS )
    876    {
    877      error = pfr_extra_items_parse( &p, limit,
    878                                     pfr_phy_font_extra_items, phy_font );
    879      if ( error )
    880        goto Fail;
    881    }
    882 
    883    /* In certain fonts, the auxiliary bytes contain interesting   */
    884    /* information.  These are not in the specification but can be */
    885    /* guessed by looking at the content of a few 'PFR0' fonts.    */
    886    PFR_CHECK( 3 );
    887    num_aux = PFR_NEXT_ULONG( p );
    888 
    889    if ( num_aux > 0 )
    890    {
    891      FT_Byte*  q = p;
    892      FT_Byte*  q2;
    893 
    894 
    895      PFR_CHECK_SIZE( num_aux );
    896      p += num_aux;
    897 
    898      while ( num_aux > 0 )
    899      {
    900        FT_UInt  length, type;
    901 
    902 
    903        if ( q + 4 > p )
    904          break;
    905 
    906        length = PFR_NEXT_USHORT( q );
    907        if ( length < 4 || length > num_aux )
    908          break;
    909 
    910        q2   = q + length - 2;
    911        type = PFR_NEXT_USHORT( q );
    912 
    913        switch ( type )
    914        {
    915        case 1:
    916          /* this seems to correspond to the font's family name, padded to */
    917          /* an even number of bytes with a zero byte appended if needed   */
    918          error = pfr_aux_name_load( q, length - 4U, memory,
    919                                     &phy_font->family_name );
    920          if ( error )
    921            goto Exit;
    922          break;
    923 
    924        case 2:
    925          if ( q + 32 > q2 )
    926            break;
    927 
    928          q += 10;
    929          phy_font->ascent  = PFR_NEXT_SHORT( q );
    930          phy_font->descent = PFR_NEXT_SHORT( q );
    931          phy_font->leading = PFR_NEXT_SHORT( q );
    932          break;
    933 
    934        case 3:
    935          /* this seems to correspond to the font's style name, padded to */
    936          /* an even number of bytes with a zero byte appended if needed  */
    937          error = pfr_aux_name_load( q, length - 4U, memory,
    938                                     &phy_font->style_name );
    939          if ( error )
    940            goto Exit;
    941          break;
    942 
    943        default:
    944          ;
    945        }
    946 
    947        q        = q2;
    948        num_aux -= length;
    949      }
    950    }
    951 
    952    /* read the blue values */
    953    {
    954      FT_UInt  n, count;
    955 
    956 
    957      PFR_CHECK( 1 );
    958      phy_font->num_blue_values = count = PFR_NEXT_BYTE( p );
    959 
    960      PFR_CHECK( count * 2 );
    961 
    962      if ( FT_QNEW_ARRAY( phy_font->blue_values, count ) )
    963        goto Fail;
    964 
    965      for ( n = 0; n < count; n++ )
    966        phy_font->blue_values[n] = PFR_NEXT_SHORT( p );
    967    }
    968 
    969    PFR_CHECK( 8 );
    970    phy_font->blue_fuzz  = PFR_NEXT_BYTE( p );
    971    phy_font->blue_scale = PFR_NEXT_BYTE( p );
    972 
    973    phy_font->vertical.standard   = PFR_NEXT_USHORT( p );
    974    phy_font->horizontal.standard = PFR_NEXT_USHORT( p );
    975 
    976    /* read the character descriptors */
    977    {
    978      FT_UInt  n, count, Size;
    979 
    980 
    981      phy_font->num_chars    = count = PFR_NEXT_USHORT( p );
    982      phy_font->chars_offset = offset + (FT_Offset)( p - stream->cursor );
    983 
    984      if ( !phy_font->num_chars )
    985      {
    986        error = FT_THROW( Invalid_Table );
    987        FT_ERROR(( "pfr_phy_font_load: no glyphs\n" ));
    988        goto Fail;
    989      }
    990 
    991      Size = 1 + 1 + 2;
    992      if ( flags & PFR_PHY_2BYTE_CHARCODE )
    993        Size += 1;
    994 
    995      if ( flags & PFR_PHY_PROPORTIONAL )
    996        Size += 2;
    997 
    998      if ( flags & PFR_PHY_ASCII_CODE )
    999        Size += 1;
   1000 
   1001      if ( flags & PFR_PHY_2BYTE_GPS_SIZE )
   1002        Size += 1;
   1003 
   1004      if ( flags & PFR_PHY_3BYTE_GPS_OFFSET )
   1005        Size += 1;
   1006 
   1007      PFR_CHECK_SIZE( count * Size );
   1008 
   1009      if ( FT_QNEW_ARRAY( phy_font->chars, count ) )
   1010        goto Fail;
   1011 
   1012      for ( n = 0; n < count; n++ )
   1013      {
   1014        PFR_Char  cur = &phy_font->chars[n];
   1015 
   1016 
   1017        cur->char_code = ( flags & PFR_PHY_2BYTE_CHARCODE )
   1018                         ? PFR_NEXT_USHORT( p )
   1019                         : PFR_NEXT_BYTE( p );
   1020 
   1021        cur->advance   = ( flags & PFR_PHY_PROPORTIONAL )
   1022                         ? PFR_NEXT_SHORT( p )
   1023                         : phy_font->standard_advance;
   1024 
   1025 #if 0
   1026        cur->ascii     = ( flags & PFR_PHY_ASCII_CODE )
   1027                         ? PFR_NEXT_BYTE( p )
   1028                         : 0;
   1029 #else
   1030        if ( flags & PFR_PHY_ASCII_CODE )
   1031          p += 1;
   1032 #endif
   1033        cur->gps_size  = ( flags & PFR_PHY_2BYTE_GPS_SIZE )
   1034                         ? PFR_NEXT_USHORT( p )
   1035                         : PFR_NEXT_BYTE( p );
   1036 
   1037        cur->gps_offset = ( flags & PFR_PHY_3BYTE_GPS_OFFSET )
   1038                          ? PFR_NEXT_ULONG( p )
   1039                          : PFR_NEXT_USHORT( p );
   1040      }
   1041    }
   1042 
   1043    /* that's it! */
   1044 
   1045  Fail:
   1046    FT_FRAME_EXIT();
   1047 
   1048    /* save position of bitmap info */
   1049    phy_font->bct_offset = FT_STREAM_POS();
   1050    phy_font->cursor     = NULL;
   1051 
   1052  Exit:
   1053    return error;
   1054 
   1055  Too_Short:
   1056    error = FT_THROW( Invalid_Table );
   1057    FT_ERROR(( "pfr_phy_font_load: invalid physical font table\n" ));
   1058    goto Fail;
   1059  }
   1060 
   1061 
   1062 /* END */