----------------------------------------------------------------------------- Load font file from disk using a ResFile object. -----------------------------------------------------------------------------
| 333 | // Load font file from disk using a ResFile object. |
| 334 | //----------------------------------------------------------------------------- |
| 335 | FT_Face Tr2FontManager::LoadFromDisk( FTC_FaceID id ) |
| 336 | { |
| 337 | CCP_STATS_ZONE( __FUNCTION__ ); |
| 338 | |
| 339 | if( !m_ftLib ) |
| 340 | { |
| 341 | CCP_LOGERR( "Tr2FontManager::LoadFromDisk: FreeType has not been initialized" ); |
| 342 | return NULL; |
| 343 | } |
| 344 | |
| 345 | uintptr_t index = reinterpret_cast<uintptr_t>( id ); |
| 346 | const char* fontPath = m_reverseFaceMap[index].c_str(); |
| 347 | |
| 348 | // Create a new ResFile instance and add to the |
| 349 | // opened ResFiles vector |
| 350 | Be::Clsid resFileClsid( "blue", "ResFile" ); |
| 351 | IResFilePtr fp( resFileClsid ); |
| 352 | s_fpVector.Insert( -1, fp ); |
| 353 | // Open the file |
| 354 | // TODO: Add error handling to close and remove |
| 355 | // object from the opened ResFile vector |
| 356 | if( !fp->Open( fontPath, true ) ) |
| 357 | { |
| 358 | CCP_LOGERR( "Tr2FontManager::LoadFromDisk: File '%s' does not exist", fontPath ); |
| 359 | return NULL; |
| 360 | } |
| 361 | ssize_t fileLength = fp->GetSize(); |
| 362 | |
| 363 | // Create our stream struct. |
| 364 | // This looks like a memory leak since we are |
| 365 | // creating a stream using new and not deleting |
| 366 | // at the end of the function. |
| 367 | // What we are doing is ensuring that the stream |
| 368 | // stays alive as long as freetype may need it. |
| 369 | // Freetype will then call FileClose with a pointer |
| 370 | // to the stream when it is done, making it safe for |
| 371 | // us to delete it there. |
| 372 | FT_Stream stream = CCP_NEW( "Tr2FontManager/LoadFromDisk/stream" ) FT_StreamRec; |
| 373 | if( !stream ) |
| 374 | { |
| 375 | return NULL; |
| 376 | } |
| 377 | memset( stream, 0, sizeof( *stream ) ); |
| 378 | |
| 379 | stream->descriptor.pointer = (void*)fp.p; |
| 380 | |
| 381 | stream->read = &FileRead; |
| 382 | stream->close = &FileClose; |
| 383 | stream->size = (unsigned int)fileLength; |
| 384 | |
| 385 | FT_Open_Args openargs; |
| 386 | openargs.flags = FT_OPEN_STREAM; |
| 387 | openargs.stream = stream; |
| 388 | FT_Face face; |
| 389 | FT_Error e = FT_Open_Face( m_ftLib, &openargs, 0, &face ); |
| 390 | if( e ) |
| 391 | { |
| 392 | CCP_LOGERR( "Tr2FontManager::LoadFromDisk: FT_Open_Face failed for '%s'", fontPath ); |
no test coverage detected