-
Notifications
You must be signed in to change notification settings - Fork 84
Expand file tree
/
Copy pathImageRenderer.java
More file actions
170 lines (144 loc) · 6.03 KB
/
ImageRenderer.java
File metadata and controls
170 lines (144 loc) · 6.03 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
/**
* ImageRenderer.java
* Copyright (c) 2005-2014 Radek Burget
*
* CSSBox is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* CSSBox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with CSSBox. If not, see <http://www.gnu.org/licenses/>.
*
* Created on 5.2.2009, 12:00:02 by burgetr
*/
package org.fit.cssbox.demo;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Arrays;
import javax.imageio.ImageIO;
import org.fit.cssbox.awt.GraphicsEngine;
import org.fit.cssbox.css.CSSNorm;
import org.fit.cssbox.css.DOMAnalyzer;
import org.fit.cssbox.io.DOMSource;
import org.fit.cssbox.io.DefaultDOMSource;
import org.fit.cssbox.io.DefaultDocumentSource;
import org.fit.cssbox.io.DocumentSource;
import org.fit.cssbox.layout.BrowserConfig;
import org.fit.cssbox.layout.Dimension;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import cz.vutbr.web.css.MediaSpec;
/**
* This class provides a rendering interface for obtaining the document image
* form an URL.
*
* @author burgetr
*/
public class ImageRenderer
{
private String mediaType = "screen";
private Dimension windowSize;
private boolean cropWindow = false;
private boolean loadImages = true;
private boolean loadBackgroundImages = true;
public ImageRenderer()
{
windowSize = new Dimension(1200, 600);
}
public void setMediaType(String media)
{
mediaType = new String(media);
}
public void setWindowSize(Dimension size, boolean crop)
{
windowSize = new Dimension(size);
cropWindow = crop;
}
public void setLoadImages(boolean content, boolean background)
{
loadImages = content;
loadBackgroundImages = background;
}
/**
* Renders the URL and prints the result to the specified output stream in the specified
* format.
* @param urlstring the source URL
* @param out output stream
* @return true in case of success, false otherwise
* @throws SAXException
*/
public boolean renderURL(String urlstring, OutputStream out) throws IOException, SAXException
{
if (!urlstring.startsWith("http:") &&
!urlstring.startsWith("https:") &&
!urlstring.startsWith("ftp:") &&
!urlstring.startsWith("file:"))
urlstring = "http://" + urlstring;
//Open the network connection
DocumentSource docSource = new DefaultDocumentSource(urlstring);
//Parse the input document
DOMSource parser = new DefaultDOMSource(docSource);
Document doc = parser.parse();
//create the media specification
MediaSpec media = new MediaSpec(mediaType);
media.setDimensions(windowSize.width, windowSize.height);
media.setDeviceDimensions(windowSize.width, windowSize.height);
//Create the CSS analyzer
DOMAnalyzer da = new DOMAnalyzer(doc, docSource.getURL());
da.setMediaSpec(media);
da.attributesToStyles(); //convert the HTML presentation attributes to inline styles
da.addStyleSheet(null, CSSNorm.stdStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the standard style sheet
da.addStyleSheet(null, CSSNorm.userStyleSheet(), DOMAnalyzer.Origin.AGENT); //use the additional style sheet
da.addStyleSheet(null, CSSNorm.formsStyleSheet(), DOMAnalyzer.Origin.AGENT); //render form fields using css
da.getStyleSheets(); //load the author style sheets
GraphicsEngine contentCanvas = new GraphicsEngine(da.getRoot(), da, docSource.getURL());
contentCanvas.setAutoMediaUpdate(false); //we have a correct media specification, do not update
contentCanvas.getConfig().setClipViewport(cropWindow);
contentCanvas.getConfig().setLoadImages(loadImages);
contentCanvas.getConfig().setLoadBackgroundImages(loadBackgroundImages);
contentCanvas.createLayout(windowSize);
ImageIO.write(contentCanvas.getImage(), "png", out);
docSource.close();
return true;
}
/**
* Sets some common fonts as the defaults for generic font families.
*/
protected void defineLogicalFonts(BrowserConfig config)
{
config.setLogicalFont(BrowserConfig.SERIF, Arrays.asList("Times", "Times New Roman"));
config.setLogicalFont(BrowserConfig.SANS_SERIF, Arrays.asList("Arial", "Helvetica"));
config.setLogicalFont(BrowserConfig.MONOSPACE, Arrays.asList("Courier New", "Courier"));
}
//=================================================================================
public static void main(String[] args)
{
if (args.length != 2)
{
System.err.println("Usage: ImageRenderer <url> <output_file>");
System.err.println();
System.err.println("Renders a document at the specified URL and stores the document image");
System.err.println("to the specified file in PNG format.");
System.err.println();
System.err.println("See the CSSBoxSvg and CSSBoxPdf or WebVector projects if you want");
System.err.println("to produce vector graphics in SVG or PDF.");
System.exit(0);
}
try {
FileOutputStream os = new FileOutputStream(args[1]);
ImageRenderer r = new ImageRenderer();
r.renderURL(args[0], os);
os.close();
System.err.println("Done.");
} catch (Exception e) {
System.err.println("Error: " + e.getMessage());
}
}
}