Python Scripting — FontForge 20230101 documentation

# Python Scripting

I assume you have a working knowledge of Python. FontForge implements two Python modules – one great huge one called `fontforge` which provides access to as much of FontForge’s functionality as I’ve had time to write, and one tiny one called `psMat` which provides quick access to some useful transformations expressed as PostScript matrices.

In python terms fontforge embeds python. It is possible to build fontforge so that it is also a python extension.

## FontForge Modules

- fontforge
  - Module attributes
    - `hooks`
    - `splineCorner`
    - `splineCurve`
    - `splineHVCurve`
    - `splineTangent`
    - `fontforge.spiroG4`
    - `fontforge.spiroG2`
    - `fontforge.spiroCorner`
    - `fontforge.spiroLeft`
    - `fontforge.spiroRight`
    - `fontforge.spiroOpen`
    - `fontforge.unspecifiedMathValue`
  - Module functions
    - `getPrefs()`
    - `setPrefs()`
    - `hasSpiro()`
    - `savePrefs()`
    - `loadPrefs()`
    - `defaultOtherSubrs()`
    - `readOtherSubrsFile()`
    - `loadEncodingFile()`
    - `loadNamelist()`
    - `loadNamelistDir()`
    - `preloadCidmap()`
    - `printSetup()`
    - `nameFromUnicode()`
    - `UnicodeAnnotationFromLib()`
    - `UnicodeBlockCountFromLib()`
    - `UnicodeBlockEndFromLib()`
    - `UnicodeBlockNameFromLib()`
    - `UnicodeBlockStartFromLib()`
    - `unicodeFromName()`
    - `UnicodeNameFromLib()`
    - `UnicodeNamesListVersion()`
    - `UnicodeNames2FromLib()`
    - `scriptFromUnicode()`
    - `SpiroVersion()`
    - `version()`
    - `loadPlugins()`
    - `getPluginInfo()`
    - `configurePlugins()`
    - `runInitScripts()`
    - `scriptPath()`
    - `fonts()`
    - `activeFont()`
    - `activeGlyph()`
    - `activeLayer()`
    - `fontsInFile()`
    - `open()`
    - `parseTTInstrs()`
    - `unParseTTInstrs()`
    - `unitShape()`
    - `registerGlyphSeparationHook()`
  - User Interface Module Functions
    - `hasUserInterface()`
    - `registerMenuItem()`
    - `registerImportExport()`
    - `logWarning()`
    - `postError()`
    - `postNotice()`
    - `openFilename()`
    - `saveFilename()`
    - `ask()`
    - `askChoices()`
    - `askString()`
    - `askMulti()`
  - Point
    - `point`
  - Contour
    - `contour`
  - Layer
    - `layer`
  - Glyph Pen
    - `glyphPen`
  - Glyph
    - `glyph`
    - `glyph.mathKern.bottomLeft`
    - `glyph.mathKern.bottomRight`
    - `glyph.mathKern.topLeft`
    - `glyph.mathKern.topRight`
  - Selection
    - `selection`
  - Private
    - `private`
  - Math
    - `math.ScriptPercentScaleDown`
    - `math.ScriptScriptPercentScaleDown`
    - `math.DelimitedSubFormulaMinHeight`
    - `math.DisplayOperatorMinHeight`
    - `math.MathLeading`
    - `math.AxisHeight`
    - `math.AccentBaseHeight`
    - `math.FlattenedAccentBaseHeight`
    - `math.SubscriptShiftDown`
    - `math.SubscriptTopMax`
    - `math.SubscriptBaselineDropMin`
    - `math.SuperscriptShiftUp`
    - `math.SuperscriptShiftUpCramped`
    - `math.SuperscriptBottomMin`
    - `math.SuperscriptBaselineDropMax`
    - `math.SubSuperscriptGapMin`
    - `math.SuperscriptBottomMaxWithSubscript`
    - `math.SpaceAfterScript`
    - `math.UpperLimitGapMin`
    - `math.UpperLimitBaselineRiseMin`
    - `math.LowerLimitGapMin`
    - `math.LowerLimitBaselineDropMin`
    - `math.StackTopShiftUp`
    - `math.StackTopDisplayStyleShiftUp`
    - `math.StackBottomShiftDown`
    - `math.StackBottomDisplayStyleShiftDown`
    - `math.StackGapMin`
    - `math.StackDisplayStyleGapMin`
    - `math.StretchStackTopShiftUp`
    - `math.StretchStackBottomShiftDown`
    - `math.StretchStackGapAboveMin`
    - `math.StretchStackGapBelowMin`
    - `math.FractionNumeratorShiftUp`
    - `math.FractionNumeratorDisplayStyleShiftUp`
    - `math.FractionDenominatorShiftDown`
    - `math.FractionDenominatorDisplayStyleShiftDown`
    - `math.FractionNumeratorGapMin`
    - `math.FractionNumeratorDisplayStyleGapMin`
    - `math.FractionRuleThickness`
    - `math.FractionDenominatorGapMin`
    - `math.FractionDenominatorDisplayStyleGapMin`
    - `math.SkewedFractionHorizontalGap`
    - `math.SkewedFractionVerticalGap`
    - `math.OverbarVerticalGap`
    - `math.OverbarRuleThickness`
    - `math.OverbarExtraAscender`
    - `math.UnderbarVerticalGap`
    - `math.UnderbarRuleThickness`
    - `math.UnderbarExtraDescender`
    - `math.RadicalVerticalGap`
    - `math.RadicalDisplayStyleVerticalGap`
    - `math.RadicalRuleThickness`
    - `math.RadicalExtraAscender`
    - `math.RadicalKernBeforeDegree`
    - `math.RadicalKernAfterDegree`
    - `math.RadicalDegreeBottomRaisePercent`
    - `math.MinConnectorOverlap`
    - `math.exists()`
    - `math.clear()`
  - Font
    - `font`
- psMat
  - `identity()`
  - `compose()`
  - `inverse()`
  - `rotate()`
  - `scale()`
  - `skew()`
  - `translate()`

## Command line convenience

For convenience, Python commands given as a `-c` argument on the command line have the following code prepended:

    from sys import argv; from fontforge import *

Hence, the trivial script to convert a font can be written:

    fontforge -c 'open(argv[1]).generate(argv[2])'

## Trivial example

    import fontforge                                 #Load the module
    amb=fontforge.open("Ambrosia.sfd")               #Open a font
    amb.selection.select(("ranges",None),"A","Z")    #select A-Z
    amb.copy()                                       #Copy those glyphs into the clipboard

    n=fontforge.font()                               #Create a new font
    n.selection.select(("ranges",None),"A","Z")      #select A-Z of it
    n.paste()                                        #paste the glyphs above in
    print n["A"].foreground                          #test to see that something
                                                     #  actually got pasted
    n.fontname="NewFont"                             #Give the new font a name
    n.save("NewFont.sfd")                            #and save it.

## FontForge as a python extension

In addition to embedding Python, FontForge typically installs a Python module accessible to the system’s `python` executable, which can be accessed using:

    >>> import fontforge