src/Pure/General/graphics_file.scala
changeset 51098 22d5c010ef5c
child 51127 5cf1604b9ef5
equal deleted inserted replaced
51097:72c355842f42 51098:22d5c010ef5c
       
     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
       
    11 import java.io.{FileOutputStream, BufferedOutputStream, File => JFile}
       
    12 
       
    13 
       
    14 object Graphics_File
       
    15 {
       
    16   /* PDF */
       
    17 
       
    18   def write_pdf(file: JFile, paint: Graphics2D => Unit, width: Int, height: Int)
       
    19   {
       
    20     import com.lowagie.text.{Document, Rectangle}
       
    21     import com.lowagie.text.pdf.PdfWriter
       
    22 
       
    23     val out = new BufferedOutputStream(new FileOutputStream(file))
       
    24     try {
       
    25       val document = new Document()
       
    26       try {
       
    27         document.setPageSize(new Rectangle(width, height))
       
    28         val writer = PdfWriter.getInstance(document, out)
       
    29         document.open()
       
    30 
       
    31         val cb = writer.getDirectContent()
       
    32         val tp = cb.createTemplate(width, height)
       
    33         val gfx = tp.createGraphics(width, height)
       
    34 
       
    35         paint(gfx)
       
    36         gfx.dispose
       
    37 
       
    38         cb.addTemplate(tp, 1, 0, 0, 1, 0, 0)
       
    39       }
       
    40       finally { document.close() }
       
    41     }
       
    42     finally { out.close }
       
    43   }
       
    44 
       
    45   def write_pdf(path: Path, paint: Graphics2D => Unit, width: Int, height: Int): Unit =
       
    46     write_pdf(path.file, paint, width, height)
       
    47 }
       
    48