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 |
|
58451
|
10 |
import java.io.{FileOutputStream, BufferedOutputStream, File => JFile}
|
51098
|
11 |
import java.awt.Graphics2D
|
51127
|
12 |
import java.awt.geom.Rectangle2D
|
58451
|
13 |
import java.awt.image.BufferedImage
|
|
14 |
import javax.imageio.ImageIO
|
51098
|
15 |
|
51127
|
16 |
import org.jfree.chart.JFreeChart
|
|
17 |
|
61177
|
18 |
import com.lowagie.text.pdf.{PdfWriter, BaseFont, FontMapper, DefaultFontMapper}
|
|
19 |
|
51098
|
20 |
|
|
21 |
object Graphics_File
|
|
22 |
{
|
58451
|
23 |
/* PNG */
|
|
24 |
|
|
25 |
def write_png(file: JFile, paint: Graphics2D => Unit, width: Int, height: Int, dpi: Int = 72)
|
|
26 |
{
|
|
27 |
val scale = dpi / 72.0f
|
|
28 |
val w = (width * scale).round
|
|
29 |
val h = (height * scale).round
|
|
30 |
|
|
31 |
val img = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB)
|
|
32 |
val gfx = img.createGraphics
|
|
33 |
try {
|
|
34 |
gfx.scale(scale, scale)
|
|
35 |
paint(gfx)
|
|
36 |
ImageIO.write(img, "png", file)
|
|
37 |
}
|
|
38 |
finally { gfx.dispose }
|
|
39 |
}
|
|
40 |
|
|
41 |
|
51098
|
42 |
/* PDF */
|
|
43 |
|
61177
|
44 |
private def font_mapper(): FontMapper =
|
|
45 |
{
|
|
46 |
val mapper = new DefaultFontMapper
|
|
47 |
for {
|
65086
|
48 |
font <- Isabelle_System.fonts()
|
61177
|
49 |
name <- Library.try_unsuffix(".ttf", font.base.implode)
|
|
50 |
} {
|
|
51 |
val parameters = new DefaultFontMapper.BaseFontParameters(File.platform_path(font))
|
|
52 |
parameters.encoding = BaseFont.IDENTITY_H
|
|
53 |
mapper.putName(name, parameters)
|
|
54 |
}
|
|
55 |
mapper
|
|
56 |
}
|
|
57 |
|
|
58 |
def write_pdf(
|
|
59 |
file: JFile, paint: Graphics2D => Unit, width: Int, height: Int)
|
51098
|
60 |
{
|
|
61 |
import com.lowagie.text.{Document, Rectangle}
|
|
62 |
|
|
63 |
val out = new BufferedOutputStream(new FileOutputStream(file))
|
|
64 |
try {
|
|
65 |
val document = new Document()
|
|
66 |
try {
|
|
67 |
document.setPageSize(new Rectangle(width, height))
|
|
68 |
val writer = PdfWriter.getInstance(document, out)
|
|
69 |
document.open()
|
|
70 |
|
|
71 |
val cb = writer.getDirectContent()
|
|
72 |
val tp = cb.createTemplate(width, height)
|
61177
|
73 |
val gfx = tp.createGraphics(width, height, font_mapper())
|
51098
|
74 |
|
|
75 |
paint(gfx)
|
|
76 |
gfx.dispose
|
|
77 |
|
|
78 |
cb.addTemplate(tp, 1, 0, 0, 1, 0, 0)
|
|
79 |
}
|
|
80 |
finally { document.close() }
|
|
81 |
}
|
|
82 |
finally { out.close }
|
|
83 |
}
|
|
84 |
|
|
85 |
def write_pdf(path: Path, paint: Graphics2D => Unit, width: Int, height: Int): Unit =
|
|
86 |
write_pdf(path.file, paint, width, height)
|
51127
|
87 |
|
|
88 |
def write_pdf(file: JFile, chart: JFreeChart, width: Int, height: Int)
|
|
89 |
{
|
|
90 |
def paint(gfx: Graphics2D) = chart.draw(gfx, new Rectangle2D.Double(0, 0, width, height))
|
|
91 |
write_pdf(file, paint _, width, height)
|
|
92 |
}
|
|
93 |
|
|
94 |
def write_pdf(path: Path, chart: JFreeChart, width: Int, height: Int): Unit =
|
|
95 |
write_pdf(path.file, chart, width, height)
|
51098
|
96 |
}
|