code:
/***************************************************************************/
/*                                                                         */
/*  cidobjs.c                                                              */
/*                                                                         */
/*    CID objects manager (body).                                          */
/*                                                                         */
/*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2008, 2010 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 FT_INTERNAL_DEBUG_H
#include FT_INTERNAL_STREAM_H

#include "cidgload.h"
#include "cidload.h"

#include FT_SERVICE_POSTSCRIPT_CMAPS_H
#include FT_INTERNAL_POSTSCRIPT_AUX_H
#include FT_INTERNAL_POSTSCRIPT_HINTS_H

#include "ciderrs.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_cidobjs

  /*************************************************************************/
  /*                                                                       */
  /*                            SLOT  FUNCTIONS                            */
  /*                                                                       */
  /*************************************************************************/

  FT_LOCAL_DEF( void )
  cid_slot_done( FT_GlyphSlot  slot )
  {
    slot->internal->glyph_hints = 0;
  }

  FT_LOCAL_DEF( FT_Error )
  cid_slot_init( FT_GlyphSlot  slot )
  {
    CID_Face          face;
    PSHinter_Service  pshinter;

    face     = (CID_Face)slot->face;
    pshinter = (PSHinter_Service)face->pshinter;

    if ( pshinter )
    {
      FT_Module  module;

      module = FT_Get_Module( slot->face->driver->root.library,
                              "pshinter" );
      if ( module )
      {
        T1_Hints_Funcs  funcs;

        funcs = pshinter->get_t1_funcs( module );
        slot->internal->glyph_hints = (void*)funcs;
      }
    }

    return 0;
  }

  /*************************************************************************/
  /*                                                                       */
  /*                           SIZE  FUNCTIONS                             */
  /*                                                                       */
  /*************************************************************************/

  static PSH_Globals_Funcs
  cid_size_get_globals_funcs( CID_Size  size )
  {
    CID_Face          face     = (CID_Face)size->root.face;
    PSHinter_Service  pshinter = (PSHinter_Service)face->pshinter;
    FT_Module         module;

    module = FT_Get_Module( size->root.face->driver->root.library,
                            "pshinter" );
    return ( module && pshinter && pshinter->get_globals_funcs )
           ? pshinter->get_globals_funcs( module )
           : 0;
  }

  FT_LOCAL_DEF( void )
  cid_size_done( FT_Size  cidsize )         /* CID_Size */
  {
    CID_Size  size = (CID_Size)cidsize;

    if ( cidsize->internal )
    {
      PSH_Globals_Funcs  funcs;

      funcs = cid_size_get_globals_funcs( size );
      if ( funcs )
        funcs->destroy( (PSH_Globals)cidsize->internal );

      cidsize->internal = 0;
    }
  }

  FT_LOCAL_DEF( FT_Error )
  cid_size_init( FT_Size  cidsize )     /* CID_Size */
  {
    CID_Size           size  = (CID_Size)cidsize;
    FT_Error           error = CID_Err_Ok;
    PSH_Globals_Funcs  funcs = cid_size_get_globals_funcs( size );

    if ( funcs )
    {
      PSH_Globals   globals;
      CID_Face      face = (CID_Face)cidsize->face;
      CID_FaceDict  dict = face->cid.font_dicts + face->root.face_index;
      PS_Private    priv = &dict->private_dict;

      error = funcs->create( cidsize->face->memory, priv, &globals );
      if ( !error )
        cidsize->internal = (FT_Size_Internal)(void*)globals;
    }

    return error;
  }

  FT_LOCAL( FT_Error )
  cid_size_request( FT_Size          size,
                    FT_Size_Request  req )
  {
    PSH_Globals_Funcs  funcs;

    FT_Request_Metrics( size->face, req );

    funcs = cid_size_get_globals_funcs( (CID_Size)size );

    if ( funcs )
      funcs->set_scale( (PSH_Globals)size->internal,
                        size->metrics.x_scale,
                        size->metrics.y_scale,
                        0, 0 );

    return CID_Err_Ok;
  }

  /*************************************************************************/
  /*                                                                       */
  /*                           FACE  FUNCTIONS                             */
  /*                                                                       */
  /*************************************************************************/

  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    cid_face_done                                                      */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Finalizes a given face object.                                     */
  /*                                                                       */
  /* <Input>                                                               */
  /*    face :: A pointer to the face object to destroy.                   */
  /*                                                                       */
  FT_LOCAL_DEF( void )
  cid_face_done( FT_Face  cidface )         /* CID_Face */
  {
    CID_Face      face = (CID_Face)cidface;
    FT_Memory     memory;
    CID_FaceInfo  cid;
    PS_FontInfo   info;

    if ( !face )
      return;

    cid    = &face->cid;
    info   = &cid->font_info;
    memory = cidface->memory;

    /* release subrs */
    if ( face->subrs )
    {
      FT_Int  n;

      for ( n = 0; n < cid->num_dicts; n++ )
      {
        CID_Subrs  subr = face->subrs + n;

        if ( subr->code )
        {
          FT_FREE( subr->code[0] );
          FT_FREE( subr->code );
        }
      }

      FT_FREE( face->subrs );
    }

    /* release FontInfo strings */
    FT_FREE( info->version );
    FT_FREE( info->notice );
    FT_FREE( info->full_name );
    FT_FREE( info->family_name );
    FT_FREE( info->weight );

    /* release font dictionaries */
    FT_FREE( cid->font_dicts );
    cid->num_dicts = 0;

    /* release other strings */
    FT_FREE( cid->cid_font_name );
    FT_FREE( cid->registry );
    FT_FREE( cid->ordering );

    cidface->family_name = 0;
    cidface->style_name  = 0;

    FT_FREE( face->binary_data );
    FT_FREE( face->cid_stream );
  }

  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    cid_face_init                                                      */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Initializes a given CID face object.                               */
  /*                                                                       */
  /* <Input>                                                               */
  /*    stream     :: The source font stream.                              */
  /*                                                                       */
  /*    face_index :: The index of the font face in the resource.          */
  /*                                                                       */
  /*    num_params :: Number of additional generic parameters.  Ignored.   */
  /*                                                                       */
  /*    params     :: Additional generic parameters.  Ignored.             */
  /*                                                                       */
  /* <InOut>                                                               */
  /*    face       :: The newly built face object.                         */
  /*                                                                       */
  /* <Return>                                                              */
  /*    FreeType error code.  0 means success.                             */
  /*                                                                       */
  FT_LOCAL_DEF( FT_Error )
  cid_face_init( FT_Stream      stream,
                 FT_Face        cidface,        /* CID_Face */
                 FT_Int         face_index,
                 FT_Int         num_params,
                 FT_Parameter*  params )
  {
    CID_Face          face = (CID_Face)cidface;
    FT_Error          error;
    PSAux_Service     psaux;
    PSHinter_Service  pshinter;

    FT_UNUSED( num_params );
    FT_UNUSED( params );
    FT_UNUSED( stream );

    cidface->num_faces = 1;

    psaux = (PSAux_Service)face->psaux;
    if ( !psaux )
    {
      psaux = (PSAux_Service)FT_Get_Module_Interface(
                FT_FACE_LIBRARY( face ), "psaux" );

      face->psaux = psaux;
    }

    pshinter = (PSHinter_Service)face->pshinter;
    if ( !pshinter )
    {
      pshinter = (PSHinter_Service)FT_Get_Module_Interface(
                   FT_FACE_LIBRARY( face ), "pshinter" );

      face->pshinter = pshinter;
    }

    /* open the tokenizer; this will also check the font format */
    if ( FT_STREAM_SEEK( 0 ) )
      goto Exit;

    error = cid_face_open( face, face_index );
    if ( error )
      goto Exit;

    /* if we just wanted to check the format, leave successfully now */
    if ( face_index < 0 )
      goto Exit;

    /* check the face index */
    /* XXX: handle CID fonts with more than a single face */
    if ( face_index != 0 )
    {
      FT_ERROR(( "cid_face_init: invalid face index\n" ));
      error = CID_Err_Invalid_Argument;
      goto Exit;
    }

    /* now load the font program into the face object */

    /* initialize the face object fields */

    /* set up root face fields */
    {
      CID_FaceInfo  cid  = &face->cid;
      PS_FontInfo   info = &cid->font_info;

      cidface->num_glyphs   = cid->cid_count;
      cidface->num_charmaps = 0;

      cidface->face_index = face_index;
      cidface->face_flags = FT_FACE_FLAG_SCALABLE   | /* scalable outlines */
                            FT_FACE_FLAG_HORIZONTAL | /* horizontal data   */
                            FT_FACE_FLAG_HINTER;      /* has native hinter */

      if ( info->is_fixed_pitch )
        cidface->face_flags |= FT_FACE_FLAG_FIXED_WIDTH;

      /* XXX: TODO: add kerning with .afm support */

      /* get style name -- be careful, some broken fonts only */
      /* have a /FontName dictionary entry!                   */
      cidface->family_name = info->family_name;
      /* assume "Regular" style if we don't know better */
      cidface->style_name = (char *)"Regular";
      if ( cidface->family_name )
      {
        char*  full   = info->full_name;
        char*  family = cidface->family_name;

        if ( full )
        {
          while ( *full )
          {
            if ( *full == *family )
            {
              family++;
              full++;
            }
            else
            {
              if ( *full == ' ' || *full == '-' )
                full++;
              else if ( *family == ' ' || *family == '-' )
                family++;
              else
              {
                if ( !*family )
                  cidface->style_name = full;
                break;
              }
            }
          }
        }
      }
      else
      {
        /* do we have a `/FontName'? */
        if ( cid->cid_font_name )
          cidface->family_name = cid->cid_font_name;
      }

      /* compute style flags */
      cidface->style_flags = 0;
      if ( info->italic_angle )
        cidface->style_flags |= FT_STYLE_FLAG_ITALIC;
      if ( info->weight )
      {
        if ( !ft_strcmp( info->weight, "Bold"  ) ||
             !ft_strcmp( info->weight, "Black" ) )
          cidface->style_flags |= FT_STYLE_FLAG_BOLD;
      }

      /* no embedded bitmap support */
      cidface->num_fixed_sizes = 0;
      cidface->available_sizes = 0;

      cidface->bbox.xMin =   cid->font_bbox.xMin            >> 16;
      cidface->bbox.yMin =   cid->font_bbox.yMin            >> 16;
      /* no `U' suffix here to 0xFFFF! */
      cidface->bbox.xMax = ( cid->font_bbox.xMax + 0xFFFF ) >> 16;
      cidface->bbox.yMax = ( cid->font_bbox.yMax + 0xFFFF ) >> 16;

      if ( !cidface->units_per_EM )
        cidface->units_per_EM = 1000;

      cidface->ascender  = (FT_Short)( cidface->bbox.yMax );
      cidface->descender = (FT_Short)( cidface->bbox.yMin );

      cidface->height = (FT_Short)( ( cidface->units_per_EM * 12 ) / 10 );
      if ( cidface->height < cidface->ascender - cidface->descender )
        cidface->height = (FT_Short)( cidface->ascender - cidface->descender );

      cidface->underline_position  = (FT_Short)info->underline_position;
      cidface->underline_thickness = (FT_Short)info->underline_thickness;
    }

  Exit:
    return error;
  }

  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    cid_driver_init                                                    */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Initializes a given CID driver object.                             */
  /*                                                                       */
  /* <Input>                                                               */
  /*    driver :: A handle to the target driver object.                    */
  /*                                                                       */
  /* <Return>                                                              */
  /*    FreeType error code.  0 means success.                             */
  /*                                                                       */
  FT_LOCAL_DEF( FT_Error )
  cid_driver_init( FT_Module  driver )
  {
    FT_UNUSED( driver );

    return CID_Err_Ok;
  }

  /*************************************************************************/
  /*                                                                       */
  /* <Function>                                                            */
  /*    cid_driver_done                                                    */
  /*                                                                       */
  /* <Description>                                                         */
  /*    Finalizes a given CID driver.                                      */
  /*                                                                       */
  /* <Input>                                                               */
  /*    driver :: A handle to the target CID driver.                       */
  /*                                                                       */
  FT_LOCAL_DEF( void )
  cid_driver_done( FT_Module  driver )
  {
    FT_UNUSED( driver );
  }

/* END */


code:
/***************************************************************************/
/*                                                                         */
/*  cidobjs.h                                                              */
/*                                                                         */
/*    CID objects manager (specification).                                 */
/*                                                                         */
/*  Copyright 1996-2001, 2002, 2004, 2006 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.                                        */
/*                                                                         */
/***************************************************************************/

#ifndef __CIDOBJS_H__
#define __CIDOBJS_H__

#include <ft2build.h>
#include FT_INTERNAL_OBJECTS_H
#include FT_CONFIG_CONFIG_H
#include FT_INTERNAL_TYPE1_TYPES_H

FT_BEGIN_HEADER

  /* The following structures must be defined by the hinter */
  typedef struct CID_Size_Hints_   CID_Size_Hints;
  typedef struct CID_Glyph_Hints_  CID_Glyph_Hints;

  /*************************************************************************/
  /*                                                                       */
  /* <Type>                                                                */
  /*    CID_Driver                                                         */
  /*                                                                       */
  /* <Description>                                                         */
  /*    A handle to a Type 1 driver object.                                */
  /*                                                                       */
  typedef struct CID_DriverRec_*  CID_Driver;

  /*************************************************************************/
  /*                                                                       */
  /* <Type>                                                                */
  /*    CID_Size                                                           */
  /*                                                                       */
  /* <Description>                                                         */
  /*    A handle to a Type 1 size object.                                  */
  /*                                                                       */
  typedef struct CID_SizeRec_*  CID_Size;

  /*************************************************************************/
  /*                                                                       */
  /* <Type>                                                                */
  /*    CID_GlyphSlot                                                      */
  /*                                                                       */
  /* <Description>                                                         */
  /*    A handle to a Type 1 glyph slot object.                            */
  /*                                                                       */
  typedef struct CID_GlyphSlotRec_*  CID_GlyphSlot;

  /*************************************************************************/
  /*                                                                       */
  /* <Type>                                                                */
  /*    CID_CharMap                                                        */
  /*                                                                       */
  /* <Description>                                                         */
  /*    A handle to a Type 1 character mapping object.                     */
  /*                                                                       */
  /* <Note>                                                                */
  /*    The Type 1 format doesn't use a charmap but an encoding table.     */
  /*    The driver is responsible for making up charmap objects            */
  /*    corresponding to these tables.                                     */
  /*                                                                       */
  typedef struct CID_CharMapRec_*  CID_CharMap;

  /*************************************************************************/
  /*                                                                       */
  /* HERE BEGINS THE TYPE 1 SPECIFIC STUFF                                 */
  /*                                                                       */
  /*************************************************************************/

  typedef struct  CID_SizeRec_
  {
    FT_SizeRec  root;
    FT_Bool     valid;

  } CID_SizeRec;

  typedef struct  CID_GlyphSlotRec_
  {
    FT_GlyphSlotRec  root;

    FT_Bool          hint;
    FT_Bool          scaled;

    FT_Fixed         x_scale;
    FT_Fixed         y_scale;

  } CID_GlyphSlotRec;

  FT_LOCAL( void )
  cid_slot_done( FT_GlyphSlot  slot );

  FT_LOCAL( FT_Error )
  cid_slot_init( FT_GlyphSlot  slot );

  FT_LOCAL( void )
  cid_size_done( FT_Size  size );       /* CID_Size */

  FT_LOCAL( FT_Error )
  cid_size_init( FT_Size  size );       /* CID_Size */

  FT_LOCAL( FT_Error )
  cid_size_request( FT_Size          size,      /* CID_Size */
                    FT_Size_Request  req );

  FT_LOCAL( FT_Error )
  cid_face_init( FT_Stream      stream,
                 FT_Face        face,           /* CID_Face */
                 FT_Int         face_index,
                 FT_Int         num_params,
                 FT_Parameter*  params );

  FT_LOCAL( void )
  cid_face_done( FT_Face  face );               /* CID_Face */

  FT_LOCAL( FT_Error )
  cid_driver_init( FT_Module  driver );

  FT_LOCAL( void )
  cid_driver_done( FT_Module  driver );

FT_END_HEADER

#endif /* __CIDOBJS_H__ */

/* END */

license: mit


code:
/***************************************************************************/
/*                                                                         */
/*  cidparse.c                                                             */
/*                                                                         */
/*    CID-keyed Type1 parser (body).                                       */
/*                                                                         */
/*  Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2009 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 FT_INTERNAL_DEBUG_H
#include FT_INTERNAL_OBJECTS_H
#include FT_INTERNAL_STREAM_H

#include "cidparse.h"

#include "ciderrs.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_cidparse

  /*************************************************************************/
  /*************************************************************************/
  /*************************************************************************/
  /*****                                                               *****/
  /*****                    INPUT STREAM PARSER                        *****/
  /*****                                                               *****/
  /*************************************************************************/
  /*************************************************************************/
  /*************************************************************************/

  FT_LOCAL_DEF( FT_Error )
  cid_parser_new( CID_Parser*    parser,
                  FT_Stream      stream,
                  FT_Memory      memory,
                  PSAux_Service  psaux )
  {
    FT_Error  error;
    FT_ULong  base_offset, offset, ps_len;
    FT_Byte   *cur, *limit;
    FT_Byte   *arg1, *arg2;

    FT_MEM_ZERO( parser, sizeof ( *parser ) );
    psaux->ps_parser_funcs->init( &parser->root, 0, 0, memory );

    parser->stream = stream;

    base_offset = FT_STREAM_POS();

    /* first of all, check the font format in the header */
    if ( FT_FRAME_ENTER( 31 ) )
      goto Exit;

    if ( ft_strncmp( (char *)stream->cursor,
                     "%!PS-Adobe-3.0 Resource-CIDFont", 31 ) )
    {
      FT_TRACE2(( "[not a valid CID-keyed font]\n" ));
      error = CID_Err_Unknown_File_Format;
    }

    FT_FRAME_EXIT();
    if ( error )
      goto Exit;

  Again:
    /* now, read the rest of the file until we find */
    /* `StartData' or `/sfnts'                      */
    {
      FT_Byte   buffer[256 + 10];
      FT_Long   read_len = 256 + 10; /* same as signed FT_Stream->size */
      FT_Byte*  p        = buffer;

      for ( offset = FT_STREAM_POS(); ; offset += 256 )
      {
        FT_Long  stream_len; /* same as signed FT_Stream->size */

        stream_len = stream->size - FT_STREAM_POS();
        if ( stream_len == 0 )
        {
          FT_TRACE2(( "cid_parser_new: no `StartData' keyword found\n" ));
          error = CID_Err_Unknown_File_Format;
          goto Exit;
        }

        read_len = FT_MIN( read_len, stream_len );
        if ( FT_STREAM_READ( p, read_len ) )
          goto Exit;

        if ( read_len < 256 )
          p[read_len]  = '\0';

        limit = p + read_len - 10;

        for ( p = buffer; p < limit; p++ )
        {
          if ( p[0] == 'S' && ft_strncmp( (char*)p, "StartData", 9 ) == 0 )
          {
            /* save offset of binary data after `StartData' */
            offset += p - buffer + 10;
            goto Found;
          }
          else if ( p[1] == 's' && ft_strncmp( (char*)p, "/sfnts", 6 ) == 0 )
          {
            offset += p - buffer + 7;
            goto Found;
          }
        }

        FT_MEM_MOVE( buffer, p, 10 );
        read_len = 256;
        p = buffer + 10;
      }
    }

  Found:
    /* We have found the start of the binary data or the `/sfnts' token. */
    /* Now rewind and extract the frame corresponding to this PostScript */
    /* section.                                                          */

    ps_len = offset - base_offset;
    if ( FT_STREAM_SEEK( base_offset )                  ||
         FT_FRAME_EXTRACT( ps_len, parser->postscript ) )
      goto Exit;

    parser->data_offset    = offset;
    parser->postscript_len = ps_len;
    parser->root.base      = parser->postscript;
    parser->root.cursor    = parser->postscript;
    parser->root.limit     = parser->root.cursor + ps_len;
    parser->num_dict       = -1;

    /* Finally, we check whether `StartData' or `/sfnts' was real --  */
    /* it could be in a comment or string.  We also get the arguments */
    /* of `StartData' to find out whether the data is represented in  */
    /* binary or hex format.                                          */

    arg1 = parser->root.cursor;
    cid_parser_skip_PS_token( parser );
    cid_parser_skip_spaces  ( parser );
    arg2 = parser->root.cursor;
    cid_parser_skip_PS_token( parser );
    cid_parser_skip_spaces  ( parser );

    limit = parser->root.limit;
    cur   = parser->root.cursor;

    while ( cur < limit )
    {
      if ( parser->root.error )
      {
        error = parser->root.error;
        goto Exit;
      }

      if ( cur[0] == 'S' && ft_strncmp( (char*)cur, "StartData", 9 ) == 0 )
      {
        if ( ft_strncmp( (char*)arg1, "(Hex)", 5 ) == 0 )
          parser->binary_length = ft_atol( (const char *)arg2 );

        limit = parser->root.limit;
        cur   = parser->root.cursor;
        goto Exit;
      }
      else if ( cur[1] == 's' && ft_strncmp( (char*)cur, "/sfnts", 6 ) == 0 )
      {
        FT_TRACE2(( "cid_parser_new: cannot handle Type 11 fonts\n" ));
        error = CID_Err_Unknown_File_Format;
        goto Exit;
      }

      cid_parser_skip_PS_token( parser );
      cid_parser_skip_spaces  ( parser );
      arg1 = arg2;
      arg2 = cur;
      cur  = parser->root.cursor;
    }

    /* we haven't found the correct `StartData'; go back and continue */
    /* searching                                                      */
    FT_FRAME_RELEASE( parser->postscript );
    if ( !FT_STREAM_SEEK( offset ) )
      goto Again;

  Exit:
    return error;
  }

  FT_LOCAL_DEF( void )
  cid_parser_done( CID_Parser*  parser )
  {
    /* always free the private dictionary */
    if ( parser->postscript )
    {
      FT_Stream  stream = parser->stream;

      FT_FRAME_RELEASE( parser->postscript );
    }
    parser->root.funcs.done( &parser->root );
  }

/* END */


code:
/***************************************************************************/
/*                                                                         */
/*  cidparse.h                                                             */
/*                                                                         */
/*    CID-keyed Type1 parser (specification).                              */
/*                                                                         */
/*  Copyright 1996-2001, 2002, 2003, 2004 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.                                        */
/*                                                                         */
/***************************************************************************/

#ifndef __CIDPARSE_H__
#define __CIDPARSE_H__

#include <ft2build.h>
#include FT_INTERNAL_TYPE1_TYPES_H
#include FT_INTERNAL_STREAM_H
#include FT_INTERNAL_POSTSCRIPT_AUX_H

FT_BEGIN_HEADER

  /*************************************************************************/
  /*                                                                       */
  /* <Struct>                                                              */
  /*    CID_Parser                                                         */
  /*                                                                       */
  /* <Description>                                                         */
  /*    A CID_Parser is an object used to parse a Type 1 fonts very        */
  /*    quickly.                                                           */
  /*                                                                       */
  /* <Fields>                                                              */
  /*    root           :: The root PS_ParserRec fields.                    */
  /*                                                                       */
  /*    stream         :: The current input stream.                        */
  /*                                                                       */
  /*    postscript     :: A pointer to the data to be parsed.              */
  /*                                                                       */
  /*    postscript_len :: The length of the data to be parsed.             */
  /*                                                                       */
  /*    data_offset    :: The start position of the binary data (i.e., the */
  /*                      end of the data to be parsed.                    */
  /*                                                                       */
  /*    binary_length  :: The length of the data after the `StartData'     */
  /*                      command if the data format is hexadecimal.       */
  /*                                                                       */
  /*    cid            :: A structure which holds the information about    */
  /*                      the current font.                                */
  /*                                                                       */
  /*    num_dict       :: The number of font dictionaries.                 */
  /*                                                                       */
  typedef struct  CID_Parser_
  {
    PS_ParserRec  root;
    FT_Stream     stream;

    FT_Byte*      postscript;
    FT_Long       postscript_len;

    FT_ULong      data_offset;

    FT_Long       binary_length;

    CID_FaceInfo  cid;
    FT_Int        num_dict;

  } CID_Parser;

  FT_LOCAL( FT_Error )
  cid_parser_new( CID_Parser*    parser,
                  FT_Stream      stream,
                  FT_Memory      memory,
                  PSAux_Service  psaux );

  FT_LOCAL( void )
  cid_parser_done( CID_Parser*  parser );

  /*************************************************************************/
  /*                                                                       */
  /*                            PARSING ROUTINES                           */
  /*                                                                       */
  /*************************************************************************/

#define cid_parser_skip_spaces( p ) \
          (p)->root.funcs.skip_spaces( &(p)->root )
#define cid_parser_skip_PS_token( p ) \
          (p)->root.funcs.skip_PS_token( &(p)->root )

#define cid_parser_to_int( p )        (p)->root.funcs.to_int( &(p)->root )
#define cid_parser_to_fixed( p, t )   (p)->root.funcs.to_fixed( &(p)->root, t )

#define cid_parser_to_coord_array( p, m, c )                          \
          (p)->root.funcs.to_coord_array( &(p)->root, m, c )
#define cid_parser_to_fixed_array( p, m, f, t )                          \
          (p)->root.funcs.to_fixed_array( &(p)->root, m, f, t )
#define cid_parser_to_token( p, t )                         \
          (p)->root.funcs.to_token( &(p)->root, t )
#define cid_parser_to_token_array( p, t, m, c )                          \
          (p)->root.funcs.to_token_array( &(p)->root, t, m, c )

#define cid_parser_load_field( p, f, o )                              \
          (p)->root.funcs.load_field( &(p)->root, f, o, 0, 0 )
#define cid_parser_load_field_table( p, f, o )                              \
          (p)->root.funcs.load_field_table( &(p)->root, f, o, 0, 0 )

FT_END_HEADER

#endif /* __CIDPARSE_H__ */

/* END */


code:
/***************************************************************************/
/*                                                                         */
/*  cidriver.c                                                             */
/*                                                                         */
/*    CID driver interface (body).                                         */
/*                                                                         */
/*  Copyright 1996-2001, 2002, 2003, 2004, 2006, 2008, 2009 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 "cidriver.h"
#include "cidgload.h"
#include FT_INTERNAL_DEBUG_H

#include "ciderrs.h"

#include FT_SERVICE_POSTSCRIPT_NAME_H
#include FT_SERVICE_XFREE86_NAME_H
#include FT_SERVICE_POSTSCRIPT_INFO_H
#include FT_SERVICE_CID_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_ciddriver

  /*
   *  POSTSCRIPT NAME SERVICE
   *
   */

  static const char*
  cid_get_postscript_name( CID_Face  face )
  {
    const char*  result = face->cid.cid_font_name;

    if ( result && result[0] == '/' )
      result++;

    return result;
  }

  static const FT_Service_PsFontNameRec  cid_service_ps_name =
  {
    (FT_PsName_GetFunc) cid_get_postscript_name
  };

  /*
   *  POSTSCRIPT INFO SERVICE
   *
   */

  static FT_Error
  cid_ps_get_font_info( FT_Face          face,
                        PS_FontInfoRec*  afont_info )
  {
    *afont_info = ((CID_Face)face)->cid.font_info;

    return CID_Err_Ok;
  }

  static FT_Error
  cid_ps_get_font_extra( FT_Face          face,
                        PS_FontExtraRec*  afont_extra )
  {
    *afont_extra = ((CID_Face)face)->font_extra;

    return CID_Err_Ok;
  }

  static const FT_Service_PsInfoRec  cid_service_ps_info =
  {
    (PS_GetFontInfoFunc)   cid_ps_get_font_info,
    (PS_GetFontExtraFunc)  cid_ps_get_font_extra,
    (PS_HasGlyphNamesFunc) NULL,        /* unsupported with CID fonts */
    (PS_GetFontPrivateFunc)NULL         /* unsupported                */
  };

  /*
   *  CID INFO SERVICE
   *
   */
  static FT_Error
  cid_get_ros( CID_Face      face,
               const char*  *registry,
               const char*  *ordering,
               FT_Int       *supplement )
  {
    CID_FaceInfo  cid = &face->cid;

    if ( registry )
      *registry = cid->registry;

    if ( ordering )
      *ordering = cid->ordering;

    if ( supplement )
      *supplement = cid->supplement;

    return CID_Err_Ok;
  }

  static FT_Error
  cid_get_is_cid( CID_Face  face,
                  FT_Bool  *is_cid )
  {
    FT_Error  error = CID_Err_Ok;
    FT_UNUSED( face );

    if ( is_cid )
      *is_cid = 1; /* cid driver is only used for CID keyed fonts */

    return error;
  }

  static FT_Error
  cid_get_cid_from_glyph_index( CID_Face  face,
                                FT_UInt   glyph_index,
                                FT_UInt  *cid )
  {
    FT_Error  error = CID_Err_Ok;
    FT_UNUSED( face );

    if ( cid )
      *cid = glyph_index; /* identity mapping */

    return error;
  }

  static const FT_Service_CIDRec  cid_service_cid_info =
  {
     (FT_CID_GetRegistryOrderingSupplementFunc)cid_get_ros,
     (FT_CID_GetIsInternallyCIDKeyedFunc)      cid_get_is_cid,
     (FT_CID_GetCIDFromGlyphIndexFunc)         cid_get_cid_from_glyph_index
  };

  /*
   *  SERVICE LIST
   *
   */

  static const FT_ServiceDescRec  cid_services[] =
  {
    { FT_SERVICE_ID_XF86_NAME,            FT_XF86_FORMAT_CID },
    { FT_SERVICE_ID_POSTSCRIPT_FONT_NAME, &cid_service_ps_name },
    { FT_SERVICE_ID_POSTSCRIPT_INFO,      &cid_service_ps_info },
    { FT_SERVICE_ID_CID,                  &cid_service_cid_info },
    { NULL, NULL }
  };

  FT_CALLBACK_DEF( FT_Module_Interface )
  cid_get_interface( FT_Module    module,
                     const char*  cid_interface )
  {
    FT_UNUSED( module );

    return ft_service_list_lookup( cid_services, cid_interface );
  }

  FT_CALLBACK_TABLE_DEF
  const FT_Driver_ClassRec  t1cid_driver_class =
  {
    /* first of all, the FT_Module_Class fields */
    {
      FT_MODULE_FONT_DRIVER       |
      FT_MODULE_DRIVER_SCALABLE   |
      FT_MODULE_DRIVER_HAS_HINTER,

      sizeof( FT_DriverRec ),
      "t1cid",   /* module name           */
      0x10000L,  /* version 1.0 of driver */
      0x20000L,  /* requires FreeType 2.0 */

      0,

      cid_driver_init,
      cid_driver_done,
      cid_get_interface
    },

    /* then the other font drivers fields */
    sizeof( CID_FaceRec ),
    sizeof( CID_SizeRec ),
    sizeof( CID_GlyphSlotRec ),

    cid_face_init,
    cid_face_done,

    cid_size_init,
    cid_size_done,
    cid_slot_init,
    cid_slot_done,

#ifdef FT_CONFIG_OPTION_OLD_INTERNALS
    ft_stub_set_char_sizes,
    ft_stub_set_pixel_sizes,
#endif

    cid_slot_load_glyph,

    0,                      /* FT_Face_GetKerningFunc  */
    0,                      /* FT_Face_AttachFunc      */

    0,                      /* FT_Face_GetAdvancesFunc */

    cid_size_request,
    0                       /* FT_Size_SelectFunc      */
  };

/* END */


code:
/***************************************************************************/
/*                                                                         */
/*  cidriver.h                                                             */
/*                                                                         */
/*    High-level CID driver interface (specification).                     */
/*                                                                         */
/*  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.                                        */
/*                                                                         */
/***************************************************************************/

#ifndef __CIDRIVER_H__
#define __CIDRIVER_H__

#include <ft2build.h>
#include FT_INTERNAL_DRIVER_H

FT_BEGIN_HEADER

#ifdef FT_CONFIG_OPTION_PIC
#error "this module does not support PIC yet"
#endif

  FT_CALLBACK_TABLE
  const FT_Driver_ClassRec  t1cid_driver_class;

FT_END_HEADER

#endif /* __CIDRIVER_H__ */

/* END */


code:
/***************************************************************************/
/*                                                                         */
/*  cidtoken.h                                                             */
/*                                                                         */
/*    CID token definitions (specification only).                          */
/*                                                                         */
/*  Copyright 1996-2001, 2002, 2003, 2006, 2008, 2009 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.                                        */
/*                                                                         */
/***************************************************************************/

#undef  FT_STRUCTURE
#define FT_STRUCTURE  CID_FaceInfoRec
#undef  T1CODE
#define T1CODE        T1_FIELD_LOCATION_CID_INFO

  T1_FIELD_KEY   ( "CIDFontName",    cid_font_name, 0 )
  T1_FIELD_FIXED ( "CIDFontVersion", cid_version,   0 )
  T1_FIELD_NUM   ( "CIDFontType",    cid_font_type, 0 )
  T1_FIELD_STRING( "Registry",       registry,      0 )
  T1_FIELD_STRING( "Ordering",       ordering,      0 )
  T1_FIELD_NUM   ( "Supplement",     supplement,    0 )
  T1_FIELD_NUM   ( "UIDBase",        uid_base,      0 )
  T1_FIELD_NUM   ( "CIDMapOffset",   cidmap_offset, 0 )
  T1_FIELD_NUM   ( "FDBytes",        fd_bytes,      0 )
  T1_FIELD_NUM   ( "GDBytes",        gd_bytes,      0 )
  T1_FIELD_NUM   ( "CIDCount",       cid_count,     0 )

#undef  FT_STRUCTURE
#define FT_STRUCTURE  PS_FontInfoRec
#undef  T1CODE
#define T1CODE        T1_FIELD_LOCATION_FONT_INFO

  T1_FIELD_STRING( "version",            version,             0 )
  T1_FIELD_STRING( "Notice",             notice,              0 )
  T1_FIELD_STRING( "FullName",           full_name,           0 )
  T1_FIELD_STRING( "FamilyName",         family_name,         0 )
  T1_FIELD_STRING( "Weight",             weight,              0 )
  T1_FIELD_NUM   ( "ItalicAngle",        italic_angle,        0 )
  T1_FIELD_BOOL  ( "isFixedPitch",       is_fixed_pitch,      0 )
  T1_FIELD_NUM   ( "UnderlinePosition",  underline_position,  0 )
  T1_FIELD_NUM   ( "UnderlineThickness", underline_thickness, 0 )

#undef  FT_STRUCTURE
#define FT_STRUCTURE  PS_FontExtraRec
#undef  T1CODE
#define T1CODE        T1_FIELD_LOCATION_FONT_EXTRA

  T1_FIELD_NUM   ( "FSType",             fs_type,             0 )

#undef  FT_STRUCTURE
#define FT_STRUCTURE  CID_FaceDictRec
#undef  T1CODE
#define T1CODE        T1_FIELD_LOCATION_FONT_DICT

  T1_FIELD_NUM  ( "PaintType",          paint_type,          0 )
  T1_FIELD_NUM  ( "FontType",           font_type,           0 )
  T1_FIELD_NUM  ( "SubrMapOffset",      subrmap_offset,      0 )
  T1_FIELD_NUM  ( "SDBytes",            sd_bytes,            0 )
  T1_FIELD_NUM  ( "SubrCount",          num_subrs,           0 )
  T1_FIELD_NUM  ( "lenBuildCharArray",  len_buildchar,       0 )
  T1_FIELD_FIXED( "ForceBoldThreshold", forcebold_threshold, 0 )
  T1_FIELD_FIXED( "StrokeWidth",        stroke_width,        0 )

#undef  FT_STRUCTURE
#define FT_STRUCTURE  PS_PrivateRec
#undef  T1CODE
#define T1CODE        T1_FIELD_LOCATION_PRIVATE

  T1_FIELD_NUM       ( "UniqueID",         unique_id,      0 )
  T1_FIELD_NUM       ( "lenIV",            lenIV,          0 )
  T1_FIELD_NUM       ( "LanguageGroup",    language_group, 0 )
  T1_FIELD_NUM       ( "password",         password,       0 )

  T1_FIELD_FIXED_1000( "BlueScale",        blue_scale,     0 )
  T1_FIELD_NUM       ( "BlueShift",        blue_shift,     0 )
  T1_FIELD_NUM       ( "BlueFuzz",         blue_fuzz,      0 )

  T1_FIELD_NUM_TABLE ( "BlueValues",       blue_values,        14, 0 )
  T1_FIELD_NUM_TABLE ( "OtherBlues",       other_blues,        10, 0 )
  T1_FIELD_NUM_TABLE ( "FamilyBlues",      family_blues,       14, 0 )
  T1_FIELD_NUM_TABLE ( "FamilyOtherBlues", family_other_blues, 10, 0 )

  T1_FIELD_NUM_TABLE2( "StdHW",            standard_width,      1, 0 )
  T1_FIELD_NUM_TABLE2( "StdVW",            standard_height,     1, 0 )
  T1_FIELD_NUM_TABLE2( "MinFeature",       min_feature,         2, 0 )

  T1_FIELD_NUM_TABLE ( "StemSnapH",        snap_widths,        12, 0 )
  T1_FIELD_NUM_TABLE ( "StemSnapV",        snap_heights,       12, 0 )

  T1_FIELD_BOOL      ( "ForceBold",        force_bold,          0 )

#undef  FT_STRUCTURE
#define FT_STRUCTURE  FT_BBox
#undef  T1CODE
#define T1CODE        T1_FIELD_LOCATION_BBOX

  T1_FIELD_BBOX( "FontBBox", xMin, 0 )

/* END */


code:
#
# FreeType 2 CID module definition
#

# Copyright 1996-2000, 2006 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.

FTMODULE_H_COMMANDS += TYPE1CID_DRIVER

define TYPE1CID_DRIVER
$(OPEN_DRIVER) FT_Driver_ClassRec, t1cid_driver_class $(CLOSE_DRIVER)
$(ECHO_DRIVER)cid       $(ECHO_DRIVER_DESC)Postscript CID-keyed fonts, no known extension$(ECHO_DRIVER_DONE)
endef

# EOF


code:
#
# FreeType 2 CID driver configuration rules
#

# Copyright 1996-2000, 2001, 2003 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.

# CID driver directory
#
CID_DIR := $(SRC_DIR)/cid

CID_COMPILE := $(FT_COMPILE) $I$(subst /,$(COMPILER_SEP),$(CID_DIR))

# CID driver sources (i.e., C files)
#
CID_DRV_SRC := $(CID_DIR)/cidparse.c \
               $(CID_DIR)/cidload.c  \
               $(CID_DIR)/cidriver.c \
               $(CID_DIR)/cidgload.c \
               $(CID_DIR)/cidobjs.c

# CID driver headers
#
CID_DRV_H := $(CID_DRV_SRC:%.c=%.h) \
             $(CID_DIR)/cidtoken.h  \
             $(CID_DIR)/ciderrs.h

# CID driver object(s)
#
#   CID_DRV_OBJ_M is used during `multi' builds
#   CID_DRV_OBJ_S is used during `single' builds
#
CID_DRV_OBJ_M := $(CID_DRV_SRC:$(CID_DIR)/%.c=$(OBJ_DIR)/%.$O)
CID_DRV_OBJ_S := $(OBJ_DIR)/type1cid.$O

# CID driver source file for single build
#
CID_DRV_SRC_S := $(CID_DIR)/type1cid.c

# CID driver - single object
#
$(CID_DRV_OBJ_S): $(CID_DRV_SRC_S) $(CID_DRV_SRC) $(FREETYPE_H) $(CID_DRV_H)
	$(CID_COMPILE) $T$(subst /,$(COMPILER_SEP),$@ $(CID_DRV_SRC_S))

# CID driver - multiple objects
#
$(OBJ_DIR)/%.$O: $(CID_DIR)/%.c $(FREETYPE_H) $(CID_DRV_H)
	$(CID_COMPILE) $T$(subst /,$(COMPILER_SEP),$@ $<)

# update main driver object lists
#
DRV_OBJS_S += $(CID_DRV_OBJ_S)
DRV_OBJS_M += $(CID_DRV_OBJ_M)

# EOF


code:
/***************************************************************************/
/*                                                                         */
/*  type1cid.c                                                             */
/*                                                                         */
/*    FreeType OpenType driver component (body only).                      */
/*                                                                         */
/*  Copyright 1996-2001 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.                                        */
/*                                                                         */
/***************************************************************************/

#define FT_MAKE_OPTION_SINGLE_OBJECT

#include <ft2build.h>
#include "cidparse.c"
#include "cidload.c"
#include "cidobjs.c"
#include "cidriver.c"
#include "cidgload.c"

/* END */


code:
# FreeType 2 src/gxvalid Jamfile
#
# Copyright 2005 by
# suzuki toshiya, Masatake YAMATO and Red Hat K.K.
#
# 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.

SubDir  FT2_TOP $(FT2_SRC_DIR) gxvalid ;

{
  local  _sources ;

  if $(FT2_MULTI)
  {
    _sources = gxvcommn gxvfeat  gxvbsln  gxvtrak  gxvopbd  gxvprop
               gxvmort  gxvmort0 gxvmort1 gxvmort2 gxvmort4 gxvmort5
               gxvmorx  gxvmorx0 gxvmorx1 gxvmorx2 gxvmorx4 gxvmorx5
               gxvlcar  gxvkern  gxvmod   gxvjust  ;
  }
  else
  {
    _sources = gxvalid ;
  }

  Library  $(FT2_LIB) : $(_sources).c ;
}

# end of src/gxvalid Jamfile


code:
gxvalid: TrueType GX validator
==============================

1. What is this
---------------

  `gxvalid' is a module to  validate TrueType GX tables: a collection of
  additional tables  in TrueType  font which are  used by  `QuickDraw GX
  Text',  Apple Advanced  Typography  (AAT).  In  addition, gxvalid  can
  validates `kern'  tables which have  been extended for AAT.   Like the
  otvalid  module,   gxvalid  uses  Freetype   2's  validator  framework
  (ftvalid).

  You can link gxvalid with your program; before running your own layout
  engine, gxvalid validates a font  file.  As the result, you can remove
  error-checking code  from the layout  engine.  It is also  possible to
  use  gxvalid  as a  stand-alone  font  validator;  the `ftvalid'  test
  program  included  in the  ft2demo  bundle  calls gxvalid  internally.
  A stand-alone font validator may be useful for font developers.

  This documents documents the following issues.

  - supported TrueType GX tables
  - fundamental validation limitations
  - permissive error handling of broken GX tables
  - `kern' table issue.

2. Supported tables
-------------------

  The following GX tables are currently supported.

    bsln
    feat
    just
    kern(*)
    lcar
    mort
    morx
    opbd
    prop
    trak

  The following GX tables are currently unsupported.

    cvar
    fdsc
    fmtx
    fvar
    gvar
    Zapf

  The following GX tables won't be supported.

    acnt(**)
    hsty(***)

  The following undocumented tables in TrueType fonts designed for Apple
  platform aren't handled either.

    addg
    CVTM
    TPNM
    umif

  *)   The `kern'  validator handles both  the classic and the  new kern
       formats;  the former  is supported  on both  Microsoft  and Apple
       platforms, while the latter is supported on Apple platforms.

  **)  `acnt' tables are not supported by currently available Apple font
       tools.

  ***) There  is  one more  Apple  extension,  `hsty',  but  it  is  for
       Newton-OS, not GX  (Newton-OS is a platform by  Apple, but it can
       use  sfnt- housed bitmap  fonts only).   Therefore, it  should be
       excluded  from  `Apple  platform'  in the  context  of  TrueType.
       gxvalid ignores it as Apple font tools do so.

  We have  checked 183  fonts bundled with  MacOS 9.1, MacOS  9.2, MacOS
  10.0, MacOS X 10.1, MSIE  for MacOS, and AppleWorks 6.0.  In addition,
  we have  checked 67 Dynalab fonts  (designed for MacOS)  and 189 Ricoh
  fonts (designed for Windows and  MacOS dual platforms).  The number of
  fonts including TrueType GX tables are as follows.

    bsln:  76
    feat: 191
    just:  84
    kern:  59
    lcar:   4
    mort: 326
    morx:  19
    opbd:   4
    prop: 114
    trak:  16

  Dynalab  and Ricoh fonts  don't have  GX tables  except of  `feat' and
  `mort'.

3. Fundamental validation limitations
-------------------------------------

  TrueType  GX  provides  layout   information  to  libraries  for  font
  rasterizers  and text layout.   gxvalid can  check whether  the layout
  data in  a font is conformant  to the TrueType GX  format specified by
  Apple.  But gxvalid cannot check  a how QuickDraw GX/AAT renderer uses
  the stored information.

  3-1. Validation of State Machine activity
  -----------------------------------------

    QuickDraw GX/AAT uses a `State Machine' to provide `stateful' layout
    features,  and TrueType GX  stores the  state transition  diagram of
    this `State  Machine' in a  `StateTable' data structure.   While the
    State  Machine receives  a series  of glyph  IDs, the  State Machine
    starts with `start  of text' state, walks around  various states and
    generates various  layout information  to the  renderer, and finally
    reaches the `end of text' state.

    gxvalid can check essential errors like:

      - possibility of state transitions to undefined states
      - existence of glyph  IDs that the State Machine  doesn't know how
        to handle
      - the  State Machine  cannot compute  the layout  information from
        given diagram

    These errors  can be  checked within finite  steps, and  without the
    State Machine itself, because these are `expression' errors of state
    transition diagram.

    There  is no  limitation  about  how long  the  State Machine  walks
    around,  so validation  of  the algorithm  in  the state  transition
    diagram requires infinite  steps, even if we had  a State Machine in
    gxvalid.   Therefore, the  following errors  and problems  cannot be
    checked.

      - existence of states which the State Machine never transits to
      - the  possibility that the  State Machine  never reaches  `end of
        text'
      - the possibility of stack underflow/overflow in the State Machine
        (in  ligature  and  contextual  glyph substitutions,  the  State
        Machine can store 16 glyphs onto its stack)

    In addition, gxvalid doesn't check `temporary glyph IDs' used in the
    chained State Machines  (in `mort' and `morx' tables).   If a layout
    feature  is  implemented by  a  single  State  Machine, a  glyph  ID
    converted by the State Machine is passed to the glyph renderer, thus
    it  should not  point to  an undefined  glyph ID.   But if  a layout
    feature is implemented by  chained State Machines, a component State
    Machine  (if it  is  not the  final  one) is  permitted to  generate
    undefined glyph IDs for temporary use, because it is handled by next
    component State Machine and not  by the glyph renderer.  To validate
    such temporary glyph IDs, gxvalid must stack all undefined glyph IDs
    which  can occur in  the output  of the  previous State  Machine and
    search  them in  the  `ClassTable' structure  of  the current  State
    Machine.  It is too complex to  list all possible glyph IDs from the
    StateTable, especially from a ligature substitution table.

  3-2. Validation of relationship between multiple layout features
  ----------------------------------------------------------------

    gxvalid does  not validate the relationship  between multiple layout
    features at all.

    If  multiple layout  features  are defined  in  TrueType GX  tables,
    possible  interactions,  overrides,  and  conflicts  between  layout
    features are implicitly  given in the font too.   For example, there
    are several predefined spacing control features:

      - Text Spacing          (Proportional/Monospace/Half-width/Normal)
      - Number Spacing        (Monospaced-numbers/Proportional-numbers)
      - Kana Spacing          (Full-width/Proportional)
      - Ideographic Spacing   (Full-width/Proportional)
      - CJK Roman Spacing     (Half-width/Proportional/Default-roman
                               /Full-width-roman/Proportional)

    If all  layout features are  independently managed, we  can activate
    inconsistent  typographic rules  like  `Text Spacing=Monospace'  and
    `Ideographic Spacing=Proportional' at the same time.

    The combinations  of layout features  is managed by a  32bit integer
    (one bit each for selector  setting), so we can define relationships
    between  up  to 32  features,  theoretically.   But  if one  feature
    setting  affects  another   feature  setting,  we  need  typographic
    priority  rules to  validate the  relationship.   Unfortunately, the
    TrueType GX format specification does not give such information even
    for predefined features.

4. Permissive error handling of broken GX tables
------------------------------------------------

  When  Apple's font  rendering system  finds an  inconsistency,  like a
  specification  violation or  an  unspecified value  in  a TrueType  GX
  table, it does not always  return error.  In most cases, the rendering
  engine silently  ignores such wrong  values or even whole  tables.  In
  fact, MacOS is shipped with  fonts including broken GX/AAT tables, but
  no harmful  effects due to  `officially broken' fonts are  observed by
  end-users.

  gxvalid  is designed  to continue  the validation  process as  long as
  possible.  When gxvalid find wrong  values, gxvalid warns it at least,
  and takes  a fallback procedure  if possible.  The  fallback procedure
  depends on the debug level.

  We used the following three tools to investigate Apple's error handling.

    - FontValidator  (for MacOS 8.5 - 9.2)  resource fork font
    - ftxvalidator   (for MacOS X 10.1 -)   dfont or naked-sfnt
    - ftxdumperfuser (for MacOS X 10.1 -)   dfont or naked-sfnt

  However, all tests were done on a PowerPC based Macintosh; at present,
  we have not checked those tools on a m68k-based Macintosh.

  In total, we checked 183 fonts  bundled to MacOS 9.1, MacOS 9.2, MacOS
  10.0, MacOS X  10.1, MSIE for MacOS, and  AppleWorks 6.0.  These fonts
  are distributed  officially, but many broken GX/AAT  tables were found
  by Apple's font tools.  In the following, we list typical violation of
  the GX specification, in fonts officially distributed with those Apple
  systems.

  4-1. broken BinSrchHeader (19/183)
  ----------------------------------

    `BinSrchHeader' is  a header of a  data array for  m68k platforms to
    access memory efficiently.  Although  there are only two independent
    parameters  for real  (`unitSize' and  `nUnits'),  BinSrchHeader has
    three additional parameters which  can be calculated from `unitSize'
    and  `nUnits',  for  fast  setup.   Apple  font  tools  ignore  them
    silently, so gxvalid warns if it finds and inconsistency, and always
    continues  validation.    The  additional  parameters   are  ignored
    regardless of the consistency.

      19  fonts include  such  inconsistencies; all  breaks  are in  the
      BinSrchHeader structure of the `kern' table.

  4-2. too-short LookupTable (5/183)
  ----------------------------------

    LookupTable format 0  is a simple array to get a  value from a given
    GID (glyph  ID); the index of  this array is a  GID too.  Therefore,
    the length  of the array is expected  to be same as  the maximum GID
    value defined  in the `maxp' table,  but there are  some fonts whose
    LookupTable format 0 is too  short to cover all GIDs.  FontValidator
    ignores  this error silently,  ftxvalidator and  ftxdumperfuser both
    warn and continue.  Similar problems are found in format 3 subtables
    of `kern'.  gxvalid  warns always and abort if  the validation level
    is set to FT_VALIDATE_PARANOID.

      5 fonts include too-short kern format 0 subtables.
      1 font includes too-short kern format 3 subtable.

  4-3. broken LookupTable format 2 (1/183)
  ----------------------------------------

    LookupTable  format  2,  subformat  4  covers the  GID  space  by  a
    collection  of  segments which  are  specified  by `firstGlyph'  and
    `lastGlyph'.   Some  fonts  store  `firstGlyph' and  `lastGlyph'  in
    reverse order,  so the segment specification is  broken.  Apple font
    tools ignore this error silently;  a broken segment is ignored as if
    it  did not  exist.   gxvalid  warns and  normalize  the segment  at
    FT_VALIDATE_DEFAULT, or ignore  the segment at FT_VALIDATE_TIGHT, or
    abort at FT_VALIDATE_PARANOID.

      1 font includes broken LookupTable format 2, in the `just' table.

    *) It seems  that all fonts manufactured by  ITC for AppleWorks have
       this error.

  4-4. bad bracketing in glyph property (14/183)
  ----------------------------------------------

    GX/AAT defines a  `bracketing' property of the glyphs  in the `prop'
    table,  to control layout  features of  strings enclosed  inside and
    outside  of   brackets.   Some  fonts   give  inappropriate  bracket
    properties  to glyphs.   Apple  font tools  warn  about this  error;
    gxvalid warns too and aborts at FT_VALIDATE_PARANOID.

      14 fonts include wrong bracket properties.

  4-5. invalid feature number (117/183)
  -------------------------------------

    The GX/AAT extension can  include 255 different layout features, but
    popular      layout      features      are      predefined      (see
    http://developer.apple.com/fonts/Registry/index.html).   Some  fonts
    include feature  numbers which are incompatible  with the predefined
    feature registry.

    In our survey, there are 140 fonts including `feat' table.

    a) 67 fonts use a feature number which should not be used.
    b) 117 fonts set the wrong feature range (nSetting).  This is mostly
       found in the `mort' and `morx' tables.

    Apple  font tools give  no warning,  although they  cannot recognize
    what  the feature  is.   At FT_VALIDATE_DEFAULT,  gxvalid warns  but
    continues in both cases (a, b).  At FT_VALIDATE_TIGHT, gxvalid warns
    and aborts for (a), but continues for (b).  At FT_VALIDATE_PARANOID,
    gxvalid warns and aborts in both cases (a, b).

  4-6. invalid prop version (10/183)
  ----------------------------------

    As most TrueType GX tables, the `prop' table must start with a 32bit
    version identifier: 0x00010000,  0x00020000 or 0x00030000.  But some
    fonts  store nonsense binary  data instead.   When Apple  font tools
    find them, they abort the processing immediately, and the data which
    follows is unhandled.  gxvalid does the same.

      10 fonts include broken `prop' version.

    All  of these  fonts are  classic  TrueType fonts  for the  Japanese
    script, manufactured by Apple.

  4-7. unknown resource name (2/183)
  ------------------------------------

    NOTE: THIS IS NOT A TRUETYPE GX ERROR.

    If  a TrueType  font is  stored  in the  resource fork  or in  dfont
    format, the data must be tagged as `sfnt' in the resource fork index
    to invoke TrueType font handler for the data.  But the TrueType font
    data  in   `Keyboard.dfont'  is  tagged   as  `kbd',  and   that  in
    `LastResort.dfont' is tagged as  `lst'.  Apple font tools can detect
    that the data is in  TrueType format and successfully validate them.
    Maybe  this is possible  because they  are known  to be  dfont.  The
    current  implementation  of the  resource  fork  driver of  FreeType
    cannot do that, thus gxvalid cannot validate them.

      2 fonts use an unknown tag for the TrueType font resource.

5. `kern' table issues
----------------------

  In common terminology of TrueType, `kern' is classified as a basic and
  platform-independent table.  But there are Apple extensions of `kern',
  and  there is  an  extension which  requires  a GX  state machine  for
  contextual kerning.   Therefore, gxvalid includes  a special validator
  for  `kern' tables.   Unfortunately, there  is no  exact  algorithm to
  check Apple's extension, so  gxvalid includes a heuristic algorithm to
  find  the proper validation  routines for  all possible  data formats,
  including    the   data    format   for    Microsoft.     By   calling
  classic_kern_validate() instead of gxv_validate(), you can specify the
  `kern' format  explicitly.  However, current  FreeType2 uses Microsoft
  `kern' format  only, others  are ignored (and  should be handled  in a
  library one level higher than FreeType).

  5-1. History
  ------------

    The original  16bit version of `kern'  was designed by  Apple in the
    pre-GX  era, and  it was  also approved  by  Microsoft.  Afterwards,
    Apple designed a  new 32bit version of the  `kern' table.  According
    to  the documentation, the  difference between  the 16bit  and 32bit
    version is only the size of  variables in the `kern' header.  In the
    following,  we call  the original  16bit version  as  `classic', and
    32bit version as `new'.

  5-2. Versions and dialects which should be differentiated
  ---------------------------------------------------------

    The `kern' table  consists of a table header  and several subtables.
    The version number  which identifies a `classic' or  a `new' version
    is  explicitly   written  in  the   table  header,  but   there  are
    undocumented  differences between  Microsoft's and  Apple's formats.
    It is  called a `dialect' in  the following.  There  are three cases
    which  should  be  handled:   the  new  Apple-dialect,  the  classic
    Apple-dialect,  and the classic  Microsoft-dialect.  An  analysis of
    the formats and the auto detection algorithm of gxvalid is described
    in the following.

    5-2-1. Version detection: classic and new kern
    ----------------------------------------------

      According  to Apple  TrueType  specification, there  are only  two
      differences between the classic and the new:

        - The `kern' table header starts with the version number.
          The classic version starts with 0x0000 (16bit),
          the new version starts with 0x00010000 (32bit).

        - In the  `kern' table header,  the number of  subtables follows
          the version number.
          In the classic version, it is stored as a 16bit value.
          In the new version, it is stored as a 32bit value.

      From Apple font tool's output (DumpKERN is also tested in addition
      to  the  three  Apple  font  tools in  above),  there  is  another
      undocumented difference.  In the  new version, the subtable header
      includes a 16bit variable  named `tupleIndex' which does not exist
      in the classic version.

      The new version  can store all subtable formats (0,  1, 2, and 3),
      but the Apple TrueType specification does not mention the subtable
      formats available in the classic version.

    5-2-2. Available subtable formats in classic version
    ----------------------------------------------------

      Although the  Apple TrueType  specification recommends to  use the
      classic version in  the case if the font is  designed for both the
      Apple and Microsoft platforms,  it does not document the available
      subtable formats in the classic version.

      According  to the Microsoft  TrueType specification,  the subtable
      format  assured for  Windows  and OS/2  support  is only  subtable
      format  0.  The  Microsoft TrueType  specification  also describes
      subtable format  2, but does  not mention which  platforms support
      it.  Aubtable formats 1, 3,  and higher are documented as reserved
      for future use.  Therefore, the classic version can store subtable
      formats 0 and 2, at least.  `ttfdump.exe', a font tool provided by
      Microsoft,  ignores the  subtable format  written in  the subtable
      header, and parses the table as if all subtables are in format 0.

      `kern'  subtable format  1  uses  a StateTable,  so  it cannot  be
      utilized without a GX  State Machine.  Therefore, it is reasonable
      to assume  that format 1 (and  3) were introduced  after Apple had
      introduced GX and moved to the new 32bit version.

    5-2-3. Apple and Microsoft dialects
    -----------------------------------

      The  `kern' subtable  has  a 16bit  `coverage'  field to  describe
      kerning attributes, but bit interpretations by Apple and Microsoft
      are different:  For example, Apple  uses bits 0-7 to  identify the
      subtable, while Microsoft uses bits 8-15.

      In  addition, due  to the  output of  DumpKERN  and FontValidator,
      Apple's bit interpretations of coverage in classic and new version
      are  incompatible also.   In  summary, there  are three  dialects:
      classic Apple  dialect, classic  Microsoft dialect, and  new Apple
      dialect.  The classic Microsoft  dialect and the new Apple dialect
      are documented  by each vendors' TrueType  font specification, but
      the documentation for classic Apple dialect is not available.

      For example,  in the  new Apple dialect,  bit 15 is  documented as
      `set to  1 if  the kerning  is vertical'.  On  the other  hand, in
      classic Microsoft dialect, bit 1 is documented as `set to 1 if the
      kerning  is  horizontal'.   From   the  outputs  of  DumpKERN  and
      FontValidator, classic  Apple dialect recognizes  15 as `set  to 1
      when  the kerning  is horizontal'.   From the  results  of similar
      experiments, classic Apple dialect  seems to be the Endian reverse
      of the classic Microsoft dialect.

      As a  conclusion it must be  noted that no font  tool can identify
      classic Apple dialect or classic Microsoft dialect automatically.

    5-2-4. gxvalid auto dialect detection algorithm
    -----------------------------------------------

      The first 16  bits of the `kern' table are  enough to identify the
      version:

        - if  the first  16  bits are  0x0000,  the `kern'  table is  in
          classic Apple dialect or classic Microsoft dialect
        - if the first 16 bits are  0x0001, and next 16 bits are 0x0000,
          the kern table is in new Apple dialect.

      If the `kern'  table is a classic one,  the 16bit `coverage' field
      is checked next.   Firstly, the coverage bits are  decoded for the
      classic Apple dialect using the following bit masks (this is based
      on DumpKERN output):

        0x8000: 1=horizontal, 0=vertical
        0x4000: not used
        0x2000: 1=cross-stream, 0=normal
        0x1FF0: reserved
        0x000F: subtable format

      If  any  of  reserved  bits  are  set  or  the  subtable  bits  is
      interpreted as format 1 or 3, we take it as `impossible in classic
      Apple dialect' and retry, using the classic Microsoft dialect.

        The most popular coverage in new Apple-dialect:         0x8000,
        The most popular coverage in classic Apple-dialect:     0x0000,
        The most popular coverage in classic Microsoft dialect: 0x0001.

  5-3. Tested fonts
  -----------------

    We checked  59 fonts  bundled with MacOS  and 38 fonts  bundled with
    Windows, where all font include a `kern' table.

      - fonts bundled with MacOS
        * new Apple dialect
          format 0: 18
          format 2:  1
          format 3:  1
        * classic Apple dialect
          format 0: 14
        * classic Microsoft dialect
          format 0: 15

      - fonts bundled with Windows
        * classic Microsoft dialect
          format 0: 38

    It looks strange that classic Microsoft-dialect fonts are bundled to
    MacOS: they come from MSIE for MacOS, except of MarkerFelt.dfont.

  ACKNOWLEDGEMENT
  ---------------

  Some parts of gxvalid are  derived from both the `gxlayout' module and
  the `otvalid'  module.  Development of  gxlayout was supported  by the
  Information-technology Promotion Agency(IPA), Japan.

  The detailed analysis of undefined  glyph ID utilization in `mort' and
  `morx' tables is provided by George Williams.

------------------------------------------------------------------------

Copyright 2004, 2005, 2007 by
suzuki toshiya, Masatake YAMATO, Red hat K.K.,
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.

--- end of README ---


code:
/***************************************************************************/
/*                                                                         */
/*  gxvalid.c                                                              */
/*                                                                         */
/*    FreeType validator for TrueTypeGX/AAT tables (body only).            */
/*                                                                         */
/*  Copyright 2005 by suzuki toshiya, Masatake YAMATO, Red Hat K.K.,       */
/*  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.                                        */
/*                                                                         */
/***************************************************************************/

#define FT_MAKE_OPTION_SINGLE_OBJECT

#include <ft2build.h>

#include "gxvfeat.c"
#include "gxvcommn.c"
#include "gxvbsln.c"
#include "gxvtrak.c"
#include "gxvjust.c"
#include "gxvmort.c"
#include "gxvmort0.c"
#include "gxvmort1.c"
#include "gxvmort2.c"
#include "gxvmort4.c"
#include "gxvmort5.c"
#include "gxvmorx.c"
#include "gxvmorx0.c"
#include "gxvmorx1.c"
#include "gxvmorx2.c"
#include "gxvmorx4.c"
#include "gxvmorx5.c"
#include "gxvkern.c"
#include "gxvopbd.c"
#include "gxvprop.c"
#include "gxvlcar.c"
#include "gxvmod.c"

/* END */


code:
/***************************************************************************/
/*                                                                         */
/*  gxvalid.h                                                              */
/*                                                                         */
/*    TrueTyeeGX/AAT table validation (specification only).                */
/*                                                                         */
/*  Copyright 2005 by suzuki toshiya, Masatake YAMATO, Red Hat K.K.,       */
/*  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.                                        */
/*                                                                         */
/***************************************************************************/

/***************************************************************************/
/*                                                                         */
/* gxvalid is derived from both gxlayout module and otvalid module.        */
/* Development of gxlayout is supported by the Information-technology      */
/* Promotion Agency(IPA), Japan.                                           */
/*                                                                         */
/***************************************************************************/

#ifndef __GXVALID_H__
#define __GXVALID_H__

#include <ft2build.h>
#include FT_FREETYPE_H

#include "gxverror.h"          /* must come before FT_INTERNAL_VALIDATE_H */

#include FT_INTERNAL_VALIDATE_H
#include FT_INTERNAL_STREAM_H

FT_BEGIN_HEADER

  FT_LOCAL( void )
  gxv_feat_validate( FT_Bytes      table,
                     FT_Face       face,
                     FT_Validator  valid );

  FT_LOCAL( void )
  gxv_bsln_validate( FT_Bytes      table,
                     FT_Face       face,
                     FT_Validator  valid );

  FT_LOCAL( void )
  gxv_trak_validate( FT_Bytes      table,
                     FT_Face       face,
                     FT_Validator  valid );

  FT_LOCAL( void )
  gxv_just_validate( FT_Bytes      table,
                     FT_Face       face,
                     FT_Validator  valid );

  FT_LOCAL( void )
  gxv_mort_validate( FT_Bytes      table,
                     FT_Face       face,
                     FT_Validator  valid );

  FT_LOCAL( void )
  gxv_morx_validate( FT_Bytes      table,
                     FT_Face       face,
                     FT_Validator  valid );

  FT_LOCAL( void )
  gxv_kern_validate( FT_Bytes      table,
                     FT_Face       face,
                     FT_Validator  valid );

  FT_LOCAL( void )
  gxv_kern_validate_classic( FT_Bytes      table,
                             FT_Face       face,
                             FT_Int        dialect_flags,
                             FT_Validator  valid );

  FT_LOCAL( void )
  gxv_opbd_validate( FT_Bytes      table,
                     FT_Face       face,
                     FT_Validator  valid );

  FT_LOCAL( void )
  gxv_prop_validate( FT_Bytes      table,
                     FT_Face       face,
                     FT_Validator  valid );

  FT_LOCAL( void )
  gxv_lcar_validate( FT_Bytes      table,
                     FT_Face       face,
                     FT_Validator  valid );

FT_END_HEADER

#endif /* __GXVALID_H__ */

/* END */


code:
/***************************************************************************/
/*                                                                         */
/*  gxvbsln.c                                                              */
/*                                                                         */
/*    TrueTypeGX/AAT bsln table validation (body).                         */
/*                                                                         */
/*  Copyright 2004, 2005 by suzuki toshiya, Masatake YAMATO, Red Hat K.K., */
/*  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.                                        */
/*                                                                         */
/***************************************************************************/

/***************************************************************************/
/*                                                                         */
/* gxvalid is derived from both gxlayout module and otvalid module.        */
/* Development of gxlayout is supported by the Information-technology      */
/* Promotion Agency(IPA), Japan.                                           */
/*                                                                         */
/***************************************************************************/

#include "gxvalid.h"
#include "gxvcommn.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_gxvbsln

  /*************************************************************************/
  /*************************************************************************/
  /*****                                                               *****/
  /*****                      Data and Types                           *****/
  /*****                                                               *****/
  /*************************************************************************/
  /*************************************************************************/

#define GXV_BSLN_VALUE_COUNT  32
#define GXV_BSLN_VALUE_EMPTY  0xFFFFU

  typedef struct  GXV_bsln_DataRec_
  {
    FT_Bytes   ctlPoints_p;
    FT_UShort  defaultBaseline;

  } GXV_bsln_DataRec, *GXV_bsln_Data;

#define GXV_BSLN_DATA( field )  GXV_TABLE_DATA( bsln, field )

  /*************************************************************************/
  /*************************************************************************/
  /*****                                                               *****/
  /*****                      UTILITY FUNCTIONS                        *****/
  /*****                                                               *****/
  /*************************************************************************/
  /*************************************************************************/

  static void
  gxv_bsln_LookupValue_validate( FT_UShort            glyph,
                                 GXV_LookupValueCPtr  value_p,
                                 GXV_Validator        valid )
  {
    FT_UShort   v = value_p->u;
    FT_UShort*  ctlPoints;

    FT_UNUSED( glyph );

    GXV_NAME_ENTER( "lookup value" );

    if ( v >= GXV_BSLN_VALUE_COUNT )
      FT_INVALID_DATA;

    ctlPoints = (FT_UShort*)GXV_BSLN_DATA( ctlPoints_p );
    if ( ctlPoints && ctlPoints[v] == GXV_BSLN_VALUE_EMPTY )
      FT_INVALID_DATA;

    GXV_EXIT;
  }

  /*
    +===============+ --------+
    | lookup header |         |
    +===============+         |
    | BinSrchHeader |         |
    +===============+         |
    | lastGlyph[0]  |         |
    +---------------+         |
    | firstGlyph[0] |         |    head of lookup table
    +---------------+         |             +
    | offset[0]     |    ->   |          offset            [byte]
    +===============+         |             +
    | lastGlyph[1]  |         | (glyphID - firstGlyph) * 2 [byte]
    +---------------+         |
    | firstGlyph[1] |         |
    +---------------+         |
    | offset[1]     |         |
    +===============+         |
                              |
    ...                       |
                              |
    16bit value array         |
    +===============+         |
    |     value     | <-------+
    ...
  */

  static GXV_LookupValueDesc
  gxv_bsln_LookupFmt4_transit( FT_UShort            relative_gindex,
                               GXV_LookupValueCPtr  base_value_p,
                               FT_Bytes             lookuptbl_limit,
                               GXV_Validator        valid )
  {
    FT_Bytes             p;
    FT_Bytes             limit;
    FT_UShort            offset;
    GXV_LookupValueDesc  value;

    /* XXX: check range ? */
    offset = (FT_UShort)( base_value_p->u +
                          ( relative_gindex * sizeof ( FT_UShort ) ) );

    p     = valid->lookuptbl_head + offset;
    limit = lookuptbl_limit;
    GXV_LIMIT_CHECK( 2 );

    value.u = FT_NEXT_USHORT( p );

    return value;
  }

  static void
  gxv_bsln_parts_fmt0_validate( FT_Bytes       tables,
                                FT_Bytes       limit,
                                GXV_Validator  valid )
  {
    FT_Bytes  p = tables;

    GXV_NAME_ENTER( "parts format 0" );

    /* deltas */
    GXV_LIMIT_CHECK( 2 * GXV_BSLN_VALUE_COUNT );

    valid->table_data = NULL;      /* No ctlPoints here. */

    GXV_EXIT;
  }

  static void
  gxv_bsln_parts_fmt1_validate( FT_Bytes       tables,
                                FT_Bytes       limit,
                                GXV_Validator  valid )
  {
    FT_Bytes  p = tables;

    GXV_NAME_ENTER( "parts format 1" );

    /* deltas */
    gxv_bsln_parts_fmt0_validate( p, limit, valid );

    /* mappingData */
    valid->lookupval_sign   = GXV_LOOKUPVALUE_UNSIGNED;
    valid->lookupval_func   = gxv_bsln_LookupValue_validate;
    valid->lookupfmt4_trans = gxv_bsln_LookupFmt4_transit;
    gxv_LookupTable_validate( p + 2 * GXV_BSLN_VALUE_COUNT,
                              limit,
                              valid );

    GXV_EXIT;
  }

  static void
  gxv_bsln_parts_fmt2_validate( FT_Bytes       tables,
                                FT_Bytes       limit,
                                GXV_Validator  valid )
  {
    FT_Bytes   p = tables;

    FT_UShort  stdGlyph;
    FT_UShort  ctlPoint;
    FT_Int     i;

    FT_UShort  defaultBaseline = GXV_BSLN_DATA( defaultBaseline );

    GXV_NAME_ENTER( "parts format 2" );

    GXV_LIMIT_CHECK( 2 + ( 2 * GXV_BSLN_VALUE_COUNT ) );

    /* stdGlyph */
    stdGlyph = FT_NEXT_USHORT( p );
    GXV_TRACE(( " (stdGlyph = %u)\n", stdGlyph ));

    gxv_glyphid_validate( stdGlyph, valid );

    /* Record the position of ctlPoints */
    GXV_BSLN_DATA( ctlPoints_p ) = p;

    /* ctlPoints */
    for ( i = 0; i < GXV_BSLN_VALUE_COUNT; i++ )
    {
      ctlPoint = FT_NEXT_USHORT( p );
      if ( ctlPoint == GXV_BSLN_VALUE_EMPTY )
      {
        if ( i == defaultBaseline )
          FT_INVALID_DATA;
      }
      else
        gxv_ctlPoint_validate( stdGlyph, (FT_Short)ctlPoint, valid );
    }

    GXV_EXIT;
  }

  static void
  gxv_bsln_parts_fmt3_validate( FT_Bytes       tables,
                                FT_Bytes       limit,
                                GXV_Validator  valid)
  {
    FT_Bytes  p = tables;

    GXV_NAME_ENTER( "parts format 3" );

    /* stdGlyph + ctlPoints */
    gxv_bsln_parts_fmt2_validate( p, limit, valid );

    /* mappingData */
    valid->lookupval_sign   = GXV_LOOKUPVALUE_UNSIGNED;
    valid->lookupval_func   = gxv_bsln_LookupValue_validate;
    valid->lookupfmt4_trans = gxv_bsln_LookupFmt4_transit;
    gxv_LookupTable_validate( p + ( 2 + 2 * GXV_BSLN_VALUE_COUNT ),
                              limit,
                              valid );

    GXV_EXIT;
  }

  /*************************************************************************/
  /*************************************************************************/
  /*****                                                               *****/
  /*****                         bsln TABLE                            *****/
  /*****                                                               *****/
  /*************************************************************************/
  /*************************************************************************/

  FT_LOCAL_DEF( void )
  gxv_bsln_validate( FT_Bytes      table,
                     FT_Face       face,
                     FT_Validator  ftvalid )
  {
    GXV_ValidatorRec  validrec;
    GXV_Validator     valid = &validrec;

    GXV_bsln_DataRec  bslnrec;
    GXV_bsln_Data     bsln = &bslnrec;

    FT_Bytes  p     = table;
    FT_Bytes  limit = 0;

    FT_ULong   version;
    FT_UShort  format;
    FT_UShort  defaultBaseline;

    GXV_Validate_Func  fmt_funcs_table [] =
    {
      gxv_bsln_parts_fmt0_validate,
      gxv_bsln_parts_fmt1_validate,
      gxv_bsln_parts_fmt2_validate,
      gxv_bsln_parts_fmt3_validate,
    };

    valid->root       = ftvalid;
    valid->table_data = bsln;
    valid->face       = face;

    FT_TRACE3(( "validating `bsln' table\n" ));
    GXV_INIT;

    GXV_LIMIT_CHECK( 4 + 2 + 2 );
    version         = FT_NEXT_ULONG( p );
    format          = FT_NEXT_USHORT( p );
    defaultBaseline = FT_NEXT_USHORT( p );

    /* only version 1.0 is defined (1996) */
    if ( version != 0x00010000UL )
      FT_INVALID_FORMAT;

    /* only format 1, 2, 3 are defined (1996) */
    GXV_TRACE(( " (format = %d)\n", format ));
    if ( format > 3 )
      FT_INVALID_FORMAT;

    if ( defaultBaseline > 31 )
      FT_INVALID_FORMAT;

    bsln->defaultBaseline = defaultBaseline;

    fmt_funcs_table[format]( p, limit, valid );

    FT_TRACE4(( "\n" ));
  }

/* arch-tag: ebe81143-fdaa-4c68-a4d1-b57227daa3bc
   (do not change this comment) */

/* END */