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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
|
/*
* 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.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
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.util.MimeConstants;
import org.apache.xmlgraphics.util.QName;
import org.apache.xmlgraphics.xmp.Metadata;
import org.apache.fop.events.ResourceEventProducer;
import org.apache.fop.fo.extensions.ExtensionElementMapping;
import org.apache.fop.render.RenderingContext;
import org.apache.fop.render.intermediate.AbstractXMLWritingIFPainter;
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.traits.BorderProps;
import org.apache.fop.traits.RuleStyle;
import org.apache.fop.util.ColorUtil;
/**
* Abstract base class for SVG Painter implementations.
*/
public abstract class AbstractSVGPainter extends AbstractXMLWritingIFPainter
implements SVGConstants {
/** logging instance */
private static Log log = LogFactory.getLog(AbstractSVGPainter.class);
/** Holds the intermediate format state */
protected IFState state;
private static final int MODE_NORMAL = 0;
private static final int MODE_TEXT = 1;
private int mode = MODE_NORMAL;
/** {@inheritDoc} */
protected String getMainNamespace() {
return NAMESPACE;
}
/** {@inheritDoc} */
public void startDocumentHeader() throws IFException {
try {
startElement("defs");
} catch (SAXException e) {
throw new IFException("SAX error in startDocumentHeader()", e);
}
}
/** {@inheritDoc} */
public void endDocumentHeader() throws IFException {
try {
endElement("defs");
} catch (SAXException e) {
throw new IFException("SAX error in startDocumentHeader()", e);
}
}
/** {@inheritDoc} */
public void startPageContent() throws IFException {
this.state = IFState.create();
}
/** {@inheritDoc} */
public void endPageContent() throws IFException {
assert this.state.pop() == null;
}
/** {@inheritDoc} */
public void startViewport(AffineTransform transform, Dimension size, Rectangle clipRect)
throws IFException {
startViewport(toString(transform), size, clipRect);
}
/** {@inheritDoc} */
public void startViewport(AffineTransform[] transforms, Dimension size, Rectangle clipRect)
throws IFException {
startViewport(toString(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) {
atts.addAttribute("", "transform", "transform", CDATA, transform);
}
startElement("g", atts);
atts.clear();
atts.addAttribute("", "width", "width", CDATA, Integer.toString(size.width));
atts.addAttribute("", "height", "height", CDATA, Integer.toString(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(v[0]).append(',');
sb.append(v[1]).append(',');
sb.append(v[2]).append(',');
sb.append(v[3]).append(')');
atts.addAttribute("", "clip", "clip", CDATA, sb.toString());
}
atts.addAttribute("", "overflow", "overflow", CDATA, "hidden");
} else {
atts.addAttribute("", "overflow", "overflow", CDATA, "visible");
}
startElement("svg", atts);
} catch (SAXException e) {
throw new IFException("SAX error in startBox()", e);
}
}
/** {@inheritDoc} */
public void endViewport() throws IFException {
try {
establish(MODE_NORMAL);
endElement("svg");
endElement("g");
} catch (SAXException e) {
throw new IFException("SAX error in endBox()", e);
}
}
/** {@inheritDoc} */
public void startGroup(AffineTransform[] transforms) throws IFException {
startGroup(toString(transforms));
}
/** {@inheritDoc} */
public void startGroup(AffineTransform transform) throws IFException {
startGroup(toString(transform));
}
private void startGroup(String transform) throws IFException {
try {
AttributesImpl atts = new AttributesImpl();
if (transform != null && transform.length() > 0) {
atts.addAttribute("", "transform", "transform", CDATA, transform);
}
startElement("g", atts);
} catch (SAXException e) {
throw new IFException("SAX error in startGroup()", e);
}
}
/** {@inheritDoc} */
public void endGroup() throws IFException {
try {
establish(MODE_NORMAL);
endElement("g");
} catch (SAXException e) {
throw new IFException("SAX error in endGroup()", e);
}
}
private static final QName CONVERSION_MODE
= new QName(ExtensionElementMapping.URI, null, "conversion-mode");
/** {@inheritDoc} */
public void drawImage(String uri, Rectangle rect, Map foreignAttributes) throws IFException {
try {
establish(MODE_NORMAL);
ImageManager manager = getUserAgent().getFactory().getImageManager();
ImageInfo info = null;
try {
ImageSessionContext sessionContext = getUserAgent().getImageSessionContext();
info = manager.getImageInfo(uri, sessionContext);
String mime = info.getMimeType();
String conversionMode = (String)foreignAttributes.get(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();
addAttribute(atts, IFConstants.XLINK_HREF, uri);
atts.addAttribute("", "x", "x", CDATA, Integer.toString(rect.x));
atts.addAttribute("", "y", "y", CDATA, Integer.toString(rect.y));
atts.addAttribute("", "width", "width", CDATA, Integer.toString(rect.width));
atts.addAttribute("", "height", "height", CDATA, Integer.toString(rect.height));
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, Map foreignAttributes) 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 fillRect(Rectangle rect, Paint fill) throws IFException {
if (fill == null) {
return;
}
try {
establish(MODE_NORMAL);
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "x", "x", CDATA, Integer.toString(rect.x));
atts.addAttribute("", "y", "y", CDATA, Integer.toString(rect.y));
atts.addAttribute("", "width", "width", CDATA, Integer.toString(rect.width));
atts.addAttribute("", "height", "height", CDATA, Integer.toString(rect.height));
if (fill != null) {
atts.addAttribute("", "fill", "fill", CDATA, toString(fill));
}
/* disabled
if (stroke != null) {
atts.addAttribute("", "stroke", "stroke", CDATA, toString(stroke));
}*/
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();
atts.addAttribute("", "x1", "x1", CDATA, Integer.toString(start.x));
atts.addAttribute("", "y1", "y1", CDATA, Integer.toString(start.y));
atts.addAttribute("", "x2", "x2", CDATA, Integer.toString(end.x));
atts.addAttribute("", "y2", "y2", CDATA, Integer.toString(end.y));
atts.addAttribute("", "stroke-width", "stroke-width", CDATA, toString(color));
atts.addAttribute("", "fill", "fill", CDATA, toString(color));
//TODO Handle style parameter
element("line", atts);
} catch (SAXException e) {
throw new IFException("SAX error in drawLine()", e);
}
}
/** {@inheritDoc} */
public void drawText(int x, int y, int[] dx, int[] dy, String text) throws IFException {
try {
establish(MODE_TEXT);
AttributesImpl atts = new AttributesImpl();
atts.addAttribute("", "x", "x", CDATA, Integer.toString(x));
atts.addAttribute("", "y", "y", CDATA, Integer.toString(y));
if (dx != null) {
atts.addAttribute("", "dx", "dx", CDATA, toString(dx));
}
if (dy != null) {
atts.addAttribute("", "dy", "dy", CDATA, toString(dy));
}
startElement("text", atts);
char[] chars = text.toCharArray();
handler.characters(chars, 0, chars.length);
endElement("text");
} catch (SAXException e) {
throw new IFException("SAX error in setFont()", e);
}
}
/** {@inheritDoc} */
public void setFont(String family, String style, Integer weight, String variant, Integer size,
Color color) throws IFException {
if (family != null) {
state.setFontFamily(family);
}
if (style != null) {
state.setFontStyle(style);
}
if (weight != null) {
state.setFontWeight(weight.intValue());
}
if (variant != null) {
state.setFontVariant(variant);
}
if (size != null) {
state.setFontSize(size.intValue());
}
if (color != null) {
state.setTextColor(color);
}
}
private void leaveTextMode() throws SAXException {
assert this.mode == MODE_TEXT;
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();
atts.addAttribute("", "font-family", "font-family",
CDATA, state.getFontFamily());
atts.addAttribute("", "font-style", "font-style",
CDATA, state.getFontStyle());
atts.addAttribute("", "font-weight", "font-weight",
CDATA, Integer.toString(state.getFontWeight()));
atts.addAttribute("", "font-variant", "font-variant",
CDATA, state.getFontVariant());
atts.addAttribute("", "font-size", "font-size",
CDATA, Integer.toString(state.getFontSize()));
atts.addAttribute("", "fill", "fill",
CDATA, toString(state.getTextColor()));
startElement("g", atts);
state.resetFontChanged();
}
/** {@inheritDoc} */
public void handleExtensionObject(Object extension) throws IFException {
if (extension instanceof Metadata) {
Metadata meta = (Metadata)extension;
try {
establish(MODE_NORMAL);
startElement("metadata");
meta.toSAX(this.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);
}
}
}
|