| 1 |
Python Scripting |
| 2 |
================ |
| 3 |
|
| 4 |
.. default-domain:: py |
| 5 |
|
| 6 |
I assume you have a working knowledge of `Python <http://www.python.org/doc/>`_. |
| 7 |
FontForge implements two Python modules -- one great huge one called :py:mod:`fontforge` |
| 8 |
which provides access to as much of FontForge's functionality as I've had |
| 9 |
time to write, and one tiny one called :py:mod:`psMat` which provides quick |
| 10 |
access to some useful transformations expressed as PostScript matrices. |
| 11 |
|
| 12 |
In python terms fontforge *embeds* python. It is possible to build |
| 13 |
fontforge so that it is also a :ref:`python extension <python.extension>`. |
| 14 |
|
| 15 |
FontForge Modules |
| 16 |
----------------- |
| 17 |
|
| 18 |
.. toctree:: |
| 19 |
:maxdepth: 3 |
| 20 |
:glob: |
| 21 |
|
| 22 |
python/* |
| 23 |
|
| 24 |
Command line convenience |
| 25 |
------------------------ |
| 26 |
For convenience, Python commands given as a ``-c`` argument on the |
| 27 |
command line have the following code prepended: |
| 28 |
|
| 29 |
:: |
| 30 |
|
| 31 |
from sys import argv; from fontforge import * |
| 32 |
|
| 33 |
Hence, the trivial script to convert a font can be written: |
| 34 |
|
| 35 |
:: |
| 36 |
|
| 37 |
fontforge -c 'open(argv[1]).generate(argv[2])' |
| 38 |
|
| 39 |
|
| 40 |
Trivial example |
| 41 |
--------------- |
| 42 |
:: |
| 43 |
|
| 44 |
import fontforge #Load the module |
| 45 |
amb=fontforge.open("Ambrosia.sfd") #Open a font |
| 46 |
amb.selection.select(("ranges",None),"A","Z") #select A-Z |
| 47 |
amb.copy() #Copy those glyphs into the clipboard |
| 48 |
|
| 49 |
n=fontforge.font() #Create a new font |
| 50 |
n.selection.select(("ranges",None),"A","Z") #select A-Z of it |
| 51 |
n.paste() #paste the glyphs above in |
| 52 |
print n["A"].foreground #test to see that something |
| 53 |
# actually got pasted |
| 54 |
n.fontname="NewFont" #Give the new font a name |
| 55 |
n.save("NewFont.sfd") #and save it. |
| 56 |
|
| 57 |
|
| 58 |
.. _python.extension: |
| 59 |
|
| 60 |
FontForge as a Python extension |
| 61 |
------------------------------- |
| 62 |
|
| 63 |
FontForge's Python modules can be used independently of the GUI application. |
| 64 |
|
| 65 |
Installation via pip |
| 66 |
^^^^^^^^^^^^^^^^^^^^ |
| 67 |
|
| 68 |
:: |
| 69 |
|
| 70 |
pip install fontforge |
| 71 |
|
| 72 |
Pre-built wheels are available for: |
| 73 |
|
| 74 |
- Linux: x86_64, aarch64 |
| 75 |
- macOS: x86_64, arm64 |
| 76 |
- Windows: x86_64 |
| 77 |
|
| 78 |
The wheels include all required native libraries (FreeType, libxml2, etc.). |
| 79 |
|
| 80 |
Installation from system packages |
| 81 |
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 82 |
|
| 83 |
When FontForge is installed via system package manager or built from source |
| 84 |
with ``-DENABLE_PYTHON_EXTENSION=ON``, the modules are installed to the |
| 85 |
system Python's site-packages. |
| 86 |
|
| 87 |
Usage |
| 88 |
^^^^^ |
| 89 |
|
| 90 |
:: |
| 91 |
|
| 92 |
>>> import fontforge |
| 93 |
>>> import psMat |
| 94 |
|
| 95 |
For technical details on building wheels and the extension architecture, |
| 96 |
see :doc:`python-extension`. |