Commit 4827e9bd2c8a4c47fcfc5ed81f25a0a8c06e73d1

Suzuki, Toshiya (鈴木俊哉) 2007-02-05T03:28:29

* Add FT_GetFilePath_From_Mac_ATS_Name as replacement for FT_GetFile_From_Mac_ATS_Name

diff --git a/ChangeLog b/ChangeLog
index a2d2e93..8d02b70 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,14 @@
 2007-02-05  suzuki toshiya  <mpsuzuki@hiroshima-u.ac.jp>
 
+	* include/freetype/ftmac.h (FT_GetFilePath_From_Mac_ATS_Name):
+	Introduced as replacement of FT_GetFile_From_Mac_ATS_Name.
+	* src/base/ftmac.c (FT_GetFilePath_From_Mac_ATS_Name): Ditto.
+	(FT_GetFile_From_Mac_ATS_Name): Rewritten as wrapper of
+	FT_GetFilePath_From_Mac_ATS_Name.
+	* builds/mac/ftmac.c: Ditto.
+
+2007-02-05  suzuki toshiya  <mpsuzuki@hiroshima-u.ac.jp>
+
 	* include/freetype/ftmac.h: Fixed wrong comment, FSSpec of
 	FT_GetFile_From_Mac_Name, FT_GetFile_From_Mac_ATS_Name are
 	for passing to FT_New_Face_From_FSSpec.
diff --git a/builds/mac/ftmac.c b/builds/mac/ftmac.c
index 59e9992..484784c 100644
--- a/builds/mac/ftmac.c
+++ b/builds/mac/ftmac.c
@@ -243,22 +243,34 @@
 #endif /* HAVE_QUICKDRAW_CARBON */
 
 
-#if !HAVE_ATS
-
-  FT_EXPORT_DEF( FT_Error )
-  FT_GetFile_From_Mac_ATS_Name( const char*  fontName,
-                                FSSpec*      pathSpec,
-                                FT_Long*     face_index )
+#if HAVE_ATS
+
+  /* Private function.                                         */
+  /* The FSSpec type has been discouraged for a long time,     */
+  /* but for some reason, there is no FSRef version of         */
+  /* ATSFontGetFileSpecification(), so we made our own.        */
+  /* Apple will provide one eventually.                        */
+  static OSStatus
+  FT_ATSFontGetFileReference( ATSFontRef  ats_font_id,
+                              FSRef*      ats_font_ref )
   {
-    return FT_Err_Unimplemented_Feature;
+    OSStatus    err;
+    FSSpec      spec;
+
+    err = ATSFontGetFileSpecification( ats_font_id, &spec );
+    if ( noErr == err )
+    {
+      err = FSpMakeFSRef( &spec, ats_font_ref );
+    }
+
+    return err;
   }
 
-#else
 
-  FT_EXPORT_DEF( FT_Error )
-  FT_GetFile_From_Mac_ATS_Name( const char*  fontName,
-                                FSSpec*      pathSpec,
-                                FT_Long*     face_index )
+  static FT_Error
+  FT_GetFileRef_From_Mac_ATS_Name( const char*  fontName,
+                                   FSRef*       ats_font_ref,
+                                   FT_Long*     face_index )
   {
     CFStringRef  cf_fontName;
     ATSFontRef   ats_font_id;
@@ -274,36 +286,104 @@
     if ( ats_font_id == 0 || ats_font_id == 0xFFFFFFFFUL )
       return FT_Err_Unknown_File_Format;
 
-    if ( 0 != ATSFontGetFileSpecification( ats_font_id, pathSpec ) )
+    if ( noErr != FT_ATSFontGetFileReference( ats_font_id, ats_font_ref ) )
       return FT_Err_Unknown_File_Format;
 
     /* face_index calculation by searching preceding fontIDs */
     /* with same FSRef                                       */
     {
-      int     i;
-      FSSpec  f;
+      ATSFontRef  id2 = ats_font_id - 1;
+      FSRef       ref2;
 
 
-      for ( i = 1; i < ats_font_id; i++ )
+      while ( id2 > 0 )
       {
-        if ( 0 != ATSFontGetFileSpecification( ats_font_id - i,
-                                               &f               ) ||
-             f.vRefNum != pathSpec->vRefNum                       ||
-             f.parID   != pathSpec->parID                         ||
-             f.name[0] != pathSpec->name[0]                       ||
-             0 != ft_strncmp( (char *)f.name + 1,
-                              (char *)pathSpec->name + 1,
-                              f.name[0]                           ) )
+        if ( noErr != FT_ATSFontGetFileReference( id2, &ref2 ) )
           break;
+        if ( noErr != FSCompareFSRefs( ats_font_ref, &ref2 ) )
+          break;
+
+        id2 --;
       }
-      *face_index = ( i - 1 );
+      *face_index = ats_font_id - ( id2 + 1 );
     }
+
+    return FT_Err_Ok;
+  }
+
+#endif
+
+#if !HAVE_ATS
+
+  FT_EXPORT_DEF( FT_Error )
+  FT_GetFilePath_From_Mac_ATS_Name( const char*  fontName,
+                                    UInt8*       path,
+                                    UInt32       maxPathSize,
+                                    FT_Long*     face_index )
+  {
+    return FT_Err_Unimplemented_Feature;
+  }
+
+#else
+
+  FT_EXPORT_DEF( FT_Error )
+  FT_GetFilePath_From_Mac_ATS_Name( const char*  fontName,
+                                    UInt8*       path,
+                                    UInt32       maxPathSize,
+                                    FT_Long*     face_index )
+  {
+    FSRef     ref;
+    FT_Error  err;
+
+
+    err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
+    if ( FT_Err_Ok != err )
+      return err;
+
+    if ( noErr != FSRefMakePath( &ref, path, maxPathSize ) )
+      return FT_Err_Unknown_File_Format;
+
     return FT_Err_Ok;
   }
 
 #endif /* HAVE_ATS */
 
 
+#if !HAVE_FSSPEC
+
+  FT_EXPORT_DEF( FT_Error )
+  FT_GetFile_From_Mac_ATS_Name( const char*  fontName,
+                                FSSpec*      pathSpec,
+                                FT_Long*     face_index )
+  {
+    return FT_Err_Unimplemented_Feature;
+  }
+
+#else
+
+  /* This function is deprecated because FSSpec is deprecated in Mac OS X  */
+  FT_EXPORT_DEF( FT_Error )
+  FT_GetFile_From_Mac_ATS_Name( const char*  fontName,
+                                FSSpec*      pathSpec,
+                                FT_Long*     face_index )
+  {
+    FSRef     ref;
+    FT_Error  err;
+
+    err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
+    if ( FT_Err_Ok != err )
+      return err;
+
+    if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone, NULL, NULL,
+                                    pathSpec, NULL ) )
+      return FT_Err_Unknown_File_Format;
+
+    return FT_Err_Ok;
+  }
+
+#endif
+
+
 #if defined( __MWERKS__ ) && !TARGET_RT_MAC_MACHO
 
 #define STREAM_FILE( stream )  ( (FT_FILE*)stream->descriptor.pointer )
diff --git a/include/freetype/ftmac.h b/include/freetype/ftmac.h
index 3b472aa..4529fe2 100644
--- a/include/freetype/ftmac.h
+++ b/include/freetype/ftmac.h
@@ -150,6 +150,37 @@ FT_BEGIN_HEADER
   /*************************************************************************/
   /*                                                                       */
   /* <Function>                                                            */
+  /*    FT_GetFilePath_From_Mac_ATS_Name                                   */
+  /*                                                                       */
+  /* <Description>                                                         */
+  /*    Return a pathname of the disk file and face index for given font   */
+  /*    name which is handled by ATS framework.                            */
+  /*                                                                       */
+  /* <Input>                                                               */
+  /*    fontName    :: Mac OS name of the font in ATS framework.           */
+  /*                                                                       */
+  /* <Output>                                                              */
+  /*    path        :: Buffer to store pathname of the file. For passing   */
+  /*                   to @FT_New_Face. The client must allocate this      */
+  /*                   buffer before calling this function.                */
+  /*                                                                       */
+  /*    maxPathSize :: Lengths of the buffer `path' that client allocated. */
+  /*                                                                       */
+  /*    face_index  :: Index of the face.  For passing to @FT_New_Face.    */
+  /*                                                                       */
+  /* <Return>                                                              */
+  /*    FreeType error code.  0 means success.                             */
+  /*                                                                       */
+  FT_EXPORT( FT_Error )
+  FT_GetFilePath_From_Mac_ATS_Name( const char*  fontName,
+                                    UInt8*       path,
+                                    UInt32       maxPathSize,
+                                    FT_Long*     face_index );
+
+
+  /*************************************************************************/
+  /*                                                                       */
+  /* <Function>                                                            */
   /*    FT_New_Face_From_FSSpec                                            */
   /*                                                                       */
   /* <Description>                                                         */
diff --git a/src/base/ftmac.c b/src/base/ftmac.c
index 07a2dc7..c8d06b0 100644
--- a/src/base/ftmac.c
+++ b/src/base/ftmac.c
@@ -108,10 +108,32 @@
   }
 
 
-  FT_EXPORT_DEF( FT_Error )
-  FT_GetFile_From_Mac_ATS_Name( const char*  fontName,
-                                FSSpec*      pathSpec,
-                                FT_Long*     face_index )
+  /* Private function.                                         */
+  /* The FSSpec type has been discouraged for a long time,     */
+  /* but for some reason, there is no FSRef version of         */
+  /* ATSFontGetFileSpecification(), so we made our own.        */
+  /* Apple will provide one eventually.                        */
+  static OSStatus
+  FT_ATSFontGetFileReference( ATSFontRef  ats_font_id,
+                              FSRef*      ats_font_ref )
+  {
+    OSStatus    err;
+    FSSpec      spec;
+
+    err = ATSFontGetFileSpecification( ats_font_id, &spec );
+    if ( noErr == err )
+    {
+      err = FSpMakeFSRef( &spec, ats_font_ref );
+    }
+
+    return err;
+  }
+
+
+  static FT_Error
+  FT_GetFileRef_From_Mac_ATS_Name( const char*  fontName,
+                                   FSRef*       ats_font_ref,
+                                   FT_Long*     face_index )
   {
     CFStringRef  cf_fontName;
     ATSFontRef   ats_font_id;
@@ -127,31 +149,75 @@
     if ( ats_font_id == 0 || ats_font_id == 0xFFFFFFFFUL )
       return FT_Err_Unknown_File_Format;
 
-    if ( 0 != ATSFontGetFileSpecification( ats_font_id, pathSpec ) )
+    if ( noErr != FT_ATSFontGetFileReference( ats_font_id, ats_font_ref ) )
       return FT_Err_Unknown_File_Format;
 
     /* face_index calculation by searching preceding fontIDs */
     /* with same FSRef                                       */
     {
-      int     i;
-      FSSpec  f;
+      ATSFontRef  id2 = ats_font_id - 1;
+      FSRef       ref2;
 
 
-      for ( i = 1; i < ats_font_id; i++ )
+      while ( id2 > 0 )
       {
-        if ( 0 != ATSFontGetFileSpecification( ats_font_id - i,
-                                               &f               ) ||
-             f.vRefNum != pathSpec->vRefNum                       ||
-             f.parID   != pathSpec->parID                         ||
-             f.name[0] != pathSpec->name[0]                       ||
-             0 != ft_strncmp( (char *)f.name + 1,
-                              (char *)pathSpec->name + 1,
-                              f.name[0]                           ) )
+        if ( noErr != FT_ATSFontGetFileReference( id2, &ref2 ) )
           break;
+        if ( noErr != FSCompareFSRefs( ats_font_ref, &ref2 ) )
+          break;
+
+        id2 --;
       }
-      *face_index = ( i - 1 );
+      *face_index = ats_font_id - ( id2 + 1 );
     }
+
+    return FT_Err_Ok;
+  }
+
+
+  FT_EXPORT_DEF( FT_Error )
+  FT_GetFilePath_From_Mac_ATS_Name( const char*  fontName,
+                                    UInt8*       path,
+                                    UInt32       maxPathSize,
+                                    FT_Long*     face_index )
+  {
+    FSRef     ref;
+    FT_Error  err;
+
+
+    err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
+    if ( FT_Err_Ok != err )
+      return err;
+
+    if ( noErr != FSRefMakePath( &ref, path, maxPathSize ) )
+      return FT_Err_Unknown_File_Format;
+
+    return FT_Err_Ok;
+  }
+
+
+  /* This function is deprecated because FSSpec is deprecated in Mac OS X  */
+  FT_EXPORT_DEF( FT_Error )
+  FT_GetFile_From_Mac_ATS_Name( const char*  fontName,
+                                FSSpec*      pathSpec,
+                                FT_Long*     face_index )
+  {
+#if __LP64__
+    return FT_Err_Unimplemented_Feature;
+#else
+    FSRef     ref;
+    FT_Error  err;
+
+    err = FT_GetFileRef_From_Mac_ATS_Name( fontName, &ref, face_index );
+    if ( FT_Err_Ok != err )
+      return err;
+
+    if ( noErr != FSGetCatalogInfo( &ref, kFSCatInfoNone, NULL, NULL,
+                                    pathSpec, NULL ) )
+      return FT_Err_Unknown_File_Format;
+
     return FT_Err_Ok;
+#endif
   }