1 /***************************************************************************/
5 /* Mac FOND support. Written by just@letterror.com. */
6 /* Heavily Fixed by mpsuzuki, George Williams and Sean McBride */
8 /* Copyright 1996-2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 by */
9 /* Just van Rossum, David Turner, Robert Wilhelm, and Werner Lemberg. */
11 /* This file is part of the FreeType project, and may only be used, */
12 /* modified, and distributed under the terms of the FreeType project */
13 /* license, LICENSE.TXT. By continuing to use, modify, or distribute */
14 /* this file you indicate that you have read the license and */
15 /* understand and accept it fully. */
17 /***************************************************************************/
23 Mac suitcase files can (and often do!) contain multiple fonts. To
24 support this I use the face_index argument of FT_(Open|New)_Face()
25 functions, and pretend the suitcase file is a collection.
27 Warning: fbit and NFNT bitmap resources are not supported yet. In old
28 sfnt fonts, bitmap glyph data for each size is stored in each `NFNT'
29 resources instead of the `bdat' table in the sfnt resource. Therefore,
30 face->num_fixed_sizes is set to 0, because bitmap data in `NFNT'
31 resource is unavailable at present.
33 The Mac FOND support works roughly like this:
35 - Check whether the offered stream points to a Mac suitcase file. This
36 is done by checking the file type: it has to be 'FFIL' or 'tfil'. The
37 stream that gets passed to our init_face() routine is a stdio stream,
38 which isn't usable for us, since the FOND resources live in the
39 resource fork. So we just grab the stream->pathname field.
41 - Read the FOND resource into memory, then check whether there is a
42 TrueType font and/or(!) a Type 1 font available.
44 - If there is a Type 1 font available (as a separate `LWFN' file), read
45 its data into memory, massage it slightly so it becomes PFB data, wrap
46 it into a memory stream, load the Type 1 driver and delegate the rest
47 of the work to it by calling FT_Open_Face(). (XXX TODO: after this
48 has been done, the kerning data from the FOND resource should be
49 appended to the face: On the Mac there are usually no AFM files
50 available. However, this is tricky since we need to map Mac char
51 codes to ps glyph names to glyph ID's...)
53 - If there is a TrueType font (an `sfnt' resource), read it into memory,
54 wrap it into a memory stream, load the TrueType driver and delegate
55 the rest of the work to it, by calling FT_Open_Face().
57 - Some suitcase fonts (notably Onyx) might point the `LWFN' file to
58 itself, even though it doesn't contains `POST' resources. To handle
59 this special case without opening the file an extra time, we just
60 ignore errors from the `LWFN' and fallback to the `sfnt' if both are
66 #include FT_FREETYPE_H
67 #include FT_TRUETYPE_TAGS_H
68 #include FT_INTERNAL_STREAM_H
71 #if defined( __GNUC__ ) || defined( __IBMC__ )
72 /* This is for Mac OS X. Without redefinition, OS_INLINE */
73 /* expands to `static inline' which doesn't survive the */
74 /* -ansi compilation flag of GCC. */
75 #if !HAVE_ANSI_OS_INLINE
77 #define OS_INLINE static __inline__
79 #include <CoreServices/CoreServices.h>
80 #include <ApplicationServices/ApplicationServices.h>
81 #include <sys/syslimits.h> /* PATH_MAX */
83 #include <Resources.h>
88 #include <TextUtils.h>
92 #define PATH_MAX 1024 /* same with Mac OS X's syslimits.h */
95 #if defined( __MWERKS__ ) && !TARGET_RT_MAC_MACHO
96 #include <FSp_fopen.h>
99 #define FT_DEPRECATED_ATTRIBUTE
103 /* undefine blocking-macros in ftmac.h */
104 #undef FT_GetFile_From_Mac_Name
105 #undef FT_GetFile_From_Mac_ATS_Name
106 #undef FT_New_Face_From_FOND
107 #undef FT_New_Face_From_FSSpec
108 #undef FT_New_Face_From_FSRef
111 /* FSSpec functions are deprecated since Mac OS X 10.4 */
113 #if TARGET_API_MAC_OS8 || TARGET_API_MAC_CARBON
114 #define HAVE_FSSPEC 1
116 #define HAVE_FSSPEC 0
120 /* most FSRef functions were introduced since Mac OS 9 */
122 #if TARGET_API_MAC_OSX
129 /* QuickDraw is deprecated since Mac OS X 10.4 */
130 #ifndef HAVE_QUICKDRAW_CARBON
131 #if TARGET_API_MAC_OS8 || TARGET_API_MAC_CARBON
132 #define HAVE_QUICKDRAW_CARBON 1
134 #define HAVE_QUICKDRAW_CARBON 0
138 /* AppleTypeService is available since Mac OS X */
140 #if TARGET_API_MAC_OSX
142 #ifndef kATSOptionFlagsUnRestrictedScope /* since Mac OS X 10.1 */
143 #define kATSOptionFlagsUnRestrictedScope kATSOptionFlagsDefault
150 /* `configure' checks the availability of `ResourceIndex' strictly */
151 /* and sets HAVE_TYPE_RESOURCE_INDEX to 1 or 0 always. If it is */
152 /* not set (e.g., a build without `configure'), the availability */
153 /* is guessed from the SDK version. */
154 #ifndef HAVE_TYPE_RESOURCE_INDEX
155 #if !defined( MAC_OS_X_VERSION_10_5 ) || \
156 ( MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5 )
157 #define HAVE_TYPE_RESOURCE_INDEX 0
159 #define HAVE_TYPE_RESOURCE_INDEX 1
161 #endif /* !HAVE_TYPE_RESOURCE_INDEX */
163 #if ( HAVE_TYPE_RESOURCE_INDEX == 0 )
164 typedef short ResourceIndex;
167 /* Set PREFER_LWFN to 1 if LWFN (Type 1) is preferred over
168 TrueType in case *both* are available (this is not common,
169 but it *is* possible). */
171 #define PREFER_LWFN 1
176 #if !HAVE_QUICKDRAW_CARBON /* QuickDraw is deprecated since Mac OS X 10.4 */
178 FT_EXPORT_DEF( FT_Error )
179 FT_GetFile_From_Mac_Name( const char* fontName,
181 FT_Long* face_index )
183 FT_UNUSED( fontName );
184 FT_UNUSED( pathSpec );
185 FT_UNUSED( face_index );
187 return FT_Err_Unimplemented_Feature;
192 FT_EXPORT_DEF( FT_Error )
193 FT_GetFile_From_Mac_Name( const char* fontName,
195 FT_Long* face_index )
197 OptionBits options = kFMUseGlobalScopeOption;
199 FMFontFamilyIterator famIter;
200 OSStatus status = FMCreateFontFamilyIterator( NULL, NULL,
204 FMFontFamily family = 0;
208 while ( status == 0 && !the_font )
210 status = FMGetNextFontFamily( &famIter, &family );
214 FMFontFamilyInstanceIterator instIter;
219 /* get the family name */
220 FMGetFontFamilyName( family, famNameStr );
221 CopyPascalStringToC( famNameStr, famName );
223 /* iterate through the styles */
224 FMCreateFontFamilyInstanceIterator( family, &instIter );
229 while ( stat2 == 0 && !the_font )
236 stat2 = FMGetNextFontFamilyInstance( &instIter, &font,
238 if ( stat2 == 0 && size == 0 )
243 /* build up a complete face name */
244 ft_strcpy( fullName, famName );
246 ft_strcat( fullName, " Bold" );
247 if ( style & italic )
248 ft_strcat( fullName, " Italic" );
250 /* compare with the name we are looking for */
251 if ( ft_strcmp( fullName, fontName ) == 0 )
261 FMDisposeFontFamilyInstanceIterator( &instIter );
265 FMDisposeFontFamilyIterator( &famIter );
269 FMGetFontContainer( the_font, pathSpec );
273 return FT_Err_Unknown_File_Format;
276 #endif /* HAVE_QUICKDRAW_CARBON */
281 /* Private function. */
282 /* The FSSpec type has been discouraged for a long time, */
283 /* unfortunately an FSRef replacement API for */
284 /* ATSFontGetFileSpecification() is only available in */
285 /* Mac OS X 10.5 and later. */
287 FT_ATSFontGetFileReference( ATSFontRef ats_font_id,
288 FSRef* ats_font_ref )
292 #if !defined( MAC_OS_X_VERSION_10_5 ) || \
293 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
297 err = ATSFontGetFileSpecification( ats_font_id, &spec );
299 err = FSpMakeFSRef( &spec, ats_font_ref );
301 err = ATSFontGetFileReference( ats_font_id, ats_font_ref );
309 FT_GetFileRef_From_Mac_ATS_Name( const char* fontName,
311 FT_Long* face_index )
313 CFStringRef cf_fontName;
314 ATSFontRef ats_font_id;
319 cf_fontName = CFStringCreateWithCString( NULL, fontName,
320 kCFStringEncodingMacRoman );
321 ats_font_id = ATSFontFindFromName( cf_fontName,
322 kATSOptionFlagsUnRestrictedScope );
323 CFRelease( cf_fontName );
325 if ( ats_font_id == 0 || ats_font_id == 0xFFFFFFFFUL )
326 return FT_Err_Unknown_File_Format;
328 if ( noErr != FT_ATSFontGetFileReference( ats_font_id, ats_font_ref ) )
329 return FT_Err_Unknown_File_Format;
331 /* face_index calculation by searching preceding fontIDs */
332 /* with same FSRef */
334 ATSFontRef id2 = ats_font_id - 1;
340 if ( noErr != FT_ATSFontGetFileReference( id2, &ref2 ) )
342 if ( noErr != FSCompareFSRefs( ats_font_ref, &ref2 ) )
347 *face_index = ats_font_id - ( id2 + 1 );
357 FT_EXPORT_DEF( FT_Error )
358 FT_GetFilePath_From_Mac_ATS_Name( const char* fontName,
361 FT_Long* face_index )
363 FT_UNUSED( fontName );
365 FT_UNUSED( maxPathSize );
366 FT_UNUSED( face_index );
368 return FT_Err_Unimplemented_Feature;
373 FT_EXPORT_DEF( FT_Error )
374 FT_GetFilePath_From_Mac_ATS_Name( const char* fontName,
377 FT_Long* face_index )
383 err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
384 if ( FT_Err_Ok != err )
387 if ( noErr != FSRefMakePath( &ref, path, maxPathSize ) )
388 return FT_Err_Unknown_File_Format;
393 #endif /* HAVE_ATS */
396 #if !HAVE_FSSPEC || !HAVE_ATS
398 FT_EXPORT_DEF( FT_Error )
399 FT_GetFile_From_Mac_ATS_Name( const char* fontName,
401 FT_Long* face_index )
403 FT_UNUSED( fontName );
404 FT_UNUSED( pathSpec );
405 FT_UNUSED( face_index );
407 return FT_Err_Unimplemented_Feature;
412 /* This function is deprecated because FSSpec is deprecated in Mac OS X. */
413 FT_EXPORT_DEF( FT_Error )
414 FT_GetFile_From_Mac_ATS_Name( const char* fontName,
416 FT_Long* face_index )
422 err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
423 if ( FT_Err_Ok != err )
426 if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone, NULL, NULL,
428 return FT_Err_Unknown_File_Format;
436 #if defined( __MWERKS__ ) && !TARGET_RT_MAC_MACHO
438 #define STREAM_FILE( stream ) ( (FT_FILE*)stream->descriptor.pointer )
441 FT_CALLBACK_DEF( void )
442 ft_FSp_stream_close( FT_Stream stream )
444 ft_fclose( STREAM_FILE( stream ) );
446 stream->descriptor.pointer = NULL;
452 FT_CALLBACK_DEF( unsigned long )
453 ft_FSp_stream_io( FT_Stream stream,
454 unsigned long offset,
455 unsigned char* buffer,
456 unsigned long count )
461 file = STREAM_FILE( stream );
463 ft_fseek( file, offset, SEEK_SET );
465 return (unsigned long)ft_fread( buffer, 1, count, file );
468 #endif /* __MWERKS__ && !TARGET_RT_MAC_MACHO */
471 #if HAVE_FSSPEC && !HAVE_FSREF
473 /* isDirectory is a dummy to synchronize API with FSPathMakeRef() */
475 FT_FSPathMakeSpec( const UInt8* pathname,
477 Boolean isDirectory )
484 FT_UNUSED( isDirectory );
487 p = q = (const char *)pathname;
493 int len = ft_strlen( p );
504 if ( 255 < ft_strlen( (char *)pathname ) )
506 while ( p < q && *q != ':' )
511 *(char *)nodeName = q - p;
512 else if ( ft_strlen( p ) < 256 )
513 *(char *)nodeName = ft_strlen( p );
515 return errFSNameTooLong;
517 ft_strncpy( (char *)nodeName + 1, (char *)p, *(char *)nodeName );
518 err = FSMakeFSSpec( vRefNum, dirID, nodeName, spec_p );
519 if ( err || '\0' == *q )
522 vRefNum = spec_p->vRefNum;
523 dirID = spec_p->parID;
531 FT_FSpMakePath( const FSSpec* spec_p,
536 FSSpec spec = *spec_p;
542 FT_MEM_SET( path, 0, maxPathSize );
545 int child_namelen = ft_strlen( (char *)path );
546 unsigned char node_namelen = spec.name[0];
547 unsigned char* node_name = spec.name + 1;
550 if ( node_namelen + child_namelen > maxPathSize )
551 return errFSNameTooLong;
553 FT_MEM_MOVE( path + node_namelen + 1, path, child_namelen );
554 FT_MEM_COPY( path, node_name, node_namelen );
555 if ( child_namelen > 0 )
556 path[node_namelen] = ':';
558 vRefNum = spec.vRefNum;
560 parDir_name[0] = '\0';
561 err = FSMakeFSSpec( vRefNum, dirID, parDir_name, &spec );
562 if ( noErr != err || dirID == spec.parID )
568 #endif /* HAVE_FSSPEC && !HAVE_FSREF */
572 FT_FSPathMakeRes( const UInt8* pathname,
582 if ( noErr != FSPathMakeRef( pathname, &ref, FALSE ) )
583 return FT_Err_Cannot_Open_Resource;
585 /* at present, no support for dfont format */
586 err = FSOpenResourceFile( &ref, 0, NULL, fsRdPerm, res );
590 /* fallback to original resource-fork font */
591 *res = FSOpenResFile( &ref, fsRdPerm );
600 if ( noErr != FT_FSPathMakeSpec( pathname, &spec, FALSE ) )
601 return FT_Err_Cannot_Open_Resource;
603 /* at present, no support for dfont format without FSRef */
604 /* (see above), try original resource-fork font */
605 *res = FSpOpenResFile( &spec, fsRdPerm );
608 #endif /* HAVE_FSREF */
614 /* Return the file type for given pathname */
616 get_file_type_from_path( const UInt8* pathname )
625 if ( noErr != FSPathMakeRef( pathname, &ref, FALSE ) )
628 if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoFinderInfo, &info,
632 return ((FInfo *)(info.finderInfo))->fdType;
640 if ( noErr != FT_FSPathMakeSpec( pathname, &spec, FALSE ) )
643 if ( noErr != FSpGetFInfo( &spec, &finfo ) )
648 #endif /* HAVE_FSREF */
653 /* Given a PostScript font name, create the Macintosh LWFN file name. */
655 create_lwfn_name( char* ps_name,
656 Str255 lwfn_file_name )
658 int max = 5, count = 0;
659 FT_Byte* p = lwfn_file_name;
660 FT_Byte* q = (FT_Byte*)ps_name;
663 lwfn_file_name[0] = 0;
667 if ( ft_isupper( *q ) )
673 if ( count < max && ( ft_isalnum( *q ) || *q == '_' ) )
685 count_faces_sfnt( char* fond_data )
687 /* The count is 1 greater than the value in the FOND. */
688 /* Isn't that cute? :-) */
690 return EndianS16_BtoN( *( (short*)( fond_data +
691 sizeof ( FamRec ) ) ) ) + 1;
696 count_faces_scalable( char* fond_data )
700 short i, face, face_all;
703 fond = (FamRec*)fond_data;
704 face_all = EndianS16_BtoN( *( (short *)( fond_data +
705 sizeof ( FamRec ) ) ) ) + 1;
706 assoc = (AsscEntry*)( fond_data + sizeof ( FamRec ) + 2 );
709 for ( i = 0; i < face_all; i++ )
711 if ( 0 == EndianS16_BtoN( assoc[i].fontSize ) )
718 /* Look inside the FOND data, answer whether there should be an SFNT
719 resource, and answer the name of a possible LWFN Type 1 file.
721 Thanks to Paul Miller (paulm@profoundeffects.com) for the fix
722 to load a face OTHER than the first one in the FOND!
726 parse_fond( char* fond_data,
729 Str255 lwfn_file_name,
733 AsscEntry* base_assoc;
739 lwfn_file_name[0] = 0;
741 fond = (FamRec*)fond_data;
742 assoc = (AsscEntry*)( fond_data + sizeof ( FamRec ) + 2 );
745 /* the maximum faces in a FOND is 48, size of StyleTable.indexes[] */
746 if ( 47 < face_index )
749 /* Let's do a little range checking before we get too excited here */
750 if ( face_index < count_faces_sfnt( fond_data ) )
752 assoc += face_index; /* add on the face_index! */
754 /* if the face at this index is not scalable,
755 fall back to the first one (old behavior) */
756 if ( EndianS16_BtoN( assoc->fontSize ) == 0 )
759 *sfnt_id = EndianS16_BtoN( assoc->fontID );
761 else if ( base_assoc->fontSize == 0 )
764 *sfnt_id = EndianS16_BtoN( base_assoc->fontID );
768 if ( EndianS32_BtoN( fond->ffStylOff ) )
770 unsigned char* p = (unsigned char*)fond_data;
772 unsigned short string_count;
774 unsigned char* names[64];
778 p += EndianS32_BtoN( fond->ffStylOff );
779 style = (StyleTable*)p;
780 p += sizeof ( StyleTable );
781 string_count = EndianS16_BtoN( *(short*)(p) );
782 p += sizeof ( short );
784 for ( i = 0; i < string_count && i < 64; i++ )
792 size_t ps_name_len = (size_t)names[0][0];
795 if ( ps_name_len != 0 )
797 ft_memcpy(ps_name, names[0] + 1, ps_name_len);
798 ps_name[ps_name_len] = 0;
800 if ( style->indexes[face_index] > 1 &&
801 style->indexes[face_index] <= FT_MIN( string_count, 64 ) )
803 unsigned char* suffixes = names[style->indexes[face_index] - 1];
806 for ( i = 1; i <= suffixes[0]; i++ )
809 size_t j = suffixes[i] - 1;
812 if ( j < string_count && ( s = names[j] ) != NULL )
814 size_t s_len = (size_t)s[0];
817 if ( s_len != 0 && ps_name_len + s_len < sizeof ( ps_name ) )
819 ft_memcpy( ps_name + ps_name_len, s + 1, s_len );
820 ps_name_len += s_len;
821 ps_name[ps_name_len] = 0;
828 create_lwfn_name( ps_name, lwfn_file_name );
834 lookup_lwfn_by_fond( const UInt8* path_fond,
835 ConstStr255Param base_lwfn,
846 /* Pathname for FSRef can be in various formats: HFS, HFS+, and POSIX. */
847 /* We should not extract parent directory by string manipulation. */
849 if ( noErr != FSPathMakeRef( path_fond, &ref, FALSE ) )
850 return FT_Err_Invalid_Argument;
852 if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone,
853 NULL, NULL, NULL, &par_ref ) )
854 return FT_Err_Invalid_Argument;
856 if ( noErr != FSRefMakePath( &par_ref, path_lwfn, path_size ) )
857 return FT_Err_Invalid_Argument;
859 if ( ft_strlen( (char *)path_lwfn ) + 1 + base_lwfn[0] > path_size )
860 return FT_Err_Invalid_Argument;
862 /* now we have absolute dirname in path_lwfn */
863 if ( path_lwfn[0] == '/' )
864 ft_strcat( (char *)path_lwfn, "/" );
866 ft_strcat( (char *)path_lwfn, ":" );
868 dirname_len = ft_strlen( (char *)path_lwfn );
869 ft_strcat( (char *)path_lwfn, (char *)base_lwfn + 1 );
870 path_lwfn[dirname_len + base_lwfn[0]] = '\0';
872 if ( noErr != FSPathMakeRef( path_lwfn, &ref, FALSE ) )
873 return FT_Err_Cannot_Open_Resource;
875 if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone,
876 NULL, NULL, NULL, NULL ) )
877 return FT_Err_Cannot_Open_Resource;
887 /* pathname for FSSpec is always HFS format */
888 if ( ft_strlen( (char *)path_fond ) > path_size )
889 return FT_Err_Invalid_Argument;
891 ft_strcpy( (char *)path_lwfn, (char *)path_fond );
893 i = ft_strlen( (char *)path_lwfn ) - 1;
894 while ( i > 0 && ':' != path_lwfn[i] )
897 if ( i + 1 + base_lwfn[0] > path_size )
898 return FT_Err_Invalid_Argument;
900 if ( ':' == path_lwfn[i] )
902 ft_strcpy( (char *)path_lwfn + i + 1, (char *)base_lwfn + 1 );
903 path_lwfn[i + 1 + base_lwfn[0]] = '\0';
907 ft_strcpy( (char *)path_lwfn, (char *)base_lwfn + 1 );
908 path_lwfn[base_lwfn[0]] = '\0';
911 if ( noErr != FT_FSPathMakeSpec( path_lwfn, &spec, FALSE ) )
912 return FT_Err_Cannot_Open_Resource;
916 #endif /* HAVE_FSREF */
922 count_faces( Handle fond,
923 const UInt8* pathname )
926 short have_sfnt, have_lwfn;
927 Str255 lwfn_file_name;
928 UInt8 buff[PATH_MAX];
933 have_sfnt = have_lwfn = 0;
936 parse_fond( *fond, &have_sfnt, &sfnt_id, lwfn_file_name, 0 );
938 if ( lwfn_file_name[0] )
940 err = lookup_lwfn_by_fond( pathname, lwfn_file_name,
941 buff, sizeof ( buff ) );
942 if ( FT_Err_Ok == err )
946 if ( have_lwfn && ( !have_sfnt || PREFER_LWFN ) )
949 num_faces = count_faces_scalable( *fond );
956 /* Read Type 1 data from the POST resources inside the LWFN file,
957 return a PFB buffer. This is somewhat convoluted because the FT2
958 PFB parser wants the ASCII header as one chunk, and the LWFN
959 chunks are often not organized that way, so we glue chunks
960 of the same type together. */
962 read_lwfn( FT_Memory memory,
967 FT_Error error = FT_Err_Ok;
969 unsigned char *buffer, *p, *size_p = NULL;
970 FT_ULong total_size = 0;
971 FT_ULong old_total_size = 0;
972 FT_ULong post_size, pfb_chunk_size;
974 char code, last_code;
979 /* First pass: load all POST resources, and determine the size of */
980 /* the output buffer. */
986 post_data = Get1Resource( TTAG_POST, res_id++ );
987 if ( post_data == NULL )
988 break; /* we are done */
990 code = (*post_data)[0];
992 if ( code != last_code )
995 total_size += 2; /* just the end code */
997 total_size += 6; /* code + 4 bytes chunk length */
1000 total_size += GetHandleSize( post_data ) - 2;
1003 /* detect integer overflows */
1004 if ( total_size < old_total_size )
1006 error = FT_Err_Array_Too_Large;
1010 old_total_size = total_size;
1013 if ( FT_ALLOC( buffer, (FT_Long)total_size ) )
1016 /* Second pass: append all POST data to the buffer, add PFB fields. */
1017 /* Glue all consecutive chunks of the same type together. */
1025 post_data = Get1Resource( TTAG_POST, res_id++ );
1026 if ( post_data == NULL )
1027 break; /* we are done */
1029 post_size = (FT_ULong)GetHandleSize( post_data ) - 2;
1030 code = (*post_data)[0];
1032 if ( code != last_code )
1034 if ( last_code != -1 )
1036 /* we are done adding a chunk, fill in the size field */
1037 if ( size_p != NULL )
1039 *size_p++ = (FT_Byte)( pfb_chunk_size & 0xFF );
1040 *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 8 ) & 0xFF );
1041 *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 16 ) & 0xFF );
1042 *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 24 ) & 0xFF );
1049 *p++ = 0x03; /* the end */
1050 else if ( code == 2 )
1051 *p++ = 0x02; /* binary segment */
1053 *p++ = 0x01; /* ASCII segment */
1057 size_p = p; /* save for later */
1058 p += 4; /* make space for size field */
1062 ft_memcpy( p, *post_data + 2, post_size );
1063 pfb_chunk_size += post_size;
1072 CloseResFile( res );
1077 /* Create a new FT_Face from a file spec to an LWFN file. */
1079 FT_New_Face_From_LWFN( FT_Library library,
1080 const UInt8* pathname,
1090 if ( noErr != FT_FSPathMakeRes( pathname, &res ) )
1091 return FT_Err_Cannot_Open_Resource;
1095 error = read_lwfn( library->memory, res, &pfb_data, &pfb_size );
1096 CloseResFile( res ); /* PFB is already loaded, useless anymore */
1100 return open_face_from_buffer( library,
1109 /* Create a new FT_Face from an SFNT resource, specified by res ID. */
1111 FT_New_Face_From_SFNT( FT_Library library,
1119 FT_Error error = FT_Err_Ok;
1120 FT_Memory memory = library->memory;
1121 int is_cff, is_sfnt_ps;
1124 sfnt = GetResource( TTAG_sfnt, sfnt_id );
1126 return FT_Err_Invalid_Handle;
1128 sfnt_size = (FT_ULong)GetHandleSize( sfnt );
1129 if ( FT_ALLOC( sfnt_data, (FT_Long)sfnt_size ) )
1131 ReleaseResource( sfnt );
1136 ft_memcpy( sfnt_data, *sfnt, sfnt_size );
1138 ReleaseResource( sfnt );
1140 is_cff = sfnt_size > 4 && !ft_memcmp( sfnt_data, "OTTO", 4 );
1141 is_sfnt_ps = sfnt_size > 4 && !ft_memcmp( sfnt_data, "typ1", 4 );
1148 if ( FT_NEW( stream ) )
1151 FT_Stream_OpenMemory( stream, sfnt_data, sfnt_size );
1152 if ( !open_face_PS_from_sfnt_stream( library,
1158 FT_Stream_Close( stream );
1160 FT_FREE( sfnt_data );
1167 error = open_face_from_buffer( library,
1171 is_cff ? "cff" : "truetype",
1178 /* Create a new FT_Face from a file spec to a suitcase file. */
1180 FT_New_Face_From_Suitcase( FT_Library library,
1181 const UInt8* pathname,
1185 FT_Error error = FT_Err_Cannot_Open_Resource;
1186 ResFileRefNum res_ref;
1187 ResourceIndex res_index;
1189 short num_faces_in_res, num_faces_in_fond;
1192 if ( noErr != FT_FSPathMakeRes( pathname, &res_ref ) )
1193 return FT_Err_Cannot_Open_Resource;
1195 UseResFile( res_ref );
1197 return FT_Err_Cannot_Open_Resource;
1199 num_faces_in_res = 0;
1200 for ( res_index = 1; ; ++res_index )
1202 fond = Get1IndResource( TTAG_FOND, res_index );
1206 num_faces_in_fond = count_faces( fond, pathname );
1207 num_faces_in_res += num_faces_in_fond;
1209 if ( 0 <= face_index && face_index < num_faces_in_fond && error )
1210 error = FT_New_Face_From_FOND( library, fond, face_index, aface );
1212 face_index -= num_faces_in_fond;
1215 CloseResFile( res_ref );
1216 if ( FT_Err_Ok == error && NULL != aface )
1217 (*aface)->num_faces = num_faces_in_res;
1222 /* documentation is in ftmac.h */
1224 FT_EXPORT_DEF( FT_Error )
1225 FT_New_Face_From_FOND( FT_Library library,
1230 short have_sfnt, have_lwfn = 0;
1231 ResID sfnt_id, fond_id;
1234 Str255 lwfn_file_name;
1235 UInt8 path_lwfn[PATH_MAX];
1237 FT_Error error = FT_Err_Ok;
1240 GetResInfo( fond, &fond_id, &fond_type, fond_name );
1241 if ( ResError() != noErr || fond_type != TTAG_FOND )
1242 return FT_Err_Invalid_File_Format;
1245 parse_fond( *fond, &have_sfnt, &sfnt_id, lwfn_file_name, face_index );
1248 if ( lwfn_file_name[0] )
1253 res = HomeResFile( fond );
1254 if ( noErr != ResError() )
1255 goto found_no_lwfn_file;
1260 UInt8 path_fond[PATH_MAX];
1264 err = FSGetForkCBInfo( res, kFSInvalidVolumeRefNum,
1265 NULL, NULL, NULL, &ref, NULL );
1267 goto found_no_lwfn_file;
1269 err = FSRefMakePath( &ref, path_fond, sizeof ( path_fond ) );
1271 goto found_no_lwfn_file;
1273 error = lookup_lwfn_by_fond( path_fond, lwfn_file_name,
1274 path_lwfn, sizeof ( path_lwfn ) );
1275 if ( FT_Err_Ok == error )
1282 UInt8 path_fond[PATH_MAX];
1284 Str255 fond_file_name;
1288 FT_MEM_SET( &spec, 0, sizeof ( FSSpec ) );
1289 FT_MEM_SET( &pb, 0, sizeof ( FCBPBRec ) );
1291 pb.ioNamePtr = fond_file_name;
1296 err = PBGetFCBInfoSync( &pb );
1298 goto found_no_lwfn_file;
1300 err = FSMakeFSSpec( pb.ioFCBVRefNum, pb.ioFCBParID,
1301 fond_file_name, &spec );
1303 goto found_no_lwfn_file;
1305 err = FT_FSpMakePath( &spec, path_fond, sizeof ( path_fond ) );
1307 goto found_no_lwfn_file;
1309 error = lookup_lwfn_by_fond( path_fond, lwfn_file_name,
1310 path_lwfn, sizeof ( path_lwfn ) );
1311 if ( FT_Err_Ok == error )
1315 #endif /* HAVE_FSREF, HAVE_FSSPEC */
1319 if ( have_lwfn && ( !have_sfnt || PREFER_LWFN ) )
1320 error = FT_New_Face_From_LWFN( library,
1325 error = FT_Err_Unknown_File_Format;
1328 if ( have_sfnt && FT_Err_Ok != error )
1329 error = FT_New_Face_From_SFNT( library,
1338 /* Common function to load a new FT_Face from a resource file. */
1340 FT_New_Face_From_Resource( FT_Library library,
1341 const UInt8* pathname,
1349 /* LWFN is a (very) specific file format, check for it explicitly */
1350 file_type = get_file_type_from_path( pathname );
1351 if ( file_type == TTAG_LWFN )
1352 return FT_New_Face_From_LWFN( library, pathname, face_index, aface );
1354 /* Otherwise the file type doesn't matter (there are more than */
1355 /* `FFIL' and `tfil'). Just try opening it as a font suitcase; */
1356 /* if it works, fine. */
1358 error = FT_New_Face_From_Suitcase( library, pathname, face_index, aface );
1362 /* let it fall through to normal loader (.ttf, .otf, etc.); */
1363 /* we signal this by returning no error and no FT_Face */
1369 /*************************************************************************/
1375 /* This is the Mac-specific implementation of FT_New_Face. In */
1376 /* addition to the standard FT_New_Face() functionality, it also */
1377 /* accepts pathnames to Mac suitcase files. For further */
1378 /* documentation see the original FT_New_Face() in freetype.h. */
1380 FT_EXPORT_DEF( FT_Error )
1381 FT_New_Face( FT_Library library,
1382 const char* pathname,
1390 /* test for valid `library' and `aface' delayed to FT_Open_Face() */
1392 return FT_Err_Invalid_Argument;
1397 /* try resourcefork based font: LWFN, FFIL */
1398 error = FT_New_Face_From_Resource( library, (UInt8 *)pathname,
1399 face_index, aface );
1400 if ( error != 0 || *aface != NULL )
1403 /* let it fall through to normal loader (.ttf, .otf, etc.) */
1404 args.flags = FT_OPEN_PATHNAME;
1405 args.pathname = (char*)pathname;
1406 return FT_Open_Face( library, &args, face_index, aface );
1410 /*************************************************************************/
1413 /* FT_New_Face_From_FSRef */
1416 /* FT_New_Face_From_FSRef is identical to FT_New_Face except it */
1417 /* accepts an FSRef instead of a path. */
1419 /* This function is deprecated because Carbon data types (FSRef) */
1420 /* are not cross-platform, and thus not suitable for the freetype API. */
1421 FT_EXPORT_DEF( FT_Error )
1422 FT_New_Face_From_FSRef( FT_Library library,
1430 FT_UNUSED( library );
1432 FT_UNUSED( face_index );
1435 return FT_Err_Unimplemented_Feature;
1442 UInt8 pathname[PATH_MAX];
1446 return FT_Err_Invalid_Argument;
1448 err = FSRefMakePath( ref, pathname, sizeof ( pathname ) );
1450 error = FT_Err_Cannot_Open_Resource;
1452 error = FT_New_Face_From_Resource( library, pathname, face_index, aface );
1453 if ( error != 0 || *aface != NULL )
1456 /* fallback to datafork font */
1457 args.flags = FT_OPEN_PATHNAME;
1458 args.pathname = (char*)pathname;
1459 return FT_Open_Face( library, &args, face_index, aface );
1461 #endif /* HAVE_FSREF */
1466 /*************************************************************************/
1469 /* FT_New_Face_From_FSSpec */
1472 /* FT_New_Face_From_FSSpec is identical to FT_New_Face except it */
1473 /* accepts an FSSpec instead of a path. */
1475 /* This function is deprecated because Carbon data types (FSSpec) */
1476 /* are not cross-platform, and thus not suitable for the freetype API. */
1477 FT_EXPORT_DEF( FT_Error )
1478 FT_New_Face_From_FSSpec( FT_Library library,
1489 if ( !spec || FSpMakeFSRef( spec, &ref ) != noErr )
1490 return FT_Err_Invalid_Argument;
1492 return FT_New_Face_From_FSRef( library, &ref, face_index, aface );
1499 UInt8 pathname[PATH_MAX];
1503 return FT_Err_Invalid_Argument;
1505 err = FT_FSpMakePath( spec, pathname, sizeof ( pathname ) );
1507 error = FT_Err_Cannot_Open_Resource;
1509 error = FT_New_Face_From_Resource( library, pathname, face_index, aface );
1510 if ( error != 0 || *aface != NULL )
1513 /* fallback to datafork font */
1514 args.flags = FT_OPEN_PATHNAME;
1515 args.pathname = (char*)pathname;
1516 return FT_Open_Face( library, &args, face_index, aface );
1520 FT_UNUSED( library );
1522 FT_UNUSED( face_index );
1525 return FT_Err_Unimplemented_Feature;
1527 #endif /* HAVE_FSREF, HAVE_FSSPEC */
1531 #endif /* FT_MACINTOSH */