-
Notifications
You must be signed in to change notification settings - Fork 70
Expand file tree
/
Copy pathHtmlImageGenerator.java
More file actions
213 lines (183 loc) · 7.18 KB
/
HtmlImageGenerator.java
File metadata and controls
213 lines (183 loc) · 7.18 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package gui.ava.html.image;
import gui.ava.html.Html2Image;
import gui.ava.html.image.util.FormatNameUtil;
import gui.ava.html.image.util.SynchronousHTMLEditorKit;
import gui.ava.html.link.LinkInfo;
import javax.imageio.ImageIO;
import javax.swing.*;
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.Collection;
import java.util.Collections;
import java.util.List;
/**
* @author Yoav Aharoni
*/
public class HtmlImageGenerator {
private JEditorPane editorPane;
private Color backgroundColor = Color.WHITE;
static final Dimension DEFAULT_SIZE = new Dimension(800, 800);
public HtmlImageGenerator() {
editorPane = createJEditorPane();
}
public HtmlImageGenerator(Color backgroundColor) {
this.backgroundColor = backgroundColor;
editorPane = createJEditorPane();
}
public ComponentOrientation getOrientation() {
return editorPane.getComponentOrientation();
}
public void setOrientation(ComponentOrientation orientation) {
editorPane.setComponentOrientation(orientation);
}
public Dimension getSize() {
return editorPane.getSize();
}
public void setSize(Dimension dimension) {
editorPane.setPreferredSize(dimension);
}
public void loadUrl(URL url) {
try {
editorPane.setPage(url);
} catch (IOException e) {
throw new RuntimeException(String.format("Exception while loading %s", url), e);
}
}
public void loadUrl(String url) {
try {
editorPane.setPage(url);
} catch (IOException e) {
throw new RuntimeException(String.format("Exception while loading %s", url), e);
}
}
public void loadHtml(String html) {
editorPane.setEditable(false);
editorPane.setText(html);
editorPane.setContentType("text/html");
onDocumentLoad();
}
public String getLinksMapMarkup(String mapName) {
final StringBuilder markup = new StringBuilder();
markup.append("<map name=\"").append(mapName).append("\">\n");
for (LinkInfo link : getLinks()) {
final List<Rectangle> bounds = link.getBounds();
for (Rectangle bound : bounds) {
final int x1 = (int) bound.getX();
final int y1 = (int) bound.getY();
final int x2 = (int) (x1 + bound.getWidth());
final int y2 = (int) (y1 + bound.getHeight());
markup.append(String.format("<area href=\"%s\" coords=\"%s,%s,%s,%s\" shape=\"rect\"", link.getHref(), x1, y1, x2, y2));
final String title = link.getTitle();
if (title != null && !title.equals("")) {
markup.append(" title=\"").append(title.replace("\"", """)).append("\"");
}
markup.append(">\n");
}
}
markup.append("</map>\n");
return markup.toString();
}
public List<LinkInfo> getLinks() {
// final LinkHarvester harvester = new LinkHarvester(editorPane);
// return harvester.getLinks();
return Collections.emptyList();
}
public void saveAsHtmlWithMap(String file, String imageUrl) {
saveAsHtmlWithMap(new File(file), imageUrl);
}
public void saveAsHtmlWithMap(File file, String imageUrl) {
FileWriter writer = null;
try {
writer = new FileWriter(file);
writer.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n");
writer.append("<html>\n<head></head>\n");
writer.append("<body style=\"margin: 0; padding: 0; text-align: center;\">\n");
final String htmlMap = getLinksMapMarkup("map");
writer.write(htmlMap);
writer.append("<img border=\"0\" usemap=\"#map\" src=\"");
writer.append(imageUrl);
writer.append("\"/>\n");
writer.append("</body>\n</html>");
} catch (IOException e) {
throw new RuntimeException(String.format("Exception while saving '%s' html file", file), e);
} finally {
if (writer != null) {
try {
writer.close();
} catch (IOException ignore) {
}
}
}
}
public void saveAsImage(String file) {
saveAsImage(new File(file));
}
public void saveAsImage(File file) {
BufferedImage image = getBufferedImage();
BufferedImage bufferedImageToWrite = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_ARGB);
bufferedImageToWrite.createGraphics().drawImage(image, 0, 0, this.backgroundColor, null);
final String formatName = FormatNameUtil.formatForFilename(file.getName());
try {
if (!ImageIO.write(bufferedImageToWrite, formatName, file)) {
throw new IOException("No formatter for specified file type [" + formatName + "] available");
}
} catch (IOException e) {
throw new RuntimeException(String.format("Exception while saving '%s' image", file), e);
}
}
protected void onDocumentLoad() {
}
public Dimension getDefaultSize() {
return DEFAULT_SIZE;
}
public BufferedImage getBufferedImage() {
JFrame frame = new JFrame();
frame.getRootPane().setOpaque(false);
frame.setPreferredSize(editorPane.getPreferredSize());
frame.setUndecorated(true);
frame.add(editorPane);
frame.pack();
Dimension prefSize = frame.getPreferredSize();
BufferedImage img = new BufferedImage(prefSize.width, prefSize.height, BufferedImage.TYPE_INT_ARGB);
Graphics graphics = img.getGraphics();
frame.setVisible(true);
frame.paint(graphics);
frame.setVisible(false);
frame.dispose();
return img;
}
protected JEditorPane createJEditorPane() {
final JEditorPane editorPane = new JEditorPane();
editorPane.setSize(getDefaultSize());
editorPane.setEditable(false);
editorPane.setBackground(this.backgroundColor);
final SynchronousHTMLEditorKit kit = new SynchronousHTMLEditorKit();
editorPane.setEditorKitForContentType("text/html", kit);
editorPane.setContentType("text/html");
editorPane.addPropertyChangeListener(new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("page")) {
onDocumentLoad();
}
}
});
return editorPane;
}
public void show() {
JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame();
frame.setTitle("My First Swing Application");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
JLabel label = new JLabel("Welcome");
frame.add(label);
frame.add(editorPane);
frame.pack();
frame.setVisible(true);
}
}