Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /***************************************************************************/
  2. /*                                                                         */
  3. /*  ftmac.c                                                                */
  4. /*                                                                         */
  5. /*    Mac FOND support.  Written by just@letterror.com.                    */
  6. /*  Heavily Fixed by mpsuzuki, George Williams and Sean McBride            */
  7. /*                                                                         */
  8. /*  Copyright 1996-2008, 2013 by                                           */
  9. /*  Just van Rossum, David Turner, Robert Wilhelm, and Werner Lemberg.     */
  10. /*                                                                         */
  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.                                        */
  16. /*                                                                         */
  17. /***************************************************************************/
  18.  
  19.  
  20.   /*
  21.     Notes
  22.  
  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.
  26.  
  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.
  32.  
  33.     The Mac FOND support works roughly like this:
  34.  
  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.
  40.  
  41.     - Read the FOND resource into memory, then check whether there is a
  42.       TrueType font and/or(!) a Type 1 font available.
  43.  
  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...)
  52.  
  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().
  56.  
  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
  61.       available.
  62.   */
  63.  
  64.  
  65. #include <ft2build.h>
  66. #include FT_FREETYPE_H
  67. #include FT_TRUETYPE_TAGS_H
  68. #include FT_INTERNAL_STREAM_H
  69. #include "ftbase.h"
  70.  
  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
  76. #undef  OS_INLINE
  77. #define OS_INLINE   static __inline__
  78. #endif
  79. #include <CoreServices/CoreServices.h>
  80. #include <ApplicationServices/ApplicationServices.h>
  81. #include <sys/syslimits.h> /* PATH_MAX */
  82. #else
  83. #include <Resources.h>
  84. #include <Fonts.h>
  85. #include <Endian.h>
  86. #include <Errors.h>
  87. #include <Files.h>
  88. #include <TextUtils.h>
  89. #endif
  90.  
  91. #ifndef PATH_MAX
  92. #define PATH_MAX 1024 /* same with Mac OS X's syslimits.h */
  93. #endif
  94.  
  95. #if defined( __MWERKS__ ) && !TARGET_RT_MAC_MACHO
  96. #include <FSp_fopen.h>
  97. #endif
  98.  
  99. #define FT_DEPRECATED_ATTRIBUTE
  100.  
  101. #include FT_MAC_H
  102.  
  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
  109.  
  110.  
  111.   /* FSSpec functions are deprecated since Mac OS X 10.4 */
  112. #ifndef HAVE_FSSPEC
  113. #if TARGET_API_MAC_OS8 || TARGET_API_MAC_CARBON
  114. #define HAVE_FSSPEC  1
  115. #else
  116. #define HAVE_FSSPEC  0
  117. #endif
  118. #endif
  119.  
  120.   /* most FSRef functions were introduced since Mac OS 9 */
  121. #ifndef HAVE_FSREF
  122. #if TARGET_API_MAC_OSX
  123. #define HAVE_FSREF  1
  124. #else
  125. #define HAVE_FSREF  0
  126. #endif
  127. #endif
  128.  
  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
  133. #else
  134. #define HAVE_QUICKDRAW_CARBON  0
  135. #endif
  136. #endif
  137.  
  138.   /* AppleTypeService is available since Mac OS X */
  139. #ifndef HAVE_ATS
  140. #if TARGET_API_MAC_OSX
  141. #define HAVE_ATS  1
  142. #ifndef kATSOptionFlagsUnRestrictedScope /* since Mac OS X 10.1 */
  143. #define kATSOptionFlagsUnRestrictedScope kATSOptionFlagsDefault
  144. #endif
  145. #else
  146. #define HAVE_ATS  0
  147. #endif
  148. #endif
  149.  
  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
  158. #else
  159. #define HAVE_TYPE_RESOURCE_INDEX 1
  160. #endif
  161. #endif /* !HAVE_TYPE_RESOURCE_INDEX */
  162.  
  163. #if ( HAVE_TYPE_RESOURCE_INDEX == 0 )
  164. typedef short ResourceIndex;
  165. #endif
  166.  
  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). */
  170. #ifndef PREFER_LWFN
  171. #define PREFER_LWFN  1
  172. #endif
  173.  
  174. #ifdef FT_MACINTOSH
  175.  
  176. #if !HAVE_QUICKDRAW_CARBON  /* QuickDraw is deprecated since Mac OS X 10.4 */
  177.  
  178.   FT_EXPORT_DEF( FT_Error )
  179.   FT_GetFile_From_Mac_Name( const char*  fontName,
  180.                             FSSpec*      pathSpec,
  181.                             FT_Long*     face_index )
  182.   {
  183.     FT_UNUSED( fontName );
  184.     FT_UNUSED( pathSpec );
  185.     FT_UNUSED( face_index );
  186.  
  187.     return FT_THROW( Unimplemented_Feature );
  188.   }
  189.  
  190. #else
  191.  
  192.   FT_EXPORT_DEF( FT_Error )
  193.   FT_GetFile_From_Mac_Name( const char*  fontName,
  194.                             FSSpec*      pathSpec,
  195.                             FT_Long*     face_index )
  196.   {
  197.     OptionBits            options = kFMUseGlobalScopeOption;
  198.  
  199.     FMFontFamilyIterator  famIter;
  200.     OSStatus              status = FMCreateFontFamilyIterator( NULL, NULL,
  201.                                                                options,
  202.                                                                &famIter );
  203.     FMFont                the_font = 0;
  204.     FMFontFamily          family   = 0;
  205.  
  206.  
  207.     *face_index = 0;
  208.     while ( status == 0 && !the_font )
  209.     {
  210.       status = FMGetNextFontFamily( &famIter, &family );
  211.       if ( status == 0 )
  212.       {
  213.         int                           stat2;
  214.         FMFontFamilyInstanceIterator  instIter;
  215.         Str255                        famNameStr;
  216.         char                          famName[256];
  217.  
  218.  
  219.         /* get the family name */
  220.         FMGetFontFamilyName( family, famNameStr );
  221.         CopyPascalStringToC( famNameStr, famName );
  222.  
  223.         /* iterate through the styles */
  224.         FMCreateFontFamilyInstanceIterator( family, &instIter );
  225.  
  226.         *face_index = 0;
  227.         stat2       = 0;
  228.  
  229.         while ( stat2 == 0 && !the_font )
  230.         {
  231.           FMFontStyle  style;
  232.           FMFontSize   size;
  233.           FMFont       font;
  234.  
  235.  
  236.           stat2 = FMGetNextFontFamilyInstance( &instIter, &font,
  237.                                                &style, &size );
  238.           if ( stat2 == 0 && size == 0 )
  239.           {
  240.             char  fullName[256];
  241.  
  242.  
  243.             /* build up a complete face name */
  244.             ft_strcpy( fullName, famName );
  245.             if ( style & bold )
  246.               ft_strcat( fullName, " Bold" );
  247.             if ( style & italic )
  248.               ft_strcat( fullName, " Italic" );
  249.  
  250.             /* compare with the name we are looking for */
  251.             if ( ft_strcmp( fullName, fontName ) == 0 )
  252.             {
  253.               /* found it! */
  254.               the_font = font;
  255.             }
  256.             else
  257.               ++(*face_index);
  258.           }
  259.         }
  260.  
  261.         FMDisposeFontFamilyInstanceIterator( &instIter );
  262.       }
  263.     }
  264.  
  265.     FMDisposeFontFamilyIterator( &famIter );
  266.  
  267.     if ( the_font )
  268.     {
  269.       FMGetFontContainer( the_font, pathSpec );
  270.       return FT_Err_Ok;
  271.     }
  272.     else
  273.       return FT_THROW( Unknown_File_Format );
  274.   }
  275.  
  276. #endif /* HAVE_QUICKDRAW_CARBON */
  277.  
  278.  
  279. #if HAVE_ATS
  280.  
  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.                                  */
  286.   static OSStatus
  287.   FT_ATSFontGetFileReference( ATSFontRef  ats_font_id,
  288.                               FSRef*      ats_font_ref )
  289.   {
  290.     OSStatus  err;
  291.  
  292. #if !defined( MAC_OS_X_VERSION_10_5 ) || \
  293.     MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_5
  294.     FSSpec    spec;
  295.  
  296.  
  297.     err = ATSFontGetFileSpecification( ats_font_id, &spec );
  298.     if ( noErr == err )
  299.       err = FSpMakeFSRef( &spec, ats_font_ref );
  300. #else
  301.     err = ATSFontGetFileReference( ats_font_id, ats_font_ref );
  302. #endif
  303.  
  304.     return err;
  305.   }
  306.  
  307.  
  308.   static FT_Error
  309.   FT_GetFileRef_From_Mac_ATS_Name( const char*  fontName,
  310.                                    FSRef*       ats_font_ref,
  311.                                    FT_Long*     face_index )
  312.   {
  313.     CFStringRef  cf_fontName;
  314.     ATSFontRef   ats_font_id;
  315.  
  316.  
  317.     *face_index = 0;
  318.  
  319.     cf_fontName = CFStringCreateWithCString( NULL, fontName,
  320.                                              kCFStringEncodingMacRoman );
  321.     ats_font_id = ATSFontFindFromName( cf_fontName,
  322.                                        kATSOptionFlagsUnRestrictedScope );
  323.     CFRelease( cf_fontName );
  324.  
  325.     if ( ats_font_id == 0 || ats_font_id == 0xFFFFFFFFUL )
  326.       return FT_THROW( Unknown_File_Format );
  327.  
  328.     if ( noErr != FT_ATSFontGetFileReference( ats_font_id, ats_font_ref ) )
  329.       return FT_THROW( Unknown_File_Format );
  330.  
  331.     /* face_index calculation by searching preceding fontIDs */
  332.     /* with same FSRef                                       */
  333.     {
  334.       ATSFontRef  id2 = ats_font_id - 1;
  335.       FSRef       ref2;
  336.  
  337.  
  338.       while ( id2 > 0 )
  339.       {
  340.         if ( noErr != FT_ATSFontGetFileReference( id2, &ref2 ) )
  341.           break;
  342.         if ( noErr != FSCompareFSRefs( ats_font_ref, &ref2 ) )
  343.           break;
  344.  
  345.         id2--;
  346.       }
  347.       *face_index = ats_font_id - ( id2 + 1 );
  348.     }
  349.  
  350.     return FT_Err_Ok;
  351.   }
  352.  
  353. #endif
  354.  
  355. #if !HAVE_ATS
  356.  
  357.   FT_EXPORT_DEF( FT_Error )
  358.   FT_GetFilePath_From_Mac_ATS_Name( const char*  fontName,
  359.                                     UInt8*       path,
  360.                                     UInt32       maxPathSize,
  361.                                     FT_Long*     face_index )
  362.   {
  363.     FT_UNUSED( fontName );
  364.     FT_UNUSED( path );
  365.     FT_UNUSED( maxPathSize );
  366.     FT_UNUSED( face_index );
  367.  
  368.     return FT_THROW( Unimplemented_Feature );
  369.   }
  370.  
  371. #else
  372.  
  373.   FT_EXPORT_DEF( FT_Error )
  374.   FT_GetFilePath_From_Mac_ATS_Name( const char*  fontName,
  375.                                     UInt8*       path,
  376.                                     UInt32       maxPathSize,
  377.                                     FT_Long*     face_index )
  378.   {
  379.     FSRef     ref;
  380.     FT_Error  err;
  381.  
  382.  
  383.     err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
  384.     if ( FT_Err_Ok != err )
  385.       return err;
  386.  
  387.     if ( noErr != FSRefMakePath( &ref, path, maxPathSize ) )
  388.       return FT_THROW( Unknown_File_Format );
  389.  
  390.     return FT_Err_Ok;
  391.   }
  392.  
  393. #endif /* HAVE_ATS */
  394.  
  395.  
  396. #if !HAVE_FSSPEC || !HAVE_ATS
  397.  
  398.   FT_EXPORT_DEF( FT_Error )
  399.   FT_GetFile_From_Mac_ATS_Name( const char*  fontName,
  400.                                 FSSpec*      pathSpec,
  401.                                 FT_Long*     face_index )
  402.   {
  403.     FT_UNUSED( fontName );
  404.     FT_UNUSED( pathSpec );
  405.     FT_UNUSED( face_index );
  406.  
  407.     return FT_THROW( Unimplemented_Feature );
  408.   }
  409.  
  410. #else
  411.  
  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,
  415.                                 FSSpec*      pathSpec,
  416.                                 FT_Long*     face_index )
  417.   {
  418.     FSRef     ref;
  419.     FT_Error  err;
  420.  
  421.  
  422.     err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
  423.     if ( FT_Err_Ok != err )
  424.       return err;
  425.  
  426.     if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone, NULL, NULL,
  427.                                     pathSpec, NULL ) )
  428.       return FT_THROW( Unknown_File_Format );
  429.  
  430.     return FT_Err_Ok;
  431.   }
  432.  
  433. #endif
  434.  
  435.  
  436. #if defined( __MWERKS__ ) && !TARGET_RT_MAC_MACHO
  437.  
  438. #define STREAM_FILE( stream )  ( (FT_FILE*)stream->descriptor.pointer )
  439.  
  440.  
  441.   FT_CALLBACK_DEF( void )
  442.   ft_FSp_stream_close( FT_Stream  stream )
  443.   {
  444.     ft_fclose( STREAM_FILE( stream ) );
  445.  
  446.     stream->descriptor.pointer = NULL;
  447.     stream->size               = 0;
  448.     stream->base               = 0;
  449.   }
  450.  
  451.  
  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 )
  457.   {
  458.     FT_FILE*  file;
  459.  
  460.  
  461.     file = STREAM_FILE( stream );
  462.  
  463.     ft_fseek( file, offset, SEEK_SET );
  464.  
  465.     return (unsigned long)ft_fread( buffer, 1, count, file );
  466.   }
  467.  
  468. #endif  /* __MWERKS__ && !TARGET_RT_MAC_MACHO */
  469.  
  470.  
  471. #if HAVE_FSSPEC && !HAVE_FSREF
  472.  
  473.   /* isDirectory is a dummy to synchronize API with FSPathMakeRef() */
  474.   static OSErr
  475.   FT_FSPathMakeSpec( const UInt8*  pathname,
  476.                      FSSpec*       spec_p,
  477.                      Boolean       isDirectory )
  478.   {
  479.     const char  *p, *q;
  480.     short       vRefNum;
  481.     long        dirID;
  482.     Str255      nodeName;
  483.     OSErr       err;
  484.     FT_UNUSED( isDirectory );
  485.  
  486.  
  487.     p = q = (const char *)pathname;
  488.     dirID   = 0;
  489.     vRefNum = 0;
  490.  
  491.     while ( 1 )
  492.     {
  493.       int  len = ft_strlen( p );
  494.  
  495.  
  496.       if ( len > 255 )
  497.         len = 255;
  498.  
  499.       q = p + len;
  500.  
  501.       if ( q == p )
  502.         return 0;
  503.  
  504.       if ( 255 < ft_strlen( (char *)pathname ) )
  505.       {
  506.         while ( p < q && *q != ':' )
  507.           q--;
  508.       }
  509.  
  510.       if ( p < q )
  511.         *(char *)nodeName = q - p;
  512.       else if ( ft_strlen( p ) < 256 )
  513.         *(char *)nodeName = ft_strlen( p );
  514.       else
  515.         return errFSNameTooLong;
  516.  
  517.       ft_strncpy( (char *)nodeName + 1, (char *)p, *(char *)nodeName );
  518.       err = FSMakeFSSpec( vRefNum, dirID, nodeName, spec_p );
  519.       if ( err || '\0' == *q )
  520.         return err;
  521.  
  522.       vRefNum = spec_p->vRefNum;
  523.       dirID   = spec_p->parID;
  524.  
  525.       p = q;
  526.     }
  527.   }
  528.  
  529.  
  530.   static OSErr
  531.   FT_FSpMakePath( const FSSpec*  spec_p,
  532.                   UInt8*         path,
  533.                   UInt32         maxPathSize )
  534.   {
  535.     OSErr   err;
  536.     FSSpec  spec = *spec_p;
  537.     short   vRefNum;
  538.     long    dirID;
  539.     Str255  parDir_name;
  540.  
  541.  
  542.     FT_MEM_SET( path, 0, maxPathSize );
  543.     while ( 1 )
  544.     {
  545.       int             child_namelen = ft_strlen( (char *)path );
  546.       unsigned char   node_namelen  = spec.name[0];
  547.       unsigned char*  node_name     = spec.name + 1;
  548.  
  549.  
  550.       if ( node_namelen + child_namelen > maxPathSize )
  551.         return errFSNameTooLong;
  552.  
  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] = ':';
  557.  
  558.       vRefNum        = spec.vRefNum;
  559.       dirID          = spec.parID;
  560.       parDir_name[0] = '\0';
  561.       err = FSMakeFSSpec( vRefNum, dirID, parDir_name, &spec );
  562.       if ( noErr != err || dirID == spec.parID )
  563.         break;
  564.     }
  565.     return noErr;
  566.   }
  567.  
  568. #endif /* HAVE_FSSPEC && !HAVE_FSREF */
  569.  
  570.  
  571.   static OSErr
  572.   FT_FSPathMakeRes( const UInt8*    pathname,
  573.                     ResFileRefNum*  res )
  574.   {
  575.  
  576. #if HAVE_FSREF
  577.  
  578.     OSErr  err;
  579.     FSRef  ref;
  580.  
  581.  
  582.     if ( noErr != FSPathMakeRef( pathname, &ref, FALSE ) )
  583.       return FT_THROW( Cannot_Open_Resource );
  584.  
  585.     /* at present, no support for dfont format */
  586.     err = FSOpenResourceFile( &ref, 0, NULL, fsRdPerm, res );
  587.     if ( noErr == err )
  588.       return err;
  589.  
  590.     /* fallback to original resource-fork font */
  591.     *res = FSOpenResFile( &ref, fsRdPerm );
  592.     err  = ResError();
  593.  
  594. #else
  595.  
  596.     OSErr   err;
  597.     FSSpec  spec;
  598.  
  599.  
  600.     if ( noErr != FT_FSPathMakeSpec( pathname, &spec, FALSE ) )
  601.       return FT_THROW( Cannot_Open_Resource );
  602.  
  603.     /* at present, no support for dfont format without FSRef */
  604.     /* (see above), try original resource-fork font          */
  605.     *res = FSpOpenResFile( &spec, fsRdPerm );
  606.     err  = ResError();
  607.  
  608. #endif /* HAVE_FSREF */
  609.  
  610.     return err;
  611.   }
  612.  
  613.  
  614.   /* Return the file type for given pathname */
  615.   static OSType
  616.   get_file_type_from_path( const UInt8*  pathname )
  617.   {
  618.  
  619. #if HAVE_FSREF
  620.  
  621.     FSRef          ref;
  622.     FSCatalogInfo  info;
  623.  
  624.  
  625.     if ( noErr != FSPathMakeRef( pathname, &ref, FALSE ) )
  626.       return ( OSType ) 0;
  627.  
  628.     if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoFinderInfo, &info,
  629.                                     NULL, NULL, NULL ) )
  630.       return ( OSType ) 0;
  631.  
  632.     return ((FInfo *)(info.finderInfo))->fdType;
  633.  
  634. #else
  635.  
  636.     FSSpec  spec;
  637.     FInfo   finfo;
  638.  
  639.  
  640.     if ( noErr != FT_FSPathMakeSpec( pathname, &spec, FALSE ) )
  641.       return ( OSType ) 0;
  642.  
  643.     if ( noErr != FSpGetFInfo( &spec, &finfo ) )
  644.       return ( OSType ) 0;
  645.  
  646.     return finfo.fdType;
  647.  
  648. #endif /* HAVE_FSREF */
  649.  
  650.   }
  651.  
  652.  
  653.   /* Given a PostScript font name, create the Macintosh LWFN file name. */
  654.   static void
  655.   create_lwfn_name( char*   ps_name,
  656.                     Str255  lwfn_file_name )
  657.   {
  658.     int       max = 5, count = 0;
  659.     FT_Byte*  p = lwfn_file_name;
  660.     FT_Byte*  q = (FT_Byte*)ps_name;
  661.  
  662.  
  663.     lwfn_file_name[0] = 0;
  664.  
  665.     while ( *q )
  666.     {
  667.       if ( ft_isupper( *q ) )
  668.       {
  669.         if ( count )
  670.           max = 3;
  671.         count = 0;
  672.       }
  673.       if ( count < max && ( ft_isalnum( *q ) || *q == '_' ) )
  674.       {
  675.         *++p = *q;
  676.         lwfn_file_name[0]++;
  677.         count++;
  678.       }
  679.       q++;
  680.     }
  681.   }
  682.  
  683.  
  684.   static short
  685.   count_faces_sfnt( char*  fond_data )
  686.   {
  687.     /* The count is 1 greater than the value in the FOND.  */
  688.     /* Isn't that cute? :-)                                */
  689.  
  690.     return EndianS16_BtoN( *( (short*)( fond_data +
  691.                                         sizeof ( FamRec ) ) ) ) + 1;
  692.   }
  693.  
  694.  
  695.   static short
  696.   count_faces_scalable( char*  fond_data )
  697.   {
  698.     AsscEntry*  assoc;
  699.     short       i, face, face_all;
  700.  
  701.  
  702.     face_all = EndianS16_BtoN( *( (short *)( fond_data +
  703.                                              sizeof ( FamRec ) ) ) ) + 1;
  704.     assoc    = (AsscEntry*)( fond_data + sizeof ( FamRec ) + 2 );
  705.     face     = 0;
  706.  
  707.     for ( i = 0; i < face_all; i++ )
  708.     {
  709.       if ( 0 == EndianS16_BtoN( assoc[i].fontSize ) )
  710.         face++;
  711.     }
  712.     return face;
  713.   }
  714.  
  715.  
  716.   /* Look inside the FOND data, answer whether there should be an SFNT
  717.      resource, and answer the name of a possible LWFN Type 1 file.
  718.  
  719.      Thanks to Paul Miller (paulm@profoundeffects.com) for the fix
  720.      to load a face OTHER than the first one in the FOND!
  721.   */
  722.  
  723.   static void
  724.   parse_fond( char*   fond_data,
  725.               short*  have_sfnt,
  726.               ResID*  sfnt_id,
  727.               Str255  lwfn_file_name,
  728.               short   face_index )
  729.   {
  730.     AsscEntry*  assoc;
  731.     AsscEntry*  base_assoc;
  732.     FamRec*     fond;
  733.  
  734.  
  735.     *sfnt_id          = 0;
  736.     *have_sfnt        = 0;
  737.     lwfn_file_name[0] = 0;
  738.  
  739.     fond       = (FamRec*)fond_data;
  740.     assoc      = (AsscEntry*)( fond_data + sizeof ( FamRec ) + 2 );
  741.     base_assoc = assoc;
  742.  
  743.     /* the maximum faces in a FOND is 48, size of StyleTable.indexes[] */
  744.     if ( 47 < face_index )
  745.       return;
  746.  
  747.     /* Let's do a little range checking before we get too excited here */
  748.     if ( face_index < count_faces_sfnt( fond_data ) )
  749.     {
  750.       assoc += face_index;        /* add on the face_index! */
  751.  
  752.       /* if the face at this index is not scalable,
  753.          fall back to the first one (old behavior) */
  754.       if ( EndianS16_BtoN( assoc->fontSize ) == 0 )
  755.       {
  756.         *have_sfnt = 1;
  757.         *sfnt_id   = EndianS16_BtoN( assoc->fontID );
  758.       }
  759.       else if ( base_assoc->fontSize == 0 )
  760.       {
  761.         *have_sfnt = 1;
  762.         *sfnt_id   = EndianS16_BtoN( base_assoc->fontID );
  763.       }
  764.     }
  765.  
  766.     if ( EndianS32_BtoN( fond->ffStylOff ) )
  767.     {
  768.       unsigned char*  p = (unsigned char*)fond_data;
  769.       StyleTable*     style;
  770.       unsigned short  string_count;
  771.       char            ps_name[256];
  772.       unsigned char*  names[64];
  773.       int             i;
  774.  
  775.  
  776.       p += EndianS32_BtoN( fond->ffStylOff );
  777.       style = (StyleTable*)p;
  778.       p += sizeof ( StyleTable );
  779.       string_count = EndianS16_BtoN( *(short*)(p) );
  780.       p += sizeof ( short );
  781.  
  782.       for ( i = 0; i < string_count && i < 64; i++ )
  783.       {
  784.         names[i] = p;
  785.         p       += names[i][0];
  786.         p++;
  787.       }
  788.  
  789.       {
  790.         size_t  ps_name_len = (size_t)names[0][0];
  791.  
  792.  
  793.         if ( ps_name_len != 0 )
  794.         {
  795.           ft_memcpy(ps_name, names[0] + 1, ps_name_len);
  796.           ps_name[ps_name_len] = 0;
  797.         }
  798.         if ( style->indexes[face_index] > 1 &&
  799.              style->indexes[face_index] <= FT_MIN( string_count, 64 ) )
  800.         {
  801.           unsigned char*  suffixes = names[style->indexes[face_index] - 1];
  802.  
  803.  
  804.           for ( i = 1; i <= suffixes[0]; i++ )
  805.           {
  806.             unsigned char*  s;
  807.             size_t          j = suffixes[i] - 1;
  808.  
  809.  
  810.             if ( j < string_count && ( s = names[j] ) != NULL )
  811.             {
  812.               size_t  s_len = (size_t)s[0];
  813.  
  814.  
  815.               if ( s_len != 0 && ps_name_len + s_len < sizeof ( ps_name ) )
  816.               {
  817.                 ft_memcpy( ps_name + ps_name_len, s + 1, s_len );
  818.                 ps_name_len += s_len;
  819.                 ps_name[ps_name_len] = 0;
  820.               }
  821.             }
  822.           }
  823.         }
  824.       }
  825.  
  826.       create_lwfn_name( ps_name, lwfn_file_name );
  827.     }
  828.   }
  829.  
  830.  
  831.   static  FT_Error
  832.   lookup_lwfn_by_fond( const UInt8*      path_fond,
  833.                        ConstStr255Param  base_lwfn,
  834.                        UInt8*            path_lwfn,
  835.                        int               path_size )
  836.   {
  837.  
  838. #if HAVE_FSREF
  839.  
  840.     FSRef  ref, par_ref;
  841.     int    dirname_len;
  842.  
  843.  
  844.     /* Pathname for FSRef can be in various formats: HFS, HFS+, and POSIX. */
  845.     /* We should not extract parent directory by string manipulation.      */
  846.  
  847.     if ( noErr != FSPathMakeRef( path_fond, &ref, FALSE ) )
  848.       return FT_THROW( Invalid_Argument );
  849.  
  850.     if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone,
  851.                                     NULL, NULL, NULL, &par_ref ) )
  852.       return FT_THROW( Invalid_Argument );
  853.  
  854.     if ( noErr != FSRefMakePath( &par_ref, path_lwfn, path_size ) )
  855.       return FT_THROW( Invalid_Argument );
  856.  
  857.     if ( ft_strlen( (char *)path_lwfn ) + 1 + base_lwfn[0] > path_size )
  858.       return FT_THROW( Invalid_Argument );
  859.  
  860.     /* now we have absolute dirname in path_lwfn */
  861.     if ( path_lwfn[0] == '/' )
  862.       ft_strcat( (char *)path_lwfn, "/" );
  863.     else
  864.       ft_strcat( (char *)path_lwfn, ":" );
  865.  
  866.     dirname_len = ft_strlen( (char *)path_lwfn );
  867.     ft_strcat( (char *)path_lwfn, (char *)base_lwfn + 1 );
  868.     path_lwfn[dirname_len + base_lwfn[0]] = '\0';
  869.  
  870.     if ( noErr != FSPathMakeRef( path_lwfn, &ref, FALSE ) )
  871.       return FT_THROW( Cannot_Open_Resource );
  872.  
  873.     if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone,
  874.                                     NULL, NULL, NULL, NULL ) )
  875.       return FT_THROW( Cannot_Open_Resource );
  876.  
  877.     return FT_Err_Ok;
  878.  
  879. #else
  880.  
  881.     int     i;
  882.     FSSpec  spec;
  883.  
  884.  
  885.     /* pathname for FSSpec is always HFS format */
  886.     if ( ft_strlen( (char *)path_fond ) > path_size )
  887.       return FT_THROW( Invalid_Argument );
  888.  
  889.     ft_strcpy( (char *)path_lwfn, (char *)path_fond );
  890.  
  891.     i = ft_strlen( (char *)path_lwfn ) - 1;
  892.     while ( i > 0 && ':' != path_lwfn[i] )
  893.       i--;
  894.  
  895.     if ( i + 1 + base_lwfn[0] > path_size )
  896.       return FT_THROW( Invalid_Argument );
  897.  
  898.     if ( ':' == path_lwfn[i] )
  899.     {
  900.       ft_strcpy( (char *)path_lwfn + i + 1, (char *)base_lwfn + 1 );
  901.       path_lwfn[i + 1 + base_lwfn[0]] = '\0';
  902.     }
  903.     else
  904.     {
  905.       ft_strcpy( (char *)path_lwfn, (char *)base_lwfn + 1 );
  906.       path_lwfn[base_lwfn[0]] = '\0';
  907.     }
  908.  
  909.     if ( noErr != FT_FSPathMakeSpec( path_lwfn, &spec, FALSE ) )
  910.       return FT_THROW( Cannot_Open_Resource );
  911.  
  912.     return FT_Err_Ok;
  913.  
  914. #endif /* HAVE_FSREF */
  915.  
  916.   }
  917.  
  918.  
  919.   static short
  920.   count_faces( Handle        fond,
  921.                const UInt8*  pathname )
  922.   {
  923.     ResID     sfnt_id;
  924.     short     have_sfnt, have_lwfn;
  925.     Str255    lwfn_file_name;
  926.     UInt8     buff[PATH_MAX];
  927.     FT_Error  err;
  928.     short     num_faces;
  929.  
  930.  
  931.     have_sfnt = have_lwfn = 0;
  932.  
  933.     HLock( fond );
  934.     parse_fond( *fond, &have_sfnt, &sfnt_id, lwfn_file_name, 0 );
  935.  
  936.     if ( lwfn_file_name[0] )
  937.     {
  938.       err = lookup_lwfn_by_fond( pathname, lwfn_file_name,
  939.                                  buff, sizeof ( buff )  );
  940.       if ( FT_Err_Ok == err )
  941.         have_lwfn = 1;
  942.     }
  943.  
  944.     if ( have_lwfn && ( !have_sfnt || PREFER_LWFN ) )
  945.       num_faces = 1;
  946.     else
  947.       num_faces = count_faces_scalable( *fond );
  948.  
  949.     HUnlock( fond );
  950.     return num_faces;
  951.   }
  952.  
  953.  
  954.   /* Read Type 1 data from the POST resources inside the LWFN file,
  955.      return a PFB buffer.  This is somewhat convoluted because the FT2
  956.      PFB parser wants the ASCII header as one chunk, and the LWFN
  957.      chunks are often not organized that way, so we glue chunks
  958.      of the same type together. */
  959.   static FT_Error
  960.   read_lwfn( FT_Memory      memory,
  961.              ResFileRefNum  res,
  962.              FT_Byte**      pfb_data,
  963.              FT_ULong*      size )
  964.   {
  965.     FT_Error       error = FT_Err_Ok;
  966.     ResID          res_id;
  967.     unsigned char  *buffer, *p, *size_p = NULL;
  968.     FT_ULong       total_size = 0;
  969.     FT_ULong       old_total_size = 0;
  970.     FT_ULong       post_size, pfb_chunk_size;
  971.     Handle         post_data;
  972.     char           code, last_code;
  973.  
  974.  
  975.     UseResFile( res );
  976.  
  977.     /* First pass: load all POST resources, and determine the size of */
  978.     /* the output buffer.                                             */
  979.     res_id    = 501;
  980.     last_code = -1;
  981.  
  982.     for (;;)
  983.     {
  984.       post_data = Get1Resource( TTAG_POST, res_id++ );
  985.       if ( post_data == NULL )
  986.         break;  /* we are done */
  987.  
  988.       code = (*post_data)[0];
  989.  
  990.       if ( code != last_code )
  991.       {
  992.         if ( code == 5 )
  993.           total_size += 2; /* just the end code */
  994.         else
  995.           total_size += 6; /* code + 4 bytes chunk length */
  996.       }
  997.  
  998.       total_size += GetHandleSize( post_data ) - 2;
  999.       last_code = code;
  1000.  
  1001.       /* detect integer overflows */
  1002.       if ( total_size < old_total_size )
  1003.       {
  1004.         error = FT_ERR( Array_Too_Large );
  1005.         goto Error;
  1006.       }
  1007.  
  1008.       old_total_size = total_size;
  1009.     }
  1010.  
  1011.     if ( FT_ALLOC( buffer, (FT_Long)total_size ) )
  1012.       goto Error;
  1013.  
  1014.     /* Second pass: append all POST data to the buffer, add PFB fields. */
  1015.     /* Glue all consecutive chunks of the same type together.           */
  1016.     p              = buffer;
  1017.     res_id         = 501;
  1018.     last_code      = -1;
  1019.     pfb_chunk_size = 0;
  1020.  
  1021.     for (;;)
  1022.     {
  1023.       post_data = Get1Resource( TTAG_POST, res_id++ );
  1024.       if ( post_data == NULL )
  1025.         break;  /* we are done */
  1026.  
  1027.       post_size = (FT_ULong)GetHandleSize( post_data ) - 2;
  1028.       code = (*post_data)[0];
  1029.  
  1030.       if ( code != last_code )
  1031.       {
  1032.         if ( last_code != -1 )
  1033.         {
  1034.           /* we are done adding a chunk, fill in the size field */
  1035.           if ( size_p != NULL )
  1036.           {
  1037.             *size_p++ = (FT_Byte)(   pfb_chunk_size         & 0xFF );
  1038.             *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 8  ) & 0xFF );
  1039.             *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 16 ) & 0xFF );
  1040.             *size_p++ = (FT_Byte)( ( pfb_chunk_size >> 24 ) & 0xFF );
  1041.           }
  1042.           pfb_chunk_size = 0;
  1043.         }
  1044.  
  1045.         *p++ = 0x80;
  1046.         if ( code == 5 )
  1047.           *p++ = 0x03;  /* the end */
  1048.         else if ( code == 2 )
  1049.           *p++ = 0x02;  /* binary segment */
  1050.         else
  1051.           *p++ = 0x01;  /* ASCII segment */
  1052.  
  1053.         if ( code != 5 )
  1054.         {
  1055.           size_p = p;   /* save for later */
  1056.           p += 4;       /* make space for size field */
  1057.         }
  1058.       }
  1059.  
  1060.       ft_memcpy( p, *post_data + 2, post_size );
  1061.       pfb_chunk_size += post_size;
  1062.       p += post_size;
  1063.       last_code = code;
  1064.     }
  1065.  
  1066.     *pfb_data = buffer;
  1067.     *size = total_size;
  1068.  
  1069.   Error:
  1070.     CloseResFile( res );
  1071.     return error;
  1072.   }
  1073.  
  1074.  
  1075.   /* Create a new FT_Face from a file spec to an LWFN file. */
  1076.   static FT_Error
  1077.   FT_New_Face_From_LWFN( FT_Library    library,
  1078.                          const UInt8*  pathname,
  1079.                          FT_Long       face_index,
  1080.                          FT_Face*      aface )
  1081.   {
  1082.     FT_Byte*       pfb_data;
  1083.     FT_ULong       pfb_size;
  1084.     FT_Error       error;
  1085.     ResFileRefNum  res;
  1086.  
  1087.  
  1088.     if ( noErr != FT_FSPathMakeRes( pathname, &res ) )
  1089.       return FT_THROW( Cannot_Open_Resource );
  1090.  
  1091.     pfb_data = NULL;
  1092.     pfb_size = 0;
  1093.     error = read_lwfn( library->memory, res, &pfb_data, &pfb_size );
  1094.     CloseResFile( res ); /* PFB is already loaded, useless anymore */
  1095.     if ( error )
  1096.       return error;
  1097.  
  1098.     return open_face_from_buffer( library,
  1099.                                   pfb_data,
  1100.                                   pfb_size,
  1101.                                   face_index,
  1102.                                   "type1",
  1103.                                   aface );
  1104.   }
  1105.  
  1106.  
  1107.   /* Create a new FT_Face from an SFNT resource, specified by res ID. */
  1108.   static FT_Error
  1109.   FT_New_Face_From_SFNT( FT_Library  library,
  1110.                          ResID       sfnt_id,
  1111.                          FT_Long     face_index,
  1112.                          FT_Face*    aface )
  1113.   {
  1114.     Handle     sfnt = NULL;
  1115.     FT_Byte*   sfnt_data;
  1116.     size_t     sfnt_size;
  1117.     FT_Error   error  = FT_Err_Ok;
  1118.     FT_Memory  memory = library->memory;
  1119.     int        is_cff, is_sfnt_ps;
  1120.  
  1121.  
  1122.     sfnt = GetResource( TTAG_sfnt, sfnt_id );
  1123.     if ( sfnt == NULL )
  1124.       return FT_THROW( Invalid_Handle );
  1125.  
  1126.     sfnt_size = (FT_ULong)GetHandleSize( sfnt );
  1127.     if ( FT_ALLOC( sfnt_data, (FT_Long)sfnt_size ) )
  1128.     {
  1129.       ReleaseResource( sfnt );
  1130.       return error;
  1131.     }
  1132.  
  1133.     HLock( sfnt );
  1134.     ft_memcpy( sfnt_data, *sfnt, sfnt_size );
  1135.     HUnlock( sfnt );
  1136.     ReleaseResource( sfnt );
  1137.  
  1138.     is_cff     = sfnt_size > 4 && !ft_memcmp( sfnt_data, "OTTO", 4 );
  1139.     is_sfnt_ps = sfnt_size > 4 && !ft_memcmp( sfnt_data, "typ1", 4 );
  1140.  
  1141.     if ( is_sfnt_ps )
  1142.     {
  1143.       FT_Stream  stream;
  1144.  
  1145.  
  1146.       if ( FT_NEW( stream ) )
  1147.         goto Try_OpenType;
  1148.  
  1149.       FT_Stream_OpenMemory( stream, sfnt_data, sfnt_size );
  1150.       if ( !open_face_PS_from_sfnt_stream( library,
  1151.                                            stream,
  1152.                                            face_index,
  1153.                                            0, NULL,
  1154.                                            aface ) )
  1155.       {
  1156.         FT_Stream_Close( stream );
  1157.         FT_FREE( stream );
  1158.         FT_FREE( sfnt_data );
  1159.         goto Exit;
  1160.       }
  1161.  
  1162.       FT_FREE( stream );
  1163.     }
  1164.   Try_OpenType:
  1165.     error = open_face_from_buffer( library,
  1166.                                    sfnt_data,
  1167.                                    sfnt_size,
  1168.                                    face_index,
  1169.                                    is_cff ? "cff" : "truetype",
  1170.                                    aface );
  1171.   Exit:
  1172.     return error;
  1173.   }
  1174.  
  1175.  
  1176.   /* Create a new FT_Face from a file spec to a suitcase file. */
  1177.   static FT_Error
  1178.   FT_New_Face_From_Suitcase( FT_Library    library,
  1179.                              const UInt8*  pathname,
  1180.                              FT_Long       face_index,
  1181.                              FT_Face*      aface )
  1182.   {
  1183.     FT_Error       error = FT_ERR( Cannot_Open_Resource );
  1184.     ResFileRefNum  res_ref;
  1185.     ResourceIndex  res_index;
  1186.     Handle         fond;
  1187.     short          num_faces_in_res;
  1188.  
  1189.  
  1190.     if ( noErr != FT_FSPathMakeRes( pathname, &res_ref ) )
  1191.       return FT_THROW( Cannot_Open_Resource );
  1192.  
  1193.     UseResFile( res_ref );
  1194.     if ( ResError() )
  1195.       return FT_THROW( Cannot_Open_Resource );
  1196.  
  1197.     num_faces_in_res = 0;
  1198.     for ( res_index = 1; ; ++res_index )
  1199.     {
  1200.       short  num_faces_in_fond;
  1201.  
  1202.  
  1203.       fond = Get1IndResource( TTAG_FOND, res_index );
  1204.       if ( ResError() )
  1205.         break;
  1206.  
  1207.       num_faces_in_fond  = count_faces( fond, pathname );
  1208.       num_faces_in_res  += num_faces_in_fond;
  1209.  
  1210.       if ( 0 <= face_index && face_index < num_faces_in_fond && error )
  1211.         error = FT_New_Face_From_FOND( library, fond, face_index, aface );
  1212.  
  1213.       face_index -= num_faces_in_fond;
  1214.     }
  1215.  
  1216.     CloseResFile( res_ref );
  1217.     if ( FT_Err_Ok == error && NULL != aface )
  1218.       (*aface)->num_faces = num_faces_in_res;
  1219.     return error;
  1220.   }
  1221.  
  1222.  
  1223.   /* documentation is in ftmac.h */
  1224.  
  1225.   FT_EXPORT_DEF( FT_Error )
  1226.   FT_New_Face_From_FOND( FT_Library  library,
  1227.                          Handle      fond,
  1228.                          FT_Long     face_index,
  1229.                          FT_Face*    aface )
  1230.   {
  1231.     short     have_sfnt, have_lwfn = 0;
  1232.     ResID     sfnt_id, fond_id;
  1233.     OSType    fond_type;
  1234.     Str255    fond_name;
  1235.     Str255    lwfn_file_name;
  1236.     UInt8     path_lwfn[PATH_MAX];
  1237.     OSErr     err;
  1238.     FT_Error  error = FT_Err_Ok;
  1239.  
  1240.  
  1241.     GetResInfo( fond, &fond_id, &fond_type, fond_name );
  1242.     if ( ResError() != noErr || fond_type != TTAG_FOND )
  1243.       return FT_THROW( Invalid_File_Format );
  1244.  
  1245.     HLock( fond );
  1246.     parse_fond( *fond, &have_sfnt, &sfnt_id, lwfn_file_name, face_index );
  1247.     HUnlock( fond );
  1248.  
  1249.     if ( lwfn_file_name[0] )
  1250.     {
  1251.       ResFileRefNum  res;
  1252.  
  1253.  
  1254.       res = HomeResFile( fond );
  1255.       if ( noErr != ResError() )
  1256.         goto found_no_lwfn_file;
  1257.  
  1258. #if HAVE_FSREF
  1259.  
  1260.       {
  1261.         UInt8  path_fond[PATH_MAX];
  1262.         FSRef  ref;
  1263.  
  1264.  
  1265.         err = FSGetForkCBInfo( res, kFSInvalidVolumeRefNum,
  1266.                                NULL, NULL, NULL, &ref, NULL );
  1267.         if ( noErr != err )
  1268.           goto found_no_lwfn_file;
  1269.  
  1270.         err = FSRefMakePath( &ref, path_fond, sizeof ( path_fond ) );
  1271.         if ( noErr != err )
  1272.           goto found_no_lwfn_file;
  1273.  
  1274.         error = lookup_lwfn_by_fond( path_fond, lwfn_file_name,
  1275.                                      path_lwfn, sizeof ( path_lwfn ) );
  1276.         if ( FT_Err_Ok == error )
  1277.           have_lwfn = 1;
  1278.       }
  1279.  
  1280. #elif HAVE_FSSPEC
  1281.  
  1282.       {
  1283.         UInt8     path_fond[PATH_MAX];
  1284.         FCBPBRec  pb;
  1285.         Str255    fond_file_name;
  1286.         FSSpec    spec;
  1287.  
  1288.  
  1289.         FT_MEM_SET( &spec, 0, sizeof ( FSSpec ) );
  1290.         FT_MEM_SET( &pb,   0, sizeof ( FCBPBRec ) );
  1291.  
  1292.         pb.ioNamePtr = fond_file_name;
  1293.         pb.ioVRefNum = 0;
  1294.         pb.ioRefNum  = res;
  1295.         pb.ioFCBIndx = 0;
  1296.  
  1297.         err = PBGetFCBInfoSync( &pb );
  1298.         if ( noErr != err )
  1299.           goto found_no_lwfn_file;
  1300.  
  1301.         err = FSMakeFSSpec( pb.ioFCBVRefNum, pb.ioFCBParID,
  1302.                             fond_file_name, &spec );
  1303.         if ( noErr != err )
  1304.           goto found_no_lwfn_file;
  1305.  
  1306.         err = FT_FSpMakePath( &spec, path_fond, sizeof ( path_fond ) );
  1307.         if ( noErr != err )
  1308.           goto found_no_lwfn_file;
  1309.  
  1310.         error = lookup_lwfn_by_fond( path_fond, lwfn_file_name,
  1311.                                      path_lwfn, sizeof ( path_lwfn ) );
  1312.         if ( FT_Err_Ok == error )
  1313.           have_lwfn = 1;
  1314.       }
  1315.  
  1316. #endif /* HAVE_FSREF, HAVE_FSSPEC */
  1317.  
  1318.     }
  1319.  
  1320.     if ( have_lwfn && ( !have_sfnt || PREFER_LWFN ) )
  1321.       error = FT_New_Face_From_LWFN( library,
  1322.                                      path_lwfn,
  1323.                                      face_index,
  1324.                                      aface );
  1325.     else
  1326.       error = FT_ERR( Unknown_File_Format );
  1327.  
  1328.   found_no_lwfn_file:
  1329.     if ( have_sfnt && FT_Err_Ok != error )
  1330.       error = FT_New_Face_From_SFNT( library,
  1331.                                      sfnt_id,
  1332.                                      face_index,
  1333.                                      aface );
  1334.  
  1335.     return error;
  1336.   }
  1337.  
  1338.  
  1339.   /* Common function to load a new FT_Face from a resource file. */
  1340.   static FT_Error
  1341.   FT_New_Face_From_Resource( FT_Library    library,
  1342.                              const UInt8*  pathname,
  1343.                              FT_Long       face_index,
  1344.                              FT_Face*      aface )
  1345.   {
  1346.     OSType    file_type;
  1347.     FT_Error  error;
  1348.  
  1349.  
  1350.     /* LWFN is a (very) specific file format, check for it explicitly */
  1351.     file_type = get_file_type_from_path( pathname );
  1352.     if ( file_type == TTAG_LWFN )
  1353.       return FT_New_Face_From_LWFN( library, pathname, face_index, aface );
  1354.  
  1355.     /* Otherwise the file type doesn't matter (there are more than  */
  1356.     /* `FFIL' and `tfil').  Just try opening it as a font suitcase; */
  1357.     /* if it works, fine.                                           */
  1358.  
  1359.     error = FT_New_Face_From_Suitcase( library, pathname, face_index, aface );
  1360.     if ( error == 0 )
  1361.       return error;
  1362.  
  1363.     /* let it fall through to normal loader (.ttf, .otf, etc.); */
  1364.     /* we signal this by returning no error and no FT_Face      */
  1365.     *aface = NULL;
  1366.     return 0;
  1367.   }
  1368.  
  1369.  
  1370.   /*************************************************************************/
  1371.   /*                                                                       */
  1372.   /* <Function>                                                            */
  1373.   /*    FT_New_Face                                                        */
  1374.   /*                                                                       */
  1375.   /* <Description>                                                         */
  1376.   /*    This is the Mac-specific implementation of FT_New_Face.  In        */
  1377.   /*    addition to the standard FT_New_Face() functionality, it also      */
  1378.   /*    accepts pathnames to Mac suitcase files.  For further              */
  1379.   /*    documentation see the original FT_New_Face() in freetype.h.        */
  1380.   /*                                                                       */
  1381.   FT_EXPORT_DEF( FT_Error )
  1382.   FT_New_Face( FT_Library   library,
  1383.                const char*  pathname,
  1384.                FT_Long      face_index,
  1385.                FT_Face*     aface )
  1386.   {
  1387.     FT_Open_Args  args;
  1388.     FT_Error      error;
  1389.  
  1390.  
  1391.     /* test for valid `library' and `aface' delayed to FT_Open_Face() */
  1392.     if ( !pathname )
  1393.       return FT_THROW( Invalid_Argument );
  1394.  
  1395.     error  = FT_Err_Ok;
  1396.     *aface = NULL;
  1397.  
  1398.     /* try resourcefork based font: LWFN, FFIL */
  1399.     error = FT_New_Face_From_Resource( library, (UInt8 *)pathname,
  1400.                                        face_index, aface );
  1401.     if ( error != 0 || *aface != NULL )
  1402.       return error;
  1403.  
  1404.     /* let it fall through to normal loader (.ttf, .otf, etc.) */
  1405.     args.flags    = FT_OPEN_PATHNAME;
  1406.     args.pathname = (char*)pathname;
  1407.     return FT_Open_Face( library, &args, face_index, aface );
  1408.   }
  1409.  
  1410.  
  1411.   /*************************************************************************/
  1412.   /*                                                                       */
  1413.   /* <Function>                                                            */
  1414.   /*    FT_New_Face_From_FSRef                                             */
  1415.   /*                                                                       */
  1416.   /* <Description>                                                         */
  1417.   /*    FT_New_Face_From_FSRef is identical to FT_New_Face except it       */
  1418.   /*    accepts an FSRef instead of a path.                                */
  1419.   /*                                                                       */
  1420.   /* This function is deprecated because Carbon data types (FSRef)         */
  1421.   /* are not cross-platform, and thus not suitable for the freetype API.   */
  1422.   FT_EXPORT_DEF( FT_Error )
  1423.   FT_New_Face_From_FSRef( FT_Library    library,
  1424.                           const FSRef*  ref,
  1425.                           FT_Long       face_index,
  1426.                           FT_Face*      aface )
  1427.   {
  1428.  
  1429. #if !HAVE_FSREF
  1430.  
  1431.     FT_UNUSED( library );
  1432.     FT_UNUSED( ref );
  1433.     FT_UNUSED( face_index );
  1434.     FT_UNUSED( aface );
  1435.  
  1436.     return FT_THROW( Unimplemented_Feature );
  1437.  
  1438. #else
  1439.  
  1440.     FT_Error      error;
  1441.     FT_Open_Args  args;
  1442.     OSErr   err;
  1443.     UInt8   pathname[PATH_MAX];
  1444.  
  1445.  
  1446.     if ( !ref )
  1447.       return FT_THROW( Invalid_Argument );
  1448.  
  1449.     err = FSRefMakePath( ref, pathname, sizeof ( pathname ) );
  1450.     if ( err )
  1451.       error = FT_ERR( Cannot_Open_Resource );
  1452.  
  1453.     error = FT_New_Face_From_Resource( library, pathname, face_index, aface );
  1454.     if ( error != 0 || *aface != NULL )
  1455.       return error;
  1456.  
  1457.     /* fallback to datafork font */
  1458.     args.flags    = FT_OPEN_PATHNAME;
  1459.     args.pathname = (char*)pathname;
  1460.     return FT_Open_Face( library, &args, face_index, aface );
  1461.  
  1462. #endif /* HAVE_FSREF */
  1463.  
  1464.   }
  1465.  
  1466.  
  1467.   /*************************************************************************/
  1468.   /*                                                                       */
  1469.   /* <Function>                                                            */
  1470.   /*    FT_New_Face_From_FSSpec                                            */
  1471.   /*                                                                       */
  1472.   /* <Description>                                                         */
  1473.   /*    FT_New_Face_From_FSSpec is identical to FT_New_Face except it      */
  1474.   /*    accepts an FSSpec instead of a path.                               */
  1475.   /*                                                                       */
  1476.   /* This function is deprecated because Carbon data types (FSSpec)        */
  1477.   /* are not cross-platform, and thus not suitable for the freetype API.   */
  1478.   FT_EXPORT_DEF( FT_Error )
  1479.   FT_New_Face_From_FSSpec( FT_Library     library,
  1480.                            const FSSpec*  spec,
  1481.                            FT_Long        face_index,
  1482.                            FT_Face*       aface )
  1483.   {
  1484.  
  1485. #if HAVE_FSREF
  1486.  
  1487.     FSRef  ref;
  1488.  
  1489.  
  1490.     if ( !spec || FSpMakeFSRef( spec, &ref ) != noErr )
  1491.       return FT_THROW( Invalid_Argument );
  1492.     else
  1493.       return FT_New_Face_From_FSRef( library, &ref, face_index, aface );
  1494.  
  1495. #elif HAVE_FSSPEC
  1496.  
  1497.     FT_Error      error;
  1498.     FT_Open_Args  args;
  1499.     OSErr         err;
  1500.     UInt8         pathname[PATH_MAX];
  1501.  
  1502.  
  1503.     if ( !spec )
  1504.       return FT_THROW( Invalid_Argument );
  1505.  
  1506.     err = FT_FSpMakePath( spec, pathname, sizeof ( pathname ) );
  1507.     if ( err )
  1508.       error = FT_ERR( Cannot_Open_Resource );
  1509.  
  1510.     error = FT_New_Face_From_Resource( library, pathname, face_index, aface );
  1511.     if ( error != 0 || *aface != NULL )
  1512.       return error;
  1513.  
  1514.     /* fallback to datafork font */
  1515.     args.flags    = FT_OPEN_PATHNAME;
  1516.     args.pathname = (char*)pathname;
  1517.     return FT_Open_Face( library, &args, face_index, aface );
  1518.  
  1519. #else
  1520.  
  1521.     FT_UNUSED( library );
  1522.     FT_UNUSED( spec );
  1523.     FT_UNUSED( face_index );
  1524.     FT_UNUSED( aface );
  1525.  
  1526.     return FT_THROW( Unimplemented_Feature );
  1527.  
  1528. #endif /* HAVE_FSREF, HAVE_FSSPEC */
  1529.  
  1530.   }
  1531.  
  1532. #endif /* FT_MACINTOSH */
  1533.  
  1534.  
  1535. /* END */
  1536.