author | wenzelm |
Mon, 22 Jul 2019 11:39:30 +0200 | |
changeset 70393 | 9e53a98702b9 |
parent 69393 | ed0824ef337e |
child 73340 | 0ffcad1f6130 |
permissions | -rw-r--r-- |
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 |
|
69360 | 47 |
for (entry <- Isabelle_Fonts.fonts()) { |
48 |
val params = new DefaultFontMapper.BaseFontParameters(File.platform_path(entry.path)) |
|
49 |
params.encoding = BaseFont.IDENTITY_H |
|
50 |
params.embedded = true |
|
69365
c5b3860d29ef
avoid loading of font file, to eliminate "Illegal reflective access by com.lowagie.text.pdf.MappedRandomAccessFile$1 (iText-2.1.5.jar) to method java.nio.DirectByteBuffer.cleaner()" -- due to com.lowagie.text.pdf.TrueTypeFont.process() / RandomAccessFileOrArray;
wenzelm
parents:
69360
diff
changeset
|
51 |
params.ttfAfm = entry.bytes.array |
69360 | 52 |
mapper.putName(entry.name, params) |
61177 | 53 |
} |
54 |
mapper |
|
55 |
} |
|
56 |
||
65862 | 57 |
def write_pdf(file: JFile, paint: Graphics2D => Unit, width: Int, height: Int) |
51098 | 58 |
{ |
59 |
import com.lowagie.text.{Document, Rectangle} |
|
60 |
||
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69365
diff
changeset
|
61 |
using(new BufferedOutputStream(new FileOutputStream(file)))(out => |
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69365
diff
changeset
|
62 |
{ |
51098 | 63 |
val document = new Document() |
64 |
try { |
|
65 |
document.setPageSize(new Rectangle(width, height)) |
|
66 |
val writer = PdfWriter.getInstance(document, out) |
|
67 |
document.open() |
|
68 |
||
69 |
val cb = writer.getDirectContent() |
|
70 |
val tp = cb.createTemplate(width, height) |
|
61177 | 71 |
val gfx = tp.createGraphics(width, height, font_mapper()) |
51098 | 72 |
|
73 |
paint(gfx) |
|
74 |
gfx.dispose |
|
75 |
||
76 |
cb.addTemplate(tp, 1, 0, 0, 1, 0, 0) |
|
77 |
} |
|
78 |
finally { document.close() } |
|
69393
ed0824ef337e
static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents:
69365
diff
changeset
|
79 |
}) |
51098 | 80 |
} |
81 |
||
65862 | 82 |
|
83 |
/* JFreeChart */ |
|
84 |
||
85 |
def paint_chart(gfx: Graphics2D, chart: JFreeChart, width: Int, height: Int): Unit = |
|
86 |
chart.draw(gfx, new Rectangle2D.Double(0, 0, width, height)) |
|
51127 | 87 |
|
65862 | 88 |
def write_chart_png(file: JFile, chart: JFreeChart, width: Int, height: Int): Unit = |
89 |
write_png(file, paint_chart(_, chart, width, height), width, height) |
|
51127 | 90 |
|
65862 | 91 |
def write_chart_pdf(file: JFile, chart: JFreeChart, width: Int, height: Int): Unit = |
92 |
write_pdf(file, paint_chart(_, chart, width, height), width, height) |
|
51098 | 93 |
} |