src/Pure/Tools/fontforge.scala
changeset 69328 4646fcb59121
child 69329 8bbde4dba926
equal deleted inserted replaced
69327:264b44dce6be 69328:4646fcb59121
       
     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   {
       
    26     val quote = if (s.contains('"')) '\'' else '"'
       
    27 
       
    28     def err(c: Char): Nothing =
       
    29       error("Bad character in fontforge string: \\u" +
       
    30         String.format(Locale.ROOT, "%04x", new Integer(c)))
       
    31 
       
    32     def escape(c: Char): String =
       
    33     {
       
    34       if (c == '\u0000' || c == '\r' || c == quote) err(c)
       
    35       else if (c == '\n') "\\n"
       
    36       else if (c == '\\') "\\\\"
       
    37       else c.toString
       
    38     }
       
    39 
       
    40     if (s.nonEmpty && s(0) == '\\') err('\\')
       
    41     s.iterator.map(escape(_)).mkString(quote.toString, "", quote.toString)
       
    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 }