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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
|
/*
* 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.Color;
import java.awt.Dimension;
import java.awt.Paint;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Map;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.apache.xmlgraphics.image.loader.ImageException;
import org.apache.xmlgraphics.image.loader.ImageInfo;
import org.apache.xmlgraphics.image.loader.ImageManager;
import org.apache.xmlgraphics.image.loader.ImageSessionContext;
import org.apache.xmlgraphics.xmp.Metadata;
import org.apache.fop.ResourceEventProducer;
import org.apache.fop.apps.MimeConstants;
import org.apache.fop.render.ImageHandlerUtil;
import org.apache.fop.render.RenderingContext;
import org.apache.fop.render.intermediate.AbstractIFPainter;
import org.apache.fop.render.intermediate.IFConstants;
import org.apache.fop.render.intermediate.IFException;
import org.apache.fop.render.intermediate.IFState;
import org.apache.fop.render.intermediate.IFUtil;
import org.apache.fop.traits.BorderProps;
import org.apache.fop.traits.RuleStyle;
import org.apache.fop.util.ColorUtil;
import org.apache.fop.util.GenerationHelperContentHandler;
import org.apache.fop.util.XMLConstants;
import org.apache.fop.util.XMLUtil;
/**
* IFPainter implementation that writes SVG.
*/
public class SVGPainter extends AbstractIFPainter<AbstractSVGDocumentHandler>
implements SVGConstants {
/** The SAX content handler that receives the generated XML events. */
private GenerationHelperContentHandler handler;
private static final int MODE_NORMAL = 0;
private static final int MODE_TEXT = 1;
private int mode = MODE_NORMAL;
/**
* Main constructor.
* @param parent the parent document handler
* @param contentHandler the target SAX content handler
*/
public SVGPainter(AbstractSVGDocumentHandler parent,
GenerationHelperContentHandler contentHandler) {
super(parent);
this.handler = contentHandler;
this.state = IFState.create();
}
/** {@inheritDoc} */
public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
throws IFException {
startViewport(SVGUtil.formatAffineTransformMptToPt(transform), size, clipRect);
}
/** {@inheritDoc} */
public void startViewport(AffineTransform[] transforms, Dimension size, Rectangle clipRect)
throws IFException {
startViewport(SVGUtil.formatAffineTransformsMptToPt(transforms), size, clipRect);
}
private void startViewport(String transform, Dimension size, Rectangle clipRect)
throws IFException {
try {
establish(MODE_NORMAL);
AttributesImpl atts = new AttributesImpl();
if (transform != null && transform.length() > 0) {
XMLUtil.addAttribute(atts, "transform", transform);
}
handler.startElement("g", atts);
atts.clear();
XMLUtil.addAttribute(atts, "width", SVGUtil.formatMptToPt(size.width));
XMLUtil.addAttribute(atts, "height", SVGUtil.formatMptToPt(size.height));
if (clipRect != null) {
int[] v = new int[] {
clipRect.y,
-clipRect.x + size.width - clipRect.width,
-clipRect.y + size.height - clipRect.height,
clipRect.x};
int sum = 0;
for (int i = 0; i < 4; i++) {
sum += Math.abs(v[i]);
}
if (sum != 0) {
StringBuffer sb = new StringBuffer("rect(");
sb.append(SVGUtil.formatMptToPt(v[0])).append(',');
sb.append(SVGUtil.formatMptToPt(v[1])).append(',');
sb.append(SVGUtil.formatMptToPt(v[2])).append(',');
sb.append(SVGUtil.formatMptToPt(v[3])).append(')');
XMLUtil.addAttribute(atts, "clip", sb.toString());
}
XMLUtil.addAttribute(atts, "overflow", "hidden");
} else {
XMLUtil.addAttribute(atts, "overflow", "visible");
}
handler.startElement("svg", atts);
} catch (SAXException e) {
throw new IFException("SAX error in startBox()", e);
}
}
/** {@inheritDoc} */
public void endViewport() throws IFException {
try {
establish(MODE_NORMAL);
handler.endElement("svg");
handler.endElement("g");
} catch (SAXException e) {
throw new IFException("SAX error in endBox()", e);
}
}
/** {@inheritDoc} */
public void startGroup(AffineTransform[] transforms, String layer) throws IFException {
startGroup(SVGUtil.formatAffineTransformsMptToPt(transforms), layer);
}
/** {@inheritDoc} */
public void startGroup(AffineTransform transform, String layer) throws IFException {
startGroup(SVGUtil.formatAffineTransformMptToPt(transform), layer);
}
private void startGroup(String transform, String layer) throws IFException {
try {
AttributesImpl atts = new AttributesImpl();
if (transform != null && transform.length() > 0) {
XMLUtil.addAttribute(atts, "transform", transform);
}
handler.startElement("g", atts);
} catch (SAXException e) {
throw new IFException("SAX error in startGroup()", e);
}
}
/** {@inheritDoc} */
public void endGroup() throws IFException {
try {
establish(MODE_NORMAL);
handler.endElement("g");
} catch (SAXException e) {
throw new IFException("SAX error in endGroup()", e);
}
}
/** {@inheritDoc} */
public void drawImage(String uri, Rectangle rect) throws IFException {
try {
establish(MODE_NORMAL);
ImageManager manager = getUserAgent().getImageManager();
ImageInfo info = null;
try {
ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
info = manager.getImageInfo(uri, sessionContext);
String mime = info.getMimeType();
Map foreignAttributes = getContext().getForeignAttributes();
String conversionMode = (String)foreignAttributes.get(
ImageHandlerUtil.CONVERSION_MODE);
if ("reference".equals(conversionMode)
&& (MimeConstants.MIME_GIF.equals(mime)
|| MimeConstants.MIME_JPEG.equals(mime)
|| MimeConstants.MIME_PNG.equals(mime)
|| MimeConstants.MIME_SVG.equals(mime))) {
//Just reference the image
//TODO Some additional URI rewriting might be necessary
AttributesImpl atts = new AttributesImpl();
XMLUtil.addAttribute(atts, IFConstants.XLINK_HREF, uri);
XMLUtil.addAttribute(atts, "x", SVGUtil.formatMptToPt(rect.x));
XMLUtil.addAttribute(atts, "y", SVGUtil.formatMptToPt(rect.y));
XMLUtil.addAttribute(atts, "width", SVGUtil.formatMptToPt(rect.width));
XMLUtil.addAttribute(atts, "height", SVGUtil.formatMptToPt(rect.height));
handler.element("image", atts);
} else {
drawImageUsingImageHandler(info, rect);
}
} catch (ImageException ie) {
ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
getUserAgent().getEventBroadcaster());
eventProducer.imageError(this, (info != null ? info.toString() : uri), ie, null);
} catch (FileNotFoundException fe) {
ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
getUserAgent().getEventBroadcaster());
eventProducer.imageNotFound(this, (info != null ? info.toString() : uri), fe, null);
} catch (IOException ioe) {
ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get(
getUserAgent().getEventBroadcaster());
eventProducer.imageIOError(this, (info != null ? info.toString() : uri), ioe, null);
}
} catch (SAXException e) {
throw new IFException("SAX error in drawImage()", e);
}
}
/** {@inheritDoc} */
public void drawImage(Document doc, Rectangle rect) throws IFException {
try {
establish(MODE_NORMAL);
drawImageUsingDocument(doc, rect);
} catch (SAXException e) {
throw new IFException("SAX error in drawImage()", e);
}
}
/** {@inheritDoc} */
protected RenderingContext createRenderingContext() {
SVGRenderingContext svgContext = new SVGRenderingContext(
getUserAgent(), handler);
return svgContext;
}
private static String toString(Paint paint) {
//TODO Paint serialization: Fine-tune and extend!
if (paint instanceof Color) {
return ColorUtil.colorToString((Color)paint);
} else {
throw new UnsupportedOperationException("Paint not supported: " + paint);
}
}
/** {@inheritDoc} */
public void clipRect(Rectangle rect) throws IFException {
//TODO Implement me!!!
}
/** {@inheritDoc} */
public void clipBackground(Rectangle rect, BorderProps bpsBefore, BorderProps bpsAfter,
BorderProps bpsStart, BorderProps bpsEnd) throws IFException {
//TODO Implement me!!!
}
/** {@inheritDoc} */
public void fillRect(Rectangle rect, Paint fill) throws IFException {
if (fill == null) {
return;
}
try {
establish(MODE_NORMAL);
AttributesImpl atts = new AttributesImpl();
XMLUtil.addAttribute(atts, "x", SVGUtil.formatMptToPt(rect.x));
XMLUtil.addAttribute(atts, "y", SVGUtil.formatMptToPt(rect.y));
XMLUtil.addAttribute(atts, "width", SVGUtil.formatMptToPt(rect.width));
XMLUtil.addAttribute(atts, "height", SVGUtil.formatMptToPt(rect.height));
XMLUtil.addAttribute(atts, "fill", toString(fill));
/* disabled
if (stroke != null) {
XMLUtil.addAttribute(atts, "stroke", toString(stroke));
}*/
handler.element("rect", atts);
} catch (SAXException e) {
throw new IFException("SAX error in fillRect()", e);
}
}
/** {@inheritDoc} */
public void drawBorderRect(Rectangle rect, BorderProps before, BorderProps after,
BorderProps start, BorderProps end) throws IFException {
// TODO Auto-generated method stub
}
/** {@inheritDoc} */
public void drawLine(Point start, Point end, int width, Color color, RuleStyle style)
throws IFException {
try {
establish(MODE_NORMAL);
AttributesImpl atts = new AttributesImpl();
XMLUtil.addAttribute(atts, "x1", SVGUtil.formatMptToPt(start.x));
XMLUtil.addAttribute(atts, "y1", SVGUtil.formatMptToPt(start.y));
XMLUtil.addAttribute(atts, "x2", SVGUtil.formatMptToPt(end.x));
XMLUtil.addAttribute(atts, "y2", SVGUtil.formatMptToPt(end.y));
XMLUtil.addAttribute(atts, "stroke-width", toString(color));
XMLUtil.addAttribute(atts, "fill", toString(color));
//TODO Handle style parameter
handler.element("line", atts);
} catch (SAXException e) {
throw new IFException("SAX error in drawLine()", e);
}
}
/** {@inheritDoc} */
public void drawText(int x, int y, int letterSpacing, int wordSpacing, int[][] dp,
String text) throws IFException {
try {
establish(MODE_TEXT);
AttributesImpl atts = new AttributesImpl();
XMLUtil.addAttribute(atts, XMLConstants.XML_SPACE, "preserve");
XMLUtil.addAttribute(atts, "x", SVGUtil.formatMptToPt(x));
XMLUtil.addAttribute(atts, "y", SVGUtil.formatMptToPt(y));
if (letterSpacing != 0) {
XMLUtil.addAttribute(atts, "letter-spacing", SVGUtil.formatMptToPt(letterSpacing));
}
if (wordSpacing != 0) {
XMLUtil.addAttribute(atts, "word-spacing", SVGUtil.formatMptToPt(wordSpacing));
}
if (dp != null) {
int[] dx = IFUtil.convertDPToDX(dp);
XMLUtil.addAttribute(atts, "dx", SVGUtil.formatMptArrayToPt(dx));
}
handler.startElement("text", atts);
char[] chars = text.toCharArray();
handler.characters(chars, 0, chars.length);
handler.endElement("text");
} catch (SAXException e) {
throw new IFException("SAX error in setFont()", e);
}
}
private void leaveTextMode() throws SAXException {
assert this.mode == MODE_TEXT;
handler.endElement("g");
this.mode = MODE_NORMAL;
}
private void establish(int newMode) throws SAXException {
switch (newMode) {
case MODE_TEXT:
enterTextMode();
break;
default:
if (this.mode == MODE_TEXT) {
leaveTextMode();
}
}
}
private void enterTextMode() throws SAXException {
if (state.isFontChanged() && this.mode == MODE_TEXT) {
leaveTextMode();
}
if (this.mode != MODE_TEXT) {
startTextGroup();
this.mode = MODE_TEXT;
}
}
private void startTextGroup() throws SAXException {
AttributesImpl atts = new AttributesImpl();
XMLUtil.addAttribute(atts, "font-family", "'" + state.getFontFamily() + "'");
XMLUtil.addAttribute(atts, "font-style", state.getFontStyle());
XMLUtil.addAttribute(atts, "font-weight", Integer.toString(state.getFontWeight()));
XMLUtil.addAttribute(atts, "font-variant", state.getFontVariant());
XMLUtil.addAttribute(atts, "font-size", SVGUtil.formatMptToPt(state.getFontSize()));
XMLUtil.addAttribute(atts, "fill", toString(state.getTextColor()));
handler.startElement("g", atts);
state.resetFontChanged();
}
/**
* @param extension an extension object
* @throws IFException if not caught
*/
public void handleExtensionObject(Object extension) throws IFException {
if (extension instanceof Metadata) {
Metadata meta = (Metadata)extension;
try {
establish(MODE_NORMAL);
handler.startElement("metadata");
meta.toSAX(this.handler);
handler.endElement("metadata");
} catch (SAXException e) {
throw new IFException("SAX error while handling extension object", e);
}
} else {
throw new UnsupportedOperationException(
"Don't know how to handle extension object: " + extension);
}
}
/** {@inheritDoc} */
public void fillBackground(Rectangle rect, Paint fill, BorderProps bpsBefore,
BorderProps bpsAfter, BorderProps bpsStart, BorderProps bpsEnd) throws IFException {
// Not supported in SVG
}
}
|