| 
51098
 | 
     1  | 
/*  Title:      Pure/General/graphics_file.scala
  | 
| 
 | 
     2  | 
    Author:     Makarius
  | 
| 
 | 
     3  | 
  | 
| 
 | 
     4  | 
File system operations for Graphics2D output.
  | 
| 
 | 
     5  | 
*/
  | 
| 
 | 
     6  | 
  | 
| 
 | 
     7  | 
package isabelle
  | 
| 
 | 
     8  | 
  | 
| 
 | 
     9  | 
  | 
| 
 | 
    10  | 
import java.awt.Graphics2D
  | 
| 
51127
 | 
    11  | 
import java.awt.geom.Rectangle2D
  | 
| 
51098
 | 
    12  | 
import java.io.{FileOutputStream, BufferedOutputStream, File => JFile}
 | 
| 
 | 
    13  | 
  | 
| 
51127
 | 
    14  | 
import org.jfree.chart.JFreeChart
  | 
| 
 | 
    15  | 
  | 
| 
51098
 | 
    16  | 
  | 
| 
 | 
    17  | 
object Graphics_File
  | 
| 
 | 
    18  | 
{
 | 
| 
 | 
    19  | 
  /* PDF */
  | 
| 
 | 
    20  | 
  | 
| 
 | 
    21  | 
  def write_pdf(file: JFile, paint: Graphics2D => Unit, width: Int, height: Int)
  | 
| 
 | 
    22  | 
  {
 | 
| 
 | 
    23  | 
    import com.lowagie.text.{Document, Rectangle}
 | 
| 
 | 
    24  | 
    import com.lowagie.text.pdf.PdfWriter
  | 
| 
 | 
    25  | 
  | 
| 
 | 
    26  | 
    val out = new BufferedOutputStream(new FileOutputStream(file))
  | 
| 
 | 
    27  | 
    try {
 | 
| 
 | 
    28  | 
      val document = new Document()
  | 
| 
 | 
    29  | 
      try {
 | 
| 
 | 
    30  | 
        document.setPageSize(new Rectangle(width, height))
  | 
| 
 | 
    31  | 
        val writer = PdfWriter.getInstance(document, out)
  | 
| 
 | 
    32  | 
        document.open()
  | 
| 
 | 
    33  | 
  | 
| 
 | 
    34  | 
        val cb = writer.getDirectContent()
  | 
| 
 | 
    35  | 
        val tp = cb.createTemplate(width, height)
  | 
| 
 | 
    36  | 
        val gfx = tp.createGraphics(width, height)
  | 
| 
 | 
    37  | 
  | 
| 
 | 
    38  | 
        paint(gfx)
  | 
| 
 | 
    39  | 
        gfx.dispose
  | 
| 
 | 
    40  | 
  | 
| 
 | 
    41  | 
        cb.addTemplate(tp, 1, 0, 0, 1, 0, 0)
  | 
| 
 | 
    42  | 
      }
  | 
| 
 | 
    43  | 
      finally { document.close() }
 | 
| 
 | 
    44  | 
    }
  | 
| 
 | 
    45  | 
    finally { out.close }
 | 
| 
 | 
    46  | 
  }
  | 
| 
 | 
    47  | 
  | 
| 
 | 
    48  | 
  def write_pdf(path: Path, paint: Graphics2D => Unit, width: Int, height: Int): Unit =
  | 
| 
 | 
    49  | 
    write_pdf(path.file, paint, width, height)
  | 
| 
51127
 | 
    50  | 
  | 
| 
 | 
    51  | 
  def write_pdf(file: JFile, chart: JFreeChart, width: Int, height: Int)
  | 
| 
 | 
    52  | 
  {
 | 
| 
 | 
    53  | 
    def paint(gfx: Graphics2D) = chart.draw(gfx, new Rectangle2D.Double(0, 0, width, height))
  | 
| 
 | 
    54  | 
    write_pdf(file, paint _, width, height)
  | 
| 
 | 
    55  | 
  }
  | 
| 
 | 
    56  | 
  | 
| 
 | 
    57  | 
  def write_pdf(path: Path, chart: JFreeChart, width: Int, height: Int): Unit =
  | 
| 
 | 
    58  | 
    write_pdf(path.file, chart, width, height)
  | 
| 
51098
 | 
    59  | 
}
  | 
| 
 | 
    60  | 
  |