1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//
package io.github.moulberry.notenoughupdates.util;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import javax.imageio.ImageIO;
import javax.swing.JEditorPane;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.ImageView;
import javax.swing.text.html.StyleSheet;
public class HtmlImageGenerator {
public JEditorPane editorPane = this.createJEditorPane();
static final Dimension DEFAULT_SIZE = new Dimension(800, 800);
public HtmlImageGenerator() {
}
public ComponentOrientation getOrientation() {
return this.editorPane.getComponentOrientation();
}
public void setOrientation(ComponentOrientation orientation) {
this.editorPane.setComponentOrientation(orientation);
}
public Dimension getSize() {
return this.editorPane.getSize();
}
public void setSize(Dimension dimension) {
this.editorPane.setSize(dimension);
}
public void loadUrl(URL url) {
try {
this.editorPane.setPage(url);
} catch (IOException var3) {
throw new RuntimeException(String.format("Exception while loading %s", url), var3);
}
}
public void loadUrl(String url) {
try {
this.editorPane.setPage(url);
} catch (IOException var3) {
throw new RuntimeException(String.format("Exception while loading %s", url), var3);
}
}
public void loadHtml(String html) {
this.editorPane.setText(html);
this.onDocumentLoad();
}
public void saveAsImage(String file) {
this.saveAsImage(new File(file));
}
public void saveAsImage(File file) {
BufferedImage img = this.getBufferedImage();
try {
ImageIO.write(img, "png", file);
} catch (IOException var4) {
throw new RuntimeException(String.format("Exception while saving '%s' image", file), var4);
}
}
protected void onDocumentLoad() {
}
public Dimension getDefaultSize() {
return DEFAULT_SIZE;
}
public BufferedImage getBufferedImage() {
Dimension prefSize = this.editorPane.getPreferredSize();
BufferedImage img = new BufferedImage(prefSize.width, this.editorPane.getPreferredSize().height, 2);
Graphics graphics = img.getGraphics();
this.editorPane.setSize(prefSize);
this.editorPane.paint(graphics);
return img;
}
public void addCss(String css) {
HTMLEditorKit kit = (HTMLEditorKit) editorPane.getEditorKitForContentType("text/html");
kit.getStyleSheet().addRule(css);
}
public void setScale(float factor) {
editorPane.getDocument().putProperty("ZOOM_FACTOR", new Double(factor));
}
protected JEditorPane createJEditorPane() {
JEditorPane editorPane = new JEditorPane();
editorPane.setSize(this.getDefaultSize());
editorPane.setEditable(false);
HTMLEditorKit kit = new LargeHTMLEditorKit();
editorPane.setEditorKitForContentType("text/html", kit);
editorPane.setBackground(new Color(0, true));
editorPane.setContentType("text/html");
editorPane.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("page")) {
HtmlImageGenerator.this.onDocumentLoad();
}
}
});
return editorPane;
}
}
|