src/Pure/Tools/fontforge.scala
changeset 69335 76c8beaf3bab
parent 69334 6b49700da068
child 69370 589896fe1df2
equal deleted inserted replaced
69334:6b49700da068 69335:76c8beaf3bab
    44   def file_name(path: Path): Script = string(File.standard_path(path))
    44   def file_name(path: Path): Script = string(File.standard_path(path))
    45 
    45 
    46 
    46 
    47   /* commands */
    47   /* commands */
    48 
    48 
       
    49   def command_list(cmds: List[String]): Script = cat_lines(cmds)
       
    50   def commands(cmds: String*): Script = command_list(cmds.toList)
       
    51 
    49   // fonts
    52   // fonts
    50   def open(path: Path): Script = "Open(" + file_name(path) +")"
    53   def open(path: Path): Script = "Open(" + file_name(path) +")"
       
    54   def close: Script = "Close()"
    51   def generate(path: Path): Script = "Generate(" + file_name(path) +")"
    55   def generate(path: Path): Script = "Generate(" + file_name(path) +")"
    52   def set_font_names(
       
    53     fontname: String = "",
       
    54     family: String = "",
       
    55     fullname: String = "",
       
    56     weight: String = "",
       
    57     copyright: String = "",
       
    58     fontversion: String = ""): Script =
       
    59   {
       
    60     List(fontname, family, fullname, weight, copyright, fontversion).map(string(_)).
       
    61       mkString("SetFontNames(", ",", ")")
       
    62   }
       
    63 
    56 
    64   // selection
    57   // selection
    65   def select(args: Seq[Int]): Script = "SelectSingletons(" + args.mkString(",") + ")"
       
    66   def select_all: Script = "SelectAll()"
    58   def select_all: Script = "SelectAll()"
    67   def select_none: Script = "SelectNone()"
    59   def select_none: Script = "SelectNone()"
    68   def select_invert: Script = "SelectInvert()"
    60   def select_invert: Script = "SelectInvert()"
       
    61   def select(args: Seq[Int]): Script =
       
    62     command_list(select_none :: args.toList.map(code => "SelectMoreSingletons(" + code + ")"))
       
    63 
    69   def clear: Script = "Clear()"
    64   def clear: Script = "Clear()"
    70   def copy: Script = "Copy()"
    65   def copy: Script = "Copy()"
    71   def paste: Script = "Paste()"
    66   def paste: Script = "Paste()"
    72 
    67 
    73 
    68 
    82         " -lang=ff -script " + File.bash_path(script_file) + " " + args)
    77         " -lang=ff -script " + File.bash_path(script_file) + " " + args)
    83     })
    78     })
    84 
    79 
    85   def font_domain(path: Path, strict: Boolean = false): List[Int] =
    80   def font_domain(path: Path, strict: Boolean = false): List[Int] =
    86   {
    81   {
    87     val script = """
    82     val script =
    88 i = 0
    83       commands(
    89 while (i < CharCnt())
    84         open(path),
    90   if (""" + (if (strict) "DrawsSomething" else "WorthOutputting") + """(i)); Print(i); endif
    85         "i = 0",
    91   i = i + 1
    86         "while (i < CharCnt())",
    92 endloop
    87         "  if (" + (if (strict) "DrawsSomething" else "WorthOutputting") + "(i)); Print(i); endif",
    93 """
    88         "  i = i + 1",
    94     Library.trim_split_lines(execute(open(path) + script).check.out).
    89         "endloop",
    95       map(Value.Int.parse)
    90         close)
       
    91     Library.trim_split_lines(execute(script).check.out).map(Value.Int.parse)
       
    92   }
       
    93 
       
    94 
       
    95   /* font names */
       
    96 
       
    97   sealed case class Font_Names(
       
    98     fontname: String = "",
       
    99     familyname: String = "",
       
   100     fullname: String = "",
       
   101     weight: String = "",
       
   102     copyright: String = "",
       
   103     fontversion: String = "")
       
   104   {
       
   105     override def toString: String = fontname
       
   106 
       
   107     def ttf: Path = Path.explode(fontname).ext("ttf")
       
   108 
       
   109     def update(prefix: String = "", version: String = ""): Font_Names =
       
   110     {
       
   111       val fontversion1 = proper_string(version) getOrElse fontversion
       
   112       if (prefix == "") copy(fontversion = fontversion1)
       
   113       else {
       
   114         copy(fontname = prefix + fontname,
       
   115           familyname = prefix + " " + familyname,
       
   116           fullname = prefix + " " + fullname,
       
   117           weight = weight,
       
   118           copyright = copyright,
       
   119           fontversion = fontversion1)
       
   120       }
       
   121     }
       
   122 
       
   123     def set: Script =
       
   124       List(fontname, familyname, fullname, weight, copyright, fontversion).map(string(_)).
       
   125         mkString("SetFontNames(", ",", ")")
       
   126   }
       
   127 
       
   128   def font_names(path: Path): Font_Names =
       
   129   {
       
   130     val script =
       
   131       commands(
       
   132         open(path),
       
   133         "Print($fontname)",
       
   134         "Print($familyname)",
       
   135         "Print($fullname)",
       
   136         "Print($weight)",
       
   137         "Print($fontversion)",
       
   138         "Print($copyright)",
       
   139         close)
       
   140     Library.trim_split_lines(execute(script).check.out) match {
       
   141       case fontname :: familyname :: fullname :: weight :: fontversion :: copyright =>
       
   142         Font_Names(fontname, familyname, fullname, weight, cat_lines(copyright), fontversion)
       
   143       case res => error(cat_lines("Bad font names: " :: res))
       
   144     }
    96   }
   145   }
    97 }
   146 }