ftsystem.c
上传用户:yisoukefu
上传日期:2020-08-09
资源大小:39506k
文件大小:13k
源码类别:

其他游戏

开发平台:

Visual C++

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /*  ftsystem.c                                                             */
  4. /*                                                                         */
  5. /*    ANSI-specific FreeType low-level system interface (body).            */
  6. /*                                                                         */
  7. /*  Copyright 1996-2001, 2002, 2006 by                                     */
  8. /*  David Turner, Robert Wilhelm, and Werner Lemberg.                      */
  9. /*                                                                         */
  10. /*  This file is part of the FreeType project, and may only be used,       */
  11. /*  modified, and distributed under the terms of the FreeType project      */
  12. /*  license, LICENSE.TXT.  By continuing to use, modify, or distribute     */
  13. /*  this file you indicate that you have read the license and              */
  14. /*  understand and accept it fully.                                        */
  15. /*                                                                         */
  16. /***************************************************************************/
  17.   /*************************************************************************/
  18.   /*                                                                       */
  19.   /* This file contains the default interface used by FreeType to access   */
  20.   /* low-level, i.e. memory management, i/o access as well as thread       */
  21.   /* synchronisation.  It can be replaced by user-specific routines if     */
  22.   /* necessary.                                                            */
  23.   /*                                                                       */
  24.   /*************************************************************************/
  25. #include <ft2build.h>
  26. #include FT_CONFIG_CONFIG_H
  27. #include FT_INTERNAL_DEBUG_H
  28. #include FT_INTERNAL_STREAM_H
  29. #include FT_SYSTEM_H
  30. #include FT_ERRORS_H
  31. #include FT_TYPES_H
  32.   /*************************************************************************/
  33.   /*                                                                       */
  34.   /*                       MEMORY MANAGEMENT INTERFACE                     */
  35.   /*                                                                       */
  36.   /*************************************************************************/
  37.   /*************************************************************************/
  38.   /*                                                                       */
  39.   /* It is not necessary to do any error checking for the                  */
  40.   /* allocation-related functions.  This will be done by the higher level  */
  41.   /* routines like ft_mem_alloc() or ft_mem_realloc().                     */
  42.   /*                                                                       */
  43.   /*************************************************************************/
  44.   /*************************************************************************/
  45.   /*                                                                       */
  46.   /* <Function>                                                            */
  47.   /*    ft_alloc                                                           */
  48.   /*                                                                       */
  49.   /* <Description>                                                         */
  50.   /*    The memory allocation function.                                    */
  51.   /*                                                                       */
  52.   /* <Input>                                                               */
  53.   /*    memory :: A pointer to the memory object.                          */
  54.   /*                                                                       */
  55.   /*    size   :: The requested size in bytes.                             */
  56.   /*                                                                       */
  57.   /* <Return>                                                              */
  58.   /*    The address of newly allocated block.                              */
  59.   /*                                                                       */
  60.   FT_CALLBACK_DEF( void* )
  61.   ft_alloc( FT_Memory  memory,
  62.             long       size )
  63.   {
  64.     FT_UNUSED( memory );
  65.     return ft_smalloc( size );
  66.   }
  67.   /*************************************************************************/
  68.   /*                                                                       */
  69.   /* <Function>                                                            */
  70.   /*    ft_realloc                                                         */
  71.   /*                                                                       */
  72.   /* <Description>                                                         */
  73.   /*    The memory reallocation function.                                  */
  74.   /*                                                                       */
  75.   /* <Input>                                                               */
  76.   /*    memory   :: A pointer to the memory object.                        */
  77.   /*                                                                       */
  78.   /*    cur_size :: The current size of the allocated memory block.        */
  79.   /*                                                                       */
  80.   /*    new_size :: The newly requested size in bytes.                     */
  81.   /*                                                                       */
  82.   /*    block    :: The current address of the block in memory.            */
  83.   /*                                                                       */
  84.   /* <Return>                                                              */
  85.   /*    The address of the reallocated memory block.                       */
  86.   /*                                                                       */
  87.   FT_CALLBACK_DEF( void* )
  88.   ft_realloc( FT_Memory  memory,
  89.               long       cur_size,
  90.               long       new_size,
  91.               void*      block )
  92.   {
  93.     FT_UNUSED( memory );
  94.     FT_UNUSED( cur_size );
  95.     return ft_srealloc( block, new_size );
  96.   }
  97.   /*************************************************************************/
  98.   /*                                                                       */
  99.   /* <Function>                                                            */
  100.   /*    ft_free                                                            */
  101.   /*                                                                       */
  102.   /* <Description>                                                         */
  103.   /*    The memory release function.                                       */
  104.   /*                                                                       */
  105.   /* <Input>                                                               */
  106.   /*    memory  :: A pointer to the memory object.                         */
  107.   /*                                                                       */
  108.   /*    block   :: The address of block in memory to be freed.             */
  109.   /*                                                                       */
  110.   FT_CALLBACK_DEF( void )
  111.   ft_free( FT_Memory  memory,
  112.            void*      block )
  113.   {
  114.     FT_UNUSED( memory );
  115.     ft_sfree( block );
  116.   }
  117.   /*************************************************************************/
  118.   /*                                                                       */
  119.   /*                     RESOURCE MANAGEMENT INTERFACE                     */
  120.   /*                                                                       */
  121.   /*************************************************************************/
  122.   /*************************************************************************/
  123.   /*                                                                       */
  124.   /* The macro FT_COMPONENT is used in trace mode.  It is an implicit      */
  125.   /* parameter of the FT_TRACE() and FT_ERROR() macros, used to print/log  */
  126.   /* messages during execution.                                            */
  127.   /*                                                                       */
  128. #undef  FT_COMPONENT
  129. #define FT_COMPONENT  trace_io
  130.   /* We use the macro STREAM_FILE for convenience to extract the       */
  131.   /* system-specific stream handle from a given FreeType stream object */
  132. #define STREAM_FILE( stream )  ( (FT_FILE*)stream->descriptor.pointer )
  133.   /*************************************************************************/
  134.   /*                                                                       */
  135.   /* <Function>                                                            */
  136.   /*    ft_ansi_stream_close                                               */
  137.   /*                                                                       */
  138.   /* <Description>                                                         */
  139.   /*    The function to close a stream.                                    */
  140.   /*                                                                       */
  141.   /* <Input>                                                               */
  142.   /*    stream :: A pointer to the stream object.                          */
  143.   /*                                                                       */
  144.   FT_CALLBACK_DEF( void )
  145.   ft_ansi_stream_close( FT_Stream  stream )
  146.   {
  147.     ft_fclose( STREAM_FILE( stream ) );
  148.     stream->descriptor.pointer = NULL;
  149.     stream->size               = 0;
  150.     stream->base               = 0;
  151.   }
  152.   /*************************************************************************/
  153.   /*                                                                       */
  154.   /* <Function>                                                            */
  155.   /*    ft_ansi_stream_io                                                  */
  156.   /*                                                                       */
  157.   /* <Description>                                                         */
  158.   /*    The function to open a stream.                                     */
  159.   /*                                                                       */
  160.   /* <Input>                                                               */
  161.   /*    stream :: A pointer to the stream object.                          */
  162.   /*                                                                       */
  163.   /*    offset :: The position in the data stream to start reading.        */
  164.   /*                                                                       */
  165.   /*    buffer :: The address of buffer to store the read data.            */
  166.   /*                                                                       */
  167.   /*    count  :: The number of bytes to read from the stream.             */
  168.   /*                                                                       */
  169.   /* <Return>                                                              */
  170.   /*    The number of bytes actually read.                                 */
  171.   /*                                                                       */
  172.   FT_CALLBACK_DEF( unsigned long )
  173.   ft_ansi_stream_io( FT_Stream       stream,
  174.                      unsigned long   offset,
  175.                      unsigned char*  buffer,
  176.                      unsigned long   count )
  177.   {
  178.     FT_FILE*  file;
  179.     file = STREAM_FILE( stream );
  180.     ft_fseek( file, offset, SEEK_SET );
  181.     return (unsigned long)ft_fread( buffer, 1, count, file );
  182.   }
  183.   /* documentation is in ftstream.h */
  184.   FT_BASE_DEF( FT_Error )
  185.   FT_Stream_Open( FT_Stream    stream,
  186.                   const char*  filepathname )
  187.   {
  188.     FT_FILE*  file;
  189.     if ( !stream )
  190.       return FT_Err_Invalid_Stream_Handle;
  191.     file = ft_fopen( filepathname, "rb" );
  192.     if ( !file )
  193.     {
  194.       FT_ERROR(( "FT_Stream_Open:" ));
  195.       FT_ERROR(( " could not open `%s'n", filepathname ));
  196.       return FT_Err_Cannot_Open_Resource;
  197.     }
  198.     ft_fseek( file, 0, SEEK_END );
  199.     stream->size = ft_ftell( file );
  200.     ft_fseek( file, 0, SEEK_SET );
  201.     stream->descriptor.pointer = file;
  202.     stream->pathname.pointer   = (char*)filepathname;
  203.     stream->pos                = 0;
  204.     stream->read  = ft_ansi_stream_io;
  205.     stream->close = ft_ansi_stream_close;
  206.     FT_TRACE1(( "FT_Stream_Open:" ));
  207.     FT_TRACE1(( " opened `%s' (%d bytes) successfullyn",
  208.                 filepathname, stream->size ));
  209.     return FT_Err_Ok;
  210.   }
  211. #ifdef FT_DEBUG_MEMORY
  212.   extern FT_Int
  213.   ft_mem_debug_init( FT_Memory  memory );
  214.   extern void
  215.   ft_mem_debug_done( FT_Memory  memory );
  216. #endif
  217.   /* documentation is in ftobjs.h */
  218.   FT_BASE_DEF( FT_Memory )
  219.   FT_New_Memory( void )
  220.   {
  221.     FT_Memory  memory;
  222.     memory = (FT_Memory)ft_smalloc( sizeof ( *memory ) );
  223.     if ( memory )
  224.     {
  225.       memory->user    = 0;
  226.       memory->alloc   = ft_alloc;
  227.       memory->realloc = ft_realloc;
  228.       memory->free    = ft_free;
  229. #ifdef FT_DEBUG_MEMORY
  230.       ft_mem_debug_init( memory );
  231. #endif
  232.     }
  233.     return memory;
  234.   }
  235.   /* documentation is in ftobjs.h */
  236.   FT_BASE_DEF( void )
  237.   FT_Done_Memory( FT_Memory  memory )
  238.   {
  239. #ifdef FT_DEBUG_MEMORY
  240.     ft_mem_debug_done( memory );
  241. #endif
  242.     memory->free( memory, memory );
  243.   }
  244. /* END */