Repository navigation
Expand file tree
/
Copy pathpyextend.html
More file actions
647 lines (602 loc) · 61.8 KB
/
Copy pathpyextend.html
File metadata and controls
647 lines (602 loc) · 61.8 KB
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
<!DOCTYPE html>
<html lang="en" data-content_root="../">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Extending FontForge With Python — FontForge 20251009 documentation</title>
<link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=4f649999" />
<link rel="stylesheet" type="text/css" href="../_static/alabaster.css?v=cb25574f" />
<script src="../_static/documentation_options.js?v=8a728459"></script>
<script src="../_static/doctools.js?v=888ff710"></script>
<script src="../_static/sphinx_highlight.js?v=dc90522c"></script>
<link rel="icon" href="../_static/fftype16.png"/>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Extensions to Adobe’s BDF for greymap fonts" href="BDFGrey.html" />
<link rel="prev" title="Expand Stroke Facility" href="stroke.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head><body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<section id="extending-fontforge-with-python">
<h1>Extending FontForge With Python<a class="headerlink" href="#extending-fontforge-with-python" title="Link to this heading">¶</a></h1>
<p>FontForge includes an extensive <a class="reference internal" href="../scripting/python.html"><span class="doc">Python API</span></a> which is
most often used in scripts to alter and generate font files. However, this API
also provides ways of extending the functionality of the FontForge program,
including the user interface. Most of these are documented in the <a class="reference internal" href="../scripting/python/fontforge.html#fontforge-ui-functions"><span class="std std-ref">User
Interface Module Functions</span></a> section of the Python
documentation.</p>
<section id="modifying-points-and-contours-references-and-anchor-points">
<h2>Modifying Points and Contours, References, and Anchor Points<a class="headerlink" href="#modifying-points-and-contours-references-and-anchor-points" title="Link to this heading">¶</a></h2>
<p>Given that points and contours are basis of vector font data many extensions
will want to modify them. There is one misleading aspect of the “layer” API
that must be clarified in order to do so. It is also important to be aware
of layers and how they relate to the FontForge user interface.</p>
<section id="the-basics">
<h3>The Basics<a class="headerlink" href="#the-basics" title="Link to this heading">¶</a></h3>
<p>A <code class="docutils literal notranslate"><span class="pre">point</span></code> is (typically) an element of a <code class="docutils literal notranslate"><span class="pre">contour</span></code> and a <code class="docutils literal notranslate"><span class="pre">contour</span></code> is
(typically) an element of a <code class="docutils literal notranslate"><span class="pre">layer</span></code>. Confusingly, however, there are really
two meanings of the term “layer” in the FontForge Python API. One is the
<code class="docutils literal notranslate"><span class="pre">fontforge.layer</span></code> object, which is the object that can contain
<code class="docutils literal notranslate"><span class="pre">fontforge.contour</span></code> objects. The other is a layer <em>internal</em> to a font
object.</p>
<p>The confusion arises when a developer attempts to modify a point in this way:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">f</span> <span class="o">=</span> <span class="n">fontforge</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">'foo.otf'</span><span class="p">)</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="s1">'a'</span><span class="p">]</span>
<span class="n">l</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">foreground</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">l</span><span class="p">:</span>
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">c</span><span class="p">:</span>
<span class="n">p</span><span class="o">.</span><span class="n">x</span> <span class="o">+=</span> <span class="mi">15</span>
<span class="n">f</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="s1">'foo_new.otf'</span><span class="p">)</span>
</pre></div>
</div>
<p>After this script executes the points of the glyph “a” in <code class="docutils literal notranslate"><span class="pre">foo_new.otf</span></code> will
be the same as those in <code class="docutils literal notranslate"><span class="pre">foo.otf</span></code>. And this is true even though the points in
the <code class="docutils literal notranslate"><span class="pre">fontforge.layer</span></code> object <code class="docutils literal notranslate"><span class="pre">l</span></code> have been changed. This is because <code class="docutils literal notranslate"><span class="pre">l</span></code>
is not a <em>reference</em> to <code class="docutils literal notranslate"><span class="pre">g.foreground</span></code> (which represents the internal state
of the layer) but a <em>copy</em> of it. Any changes to the layer must therefore be
“copied back” to have an effect on the font. This can be done with an
assignment but we now recommend using the <code class="docutils literal notranslate"><span class="pre">glyph.setLayer()</span></code> method instead,
as this gives you more control over how point types are preserved or converted
as they are copied back.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">f</span> <span class="o">=</span> <span class="n">fontforge</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">'foo.otf'</span><span class="p">)</span>
<span class="n">g</span> <span class="o">=</span> <span class="n">f</span><span class="p">[</span><span class="s1">'a'</span><span class="p">]</span>
<span class="n">l</span> <span class="o">=</span> <span class="n">g</span><span class="o">.</span><span class="n">foreground</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">l</span><span class="p">:</span>
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">c</span><span class="p">:</span>
<span class="n">p</span><span class="o">.</span><span class="n">x</span> <span class="o">+=</span> <span class="mi">15</span>
<span class="n">g</span><span class="o">.</span><span class="n">setLayer</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="s1">'Fore'</span><span class="p">)</span>
<span class="n">f</span><span class="o">.</span><span class="n">generate</span><span class="p">(</span><span class="s1">'foo_new.otf'</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="layer-awareness-and-undo-preservation">
<h3>Layer Awareness and Undo Preservation<a class="headerlink" href="#layer-awareness-and-undo-preservation" title="Link to this heading">¶</a></h3>
<p>The author of a “top-down” script for modifying a font will typically know what
layer to modify—most often <code class="docutils literal notranslate"><span class="pre">glyph.foreground</span></code> (which is also identified as
“0” or “Fore”). A script that extends the FontForge UI with a new menu option
should not make such assumptions. At a given time the user may be editing the
foreground or background layer or even one of a few layers in a multi-layer
font. Such a function should therefore typically not reference specific layers
at all and use <code class="docutils literal notranslate"><span class="pre">glyph.activeLayer</span></code> instead, as in:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">shiftBy15</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">glyph</span><span class="p">):</span>
<span class="n">l</span> <span class="o">=</span> <span class="n">glyph</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span><span class="p">]</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">l</span><span class="p">:</span>
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">c</span><span class="p">:</span>
<span class="n">p</span><span class="o">.</span><span class="n">x</span> <span class="o">+=</span> <span class="mi">15</span>
<span class="n">glyph</span><span class="o">.</span><span class="n">preserveLayerAsUndo</span><span class="p">(</span><span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span><span class="p">)</span>
<span class="n">g</span><span class="o">.</span><span class="n">setLayer</span><span class="p">(</span><span class="n">l</span><span class="p">,</span> <span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span><span class="p">)</span>
</pre></div>
</div>
<p>This function also demonstrates an appropriate use of the
<code class="docutils literal notranslate"><span class="pre">preserveLayerAsUndo()</span></code> method. Setting an “internal” layer (either through
assignment for with the <code class="docutils literal notranslate"><span class="pre">glyph.setLayer()</span></code> method) does not automatically add
the previous layer to the Undo/Redo state. By calling
<code class="docutils literal notranslate"><span class="pre">glyph.preserveLayerAsUndo()</span></code> you give the user the option of reversing your
change with “Edit -> Undo”.</p>
</section>
<section id="references">
<h3>References<a class="headerlink" href="#references" title="Link to this heading">¶</a></h3>
<p>The reference API is similar to the layer API, with the same caveats. You can
get the list of references for the current UI layer with <code class="docutils literal notranslate"><span class="pre">rl</span> <span class="pre">=</span>
<span class="pre">g.layerrefs[g.activeLayer]</span></code> (where <code class="docutils literal notranslate"><span class="pre">g</span></code> is a <code class="docutils literal notranslate"><span class="pre">fontforge.glyph</span></code> object). To
effect changes in the references you need to assign back to
<code class="docutils literal notranslate"><span class="pre">g.layerrefs[g.activeLayer]</span></code>. Each reference is represented by a tuple of
glyph name, transformation matrix tuple (that is, a sub-tuple of six floating
point numbers representing the transformation) and a boolean <code class="docutils literal notranslate"><span class="pre">selected</span></code> status.
This script accordingly shifts the references in the current layer by 15 points:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">shiftRefsBy15</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">glyph</span><span class="p">):</span>
<span class="n">rl</span> <span class="o">=</span> <span class="n">glyph</span><span class="o">.</span><span class="n">layerrefs</span><span class="p">[</span><span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span><span class="p">]</span>
<span class="n">nl</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">tr</span> <span class="o">=</span> <span class="n">psMat</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
<span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">rl</span><span class="p">:</span>
<span class="n">nl</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">r</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">psMat</span><span class="o">.</span><span class="n">compose</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">tr</span><span class="p">),</span> <span class="n">r</span><span class="p">[</span><span class="mi">2</span><span class="p">]))</span>
<span class="n">glyph</span><span class="o">.</span><span class="n">preserveLayerAsUndo</span><span class="p">(</span><span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span><span class="p">)</span>
<span class="n">glyph</span><span class="o">.</span><span class="n">layerrefs</span><span class="p">[</span><span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span><span class="p">]</span> <span class="o">=</span> <span class="n">nl</span>
</pre></div>
</div>
<p>(One could also have used a transformation matrix to move the points in the earlier
fragments by passing it to <code class="docutils literal notranslate"><span class="pre">p.transform()</span></code>.)</p>
<p>As noted in the API documentation, when assigning to the reference list the
transformation matrix and selection status tuple elements are optional (with
respective defaults of the identity matrix and <code class="docutils literal notranslate"><span class="pre">False</span></code>).</p>
</section>
<section id="anchor-points">
<h3>Anchor Points<a class="headerlink" href="#anchor-points" title="Link to this heading">¶</a></h3>
<p>In contrast with points and references, all anchor points are considered part
of the foreground layer. They can be accessed and edited through the
<code class="docutils literal notranslate"><span class="pre">glyph.anchorPointsWithSel</span></code> property. When editing the list care must be
taken to construct the tuples so that the name, type, position, selection and
(when relevant) ligature information are preserved or changed as desired.</p>
</section>
</section>
<section id="adding-a-menu-item">
<span id="fontforge-plugin-menu"></span><h2>Adding a Menu Item<a class="headerlink" href="#adding-a-menu-item" title="Link to this heading">¶</a></h2>
<p>The functions <code class="docutils literal notranslate"><span class="pre">shiftBy15()</span></code> and <code class="docutils literal notranslate"><span class="pre">shiftRefsBy15()</span></code> above were written to
be added as menu items using <a class="reference internal" href="../scripting/python/fontforge.html#fontforge.registerMenuItem" title="fontforge.registerMenuItem"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fontforge.registerMenuItem()</span></code></a>. This is
the most common form of FontForge UI extension and we recommend familiarizing
(or re-familiarizing) yourself with the documentation as the API has recently
been enhanced.</p>
<section id="selection-state">
<h3>Selection State<a class="headerlink" href="#selection-state" title="Link to this heading">¶</a></h3>
<p>In the case of many menu items, probably most of them, the user should have
some “input” into how one applies in a given situation. Sometimes this is a
reason to raise a dialog asking the user for input, but in the majority of
cases a menu callback will “interact” with a user by way of <em>selection state</em>.</p>
<p>Each point, both on-curve and control, has a boolean selection state. The
selection state of on-curve points is generally more important or “primary”.
The “Transform …” dialog, for example, transforms selected on-curve points
(and references) and their associated off-curve points regardless of the
latter’s selection status. (This is true unless no points are selected, in
which case all points are transformed.) Other contour-level facilities, such as
“Correct Direction”, consider a contour to be selected if at least one of its
on-curve points is selected, and changes those contours accordingly.</p>
<p>Still, in some cases you may want to attend to the selection state of
control points.</p>
<p>Each reference and anchor point also has a selection state. The Python
reference API has recently been enhanced to make the state available.</p>
<p>As an example, consider the <code class="docutils literal notranslate"><span class="pre">shiftRefsBy15</span></code> function above. We could
enhance it to shift only <em>selected</em> references unless there are none,
in all references are shifted:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">shiftRefsBy15</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">glyph</span><span class="p">):</span>
<span class="n">rl</span> <span class="o">=</span> <span class="n">glyph</span><span class="o">.</span><span class="n">layerrefs</span><span class="p">[</span><span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span><span class="p">]</span>
<span class="n">has_selected</span> <span class="o">=</span> <span class="kc">False</span>
<span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">rl</span><span class="p">:</span>
<span class="k">if</span> <span class="n">r</span><span class="p">[</span><span class="mi">2</span><span class="p">]:</span>
<span class="n">has_selected</span> <span class="o">=</span> <span class="kc">True</span>
<span class="n">nl</span> <span class="o">=</span> <span class="p">[]</span>
<span class="n">tr</span> <span class="o">=</span> <span class="n">psMat</span><span class="o">.</span><span class="n">translate</span><span class="p">(</span><span class="mi">15</span><span class="p">,</span><span class="mi">0</span><span class="p">)</span>
<span class="k">for</span> <span class="n">r</span> <span class="ow">in</span> <span class="n">rl</span><span class="p">:</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">has_selected</span> <span class="ow">or</span> <span class="n">r</span><span class="p">[</span><span class="mi">2</span><span class="p">]:</span>
<span class="n">nl</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">r</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">psMat</span><span class="o">.</span><span class="n">compose</span><span class="p">(</span><span class="n">r</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">tr</span><span class="p">),</span> <span class="n">r</span><span class="p">[</span><span class="mi">2</span><span class="p">]))</span>
<span class="k">else</span><span class="p">:</span>
<span class="n">nl</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">r</span><span class="p">)</span>
<span class="n">glyph</span><span class="o">.</span><span class="n">preserveLayerAsUndo</span><span class="p">(</span><span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span><span class="p">)</span>
<span class="n">glyph</span><span class="o">.</span><span class="n">layerrefs</span><span class="p">[</span><span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span><span class="p">]</span> <span class="o">=</span> <span class="n">nl</span>
</pre></div>
</div>
<p>At the font level each font has its own selection state accessible via the
<a class="reference internal" href="../scripting/python/fontforge.html#fontforge.font.selection" title="fontforge.font.selection"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fontforge.font.selection</span></code></a> property. Many useful font-level tools will
be selection-agnostic but some should only apply to selected glyphs. When
these are added as menu items they should include enable_functions analogous to
those of a glyph-level tool, so that a user learns whey they do and do not
apply.</p>
</section>
<section id="enable-functions">
<h3>Enable Functions<a class="headerlink" href="#enable-functions" title="Link to this heading">¶</a></h3>
<p>Some menu items, such as one that adds a new point in a CharView, may always be
“applicable”. Others can only do something sensible if the character (or font)
has certain features, such as at least one point or reference, or if certain
combinations of points, anchor points, and references are selected. When adding
a new option you should help the user understand when it is and is not
applicable by writing and registering an <code class="docutils literal notranslate"><span class="pre">enable</span></code> function. This will allow
FontForge to display the item as enabled in the menu when it applies and as
disabled when it does not apply.</p>
<p>The enhanced version of <code class="docutils literal notranslate"><span class="pre">shiftRefsBy15()</span></code> only applies if there is at least
one reference, so it’s enable function could be:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">SRB15Enable</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">glyph</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">glyph</span><span class="o">.</span><span class="n">layerrefs</span><span class="p">[</span><span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span><span class="p">])</span><span class="o">></span><span class="mi">0</span>
</pre></div>
</div>
<p>See the next section for an example of how to register this function.</p>
</section>
<section id="hotkeys-and-mnemonics">
<h3>Hotkeys and Mnemonics<a class="headerlink" href="#hotkeys-and-mnemonics" title="Link to this heading">¶</a></h3>
<p>A menu item can have both a Hotkey and a Mnemonic. These are two different means
of picking a menu item using the keyboard.</p>
<p>A Hotkey is a single key combination, usually including modifiers like Control,
Alt, and Shift, that directly invokes a menu item or other action. Because
hotkeys are a “limited resource”, unless you are confident that a menu item you
register should have one it may be better to let the user add their own if they
find your addition particularly useful. As the
<a class="reference internal" href="../scripting/python/fontforge.html#fontforge.registerMenuItem" title="fontforge.registerMenuItem"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fontforge.registerMenuItem()</span></code></a> documentation notes, even if you do
include a <code class="docutils literal notranslate"><span class="pre">hotkey</span></code> string when registering that key combination may already
be taken, in which case no hotkey will be assigned.</p>
<p>Mnemonics provide a <em>per-menu</em> key accelerator, usually activated by pressing
Alt and then the mnemonic key displayed with an underline. Using sequences of
mnemonic key combinations a user can navigate from the menu bar down through
sub-menus and choose a menu action entirely with the keyboard. Mnemonics are also
a “limited resource” but <em>less</em> limited, and we encourage all plugin developers to
specify them.</p>
<p>You specify a mnemonic key by preceding it with an underscore in the name, as in:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">fontforge</span><span class="o">.</span><span class="n">registerMenuItem</span><span class="p">(</span><span class="n">callback</span><span class="o">=</span><span class="n">shiftRefsBy15</span><span class="p">,</span> <span class="n">enable</span><span class="o">=</span><span class="n">SRB15Enable</span><span class="p">,</span>
<span class="n">context</span><span class="o">=</span><span class="p">(</span><span class="s2">"Glyph"</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="p">(</span><span class="s2">"_Shift References by 15"</span><span class="p">,</span> <span class="s2">"MyExt.shiftRefsBy15"</span><span class="p">),</span>
<span class="n">submenu</span><span class="o">=</span><span class="p">(</span><span class="s2">"_MyExt"</span><span class="p">,</span> <span class="s2">"MyExt.submenu"</span><span class="p">))</span>
</pre></div>
</div>
<p>which specifes “M” as the mnemonic for the “MyExt” submenu and “S” as the mnemonic for
the action.</p>
<p>Note, however, that the specified mnemonic for the top level is only taken as a
suggestion, and if it is not available another will be assigned. This is
because different users will have different combinations of plugins and init
scripts installed in different orders, so any given mnemonic may already be
in use. Because lower menu levels will typically have entries for one plugin
or script you can count on getting the mnemonic you specify.</p>
</section>
<section id="best-practices">
<h3>Best Practices<a class="headerlink" href="#best-practices" title="Link to this heading">¶</a></h3>
<p>In addition to the advice above, we recommend that a plugin put all of its
added menu items into or under a single sub-menu of “Tools”. A user may have
many plugins installed or have their own init scripts so a single plugin should
avoid taking up a lot of space.</p>
</section>
</section>
<section id="storing-per-font-and-per-glyph-information">
<h2>Storing Per-Font and Per-Glyph Information<a class="headerlink" href="#storing-per-font-and-per-glyph-information" title="Link to this heading">¶</a></h2>
<p>The FontForge SFD file format and python API includes
<a class="reference internal" href="../scripting/python/fontforge.html#fontforge.font.persistent" title="fontforge.font.persistent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fontforge.font.persistent</span></code></a> for storing per-font information and
<a class="reference internal" href="../scripting/python/fontforge.html#fontforge.glyph.persistent" title="fontforge.glyph.persistent"><code class="xref py py-attr docutils literal notranslate"><span class="pre">fontforge.glyph.persistent</span></code></a> for storing per-glyph information.
These are fragile interfaces, however, and should only be used when strictly
necessary.</p>
<p>The FontForge program does not enforce any pattern of use on these attributes.
We suggest the following conventions:</p>
<ol class="arabic simple">
<li><p>If the contents are <em>empty</em>, create a dictionary and add an entry to it
with your plugin name as the key. Store your data in or “under” the value
of that entry. Then assign it to the <code class="docutils literal notranslate"><span class="pre">persistent</span></code> attribute.</p></li>
<li><p>If the content is a dictionary add your entry to it if it is not already
present and store the result.</p></li>
<li><p>If the content is not a dictionary warn the user with an
<a class="reference internal" href="../scripting/python/fontforge.html#fontforge.ask" title="fontforge.ask"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fontforge.ask()</span></code></a> dialog that allows them to opt-out of overwriting
the value. If they give permission you can create a dictionary as in step
1 and overwrite the existing data.</p></li>
</ol>
</section>
<section id="writing-a-fontforge-plugin">
<span id="fontforge-plugin-api"></span><h2>Writing a FontForge Plugin<a class="headerlink" href="#writing-a-fontforge-plugin" title="Link to this heading">¶</a></h2>
<p>Even when you eventually plan on writing a FontForge Python plugin it will
generally be easiest to start by writing an “Init Script”. This is a script
that, when placed in one of the directories listed by the
<a class="reference internal" href="../scripting/python/fontforge.html#fontforge.scriptPath" title="fontforge.scriptPath"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fontforge.scriptPath()</span></code></a> function, is
automatically run when FontForge is started.</p>
<p>As a simple example, here is a script that adds an “Add Midpoint Contour” entry
to the Char View Tools menu. When any two on-curve points of a glyph are
selected this adds a new contour containing a single point that is located
midway between the two selected points.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">fontforge</span>
<span class="k">def</span> <span class="nf">getSelectedPoints</span><span class="p">(</span><span class="n">l</span><span class="p">):</span>
<span class="n">pl</span> <span class="o">=</span> <span class="p">[]</span>
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">l</span><span class="p">:</span>
<span class="n">pl</span><span class="o">.</span><span class="n">extend</span><span class="p">(</span> <span class="p">[</span> <span class="n">p</span> <span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">c</span> <span class="k">if</span> <span class="n">p</span><span class="o">.</span><span class="n">on_curve</span> <span class="ow">and</span> <span class="n">p</span><span class="o">.</span><span class="n">selected</span> <span class="p">]</span> <span class="p">)</span>
<span class="k">return</span> <span class="n">pl</span>
<span class="k">def</span> <span class="nf">midContourEnable</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">glyph</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">len</span><span class="p">(</span><span class="n">getSelectedPoints</span><span class="p">(</span><span class="n">glyph</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span><span class="p">]))</span><span class="o">==</span><span class="mi">2</span>
<span class="k">def</span> <span class="nf">addMidContour</span><span class="p">(</span><span class="n">u</span><span class="p">,</span> <span class="n">glyph</span><span class="p">):</span>
<span class="n">layer_id</span> <span class="o">=</span> <span class="n">glyph</span><span class="o">.</span><span class="n">activeLayer</span>
<span class="n">l</span> <span class="o">=</span> <span class="n">glyph</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="n">layer_id</span><span class="p">]</span>
<span class="n">pl</span> <span class="o">=</span> <span class="n">getSelectedPoints</span><span class="p">(</span><span class="n">l</span><span class="p">)</span>
<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">pl</span><span class="p">)</span> <span class="o">!=</span> <span class="mi">2</span><span class="p">:</span>
<span class="n">fontforge</span><span class="o">.</span><span class="n">postError</span><span class="p">(</span><span class="s2">"Bad selection"</span><span class="p">,</span> <span class="s2">"You must select "</span>
<span class="s2">"exactly two on-curve points to add a midpoint contour."</span><span class="p">)</span>
<span class="k">return</span>
<span class="n">nc</span> <span class="o">=</span> <span class="n">fontforge</span><span class="o">.</span><span class="n">contour</span><span class="p">()</span>
<span class="n">nc</span><span class="o">.</span><span class="n">insertPoint</span><span class="p">(((</span><span class="n">pl</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">x</span><span class="o">+</span><span class="n">pl</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">x</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="n">pl</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">.</span><span class="n">y</span><span class="o">+</span><span class="n">pl</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span><span class="o">.</span><span class="n">y</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">,</span> <span class="kc">True</span><span class="p">,</span>
<span class="n">fontforge</span><span class="o">.</span><span class="n">splineCorner</span><span class="p">,</span> <span class="kc">True</span><span class="p">))</span>
<span class="n">l</span> <span class="o">+=</span> <span class="n">nc</span>
<span class="n">glyph</span><span class="o">.</span><span class="n">preserveLayerAsUndo</span><span class="p">(</span><span class="n">layer_id</span><span class="p">)</span>
<span class="n">glyph</span><span class="o">.</span><span class="n">layers</span><span class="p">[</span><span class="n">layer_id</span><span class="p">]</span> <span class="o">=</span> <span class="n">l</span>
<span class="n">fontforge</span><span class="o">.</span><span class="n">registerMenuItem</span><span class="p">(</span><span class="n">callback</span><span class="o">=</span><span class="n">addMidContour</span><span class="p">,</span> <span class="n">enable</span><span class="o">=</span><span class="n">midContourEnable</span><span class="p">,</span>
<span class="n">context</span><span class="o">=</span><span class="p">(</span><span class="s2">"Glyph"</span><span class="p">),</span> <span class="n">name</span><span class="o">=</span><span class="s2">"_Add Midpoint Contour"</span><span class="p">)</span>
</pre></div>
</div>
<p>This script is typical in that it starts with some imports, defines some
functions, and ends by invoking <a class="reference internal" href="../scripting/python/fontforge.html#fontforge.registerMenuItem" title="fontforge.registerMenuItem"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fontforge.registerMenuItem()</span></code></a>. Other
scripts may import other modules, define more elaborate functions or classes,
or register more menu items or other callbacks. Still, this is a common
general pattern.</p>
<p>The first step in converting the script to a plugin is to wrap the last
section in a function called <code class="docutils literal notranslate"><span class="pre">fontforge_plugin_init()</span></code>. Once the file
is packaged appropriately this function will be called after FontForge
discovers and loads the plugin (if the user has Enabled it).:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="k">def</span> <span class="nf">fontforge_plugin_init</span><span class="p">(</span><span class="o">**</span><span class="n">kw</span><span class="p">):</span>
<span class="n">fontforge</span><span class="o">.</span><span class="n">registerMenuItem</span><span class="p">(</span><span class="n">callback</span><span class="o">=</span><span class="n">addMidContour</span><span class="p">,</span>
<span class="n">enable</span><span class="o">=</span><span class="n">midContourEnable</span><span class="p">,</span> <span class="n">context</span><span class="o">=</span><span class="p">(</span><span class="s2">"Glyph"</span><span class="p">),</span>
<span class="n">name</span><span class="o">=</span><span class="s2">"_Add Midpoint Contour"</span><span class="p">)</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">**kw</span></code> function argument will capture any keyword arguments passed
to the function. There is currently one, discussed below, but more may
be added in the future. (Note that while the function can be located
in other places (see the section on Entry-points below) it must be a
<em>function</em>, not a <em>method</em>.</p>
<p>The remaining steps have to do with python “packaging”, which is too
large a topic to discuss extensively in this guide. A good starting
point is <a class="reference external" href="https://packaging.python.org/tutorials/packaging-projects/">Packaging Python Projects</a> from the
Python documentation. The following are the very basics.</p>
<p>First we create a new directory for our project with the following files:</p>
<ol class="arabic simple">
<li><p><code class="docutils literal notranslate"><span class="pre">fontforge_midpoint.py</span></code> (The script itself as a single module file.)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">LICENSE</span></code> (Containing the license text.)</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">README.md</span></code> (A file containing basic documentation for the plugin, in this
case written in <a class="reference external" href="https://pypi.org/project/Markdown/">Python-Mark-down format</a>.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">pyproject.toml</span></code></p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">setup.cfg</span></code> or <code class="docutils literal notranslate"><span class="pre">setup.py</span></code></p></li>
</ol>
<p><code class="docutils literal notranslate"><span class="pre">pyproject.toml</span></code> is a short helper file for the <code class="docutils literal notranslate"><span class="pre">setuptools</span></code> package
builder. It will typically have these contents:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">build</span><span class="o">-</span><span class="n">system</span><span class="p">]</span>
<span class="n">requires</span> <span class="o">=</span> <span class="p">[</span>
<span class="s2">"setuptools>=42"</span><span class="p">,</span>
<span class="s2">"wheel"</span>
<span class="p">]</span>
<span class="n">build</span><span class="o">-</span><span class="n">backend</span> <span class="o">=</span> <span class="s2">"setuptools.build_meta"</span>
</pre></div>
</div>
<p>Finally there is the <code class="docutils literal notranslate"><span class="pre">setup.cfg</span></code> file (or, less typically these days, the
<code class="docutils literal notranslate"><span class="pre">setup.py</span></code> file). It should have contents analogous to these:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">metadata</span><span class="p">]</span>
<span class="n">name</span> <span class="o">=</span> <span class="n">fontforge</span><span class="o">-</span><span class="n">midcontour</span>
<span class="n">version</span> <span class="o">=</span> <span class="mf">1.0.0</span>
<span class="n">author</span> <span class="o">=</span> <span class="n">Example</span> <span class="n">Author</span>
<span class="n">author_email</span> <span class="o">=</span> <span class="n">author</span><span class="nd">@example</span><span class="o">.</span><span class="n">com</span>
<span class="n">description</span> <span class="o">=</span> <span class="n">A</span> <span class="n">FontForge_plugin</span> <span class="n">to</span> <span class="n">add</span> <span class="n">a</span> <span class="n">single</span><span class="o">-</span><span class="n">point</span> <span class="n">contour</span> <span class="n">between</span> <span class="n">points</span>
<span class="n">long_description</span> <span class="o">=</span> <span class="n">file</span><span class="p">:</span> <span class="n">README</span><span class="o">.</span><span class="n">md</span>
<span class="n">long_description_content_type</span> <span class="o">=</span> <span class="n">text</span><span class="o">/</span><span class="n">markdown</span>
<span class="n">url</span> <span class="o">=</span> <span class="n">https</span><span class="p">::</span><span class="o">/</span><span class="n">github</span><span class="o">.</span><span class="n">com</span><span class="o">/</span><span class="n">author</span><span class="o">/</span><span class="n">MidContour</span>
<span class="n">classifiers</span> <span class="o">=</span>
<span class="n">Programming</span> <span class="n">Language</span> <span class="p">::</span> <span class="n">Python</span> <span class="p">::</span> <span class="mi">3</span>
<span class="n">License</span> <span class="p">::</span> <span class="n">OSI</span> <span class="n">Approved</span> <span class="p">::</span> <span class="n">MIT</span> <span class="n">License</span>
<span class="n">Operating</span> <span class="n">System</span> <span class="p">::</span> <span class="n">OS</span> <span class="n">Independent</span>
<span class="n">Topic</span> <span class="p">::</span> <span class="n">Text</span> <span class="n">Processing</span> <span class="p">::</span> <span class="n">Fonts</span>
<span class="p">[</span><span class="n">options</span><span class="p">]</span>
<span class="n">py_modules</span> <span class="o">=</span> <span class="n">fontforge_midcontour</span>
<span class="n">python_requires</span> <span class="o">=</span> <span class="o">>=</span><span class="mf">3.6</span>
<span class="p">[</span><span class="n">options</span><span class="o">.</span><span class="n">entry_points</span><span class="p">]</span>
<span class="n">fontforge_plugin</span> <span class="o">=</span>
<span class="n">MidContour</span> <span class="o">=</span> <span class="n">fontforge_midcontour</span>
</pre></div>
</div>
<p>The <code class="docutils literal notranslate"><span class="pre">name</span></code> field is the name of the <em>package</em>. Recent Python documentation
encourages developers to add their PyPI username to the package name but
this practice has not been widely adopted. For now we recommend that you
include “fontforge” in the package name along with a string that either
specifically identifies what your plugin does or is just a distinctive
project name. Therefore ambiguous names like “Point” or “Contour” should
be avoided as these are more likely to also be used by someone else. Please
search the PyPI database for a candidate name to help ensure it is not already
in use. Package names should be all lower case but can contain dashes and
underscores.</p>
<p>The <code class="docutils literal notranslate"><span class="pre">url</span></code> should point to your project page or a GitHub (or other such
service) repository for the plugin code.</p>
<p>Obviously the <code class="docutils literal notranslate"><span class="pre">description</span></code> field should contain a one-line description of
the plugin. However, it is important that this description (or, if that is not
possible, the README file) contain the exact string “FontForge_plugin”.
FontForge does not maintain its own database of plugins; it instead links to
the PyPI database query system passing that string.</p>
<p><code class="docutils literal notranslate"><span class="pre">classifiers</span></code> help users people searching on PyPI to find
relevant packages (although the list of classifiers is, unfortunately, fixed).
This is a reasonable selection.</p>
<p><code class="docutils literal notranslate"><span class="pre">py_modules</span></code> should be the list of modules provided by the package without
any <code class="docutils literal notranslate"><span class="pre">.py</span></code> extensions. (More complicated packages with sub-directories
could benefit from using <code class="docutils literal notranslate"><span class="pre">packages</span> <span class="pre">=</span> <span class="pre">find:</span></code> instead.</p>
<p>The last directives specify the Python package “Entry-points”, which are
the basis of FontForge’s discovery system. The Entry-point identifier
must be <code class="docutils literal notranslate"><span class="pre">fontforge_plugin</span></code>.</p>
<p>The token on the left of the indented line is the <em>plugin name</em>. This will
appear in the “Configure Plugins…” dialog and other contexts. It will
normally be the name of your package except without “fontforge” and with
optional capitalization. It can contain the usual
alphanumerics-plus-underscores and also spaces (although spaces are not
recommended).</p>
<p>The token on the right of the equals sign identifies the location of the
<code class="docutils literal notranslate"><span class="pre">fontforge_plugin_init</span></code> function. In this case it is just at the top
level of the module so the identifier is just the module name. If it were
instead a property of object “MC” in that module the string would be
<code class="docutils literal notranslate"><span class="pre">fontforge_midcontour.MC</span></code>.</p>
<p>In some cases you may want to use a <code class="docutils literal notranslate"><span class="pre">setup.py</span></code> file instead. The file
equivalent to the <code class="docutils literal notranslate"><span class="pre">setup.cfg</span></code> above is:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">setuptools</span>
<span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s2">"README.md"</span><span class="p">,</span> <span class="s2">"r"</span><span class="p">,</span> <span class="n">encoding</span><span class="o">=</span><span class="s2">"utf-8"</span><span class="p">)</span> <span class="k">as</span> <span class="n">fh</span><span class="p">:</span>
<span class="n">long_description</span> <span class="o">=</span> <span class="n">fh</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">setuptools</span><span class="o">.</span><span class="n">setup</span><span class="p">(</span>
<span class="n">name</span><span class="o">=</span><span class="s2">"fontforge_midcontour-author"</span><span class="p">,</span>
<span class="n">version</span><span class="o">=</span><span class="s2">"1.0.0"</span><span class="p">,</span>
<span class="n">author</span><span class="o">=</span><span class="s2">"Example Author"</span><span class="p">,</span>
<span class="n">author_email</span><span class="o">=</span><span class="s2">"author@example.com"</span><span class="p">,</span>
<span class="n">description</span><span class="o">=</span><span class="s2">"A FontForge_plugin to add a single-point contour between points"</span><span class="p">,</span>
<span class="n">long_description</span> <span class="o">=</span> <span class="n">long_description</span><span class="p">,</span>
<span class="n">long_description_content_type</span> <span class="o">=</span> <span class="s2">"text/markdown"</span><span class="p">,</span>
<span class="n">url</span><span class="o">=</span><span class="s2">"https::/github.com/author/MidContour"</span><span class="p">,</span>
<span class="n">py_modules</span><span class="o">=</span><span class="p">[</span><span class="s2">"fontforge_midcontour"</span><span class="p">],</span>
<span class="n">classifiers</span><span class="o">=</span><span class="p">[</span>
<span class="s2">"Programming Language :: Python :: 3"</span><span class="p">,</span>
<span class="s2">"License :: OSI Approved :: MIT License"</span><span class="p">,</span>
<span class="s2">"Operating System :: OS Independent"</span><span class="p">,</span>
<span class="s2">"Topic :: Text Processing :: Fonts"</span><span class="p">,</span>
<span class="p">],</span>
<span class="n">python_requires</span><span class="o">=</span><span class="s2">">=3.6"</span><span class="p">,</span>
<span class="n">entry_points</span><span class="o">=</span><span class="p">{</span> <span class="s1">'fontforge_plugin'</span><span class="p">:</span> <span class="p">[</span> <span class="s1">'MidContour = fontforge_midcontour'</span> <span class="p">],},</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Once the directory has the five files with the appropriate contents you can
build the package by entering the directory and running <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">build</span></code> .
If there are no errors the package archive will be added to the <code class="docutils literal notranslate"><span class="pre">dist</span></code>
subdirectory (which will be created if necessary). This package can be
installed directly with <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">[name]</span></code> or <a class="reference external" href="https://realpython.com/pypi-publish-python-package/#publishing-to-pypi">published on PyPI</a>.</p>
<section id="marking-dependencies">
<h3>Marking Dependencies<a class="headerlink" href="#marking-dependencies" title="Link to this heading">¶</a></h3>
<p>A simple plugin may have no dependencies beyond FontForge itself, and because
only FontForge will attempt to discover and load plugins it is not necessary
to mark that dependency. Other plugins may import numerical processing modules
or use <code class="docutils literal notranslate"><span class="pre">tkinter</span></code> to raise dialogs.</p>
<p>“Hard” dependencies—those that a plugin cannot operate without—should be marked
in the <code class="docutils literal notranslate"><span class="pre">[options]</span></code> section of <code class="docutils literal notranslate"><span class="pre">setup.cfg</span></code> with an <code class="docutils literal notranslate"><span class="pre">install_requires</span></code>
directive. If any version of the package will suffice just use its name. If
your code requires a minimum version add <code class="docutils literal notranslate"><span class="pre">>=</span></code> and then that version. For
example, these directives specify that <code class="docutils literal notranslate"><span class="pre">tkinter</span></code> and <code class="docutils literal notranslate"><span class="pre">numpy</span></code> must be
installed, with a minimum <code class="docutils literal notranslate"><span class="pre">numpy</span></code> version of 1.19.3:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="n">options</span><span class="p">]</span>
<span class="o">...</span>
<span class="n">install_requires</span>
<span class="n">tkinter</span>
<span class="n">numpy</span><span class="o">>=</span><span class="mf">1.19.3</span>
</pre></div>
</div>
<p>Use of the more complex <code class="docutils literal notranslate"><span class="pre">requirements.txt</span></code> Python dependency mechanism is not
recommended, as it implies more control over the Python environment than makes
sense for a FontForge plugin.</p>
<p>When possible you should avoid dependencies on Python packages that require
compilation, either because they are written in another language or because
they rely on “external” libraries. The Windows version of FontForge has an
embedded Python environment that is not capable of compiling packages, so
(at least for now) Windows users will not be able to use your plugin if it
has such dependencies.</p>
</section>
<section id="the-configuration-system">
<h3>The Configuration System<a class="headerlink" href="#the-configuration-system" title="Link to this heading">¶</a></h3>
<p>The plugin API offers an optional convention for setting and storing plugin-
specific parameters. It has two parts:</p>
<ol class="arabic">
<li><p>The initializing call to <code class="docutils literal notranslate"><span class="pre">fontforge_plugin_init</span></code> is passed a keyword
parameter <code class="docutils literal notranslate"><span class="pre">preferences_path</span></code>. This is a filesystem path that points to
an appropriate location for storing plugin data. It is recommended that
plugins either add an appropriate extension to the path (if they only
need a single file) or to create a directory of that name and store
files within it. The content should point to the same directory on every
initialization unless the name of the plugin is changed.</p>
<p>The parameter can be extracted from the <code class="docutils literal notranslate"><span class="pre">**kw</span></code> dictionary (see above)
or caught explicitly by changing the definition to
<code class="docutils literal notranslate"><span class="pre">def</span> <span class="pre">fontforge_plugin_init(preferences_path=None,</span> <span class="pre">**kw):</span></code></p>
</li>
<li><p>Plugin configuration is triggered by a call to <code class="docutils literal notranslate"><span class="pre">fontforge_plugin_config</span></code>,
which is a function defined on the same module or object as
<code class="docutils literal notranslate"><span class="pre">fontforge_plugin_init</span></code>. If a plugin defines that function it will be
called when a user presses the “Configure” button in the Plugin dialog.
If <code class="docutils literal notranslate"><span class="pre">fontforge_plugin_init</span></code> is <em>not</em> defined the button will be disabled.</p></li>
</ol>
<p>Beyond this API it is entirely up to the plugin to store, retrieve, and offer
configuration choices to the user. Until FontForge provides more dialog choices
the latter may be difficult without resorting to <code class="docutils literal notranslate"><span class="pre">tkinter</span></code>. It is also possible
to support file-based configuration and use an <a class="reference internal" href="../scripting/python/fontforge.html#fontforge.openFilename" title="fontforge.openFilename"><code class="xref py py-meth docutils literal notranslate"><span class="pre">fontforge.openFilename()</span></code></a>
dialog to ask for the file so that it can be copied into the appropriate location.</p>
</section>
</section>
</section>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h1 class="logo"><a href="../index.html">FontForge</a></h1>
<h3>Navigation</h3>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../tutorial.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../ui.html">FontForge’s User Interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="../scripting/scripting.html">Scripting FontForge</a></li>
<li class="toctree-l1 current"><a class="reference internal" href="../techref.html">Technical References</a><ul class="current">
<li class="toctree-l2"><a class="reference internal" href="gposgsub.html">Advanced Typography Tables</a></li>
<li class="toctree-l2"><a class="reference internal" href="AA-Comparison.html">Anti-Alias Comparison</a></li>
<li class="toctree-l2"><a class="reference internal" href="autotrace.html">Autotracing bitmaps in FontForge</a></li>
<li class="toctree-l2"><a class="reference internal" href="accented.html">Building Accented and other Composite Glyphs</a></li>
<li class="toctree-l2"><a class="reference internal" href="bezier.html">Bézier Splines </a></li>
<li class="toctree-l2"><a class="reference internal" href="askmulti.html">The <cite>askMulti</cite> Dialog</a></li>
<li class="toctree-l2"><a class="reference internal" href="ref-caveats.html">Caveats about References</a></li>
<li class="toctree-l2"><a class="reference internal" href="sfdchangelog.html">Changes to the sfd format</a></li>
<li class="toctree-l2"><a class="reference internal" href="cidmapformat.html">Cidmap files</a></li>
<li class="toctree-l2"><a class="reference internal" href="cliargs.html">Command Line Arguments</a></li>
<li class="toctree-l2"><a class="reference internal" href="splinefont.html">Data Types</a></li>
<li class="toctree-l2"><a class="reference internal" href="SuggestDeltas.html">Delta Instruction Suggestions</a></li>
<li class="toctree-l2"><a class="reference internal" href="stroke.html">Expand Stroke Facility</a></li>
<li class="toctree-l2 current"><a class="current reference internal" href="#">Extending FontForge With Python</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#modifying-points-and-contours-references-and-anchor-points">Modifying Points and Contours, References, and Anchor Points</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#the-basics">The Basics</a></li>
<li class="toctree-l4"><a class="reference internal" href="#layer-awareness-and-undo-preservation">Layer Awareness and Undo Preservation</a></li>
<li class="toctree-l4"><a class="reference internal" href="#references">References</a></li>
<li class="toctree-l4"><a class="reference internal" href="#anchor-points">Anchor Points</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#adding-a-menu-item">Adding a Menu Item</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#selection-state">Selection State</a></li>
<li class="toctree-l4"><a class="reference internal" href="#enable-functions">Enable Functions</a></li>
<li class="toctree-l4"><a class="reference internal" href="#hotkeys-and-mnemonics">Hotkeys and Mnemonics</a></li>
<li class="toctree-l4"><a class="reference internal" href="#best-practices">Best Practices</a></li>
</ul>
</li>
<li class="toctree-l3"><a class="reference internal" href="#storing-per-font-and-per-glyph-information">Storing Per-Font and Per-Glyph Information</a></li>
<li class="toctree-l3"><a class="reference internal" href="#writing-a-fontforge-plugin">Writing a FontForge Plugin</a><ul>
<li class="toctree-l4"><a class="reference internal" href="#marking-dependencies">Marking Dependencies</a></li>
<li class="toctree-l4"><a class="reference internal" href="#the-configuration-system">The Configuration System</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l2"><a class="reference internal" href="BDFGrey.html">Extensions to Adobe’s BDF for greymap fonts</a></li>
<li class="toctree-l2"><a class="reference internal" href="PfaEdit-TeX.html">FontForge and TeX</a></li>
<li class="toctree-l2"><a class="reference internal" href="pfaeditmath.html">FontForge’s math</a></li>
<li class="toctree-l2"><a class="reference internal" href="hinting.html">Hinting</a></li>
<li class="toctree-l2"><a class="reference internal" href="macformats.html">Macintosh font formats</a></li>
<li class="toctree-l2"><a class="reference internal" href="featurefile.html">Major differences between FontForge’s and Adobe’s interpretation of feature files</a></li>
<li class="toctree-l2"><a class="reference internal" href="non-standard.html">Non standard extensions used FontForge in True/Open Type</a></li>
<li class="toctree-l2"><a class="reference internal" href="palmfonts.html">Palm fonts</a></li>
<li class="toctree-l2"><a class="reference internal" href="plugins.html">Using FontForge Plugins</a></li>
<li class="toctree-l2"><a class="reference internal" href="bitmaponlysfnt.html">Several formats for bitmap only sfnts</a></li>
<li class="toctree-l2"><a class="reference internal" href="sfdformat.html">Spline Font Database</a></li>
<li class="toctree-l2"><a class="reference internal" href="pcf-format.html">The X11 PCF bitmap font file format</a></li>
<li class="toctree-l2"><a class="reference internal" href="TrueOpenTables.html">TrueType and OpenType tables supported by FontForge</a></li>
<li class="toctree-l2"><a class="reference internal" href="corpchar.html">Unicode Corporate Characters used by FontForge</a></li>
<li class="toctree-l2"><a class="reference internal" href="UniqueID.html">UniqueID and XUID</a></li>
<li class="toctree-l2"><a class="reference internal" href="selections.html">X Selections and the X Clipboard</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="../fontutils.html">Utilities for examining fonts</a></li>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../appendices.html">Appendices</a></li>
<li class="toctree-l1"><a class="reference internal" href="../glossary.html">Typographical glossary</a></li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li><a href="../techref.html">Technical References</a><ul>
<li>Previous: <a href="stroke.html" title="previous chapter">Expand Stroke Facility</a></li>
<li>Next: <a href="BDFGrey.html" title="next chapter">Extensions to Adobe’s BDF for greymap fonts</a></li>
</ul></li>
</ul></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3 id="searchlabel">Quick search</h3>
<div class="searchformwrapper">
<form class="search" action="../search.html" method="get">
<input type="text" name="q" aria-labelledby="searchlabel" autocomplete="off" autocorrect="off" autocapitalize="off" spellcheck="false"/>
<input type="submit" value="Go" />
</form>
</div>
</div>
<script>document.getElementById('searchbox').style.display = "block"</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2000-2012 by George Williams, 2012-2025 by FontForge authors.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 7.2.6</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.12</a>
</div>
</body>
</html>