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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
/*
* 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.pdf;
import java.awt.Color;
import java.awt.geom.AffineTransform;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Map;
import org.w3c.dom.Document;
import org.apache.avalon.framework.configuration.Configuration;
import org.apache.batik.bridge.BridgeContext;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import org.apache.batik.dom.util.DOMUtilities;
import org.apache.batik.gvt.GraphicsNode;
import org.apache.batik.util.SVGConstants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.fonts.FontInfo;
import org.apache.fop.pdf.PDFDocument;
import org.apache.fop.pdf.PDFPage;
import org.apache.fop.pdf.PDFPaintingState;
import org.apache.fop.pdf.PDFResourceContext;
import org.apache.fop.pdf.PDFStream;
import org.apache.fop.render.AbstractGenericSVGHandler;
import org.apache.fop.render.Renderer;
import org.apache.fop.render.RendererContext;
import org.apache.fop.render.RendererContextConstants;
import org.apache.fop.svg.PDFAElementBridge;
import org.apache.fop.svg.PDFBridgeContext;
import org.apache.fop.svg.PDFGraphics2D;
import org.apache.fop.svg.SVGEventProducer;
import org.apache.fop.svg.SVGUserAgent;
/**
* PDF XML handler for SVG (uses Apache Batik).
* This handler handles XML for foreign objects when rendering to PDF.
* It renders SVG to the PDF document using the PDFGraphics2D.
* The properties from the PDF renderer are subject to change.
*/
public class PDFSVGHandler extends AbstractGenericSVGHandler
implements PDFRendererContextConstants {
/** logging instance */
private static Log log = LogFactory.getLog(PDFSVGHandler.class);
/**
* Get the pdf information from the render context.
*
* @param context the renderer context
* @return the pdf information retrieved from the context
*/
public static PDFInfo getPDFInfo(RendererContext context) {
PDFInfo pdfi = new PDFInfo();
pdfi.pdfDoc = (PDFDocument)context.getProperty(PDF_DOCUMENT);
pdfi.outputStream = (OutputStream)context.getProperty(OUTPUT_STREAM);
pdfi.pdfPaintingState = (PDFPaintingState)context.getProperty(PDF_PAINTING_STATE);
pdfi.pdfPage = (PDFPage)context.getProperty(PDF_PAGE);
pdfi.pdfContext = (PDFResourceContext)context.getProperty(PDF_CONTEXT);
pdfi.currentStream = (PDFStream)context.getProperty(PDF_STREAM);
pdfi.width = ((Integer)context.getProperty(WIDTH)).intValue();
pdfi.height = ((Integer)context.getProperty(HEIGHT)).intValue();
pdfi.fi = (FontInfo)context.getProperty(PDF_FONT_INFO);
pdfi.currentFontName = (String)context.getProperty(PDF_FONT_NAME);
pdfi.currentFontSize = ((Integer)context.getProperty(PDF_FONT_SIZE)).intValue();
pdfi.currentXPosition = ((Integer)context.getProperty(XPOS)).intValue();
pdfi.currentYPosition = ((Integer)context.getProperty(YPOS)).intValue();
pdfi.cfg = (Configuration)context.getProperty(HANDLER_CONFIGURATION);
Map foreign = (Map)context.getProperty(RendererContextConstants.FOREIGN_ATTRIBUTES);
if (foreign != null
&& BITMAP.equalsIgnoreCase((String)foreign.get(CONVERSION_MODE))) {
pdfi.paintAsBitmap = true;
}
return pdfi;
}
/**
* PDF information structure for drawing the XML document.
*/
public static class PDFInfo {
/** see PDF_DOCUMENT */
public PDFDocument pdfDoc;
/** see OUTPUT_STREAM */
public OutputStream outputStream;
/** see PDF_STATE */
public PDFPaintingState pdfPaintingState;
/** see PDF_PAGE */
public PDFPage pdfPage;
/** see PDF_CONTEXT */
public PDFResourceContext pdfContext;
/** see PDF_STREAM */
public PDFStream currentStream;
/** see PDF_WIDTH */
public int width;
/** see PDF_HEIGHT */
public int height;
/** see PDF_FONT_INFO */
public FontInfo fi;
/** see PDF_FONT_NAME */
public String currentFontName;
/** see PDF_FONT_SIZE */
public int currentFontSize;
/** see PDF_XPOS */
public int currentXPosition;
/** see PDF_YPOS */
public int currentYPosition;
/** see PDF_HANDLER_CONFIGURATION */
public Configuration cfg;
/** true if SVG should be rendered as a bitmap instead of natively */
public boolean paintAsBitmap;
}
/**
* {@inheritDoc}
*/
protected void renderSVGDocument(RendererContext context,
Document doc) {
PDFRenderer renderer = (PDFRenderer)context.getRenderer();
PDFInfo pdfInfo = getPDFInfo(context);
if (pdfInfo.paintAsBitmap) {
try {
super.renderSVGDocument(context, doc);
} catch (IOException ioe) {
SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
context.getUserAgent().getEventBroadcaster());
eventProducer.svgRenderingError(this, ioe, getDocumentURI(doc));
}
return;
}
int xOffset = pdfInfo.currentXPosition;
int yOffset = pdfInfo.currentYPosition;
FOUserAgent userAgent = context.getUserAgent();
final float deviceResolution = userAgent.getTargetResolution();
if (log.isDebugEnabled()) {
log.debug("Generating SVG at " + deviceResolution + "dpi.");
}
final float uaResolution = userAgent.getSourceResolution();
SVGUserAgent ua = new SVGUserAgent(userAgent, new AffineTransform());
//Scale for higher resolution on-the-fly images from Batik
double s = uaResolution / deviceResolution;
AffineTransform resolutionScaling = new AffineTransform();
resolutionScaling.scale(s, s);
//Controls whether text painted by Batik is generated using text or path operations
boolean strokeText = false;
Configuration cfg = pdfInfo.cfg;
if (cfg != null) {
strokeText = cfg.getChild("stroke-text", true).getValueAsBoolean(strokeText);
}
BridgeContext ctx = new PDFBridgeContext(ua,
(strokeText ? null : pdfInfo.fi),
userAgent.getFactory().getImageManager(),
userAgent.getImageSessionContext(),
new AffineTransform());
//Cloning SVG DOM as Batik attaches non-thread-safe facilities (like the CSS engine)
//to it.
Document clonedDoc = DOMUtilities.deepCloneDocument(doc, doc.getImplementation());
GraphicsNode root;
try {
GVTBuilder builder = new GVTBuilder();
root = builder.build(ctx, clonedDoc);
} catch (Exception e) {
SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
context.getUserAgent().getEventBroadcaster());
eventProducer.svgNotBuilt(this, e, getDocumentURI(doc));
return;
}
// get the 'width' and 'height' attributes of the SVG document
float w = (float)ctx.getDocumentSize().getWidth() * 1000f;
float h = (float)ctx.getDocumentSize().getHeight() * 1000f;
float sx = pdfInfo.width / w;
float sy = pdfInfo.height / h;
//Scaling and translation for the bounding box of the image
AffineTransform scaling = new AffineTransform(
sx, 0, 0, sy, xOffset / 1000f, yOffset / 1000f);
//Transformation matrix that establishes the local coordinate system for the SVG graphic
//in relation to the current coordinate system
AffineTransform imageTransform = new AffineTransform();
imageTransform.concatenate(scaling);
imageTransform.concatenate(resolutionScaling);
/*
* Clip to the svg area.
* Note: To have the svg overlay (under) a text area then use
* an fo:block-container
*/
pdfInfo.currentStream.add("%SVG setup\n");
renderer.saveGraphicsState();
renderer.setColor(Color.black, false, null);
renderer.setColor(Color.black, true, null);
if (!scaling.isIdentity()) {
pdfInfo.currentStream.add("%viewbox\n");
pdfInfo.currentStream.add(CTMHelper.toPDFString(scaling, false) + " cm\n");
}
//SVGSVGElement svg = ((SVGDocument)doc).getRootElement();
if (pdfInfo.pdfContext == null) {
pdfInfo.pdfContext = pdfInfo.pdfPage;
}
PDFGraphics2D graphics = new PDFGraphics2D(true, pdfInfo.fi,
pdfInfo.pdfDoc,
pdfInfo.pdfContext, pdfInfo.pdfPage.referencePDF(),
pdfInfo.currentFontName, pdfInfo.currentFontSize);
graphics.setGraphicContext(new org.apache.xmlgraphics.java2d.GraphicContext());
if (!resolutionScaling.isIdentity()) {
pdfInfo.currentStream.add("%resolution scaling for " + uaResolution
+ " -> " + deviceResolution + "\n");
pdfInfo.currentStream.add(
CTMHelper.toPDFString(resolutionScaling, false) + " cm\n");
graphics.scale(1 / s, 1 / s);
}
pdfInfo.currentStream.add("%SVG start\n");
//Save state and update coordinate system for the SVG image
pdfInfo.pdfPaintingState.save();
pdfInfo.pdfPaintingState.concatenate(imageTransform);
//Now that we have the complete transformation matrix for the image, we can update the
//transformation matrix for the AElementBridge.
PDFAElementBridge aBridge = (PDFAElementBridge)ctx.getBridge(
SVGDOMImplementation.SVG_NAMESPACE_URI, SVGConstants.SVG_A_TAG);
aBridge.getCurrentTransform().setTransform(pdfInfo.pdfPaintingState.getTransform());
graphics.setPaintingState(pdfInfo.pdfPaintingState);
graphics.setOutputStream(pdfInfo.outputStream);
try {
root.paint(graphics);
pdfInfo.currentStream.add(graphics.getString());
} catch (Exception e) {
SVGEventProducer eventProducer = SVGEventProducer.Provider.get(
context.getUserAgent().getEventBroadcaster());
eventProducer.svgRenderingError(this, e, getDocumentURI(doc));
}
pdfInfo.pdfPaintingState.restore();
renderer.restoreGraphicsState();
pdfInfo.currentStream.add("%SVG end\n");
}
/** {@inheritDoc} */
public boolean supportsRenderer(Renderer renderer) {
return (renderer instanceof PDFRenderer);
}
}
|