Debian Codegraph @

doc/sphinx/scripting/scripting.rst

raw
1 Scripting FontForge
2 ===================
3
4 FontForge includes two interpreters so you can write scripts to modify fonts.
5 One of these interpreters is :doc:`python </scripting/python>`, one is a legacy language I
6 came up with. FontForge may be configured with either or both of these. If
7 configured with both then fontforge will make a guess at which to use based on
8 the script file's extension ("py" means use the python interpreter, "ff" or "pe"
9 means use the old interpreter)
10
11 .. toctree::
12
13 scripting-alpha
14 python
15 python-extension
16
17 .. contents::
18 :backlinks: none
19
20 .. default-domain:: ff
21
22 .. _scripting.Starting:
23
24 Invoking scripts
25 ----------------
26
27 If you start fontforge with a script on the command line it will not put up any
28 windows and it will exit when the script is done. The script can be in a file,
29 or just a string presented as an argument. You may need to specify which
30 interpreter to use with the -lang argument.
31
32 ::
33
34 $ fontforge -script scriptfile.pe {arguments}
35 $ fontforge -c "script-string" {arguments}
36 $ fontforge -lang={ff|py} -c "script-string"
37
38 FontForge can also be used as an interpreter to which the shell will
39 automatically pass scripts. If you a mark your script files as executable ::
40
41 $ chmod +x scriptfile.pe
42
43 and begin each one with the line ::
44
45 #!/usr/local/bin/fontforge
46
47 (or wherever fontforge happens to reside on your system) then you can invoke the
48 script just by typing ::
49
50 $ scriptfile.pe {fontnames}
51
52 If you wish FontForge to read a script from stdin then you can use "-" as a
53 "filename" for stdin. (If you build FontForge without X11 then fontforge will
54 attempt to read a script file from ``stdin`` if none is given on the command
55 line.)
56
57 You can also start a script from within FontForge with
58 :menuselection:`File --> Execute Script`, and you can use the Preference Dlg to
59 define a set of frequently used scripts which can be invoked directly by menu.
60
61 The scripting language provides access to much of the functionality found in the
62 font view's menus. It does not currently (and probably never will) provide
63 access to everything. (If you find a lack let me know, I may put it in for you).
64 It does not provide commands for building up a glyph out of splines, instead it
65 allows you to do high level modifications to glyphs.
66
67 If you set the environment variable ``FONTFORGE_VERBOSE`` (it doesn't need a
68 value, just needs to be set) then FontForge will print scripts to stdout as it
69 executes them.
70
71 You may set the environment variable ``FONTFORGE_LANGUAGE`` to either "py" (for
72 python) or "ff" or "pe" (for native scripting) as another way to determne what
73 interpreter to use.
74
75
76 .. _scripting.Language:
77
78 Scripting Language
79 ------------------
80
81 .. note::
82
83 This section covers FontForge's native scripting functionality.
84
85 For the Python scripting functionality, see :doc:`here </scripting/python>`.
86
87 The syntax is rather like a mixture of C and shell commands. Every file
88 corresponds to a procedure. As in a shell script arguments passed to the file
89 are identified as $1, $2, ... $n. $0 is the file name itself. $argc gives the
90 number of arguments. $argv[<expr>] provides array access to the arguments.
91
92 Terms can be
93
94 * A variable name (like "$1" or "i" or "@fontvar" or "_global")
95
96 The scope of the variable depends on the initial character of its name.
97
98 * A '$' signifies that it is a built-in variable. The user cannot create any new
99 variables beginning with '$'. Some, but not all, of these may be assigned to.
100 * A '_' signifies that the variable is global, it is always available. You can use
101 these to store context across different script files (or to access data within
102 nested script files).
103 * A '@' signifies that the variable is associated with the font. Any two scripts
104 looking at the same font will have access to the same variables.
105 * A variable which begins with a letter is a local variable. It is only meaningful
106 within the current script file. Nested script files may have different variables
107 with the same names.
108 * an integer expressed in decimal, hex or octal
109 * a unicode code point (which has a prefix of "0u" or "0U" and is followed by a
110 string of hex digits. This is only used by the select command.
111 * a real number (in "C" locale format -- "." as the decimal point character)
112 * A string which may be enclosed in either double or single quotes. String tokens
113 are limited to 256 bytes. "\n" can be used to represent a newline character. (If
114 you need longer strings use the concatenation operator).
115 * a procedure to call or file to invoke.
116 * an expression within parentheses
117 * a series of expressions (separated by commas) within brackets, as
118 ``[1, 2, 3, 5, 8]``, used to create an array.
119
120 There are three different comments supported:
121
122 * Starting with a "#" character and proceeding to end of line
123 * Starting with "//" and proceeding to end of line
124 * Starting with "/*" and proceeding to "*/"
125
126 .. _scripting.Expressions:
127
128 Expressions
129 ^^^^^^^^^^^
130
131 Expressions are similar to those in C, a few operators have been omitted, a few
132 added from shell scripts. Operator precedence has been simplified slightly. So
133 operators (and their precedences) are:
134
135 * unary operators (+, -, !, ~, ++ (prefix and postfix), --(prefix and postfix), ()
136 (procedure call), [] (array index), :h, :t, :r, :e
137
138 Most of these are as expected in C, the last four are borrowed from shell
139 scripts and are applied to strings
140
141 * :h gives the head (directory) of a pathspec
142 * :t gives the tail (filename) of a pathspec
143 * :r gives the pathspec without the extension (if any)
144 * :e gives the extension
145 * \*, /, % (binary multiplicative operators)
146 * +, - (binary arithmetic operators)
147
148 If the first operand of + is a string then + will be treated as concatenation
149 rather than addition. If the second operand is a number it will be converted to
150 a string (decimal representation) and then concatenated. If the first operand of
151 + is an array then + will do array concatenation -- if the second argument is
152 also an array the two will be concatenated ([1,2] + [3,4] yields [1,2,3,4],
153 while [1,2] + 3 yields [1,2,3]). Otherwise these are the normal arithmetric
154 operations.
155 * ==, !=, >, <, >=, <= (comparison operators, may be applied to either two
156 integers or two strings)
157 * &&, & (logical and, bitwise and. (logical and will do short circuit evaluation))
158 * \|\|, \|, ^ (logical or, bitwise or, bitwise exclusive or (logical or will do short
159 circuit evaluation))
160 * =, +=, -=, \*=, /=, %= (assignment operators as in C. The += will act as
161 concatenation if the first operand is a string.)
162
163 Note there is no comma operator, and no "?:" operator. The precedence of "and"
164 and "or" has been simplified, as has that of the assignment operators.
165
166 Procedure calls may be applied either to a name token, or to a string. If the
167 name or string is recognized as one of FontForge's internal procedures it will
168 be executed, otherwise it will be assumed to be a filename containing another
169 fontforge script file, this file will be invoked (since filenames can contain
170 characters not legal in name tokens it is important to allow general strings to
171 specify filenames). If the procedure name does not contain a directory then it
172 is assumed to be in the same directory as the current script file. As a
173 convenience, it is not necessary to include the script's extension if it is
174 either ".ff" or ".pe". At most 25 arguments can be passed to a procedure.
175
176 Arrays are passed by reference, strings and integers are passed by value.
177
178 Variables may be created by assigning a value to them (only with the "="), so: ::
179
180 i=3
181
182 could be used to define "i" as a variable. Variables are limited in scope to the
183 current file, they will not be inherited by called procedures.
184
185 A statement may be
186
187 * an expression
188 * ::
189
190 if ( expression )
191 statements
192 {elseif ( expression )
193 statements}
194 [else
195 statements]
196 endif
197 * ::
198
199 while ( expression )
200 statements
201 endloop
202 * ::
203
204 foreach
205 statements
206 endloop
207
208 * ``break``
209 * ``return [ expression ]``
210 * ``shift``
211
212 As with C, non-zero expressions are defined to be true.
213
214 A return statement may be followed by a return value (the expression) or a
215 procedure may return nothing (void).
216
217 The shift statement is stolen from shell scripts and shifts all arguments down
218 by one. (argument 0, the name of the script file, remains unchanged.
219
220 The foreach statement requires that there be a current font. It executes the
221 statements once for each glyph in the selection. Within the statements only one
222 glyph at a time will be selected. After execution the selection will be restored
223 to what it was initially. (Caveat: Don't reencode the font within a foreach
224 statement).
225
226 Statements are terminated either by a new line (you can break up long lines with
227 backslash newline) or a semicolon.
228
229 Trivial example:
230
231 ::
232
233 i=0; #semicolon is not needed here, but it's ok
234 while ( i<3 )
235 if ( i==1 /* pointless comment */ )
236 Print( "Got to one" ) // Another comment
237 endif
238 ++i
239 endloop
240
241 FontForge maintains the concept of a "current font"-- almost all commands refer
242 only to the current font (and require that there be a font). If you start a
243 script with :menuselection:`File --> Execute Script`, the font you were editing
244 will be current, otherwise there will be no initial current font. The Open(),
245 New() and Close() commands all change the current font. FontForge also maintains
246 a list of all fonts that are currently open. This list is in no particular
247 order. The list starts with $firstfont.
248
249 Similarly when working with cid keyed fonts, FontForge works in the "current sub
250 font", and most commands refer to this font. The CIDChangeSubFont() command can
251 alter that.
252
253 .. _scripting.variables:
254
255 Variables
256 ^^^^^^^^^
257
258 All builtin variables begin with "$", you may not create any variables that
259 start with "$" yourself (though you may assign to (some) already existing ones)
260
261 .. data:: $0
262
263 the current script filename
264
265 .. data:: $1
266 $2
267 $n
268
269 the first, second (...) argument to the script file
270
271 .. data:: $argc
272
273 the number of arguments passed to the script file (this will always be
274 at least 1 as $0 is always present)
275
276 .. data:: $argv
277
278 allows you to access the array of all the arguments
279
280 .. data:: $curfont
281
282 the name of the filename in which the current font resides
283
284 .. data:: $firstfont
285
286 the name of the filename of the font which is first on the font
287 list (Can be used by :func:`Open()`), if there are no fonts loaded this
288 returns an empty string. This can be used to determine if any font at all
289 is loaded into fontforge.
290
291 .. data:: $nextfont
292
293 the name of the filename of the font which follows the current
294 font on the list (or the empty string if the current font is the last one on the
295 list)
296
297 .. data:: $fontchanged
298
299 returns 1 if the current font has changed, 0 if it has not
300 changed since it was read in (or saved).
301
302 .. data:: $fontname
303
304 the name contained in the postscript FontName field
305
306 .. data:: $familyname
307
308 the name contained in the postscript FamilyName field
309
310 .. data:: $fullname
311
312 the name contained in the postscript FullName field
313
314 .. data:: $fondname
315
316 if set this name indicates what FOND the current font should be
317 put in under Generate Mac Family.
318
319 .. data:: $weight
320
321 the name contained in the postscript Weight field
322
323 .. data:: $copyright
324
325 the name contained in the postscript Notice field
326
327 .. data:: $filename
328
329 the name of the file containing the font.
330
331 .. data:: $fontversion
332
333 the string containing the font's version
334
335 .. data:: $iscid
336
337 1 if the current font is a cid keyed font, 0 if not
338
339 .. data:: $cidfontname
340
341 returns the fontname of the top-level cid-keyed font (or the
342 empty string if there is none)
343
344 Can be used to detect if this is a cid keyed font.
345
346 .. data:: $cidfamilyname, $cidfullname, $cidweight, $cidcopyright
347
348 similar to :data:`cidfontname`
349
350 .. data:: $mmcount
351
352 returns 0 for non multiple master fonts, returns the number of
353 instances in a multiple master font.
354
355 .. data:: $italicangle
356
357 the value of the postscript italic angle field
358
359 .. data:: $loadState
360
361 a bitmask of non-fatal errors encountered when loading the font.
362
363 .. data:: $privateState
364
365 a bitmask of some errors in the PostScript Private dictionary
366 (see :py:attr:`fontforge.font.privateState` for more info).
367
368 .. data:: $curcid
369
370 returns the fontname of the current font
371
372 .. data:: $firstcid
373
374 returns the fontname of the first font within this cid font
375
376 .. data:: $nextcid
377
378 returns the fontname of the next font within this cid font (or the
379 empty string if the current sub-font is the last)
380
381 .. data:: $macstyle
382
383 returns the value of the macstyle field (a set of bits indicating
384 whether the font is bold, italic, condensed, etc.)
385
386 .. data:: $bitmaps
387
388 returns an array containing all bitmap pixelsizes generated for
389 this font. (If the font database contains greymaps then they will be indicated
390 in the array as ``(<BitmapDepth><<16)|<PixelSize>``)
391
392 .. data:: $order
393
394 returns an integer containing either 2 (for truetype fonts) or 3 (for
395 postscript fonts). This indicates whether the font uses quadratic or cubic
396 splines.
397
398 .. data:: $em
399
400 returns the number of em-units used by the font.
401
402 .. data:: $ascent
403
404 returns the ascent of the font
405
406 .. data:: $descent
407
408 returns the descent of the font.
409
410 .. data:: $selection
411
412 returns an array containing one entry for each glyph in the
413 current font indicating whether that glyph is selected or not (0=>not,
414 1=>selected)
415
416 .. data:: $panose
417
418 returns an array containing the 10 panose values for the font.
419
420 .. data:: $trace
421
422 if this is set to one then FontForge will trace each procedure call.
423
424 .. data:: $version
425
426 returns a string containing the current version of fontforge. This
427 should look something like "20050817".
428
429 .. data:: $haspython
430
431 returns 1 if python scripting is available, 0 if it is not.
432
433
434 .. _scripting.Preference:
435
436 Preferences
437 ^^^^^^^^^^^
438
439 * ``$<Preference Item>`` (for example ``$AutoHint``) allows you to examine the
440 value of that preference item (to set it use
441 :func:`SetPref()`)
442
443 The following example will perform an action on all loaded fonts:
444
445 ::
446
447 file = $firstfont
448 while ( file != "" )
449 Open(file)
450 /* Do Stuff */
451 file = $nextfont
452 endloop
453
454 .. _scripting.procedures:
455
456 The built in procedures are very similar to the menu items with the same names.
457 Often the description here is sketchy, look at the menu item for more
458 information.
459
460 * :doc:`Built-in procedures in alphabetic order <scripting-alpha>`
461 * :ref:`Built-in procedures that do not require a font <scripting.no-font>`
462 * :ref:`File menu <scripting.file-menu>`
463 * :ref:`File manipulation <scripting.files>`
464 * :ref:`Edit menu <scripting.edit-menu>`
465 * :ref:`Select menu <scripting.select-menu>`
466 * :ref:`Element menu <scripting.element-menu>`
467 * :ref:`Font information <scripting.font-info>`
468 * :ref:`Glyph information <scripting.glyph-info>`
469 * :ref:`Advanced Typography <scripting.ATT>`
470 * :ref:`Encoding menu <scripting.encoding-menu>`
471 * :ref:`Hint menu <scripting.hint-menu>`
472 * :ref:`Metrics menu <scripting.metrics-menu>`
473 * :ref:`Multiple master <scripting.MM>`
474 * :ref:`CID keyed fonts <scripting.CID>`
475 * :ref:`User interaction <scripting.user>`
476 * :ref:`Preferences <scripting.prefs>`
477 * :ref:`Math <scripting.math>`
478 * :ref:`Unicode <scripting.unicode>`
479 * :ref:`String manipulation <scripting.strings>`
480 * :ref:`Character manipulation <scripting.Character>`
481 * :ref:`Arrays <scripting.arrays>`
482 * :ref:`Miscellaneous <scripting.misc>`
483 * :ref:`Deprecated Names <scripting.Deprecated>`
484
485
486 .. _scripting.alpha:
487
488 Built-in procedures in alphabetic order
489 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
490
491 .. _scripting.no-font:
492
493 Built-in procedures that do not require a loaded font
494 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
495
496 * :func:`Array()`
497 * :func:`AskUser()`
498 * :func:`ATan2()`
499 * :func:`Ceil()`
500 * :func:`Chr()`
501 * :func:`Cos()`
502 * :func:`Floor()`
503 * :func:`Error()`
504 * :func:`Exp()`
505 * :func:`DefaultOtherSubrs()`
506 * :func:`FileAccess()`
507 * :func:`FontsInFile()`
508 * :func:`GetEnv()`
509 * :func:`GetPref()`
510 * :func:`Int()`
511 * :func:`IsAlNum()`
512 * :func:`IsAlpha()`
513 * :func:`IsDigit()`
514 * :func:`IsFinite()`
515 * :func:`IsHexDigit()`
516 * :func:`IsLower()`
517 * :func:`IsNan()`
518 * :func:`IsSpace()`
519 * :func:`IsUpper()`
520 * :func:`LoadEncodingFile()`
521 * :func:`LoadNamelist()`
522 * :func:`LoadNamelistDir()`
523 * :func:`LoadPrefs()`
524 * :func:`LoadStringFromFile()`
525 * :func:`Log()`
526 * :func:`NameFromUnicode()`
527 * :func:`New()`
528 * :func:`Open()`
529 * :func:`Ord()`
530 * :func:`PostNotice()`
531 * :func:`Pow()`
532 * :func:`PreloadCidmap()`
533 * :func:`Print()`
534 * :func:`Rand()`
535 * :func:`ReadOtherSubrsFile()`
536 * :func:`Real()`
537 * :func:`Round()`
538 * :func:`SavePrefs()`
539 * :func:`SetPref()`
540 * :func:`SizeOf()`
541 * :func:`Sin()`
542 * :func:`Sqrt()`
543 * :func:`Strcasestr()`
544 * :func:`Strcasecmp()`
545 * :func:`Strftime()`
546 * :func:`StrJoin()`
547 * :func:`Strlen()`
548 * :func:`Strrstr()`
549 * :func:`Strskipint()`
550 * :func:`StrSplit()`
551 * :func:`Strstr()`
552 * :func:`Strsub()`
553 * :func:`Strtod()`
554 * :func:`Strtol()`
555 * :func:`Tan()`
556 * :func:`ToLower()`
557 * :func:`ToMirror()`
558 * :func:`ToString()`
559 * :func:`ToUpper()`
560 * :func:`TypeOf()`
561 * :func:`UCodePoint()`
562 * :func:`UnicodeFromName()`
563 * :func:`Ucs4()`
564 * :func:`Utf8()`
565 * :func:`WriteStringToFile()`
566
567
568 .. _scripting.file-menu:
569
570 Built-in procedures that act like the File Menu
571 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
572
573 * :func:`Close()`
574 * :func:`Export()`
575 * :func:`FontsInFile()`
576 * :func:`Generate()`
577 * :func:`GenerateFamily()`
578 * :func:`Import()`
579 * :func:`MergeKern()` deprecated
580 * :func:`MergeFeature()`
581 * :func:`New()`
582 * :func:`Open()`
583 * :func:`PrintFont()`
584 * :func:`PrintSetup()`
585 * :func:`Quit()`
586 * :func:`Revert()`
587 * :func:`RevertToBackup()`
588 * :func:`Save()`
589
590
591 .. _scripting.files:
592
593 File Manipulation
594 ^^^^^^^^^^^^^^^^^
595
596 * :func:`FileAccess()`
597 * :func:`FontImage()`
598 * :func:`LoadStringFromFile()`
599 * :func:`WriteStringToFile()`
600
601
602 .. _scripting.edit-menu:
603
604 Built-in procedures that act like the Edit Menu
605 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
606
607 * :func:`Clear()`
608 * :func:`ClearBackground()`
609 * :func:`Copy()`
610 * :func:`CopyAnchors()`
611 * :func:`CopyFgToBg()`
612 * :func:`CopyLBearing()`
613 * :func:`CopyRBearing()`
614 * :func:`CopyReference()`
615 * :func:`CopyUnlinked()`
616 * :func:`CopyVWidth()`
617 * :func:`CopyWidth()`
618 * :func:`Cut()`
619 * :func:`Join()`
620 * :func:`Paste()`
621 * :func:`Paste()`
622 * :func:`PasteWithOffset()`
623 * :func:`ReplaceWithReference()`
624 * :func:`SameGlyphAs()`
625 * :func:`UnlinkReference()`
626
627
628 .. _scripting.select-menu:
629
630 Built-in procedures that act like the Select Menu
631 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
632
633 * :func:`Select()`
634 * :func:`SelectAll()`
635 * :func:`SelectAllInstancesOf()`
636 * :func:`SelectBitmap()`
637 * :func:`SelectByPosSub()`
638 * :func:`SelectChanged()`
639 * :func:`SelectFewer()`
640 * :func:`SelectFewerSingletons()`
641 * :func:`SelectGlyphsBoth()`
642 * :func:`SelectGlyphsReferences()`
643 * :func:`SelectGlyphsSplines()`
644 * :func:`SelectHintingNeeded()`
645 * :func:`SelectIf()`
646 * :func:`SelectInvert()`
647 * :func:`SelectMore()`
648 * :func:`SelectMoreIf()`
649 * :func:`SelectMoreSingletons()`
650 * :func:`SelectMoreSingletonsIf()`
651 * :func:`SelectNone()`
652 * :func:`SelectSingletons()`
653 * :func:`SelectSingletonsIf()`
654 * :func:`SelectWorthOutputting()`
655
656
657 .. _scripting.element-menu:
658
659 Built-in procedures that act like the Element Menu
660 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
661
662 * :func:`AddAccent()`
663 * :func:`AddExtrema()`
664 * :func:`AddInflections()`
665 * :func:`ApplySubstitution()`
666 * :func:`AutoTrace()`
667 * :func:`Balance()`
668 * :func:`BitmapsAvail()`
669 * :func:`BitmapsRegen()`
670 * :func:`BuildAccented()`
671 * :func:`BuildComposite()`
672 * :func:`BuildDuplicate()`
673 * :func:`CanonicalContours()`
674 * :func:`CanonicalStart()`
675 * :func:`ChangeWeight()`
676 * :func:`CompareFonts()`
677 * :func:`CompareGlyphs()`
678 * :func:`CorrectDirection()`
679 * :func:`DefaultRoundToGrid()`
680 * :func:`DefaultUseMyMetrics()`
681 * :func:`ExpandStroke()`
682 * :func:`FindIntersections()`
683 * :func:`Harmonize()`
684 * :func:`HFlip()`
685 * :func:`Inline()`
686 * :func:`InterpolateFonts()`
687 * :func:`Italic()`
688 * :func:`MergeFonts()`
689 * :func:`Move()`
690 * :func:`MoveReference()`
691 * :func:`NearlyHvCps()`
692 * :func:`NearlyHvLines()`
693 * :func:`NearlyLines()`
694 * :func:`NonLinearTransform()`
695 * :func:`Outline()`
696 * :func:`OverlapIntersect()`
697 * :func:`PositionReference()`
698 * :func:`RemoveOverlap()`
699 * :func:`Rotate()`
700 * :func:`RoundToCluster()`
701 * :func:`RoundToInt()`
702 * :func:`Scale()`
703 * :func:`ScaleToEm()`
704 * :func:`Shadow()`
705 * :func:`Simplify()`
706 * :func:`Skew()`
707 * :func:`SmallCaps()`
708 * :func:`Transform()`
709 * :func:`VFlip()`
710 * :func:`Wireframe()`
711
712
713 .. _scripting.font-info:
714
715 Font Info
716 ^^^^^^^^^
717
718 * :func:`AddSizeFeature()`
719 * :func:`ChangePrivateEntry()`
720 * :func:`ClearPrivateEntry()`
721 * :func:`GetFontBoundingBox()`
722 * :func:`GetMaxpValue()`
723 * :func:`GetOS2Value()`
724 * :func:`GetPrivateEntry()`
725 * :func:`GetTeXParam()`
726 * :func:`GetTTFName()`
727 * :func:`HasPrivateEntry()`
728 * :func:`ScaleToEm()`
729 * :func:`SetFondName()`
730 * :func:`SetFontHasVerticalMetrics()`
731 * :func:`SetFontNames()`
732 * :func:`SetFontOrder()`
733 * :func:`SetGasp()`
734 * :func:`SetItalicAngle()`
735 * :func:`SetMacStyle()`
736 * :func:`SetMaxpValue()`
737 * :func:`SetOS2Value()`
738 * :func:`SetPanose()`
739 * :func:`SetTeXParams()`
740 * :func:`SetTTFName()`
741 * :func:`SetUniqueID()`
742 * Some items (such as the font name) may be retrieved via built-in variables.
743
744
745 .. _scripting.glyph-info:
746
747 Glyph Info
748 ^^^^^^^^^^
749
750 * :func:`DrawsSomething()`
751 * :func:`GetPosSub()`
752 * :func:`GlyphInfo()`
753 * :func:`SetGlyphColor()`
754 * :func:`SetGlyphComment()`
755 * :func:`SetGlyphChanged()`
756 * :func:`SetGlyphClass()`
757 * :func:`SetGlyphName()`
758 * :func:`SetUnicodeValue()`
759 * :func:`SetGlyphTeX()`
760 * :func:`WorthOutputting()`
761
762
763 .. _scripting.ATT:
764
765 Built-in procedures that handle Advanced Typography
766 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
767
768 * :func:`AddAnchorClass()`
769 * :func:`AddAnchorPoint()`
770 * :func:`AddLookup()`
771 * :func:`AddLookupSubtable()`
772 * :func:`AddPosSub()`
773 * :func:`AddSizeFeature()`
774 * :func:`ApplySubstitution()`
775 * :func:`CheckForAnchorClass()`
776 * :func:`GetAnchorPoints()`
777 * :func:`GetLookupInfo()`
778 * :func:`GetLookups()`
779 * :func:`GetLookupSubtables()`
780 * :func:`GetLookupOfSubtable()`
781 * :func:`GetPosSub()`
782 * :func:`GetSubtableOfAnchor()`
783 * :func:`GenerateFeatureFile()`
784 * :func:`HasPreservedTable()`
785 * :func:`LoadTableFromFile()`
786 * :func:`LookupStoreLigatureInAfm()`
787 * :func:`LookupSetFeatureList()`
788 * :func:`MergeLookups()`
789 * :func:`MergeLookupSubtables()`
790 * :func:`RemoveAnchorClass()`
791 * :func:`RemoveLookup()`
792 * :func:`RemoveLookupSubtable()`
793 * :func:`RemovePosSub()`
794 * :func:`RemovePreservedTable()`
795 * :func:`SaveTableToFile()`
796
797
798 .. _scripting.encoding-menu:
799
800 Built-in procedures that act like the Encoding Menu
801 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
802
803 * :func:`CharCnt()`
804 * :func:`DetachGlyphs()`
805 * :func:`DetachAndRemoveGlyphs()`
806 * :func:`LoadEncodingFile()`
807 * :func:`MultipleEncodingsToReferences()`
808 * :func:`Reencode()`
809 * :func:`RemoveDetachedGlyphs()`
810 * :func:`RenameGlyphs()`
811 * :func:`SameGlyphAs()`
812 * :func:`SetCharCnt()`
813
814
815 .. _scripting.hint-menu:
816
817 Built-in procedures that act like the Hint Menu
818 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
819
820 * :func:`AddDHint()`
821 * :func:`AddHHint()`
822 * :func:`AddInstrs()`
823 * :func:`AddVHint()`
824 * :func:`AutoCounter()`
825 * :func:`AutoHint()`
826 * :func:`AutoInstr()`
827 * :func:`ChangePrivateEntry()`
828 * :func:`ClearGlyphCounterMasks()`
829 * :func:`ClearHints()`
830 * :func:`ClearInstrs()`
831 * :func:`ClearPrivateEntry()`
832 * :func:`ClearTable()`
833 * :func:`DontAutoHint()`
834 * :func:`FindOrAddCvtIndex()`
835 * :func:`GetCvtAt()`
836 * :func:`GetPrivateEntry()`
837 * :func:`HasPrivateEntry()`
838 * :func:`ReplaceGlyphCounterMasks()`
839 * :func:`ReplaceCvtAt()`
840 * :func:`SetGlyphCounterMask()`
841 * :func:`SubstitutionPoints()`
842
843
844 .. _scripting.metrics-menu:
845
846 Built-in procedures that act like the Metrics Menu
847 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
848
849 * :func:`AutoKern()`
850 * :func:`AutoWidth()`
851 * :func:`CenterInWidth()`
852 * :func:`SetKern()`
853 * :func:`RemoveAllKerns()`
854 * :func:`RemoveAllVKerns()`
855 * :func:`SetLBearing()`
856 * :func:`SetRBearing()`
857 * :func:`SetVKern()`
858 * :func:`SetVWidth()`
859 * :func:`SetWidth()`
860 * :func:`VKernFromHKern()`
861
862
863 .. _scripting.MM:
864
865 Multiple master routines
866 ^^^^^^^^^^^^^^^^^^^^^^^^
867
868 * :func:`MMAxisBounds()`
869 * :func:`MMAxisNames()`
870 * :func:`MMBlendToNewFont()`
871 * :func:`MMChangeInstance()`
872 * :func:`MMChangeWeight()`
873 * :func:`MMInstanceNames()`
874 * :func:`MMWeightedName()`
875
876
877 .. _scripting.CID:
878
879 CID routines
880 ^^^^^^^^^^^^
881
882 * :func:`CIDChangeSubFont()`
883 * :func:`CIDFlatten()`
884 * :func:`CIDFlattenByCMap()`
885 * :func:`CIDSetFontNames()`
886 * :func:`ConvertToCID()`
887 * :func:`ConvertByCMap()`
888 * :func:`PreloadCidmap()`
889
890
891 .. _scripting.user:
892
893 User Interaction
894 ^^^^^^^^^^^^^^^^
895
896 * :func:`AskUser()`
897 * :func:`Error()`
898 * :func:`PostNotice()`
899 * :func:`Print()`
900
901
902 .. _scripting.prefs:
903
904 Preferences
905 ^^^^^^^^^^^
906
907 * :func:`DefaultOtherSubrs()`
908 * :func:`GetPref()`
909 * :func:`LoadEncodingFile()`
910 * :func:`LoadNamelist()`
911 * :func:`LoadNamelistDir()`
912 * :func:`LoadPrefs()`
913 * :func:`ReadOtherSubrsFile()`
914 * :func:`SavePrefs()`
915 * :func:`SetPref()`
916 * It it also possible to get the value of a preference item by preceding its name
917 with a dollar sign.
918
919
920 .. _scripting.math:
921
922 Math
923 ^^^^
924
925 * :func:`ATan2()`
926 * :func:`Ceil()`
927 * :func:`Chr()`
928 * :func:`Cos()`
929 * :func:`Exp()`
930 * :func:`Floor()`
931 * :func:`Int()`
932 * :func:`IsFinite()`
933 * :func:`IsNan()`
934 * :func:`Log()`
935 * :func:`Ord()`
936 * :func:`Pow()`
937 * :func:`Rand()`
938 * :func:`Real()`
939 * :func:`Round()`
940 * :func:`Sin()`
941 * :func:`Sqrt()`
942 * :func:`Strskipint()`
943 * :func:`Strtod()`
944 * :func:`Strtol()`
945 * :func:`Tan()`
946 * :func:`ToString()`
947 * :func:`UCodePoint()`
948
949
950 .. _scripting.unicode:
951
952 Unicode
953 ^^^^^^^
954
955 * :func:`NameFromUnicode()`
956 * :func:`UCodePoint()`
957 * :func:`UnicodeFromName()`
958 * :func:`Ucs4()`
959 * :func:`Utf8()`
960
961
962 .. _scripting.strings:
963
964 String manipulation
965 ^^^^^^^^^^^^^^^^^^^
966
967 * :func:`Chr()`
968 * :func:`GetEnv()`
969 * :func:`NameFromUnicode()`
970 * :func:`Ord()`
971 * :func:`Strcasecmp()`
972 * :func:`Strcasestr()`
973 * :func:`Strftime()`
974 * :func:`StrJoin()`
975 * :func:`Strlen()`
976 * :func:`Strrstr()`
977 * :func:`Strskipint()`
978 * :func:`StrSplit()`
979 * :func:`Strstr()`
980 * :func:`Strsub()`
981 * :func:`Strtod()`
982 * :func:`Strtol()`
983 * :func:`ToString()`
984 * :func:`UnicodeFromName()`
985 * :func:`Ucs4()`
986 * :func:`Utf8()`
987
988
989 .. _scripting.Character:
990
991 Character Manipulation
992 ^^^^^^^^^^^^^^^^^^^^^^
993
994 * :func:`IsAlNum()`
995 * :func:`IsAlpha()`
996 * :func:`IsDigit()`
997 * :func:`IsHexDigit()`
998 * :func:`IsLower()`
999 * :func:`IsSpace()`
1000 * :func:`IsUpper()`
1001 * :func:`ToLower()`
1002 * :func:`ToMirror()`
1003 * :func:`ToUpper()`
1004
1005
1006 .. _scripting.arrays:
1007
1008 Arrays
1009 ^^^^^^
1010
1011 * :func:`Array()`
1012 * :func:`SizeOf()`
1013
1014
1015 .. _scripting.misc:
1016
1017 Miscellaneous
1018 ^^^^^^^^^^^^^
1019
1020 * :func:`InFont()`
1021 * :func:`TypeOf()`
1022
1023
1024 .. _scripting.Deprecated:
1025
1026 Deprecated Names
1027 ^^^^^^^^^^^^^^^^
1028
1029 * :func:`ClearGlyphCounterMasks()`
1030 * :func:`GlyphInfo()`
1031 * :func:`ReplaceGlyphCounterMasks()`
1032 * :func:`SetGlyphColor()`
1033 * :func:`SetGlyphComment()`
1034 * :func:`SetGlyphCounterMask()`
1035 * :func:`SetGlyphName()`
1036
1037 --------------------------------------------------------------------------------
1038
1039
1040 .. _scripting.Example:
1041
1042 Examples
1043 --------
1044
1045 * `FontForge's testsuite in the test subdirectory <https://github.com/fontforge/fontforge/tree/master/tests>`__
1046 (such as it is)
1047 * `Directory of donated scripts <https://github.com/fontforge/fontforge/tree/master/pycontrib>`__
1048 * Scripts used in other projects
1049
1050 * `x-symbol <http://sourceforge.net/projects/x-symbol/>`__
1051 * :doc:`the scripting tutorial </tutorial/scripting-tutorial>`
1052
1053
1054 Example 1:
1055 ^^^^^^^^^^
1056
1057 ::
1058
1059 #Set the color of all selected glyphs to be yellow
1060 #designed to be run within an interactive fontforge session.
1061 foreach
1062 SetCharColor(0xffff00)
1063 endloop
1064
1065
1066 Example 2:
1067 ^^^^^^^^^^
1068
1069 ::
1070
1071 #!/usr/local/bin/fontforge
1072 #This is the sfddiff script which compares two fonts
1073
1074 if ( Strtol($version) < 20060330 )
1075 Error( "Please upgrade to a more recent version of fontforge" )
1076 endif
1077
1078 flags=0x789
1079 outfile=""
1080
1081 while ( $argc > 1 && Strsub($1,0,1)=="-" )
1082 temp = $1
1083 if ( Strsub(temp,1,2)=='-' )
1084 temp = Strsub(temp,1)
1085 endif
1086
1087 if ( temp=="-ignorehints" )
1088 flags = flags & ~0x8
1089 elseif ( temp=="-ignorenames" )
1090 flags = flags & ~0x100
1091 elseif ( temp=="-ignoregpos" )
1092 flags = flags & ~0x200
1093 elseif ( temp=="-ignoregsub" )
1094 flags = flags & ~0x400
1095 elseif ( temp=="-ignorebitmaps" )
1096 flags = flags & ~0x80
1097 elseif ( temp=="-exact" )
1098 flags = flags | 0x2
1099 elseif ( temp=="-warn" )
1100 flags = flags | 0x44
1101 elseif ( temp=="-merge" )
1102 flags = flags | 0x1800
1103 shift
1104 outfile = $1
1105 elseif ( temp=="-help" )
1106 Print( "sfddiff: [--version] [--help] [--usage] [--ignorehints] [--ignorenames] [--ignoregpos] [--ignoregsup] [--ignorebitmaps] [--warn] [--exact] fontfile1 fontfile2" )
1107 Print( " Compares two fontfiles" )
1108 Print( " --ignorehints: Do not compare postscript hints or truetype instructions" )
1109 Print( " --ignorenames: Do not compare font names" )
1110 Print( " --ignoregpos: Do not compare kerning, etc." )
1111 Print( " --ignoregsub: Do not compare ligatures, etc." )
1112 Print( " --ignorebitmaps: Do not compare bitmap strikes" )
1113 Print( " --exact: Normally sfddiff will match contours which are not exact" )
1114 Print( " but where the differences are slight (so you could compare" )
1115 Print( " truetype and postscript and get reasonable answers). Also" )
1116 Print( " normally sfddiff will unlink references before it compares" )
1117 Print( " (so you can compare a postscript font (with no references)" )
1118 Print( " to the original source (which does have references)). Setting")
1119 Print( " this flag means glyphs must match exactly.")
1120 Print( " --warn: Provides a warning when an exact match is not found" )
1121 Print( " --merge outfile: Put any outline differences in the backgrounds of" )
1122 Print( " appropriate glyphs" )
1123 return(0)
1124 elseif ( temp=="-version" )
1125 Print( "Version 1.0" )
1126 return(0)
1127 else
1128 break
1129 endif
1130 shift
1131 endloop
1132
1133 if ( $argc!=3 || $1=="--usage" || $1=="-usage" )
1134 Print( "sfddiff: [--version] [--help] [--usage] [--ignorehints] [--ignorenames] [--ignoregpos] [--ignoregsup] [--ignorebitmaps] [--warn] [--exact] [--merge outfile] fontfile1 fontfile2" )
1135 return(0)
1136 endif
1137
1138 Open($2)
1139 Open($1)
1140 CompareFonts($2,"-",flags)
1141 if ( outfile!="" )
1142 Save(outfile)
1143 endif
1144
1145
1146 Example 3:
1147 ^^^^^^^^^^
1148
1149 ::
1150
1151 #!/usr/local/bin/fontforge
1152 #Take a Latin font and apply some simple transformations to it
1153 #prior to adding cyrillic letters.
1154 #can be run in a non-interactive fontforge session.
1155 Open($1);
1156 Reencode("KOI8-R");
1157 Select(0xa0,0xff);
1158 //Copy those things which look just like latin
1159 BuildComposit();
1160 BuildAccented();
1161
1162 //Handle Ya which looks like a backwards "R"
1163 Select("R");
1164 Copy();
1165 Select("afii10049");
1166 Paste();
1167 HFlip();
1168 CorrectDirection();
1169 Copy();
1170 Select(0u044f);
1171 Paste();
1172 CopyFgToBg();
1173 Clear();
1174
1175 //Gamma looks like an upside-down L
1176 Select("L");
1177 Copy();
1178 Select(0u0413);
1179 Paste();
1180 VFlip();
1181 CorrectDirection();
1182 Copy();
1183 Select(0u0433);
1184 Paste();
1185 CopyFgToBg();
1186 Clear();
1187
1188 //Prepare for editing small caps K, etc.
1189 Select("K");
1190 Copy();
1191 Select(0u043a);
1192 Paste();
1193 CopyFgToBg();
1194 Clear();
1195
1196 Select("H");
1197 Copy();
1198 Select(0u043d);
1199 Paste();
1200 CopyFgToBg();
1201 Clear();
1202
1203 Select("T");
1204 Copy();
1205 Select(0u0442);
1206 Paste();
1207 CopyFgToBg();
1208 Clear();
1209
1210 Select("B");
1211 Copy();
1212 Select(0u0432);
1213 Paste();
1214 CopyFgToBg();
1215 Clear();
1216
1217 Select("M");
1218 Copy();
1219 Select(0u043C);
1220 Paste();
1221 CopyFgToBg();
1222 Clear();
1223
1224 Save($1:r+"-koi8-r.sfd");
1225 Quit(0);
1226
1227
1228 .. _scripting.Execute:
1229
1230 The Execute Script dialog
1231 -------------------------
1232
1233 This dialog allows you to type a script directly in to FontForge and then run
1234 it. Of course the most common case is that you'll have a script file somewhere
1235 that you want to execute, so there's a button [Call] down at the bottom of the
1236 dlg. Pressing [Call] will bring up a file picker dlg looking for files with the
1237 extension \*.pe (you can change that by typing a wildcard sequence and pressing
1238 the [Filter] button). After you have selected your script the appropriate text
1239 to text to invoke it will be placed in the text area.
1240
1241 The current font of the script will be set to whatever font you invoked it from
1242 (in python this is :py:func:`fontforge.activeFont()`).
1243
1244 Note that if you try to print from a script the output will go to stdout. If you
1245 have invoked fontforge from a window manager's menu stdout will often be bound
1246 to /dev/null and not useful. Try starting fontforge from the command line
1247 instead.
1248
1249
1250 .. _scripting.menu:
1251
1252 The Scripts Menu
1253 ----------------
1254
1255 You can use the preference dialog to create a list of frequently used scripts.
1256 Invoke :ref:`File->Preferences <prefs.scripts>` and select the Scripts tag. In
1257 this dialog are ten possible entries, each one should have a name (to be
1258 displayed in the menu) and an associated script file to be run.
1259
1260 After you have set up your preferences you can invoke scripts from the font
1261 view, either directly from the menu
1262 (:menuselection:`File --> Scripts --> <your name>`) or by a hot key. The first
1263 script you added will be invoked by Cnt-Alt-1, then second by Cnt-Alt-2, and the
1264 tenth by Cnt-Alt-0.
1265
1266 The current font of the script will be set to whatever font you invoked it from.