Font list

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Font list

Post 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 :)
akj
Enthusiast
Enthusiast
Posts: 668
Joined: Mon Jun 09, 2003 10:08 pm
Location: Nottingham

Re: Font list

Post by akj »

Anthony Jordan
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: Font list

Post by Polo »

Thanks!

Looking for a Mac solution now, seems CTFontCollectionCreateFromAvailableFonts should be used though I have no idea how :oops:
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Font list

Post by Shardik »

Polo wrote:Looking for a Mac solution now, seems CTFontCollectionCreateFromAvailableFonts should be used though I have no idea how :oops:

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
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: Font list

Post 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.
Post Reply