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
|
/*
* $Id$
* Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
* For details on use and redistribution please refer to the
* LICENSE file included with these sources.
*/
package org.apache.fop.svg;
// FOP
import org.apache.fop.fo.*;
import org.apache.fop.fo.properties.*;
import org.apache.fop.layout.Area;
import org.apache.fop.layout.FontState;
import org.apache.fop.apps.FOPException;
import org.apache.fop.layout.inline.*;
import org.apache.fop.configuration.Configuration;
import org.apache.batik.dom.svg.*;
import org.apache.batik.dom.util.XMLSupport;
import org.w3c.dom.*;
import org.w3c.dom.svg.*;
import org.w3c.dom.svg.SVGLength;
import org.xml.sax.Attributes;
import org.apache.batik.bridge.*;
import org.apache.batik.swing.svg.*;
import org.apache.batik.swing.gvt.*;
import org.apache.batik.gvt.*;
import org.apache.batik.gvt.renderer.*;
import org.apache.batik.gvt.filter.*;
import org.apache.batik.gvt.event.*;
import org.apache.batik.bridge.UnitProcessor;
import org.apache.batik.css.value.ImmutableFloat;
import org.apache.batik.css.CSSOMReadOnlyValue;
import org.apache.batik.util.SVGConstants;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.css.CSSPrimitiveValue;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
/**
* class representing svg:svg pseudo flow object.
*/
public class SVGElement extends SVGObj {
FontState fs;
/**
* constructs an SVG object (called by Maker).
*
* @param parent the parent formatting object
* @param propertyList the explicit properties of this object
*/
public SVGElement(FONode parent) {
super(parent);
}
public void handleAttrs(Attributes attlist) throws FOPException {
super.handleAttrs(attlist);
init();
}
/**
* layout this formatting object.
*
* @param area the area to layout the object into
*
* @return the status of the layout
*/
public Status layout(final Area area) throws FOPException {
if (!(area instanceof ForeignObjectArea)) {
// this is an error
throw new FOPException("SVG not in fo:instream-foreign-object");
}
this.fs = area.getFontState();
// TODO - change so doesn't hold onto fo,area tree
Element svgRoot = element;
/* create an SVG area */
/* if width and height are zero, get the bounds of the content. */
FOPSVGContext dc = new FOPSVGContext();
dc.svgRoot = element;
ForeignObjectArea foa = (ForeignObjectArea)area;
dc.cwauto = foa.isContentWidthAuto();
dc.chauto = foa.isContentHeightAuto();
dc.cwidth = foa.getContentWidth();
dc.cheight = foa.getContentHeight();
((SVGOMDocument)doc).setSVGContext(dc);
try {
String baseDir = Configuration.getStringValue("baseDir");
if(baseDir != null) {
((SVGOMDocument)doc).setURLObject(new URL(baseDir));
}
} catch (Exception e) {
log.error("Could not set base URL for svg", e);
}
Element e = ((SVGDocument)doc).getRootElement();
//if(!e.hasAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns")) {
e.setAttributeNS(XMLSupport.XMLNS_NAMESPACE_URI, "xmlns", SVGDOMImplementation.SVG_NAMESPACE_URI);
//}
Point2D p2d = getSize(this.fs, svgRoot);
SVGArea svg = new SVGArea(fs, (float)p2d.getX(),
(float)p2d.getY());
svg.setSVGDocument(doc);
svg.start();
/* finish off the SVG area */
svg.end();
/* add the SVG area to the containing area */
foa.setObject(svg);
foa.setIntrinsicWidth(svg.getWidth());
foa.setIntrinsicHeight(svg.getHeight());
/* return status */
return new Status(Status.OK);
}
private void init() {
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
doc = impl.createDocument(svgNS, "svg", null);
element = doc.getDocumentElement();
buildTopLevel(doc, element);
}
public static Point2D getSize(FontState fs, Element svgRoot) {
String str;
UnitProcessor.Context ctx;
ctx = new PDFUnitContext(fs, svgRoot);
str = svgRoot.getAttributeNS(null, SVGConstants.SVG_WIDTH_ATTRIBUTE);
if (str.length() == 0) str = "100%";
float width = UnitProcessor.svgHorizontalLengthToUserSpace
(str, SVGConstants.SVG_WIDTH_ATTRIBUTE, ctx);
str = svgRoot.getAttributeNS(null, SVGConstants.SVG_HEIGHT_ATTRIBUTE);
if (str.length() == 0) str = "100%";
float height = UnitProcessor.svgVerticalLengthToUserSpace
(str, SVGConstants.SVG_HEIGHT_ATTRIBUTE, ctx);
return new Point2D.Float(width, height);
}
/**
* This class is the default context for a particular
* element. Informations not available on the element are get from
* the bridge context (such as the viewport or the pixel to
* millimeter factor.
*/
public static class PDFUnitContext implements UnitProcessor.Context {
/** The element. */
protected Element e;
protected FontState fs;
public PDFUnitContext(FontState fs, Element e) {
this.e = e;
this.fs = fs;
}
/**
* Returns the element.
*/
public Element getElement() {
return e;
}
/**
* Returns the context of the parent element of this context.
* Since this is always for the root SVG element there never
* should be one...
*/
public UnitProcessor.Context getParentElementContext() {
return null;
}
/**
* Returns the pixel to mm factor.
*/
public float getPixelToMM() {
return 0.264583333333333333333f;
// 72 dpi
}
/**
* Returns the font-size medium value in pt.
*/
public float getMediumFontSize() {
return 9f;
}
/**
* Returns the font-size value.
*/
public CSSPrimitiveValue getFontSize() {
return new CSSOMReadOnlyValue
(new ImmutableFloat(CSSPrimitiveValue.CSS_PT,
fs.getFontSize()));
}
/**
* Returns the x-height value.
*/
public float getXHeight() {
return 0.5f;
}
/**
* Returns the viewport width used to compute units.
*/
public float getViewportWidth() {
return 100;
}
/**
* Returns the viewport height used to compute units.
*/
public float getViewportHeight() {
return 100;
}
}
}
class FOPSVGContext extends DefaultSVGContext {
public boolean cwauto;
public boolean chauto;
public float cwidth;
public float cheight;
public Element svgRoot;
public float getPixelToMM() {
// 72 dpi
return 0.35277777777777777778f;
}
public float getViewportWidth(Element e) throws IllegalStateException {
if(e == svgRoot) {
if(!cwauto) {
return cwidth;
}
}
return super.getViewportWidth(e);
}
public float getViewportHeight(Element e) throws IllegalStateException {
if(e == svgRoot) {
if(!chauto) {
return cheight;
}
}
return super.getViewportHeight(e);
}
public List getDefaultFontFamilyValue() {
return FONT_FAMILY;
}
public final static List FONT_FAMILY;
static {
FONT_FAMILY = new ArrayList();
FONT_FAMILY.add("Helvetica");
FONT_FAMILY.add("Times");
FONT_FAMILY.add("Courier");
FONT_FAMILY.add("sans-serif");
FONT_FAMILY.add("serif");
}
}
|