Question about retrieving fonts

I’ve been able to implement the code that retrieves a list of fonts that are installed on the machine - that was easy. But I should be more specific and say “font families.” I haven’t found any examples, though, of how to retrieve the typefaces that are available for each of the fonts.

the FONT() collection doesn’t do that unfortunatly… so you would need DECLARES and those are going to be OS specific

I haven’t used DECLARES in any of my projects yet.
Can you point me to an example that populates a list of font family names and type faces?

That’s the code I use on OS X:

[code]Declare Function NSClassFromString Lib “Cocoa” (className As CFStringRef) As Ptr
Declare Function sharedFontManager Lib “Cocoa” Selector “sharedFontManager” (NSFontManagerClass As Ptr) As Ptr
Declare Function availableFontFamilies Lib “Cocoa” Selector “availableFontFamilies” (NSFontManager As Ptr) As Ptr
Declare Function count Lib “Cocoa” Selector “count” (NSArray As Ptr) As UInteger
Declare Function objectAtIndex Lib “Cocoa” Selector “objectAtIndex:” (NSArray As Ptr, index As UInteger) As CFStringRef

Dim fontManagerClass As Ptr = NSClassFromString(“NSFontManager”)
Dim fontManager As Ptr = sharedFontManager(fontManagerClass)
Dim availableFontFamilies As Ptr = availableFontFamilies(fontManager)
Dim count As UInteger = count(availableFontFamilies)

Dim names() As String

For index As Integer = 0 To count - 1
Dim fontName As String = objectAtIndex(availableFontFamilies, index)
names.Append(fontName)
Next

BREAK[/code]

Am I misunderstanding something.
I’m using OS X - Mavericks. It seems to me that after I run this code, I have an array, Names(), that contains a list of font family names – but not the typefaces. That is, I have Arrus BT as an entry (the font family), but not Arrus BT Roman, Arrus BT Italic, Arrus BT Bold, Arrus BT Black, Arrus BT Bold Italic, or Arrus BT Black Italic.

This was meant as a starter which I whipped together quickly. You’ll have to further dig into declares. Here is the link to NSFontManager. There is a method called availableMembersOfFontFamily:, which is probably what you want.

For what you want to use it?
In a TextArea you can use the Font Inspector or the Font Window
(in both you have bold, italic …)

Font Inspector
TextArea.Open

  #if TargetCocoa then
    declare function documentView lib "Cocoa" selector "documentView" _
    (obj_id as Integer) as Ptr
    dim myTextArea as ptr = documentView(me.Handle)
    declare sub setUsesInspectorBar lib "Cocoa" selector "setUsesInspectorBar:" _
    (obj_id as Ptr, value as Boolean)
    setUsesInspectorBar (myTextArea, true)
  #endif

Font Window

 #if TargetCocoa then
    Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr
    Soft Declare Function SharedApplication Lib "Cocoa" Selector "sharedApplication" (receiver As Ptr) As Ptr
    Dim sA As Ptr = NSClassFromString("NSApplication")
    sA = SharedApplication(sA)
    Soft Declare Sub orderFrontFontPanel Lib "Cocoa" Selector "orderFrontFontPanel:" (receiver As Ptr, iD As Ptr)
    orderFrontFontPanel(sA, nil)
  #endif

You are indeed right. This is exactly what is needed by the OP to get the styles for each font that has several :
https://developer.apple.com/library/mac/documentation/Cocoa/Reference/ApplicationKit/Classes/NSFontManager_Class/#//apple_ref/occ/instm/NSFontManager/availableMembersOfFontFamily:

I may need that in the future.

It is in MacOSLib, in the Cocoa Classes.

Thank you.

@ Axel,
what this is being used for: is to let users ste the font that they want to use to display their data on a screen. I’m going to provide a dropdown that lists all of the fonts on the system from them to choose from. (Is that what you’re asking?)

something like that?

PopupMenu.Open

    Declare Function NSClassFromString Lib "Cocoa" (aClassName As CFStringRef) As Ptr
  
  Dim myFontManger As Ptr = NSClassFromString("NSFontManager")
  Declare Function sharedFontManager Lib "Cocoa" Selector "sharedFontManager" (receiver As Ptr) As Ptr
  myFontManger = sharedFontManager(myFontManger)
  
  Dim fnt As Ptr
  Declare Function availableFonts lib "Cocoa" selector "availableFonts" (receiver as Ptr) As Ptr
  fnt = availableFonts(myFontManger)
  
  Dim count As Integer
  Declare Function aCount lib "Cocoa" selector "count" (receiver as Ptr) As Integer
  count = aCount(fnt)
  
  Dim myfont As String
  Declare Function objectAtIndex lib "Cocoa" selector "objectAtIndex:" _
  (receiver as Ptr, idx as Integer) As CFStringRef
  
  Dim fnt2 As Ptr
  Dim nFont As Ptr = NSClassFromString("NSFont")
  Declare Function fontWithName Lib "Cocoa" Selector "fontWithName:size:" _
  (receiver As Ptr, aName as CFStringRef) As Ptr
  
  Declare Function displayName lib "Cocoa" selector "displayName" (receiver as Ptr) As CFStringRef
  
  Dim i As Integer
  for i=0 to count-1
    myfont = objectAtIndex(fnt,i)
    me.AddRow displayName(fontWithName(nFont, myfont))
  next
  me.ListIndex = 30

[quote=202906:@Axel Schneider]something like that?
[/quote]

Superb. Thank you Axel :slight_smile:

Thank you, Axel; that’s exactly what I’m looking for.
It seemed like something that someone would have written before.
Chuck

To be complete, the equivalent in Windows is EnumFontFamiliesEx
https://msdn.microsoft.com/en-us/library/windows/desktop/dd162620(v=vs.85).aspx

I found an example in VB6 that may be easier to port to Xojo at
http://www.webtropy.com/articles/art9-1.asp?f=EnumFontFamiliesEx

Example Cocoa

If it’s not too imposing to ask a follow-up question, I wonder if there is a way, when you do the call, to separate the font family from the typeface. That is, I have a very long list of font/faces. I might want to provide a cascading list. Any ideas?
Chuck

[quote=203186:@C L Hinkle]If it’s not too imposing to ask a follow-up question, I wonder if there is a way, when you do the call, to separate the font family from the typeface. That is, I have a very long list of font/faces. I might want to provide a cascading list. Any ideas?
Chuck[/quote]

  • Draw the list of families only with the regular method which will become nodes
  • Using that list, you can fetch in the extended one the stylistic variation for each family name and make them subnodes.

If I understand you correctly, I think I had a similar plan, but I ran into a problem.
My plan was to loop through the Xojo font array using FontCount to get the names of families. Then I was going to use those names to determine family typefaces from Axel’s comprehensive list.

Where my plan went badly is that the the font array names don’t always match the fontwithname names. For example, the Font arrays lists a font family as Arrus BT. But when I use procedure to list all of the Cocoa fonts, the same font is called Bitstream Arrus BT.

try this
(I do not have Arrus BT, so I don’t know what name appear)

Download