File: python.rst

package info (click to toggle)
fontforge 1%3A20230101~dfsg-8
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 63,284 kB
  • sloc: ansic: 462,486; python: 6,916; cpp: 214; objc: 122; sh: 101; makefile: 55; xml: 11
file content (69 lines) | stat: -rw-r--r-- 2,142 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
Python Scripting
================

.. default-domain:: py

I assume you have a working knowledge of `Python <http://www.python.org/doc/>`_.
FontForge implements two Python modules -- one great huge one called :py:mod:`fontforge`
which provides access to as much of FontForge's functionality as I've had
time to write, and one tiny one called :py:mod:`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 :ref:`python extension <python.extension>`.

FontForge Modules
-----------------

.. toctree::
   :maxdepth: 3
   :glob:

   python/*

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.


.. _python.extension:

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