📄 ttfile.c
字号:
} CUR_Frame.cursor = CUR_Frame.address; return error; }/******************************************************************* * * Function : TT_Forget_Frame * * Description : Releases a cached frame after reading. * * Input : None * * Output : SUCCESS on success. FAILURE on error. * ******************************************************************/ EXPORT_FUNC TT_Error TT_Forget_Frame( FRAME_ARG ) { if ( CUR_Frame.address == NULL ) return TT_Err_Nested_Frame_Access; if ( CUR_Frame.size > FRAME_CACHE_SIZE ) FREE( CUR_Frame.address ); ZERO_Frame( CUR_Frame ); return TT_Err_Ok; }#else /* TT_CONFIG_OPTION_THREAD_SAFE */ /*******************************************************************/ /*******************************************************************/ /*******************************************************************/ /******** ********/ /******** R E E N T R A N T I M P L E M E N T A T I O N ********/ /******** ********/ /*******************************************************************/ /*******************************************************************/ /*******************************************************************//* a simple macro to access the file component's data */#define files ( *((TFile_Component*)engine.file_component) )#define CUR_Stream STREAM2REC( stream ) /* re-entrant macros */#define CUR_Frame (*frame)#define STREAM_VARS stream,#define STREAM_VAR stream/******************************************************************* * * Function : TTFile_Init * * Description : Initializes the File component. * ******************************************************************/ LOCAL_FUNC TT_Error TTFile_Init( PEngine_Instance engine ) { return TT_Err_Ok; }/******************************************************************* * * Function : TTFile_Done * * Description : Finalizes the File component. * ******************************************************************/ LOCAL_FUNC TT_Error TTFile_Done( PEngine_Instance engine ) { return TT_Err_Ok; }/******************************************************************* * * Function : TT_Use_Stream * * Description : Duplicates a stream for a new usage. * * Input : input_stream source stream to duplicate * copy address of target duplicate stream * * Output : error code. * The target stream is set to NULL in case of failure. * ******************************************************************/ EXPORT_FUNC TT_Error TT_Use_Stream( TT_Stream input_stream, TT_Stream* copy ) { PStream_Rec rec = STREAM2REC( input_stream ); return TT_Open_Stream( rec->name, copy ); }/******************************************************************* * * Function : TT_Done_Stream * * Description : Releases a given stream. * * Input : stream target stream * * Output : * ******************************************************************/ EXPORT_FUNC TT_Error TT_Done_Stream( TT_Stream* stream ) { return TT_Close_Stream( stream ); }/******************************************************************* * * Function : TT_Access_Frame * * Description : Notifies the component that we're going to read * 'size' bytes from the current file position. * This function should load/cache/map these bytes * so that they will be addressed by the GET_xxx * functions easily. * * Input : size number of bytes to access. * * Output : SUCCESS on success. FAILURE on error. * * Notes: The function fails if the byte range is not within the * the file, or if there is not enough memory to cache * the bytes properly (which usually means that `size' is * too big in both cases). * ******************************************************************/ EXPORT_FUNC TT_Error TT_Access_Frame( STREAM_ARGS FRAME_ARGS Long size ) { TT_Error error; if ( CUR_Frame.address != NULL ) return TT_Err_Nested_Frame_Access; if ( ALLOC( CUR_Frame.address, size ) ) return error; CUR_Frame.size = size; error = TT_Read_File( STREAM_VARS (void*)CUR_Frame.address, size ); if ( error ) { FREE( CUR_Frame.address ); CUR_Frame.size = 0; } CUR_Frame.cursor = CUR_Frame.address; return error; }/******************************************************************* * * Function : TT_Check_And_Access_Frame * * Description : Notifies the component that we're going to read * `size' bytes from the current file position. * This function should load/cache/map these bytes * so that they will be addressed by the GET_xxx * functions easily. * * Input : size number of bytes to access. * * Output : SUCCESS on success. FAILURE on error. * * Notes: The function truncates `size' if the byte range is not * within the file. * * It will fail if there is not enough memory to cache * the bytes properly (which usually means that `size' is * too big). * * It will fail if you make two consecutive calls * to TT_Access_Frame(), without a TT_Forget_Frame() between * them. * * The only difference with TT_Access_Frame() is that we * check that the frame is within the current file. We * otherwise truncate it. * ******************************************************************/ EXPORT_FUNC TT_Error TT_Check_And_Access_Frame( STREAM_ARGS FRAME_ARGS Long size ) { TT_Error error; Long readBytes; if ( CUR_Frame.address != NULL ) return TT_Err_Nested_Frame_Access; if ( ALLOC( CUR_Frame.address, size ) ) return error; CUR_Frame.size = size; readBytes = CUR_Stream->size - TT_File_Pos( STREAM_VAR ); if ( size > readBytes ) size = readBytes; error = TT_Read_File( STREAM_VARS (void*)CUR_Frame.address, size ); if ( error ) { FREE( CUR_Frame.address ); CUR_Frame.size = 0; } CUR_Frame.cursor = CUR_Frame.address; return error; }/******************************************************************* * * Function : TT_Forget_Frame * * Description : Releases a cached frame after reading. * * Input : None * * Output : SUCCESS on success. FAILURE on error. * ******************************************************************/ EXPORT_FUNC TT_Error TT_Forget_Frame( FRAME_ARG ) { if ( CUR_Frame.address == NULL ) return TT_Err_Nested_Frame_Access; FREE( CUR_Frame.address ); ZERO_Frame( CUR_Frame ); return TT_Err_Ok; }#endif /* TT_CONFIG_OPTION_THREAD_SAFE */ /*******************************************************************/ /*******************************************************************/ /*******************************************************************/ /*********** ***********/ /*********** C O M M O N I M P L E M E N T A T I O N ***********/ /*********** ***********/ /*******************************************************************/ /*******************************************************************/ /*******************************************************************//******************************************************************* * * Function : Stream_Activate * * Description : activates a stream, this will either: * - open a new file handle if the stream is closed * - move the stream to the head of the linked list * * Input : stream the stream to activate * * Output : error condition. * * Note : This function is also called with fresh new streams * created by TT_Open_Stream(). They have their 'size' * field set to -1. * ******************************************************************/ static TT_Error Stream_Activate( PStream_Rec stream ) { if ( !stream->opened ) { if ( (stream->file = fopen( (TT_Text*)stream->name, "rb" )) == 0 ) return TT_Err_Could_Not_ReOpen_File; stream->opened = TRUE; /* A newly created stream has a size field of -1 */ if ( stream->size < 0 ) { fseek( stream->file, 0, SEEK_END ); stream->size = ftell( stream->file ); fseek( stream->file, 0, SEEK_SET ); } /* Reset cursor in file */ if ( stream->position ) { if ( fseek( stream->file, stream->position, SEEK_SET ) != 0 ) { /* error during seek */ fclose( stream->file ); stream->opened = FALSE; return TT_Err_Could_Not_ReSeek_File; } } } return TT_Err_Ok; }/******************************************************************* * * Function : Stream_DeActivate * * Description : deactivates a stream, this will : * - close its file handle if it was opened * - remove it from the opened list if necessary * * Input : stream the stream to deactivate * * Output : Error condition * * Note : the function is called whenever a stream is deleted * (_not_ when a stream handle's is closed due to an * activation). However, the stream record isn't * destroyed by it.. * ******************************************************************/ static TT_Error Stream_Deactivate( PStream_Rec stream ) { if ( stream->opened ) { /* Save its current position within the file */ stream->position = ftell( stream->file ); fclose( stream->file ); stream->file = 0; stream->opened = FALSE; } return TT_Err_Ok; }/******************************************************************* * * Function : TT_Stream_Size * * Description : Returns the length of a given stream, even if it * is flushed. * * Input : stream the stream * * Output : Length of stream in bytes. * ******************************************************************/ EXPORT_FUNC Long TT_Stream_Size( TT_Stream stream ) { PStream_Rec rec = STREAM2REC( stream ); if ( rec ) return rec->size; else return 0; /* invalid stream - return 0 */ }/******************************************************************* * * Function : TT_Open_Stream * * Description : Opens the font file and saves the total file size. * * Input : error address of stream's error variable * (re-entrant build only)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -