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
contourcontour.is_quadraticcontour.closedcontour.namecontour.spiroscontour.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
layerlayer.is_quadraticlayer.__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
glyphglyph.activeLayerglyph.altuniglyph.anchorPointsglyph.anchorPointsWithSelglyph.backgroundglyph.changedglyph.colorglyph.commentglyph.dhintsglyph.encodingglyph.fontglyph.foregroundglyph.glyphclassglyph.glyphnameglyph.hhintsglyph.horizontalComponentsglyph.horizontalComponentItalicCorrectionglyph.horizontalVariantsglyph.isExtendedShapeglyph.italicCorrectionglyph.layer_cntglyph.layersglyph.layerrefsglyph.lcaretsglyph.left_side_bearingglyph.manualHintsglyph.mathKern.bottomLeftglyph.mathKern.bottomRightglyph.mathKern.topLeftglyph.mathKern.topRightglyph.originalgidglyph.persistentglyph.referencesglyph.right_side_bearingglyph.scriptglyph.temporaryglyph.texheightglyph.texdepthglyph.topaccentglyph.ttinstrsglyph.unicodeglyph.unlinkRmOvrlpSaveglyph.user_decompglyph.userdataglyph.vhintsglyph.validation_stateglyph.verticalComponentsglyph.verticalComponentItalicCorrectionglyph.verticalVariantsglyph.widthglyph.vwidthglyph.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.ScriptPercentScaleDownmath.ScriptScriptPercentScaleDownmath.DelimitedSubFormulaMinHeightmath.DisplayOperatorMinHeightmath.MathLeadingmath.MathLeadingDeviceTablemath.AxisHeightmath.AxisHeightDeviceTablemath.AccentBaseHeightmath.AccentBaseHeightDeviceTablemath.FlattenedAccentBaseHeightmath.FlattenedAccentBaseHeightDeviceTablemath.SubscriptShiftDownmath.SubscriptShiftDownDeviceTablemath.SubscriptTopMaxmath.SubscriptTopMaxDeviceTablemath.SubscriptBaselineDropMinmath.SubscriptBaselineDropMinDeviceTablemath.SuperscriptShiftUpmath.SuperscriptShiftUpDeviceTablemath.SuperscriptShiftUpCrampedmath.SuperscriptShiftUpCrampedDeviceTablemath.SuperscriptBottomMinmath.SuperscriptBottomMinDeviceTablemath.SuperscriptBaselineDropMaxmath.SuperscriptBaselineDropMaxDeviceTablemath.SubSuperscriptGapMinmath.SubSuperscriptGapMinDeviceTablemath.SuperscriptBottomMaxWithSubscriptmath.SuperscriptBottomMaxWithSubscriptDeviceTablemath.SpaceAfterScriptmath.SpaceAfterScriptDeviceTablemath.UpperLimitGapMinmath.UpperLimitGapMinDeviceTablemath.UpperLimitBaselineRiseMinmath.UpperLimitBaselineRiseMinDeviceTablemath.LowerLimitGapMinmath.LowerLimitGapMinDeviceTablemath.LowerLimitBaselineDropMinmath.LowerLimitBaselineDropMinDeviceTablemath.StackTopShiftUpmath.StackTopShiftUpDeviceTablemath.StackTopDisplayStyleShiftUpmath.StackTopDisplayStyleShiftUpDeviceTablemath.StackBottomShiftDownmath.StackBottomShiftDownDeviceTablemath.StackBottomDisplayStyleShiftDownmath.StackBottomDisplayStyleShiftDownDeviceTablemath.StackGapMinmath.StackGapMinDeviceTablemath.StackDisplayStyleGapMinmath.StackDisplayStyleGapMinDeviceTablemath.StretchStackTopShiftUpmath.StretchStackTopShiftUpDeviceTablemath.StretchStackBottomShiftDownmath.StretchStackBottomShiftDownDeviceTablemath.StretchStackGapAboveMinmath.StretchStackGapAboveMinDeviceTablemath.StretchStackGapBelowMinmath.StretchStackGapBelowMinDeviceTablemath.FractionNumeratorShiftUpmath.FractionNumeratorShiftUpDeviceTablemath.FractionNumeratorDisplayStyleShiftUpmath.FractionNumeratorDisplayStyleShiftUpDeviceTablemath.FractionDenominatorShiftDownmath.FractionDenominatorShiftDownDeviceTablemath.FractionDenominatorDisplayStyleShiftDownmath.FractionDenominatorDisplayStyleShiftDownDeviceTablemath.FractionNumeratorGapMinmath.FractionNumeratorGapMinDeviceTablemath.FractionNumeratorDisplayStyleGapMinmath.FractionNumeratorDisplayStyleGapMinDeviceTablemath.FractionRuleThicknessmath.FractionRuleThicknessDeviceTablemath.FractionDenominatorGapMinmath.FractionDenominatorGapMinDeviceTablemath.FractionDenominatorDisplayStyleGapMinmath.FractionDenominatorDisplayStyleGapMinDeviceTablemath.SkewedFractionHorizontalGapmath.SkewedFractionHorizontalGapDeviceTablemath.SkewedFractionVerticalGapmath.SkewedFractionVerticalGapDeviceTablemath.OverbarVerticalGapmath.OverbarVerticalGapDeviceTablemath.OverbarRuleThicknessmath.OverbarRuleThicknessDeviceTablemath.OverbarExtraAscendermath.OverbarExtraAscenderDeviceTablemath.UnderbarVerticalGapmath.UnderbarVerticalGapDeviceTablemath.UnderbarRuleThicknessmath.UnderbarRuleThicknessDeviceTablemath.UnderbarExtraDescendermath.UnderbarExtraDescenderDeviceTablemath.RadicalVerticalGapmath.RadicalVerticalGapDeviceTablemath.RadicalDisplayStyleVerticalGapmath.RadicalDisplayStyleVerticalGapDeviceTablemath.RadicalRuleThicknessmath.RadicalRuleThicknessDeviceTablemath.RadicalExtraAscendermath.RadicalExtraAscenderDeviceTablemath.RadicalKernBeforeDegreemath.RadicalKernBeforeDegreeDeviceTablemath.RadicalKernAfterDegreemath.RadicalKernAfterDegreeDeviceTablemath.RadicalDegreeBottomRaisePercentmath.MinConnectorOverlapmath.exists()math.clear()
- Font
fontfont.activeLayerfont.ascentfont.bitmapSizesfont.capHeightfont.changedfont.cidcopyrightfont.cidfamilynamefont.cidfontnamefont.cidfullnamefont.cidorderingfont.cidregistryfont.cidsubfontfont.cidsubfontcntfont.cidsubfontnamesfont.cidsupplementfont.cidversionfont.cidweightfont.commentfont.copyrightfont.cvtfont.default_base_filenamefont.descentfont.design_sizefont.emfont.encodingfont.familynamefont.fondnamefont.fontlogfont.fontnamefont.fullnamefont.gaspfont.gasp_versionfont.gpos_lookupsfont.gsub_lookupsfont.guidefont.hasvmetricsfont.head_optimized_for_cleartypefont.hhea_ascentfont.hhea_ascent_addfont.hhea_descentfont.hhea_descent_addfont.hhea_linegapfont.horizontalBaselinefont.is_cidfont.is_quadraticfont.isnewfont.italicanglefont.macstylefont.layer_cntfont.layersfont.loadStatefont.maxp_FDEFsfont.maxp_IDEFsfont.maxp_maxStackDepthfont.maxp_storageCntfont.maxp_twilightPtCntfont.maxp_zonesfont.multilayerfont.onlybitmapsfont.os2_capheightfont.os2_codepagesfont.os2_family_classfont.os2_fstypefont.os2_panosefont.os2_strikeyposfont.os2_strikeysizefont.os2_stylemapfont.os2_subxofffont.os2_subxsizefont.os2_subyofffont.os2_subysizefont.os2_supxofffont.os2_supxsizefont.os2_supyofffont.os2_supysizefont.os2_typoascentfont.os2_typoascent_addfont.os2_typodescentfont.os2_typodescent_addfont.os2_typolinegapfont.os2_unicoderangesfont.os2_use_typo_metricsfont.os2_vendorfont.os2_versionfont.os2_weightfont.os2_weight_width_slope_onlyfont.os2_widthfont.os2_winascentfont.os2_winascent_addfont.os2_windescentfont.os2_windescent_addfont.os2_xheightfont.pathfont.persistentfont.mathfont.privatefont.privateStatefont.selectionfont.sfd_pathfont.sfnt_namesfont.sfntRevisionfont.size_featurefont.strokedfontfont.strokewidthfont.style_set_namesfont.temporaryfont.texparametersfont.uniqueidfont.uposfont.userdatafont.uwidthfont.versionfont.verticalBaselinefont.vertical_originfont.vhea_linegapfont.weightfont.woffMajorfont.woffMinorfont.woffMetadatafont.xHeightfont.__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.reencode()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