📄 ftmac.c
字号:
{ unsigned char* s; size_t j = suffixes[i] - 1; if ( j < string_count && ( s = names[j] ) != NULL ) { size_t s_len = (size_t)s[0]; if ( s_len != 0 && ps_name_len + s_len < sizeof ( ps_name ) ) { ft_memcpy( ps_name + ps_name_len, s + 1, s_len ); ps_name_len += s_len; ps_name[ps_name_len] = 0; } } } } } create_lwfn_name( ps_name, lwfn_file_name ); } } static short count_faces( Handle fond ) { short sfnt_id, have_sfnt, have_lwfn = 0; Str255 lwfn_file_name; FSSpec lwfn_spec; HLock( fond ); parse_fond( *fond, &have_sfnt, &sfnt_id, lwfn_file_name, 0 ); HUnlock( fond ); if ( lwfn_file_name[0] ) { if ( make_lwfn_spec( fond, lwfn_file_name, &lwfn_spec ) == FT_Err_Ok ) have_lwfn = 1; /* yeah, we got one! */ else have_lwfn = 0; /* no LWFN file found */ } if ( have_lwfn && ( !have_sfnt || PREFER_LWFN ) ) return 1; else return count_faces_sfnt( *fond ); } /* Read Type 1 data from the POST resources inside the LWFN file, return a PFB buffer. This is somewhat convoluted because the FT2 PFB parser wants the ASCII header as one chunk, and the LWFN chunks are often not organized that way, so we'll glue chunks of the same type together. */ static FT_Error read_lwfn( FT_Memory memory, short res_ref, FT_Byte** pfb_data, FT_ULong* size ) { FT_Error error = FT_Err_Ok; short res_id; unsigned char *buffer, *p, *size_p = NULL; FT_ULong total_size = 0; FT_ULong post_size, pfb_chunk_size; Handle post_data; char code, last_code; UseResFile( res_ref ); /* First pass: load all POST resources, and determine the size of */ /* the output buffer. */ res_id = 501; last_code = -1; for (;;) { post_data = Get1Resource( 'POST', res_id++ ); if ( post_data == NULL ) break; /* we're done */ code = (*post_data)[0]; if ( code != last_code ) { if ( code == 5 ) total_size += 2; /* just the end code */ else total_size += 6; /* code + 4 bytes chunk length */ } total_size += GetHandleSize( post_data ) - 2; last_code = code; } if ( FT_ALLOC( buffer, (FT_Long)total_size ) ) goto Error; /* Second pass: append all POST data to the buffer, add PFB fields. */ /* Glue all consecutive chunks of the same type together. */ p = buffer; res_id = 501; last_code = -1; pfb_chunk_size = 0; for (;;) { post_data = Get1Resource( 'POST', res_id++ ); if ( post_data == NULL ) break; /* we're done */ post_size = (FT_ULong)GetHandleSize( post_data ) - 2; code = (*post_data)[0]; if ( code != last_code ) { if ( last_code != -1 ) { /* we're done adding a chunk, fill in the size field */ if ( size_p != NULL ) { *size_p++ = (FT_Byte)( pfb_chunk_size & 0xFF ); *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 8 ) & 0xFF ); *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 16 ) & 0xFF ); *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 24 ) & 0xFF ); } pfb_chunk_size = 0; } *p++ = 0x80; if ( code == 5 ) *p++ = 0x03; /* the end */ else if ( code == 2 ) *p++ = 0x02; /* binary segment */ else *p++ = 0x01; /* ASCII segment */ if ( code != 5 ) { size_p = p; /* save for later */ p += 4; /* make space for size field */ } } ft_memcpy( p, *post_data + 2, post_size ); pfb_chunk_size += post_size; p += post_size; last_code = code; } *pfb_data = buffer; *size = total_size; Error: CloseResFile( res_ref ); return error; } /* Finalizer for a memory stream; gets called by FT_Done_Face(). It frees the memory it uses. */ static void memory_stream_close( FT_Stream stream ) { FT_Memory memory = stream->memory; FT_FREE( stream->base ); stream->size = 0; stream->base = 0; stream->close = 0; } /* Create a new memory stream from a buffer and a size. */ static FT_Error new_memory_stream( FT_Library library, FT_Byte* base, FT_ULong size, FT_Stream_CloseFunc close, FT_Stream *astream ) { FT_Error error; FT_Memory memory; FT_Stream stream; if ( !library ) return FT_Err_Invalid_Library_Handle; if ( !base ) return FT_Err_Invalid_Argument; *astream = 0; memory = library->memory; if ( FT_NEW( stream ) ) goto Exit; FT_Stream_OpenMemory( stream, base, size ); stream->close = close; *astream = stream; Exit: return error; } /* Create a new FT_Face given a buffer and a driver name. */ static FT_Error open_face_from_buffer( FT_Library library, FT_Byte* base, FT_ULong size, FT_Long face_index, char* driver_name, FT_Face *aface ) { FT_Open_Args args; FT_Error error; FT_Stream stream; FT_Memory memory = library->memory; error = new_memory_stream( library, base, size, memory_stream_close, &stream ); if ( error ) { FT_FREE( base ); return error; } args.flags = FT_OPEN_STREAM; args.stream = stream; if ( driver_name ) { args.flags = args.flags | FT_OPEN_DRIVER; args.driver = FT_Get_Module( library, driver_name ); } /* At this point, face_index has served its purpose; */ /* whoever calls this function has already used it to */ /* locate the correct font data. We should not propagate */ /* this index to FT_Open_Face() (unless it is negative). */ if ( face_index > 0 ) face_index = 0; error = FT_Open_Face( library, &args, face_index, aface ); if ( error == FT_Err_Ok ) (*aface)->face_flags &= ~FT_FACE_FLAG_EXTERNAL_STREAM; return error; } static FT_Error OpenFileAsResource( const FSSpec* spec, short *p_res_ref ) { FT_Error error;#if !TARGET_API_MAC_OS8 FSRef hostContainerRef; error = FSpMakeFSRef( spec, &hostContainerRef ); if ( error == noErr ) error = FSOpenResourceFile( &hostContainerRef, 0, NULL, fsRdPerm, p_res_ref ); /* If the above fails, then it is probably not a resource file */ /* However, it has been reported that FSOpenResourceFile() sometimes */ /* fails on some old resource-fork files, which FSpOpenResFile() can */ /* open. So, just try again with FSpOpenResFile() and see what */ /* happens :-) */ if ( error != noErr )#endif /* !TARGET_API_MAC_OS8 */ { *p_res_ref = FSpOpenResFile( spec, fsRdPerm ); error = ResError(); } return error ? FT_Err_Cannot_Open_Resource : FT_Err_Ok; } /* Create a new FT_Face from a file spec to an LWFN file. */ static FT_Error FT_New_Face_From_LWFN( FT_Library library, const FSSpec* lwfn_spec, FT_Long face_index, FT_Face *aface ) { FT_Byte* pfb_data; FT_ULong pfb_size; FT_Error error; short res_ref; error = OpenFileAsResource( lwfn_spec, &res_ref ); if ( error ) return error; error = read_lwfn( library->memory, res_ref, &pfb_data, &pfb_size ); if ( error ) return error; return open_face_from_buffer( library, pfb_data, pfb_size, face_index, "type1", aface ); } /* Create a new FT_Face from an SFNT resource, specified by res ID. */ static FT_Error FT_New_Face_From_SFNT( FT_Library library, short sfnt_id, FT_Long face_index, FT_Face *aface ) { Handle sfnt = NULL; FT_Byte* sfnt_data; size_t sfnt_size; FT_Error error = 0; FT_Memory memory = library->memory; int is_cff; sfnt = GetResource( 'sfnt', sfnt_id ); if ( ResError() ) return FT_Err_Invalid_Handle; sfnt_size = (FT_ULong)GetHandleSize( sfnt ); if ( FT_ALLOC( sfnt_data, (FT_Long)sfnt_size ) ) { ReleaseResource( sfnt ); return error; } HLock( sfnt ); ft_memcpy( sfnt_data, *sfnt, sfnt_size ); HUnlock( sfnt ); ReleaseResource( sfnt ); is_cff = sfnt_size > 4 && sfnt_data[0] == 'O' && sfnt_data[1] == 'T' && sfnt_data[2] == 'T' &&
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -