| author | wenzelm | 
| Sun, 03 Sep 2023 16:44:07 +0200 | |
| changeset 78639 | ca56952b7322 | 
| parent 76449 | 739edaad4f42 | 
| child 82142 | 508a673c87ac | 
| permissions | -rw-r--r-- | 
| 59441 | 1 | /* Title: Tools/Graphview/graph_file.scala | 
| 2 | Author: Makarius | |
| 3 | ||
| 4 | File system operations for graph output. | |
| 5 | */ | |
| 6 | ||
| 7 | package isabelle.graphview | |
| 8 | ||
| 9 | ||
| 10 | import isabelle._ | |
| 11 | ||
| 12 | import java.io.{File => JFile}
 | |
| 13 | import java.awt.{Color, Graphics2D}
 | |
| 14 | ||
| 15 | ||
| 75393 | 16 | object Graph_File {
 | 
| 17 |   def write(file: JFile, graphview: Graphview): Unit = {
 | |
| 59459 | 18 | val box = graphview.bounding_box() | 
| 59441 | 19 | val w = box.width.ceil.toInt | 
| 20 | val h = box.height.ceil.toInt | |
| 21 | ||
| 75393 | 22 |     def paint(gfx: Graphics2D): Unit = {
 | 
| 61176 | 23 | gfx.setColor(graphview.background_color) | 
| 59441 | 24 | gfx.fillRect(0, 0, w, h) | 
| 25 | gfx.translate(- box.x, - box.y) | |
| 59460 | 26 | graphview.paint(gfx) | 
| 59441 | 27 | } | 
| 28 | ||
| 29 | val name = file.getName | |
| 75906 
2167b9e3157a
clarified signature: support for adhoc file types;
 wenzelm parents: 
75394diff
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: 
75394diff
changeset | 31 | else if (File.is_pdf(name)) Graphics_File.write_pdf(file, paint, w, h) | 
| 59441 | 32 |     else error("Bad type of file: " + quote(name) + " (.png or .pdf expected)")
 | 
| 33 | } | |
| 59443 
5b552b4f63a5
support for off-line graph output, without GUI thread;
 wenzelm parents: 
59441diff
changeset | 34 | |
| 75393 | 35 |   def write(options: Options, file: JFile, graph: Graph_Display.Graph): Unit = {
 | 
| 76449 | 36 | val graphview_options = options | 
| 59462 | 37 | val graphview = | 
| 76449 | 38 |       new Graphview(graph.transitive_reduction_acyclic) {
 | 
| 39 | def options: Options = graphview_options | |
| 40 | } | |
| 59459 | 41 | graphview.update_layout() | 
| 42 | write(file, graphview) | |
| 59443 
5b552b4f63a5
support for off-line graph output, without GUI thread;
 wenzelm parents: 
59441diff
changeset | 43 | } | 
| 72573 | 44 | |
| 45 | def make_pdf(options: Options, graph: Graph_Display.Graph): Bytes = | |
| 75394 | 46 |     Isabelle_System.with_tmp_file("graph", ext = "pdf") { graph_file =>
 | 
| 72573 | 47 | write(options, graph_file.file, graph) | 
| 48 | Bytes.read(graph_file) | |
| 75394 | 49 | } | 
| 59441 | 50 | } |