File: scripting.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 (1265 lines) | stat: -rw-r--r-- 33,705 bytes parent folder | download | duplicates (3)
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
Scripting FontForge
===================

FontForge includes two interpreters so you can write scripts to modify fonts.
One of these interpreters is :doc:`python </scripting/python>`, one is a legacy language I
came up with. FontForge may be configured with either or both of these. If
configured with both then fontforge will make a guess at which to use based on
the script file's extension ("py" means use the python interpreter, "ff" or "pe"
means use the old interpreter)

.. toctree::

   scripting-alpha
   python

.. contents::
   :backlinks: none

.. default-domain:: ff

.. _scripting.Starting:

Invoking scripts
----------------

If you start fontforge with a script on the command line it will not put up any
windows and it will exit when the script is done. The script can be in a file,
or just a string presented as an argument. You may need to specify which
interpreter to use with the -lang argument.

::

   $ fontforge -script scriptfile.pe {arguments}
   $ fontforge -c "script-string" {arguments}
   $ fontforge -lang={ff|py} -c "script-string"

FontForge can also be used as an interpreter to which the shell will
automatically pass scripts. If you a mark your script files as executable ::

   $ chmod +x scriptfile.pe

and begin each one with the line ::

   #!/usr/local/bin/fontforge

(or wherever fontforge happens to reside on your system) then you can invoke the
script just by typing ::

   $ scriptfile.pe {fontnames}

If you wish FontForge to read a script from stdin then you can use "-" as a
"filename" for stdin. (If you build FontForge without X11 then fontforge will
attempt to read a script file from ``stdin`` if none is given on the command
line.)

You can also start a script from within FontForge with
:menuselection:`File --> Execute Script`, and you can use the Preference Dlg to
define a set of frequently used scripts which can be invoked directly by menu.

The scripting language provides access to much of the functionality found in the
font view's menus. It does not currently (and probably never will) provide
access to everything. (If you find a lack let me know, I may put it in for you).
It does not provide commands for building up a glyph out of splines, instead it
allows you to do high level modifications to glyphs.

If you set the environment variable ``FONTFORGE_VERBOSE`` (it doesn't need a
value, just needs to be set) then FontForge will print scripts to stdout as it
executes them.

You may set the environment variable ``FONTFORGE_LANGUAGE`` to either "py" (for
python) or "ff" or "pe" (for native scripting) as another way to determne what
interpreter to use.


.. _scripting.Language:

Scripting Language
------------------

.. note::

   This section covers FontForge's native scripting functionality.

   For the Python scripting functionality, see :doc:`here </scripting/python>`.

The syntax is rather like a mixture of C and shell commands. Every file
corresponds to a procedure. As in a shell script arguments passed to the file
are identified as $1, $2, ... $n. $0 is the file name itself. $argc gives the
number of arguments. $argv[<expr>] provides array access to the arguments.

Terms can be

* A variable name (like "$1" or "i" or "@fontvar" or "_global")

  The scope of the variable depends on the initial character of its name.

  * A '$' signifies that it is a built-in variable. The user cannot create any new
    variables beginning with '$'. Some, but not all, of these may be assigned to.
  * A '_' signifies that the variable is global, it is always available. You can use
    these to store context across different script files (or to access data within
    nested script files).
  * A '@' signifies that the variable is associated with the font. Any two scripts
    looking at the same font will have access to the same variables.
  * A variable which begins with a letter is a local variable. It is only meaningful
    within the current script file. Nested script files may have different variables
    with the same names.
* an integer expressed in decimal, hex or octal
* a unicode code point (which has a prefix of "0u" or "0U" and is followed by a
  string of hex digits. This is only used by the select command.
* a real number (in "C" locale format -- "." as the decimal point character)
* A string which may be enclosed in either double or single quotes. String tokens
  are limited to 256 bytes. "\n" can be used to represent a newline character. (If
  you need longer strings use the concatenation operator).
* a procedure to call or file to invoke.
* an expression within parentheses
* a series of expressions (separated by commas) within brackets, as
  ``[1, 2, 3, 5, 8]``, used to create an array.

There are three different comments supported:

* Starting with a "#" character and proceeding to end of line
* Starting with "//" and proceeding to end of line
* Starting with "/*" and proceeding to "*/"

.. _scripting.Expressions:

Expressions
^^^^^^^^^^^

Expressions are similar to those in C, a few operators have been omitted, a few
added from shell scripts. Operator precedence has been simplified slightly. So
operators (and their precedences) are:

* unary operators (+, -, !, ~, ++ (prefix and postfix), --(prefix and postfix), ()
  (procedure call), [] (array index), :h, :t, :r, :e

  Most of these are as expected in C, the last four are borrowed from shell
  scripts and are applied to strings

  * :h gives the head (directory) of a pathspec
  * :t gives the tail (filename) of a pathspec
  * :r gives the pathspec without the extension (if any)
  * :e gives the extension
* \*, /, % (binary multiplicative operators)
* +, - (binary arithmetic operators)

  If the first operand of + is a string then + will be treated as concatenation
  rather than addition. If the second operand is a number it will be converted to
  a string (decimal representation) and then concatenated. If the first operand of
  + is an array then + will do array concatenation -- if the second argument is
  also an array the two will be concatenated ([1,2] + [3,4] yields [1,2,3,4],
  while [1,2] + 3 yields [1,2,3]). Otherwise these are the normal arithmetric
  operations.
* ==, !=, >, <, >=, <= (comparison operators, may be applied to either two
  integers or two strings)
* &&, & (logical and, bitwise and. (logical and will do short circuit evaluation))
* \|\|, \|, ^ (logical or, bitwise or, bitwise exclusive or (logical or will do short
  circuit evaluation))
* =, +=, -=, \*=, /=, %= (assignment operators as in C. The += will act as
  concatenation if the first operand is a string.)

Note there is no comma operator, and no "?:" operator. The precedence of "and"
and "or" has been simplified, as has that of the assignment operators.

Procedure calls may be applied either to a name token, or to a string. If the
name or string is recognized as one of FontForge's internal procedures it will
be executed, otherwise it will be assumed to be a filename containing another
fontforge script file, this file will be invoked (since filenames can contain
characters not legal in name tokens it is important to allow general strings to
specify filenames). If the procedure name does not contain a directory then it
is assumed to be in the same directory as the current script file. As a
convenience, it is not necessary to include the script's extension if it is
either ".ff" or ".pe". At most 25 arguments can be passed to a procedure.

Arrays are passed by reference, strings and integers are passed by value.

Variables may be created by assigning a value to them (only with the "="), so: ::

   i=3

could be used to define "i" as a variable. Variables are limited in scope to the
current file, they will not be inherited by called procedures.

A statement may be

* an expression
* ::

       if ( expression )
           statements
       {elseif ( expression )
           statements}
       [else
           statements]
       endif
* ::

       while ( expression )
           statements
       endloop
* ::

       foreach
           statements
       endloop

* ``break``
* ``return [ expression ]``
* ``shift``

As with C, non-zero expressions are defined to be true.

A return statement may be followed by a return value (the expression) or a
procedure may return nothing (void).

The shift statement is stolen from shell scripts and shifts all arguments down
by one. (argument 0, the name of the script file, remains unchanged.

The foreach statement requires that there be a current font. It executes the
statements once for each glyph in the selection. Within the statements only one
glyph at a time will be selected. After execution the selection will be restored
to what it was initially. (Caveat: Don't reencode the font within a foreach
statement).

Statements are terminated either by a new line (you can break up long lines with
backslash newline) or a semicolon.

Trivial example:

::

   i=0;   #semicolon is not needed here, but it's ok
   while ( i<3 )
      if ( i==1 /* pointless comment */ )
           Print( "Got to one" )   // Another comment
      endif
      ++i
   endloop

FontForge maintains the concept of a "current font"-- almost all commands refer
only to the current font (and require that there be a font). If you start a
script with :menuselection:`File --> Execute Script`, the font you were editing
will be current, otherwise there will be no initial current font. The Open(),
New() and Close() commands all change the current font. FontForge also maintains
a list of all fonts that are currently open. This list is in no particular
order. The list starts with $firstfont.

Similarly when working with cid keyed fonts, FontForge works in the "current sub
font", and most commands refer to this font. The CIDChangeSubFont() command can
alter that.

.. _scripting.variables:

Variables
^^^^^^^^^

All builtin variables begin with "$", you may not create any variables that
start with "$" yourself (though you may assign to (some) already existing ones)

.. data:: $0

   the current script filename

.. data:: $1
          $2
          $n

   the first, second (...) argument to the script file

.. data:: $argc

   the number of arguments passed to the script file (this will always be
   at least 1 as $0 is always present)

.. data:: $argv

   allows you to access the array of all the arguments

.. data:: $curfont

   the name of the filename in which the current font resides

.. data:: $firstfont

   the name of the filename of the font which is first on the font
   list (Can be used by :func:`Open()`), if there are no fonts loaded this
   returns an empty string. This can be used to determine if any font at all
   is loaded into fontforge.

.. data:: $nextfont

   the name of the filename of the font which follows the current
   font on the list (or the empty string if the current font is the last one on the
   list)

.. data:: $fontchanged

   returns 1 if the current font has changed, 0 if it has not
   changed since it was read in (or saved).

.. data:: $fontname

   the name contained in the postscript FontName field

.. data:: $familyname

   the name contained in the postscript FamilyName field

.. data:: $fullname

   the name contained in the postscript FullName field

.. data:: $fondname

   if set this name indicates what FOND the current font should be
   put in under Generate Mac Family.

.. data:: $weight

   the name contained in the postscript Weight field

.. data:: $copyright

   the name contained in the postscript Notice field

.. data:: $filename

   the name of the file containing the font.

.. data:: $fontversion

   the string containing the font's version

.. data:: $iscid

   1 if the current font is a cid keyed font, 0 if not

.. data:: $cidfontname

   returns the fontname of the top-level cid-keyed font (or the
   empty string if there is none)

   Can be used to detect if this is a cid keyed font.

.. data:: $cidfamilyname, $cidfullname, $cidweight, $cidcopyright

   similar to :data:`cidfontname`

.. data:: $mmcount

   returns 0 for non multiple master fonts, returns the number of
   instances in a multiple master font.

.. data:: $italicangle

   the value of the postscript italic angle field

.. data:: $loadState

   a bitmask of non-fatal errors encountered when loading the font.

.. data:: $privateState

   a bitmask of some errors in the PostScript Private dictionary
   (see :py:attr:`fontforge.font.privateState` for more info).

.. data:: $curcid

   returns the fontname of the current font

.. data:: $firstcid

   returns the fontname of the first font within this cid font

.. data:: $nextcid

   returns the fontname of the next font within this cid font (or the
   empty string if the current sub-font is the last)

.. data:: $macstyle

   returns the value of the macstyle field (a set of bits indicating
   whether the font is bold, italic, condensed, etc.)

.. data:: $bitmaps

   returns an array containing all bitmap pixelsizes generated for
   this font. (If the font database contains greymaps then they will be indicated
   in the array as ``(<BitmapDepth><<16)|<PixelSize>``)

.. data:: $order

   returns an integer containing either 2 (for truetype fonts) or 3 (for
   postscript fonts). This indicates whether the font uses quadratic or cubic
   splines.

.. data:: $em

   returns the number of em-units used by the font.

.. data:: $ascent

   returns the ascent of the font

.. data:: $descent

   returns the descent of the font.

.. data:: $selection

   returns an array containing one entry for each glyph in the
   current font indicating whether that glyph is selected or not (0=>not,
   1=>selected)

.. data:: $panose

   returns an array containing the 10 panose values for the font.

.. data:: $trace

   if this is set to one then FontForge will trace each procedure call.

.. data:: $version

   returns a string containing the current version of fontforge. This
    should look something like "20050817".

.. data:: $haspython

   returns 1 if python scripting is available, 0 if it is not.


.. _scripting.Preference:

Preferences
^^^^^^^^^^^

* ``$<Preference Item>`` (for example ``$AutoHint``) allows you to examine the
  value of that preference item (to set it use
  :func:`SetPref()`)

The following example will perform an action on all loaded fonts:

::

   file = $firstfont
   while ( file != "" )
      Open(file)
      /* Do Stuff */
      file = $nextfont
   endloop

.. _scripting.procedures:

The built in procedures are very similar to the menu items with the same names.
Often the description here is sketchy, look at the menu item for more
information.

* :doc:`Built-in procedures in alphabetic order <scripting-alpha>`
* :ref:`Built-in procedures that do not require a font <scripting.no-font>`
* :ref:`File menu <scripting.file-menu>`
* :ref:`File manipulation <scripting.files>`
* :ref:`Edit menu <scripting.edit-menu>`
* :ref:`Select menu <scripting.select-menu>`
* :ref:`Element menu <scripting.element-menu>`
* :ref:`Font information <scripting.font-info>`
* :ref:`Glyph information <scripting.glyph-info>`
* :ref:`Advanced Typography <scripting.ATT>`
* :ref:`Encoding menu <scripting.encoding-menu>`
* :ref:`Hint menu <scripting.hint-menu>`
* :ref:`Metrics menu <scripting.metrics-menu>`
* :ref:`Multiple master <scripting.MM>`
* :ref:`CID keyed fonts <scripting.CID>`
* :ref:`User interaction <scripting.user>`
* :ref:`Preferences <scripting.prefs>`
* :ref:`Math <scripting.math>`
* :ref:`Unicode <scripting.unicode>`
* :ref:`String manipulation <scripting.strings>`
* :ref:`Character manipulation <scripting.Character>`
* :ref:`Arrays <scripting.arrays>`
* :ref:`Miscellaneous <scripting.misc>`
* :ref:`Deprecated Names <scripting.Deprecated>`


.. _scripting.alpha:

Built-in procedures in alphabetic order
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. _scripting.no-font:

Built-in procedures that do not require a loaded font
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* :func:`Array()`
* :func:`AskUser()`
* :func:`ATan2()`
* :func:`Ceil()`
* :func:`Chr()`
* :func:`Cos()`
* :func:`Floor()`
* :func:`Error()`
* :func:`Exp()`
* :func:`DefaultOtherSubrs()`
* :func:`FileAccess()`
* :func:`FontsInFile()`
* :func:`GetEnv()`
* :func:`GetPref()`
* :func:`Int()`
* :func:`IsAlNum()`
* :func:`IsAlpha()`
* :func:`IsDigit()`
* :func:`IsFinite()`
* :func:`IsHexDigit()`
* :func:`IsLower()`
* :func:`IsNan()`
* :func:`IsSpace()`
* :func:`IsUpper()`
* :func:`LoadEncodingFile()`
* :func:`LoadNamelist()`
* :func:`LoadNamelistDir()`
* :func:`LoadPrefs()`
* :func:`LoadStringFromFile()`
* :func:`Log()`
* :func:`NameFromUnicode()`
* :func:`New()`
* :func:`Open()`
* :func:`Ord()`
* :func:`PostNotice()`
* :func:`Pow()`
* :func:`PreloadCidmap()`
* :func:`Print()`
* :func:`Rand()`
* :func:`ReadOtherSubrsFile()`
* :func:`Real()`
* :func:`Round()`
* :func:`SavePrefs()`
* :func:`SetPref()`
* :func:`SizeOf()`
* :func:`Sin()`
* :func:`Sqrt()`
* :func:`Strcasestr()`
* :func:`Strcasecmp()`
* :func:`Strftime()`
* :func:`StrJoin()`
* :func:`Strlen()`
* :func:`Strrstr()`
* :func:`Strskipint()`
* :func:`StrSplit()`
* :func:`Strstr()`
* :func:`Strsub()`
* :func:`Strtod()`
* :func:`Strtol()`
* :func:`Tan()`
* :func:`ToLower()`
* :func:`ToMirror()`
* :func:`ToString()`
* :func:`ToUpper()`
* :func:`TypeOf()`
* :func:`UCodePoint()`
* :func:`UnicodeFromName()`
* :func:`Ucs4()`
* :func:`Utf8()`
* :func:`WriteStringToFile()`


.. _scripting.file-menu:

Built-in procedures that act like the File Menu
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* :func:`Close()`
* :func:`Export()`
* :func:`FontsInFile()`
* :func:`Generate()`
* :func:`GenerateFamily()`
* :func:`Import()`
* :func:`MergeKern()` deprecated
* :func:`MergeFeature()`
* :func:`New()`
* :func:`Open()`
* :func:`PrintFont()`
* :func:`PrintSetup()`
* :func:`Quit()`
* :func:`Revert()`
* :func:`RevertToBackup()`
* :func:`Save()`


.. _scripting.files:

File Manipulation
^^^^^^^^^^^^^^^^^

* :func:`FileAccess()`
* :func:`FontImage()`
* :func:`LoadStringFromFile()`
* :func:`WriteStringToFile()`


.. _scripting.edit-menu:

Built-in procedures that act like the Edit Menu
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* :func:`Clear()`
* :func:`ClearBackground()`
* :func:`Copy()`
* :func:`CopyAnchors()`
* :func:`CopyFgToBg()`
* :func:`CopyLBearing()`
* :func:`CopyRBearing()`
* :func:`CopyReference()`
* :func:`CopyUnlinked()`
* :func:`CopyVWidth()`
* :func:`CopyWidth()`
* :func:`Cut()`
* :func:`Join()`
* :func:`Paste()`
* :func:`Paste()`
* :func:`PasteWithOffset()`
* :func:`ReplaceWithReference()`
* :func:`SameGlyphAs()`
* :func:`UnlinkReference()`


.. _scripting.select-menu:

Built-in procedures that act like the Select Menu
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* :func:`Select()`
* :func:`SelectAll()`
* :func:`SelectAllInstancesOf()`
* :func:`SelectBitmap()`
* :func:`SelectByPosSub()`
* :func:`SelectChanged()`
* :func:`SelectFewer()`
* :func:`SelectFewerSingletons()`
* :func:`SelectGlyphsBoth()`
* :func:`SelectGlyphsReferences()`
* :func:`SelectGlyphsSplines()`
* :func:`SelectHintingNeeded()`
* :func:`SelectIf()`
* :func:`SelectInvert()`
* :func:`SelectMore()`
* :func:`SelectMoreIf()`
* :func:`SelectMoreSingletons()`
* :func:`SelectMoreSingletonsIf()`
* :func:`SelectNone()`
* :func:`SelectSingletons()`
* :func:`SelectSingletonsIf()`
* :func:`SelectWorthOutputting()`


.. _scripting.element-menu:

Built-in procedures that act like the Element Menu
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* :func:`AddAccent()`
* :func:`AddExtrema()`
* :func:`AddInflections()`
* :func:`ApplySubstitution()`
* :func:`AutoTrace()`
* :func:`Balance()`
* :func:`BitmapsAvail()`
* :func:`BitmapsRegen()`
* :func:`BuildAccented()`
* :func:`BuildComposite()`
* :func:`BuildDuplicate()`
* :func:`CanonicalContours()`
* :func:`CanonicalStart()`
* :func:`ChangeWeight()`
* :func:`CompareFonts()`
* :func:`CompareGlyphs()`
* :func:`CorrectDirection()`
* :func:`DefaultRoundToGrid()`
* :func:`DefaultUseMyMetrics()`
* :func:`ExpandStroke()`
* :func:`FindIntersections()`
* :func:`Harmonize()`
* :func:`HFlip()`
* :func:`Inline()`
* :func:`InterpolateFonts()`
* :func:`Italic()`
* :func:`MergeFonts()`
* :func:`Move()`
* :func:`MoveReference()`
* :func:`NearlyHvCps()`
* :func:`NearlyHvLines()`
* :func:`NearlyLines()`
* :func:`NonLinearTransform()`
* :func:`Outline()`
* :func:`OverlapIntersect()`
* :func:`PositionReference()`
* :func:`RemoveOverlap()`
* :func:`Rotate()`
* :func:`RoundToCluster()`
* :func:`RoundToInt()`
* :func:`Scale()`
* :func:`ScaleToEm()`
* :func:`Shadow()`
* :func:`Simplify()`
* :func:`Skew()`
* :func:`SmallCaps()`
* :func:`Transform()`
* :func:`VFlip()`
* :func:`Wireframe()`


.. _scripting.font-info:

Font Info
^^^^^^^^^

* :func:`AddSizeFeature()`
* :func:`ChangePrivateEntry()`
* :func:`ClearPrivateEntry()`
* :func:`GetFontBoundingBox()`
* :func:`GetMaxpValue()`
* :func:`GetOS2Value()`
* :func:`GetPrivateEntry()`
* :func:`GetTeXParam()`
* :func:`GetTTFName()`
* :func:`HasPrivateEntry()`
* :func:`ScaleToEm()`
* :func:`SetFondName()`
* :func:`SetFontHasVerticalMetrics()`
* :func:`SetFontNames()`
* :func:`SetFontOrder()`
* :func:`SetGasp()`
* :func:`SetItalicAngle()`
* :func:`SetMacStyle()`
* :func:`SetMaxpValue()`
* :func:`SetOS2Value()`
* :func:`SetPanose()`
* :func:`SetTeXParams()`
* :func:`SetTTFName()`
* :func:`SetUniqueID()`
* Some items (such as the font name) may be retrieved via built-in variables.


.. _scripting.glyph-info:

Glyph Info
^^^^^^^^^^

* :func:`DrawsSomething()`
* :func:`GetPosSub()`
* :func:`GlyphInfo()`
* :func:`SetGlyphColor()`
* :func:`SetGlyphComment()`
* :func:`SetGlyphChanged()`
* :func:`SetGlyphClass()`
* :func:`SetGlyphName()`
* :func:`SetUnicodeValue()`
* :func:`SetGlyphTeX()`
* :func:`WorthOutputting()`


.. _scripting.ATT:

Built-in procedures that handle Advanced Typography
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* :func:`AddAnchorClass()`
* :func:`AddAnchorPoint()`
* :func:`AddLookup()`
* :func:`AddLookupSubtable()`
* :func:`AddPosSub()`
* :func:`AddSizeFeature()`
* :func:`ApplySubstitution()`
* :func:`CheckForAnchorClass()`
* :func:`GetAnchorPoints()`
* :func:`GetLookupInfo()`
* :func:`GetLookups()`
* :func:`GetLookupSubtables()`
* :func:`GetLookupOfSubtable()`
* :func:`GetPosSub()`
* :func:`GetSubtableOfAnchor()`
* :func:`GenerateFeatureFile()`
* :func:`HasPreservedTable()`
* :func:`LoadTableFromFile()`
* :func:`LookupStoreLigatureInAfm()`
* :func:`LookupSetFeatureList()`
* :func:`MergeLookups()`
* :func:`MergeLookupSubtables()`
* :func:`RemoveAnchorClass()`
* :func:`RemoveLookup()`
* :func:`RemoveLookupSubtable()`
* :func:`RemovePosSub()`
* :func:`RemovePreservedTable()`
* :func:`SaveTableToFile()`


.. _scripting.encoding-menu:

Built-in procedures that act like the Encoding Menu
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* :func:`CharCnt()`
* :func:`DetachGlyphs()`
* :func:`DetachAndRemoveGlyphs()`
* :func:`LoadEncodingFile()`
* :func:`MultipleEncodingsToReferences()`
* :func:`Reencode()`
* :func:`RemoveDetachedGlyphs()`
* :func:`RenameGlyphs()`
* :func:`SameGlyphAs()`
* :func:`SetCharCnt()`


.. _scripting.hint-menu:

Built-in procedures that act like the Hint Menu
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* :func:`AddDHint()`
* :func:`AddHHint()`
* :func:`AddInstrs()`
* :func:`AddVHint()`
* :func:`AutoCounter()`
* :func:`AutoHint()`
* :func:`AutoInstr()`
* :func:`ChangePrivateEntry()`
* :func:`ClearGlyphCounterMasks()`
* :func:`ClearHints()`
* :func:`ClearInstrs()`
* :func:`ClearPrivateEntry()`
* :func:`ClearTable()`
* :func:`DontAutoHint()`
* :func:`FindOrAddCvtIndex()`
* :func:`GetCvtAt()`
* :func:`GetPrivateEntry()`
* :func:`HasPrivateEntry()`
* :func:`ReplaceGlyphCounterMasks()`
* :func:`ReplaceCvtAt()`
* :func:`SetGlyphCounterMask()`
* :func:`SubstitutionPoints()`


.. _scripting.metrics-menu:

Built-in procedures that act like the Metrics Menu
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

* :func:`AutoKern()`
* :func:`AutoWidth()`
* :func:`CenterInWidth()`
* :func:`SetKern()`
* :func:`RemoveAllKerns()`
* :func:`RemoveAllVKerns()`
* :func:`SetLBearing()`
* :func:`SetRBearing()`
* :func:`SetVKern()`
* :func:`SetVWidth()`
* :func:`SetWidth()`
* :func:`VKernFromHKern()`


.. _scripting.MM:

Multiple master routines
^^^^^^^^^^^^^^^^^^^^^^^^

* :func:`MMAxisBounds()`
* :func:`MMAxisNames()`
* :func:`MMBlendToNewFont()`
* :func:`MMChangeInstance()`
* :func:`MMChangeWeight()`
* :func:`MMInstanceNames()`
* :func:`MMWeightedName()`


.. _scripting.CID:

CID routines
^^^^^^^^^^^^

* :func:`CIDChangeSubFont()`
* :func:`CIDFlatten()`
* :func:`CIDFlattenByCMap()`
* :func:`CIDSetFontNames()`
* :func:`ConvertToCID()`
* :func:`ConvertByCMap()`
* :func:`PreloadCidmap()`


.. _scripting.user:

User Interaction
^^^^^^^^^^^^^^^^

* :func:`AskUser()`
* :func:`Error()`
* :func:`PostNotice()`
* :func:`Print()`


.. _scripting.prefs:

Preferences
^^^^^^^^^^^

* :func:`DefaultOtherSubrs()`
* :func:`GetPref()`
* :func:`LoadEncodingFile()`
* :func:`LoadNamelist()`
* :func:`LoadNamelistDir()`
* :func:`LoadPrefs()`
* :func:`ReadOtherSubrsFile()`
* :func:`SavePrefs()`
* :func:`SetPref()`
* It it also possible to get the value of a preference item by preceding its name
  with a dollar sign.


.. _scripting.math:

Math
^^^^

* :func:`ATan2()`
* :func:`Ceil()`
* :func:`Chr()`
* :func:`Cos()`
* :func:`Exp()`
* :func:`Floor()`
* :func:`Int()`
* :func:`IsFinite()`
* :func:`IsNan()`
* :func:`Log()`
* :func:`Ord()`
* :func:`Pow()`
* :func:`Rand()`
* :func:`Real()`
* :func:`Round()`
* :func:`Sin()`
* :func:`Sqrt()`
* :func:`Strskipint()`
* :func:`Strtod()`
* :func:`Strtol()`
* :func:`Tan()`
* :func:`ToString()`
* :func:`UCodePoint()`


.. _scripting.unicode:

Unicode
^^^^^^^

* :func:`NameFromUnicode()`
* :func:`UCodePoint()`
* :func:`UnicodeFromName()`
* :func:`Ucs4()`
* :func:`Utf8()`


.. _scripting.strings:

String manipulation
^^^^^^^^^^^^^^^^^^^

* :func:`Chr()`
* :func:`GetEnv()`
* :func:`NameFromUnicode()`
* :func:`Ord()`
* :func:`Strcasecmp()`
* :func:`Strcasestr()`
* :func:`Strftime()`
* :func:`StrJoin()`
* :func:`Strlen()`
* :func:`Strrstr()`
* :func:`Strskipint()`
* :func:`StrSplit()`
* :func:`Strstr()`
* :func:`Strsub()`
* :func:`Strtod()`
* :func:`Strtol()`
* :func:`ToString()`
* :func:`UnicodeFromName()`
* :func:`Ucs4()`
* :func:`Utf8()`


.. _scripting.Character:

Character Manipulation
^^^^^^^^^^^^^^^^^^^^^^

* :func:`IsAlNum()`
* :func:`IsAlpha()`
* :func:`IsDigit()`
* :func:`IsHexDigit()`
* :func:`IsLower()`
* :func:`IsSpace()`
* :func:`IsUpper()`
* :func:`ToLower()`
* :func:`ToMirror()`
* :func:`ToUpper()`


.. _scripting.arrays:

Arrays
^^^^^^

* :func:`Array()`
* :func:`SizeOf()`


.. _scripting.misc:

Miscellaneous
^^^^^^^^^^^^^

* :func:`InFont()`
* :func:`TypeOf()`


.. _scripting.Deprecated:

Deprecated Names
^^^^^^^^^^^^^^^^

* :func:`ClearGlyphCounterMasks()`
* :func:`GlyphInfo()`
* :func:`ReplaceGlyphCounterMasks()`
* :func:`SetGlyphColor()`
* :func:`SetGlyphComment()`
* :func:`SetGlyphCounterMask()`
* :func:`SetGlyphName()`

--------------------------------------------------------------------------------


.. _scripting.Example:

Examples
--------

* `FontForge's testsuite in the test subdirectory <https://github.com/fontforge/fontforge/tree/master/tests>`__
  (such as it is)
* `Directory of donated scripts <https://github.com/fontforge/fontforge/tree/master/pycontrib>`__
* Scripts used in other projects

  * `x-symbol <http://sourceforge.net/projects/x-symbol/>`__
* :doc:`the scripting tutorial </tutorial/scripting-tutorial>`


Example 1:
^^^^^^^^^^

::

   #Set the color of all selected glyphs to be yellow
   #designed to be run within an interactive fontforge session.
   foreach
     SetCharColor(0xffff00)
   endloop


Example 2:
^^^^^^^^^^

::

   #!/usr/local/bin/fontforge
   #This is the sfddiff script which compares two fonts
   
   if ( Strtol($version) < 20060330 )
     Error( "Please upgrade to a more recent version of fontforge" )
   endif
   
   flags=0x789
   outfile=""
   
   while ( $argc > 1 && Strsub($1,0,1)=="-" )
     temp = $1
     if ( Strsub(temp,1,2)=='-' )
       temp = Strsub(temp,1)
     endif
   
     if ( temp=="-ignorehints" )
       flags = flags & ~0x8
     elseif ( temp=="-ignorenames" )
       flags = flags & ~0x100
     elseif ( temp=="-ignoregpos" )
       flags = flags & ~0x200
     elseif ( temp=="-ignoregsub" )
       flags = flags & ~0x400
     elseif ( temp=="-ignorebitmaps" )
       flags = flags & ~0x80
     elseif ( temp=="-exact" )
       flags = flags | 0x2
     elseif ( temp=="-warn" )
       flags = flags | 0x44
     elseif ( temp=="-merge" )
       flags = flags | 0x1800
       shift
       outfile = $1
     elseif ( temp=="-help" )
       Print( "sfddiff: [--version] [--help] [--usage] [--ignorehints] [--ignorenames] [--ignoregpos] [--ignoregsup] [--ignorebitmaps] [--warn] [--exact] fontfile1 fontfile2" )
       Print( " Compares two fontfiles" )
       Print( " --ignorehints: Do not compare postscript hints or truetype instructions" )
       Print( " --ignorenames: Do not compare font names" )
       Print( " --ignoregpos:  Do not compare kerning, etc." )
       Print( " --ignoregsub:  Do not compare ligatures, etc." )
       Print( " --ignorebitmaps: Do not compare bitmap strikes" )
       Print( " --exact:       Normally sfddiff will match contours which are not exact" )
       Print( "                but where the differences are slight (so you could compare" )
       Print( "                truetype and postscript and get reasonable answers). Also" )
       Print( "                normally sfddiff will unlink references before it compares" )
       Print( "                (so you can compare a postscript font (with no references)" )
       Print( "                to the original source (which does have references)). Setting")
       Print( "                this flag means glyphs must match exactly.")
       Print( " --warn:        Provides a warning when an exact match is not found" )
       Print( " --merge outfile: Put any outline differences in the backgrounds of" )
       Print( "                appropriate glyphs" )
   return(0)
     elseif ( temp=="-version" )
       Print( "Version 1.0" )
   return(0)
     else
   break
     endif
     shift
   endloop
   
   if ( $argc!=3 || $1=="--usage" || $1=="-usage" )
     Print( "sfddiff: [--version] [--help] [--usage] [--ignorehints] [--ignorenames] [--ignoregpos] [--ignoregsup] [--ignorebitmaps] [--warn] [--exact] [--merge outfile] fontfile1 fontfile2" )
   return(0)
   endif
   
   Open($2)
   Open($1)
   CompareFonts($2,"-",flags)
   if ( outfile!="" )
     Save(outfile)
   endif


Example 3:
^^^^^^^^^^

::

   #!/usr/local/bin/fontforge
   #Take a Latin font and apply some simple transformations to it
   #prior to adding cyrillic letters.
   #can be run in a non-interactive fontforge session.
   Open($1);
   Reencode("KOI8-R");
   Select(0xa0,0xff);
   //Copy those things which look just like latin
   BuildComposit();
   BuildAccented();
   
   //Handle Ya which looks like a backwards "R"
   Select("R");
   Copy();
   Select("afii10049");
   Paste();
   HFlip();
   CorrectDirection();
   Copy();
   Select(0u044f);
   Paste();
   CopyFgToBg();
   Clear();
   
   //Gamma looks like an upside-down L
   Select("L");
   Copy();
   Select(0u0413);
   Paste();
   VFlip();
   CorrectDirection();
   Copy();
   Select(0u0433);
   Paste();
   CopyFgToBg();
   Clear();
   
   //Prepare for editing small caps K, etc.
   Select("K");
   Copy();
   Select(0u043a);
   Paste();
   CopyFgToBg();
   Clear();
   
   Select("H");
   Copy();
   Select(0u043d);
   Paste();
   CopyFgToBg();
   Clear();
   
   Select("T");
   Copy();
   Select(0u0442);
   Paste();
   CopyFgToBg();
   Clear();
   
   Select("B");
   Copy();
   Select(0u0432);
   Paste();
   CopyFgToBg();
   Clear();
   
   Select("M");
   Copy();
   Select(0u043C);
   Paste();
   CopyFgToBg();
   Clear();
   
   Save($1:r+"-koi8-r.sfd");
   Quit(0);


.. _scripting.Execute:

The Execute Script dialog
-------------------------

This dialog allows you to type a script directly in to FontForge and then run
it. Of course the most common case is that you'll have a script file somewhere
that you want to execute, so there's a button [Call] down at the bottom of the
dlg. Pressing [Call] will bring up a file picker dlg looking for files with the
extension \*.pe (you can change that by typing a wildcard sequence and pressing
the [Filter] button). After you have selected your script the appropriate text
to text to invoke it will be placed in the text area.

The current font of the script will be set to whatever font you invoked it from
(in python this is :py:func:`fontforge.activeFont()`).

Note that if you try to print from a script the output will go to stdout. If you
have invoked fontforge from a window manager's menu stdout will often be bound
to /dev/null and not useful. Try starting fontforge from the command line
instead.


.. _scripting.menu:

The Scripts Menu
----------------

You can use the preference dialog to create a list of frequently used scripts.
Invoke :ref:`File->Preferences <prefs.scripts>` and select the Scripts tag. In
this dialog are ten possible entries, each one should have a name (to be
displayed in the menu) and an associated script file to be run.

After you have set up your preferences you can invoke scripts from the font
view, either directly from the menu
(:menuselection:`File --> Scripts --> <your name>`) or by a hot key. The first
script you added will be invoked by Cnt-Alt-1, then second by Cnt-Alt-2, and the
tenth by Cnt-Alt-0.

The current font of the script will be set to whatever font you invoked it from.