src/Tools/Graphview/graph_file.scala
author wenzelm
Wed, 22 Jan 2025 22:22:19 +0100
changeset 81954 6f2bcdfa9a19
parent 76449 739edaad4f42
child 82142 508a673c87ac
permissions -rw-r--r--
misc tuning: more concise operations on prems (without change of exceptions); discontinue odd clone Drule.cprems_of (see also 991a3feaf270);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
59441
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
     1
/*  Title:      Tools/Graphview/graph_file.scala
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
     3
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
     4
File system operations for graph output.
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
     5
*/
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
     6
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
     7
package isabelle.graphview
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
     8
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
     9
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    10
import isabelle._
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    11
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    12
import java.io.{File => JFile}
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    13
import java.awt.{Color, Graphics2D}
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    14
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    15
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    16
object Graph_File {
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    17
  def write(file: JFile, graphview: Graphview): Unit = {
59459
985fc55e9f27 clarified module name;
wenzelm
parents: 59443
diff changeset
    18
    val box = graphview.bounding_box()
59441
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    19
    val w = box.width.ceil.toInt
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    20
    val h = box.height.ceil.toInt
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    21
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    22
    def paint(gfx: Graphics2D): Unit = {
61176
9791f631c20d avoid hardwired colors;
wenzelm
parents: 59462
diff changeset
    23
      gfx.setColor(graphview.background_color)
59441
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    24
      gfx.fillRect(0, 0, w, h)
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    25
      gfx.translate(- box.x, - box.y)
59460
3a357fef24e8 tuned signature;
wenzelm
parents: 59459
diff changeset
    26
      graphview.paint(gfx)
59441
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    27
    }
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    28
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    29
    val name = file.getName
75906
2167b9e3157a clarified signature: support for adhoc file types;
wenzelm
parents: 75394
diff changeset
    30
    if (File.is_png(name)) Graphics_File.write_png(file, paint, w, h)
2167b9e3157a clarified signature: support for adhoc file types;
wenzelm
parents: 75394
diff changeset
    31
    else if (File.is_pdf(name)) Graphics_File.write_pdf(file, paint, w, h)
59441
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    32
    else error("Bad type of file: " + quote(name) + " (.png or .pdf expected)")
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    33
  }
59443
5b552b4f63a5 support for off-line graph output, without GUI thread;
wenzelm
parents: 59441
diff changeset
    34
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 73340
diff changeset
    35
  def write(options: Options, file: JFile, graph: Graph_Display.Graph): Unit = {
76449
739edaad4f42 tuned signature;
wenzelm
parents: 75906
diff changeset
    36
    val graphview_options = options
59462
c7eff4356885 tuned signature;
wenzelm
parents: 59460
diff changeset
    37
    val graphview =
76449
739edaad4f42 tuned signature;
wenzelm
parents: 75906
diff changeset
    38
      new Graphview(graph.transitive_reduction_acyclic) {
739edaad4f42 tuned signature;
wenzelm
parents: 75906
diff changeset
    39
        def options: Options = graphview_options
739edaad4f42 tuned signature;
wenzelm
parents: 75906
diff changeset
    40
      }
59459
985fc55e9f27 clarified module name;
wenzelm
parents: 59443
diff changeset
    41
    graphview.update_layout()
985fc55e9f27 clarified module name;
wenzelm
parents: 59443
diff changeset
    42
    write(file, graphview)
59443
5b552b4f63a5 support for off-line graph output, without GUI thread;
wenzelm
parents: 59441
diff changeset
    43
  }
72573
a085a1a89388 more operations;
wenzelm
parents: 71601
diff changeset
    44
a085a1a89388 more operations;
wenzelm
parents: 71601
diff changeset
    45
  def make_pdf(options: Options, graph: Graph_Display.Graph): Bytes =
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    46
    Isabelle_System.with_tmp_file("graph", ext = "pdf") { graph_file =>
72573
a085a1a89388 more operations;
wenzelm
parents: 71601
diff changeset
    47
      write(options, graph_file.file, graph)
a085a1a89388 more operations;
wenzelm
parents: 71601
diff changeset
    48
      Bytes.read(graph_file)
75394
42267c650205 tuned formatting;
wenzelm
parents: 75393
diff changeset
    49
    }
59441
ab2c3597f1d3 separate module Graph_File;
wenzelm
parents:
diff changeset
    50
}