How to get a font ↩
FontParts provides ways to open and create fonts and to interact with fonts which are currently open in the font editor’s interface.
This page gives an overview of the different ways to get a font object.
CurrentFont
We can get the font which is already open in RoboFont:
f = CurrentFont()
print(f)
<Font MyFont Regular>
- If several fonts are open,
CurrentFont
will return the one at the top. - If no font is open,
CurrentFont
will returnNone
.
OpenFont
We can also open an existing font from disk:
f = OpenFont()
If OpenFont
is used without any arguments, it will open a dialog so you can locate the font file in your machine.
If you already know the path to the font file, you can pass it to OpenFont
:
f = OpenFont("/Users/me/Desktop/myfont.ufo")
To get get the full path to a file without having to type it, drag the file from the Finder and drop it into the Scripting Window.
Opening a font without the interface
We can also open a font in memory only, without opening a font window for it – this makes some operations faster, and can be useful when working with very large fonts or batch processing.
f = OpenFont("/Users/me/Desktop/myfont.ufo", showInterface=False)
print(f)
<Font MyFont Regular>
Because the font has no interface, there is no visual feedback to show the result of your actions. Don’t forget to save the font after you’re done.
In RoboFont 1.8, use
showUI
instead ofshowInterface
.
It is also possible to open a font without the interface, make some changes to it, and only then open it in the UI:
f = OpenFont("/Users/me/Desktop/myfont.ufo", showInterface=False)
# do something
f.openInterface()
NewFont
We can use NewFont
to create a new empty font (with or without an interface):
f = NewFont()
print(f)
<Font None None>
f = NewFont(showInterface=False)
The new font can be created with a family name and style name:
f = NewFont(familyName='HelloWorld', styleName='Regular')
AllFonts
If there are not one but several fonts open in the UI, we can iterate over all of them at once using AllFonts
:
for f in AllFonts():
print(f)
<Font None None>
<Font MyOtherFont Regular>
<Font MyFont Bold>
<Font MyFont Regular>
Getting only some fonts
The output from the script above shows fonts from different families, and of different styles. AllFonts
offers some handy methods to narrow down the list of fonts:
for f in AllFonts().getFontsByFamilyName('MyFont'):
print(f)
<Font MyFont Bold>
<Font MyFont Regular>
for f in AllFonts().getFontsByStyleName('Regular'):
print(f)
<Font MyOtherFont Regular>
<Font MyFont Regular>
Sorting the list of open fonts
We can also ask AllFonts
to sort the list of fonts by one or more font info attributes:
for f in AllFonts(sortOptions=["openTypeOS2WeightClass"]):
print(f)