1 /***************************************************************************/
5 /* Mac FOND support. Written by just@letterror.com. */
6 /* Heavily Fixed by mpsuzuki, George Williams and Sean McBride */
8 /* Copyright (C) 1996-2023 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
65 #include <freetype/freetype.h>
66 #include <freetype/tttags.h>
67 #include <freetype/internal/ftstream.h>
70 #if defined( __GNUC__ ) || defined( __IBMC__ )
71 /* This is for Mac OS X. Without redefinition, OS_INLINE */
72 /* expands to `static inline' which doesn't survive the */
73 /* -ansi compilation flag of GCC. */
74 #if !HAVE_ANSI_OS_INLINE
76 #define OS_INLINE static __inline__
78 #include <CoreServices/CoreServices.h>
79 #include <ApplicationServices/ApplicationServices.h>
80 #include <sys/syslimits.h> /* PATH_MAX */
82 #include <Resources.h>
87 #include <TextUtils.h>
91 #define PATH_MAX 1024 /* same with Mac OS X's syslimits.h */
94 #if defined( __MWERKS__ ) && !TARGET_RT_MAC_MACHO
95 #include <FSp_fopen.h>
98 #define FT_DEPRECATED_ATTRIBUTE
100 #include <freetype/ftmac.h>
102 /* undefine blocking-macros in ftmac.h */
103 #undef FT_GetFile_From_Mac_Name
104 #undef FT_GetFile_From_Mac_ATS_Name
105 #undef FT_New_Face_From_FOND
106 #undef FT_New_Face_From_FSSpec
107 #undef FT_New_Face_From_FSRef
110 /* FSSpec functions are deprecated since Mac OS X 10.4 */
112 #if TARGET_API_MAC_OS8 || TARGET_API_MAC_CARBON
113 #define HAVE_FSSPEC 1
115 #define HAVE_FSSPEC 0
119 /* most FSRef functions were introduced since Mac OS 9 */
121 #if TARGET_API_MAC_OSX
128 /* QuickDraw is deprecated since Mac OS X 10.4 */
129 #ifndef HAVE_QUICKDRAW_CARBON
130 #if TARGET_API_MAC_OS8 || TARGET_API_MAC_CARBON
131 #define HAVE_QUICKDRAW_CARBON 1
133 #define HAVE_QUICKDRAW_CARBON 0
137 /* AppleTypeService is available since Mac OS X */
139 #if TARGET_API_MAC_OSX
141 #ifndef kATSOptionFlagsUnRestrictedScope /* since Mac OS X 10.1 */
142 #define kATSOptionFlagsUnRestrictedScope kATSOptionFlagsDefault
149 /* `configure' checks the availability of `ResourceIndex' strictly */
150 /* and sets HAVE_TYPE_RESOURCE_INDEX to 1 or 0 always. If it is */
151 /* not set (e.g., a build without `configure'), the availability */
152 /* is guessed from the SDK version. */
153 #ifndef HAVE_TYPE_RESOURCE_INDEX
154 #if !defined( MAC_OS_X_VERSION_10_5 ) || \
155 ( MAC_OS_X_VERSION_MAX_ALLOWED < MAC_OS_X_VERSION_10_5 )
156 #define HAVE_TYPE_RESOURCE_INDEX 0
158 #define HAVE_TYPE_RESOURCE_INDEX 1
160 #endif /* !HAVE_TYPE_RESOURCE_INDEX */
162 #if ( HAVE_TYPE_RESOURCE_INDEX == 0 )
163 typedef short ResourceIndex;
166 /* Set PREFER_LWFN to 1 if LWFN (Type 1) is preferred over
167 TrueType in case *both* are available (this is not common,
168 but it *is* possible). */
170 #define PREFER_LWFN 1
175 #if !HAVE_QUICKDRAW_CARBON /* QuickDraw is deprecated since Mac OS X 10.4 */
177 FT_EXPORT_DEF( FT_Error )
178 FT_GetFile_From_Mac_Name( const char* fontName,
180 FT_Long* face_index )
182 FT_UNUSED( fontName );
183 FT_UNUSED( pathSpec );
184 FT_UNUSED( face_index );
186 return FT_THROW( Unimplemented_Feature );
191 FT_EXPORT_DEF( FT_Error )
192 FT_GetFile_From_Mac_Name( const char* fontName,
194 FT_Long* face_index )
196 OptionBits options = kFMUseGlobalScopeOption;
198 FMFontFamilyIterator famIter;
199 OSStatus status = FMCreateFontFamilyIterator( NULL, NULL,
203 FMFontFamily family = 0;
206 if ( !fontName || !face_index )
207 return FT_THROW( Invalid_Argument );
210 while ( status == 0 && !the_font )
212 status = FMGetNextFontFamily( &famIter, &family );
216 FMFontFamilyInstanceIterator instIter;
221 /* get the family name */
222 FMGetFontFamilyName( family, famNameStr );
223 CopyPascalStringToC( famNameStr, famName );
225 /* iterate through the styles */
226 FMCreateFontFamilyInstanceIterator( family, &instIter );
231 while ( stat2 == 0 && !the_font )
238 stat2 = FMGetNextFontFamilyInstance( &instIter, &font,
240 if ( stat2 == 0 && size == 0 )
245 /* build up a complete face name */
246 ft_strcpy( fullName, famName );
248 ft_strcat( fullName, " Bold" );
249 if ( style & italic )
250 ft_strcat( fullName, " Italic" );
252 /* compare with the name we are looking for */
253 if ( ft_strcmp( fullName, fontName ) == 0 )
263 FMDisposeFontFamilyInstanceIterator( &instIter );
267 FMDisposeFontFamilyIterator( &famIter );
271 FMGetFontContainer( the_font, pathSpec );
275 return FT_THROW( Unknown_File_Format );
278 #endif /* HAVE_QUICKDRAW_CARBON */
283 /* Private function. */
284 /* The FSSpec type has been discouraged for a long time, */
285 /* unfortunately an FSRef replacement API for */
286 /* ATSFontGetFileSpecification() is only available in */
287 /* Mac OS X 10.5 and later. */
289 FT_ATSFontGetFileReference( ATSFontRef ats_font_id,
290 FSRef* ats_font_ref )
294 #if !defined( MAC_OS_X_VERSION_10_5 ) || \
295 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
299 err = ATSFontGetFileSpecification( ats_font_id, &spec );
301 err = FSpMakeFSRef( &spec, ats_font_ref );
303 err = ATSFontGetFileReference( ats_font_id, ats_font_ref );
311 FT_GetFileRef_From_Mac_ATS_Name( const char* fontName,
313 FT_Long* face_index )
315 CFStringRef cf_fontName;
316 ATSFontRef ats_font_id;
321 cf_fontName = CFStringCreateWithCString( NULL, fontName,
322 kCFStringEncodingMacRoman );
323 ats_font_id = ATSFontFindFromName( cf_fontName,
324 kATSOptionFlagsUnRestrictedScope );
325 CFRelease( cf_fontName );
327 if ( ats_font_id == 0 || ats_font_id == 0xFFFFFFFFUL )
328 return FT_THROW( Unknown_File_Format );
330 if ( noErr != FT_ATSFontGetFileReference( ats_font_id, ats_font_ref ) )
331 return FT_THROW( Unknown_File_Format );
333 /* face_index calculation by searching preceding fontIDs */
334 /* with same FSRef */
336 ATSFontRef id2 = ats_font_id - 1;
342 if ( noErr != FT_ATSFontGetFileReference( id2, &ref2 ) )
344 if ( noErr != FSCompareFSRefs( ats_font_ref, &ref2 ) )
349 *face_index = ats_font_id - ( id2 + 1 );
359 FT_EXPORT_DEF( FT_Error )
360 FT_GetFilePath_From_Mac_ATS_Name( const char* fontName,
363 FT_Long* face_index )
365 FT_UNUSED( fontName );
367 FT_UNUSED( maxPathSize );
368 FT_UNUSED( face_index );
370 return FT_THROW( Unimplemented_Feature );
375 FT_EXPORT_DEF( FT_Error )
376 FT_GetFilePath_From_Mac_ATS_Name( const char* fontName,
379 FT_Long* face_index )
385 err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
389 if ( noErr != FSRefMakePath( &ref, path, maxPathSize ) )
390 return FT_THROW( Unknown_File_Format );
395 #endif /* HAVE_ATS */
398 #if !HAVE_FSSPEC || !HAVE_ATS
400 FT_EXPORT_DEF( FT_Error )
401 FT_GetFile_From_Mac_ATS_Name( const char* fontName,
403 FT_Long* face_index )
405 FT_UNUSED( fontName );
406 FT_UNUSED( pathSpec );
407 FT_UNUSED( face_index );
409 return FT_THROW( Unimplemented_Feature );
414 /* This function is deprecated because FSSpec is deprecated in Mac OS X. */
415 FT_EXPORT_DEF( FT_Error )
416 FT_GetFile_From_Mac_ATS_Name( const char* fontName,
418 FT_Long* face_index )
424 err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
428 if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone, NULL, NULL,
430 return FT_THROW( Unknown_File_Format );
438 #if defined( __MWERKS__ ) && !TARGET_RT_MAC_MACHO
440 #define STREAM_FILE( stream ) ( (FT_FILE*)stream->descriptor.pointer )
443 FT_CALLBACK_DEF( void )
444 ft_FSp_stream_close( FT_Stream stream )
446 ft_fclose( STREAM_FILE( stream ) );
448 stream->descriptor.pointer = NULL;
454 FT_CALLBACK_DEF( unsigned long )
455 ft_FSp_stream_io( FT_Stream stream,
456 unsigned long offset,
457 unsigned char* buffer,
458 unsigned long count )
463 file = STREAM_FILE( stream );
465 ft_fseek( file, offset, SEEK_SET );
467 return (unsigned long)ft_fread( buffer, 1, count, file );
470 #endif /* __MWERKS__ && !TARGET_RT_MAC_MACHO */
473 #if HAVE_FSSPEC && !HAVE_FSREF
475 /* isDirectory is a dummy to synchronize API with FSPathMakeRef() */
477 FT_FSPathMakeSpec( const UInt8* pathname,
479 Boolean isDirectory )
486 FT_UNUSED( isDirectory );
489 p = q = (const char *)pathname;
495 int len = ft_strlen( p );
506 if ( 255 < ft_strlen( (char *)pathname ) )
508 while ( p < q && *q != ':' )
513 *(char *)nodeName = q - p;
514 else if ( ft_strlen( p ) < 256 )
515 *(char *)nodeName = ft_strlen( p );
517 return errFSNameTooLong;
519 ft_strncpy( (char *)nodeName + 1, (char *)p, *(char *)nodeName );
520 err = FSMakeFSSpec( vRefNum, dirID, nodeName, spec_p );
521 if ( err || '\0' == *q )
524 vRefNum = spec_p->vRefNum;
525 dirID = spec_p->parID;
533 FT_FSpMakePath( const FSSpec* spec_p,
538 FSSpec spec = *spec_p;
544 FT_MEM_SET( path, 0, maxPathSize );
547 int child_namelen = ft_strlen( (char *)path );
548 unsigned char node_namelen = spec.name[0];
549 unsigned char* node_name = spec.name + 1;
552 if ( node_namelen + child_namelen > maxPathSize )
553 return errFSNameTooLong;
555 FT_MEM_MOVE( path + node_namelen + 1, path, child_namelen );
556 FT_MEM_COPY( path, node_name, node_namelen );
557 if ( child_namelen > 0 )
558 path[node_namelen] = ':';
560 vRefNum = spec.vRefNum;
562 parDir_name[0] = '\0';
563 err = FSMakeFSSpec( vRefNum, dirID, parDir_name, &spec );
564 if ( noErr != err || dirID == spec.parID )
570 #endif /* HAVE_FSSPEC && !HAVE_FSREF */
574 FT_FSPathMakeRes( const UInt8* pathname,
584 if ( noErr != FSPathMakeRef( pathname, &ref, FALSE ) )
585 return FT_THROW( Cannot_Open_Resource );
587 /* at present, no support for dfont format */
588 err = FSOpenResourceFile( &ref, 0, NULL, fsRdPerm, res );
592 /* fallback to original resource-fork font */
593 *res = FSOpenResFile( &ref, fsRdPerm );
602 if ( noErr != FT_FSPathMakeSpec( pathname, &spec, FALSE ) )
603 return FT_THROW( Cannot_Open_Resource );
605 /* at present, no support for dfont format without FSRef */
606 /* (see above), try original resource-fork font */
607 *res = FSpOpenResFile( &spec, fsRdPerm );
610 #endif /* HAVE_FSREF */
616 /* Return the file type for given pathname */
618 get_file_type_from_path( const UInt8* pathname )
627 if ( noErr != FSPathMakeRef( pathname, &ref, FALSE ) )
630 if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoFinderInfo, &info,
634 return ((FInfo *)(info.finderInfo))->fdType;
642 if ( noErr != FT_FSPathMakeSpec( pathname, &spec, FALSE ) )
645 if ( noErr != FSpGetFInfo( &spec, &finfo ) )
650 #endif /* HAVE_FSREF */
655 /* Given a PostScript font name, create the Macintosh LWFN file name. */
657 create_lwfn_name( char* ps_name,
658 Str255 lwfn_file_name )
660 int max = 5, count = 0;
661 FT_Byte* p = lwfn_file_name;
662 FT_Byte* q = (FT_Byte*)ps_name;
665 lwfn_file_name[0] = 0;
669 if ( ft_isupper( *q ) )
675 if ( count < max && ( ft_isalnum( *q ) || *q == '_' ) )
687 count_faces_sfnt( char* fond_data )
689 /* The count is 1 greater than the value in the FOND. */
690 /* Isn't that cute? :-) */
692 return EndianS16_BtoN( *( (short*)( fond_data +
693 sizeof ( FamRec ) ) ) ) + 1;
698 count_faces_scalable( char* fond_data )
701 short i, face, face_all;
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 string_count = FT_MIN( 64, string_count );
783 p += sizeof ( short );
785 for ( i = 0; i < string_count; i++ )
793 size_t ps_name_len = (size_t)names[0][0];
796 if ( ps_name_len != 0 )
798 ft_memcpy(ps_name, names[0] + 1, ps_name_len);
799 ps_name[ps_name_len] = 0;
801 if ( style->indexes[face_index] > 1 &&
802 style->indexes[face_index] <= string_count )
804 unsigned char* suffixes = names[style->indexes[face_index] - 1];
807 for ( i = 1; i <= suffixes[0]; i++ )
810 size_t j = suffixes[i] - 1;
813 if ( j < string_count && ( s = names[j] ) != NULL )
815 size_t s_len = (size_t)s[0];
818 if ( s_len != 0 && ps_name_len + s_len < sizeof ( ps_name ) )
820 ft_memcpy( ps_name + ps_name_len, s + 1, s_len );
821 ps_name_len += s_len;
822 ps_name[ps_name_len] = 0;
829 create_lwfn_name( ps_name, lwfn_file_name );
835 lookup_lwfn_by_fond( const UInt8* path_fond,
836 ConstStr255Param base_lwfn,
847 /* Pathname for FSRef can be in various formats: HFS, HFS+, and POSIX. */
848 /* We should not extract parent directory by string manipulation. */
850 if ( noErr != FSPathMakeRef( path_fond, &ref, FALSE ) )
851 return FT_THROW( Invalid_Argument );
853 if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone,
854 NULL, NULL, NULL, &par_ref ) )
855 return FT_THROW( Invalid_Argument );
857 if ( noErr != FSRefMakePath( &par_ref, path_lwfn, path_size ) )
858 return FT_THROW( Invalid_Argument );
860 if ( ft_strlen( (char *)path_lwfn ) + 1 + base_lwfn[0] > path_size )
861 return FT_THROW( Invalid_Argument );
863 /* now we have absolute dirname in path_lwfn */
864 if ( path_lwfn[0] == '/' )
865 ft_strcat( (char *)path_lwfn, "/" );
867 ft_strcat( (char *)path_lwfn, ":" );
869 dirname_len = ft_strlen( (char *)path_lwfn );
870 ft_strcat( (char *)path_lwfn, (char *)base_lwfn + 1 );
871 path_lwfn[dirname_len + base_lwfn[0]] = '\0';
873 if ( noErr != FSPathMakeRef( path_lwfn, &ref, FALSE ) )
874 return FT_THROW( Cannot_Open_Resource );
876 if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone,
877 NULL, NULL, NULL, NULL ) )
878 return FT_THROW( Cannot_Open_Resource );
888 /* pathname for FSSpec is always HFS format */
889 if ( ft_strlen( (char *)path_fond ) > path_size )
890 return FT_THROW( Invalid_Argument );
892 ft_strcpy( (char *)path_lwfn, (char *)path_fond );
894 i = ft_strlen( (char *)path_lwfn ) - 1;
895 while ( i > 0 && ':' != path_lwfn[i] )
898 if ( i + 1 + base_lwfn[0] > path_size )
899 return FT_THROW( Invalid_Argument );
901 if ( ':' == path_lwfn[i] )
903 ft_strcpy( (char *)path_lwfn + i + 1, (char *)base_lwfn + 1 );
904 path_lwfn[i + 1 + base_lwfn[0]] = '\0';
908 ft_strcpy( (char *)path_lwfn, (char *)base_lwfn + 1 );
909 path_lwfn[base_lwfn[0]] = '\0';
912 if ( noErr != FT_FSPathMakeSpec( path_lwfn, &spec, FALSE ) )
913 return FT_THROW( Cannot_Open_Resource );
917 #endif /* HAVE_FSREF */
923 count_faces( Handle fond,
924 const UInt8* pathname )
927 short have_sfnt, have_lwfn;
928 Str255 lwfn_file_name;
929 UInt8 buff[PATH_MAX];
934 have_sfnt = have_lwfn = 0;
937 parse_fond( *fond, &have_sfnt, &sfnt_id, lwfn_file_name, 0 );
939 if ( lwfn_file_name[0] )
941 err = lookup_lwfn_by_fond( pathname, lwfn_file_name,
942 buff, sizeof ( buff ) );
947 if ( have_lwfn && ( !have_sfnt || PREFER_LWFN ) )
950 num_faces = count_faces_scalable( *fond );
957 /* Read Type 1 data from the POST resources inside the LWFN file,
958 return a PFB buffer. This is somewhat convoluted because the FT2
959 PFB parser wants the ASCII header as one chunk, and the LWFN
960 chunks are often not organized that way, so we glue chunks
961 of the same type together. */
963 read_lwfn( FT_Memory memory,
968 FT_Error error = FT_Err_Ok;
970 unsigned char *buffer, *p, *size_p = NULL;
971 FT_ULong total_size = 0;
972 FT_ULong old_total_size = 0;
973 FT_ULong post_size, pfb_chunk_size;
975 char code, last_code;
980 /* First pass: load all POST resources, and determine the size of */
981 /* the output buffer. */
987 post_data = Get1Resource( TTAG_POST, res_id++ );
988 if ( post_data == NULL )
989 break; /* we are done */
991 code = (*post_data)[0];
993 if ( code != last_code )
996 total_size += 2; /* just the end code */
998 total_size += 6; /* code + 4 bytes chunk length */
1001 total_size += GetHandleSize( post_data ) - 2;
1004 /* detect integer overflows */
1005 if ( total_size < old_total_size )
1007 error = FT_ERR( Array_Too_Large );
1011 old_total_size = total_size;
1014 if ( FT_QALLOC( buffer, (FT_Long)total_size ) )
1017 /* Second pass: append all POST data to the buffer, add PFB fields. */
1018 /* Glue all consecutive chunks of the same type together. */
1026 post_data = Get1Resource( TTAG_POST, res_id++ );
1027 if ( post_data == NULL )
1028 break; /* we are done */
1030 post_size = (FT_ULong)GetHandleSize( post_data ) - 2;
1031 code = (*post_data)[0];
1033 if ( code != last_code )
1035 if ( last_code != -1 )
1037 /* we are done adding a chunk, fill in the size field */
1038 if ( size_p != NULL )
1040 *size_p++ = (FT_Byte)( pfb_chunk_size & 0xFF );
1041 *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 8 ) & 0xFF );
1042 *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 16 ) & 0xFF );
1043 *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 24 ) & 0xFF );
1050 *p++ = 0x03; /* the end */
1051 else if ( code == 2 )
1052 *p++ = 0x02; /* binary segment */
1054 *p++ = 0x01; /* ASCII segment */
1058 size_p = p; /* save for later */
1059 p += 4; /* make space for size field */
1063 ft_memcpy( p, *post_data + 2, post_size );
1064 pfb_chunk_size += post_size;
1073 CloseResFile( res );
1078 /* Create a new FT_Face from a file spec to an LWFN file. */
1080 FT_New_Face_From_LWFN( FT_Library library,
1081 const UInt8* pathname,
1091 if ( noErr != FT_FSPathMakeRes( pathname, &res ) )
1092 return FT_THROW( Cannot_Open_Resource );
1096 error = read_lwfn( library->memory, res, &pfb_data, &pfb_size );
1097 CloseResFile( res ); /* PFB is already loaded, useless anymore */
1101 return open_face_from_buffer( library,
1110 /* Create a new FT_Face from an SFNT resource, specified by res ID. */
1112 FT_New_Face_From_SFNT( FT_Library library,
1120 FT_Error error = FT_Err_Ok;
1121 FT_Memory memory = library->memory;
1122 int is_cff, is_sfnt_ps;
1125 sfnt = GetResource( TTAG_sfnt, sfnt_id );
1127 return FT_THROW( Invalid_Handle );
1129 sfnt_size = (FT_ULong)GetHandleSize( sfnt );
1130 if ( FT_QALLOC( sfnt_data, (FT_Long)sfnt_size ) )
1132 ReleaseResource( sfnt );
1137 ft_memcpy( sfnt_data, *sfnt, sfnt_size );
1139 ReleaseResource( sfnt );
1141 is_cff = sfnt_size > 4 && !ft_memcmp( sfnt_data, "OTTO", 4 );
1142 is_sfnt_ps = sfnt_size > 4 && !ft_memcmp( sfnt_data, "typ1", 4 );
1149 if ( FT_NEW( stream ) )
1152 FT_Stream_OpenMemory( stream, sfnt_data, sfnt_size );
1153 if ( !open_face_PS_from_sfnt_stream( library,
1159 FT_Stream_Close( stream );
1161 FT_FREE( sfnt_data );
1168 error = open_face_from_buffer( library,
1172 is_cff ? "cff" : "truetype",
1179 /* Create a new FT_Face from a file spec to a suitcase file. */
1181 FT_New_Face_From_Suitcase( FT_Library library,
1182 const UInt8* pathname,
1186 FT_Error error = FT_ERR( Cannot_Open_Resource );
1187 ResFileRefNum res_ref;
1188 ResourceIndex res_index;
1190 short num_faces_in_res;
1193 if ( noErr != FT_FSPathMakeRes( pathname, &res_ref ) )
1194 return FT_THROW( Cannot_Open_Resource );
1196 UseResFile( res_ref );
1198 return FT_THROW( Cannot_Open_Resource );
1200 num_faces_in_res = 0;
1201 for ( res_index = 1; ; ++res_index )
1203 short num_faces_in_fond;
1206 fond = Get1IndResource( TTAG_FOND, res_index );
1210 num_faces_in_fond = count_faces( fond, pathname );
1211 num_faces_in_res += num_faces_in_fond;
1213 if ( 0 <= face_index && face_index < num_faces_in_fond && error )
1214 error = FT_New_Face_From_FOND( library, fond, face_index, aface );
1216 face_index -= num_faces_in_fond;
1219 CloseResFile( res_ref );
1220 if ( !error && aface )
1221 (*aface)->num_faces = num_faces_in_res;
1226 /* documentation is in ftmac.h */
1228 FT_EXPORT_DEF( FT_Error )
1229 FT_New_Face_From_FOND( FT_Library library,
1234 short have_sfnt, have_lwfn = 0;
1235 ResID sfnt_id, fond_id;
1238 Str255 lwfn_file_name;
1239 UInt8 path_lwfn[PATH_MAX];
1241 FT_Error error = FT_Err_Ok;
1244 /* test for valid `aface' and `library' delayed to */
1245 /* `FT_New_Face_From_XXX' */
1247 GetResInfo( fond, &fond_id, &fond_type, fond_name );
1248 if ( ResError() != noErr || fond_type != TTAG_FOND )
1249 return FT_THROW( Invalid_File_Format );
1252 parse_fond( *fond, &have_sfnt, &sfnt_id, lwfn_file_name, face_index );
1255 if ( lwfn_file_name[0] )
1260 res = HomeResFile( fond );
1261 if ( noErr != ResError() )
1262 goto found_no_lwfn_file;
1267 UInt8 path_fond[PATH_MAX];
1271 err = FSGetForkCBInfo( res, kFSInvalidVolumeRefNum,
1272 NULL, NULL, NULL, &ref, NULL );
1274 goto found_no_lwfn_file;
1276 err = FSRefMakePath( &ref, path_fond, sizeof ( path_fond ) );
1278 goto found_no_lwfn_file;
1280 error = lookup_lwfn_by_fond( path_fond, lwfn_file_name,
1281 path_lwfn, sizeof ( path_lwfn ) );
1289 UInt8 path_fond[PATH_MAX];
1291 Str255 fond_file_name;
1295 FT_MEM_SET( &spec, 0, sizeof ( FSSpec ) );
1296 FT_MEM_SET( &pb, 0, sizeof ( FCBPBRec ) );
1298 pb.ioNamePtr = fond_file_name;
1303 err = PBGetFCBInfoSync( &pb );
1305 goto found_no_lwfn_file;
1307 err = FSMakeFSSpec( pb.ioFCBVRefNum, pb.ioFCBParID,
1308 fond_file_name, &spec );
1310 goto found_no_lwfn_file;
1312 err = FT_FSpMakePath( &spec, path_fond, sizeof ( path_fond ) );
1314 goto found_no_lwfn_file;
1316 error = lookup_lwfn_by_fond( path_fond, lwfn_file_name,
1317 path_lwfn, sizeof ( path_lwfn ) );
1322 #endif /* HAVE_FSREF, HAVE_FSSPEC */
1326 if ( have_lwfn && ( !have_sfnt || PREFER_LWFN ) )
1327 error = FT_New_Face_From_LWFN( library,
1332 error = FT_ERR( Unknown_File_Format );
1335 if ( have_sfnt && error )
1336 error = FT_New_Face_From_SFNT( library,
1345 /* Common function to load a new FT_Face from a resource file. */
1347 FT_New_Face_From_Resource( FT_Library library,
1348 const UInt8* pathname,
1356 /* LWFN is a (very) specific file format, check for it explicitly */
1357 file_type = get_file_type_from_path( pathname );
1358 if ( file_type == TTAG_LWFN )
1359 return FT_New_Face_From_LWFN( library, pathname, face_index, aface );
1361 /* Otherwise the file type doesn't matter (there are more than */
1362 /* `FFIL' and `tfil'). Just try opening it as a font suitcase; */
1363 /* if it works, fine. */
1365 error = FT_New_Face_From_Suitcase( library, pathname, face_index, aface );
1369 /* let it fall through to normal loader (.ttf, .otf, etc.); */
1370 /* we signal this by returning no error and no FT_Face */
1376 /*************************************************************************/
1382 /* This is the Mac-specific implementation of FT_New_Face. In */
1383 /* addition to the standard FT_New_Face() functionality, it also */
1384 /* accepts pathnames to Mac suitcase files. For further */
1385 /* documentation see the original FT_New_Face() in freetype.h. */
1387 FT_EXPORT_DEF( FT_Error )
1388 FT_New_Face( FT_Library library,
1389 const char* pathname,
1397 /* test for valid `library' and `aface' delayed to FT_Open_Face() */
1399 return FT_THROW( Invalid_Argument );
1403 /* try resourcefork based font: LWFN, FFIL */
1404 error = FT_New_Face_From_Resource( library, (UInt8 *)pathname,
1405 face_index, aface );
1406 if ( error || *aface )
1409 /* let it fall through to normal loader (.ttf, .otf, etc.) */
1410 args.flags = FT_OPEN_PATHNAME;
1411 args.pathname = (char*)pathname;
1412 return FT_Open_Face( library, &args, face_index, aface );
1416 /*************************************************************************/
1419 /* FT_New_Face_From_FSRef */
1422 /* FT_New_Face_From_FSRef is identical to FT_New_Face except it */
1423 /* accepts an FSRef instead of a path. */
1425 /* This function is deprecated because Carbon data types (FSRef) */
1426 /* are not cross-platform, and thus not suitable for the FreeType API. */
1427 FT_EXPORT_DEF( FT_Error )
1428 FT_New_Face_From_FSRef( FT_Library library,
1436 FT_UNUSED( library );
1438 FT_UNUSED( face_index );
1441 return FT_THROW( Unimplemented_Feature );
1448 UInt8 pathname[PATH_MAX];
1451 /* test for valid `library' and `aface' delayed to `FT_Open_Face' */
1454 return FT_THROW( Invalid_Argument );
1456 err = FSRefMakePath( ref, pathname, sizeof ( pathname ) );
1458 error = FT_ERR( Cannot_Open_Resource );
1460 error = FT_New_Face_From_Resource( library, pathname, face_index, aface );
1461 if ( error || *aface )
1464 /* fallback to datafork font */
1465 args.flags = FT_OPEN_PATHNAME;
1466 args.pathname = (char*)pathname;
1467 return FT_Open_Face( library, &args, face_index, aface );
1469 #endif /* HAVE_FSREF */
1474 /*************************************************************************/
1477 /* FT_New_Face_From_FSSpec */
1480 /* FT_New_Face_From_FSSpec is identical to FT_New_Face except it */
1481 /* accepts an FSSpec instead of a path. */
1483 /* This function is deprecated because Carbon data types (FSSpec) */
1484 /* are not cross-platform, and thus not suitable for the FreeType API. */
1485 FT_EXPORT_DEF( FT_Error )
1486 FT_New_Face_From_FSSpec( FT_Library library,
1497 if ( !spec || FSpMakeFSRef( spec, &ref ) != noErr )
1498 return FT_THROW( Invalid_Argument );
1500 return FT_New_Face_From_FSRef( library, &ref, face_index, aface );
1507 UInt8 pathname[PATH_MAX];
1511 return FT_THROW( Invalid_Argument );
1513 err = FT_FSpMakePath( spec, pathname, sizeof ( pathname ) );
1515 error = FT_ERR( Cannot_Open_Resource );
1517 error = FT_New_Face_From_Resource( library, pathname, face_index, aface );
1518 if ( error || *aface )
1521 /* fallback to datafork font */
1522 args.flags = FT_OPEN_PATHNAME;
1523 args.pathname = (char*)pathname;
1524 return FT_Open_Face( library, &args, face_index, aface );
1528 FT_UNUSED( library );
1530 FT_UNUSED( face_index );
1533 return FT_THROW( Unimplemented_Feature );
1535 #endif /* HAVE_FSREF, HAVE_FSSPEC */
1539 #endif /* FT_MACINTOSH */