As I don’t seem to be able to understand how to mitigate the issue I have with the declare I use to get a font name in my desktop app, I decided to move the code to a console helper program, so if it crashes, that won’t crash the main app.
I copied the classes and placed the code in the Run event, but I get a Segmentation Fault 11 in CFArrayGetValueAtIndex
.
I tried a System.Debuglog(str(aCFArray))
as well as a Print (str(aCFArray))
and it triggers the segmentation fault immediately. I use the CFArray from MacOSLib, and have to admit it is a bit over my league.
It may very well be where it messes up in the Desktop program as well.
Any suggestion ?
Thank you in advance.
[code] if args.Ubound = -1 then quit
dim URL as string = args(0)
dim f as folderitem = GetFolderItem(URL, FolderItem.PathTypeShell)
dim result as string
if f <> nil then
// - We have a True Type Font file, first step to make a CFURL.
// CFURLRef CFURLCreateFromFSRef (
// CFAllocatorRef allocator,
// const struct FSRef *fsRef
// );
declare function CFURLCreateFromFSRef lib "Cocoa" ( allocator as Ptr, fsRef as Ptr ) as Ptr
Dim CFURLRef as Ptr = CFUrlCreateFromFSRef( nil, f.MacFSRef )
if CFUrlRef = nil then
Msgbox "Failed to get the CFURLRef of folder """+ f.nativePath + """"
else
'SWIFT
'func CTFontManagerCreateFontDescriptorsFromURL(_ fileURL: CFURL!) -> CFArray!
declare function CTFontManagerCreateFontDescriptorsFromURL lib "Cocoa" ( fontURL as Ptr) as CFTypeRef
Dim aCFArray As CFTyperef = CTFontManagerCreateFontDescriptorsFromURL( CFURLRef )
'func CTFontDescriptorCopyAttribute(_ descriptor: CTFontDescriptor!,
'_ attribute: CFString!) -> AnyObject!
declare function CTFontDescriptorCopyAttribute lib "Cocoa" (CTFontDescriptor as Ptr, Attribut as CFStringRef) as CFStringRef
Dim attribute as CFStringRef = "NSFontVisibleNameAttribute"
declare function CFArrayGetCount lib "CoreFoundation.framework" (theArray as CFTypeRef) as Integer
declare function CFArrayGetValueAtIndex lib "CoreFoundation.framework" (theArray as CFTypeRef, idx as Integer) as Ptr
Dim ub As Integer = 1 'CFArrayGetCount( aCFArray ) - 1
if ub > -1 then
Dim aCTFontDescriptorRef As Ptr = CFArrayGetValueAtIndex( aCFArray, 0 ) // <------ Segmentation fault occurs
// then use CTFontDescriptorCopyAttribute() to get the name
'System.DebugLog CTFontDescriptorCopyAttribute(aCTFontDescriptorRef, attribute)
if aCTFontDescriptorRef <> nil then
result = CTFontDescriptorCopyAttribute(aCTFontDescriptorRef, attribute)
end if
end if
// - As we had a valid CFURLRef from a 'Create' or 'Copy' API, we must release it.
declare sub CFRelease lib "Cocoa" ( ref as ptr )
CFRelease CFUrlRef
end if
end if
if Result <> “” then
Print Result
else
Print f.name
end if[/code]