/*******************************************************************
 *
 *  ttfile.h                                                     1.3
 *
 *    File I/O Component (specification).
 *
 *  Copyright 1996, 1997 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.
 *
 *  Changes between 1.3 and 1.2:
 *
 *  - all functions report error values now
 *
 *  - the stream semantics have also changed
 *
 *  Changes between 1.2 and 1.1:
 *
 *  - added macros to support re-entrant builds
 *
 *  - added the TT_Duplicate_File function to duplicate streams
 *    (re-entrant builds only)
 *
 ******************************************************************/

/* #define TT_CONFIG_REENTRANT */

#ifndef TTFILE_H
#define TTFILE_H

#include "freetype.h"
#include "ttengine.h"
#include "tterror.h"

#ifdef __cplusplus
  extern "C" {
#endif

  /* Initialize file component */  
  TT_Error  TTFile_Init( PEngine_Instance  engine );

  /* Done with file component */
  TT_Error  TTFile_Done( PEngine_Instance  engine );


  /**********************************************************************/
  /*                                                                    */
  /*  Stream functions.                                                 */
  /*                                                                    */
  /**********************************************************************/

  /* Open a file and return a stream handle for it.           */
  /* Should only be used for a new face object's main stream. */

  TT_Error  TT_Open_Stream( const char*  name,
                                   TT_Stream*   stream );


  /* Closes, then discards, a stream when it's no longer needed.   */
  /* Should only be used for a stream opend with TT_Open_Stream(). */

  TT_Error  TT_Close_Stream( TT_Stream*  stream );


  /* Informs the component that we're going to use the file   */
  /* opened in 'org_stream', and report errors to the 'error' */
  /* variable.                                                */

  /* in non re-entrant builds, 'org_stream' is simply copied   */
  /* to 'stream'. Otherwise, the latter is a duplicate handle  */
  /* for the file opened with 'org_stream'                     */

  TT_Error  TT_Use_Stream( TT_Stream   org_stream,
                           TT_Stream*  stream );

  /* Informs the component that we don't need to perform file */
  /* operations on the stream 'stream' anymore.  This must be */
  /* used with streams "opened" with TT_Use_Stream() only!    */

  /* in re-entrant builds, this will really discard the stream */

  TT_Error  TT_Done_Stream( TT_Stream*  stream );

  /* Closes the stream's file handle to release system resources */
  /* The function TT_Use_Stream automatically re-activates a     */
  /* flushed stream when it uses one                             */

  TT_Error  TT_Flush_Stream( TT_Stream*  stream );

/* The macros STREAM_ARGS and STREAM_ARG let us build a thread-safe */
/* or re-entrant implementation depending on a single configuration */
/*define.                                                           */

#ifdef TT_CONFIG_REENTRANT

#define STREAM_ARGS   TT_Stream  stream,
#define STREAM_ARG    TT_Stream  stream

#else

#define STREAM_ARGS   /* void */
#define STREAM_ARG

#endif /* TT_CONFIG_REENTRANT */


  /****************************************************************/
  /*                                                              */
  /*  File Functions.                                             */
  /*                                                              */
  /*  The following functions perform file operations on the      */
  /*  currently 'used' stream.  In thread-safe builds, only one   */
  /*  stream can be used at a time.  Synchronisation is performed */
  /*  through the Use_Stream()/Done_Stream() functions.           */
  /*                                                              */
  /****************************************************************/

  /* Read 'count' bytes from file into 'buffer' */

  TT_Error  TT_Read_File( STREAM_ARGS void*  buffer, 
                                      long   count );


  /* Seek file cursor to a given position */

  TT_Error  TT_Seek_File( STREAM_ARGS long  position );


  /* Skip the next 'distance' bytes in file */

  TT_Error  TT_Skip_File( STREAM_ARGS long  distance );


  /* Read the 'count' bytes at 'position' into 'buffer' */

  TT_Error  TT_Read_At_File( STREAM_ARGS long   position,
                                         void*  buffer,
                                         long   count );


  /* Return current file position */

  Long  TT_File_Pos( STREAM_ARG );

  /* Return length of a given stream, even if it is flushed */

  Long  TT_Stream_Size( TT_Stream  stream );


  /********************************************************************/
  /*                                                                  */
  /*  Frame operations.                                               */
  /*                                                                  */
  /*  For a comprehensive explanation of frames, please refer to the  */
  /*  documentation files.                                            */
  /*                                                                  */
  /********************************************************************/

  /* Frame type declaration.*/

  struct  _TFileFrame
  {
    unsigned char*  address;  /* frame buffer                     */
    unsigned char*  cursor;   /* current cursor position in frame */
    int             size;     /* frame size                       */
  };

  typedef struct _TFileFrame  TFileFrame;

  extern const TFileFrame  TT_Null_FileFrame;


/* The macro ZERO_Frame is used to define and init a frame.      */
/* It is important to have a default frame of { NULL, NULL, 0 }  */
/* before a call to TT_Access_Frame().  Otherwise, the call will */
/* fail with a TT_Err_Nested_Frame_Accesses error.               */

#define ZERO_Frame( frame )     \
      {                         \
        (frame).address = NULL; \
        (frame).cursor  = NULL; \
        (frame).size    = 0;    \
      }


/* The macros FRAME_ARGS and FRAME_ARG let us build a thread-safe   */
/* or re-entrant implementation depending on a single configuration */
/* define                                                           */

#ifdef TT_CONFIG_REENTRANT

  #define FRAME_ARGS   TFileFrame* frame,
  #define FRAME_ARG    TFileFrame* frame

#else
  #define FRAME_ARGS  /* void */
  #define FRAME_ARG   /* void */

#endif /* TT_CONFIG_REENTRANT */


  /* Access the next 'size' bytes from current position. */
  /* Fails if all bytes cannot be read/accessed.         */

  TT_Error  TT_Access_Frame( STREAM_ARGS FRAME_ARGS int  size );


  /* Access the bytes located in the next 'size' bytes of the file. */
  /* Doesn't fail if less than 'size' bytes are accessible (like    */
  /* at the end of the file).                                       */

  TT_Error  TT_Check_And_Access_Frame( STREAM_ARGS FRAME_ARGS int  size );


  /* Forget frame */

  TT_Error  TT_Forget_Frame( FRAME_ARG );


  /* primitive routines for data accessing */

  unsigned char   TT_Get_Byte  ( FRAME_ARG );
  char            TT_Get_Char  ( FRAME_ARG );
  short           TT_Get_Short ( FRAME_ARG );
  unsigned short  TT_Get_UShort( FRAME_ARG );
  long            TT_Get_Long  ( FRAME_ARG );
  unsigned long   TT_Get_ULong ( FRAME_ARG );


#ifdef __cplusplus
  }
#endif

#endif /* TTFILE_H */


/* END */
