Page 1 of 1
Font list
Posted: Thu Sep 22, 2011 12:51 pm
by Polo
Hello,
Would be nice to have a set a functions to get all fonts available on the system.
Pretty sure there's an easy way to do it on Windows (probably on Mac too!), feel free to show some code if you have some

Re: Font list
Posted: Thu Sep 22, 2011 9:38 pm
by akj
Re: Font list
Posted: Thu Sep 22, 2011 11:42 pm
by Polo
Thanks!
Looking for a Mac solution now, seems CTFontCollectionCreateFromAvailableFonts should be used though I have no idea how

Re: Font list
Posted: Sun Sep 25, 2011 8:22 am
by Shardik
Polo wrote:Looking for a Mac solution now, seems CTFontCollectionCreateFromAvailableFonts should be used though I have no idea how

Code: Select all
EnableExplicit
ImportC ""
CFArrayGetCount(CFArrayRef.L)
CFArrayGetValueAtIndex(CFArrayRef.L, Index.L)
CFRelease(CFTypeRef.L)
CFStringGetCString(CFStringRef.L, *StringBuffer, BufferSize.L, CFStringEncoding.L)
CTFontCollectionCreateFromAvailableFonts(RemoveDuplicates.L)
CTFontCollectionCreateMatchingFontDescriptors(FontCollectionRef.L)
CTFontDescriptorCopyAttribute(FontDescriptorRef.L, AttributeStringRef.L)
EndImport
Procedure.S ConvertCFStringIntoString(CFStringRef.L)
Protected String.S = Space(256)
CFStringGetCString(CFStringRef, @String, Len(String), 0)
ProcedureReturn Trim(String)
EndProcedure
NewList FontName.S()
Define FontCollectionRef.L
Define FontCount.L
Define FontDescriptorArrayRef.L
Define FontDescriptorRef.L
Define FontDictionaryRef.L
Define FontNameRef.L
Define i.L
Define NSFontNameAttribute.L = CFStringCreateWithCString_(0, @"NSFontNameAttribute", 0)
FontCollectionRef = CTFontCollectionCreateFromAvailableFonts(0)
If FontCollectionRef
FontDescriptorArrayRef = CTFontCollectionCreateMatchingFontDescriptors(FontCollectionRef)
If FontDescriptorArrayRef
FontCount = CFArrayGetCount(FontDescriptorArrayRef)
Debug "Fonts available: " + Str(FontCount)
For i = 0 To FontCount - 1
FontDescriptorRef = CFArrayGetValueAtIndex(FontDescriptorArrayRef, i)
If FontDescriptorRef
FontNameRef = CTFontDescriptorCopyAttribute(FontDescriptorRef, NSFontNameAttribute)
If FontNameRef
AddElement(FontName())
FontName() = ConvertCFStringIntoString(FontNameRef)
CFRelease(FontNameRef)
EndIf
CFRelease(FontDescriptorRef)
EndIf
Next i
CFRelease(FontDescriptorArrayRef)
EndIf
EndIf
If FontCount > 0
OpenWindow(0, 270, 100, 280, 400, "Available fonts: " + Str(FontCount), #PB_Window_SystemMenu | #PB_Window_Invisible)
ListViewGadget(0, 10, 10, WindowWidth(0) - 20, WindowHeight(0) - 20)
SortList(FontName(), #PB_Sort_Ascending)
ForEach FontName()
AddGadgetItem(0, -1, FontName())
Next
HideWindow(0, #False)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
Re: Font list
Posted: Sun Sep 25, 2011 12:53 pm
by Polo
Thanks Shardik! I had the exact same code, but was using kCTFontDisplayNameAttribute instead of NSFontNameAttribute, and it doesn't work.
I'm wondering now how can I get the font list without the bold/italic etc... ? I guess it's a matter of attribute, but I can't get to work any of those supposed to be used in this code, only NSFontNameAttribute is ok.