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
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.render.svg;
import java.awt.Dimension;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.batik.dom.GenericDOMImplementation;
import org.apache.batik.svggen.SVGGeneratorContext;
import org.apache.batik.svggen.SVGGraphics2D;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.area.PageViewport;
import org.apache.fop.render.bitmap.MultiFileRenderingUtil;
import org.apache.fop.render.java2d.Java2DGraphicsState;
import org.apache.fop.render.java2d.Java2DRenderer;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.Document;
/**
* <p>
* This renderer generates SVG (Scalable Vector Graphics) format.
* <p>
* This class actually does not render itself, instead it extends
* <code>org.apache.fop.render.java2D.Java2DRenderer</code> and uses
* Apache Batik's SVGGraphics2D for SVG generation.
*/
public class SVGRenderer extends Java2DRenderer {
/** logging instance */
private static Log log = LogFactory.getLog(SVGRenderer.class);
/** The MIME type for the SVG format */
public static final String MIME_TYPE = MimeConstants.MIME_SVG;
private static final String SVG_FILE_EXTENSION = "svg";
private OutputStream firstOutputStream;
private Document document;
private SVGGraphics2D svgGenerator;
/** Helper class for generating multiple files */
private MultiFileRenderingUtil multiFileUtil;
/** @see org.apache.fop.render.AbstractRenderer */
public String getMimeType() {
return MIME_TYPE;
}
/** Creates TIFF renderer. */
public SVGRenderer() {
}
/**
* @see org.apache.fop.render.AbstractRenderer#configure(
* org.apache.avalon.framework.configuration.Configuration)
*/
public void configure(Configuration cfg) throws ConfigurationException {
}
/** @see org.apache.fop.render.Renderer#startRenderer(java.io.OutputStream) */
public void startRenderer(OutputStream outputStream) throws IOException {
this.firstOutputStream = outputStream;
this.multiFileUtil = new MultiFileRenderingUtil(SVG_FILE_EXTENSION,
getUserAgent().getOutputFile());
super.startRenderer(this.firstOutputStream);
}
/**
* @see org.apache.fop.render.java2d.Java2DRenderer#renderPage(org.apache.fop.area.PageViewport)
*/
public void renderPage(PageViewport pageViewport) throws IOException {
log.debug("Rendering page: " + pageViewport.getPageNumberString());
// Get a DOMImplementation
DOMImplementation domImpl = GenericDOMImplementation.getDOMImplementation();
// Create an instance of org.w3c.dom.Document
this.document = domImpl.createDocument(null, "svg", null);
// Create an SVGGeneratorContext to customize SVG generation
SVGGeneratorContext ctx = SVGGeneratorContext.createDefault(this.document);
ctx.setComment("Generated by " + userAgent.getProducer() + " with Batik SVG Generator");
ctx.setEmbeddedFontsOn(true);
// Create an instance of the SVG Generator
this.svgGenerator = new SVGGraphics2D(ctx, true);
Rectangle2D viewArea = pageViewport.getViewArea();
Dimension dim = new Dimension();
dim.setSize(viewArea.getWidth() / 1000, viewArea.getHeight() / 1000);
this.svgGenerator.setSVGCanvasSize(dim);
AffineTransform at = this.svgGenerator.getTransform();
this.state = new Java2DGraphicsState(this.svgGenerator, this.fontInfo, at);
try {
//super.renderPage(pageViewport);
renderPageAreas(pageViewport.getPage());
} finally {
this.state = null;
}
writeSVGFile(pageViewport.getPageIndex());
this.svgGenerator = null;
this.document = null;
}
/** @see org.apache.fop.render.Renderer#stopRenderer() */
public void stopRenderer() throws IOException {
super.stopRenderer();
// Cleaning
clearViewportList();
log.debug("SVG generation complete.");
}
private void writeSVGFile(int pageNumber) throws IOException {
log.debug("Writing out SVG file...");
// Finally, stream out SVG to the standard output using UTF-8
// character to byte encoding
boolean useCSS = true; // we want to use CSS style attribute
OutputStream out = getCurrentOutputStream(pageNumber);
if (out == null) {
log.warn("No filename information available."
+ " Stopping early after the first page.");
return;
}
try {
Writer writer = new java.io.OutputStreamWriter(out, "UTF-8");
this.svgGenerator.stream(writer, useCSS);
} finally {
if (out != this.firstOutputStream) {
IOUtils.closeQuietly(out);
} else {
out.flush();
}
}
}
/**
* Returns the OutputStream corresponding to this page
* @param 0-based pageNumber
* @return the corresponding OutputStream
* @throws IOException In case of an I/O error
*/
protected OutputStream getCurrentOutputStream(int pageNumber) throws IOException {
if (pageNumber == 0) {
return firstOutputStream;
} else {
return multiFileUtil.createOutputStream(pageNumber);
}
}
}
|