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
- 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
- Point
- Contour- contour
- contour.is_quadratic
- contour.closed
- contour.name
- contour.spiros
- contour.dup()
- contour.isEmpty()
- contour.boundingBox()
- contour.getSplineAfterPoint()
- contour.draw()
- contour.__reduce__()
- contour.__iter__()
- contour.moveTo()
- contour.lineTo()
- contour.cubicTo()
- contour.quadraticTo()
- contour.insertPoint()
- contour.makeFirst()
- contour.isClockwise()
- contour.reverseDirection()
- contour.similar()
- contour.xBoundsAtY()
- contour.yBoundsAtX()
- contour.addExtrema()
- contour.cluster()
- contour.merge()
- contour.round()
- contour.selfIntersects()
- contour.simplify()
- contour.transform()
- contour.addInflections()
- contour.balance()
- contour.harmonize()
 
- Layer- layer
- layer.is_quadratic()
- layer.__iter__()
- layer.__reduce__()
- layer.dup()
- layer.isEmpty()
- layer.addExtrema()
- layer.cluster()
- layer.correctDirection()
- layer.export()
- layer.exclude()
- layer.intersect()
- layer.removeOverlap()
- layer.interpolateNewLayer()
- layer.round()
- layer.selfIntersects()
- layer.similar()
- layer.simplify()
- layer.stemControl()
- layer.stroke()
- layer.transform()
- layer.nltransform()
- layer.boundingBox()
- layer.xBoundsAtY()
- layer.yBoundsAtX()
- layer.draw()
- layer.addInflections()
- layer.balance()
- layer.harmonize()
 
- Glyph Pen
- Glyph- glyph
- glyph.activeLayer
- glyph.altuni
- glyph.anchorPoints
- glyph.anchorPointsWithSel
- glyph.background
- glyph.changed
- glyph.color
- glyph.comment
- glyph.dhints
- glyph.encoding
- glyph.font
- glyph.foreground
- glyph.glyphclass
- glyph.glyphname
- glyph.hhints
- glyph.horizontalComponents
- glyph.horizontalComponentItalicCorrection
- glyph.horizontalVariants
- glyph.isExtendedShape
- glyph.italicCorrection
- glyph.layer_cnt
- glyph.layers
- glyph.layerrefs
- glyph.lcarets
- glyph.left_side_bearing
- glyph.manualHints
- glyph.mathKern.bottomLeft
- glyph.mathKern.bottomRight
- glyph.mathKern.topLeft
- glyph.mathKern.topRight
- glyph.originalgid
- glyph.persistent
- glyph.references
- glyph.right_side_bearing
- glyph.script
- glyph.temporary
- glyph.texheight
- glyph.texdepth
- glyph.topaccent
- glyph.ttinstrs
- glyph.unicode
- glyph.unlinkRmOvrlpSave
- glyph.user_decomp
- glyph.userdata
- glyph.vhints
- glyph.validation_state
- glyph.verticalComponents
- glyph.verticalComponentItalicCorrection
- glyph.verticalVariants
- glyph.width
- glyph.vwidth
- glyph.addAnchorPoint()
- glyph.addExtrema()
- glyph.addReference()
- glyph.addHint()
- glyph.addPosSub()
- glyph.appendAccent()
- glyph.autoHint()
- glyph.autoInstr()
- glyph.autoTrace()
- glyph.boundingBox()
- glyph.build()
- glyph.canonicalContours()
- glyph.canonicalStart()
- glyph.changeWeight()
- glyph.condenseExtend()
- glyph.clear()
- glyph.cluster()
- glyph.correctDirection()
- glyph.doUndoLayer()
- glyph.exclude()
- glyph.export()
- glyph.genericGlyphChange()
- glyph.getPosSub()
- glyph.importOutlines()
- glyph.intersect()
- glyph.isWorthOutputting()
- glyph.preserveLayerAsUndo()
- glyph.removeOverlap()
- glyph.removePosSub()
- glyph.round()
- glyph.selfIntersects()
- glyph.setLayer()
- glyph.simplify()
- glyph.stroke()
- glyph.transform()
- glyph.nltransform()
- glyph.unlinkRef()
- glyph.unlinkThisGlyph()
- glyph.useRefsMetrics()
- glyph.validate()
- glyph.draw()
- glyph.glyphPen()
- glyph.addInflections()
- glyph.balance()
- glyph.harmonize()
 
- Selection
- 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
- font.activeLayer
- font.ascent
- font.bitmapSizes
- font.capHeight
- font.changed
- font.cidcopyright
- font.cidfamilyname
- font.cidfontname
- font.cidfullname
- font.cidordering
- font.cidregistry
- font.cidsubfont
- font.cidsubfontcnt
- font.cidsubfontnames
- font.cidsupplement
- font.cidversion
- font.cidweight
- font.comment
- font.copyright
- font.cvt
- font.default_base_filename
- font.descent
- font.design_size
- font.em
- font.encoding
- font.familyname
- font.fondname
- font.fontlog
- font.fontname
- font.fullname
- font.gasp
- font.gasp_version
- font.gpos_lookups
- font.gsub_lookups
- font.guide
- font.hasvmetrics
- font.head_optimized_for_cleartype
- font.hhea_ascent
- font.hhea_ascent_add
- font.hhea_descent
- font.hhea_descent_add
- font.hhea_linegap
- font.horizontalBaseline
- font.is_cid
- font.is_quadratic
- font.isnew
- font.italicangle
- font.macstyle
- font.layer_cnt
- font.layers
- font.loadState
- font.maxp_FDEFs
- font.maxp_IDEFs
- font.maxp_maxStackDepth
- font.maxp_storageCnt
- font.maxp_twilightPtCnt
- font.maxp_zones
- font.multilayer
- font.onlybitmaps
- font.os2_codepages
- font.os2_family_class
- font.os2_fstype
- font.os2_stylemap
- font.os2_panose
- font.os2_strikeypos
- font.os2_strikeysize
- font.os2_subxoff
- font.os2_subxsize
- font.os2_subyoff
- font.os2_subysize
- font.os2_supxoff
- font.os2_supxsize
- font.os2_supyoff
- font.os2_supysize
- font.os2_typoascent
- font.os2_typoascent_add
- font.os2_typodescent
- font.os2_typodescent_add
- font.os2_typolinegap
- font.os2_use_typo_metrics
- font.os2_unicoderanges
- font.os2_vendor
- font.os2_version
- font.os2_weight
- font.os2_weight_width_slope_only
- font.os2_width
- font.os2_winascent
- font.os2_winascent_add
- font.os2_windescent
- font.os2_windescent_add
- font.path
- font.persistent
- font.math
- font.private
- font.privateState
- font.selection
- font.sfd_path
- font.sfnt_names
- font.sfntRevision
- font.size_feature
- font.strokedfont
- font.strokewidth
- font.temporary
- font.texparameters
- font.uniqueid
- font.upos
- font.userdata
- font.uwidth
- font.version
- font.verticalBaseline
- font.vertical_origin
- font.vhea_linegap
- font.weight
- font.woffMajor
- font.woffMinor
- font.woffMetadata
- font.xHeight
- font.__iter__()
- font.__contains__()
- font.__len__()
- font.__getitem__()
- font.addAnchorClass()
- font.addKerningClass()
- font.addLookup()
- font.addLookupSubtable()
- font.addContextualSubtable()
- font.addSmallCaps()
- font.alterKerningClass()
- font.autoKern()
- font.appendSFNTName()
- font.buildOrReplaceAALTFeatures()
- font.cidConvertByCMap()
- font.cidConvertTo()
- font.cidFlatten()
- font.cidFlattenByCMap()
- font.cidInsertBlankSubFont()
- font.cidRemoveSubFont()
- font.close()
- font.compareFonts()
- font.createChar()
- font.createInterpolatedGlyph()
- font.createMappedChar()
- font.find()
- font.findEncodingSlot()
- font.glyphs()
- font.generate()
- font.generateTtc()
- font.generateFeatureFile()
- font.genericGlyphChange()
- font.getKerningClass()
- font.getLookupInfo()
- font.getLookupSubtables()
- font.getLookupSubtableAnchorClasses()
- font.getLookupOfSubtable()
- font.getSubtableOfAnchor()
- font.importBitmaps()
- font.importLookups()
- font.interpolateFonts()
- font.isKerningClass()
- font.isVerticalKerning()
- font.italicize()
- font.lookupSetFeatureList()
- font.lookupSetFlags()
- font.lookupSetStoreLigatureInAfm()
- font.mergeFonts()
- font.mergeFeature()
- font.mergeKern()
- font.mergeLookups()
- font.mergeLookupSubtables()
- font.printSample()
- font.randomText()
- font.regenBitmaps()
- font.removeAnchorClass()
- font.removeLookup()
- font.removeLookupSubtable()
- font.removeGlyph()
- font.replaceAll()
- font.revert()
- font.revertFromBackup()
- font.save()
- font.saveNamelist()
- font.getTableData()
- font.setTableData()
- font.validate()
- font.addExtrema()
- font.addInflections()
- font.autoHint()
- font.autoInstr()
- font.autoWidth()
- font.autoTrace()
- font.build()
- font.canonicalContours()
- font.canonicalStart()
- font.changeWeight()
- font.condenseExtend()
- font.clear()
- font.cluster()
- font.copy()
- font.copyReference()
- font.correctDirection()
- font.correctReferences()
- font.cut()
- font.paste()
- font.intersect()
- font.pasteInto()
- font.removeOverlap()
- font.replaceWithReference()
- font.round()
- font.simplify()
- font.stroke()
- font.transform()
- font.nltransform()
- font.unlinkReferences()
 
 
- psMat
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