src/Pure/General/graphics_file.scala
author wenzelm
Thu, 30 Oct 2014 22:45:19 +0100
changeset 58839 ccda99401bc8
parent 58451 9c3da105db2d
child 61177 8e6a3fbc91fa
permissions -rw-r--r--
eliminated aliases;
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
58451
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    10
import java.io.{FileOutputStream, BufferedOutputStream, File => JFile}
51098
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    11
import java.awt.Graphics2D
51127
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    12
import java.awt.geom.Rectangle2D
58451
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    13
import java.awt.image.BufferedImage
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    14
import javax.imageio.ImageIO
51098
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    15
51127
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    16
import org.jfree.chart.JFreeChart
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    17
51098
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    18
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    19
object Graphics_File
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    20
{
58451
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    21
  /* PNG */
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    22
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    23
  def write_png(file: JFile, paint: Graphics2D => Unit, width: Int, height: Int, dpi: Int = 72)
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    24
  {
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    25
    val scale = dpi / 72.0f
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    26
    val w = (width * scale).round
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    27
    val h = (height * scale).round
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    28
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    29
    val img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB)
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    30
    val gfx = img.createGraphics
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    31
    try {
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    32
      gfx.scale(scale, scale)
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    33
      paint(gfx)
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    34
      ImageIO.write(img, "png", file)
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    35
    }
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    36
    finally { gfx.dispose }
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    37
  }
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    38
9c3da105db2d support for PNG output;
wenzelm
parents: 51127
diff changeset
    39
51098
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    40
  /* PDF */
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    41
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    42
  def write_pdf(file: JFile, paint: Graphics2D => Unit, width: Int, height: Int)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    43
  {
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    44
    import com.lowagie.text.{Document, Rectangle}
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    45
    import com.lowagie.text.pdf.PdfWriter
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    46
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    47
    val out = new BufferedOutputStream(new FileOutputStream(file))
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    48
    try {
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    49
      val document = new Document()
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    50
      try {
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    51
        document.setPageSize(new Rectangle(width, height))
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    52
        val writer = PdfWriter.getInstance(document, out)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    53
        document.open()
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    54
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    55
        val cb = writer.getDirectContent()
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    56
        val tp = cb.createTemplate(width, height)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    57
        val gfx = tp.createGraphics(width, height)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    58
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    59
        paint(gfx)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    60
        gfx.dispose
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    61
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    62
        cb.addTemplate(tp, 1, 0, 0, 1, 0, 0)
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    63
      }
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    64
      finally { document.close() }
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    65
    }
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    66
    finally { out.close }
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    67
  }
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    68
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    69
  def write_pdf(path: Path, paint: Graphics2D => Unit, width: Int, height: Int): Unit =
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    70
    write_pdf(path.file, paint, width, height)
51127
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    71
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    72
  def write_pdf(file: JFile, chart: JFreeChart, width: Int, height: Int)
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    73
  {
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    74
    def paint(gfx: Graphics2D) = chart.draw(gfx, new Rectangle2D.Double(0, 0, width, height))
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    75
    write_pdf(file, paint _, width, height)
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    76
  }
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    77
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    78
  def write_pdf(path: Path, chart: JFreeChart, width: Int, height: Int): Unit =
5cf1604b9ef5 write_pdf for JFreeChart;
wenzelm
parents: 51098
diff changeset
    79
    write_pdf(path.file, chart, width, height)
51098
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    80
}
22d5c010ef5c file system operations for Graphics2D output;
wenzelm
parents:
diff changeset
    81