MacOS: How can I activate fonts programmatically, systemwide?
Asked Answered
A

3

7

I'm trying to make something that behaves very similarly to a font manager, but I simply can't find any documentation on how I can, for example, add an OpenType font to the list NSFontManager provides. I see even Font Book shows fonts activated through a font manager app, so I want to understand what's happening under the hood. What libraries are being called?

Thank you!

Alver answered 8/12, 2019 at 23:39 Comment(1)
Did you ever finish making this? Did you ever figure out how to do autoactivation?Lecithinase
A
7

It took me quite some time, but here's the way to go.

CTFontManagerRegisterFontURLs does exactly what I was looking for on macOS. Also, session 227 from 2019's WWDC has more info on it, albeit focused on iOS.

Alver answered 14/12, 2019 at 15:11 Comment(0)
P
0

Apple has an old archived article entitled Mac OS X: Font locations and their purposes which has a section on manually managing fonts:

[...] You may install fonts by double-clicking them and following the on screen prompts in the Font Book application, or by manually copying or dragging font files to any of the standard Fonts folders in Mac OS X.

The folder in which a font is located determines who can access and use the font. For example: If a user manually installs a new font at ~/Library/Fonts/, the font is available only to that user. If a root or admin user installs the same font at /Network/Library/Fonts/, all network users can use the fonts (assuming that the network administrator has set up computers for this type of sharing).

Changes to fonts take effect when an application is opened or a user logs in to the account or computer on which the changes occurred. Duplicate fonts are resolved based on the order of precedence defined for the standard Fonts folders and are described from highest to lowest priority below.

I tested this using an empty font I created (Awfulcode-Regular.otf) and TextEdit and found the changes to be instant. I therefor conjecture that any delay is actually introduced by specific apps rather than macOS itself.

Paola answered 11/12, 2019 at 9:51 Comment(1)
Thanks for your answer. A simple solution, but that's not what I'm looking for. I want to understand how font managers work and how they're able to auto-activate fonts, for example. They're not copying font files to those folders, they're interacting with some system library that's feeding the NSFontManager list. I really want to understand what's happening under the hood, not just activate a font. Makes sense?Alver
W
-1

Function

CTFontManagerRegisterFontURLs(::::)

  • Declaration *
func CTFontManagerRegisterFontURLs(_ fontURLs: CFArray, 
                                 _ scope: CTFontManagerScope, 
                                 _ enabled: Bool, 
                                 _ registrationHandler: ((CFArray, Bool) -> Bool)?)
Warner answered 18/12, 2019 at 16:5 Comment(1)
Please dont post code only answer, try to add some context of how this will solve the problem in question.Cut

© 2022 - 2024 — McMap. All rights reserved.