src/Pure/Admin/build_fonts.scala
author wenzelm
Sun Feb 10 18:12:24 2019 +0100 (5 months ago ago)
changeset 69975 04c50000fad1
parent 69974 4791988fcbc4
child 70066 6dc5506ad449
permissions -rw-r--r--
recovered missing glyph;
wenzelm@69349
     1
/*  Title:      Pure/Admin/build_fonts.scala
wenzelm@69340
     2
    Author:     Makarius
wenzelm@69340
     3
wenzelm@69364
     4
Build standard Isabelle fonts: DejaVu base + Isabelle symbols.
wenzelm@69340
     5
*/
wenzelm@69340
     6
wenzelm@69340
     7
package isabelle
wenzelm@69340
     8
wenzelm@69340
     9
wenzelm@69349
    10
object Build_Fonts
wenzelm@69340
    11
{
wenzelm@69340
    12
  /* relevant codepoint ranges */
wenzelm@69340
    13
wenzelm@69340
    14
  object Range
wenzelm@69340
    15
  {
wenzelm@69346
    16
    def base_font: Seq[Int] =
wenzelm@69340
    17
      (0x0020 to 0x007e) ++  // ASCII
wenzelm@69340
    18
      (0x00a0 to 0x024f) ++  // Latin Extended-A/B
wenzelm@69340
    19
      (0x0400 to 0x04ff) ++  // Cyrillic
wenzelm@69340
    20
      (0x0600 to 0x06ff) ++  // Arabic
wenzelm@69340
    21
      Seq(
wenzelm@69975
    22
        0x02dd,  // hungarumlaut
wenzelm@69340
    23
        0x2018,  // single quote
wenzelm@69340
    24
        0x2019,  // single quote
wenzelm@69340
    25
        0x201a,  // single quote
wenzelm@69340
    26
        0x201c,  // double quote
wenzelm@69340
    27
        0x201d,  // double quote
wenzelm@69340
    28
        0x201e,  // double quote
wenzelm@69340
    29
        0x2039,  // single guillemet
wenzelm@69340
    30
        0x203a,  // single guillemet
wenzelm@69341
    31
        0x204b,  // FOOTNOTE
wenzelm@69340
    32
        0x20ac,  // Euro
wenzelm@69340
    33
        0xfb01,  // ligature fi
wenzelm@69340
    34
        0xfb02,  // ligature fl
wenzelm@69340
    35
        0xfffd,  // replacement character
wenzelm@69340
    36
      )
wenzelm@69340
    37
wenzelm@69346
    38
    def isabelle_font: Seq[Int] =
wenzelm@69340
    39
      Seq(
wenzelm@69340
    40
        0x05,  // X
wenzelm@69340
    41
        0x06,  // Y
wenzelm@69340
    42
        0x07,  // EOF
wenzelm@69340
    43
        0x7f,  // DEL
wenzelm@69351
    44
        0xaf,  // INVERSE
wenzelm@69340
    45
        0xac,  // logicalnot
wenzelm@69340
    46
        0xb0,  // degree
wenzelm@69340
    47
        0xb1,  // plusminus
wenzelm@69340
    48
        0xb7,  // periodcentered
wenzelm@69340
    49
        0xd7,  // multiply
wenzelm@69340
    50
        0xf7,  // divide
wenzelm@69340
    51
      ) ++
wenzelm@69340
    52
      (0x0370 to 0x03ff) ++  // Greek (pseudo math)
wenzelm@69346
    53
      (0x0590 to 0x05ff) ++  // Hebrew (non-mono)
wenzelm@69340
    54
      Seq(
wenzelm@69340
    55
        0x2010,  // hyphen
wenzelm@69340
    56
        0x2013,  // dash
wenzelm@69340
    57
        0x2014,  // dash
wenzelm@69340
    58
        0x2015,  // dash
wenzelm@69340
    59
        0x2020,  // dagger
wenzelm@69340
    60
        0x2021,  // double dagger
wenzelm@69340
    61
        0x2022,  // bullet
wenzelm@69340
    62
        0x2026,  // ellipsis
wenzelm@69340
    63
        0x2030,  // perthousand
wenzelm@69340
    64
        0x2032,  // minute
wenzelm@69340
    65
        0x2033,  // second
wenzelm@69340
    66
        0x2038,  // caret
wenzelm@69340
    67
        0x20cd,  // currency symbol
wenzelm@69340
    68
      ) ++
wenzelm@69340
    69
      (0x2100 to 0x214f) ++  // Letterlike Symbols
wenzelm@69340
    70
      (0x2190 to 0x21ff) ++  // Arrows
wenzelm@69340
    71
      (0x2200 to 0x22ff) ++  // Mathematical Operators
wenzelm@69340
    72
      (0x2300 to 0x23ff) ++  // Miscellaneous Technical
wenzelm@69340
    73
      Seq(
wenzelm@69340
    74
        0x2423,  // graphic for space
wenzelm@69340
    75
        0x2500,  // box drawing
wenzelm@69340
    76
        0x2501,  // box drawing
wenzelm@69340
    77
        0x2508,  // box drawing
wenzelm@69340
    78
        0x2509,  // box drawing
wenzelm@69340
    79
        0x2550,  // box drawing
wenzelm@69340
    80
      ) ++
wenzelm@69340
    81
      (0x25a0 to 0x25ff) ++  // Geometric Shapes
wenzelm@69340
    82
      Seq(
wenzelm@69341
    83
        0x261b,  // ACTION
wenzelm@69340
    84
        0x2660,  // spade suit
wenzelm@69340
    85
        0x2661,  // heart suit
wenzelm@69340
    86
        0x2662,  // diamond suit
wenzelm@69340
    87
        0x2663,  // club suit
wenzelm@69340
    88
        0x266d,  // musical flat
wenzelm@69340
    89
        0x266e,  // musical natural
wenzelm@69340
    90
        0x266f,  // musical sharp
wenzelm@69340
    91
        0x2756,  // UNDEFINED
wenzelm@69340
    92
        0x2759,  // BOLD
wenzelm@69340
    93
        0x27a7,  // DESCR
wenzelm@69340
    94
        0x27e6,  // left white square bracket
wenzelm@69340
    95
        0x27e7,  // right white square bracket
wenzelm@69340
    96
        0x27e8,  // left angle bracket
wenzelm@69340
    97
        0x27e9,  // right angle bracket
wenzelm@69340
    98
      ) ++
wenzelm@69340
    99
      (0x27f0 to 0x27ff) ++  // Supplemental Arrows-A
wenzelm@69340
   100
      (0x2900 to 0x297f) ++  // Supplemental Arrows-B
wenzelm@69340
   101
      (0x2980 to 0x29ff) ++  // Miscellaneous Mathematical Symbols-B
wenzelm@69340
   102
      (0x2a00 to 0x2aff) ++  // Supplemental Mathematical Operators
wenzelm@69340
   103
      Seq(0x2b1a) ++  // VERBATIM
wenzelm@69340
   104
      (0x1d400 to 0x1d7ff) ++  // Mathematical Alphanumeric Symbols
wenzelm@69340
   105
      Seq(
wenzelm@69341
   106
        0x1f310,  // globe with meridians (Symbola font)
wenzelm@69341
   107
        0x1f4d3,  // notebook (Symbola font)
wenzelm@69341
   108
        0x1f5c0,  // folder (Symbola font)
wenzelm@69341
   109
        0x1f5cf,  // page (Symbola font)
wenzelm@69340
   110
      )
wenzelm@69381
   111
wenzelm@69447
   112
    def isabelle_math_font: Seq[Int] =
wenzelm@69447
   113
      (0x21 to 0x2f) ++  // bang .. slash
wenzelm@69447
   114
      (0x3a to 0x40) ++  // colon .. atsign
wenzelm@69447
   115
      (0x5b to 0x5f) ++  // leftbracket .. underscore
wenzelm@69447
   116
      (0x7b to 0x7e) ++  // leftbrace .. tilde
wenzelm@69447
   117
      Seq(
wenzelm@69447
   118
        0xa9,  // copyright
wenzelm@69447
   119
        0xae,  // registered
wenzelm@69447
   120
      )
wenzelm@69447
   121
wenzelm@69447
   122
    val vacuous_font: Seq[Int] =
wenzelm@69447
   123
      Seq(0x3c)  // "<" as template
wenzelm@69340
   124
  }
wenzelm@69340
   125
wenzelm@69340
   126
wenzelm@69340
   127
  /* font families */
wenzelm@69340
   128
wenzelm@69342
   129
  sealed case class Family(
wenzelm@69342
   130
    plain: String = "",
wenzelm@69342
   131
    bold: String = "",
wenzelm@69342
   132
    italic: String = "",
wenzelm@69346
   133
    bold_italic: String = "")
wenzelm@69346
   134
  {
wenzelm@69346
   135
    val fonts: List[String] =
wenzelm@69346
   136
      proper_string(plain).toList :::
wenzelm@69346
   137
      proper_string(bold).toList :::
wenzelm@69346
   138
      proper_string(italic).toList :::
wenzelm@69346
   139
      proper_string(bold_italic).toList
wenzelm@69346
   140
wenzelm@69346
   141
    def get(index: Int): String = fonts(index % fonts.length)
wenzelm@69346
   142
  }
wenzelm@69340
   143
wenzelm@69340
   144
  object Family
wenzelm@69340
   145
  {
wenzelm@69446
   146
    def isabelle_symbols: Family =
wenzelm@69346
   147
      Family(
wenzelm@69446
   148
        plain = "IsabelleSymbols.sfd",
wenzelm@69446
   149
        bold = "IsabelleSymbolsBold.sfd")
wenzelm@69346
   150
wenzelm@69342
   151
    def deja_vu_sans_mono: Family =
wenzelm@69340
   152
      Family(
wenzelm@69342
   153
        plain = "DejaVuSansMono.ttf",
wenzelm@69342
   154
        bold = "DejaVuSansMono-Bold.ttf",
wenzelm@69342
   155
        italic = "DejaVuSansMono-Oblique.ttf",
wenzelm@69346
   156
        bold_italic = "DejaVuSansMono-BoldOblique.ttf")
wenzelm@69340
   157
wenzelm@69342
   158
    def deja_vu_sans: Family =
wenzelm@69340
   159
      Family(
wenzelm@69342
   160
        plain = "DejaVuSans.ttf",
wenzelm@69342
   161
        bold = "DejaVuSans-Bold.ttf",
wenzelm@69342
   162
        italic = "DejaVuSans-Oblique.ttf",
wenzelm@69342
   163
        bold_italic = "DejaVuSans-BoldOblique.ttf")
wenzelm@69340
   164
wenzelm@69342
   165
    def deja_vu_serif: Family =
wenzelm@69340
   166
      Family(
wenzelm@69342
   167
        plain = "DejaVuSerif.ttf",
wenzelm@69342
   168
        bold = "DejaVuSerif-Bold.ttf",
wenzelm@69342
   169
        italic = "DejaVuSerif-Italic.ttf",
wenzelm@69342
   170
        bold_italic = "DejaVuSerif-BoldItalic.ttf")
wenzelm@69381
   171
wenzelm@69381
   172
    def vacuous: Family = Family(plain = "Vacuous.sfd")
wenzelm@69340
   173
  }
wenzelm@69346
   174
wenzelm@69346
   175
wenzelm@69974
   176
  /* auto-hinting */
wenzelm@69974
   177
  // see https://www.freetype.org/ttfautohint/doc/ttfautohint.html
wenzelm@69974
   178
wenzelm@69974
   179
  def auto_hint(source: Path, target: Path)
wenzelm@69974
   180
  {
wenzelm@69974
   181
    Isabelle_System.bash("ttfautohint -i " +
wenzelm@69974
   182
      File.bash_path(source) + " " + File.bash_path(target)).check
wenzelm@69974
   183
  }
wenzelm@69974
   184
wenzelm@69974
   185
wenzelm@69974
   186
wenzelm@69346
   187
  /* build fonts */
wenzelm@69346
   188
wenzelm@69346
   189
  private def find_file(dirs: List[Path], name: String): Path =
wenzelm@69346
   190
  {
wenzelm@69346
   191
    val path = Path.explode(name)
wenzelm@69346
   192
    dirs.collectFirst({ case dir if (dir + path).is_file => dir + path }) match {
wenzelm@69346
   193
      case Some(file) => file
wenzelm@69346
   194
      case None =>
wenzelm@69346
   195
        error(cat_lines(
wenzelm@69346
   196
          ("Failed to find font file " + path + " in directories:") ::
wenzelm@69346
   197
            dirs.map(dir => "  " + dir.toString)))
wenzelm@69346
   198
    }
wenzelm@69346
   199
  }
wenzelm@69346
   200
wenzelm@69346
   201
  val default_sources: List[Family] =
wenzelm@69346
   202
    List(Family.deja_vu_sans_mono, Family.deja_vu_sans, Family.deja_vu_serif)
wenzelm@69346
   203
wenzelm@69382
   204
  val default_target_dir: Path = Path.explode("isabelle_fonts")
wenzelm@69382
   205
wenzelm@69346
   206
  def build_fonts(
wenzelm@69346
   207
    sources: List[Family] = default_sources,
wenzelm@69346
   208
    source_dirs: List[Path] = Nil,
wenzelm@69346
   209
    target_prefix: String = "Isabelle",
wenzelm@69346
   210
    target_version: String = "",
wenzelm@69382
   211
    target_dir: Path = default_target_dir,
wenzelm@69346
   212
    progress: Progress = No_Progress)
wenzelm@69346
   213
  {
wenzelm@69382
   214
    progress.echo("Directory " + target_dir)
wenzelm@69346
   215
    Isabelle_System.mkdirs(target_dir)
wenzelm@69346
   216
wenzelm@69364
   217
    val font_dirs = source_dirs ::: List(Path.explode("~~/Admin/isabelle_fonts"))
wenzelm@69364
   218
    for (dir <- font_dirs if !dir.is_dir) error("Bad source directory: " + dir)
wenzelm@69346
   219
wenzelm@69381
   220
wenzelm@69381
   221
    // Isabelle fonts
wenzelm@69381
   222
wenzelm@69381
   223
    val targets =
wenzelm@69381
   224
      for { source <- sources; (source_font, index) <- source.fonts.zipWithIndex }
wenzelm@69381
   225
      yield {
wenzelm@69446
   226
        val isabelle_file = find_file(font_dirs, Family.isabelle_symbols.get(index))
wenzelm@69381
   227
wenzelm@69381
   228
        val source_file = find_file(font_dirs, source_font)
wenzelm@69381
   229
        val source_names = Fontforge.font_names(source_file)
wenzelm@69381
   230
wenzelm@69381
   231
        val target_names = source_names.update(prefix = target_prefix, version = target_version)
wenzelm@69381
   232
        val target_file = target_dir + target_names.ttf
wenzelm@69381
   233
wenzelm@69382
   234
        progress.echo("Font " + target_file.toString + " ...")
wenzelm@69974
   235
        Isabelle_System.with_tmp_file("font", "ttf")(tmp_file =>
wenzelm@69974
   236
        {
wenzelm@69974
   237
          auto_hint(source_file, tmp_file)
wenzelm@69974
   238
wenzelm@69974
   239
          Fontforge.execute(
wenzelm@69974
   240
            Fontforge.commands(
wenzelm@69974
   241
              Fontforge.open(isabelle_file),
wenzelm@69974
   242
              Fontforge.select(Range.isabelle_font),
wenzelm@69974
   243
              Fontforge.copy,
wenzelm@69974
   244
              Fontforge.close,
wenzelm@69346
   245
wenzelm@69974
   246
              Fontforge.open(tmp_file),
wenzelm@69974
   247
              Fontforge.select(Range.base_font),
wenzelm@69974
   248
              Fontforge.select_invert,
wenzelm@69974
   249
              Fontforge.clear,
wenzelm@69974
   250
              Fontforge.select(Range.isabelle_font),
wenzelm@69974
   251
              Fontforge.paste,
wenzelm@69346
   252
wenzelm@69974
   253
              target_names.set,
wenzelm@69974
   254
              Fontforge.generate(target_file),
wenzelm@69974
   255
              Fontforge.close)
wenzelm@69974
   256
          ).check
wenzelm@69974
   257
        })
wenzelm@69381
   258
wenzelm@69381
   259
        (target_file, index)
wenzelm@69381
   260
      }
wenzelm@69381
   261
wenzelm@69381
   262
wenzelm@69381
   263
    // Vacuous font
wenzelm@69381
   264
wenzelm@69381
   265
    {
wenzelm@69381
   266
      val vacuous_file = find_file(font_dirs, Family.vacuous.get(0))
wenzelm@69381
   267
wenzelm@69381
   268
      val target_names = Fontforge.font_names(vacuous_file)
wenzelm@69346
   269
      val target_file = target_dir + target_names.ttf
wenzelm@69346
   270
wenzelm@69382
   271
      progress.echo("Font " + target_file.toString + " ...")
wenzelm@69381
   272
wenzelm@69381
   273
      val domain =
wenzelm@69381
   274
        (for ((target_file, index) <- targets if index == 0)
wenzelm@69381
   275
          yield Fontforge.font_domain(target_file)).flatten.toSet.toList.sorted
wenzelm@69381
   276
wenzelm@69346
   277
      Fontforge.execute(
wenzelm@69346
   278
        Fontforge.commands(
wenzelm@69381
   279
          Fontforge.open(vacuous_file),
wenzelm@69381
   280
          Fontforge.select(Range.vacuous_font),
wenzelm@69381
   281
          Fontforge.copy) +
wenzelm@69346
   282
wenzelm@69381
   283
        domain.map(code =>
wenzelm@69381
   284
            Fontforge.commands(
wenzelm@69381
   285
              Fontforge.select(Seq(code)),
wenzelm@69381
   286
              Fontforge.paste))
wenzelm@69381
   287
          .mkString("\n", "\n", "\n") +
wenzelm@69346
   288
wenzelm@69381
   289
        Fontforge.commands(
wenzelm@69346
   290
          Fontforge.generate(target_file),
wenzelm@69346
   291
          Fontforge.close)
wenzelm@69346
   292
      ).check
wenzelm@69346
   293
    }
wenzelm@69383
   294
wenzelm@69383
   295
wenzelm@69383
   296
    // etc/settings
wenzelm@69383
   297
wenzelm@69405
   298
    val settings_path = Components.settings(target_dir)
wenzelm@69383
   299
    Isabelle_System.mkdirs(settings_path.dir)
wenzelm@69383
   300
    File.write(settings_path,
wenzelm@69383
   301
      "# -*- shell-script -*- :mode=shellscript:\n\nisabelle_fonts \\\n" +
wenzelm@69383
   302
      (for ((path, _) <- targets)
wenzelm@69383
   303
        yield """  "$COMPONENT/""" + path.file_name + "\"").mkString(" \\\n") +
wenzelm@69383
   304
      """
wenzelm@69383
   305
wenzelm@69384
   306
isabelle_fonts_hidden "$COMPONENT/Vacuous.ttf"
wenzelm@69383
   307
""")
wenzelm@69383
   308
wenzelm@69383
   309
wenzelm@69383
   310
    // README
wenzelm@69383
   311
    File.copy(Path.explode("~~/Admin/isabelle_fonts/README"), target_dir)
wenzelm@69346
   312
  }
wenzelm@69347
   313
wenzelm@69347
   314
wenzelm@69347
   315
  /* Isabelle tool wrapper */
wenzelm@69347
   316
wenzelm@69347
   317
  val isabelle_tool =
wenzelm@69347
   318
    Isabelle_Tool("build_fonts", "construct Isabelle fonts", args =>
wenzelm@69347
   319
    {
wenzelm@69347
   320
      var source_dirs: List[Path] = Nil
wenzelm@69347
   321
wenzelm@69347
   322
      val getopts = Getopts("""
wenzelm@69347
   323
Usage: isabelle build_fonts [OPTIONS]
wenzelm@69347
   324
wenzelm@69347
   325
  Options are:
wenzelm@69347
   326
    -d DIR       additional source directory
wenzelm@69347
   327
wenzelm@69353
   328
  Construct Isabelle fonts from DejaVu font families and Isabelle symbols.
wenzelm@69347
   329
""",
wenzelm@69347
   330
        "d:" -> (arg => source_dirs = source_dirs ::: List(Path.explode(arg))))
wenzelm@69347
   331
wenzelm@69347
   332
      val more_args = getopts(args)
wenzelm@69347
   333
      if (more_args.nonEmpty) getopts.usage()
wenzelm@69347
   334
wenzelm@69382
   335
      val target_version = Date.Format("uuuuMMdd")(Date.now())
wenzelm@69382
   336
      val target_dir = Path.explode("isabelle_fonts-" + target_version)
wenzelm@69382
   337
wenzelm@69347
   338
      val progress = new Console_Progress
wenzelm@69347
   339
wenzelm@69382
   340
      build_fonts(source_dirs = source_dirs, target_dir = target_dir,
wenzelm@69382
   341
        target_version = target_version, progress = progress)
wenzelm@69347
   342
    })
wenzelm@69340
   343
}