69328
|
1 |
/* Title: Pure/Tools/fontforge.scala
|
|
2 |
Author: Makarius
|
|
3 |
|
|
4 |
Support for fontforge and its scripting language:
|
|
5 |
https://github.com/fontforge/fontforge/blob/master/fontforge/scripting.c
|
|
6 |
*/
|
|
7 |
|
|
8 |
package isabelle
|
|
9 |
|
|
10 |
|
|
11 |
import java.io.{File => JFile}
|
|
12 |
import java.util.Locale
|
|
13 |
|
|
14 |
|
|
15 |
object Fontforge
|
|
16 |
{
|
|
17 |
/** scripting language **/
|
|
18 |
|
|
19 |
type Script = String
|
|
20 |
|
|
21 |
|
|
22 |
/* concrete syntax */
|
|
23 |
|
|
24 |
def string(s: String): Script =
|
|
25 |
{
|
69329
|
26 |
def err(c: Char): Nothing =
|
|
27 |
error("Bad character \\u" + String.format(Locale.ROOT, "%04x", new Integer(c)) +
|
|
28 |
" in fontforge string " + quote(s))
|
69328
|
29 |
|
69329
|
30 |
val q = if (s.contains('"')) '\'' else '"'
|
69328
|
31 |
|
|
32 |
def escape(c: Char): String =
|
|
33 |
{
|
69329
|
34 |
if (c == '\u0000' || c == '\r' || c == q) err(c)
|
69328
|
35 |
else if (c == '\n') "\\n"
|
|
36 |
else if (c == '\\') "\\\\"
|
|
37 |
else c.toString
|
|
38 |
}
|
|
39 |
|
|
40 |
if (s.nonEmpty && s(0) == '\\') err('\\')
|
69329
|
41 |
s.iterator.map(escape(_)).mkString(q.toString, "", q.toString)
|
69328
|
42 |
}
|
|
43 |
|
|
44 |
|
|
45 |
/* execute process */
|
|
46 |
|
|
47 |
def execute(script: Script, args: String = "", cwd: JFile = null): Process_Result =
|
|
48 |
Isabelle_System.with_tmp_file("fontforge")(script_file =>
|
|
49 |
{
|
|
50 |
File.write(script_file, script)
|
|
51 |
Isabelle_System.bash(File.bash_path(Path.explode("$ISABELLE_FONTFORGE")) +
|
|
52 |
" -lang=ff -script " + File.bash_path(script_file) + " " + args)
|
|
53 |
})
|
|
54 |
}
|