src/Pure/General/graphics_file.scala
author blanchet
Wed, 24 Sep 2014 15:45:55 +0200
changeset 58425 246985c6b20b
parent 51127 5cf1604b9ef5
child 58451 9c3da105db2d
permissions -rw-r--r--
simpler proof
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
51098
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/graphics_file.scala
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
     3
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
     4
File system operations for Graphics2D output.
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
     5
*/
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
     6
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
     7
package isabelle
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
     8
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
     9
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    10
import java.awt.Graphics2D
51127
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    11
import java.awt.geom.Rectangle2D
51098
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    12
import java.io.{FileOutputStream, BufferedOutputStream, File => JFile}
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    13
51127
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    14
import org.jfree.chart.JFreeChart
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    15
51098
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    16
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    17
object Graphics_File
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    18
{
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    19
  /* PDF */
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    20
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    21
  def write_pdf(file: JFile, paint: Graphics2D => Unit, width: Int, height: Int)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    22
  {
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    23
    import com.lowagie.text.{Document, Rectangle}
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    24
    import com.lowagie.text.pdf.PdfWriter
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    25
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    26
    val out = new BufferedOutputStream(new FileOutputStream(file))
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    27
    try {
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    28
      val document = new Document()
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    29
      try {
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    30
        document.setPageSize(new Rectangle(width, height))
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    31
        val writer = PdfWriter.getInstance(document, out)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    32
        document.open()
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    33
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    34
        val cb = writer.getDirectContent()
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    35
        val tp = cb.createTemplate(width, height)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    36
        val gfx = tp.createGraphics(width, height)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    37
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    38
        paint(gfx)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    39
        gfx.dispose
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    40
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    41
        cb.addTemplate(tp, 1, 0, 0, 1, 0, 0)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    42
      }
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    43
      finally { document.close() }
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    44
    }
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    45
    finally { out.close }
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    46
  }
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    47
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    48
  def write_pdf(path: Path, paint: Graphics2D => Unit, width: Int, height: Int): Unit =
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    49
    write_pdf(path.file, paint, width, height)
51127
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    50
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    51
  def write_pdf(file: JFile, chart: JFreeChart, width: Int, height: Int)
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    52
  {
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    53
    def paint(gfx: Graphics2D) = chart.draw(gfx, new Rectangle2D.Double(0, 0, width, height))
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    54
    write_pdf(file, paint _, width, height)
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    55
  }
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    56
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    57
  def write_pdf(path: Path, chart: JFreeChart, width: Int, height: Int): Unit =
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    58
    write_pdf(path.file, chart, width, height)
51098
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    59
}
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    60