1 /***************************************************************************/
5 /* Mac FOND support. Written by just@letterror.com. */
6 /* Heavily Fixed by mpsuzuki, George Williams and Sean McBride */
8 /* Copyright 1996-2008, 2013, 2014 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_THROW( 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;
207 if ( !fontName || !face_index )
208 return FT_THROW( Invalid_Argument );
211 while ( status == 0 && !the_font )
213 status = FMGetNextFontFamily( &famIter, &family );
217 FMFontFamilyInstanceIterator instIter;
222 /* get the family name */
223 FMGetFontFamilyName( family, famNameStr );
224 CopyPascalStringToC( famNameStr, famName );
226 /* iterate through the styles */
227 FMCreateFontFamilyInstanceIterator( family, &instIter );
232 while ( stat2 == 0 && !the_font )
239 stat2 = FMGetNextFontFamilyInstance( &instIter, &font,
241 if ( stat2 == 0 && size == 0 )
246 /* build up a complete face name */
247 ft_strcpy( fullName, famName );
249 ft_strcat( fullName, " Bold" );
250 if ( style & italic )
251 ft_strcat( fullName, " Italic" );
253 /* compare with the name we are looking for */
254 if ( ft_strcmp( fullName, fontName ) == 0 )
264 FMDisposeFontFamilyInstanceIterator( &instIter );
268 FMDisposeFontFamilyIterator( &famIter );
272 FMGetFontContainer( the_font, pathSpec );
276 return FT_THROW( Unknown_File_Format );
279 #endif /* HAVE_QUICKDRAW_CARBON */
284 /* Private function. */
285 /* The FSSpec type has been discouraged for a long time, */
286 /* unfortunately an FSRef replacement API for */
287 /* ATSFontGetFileSpecification() is only available in */
288 /* Mac OS X 10.5 and later. */
290 FT_ATSFontGetFileReference( ATSFontRef ats_font_id,
291 FSRef* ats_font_ref )
295 #if !defined( MAC_OS_X_VERSION_10_5 ) || \
296 MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
300 err = ATSFontGetFileSpecification( ats_font_id, &spec );
302 err = FSpMakeFSRef( &spec, ats_font_ref );
304 err = ATSFontGetFileReference( ats_font_id, ats_font_ref );
312 FT_GetFileRef_From_Mac_ATS_Name( const char* fontName,
314 FT_Long* face_index )
316 CFStringRef cf_fontName;
317 ATSFontRef ats_font_id;
322 cf_fontName = CFStringCreateWithCString( NULL, fontName,
323 kCFStringEncodingMacRoman );
324 ats_font_id = ATSFontFindFromName( cf_fontName,
325 kATSOptionFlagsUnRestrictedScope );
326 CFRelease( cf_fontName );
328 if ( ats_font_id == 0 || ats_font_id == 0xFFFFFFFFUL )
329 return FT_THROW( Unknown_File_Format );
331 if ( noErr != FT_ATSFontGetFileReference( ats_font_id, ats_font_ref ) )
332 return FT_THROW( Unknown_File_Format );
334 /* face_index calculation by searching preceding fontIDs */
335 /* with same FSRef */
337 ATSFontRef id2 = ats_font_id - 1;
343 if ( noErr != FT_ATSFontGetFileReference( id2, &ref2 ) )
345 if ( noErr != FSCompareFSRefs( ats_font_ref, &ref2 ) )
350 *face_index = ats_font_id - ( id2 + 1 );
360 FT_EXPORT_DEF( FT_Error )
361 FT_GetFilePath_From_Mac_ATS_Name( const char* fontName,
364 FT_Long* face_index )
366 FT_UNUSED( fontName );
368 FT_UNUSED( maxPathSize );
369 FT_UNUSED( face_index );
371 return FT_THROW( Unimplemented_Feature );
376 FT_EXPORT_DEF( FT_Error )
377 FT_GetFilePath_From_Mac_ATS_Name( const char* fontName,
380 FT_Long* face_index )
386 err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
390 if ( noErr != FSRefMakePath( &ref, path, maxPathSize ) )
391 return FT_THROW( Unknown_File_Format );
396 #endif /* HAVE_ATS */
399 #if !HAVE_FSSPEC || !HAVE_ATS
401 FT_EXPORT_DEF( FT_Error )
402 FT_GetFile_From_Mac_ATS_Name( const char* fontName,
404 FT_Long* face_index )
406 FT_UNUSED( fontName );
407 FT_UNUSED( pathSpec );
408 FT_UNUSED( face_index );
410 return FT_THROW( Unimplemented_Feature );
415 /* This function is deprecated because FSSpec is deprecated in Mac OS X. */
416 FT_EXPORT_DEF( FT_Error )
417 FT_GetFile_From_Mac_ATS_Name( const char* fontName,
419 FT_Long* face_index )
425 err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
429 if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone, NULL, NULL,
431 return FT_THROW( Unknown_File_Format );
439 #if defined( __MWERKS__ ) && !TARGET_RT_MAC_MACHO
441 #define STREAM_FILE( stream ) ( (FT_FILE*)stream->descriptor.pointer )
444 FT_CALLBACK_DEF( void )
445 ft_FSp_stream_close( FT_Stream stream )
447 ft_fclose( STREAM_FILE( stream ) );
449 stream->descriptor.pointer = NULL;
455 FT_CALLBACK_DEF( unsigned long )
456 ft_FSp_stream_io( FT_Stream stream,
457 unsigned long offset,
458 unsigned char* buffer,
459 unsigned long count )
464 file = STREAM_FILE( stream );
466 ft_fseek( file, offset, SEEK_SET );
468 return (unsigned long)ft_fread( buffer, 1, count, file );
471 #endif /* __MWERKS__ && !TARGET_RT_MAC_MACHO */
474 #if HAVE_FSSPEC && !HAVE_FSREF
476 /* isDirectory is a dummy to synchronize API with FSPathMakeRef() */
478 FT_FSPathMakeSpec( const UInt8* pathname,
480 Boolean isDirectory )
487 FT_UNUSED( isDirectory );
490 p = q = (const char *)pathname;
496 int len = ft_strlen( p );
507 if ( 255 < ft_strlen( (char *)pathname ) )
509 while ( p < q && *q != ':' )
514 *(char *)nodeName = q - p;
515 else if ( ft_strlen( p ) < 256 )
516 *(char *)nodeName = ft_strlen( p );
518 return errFSNameTooLong;
520 ft_strncpy( (char *)nodeName + 1, (char *)p, *(char *)nodeName );
521 err = FSMakeFSSpec( vRefNum, dirID, nodeName, spec_p );
522 if ( err || '\0' == *q )
525 vRefNum = spec_p->vRefNum;
526 dirID = spec_p->parID;
534 FT_FSpMakePath( const FSSpec* spec_p,
539 FSSpec spec = *spec_p;
545 FT_MEM_SET( path, 0, maxPathSize );
548 int child_namelen = ft_strlen( (char *)path );
549 unsigned char node_namelen = spec.name[0];
550 unsigned char* node_name = spec.name + 1;
553 if ( node_namelen + child_namelen > maxPathSize )
554 return errFSNameTooLong;
556 FT_MEM_MOVE( path + node_namelen + 1, path, child_namelen );
557 FT_MEM_COPY( path, node_name, node_namelen );
558 if ( child_namelen > 0 )
559 path[node_namelen] = ':';
561 vRefNum = spec.vRefNum;
563 parDir_name[0] = '\0';
564 err = FSMakeFSSpec( vRefNum, dirID, parDir_name, &spec );
565 if ( noErr != err || dirID == spec.parID )
571 #endif /* HAVE_FSSPEC && !HAVE_FSREF */
575 FT_FSPathMakeRes( const UInt8* pathname,
585 if ( noErr != FSPathMakeRef( pathname, &ref, FALSE ) )
586 return FT_THROW( Cannot_Open_Resource );
588 /* at present, no support for dfont format */
589 err = FSOpenResourceFile( &ref, 0, NULL, fsRdPerm, res );
593 /* fallback to original resource-fork font */
594 *res = FSOpenResFile( &ref, fsRdPerm );
603 if ( noErr != FT_FSPathMakeSpec( pathname, &spec, FALSE ) )
604 return FT_THROW( Cannot_Open_Resource );
606 /* at present, no support for dfont format without FSRef */
607 /* (see above), try original resource-fork font */
608 *res = FSpOpenResFile( &spec, fsRdPerm );
611 #endif /* HAVE_FSREF */
617 /* Return the file type for given pathname */
619 get_file_type_from_path( const UInt8* pathname )
628 if ( noErr != FSPathMakeRef( pathname, &ref, FALSE ) )
631 if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoFinderInfo, &info,
635 return ((FInfo *)(info.finderInfo))->fdType;
643 if ( noErr != FT_FSPathMakeSpec( pathname, &spec, FALSE ) )
646 if ( noErr != FSpGetFInfo( &spec, &finfo ) )
651 #endif /* HAVE_FSREF */
656 /* Given a PostScript font name, create the Macintosh LWFN file name. */
658 create_lwfn_name( char* ps_name,
659 Str255 lwfn_file_name )
661 int max = 5, count = 0;
662 FT_Byte* p = lwfn_file_name;
663 FT_Byte* q = (FT_Byte*)ps_name;
666 lwfn_file_name[0] = 0;
670 if ( ft_isupper( *q ) )
676 if ( count < max && ( ft_isalnum( *q ) || *q == '_' ) )
688 count_faces_sfnt( char* fond_data )
690 /* The count is 1 greater than the value in the FOND. */
691 /* Isn't that cute? :-) */
693 return EndianS16_BtoN( *( (short*)( fond_data +
694 sizeof ( FamRec ) ) ) ) + 1;
699 count_faces_scalable( char* fond_data )
702 short i, face, face_all;
705 face_all = EndianS16_BtoN( *( (short *)( fond_data +
706 sizeof ( FamRec ) ) ) ) + 1;
707 assoc = (AsscEntry*)( fond_data + sizeof ( FamRec ) + 2 );
710 for ( i = 0; i < face_all; i++ )
712 if ( 0 == EndianS16_BtoN( assoc[i].fontSize ) )
719 /* Look inside the FOND data, answer whether there should be an SFNT
720 resource, and answer the name of a possible LWFN Type 1 file.
722 Thanks to Paul Miller (paulm@profoundeffects.com) for the fix
723 to load a face OTHER than the first one in the FOND!
727 parse_fond( char* fond_data,
730 Str255 lwfn_file_name,
734 AsscEntry* base_assoc;
740 lwfn_file_name[0] = 0;
742 fond = (FamRec*)fond_data;
743 assoc = (AsscEntry*)( fond_data + sizeof ( FamRec ) + 2 );
746 /* the maximum faces in a FOND is 48, size of StyleTable.indexes[] */
747 if ( 47 < face_index )
750 /* Let's do a little range checking before we get too excited here */
751 if ( face_index < count_faces_sfnt( fond_data ) )
753 assoc += face_index; /* add on the face_index! */
755 /* if the face at this index is not scalable,
756 fall back to the first one (old behavior) */
757 if ( EndianS16_BtoN( assoc->fontSize ) == 0 )
760 *sfnt_id = EndianS16_BtoN( assoc->fontID );
762 else if ( base_assoc->fontSize == 0 )
765 *sfnt_id = EndianS16_BtoN( base_assoc->fontID );
769 if ( EndianS32_BtoN( fond->ffStylOff ) )
771 unsigned char* p = (unsigned char*)fond_data;
773 unsigned short string_count;
775 unsigned char* names[64];
779 p += EndianS32_BtoN( fond->ffStylOff );
780 style = (StyleTable*)p;
781 p += sizeof ( StyleTable );
782 string_count = EndianS16_BtoN( *(short*)(p) );
783 p += sizeof ( short );
785 for ( i = 0; i < string_count && i < 64; 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] <= FT_MIN( string_count, 64 ) )
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 ) );
943 if ( FT_Err_Ok == err )
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_ALLOC( 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_ALLOC( 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 ( FT_Err_Ok == error && NULL != 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 ) );
1282 if ( FT_Err_Ok == error )
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 ) );
1318 if ( FT_Err_Ok == error )
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 && FT_Err_Ok != 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 != 0 || *aface != NULL )
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 != 0 || *aface != NULL )
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 != 0 || *aface != NULL )
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 */