blob: b4f3dd811dc326f4a6d2d506a31c08e0cbc19f36 (
plain)
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
|
/*
* $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.w3c.dom.*;
import org.w3c.dom.svg.*;
import org.w3c.dom.svg.SVGLength;
import org.w3c.dom.DOMImplementation;
import org.apache.batik.dom.svg.SVGDOMImplementation;
import java.io.File;
import java.net.URL;
import java.util.List;
import java.util.ArrayList;
/**
* class representing svg:svg pseudo flow object.
*/
public class SVGElement extends Svg {
/**
* inner class for making SVG objects.
*/
public static class Maker extends FObj.Maker {
/**
* make an SVG object.
*
* @param parent the parent formatting object
* @param propertyList the explicit properties of this object
*
* @return the SVG object
*/
public FObj make(FObj parent,
PropertyList propertyList) throws FOPException {
return new SVGElement(parent, propertyList);
}
}
/**
* returns the maker for this object.
*
* @return the maker for SVG objects
*/
public static FObj.Maker maker() {
return new SVGElement.Maker();
}
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(FObj parent, PropertyList propertyList) {
super(parent, propertyList);
}
/**
* 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");
}
if (this.marker == START) {
this.fs = area.getFontState();
this.marker = 0;
}
/* create an SVG area */
/* if width and height are zero, may want to get the bounds of the content. */
DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
Document doc = impl.createDocument(svgNS, "svg", null);
final Element svgRoot = doc.getDocumentElement();
try {
String baseDir = Configuration.getStringValue("baseDir");
((SVGOMDocument)doc).setURLObject(new URL(baseDir));
} catch (Exception e) {}
DefaultSVGContext dc = new DefaultSVGContext() {
public float getPixelToMM() {
// 72 dpi
return 0.35277777777777777778f;
}
public float getViewportWidth(Element e) throws IllegalStateException {
if(e == svgRoot) {
ForeignObjectArea foa = (ForeignObjectArea)area;
if(!foa.isContentWidthAuto()) {
return foa.getContentWidth();
}
}
return super.getViewportWidth(e);
}
public float getViewportHeight(Element e) throws IllegalStateException {
if(e == svgRoot) {
ForeignObjectArea foa = (ForeignObjectArea)area;
if(!foa.isContentHeightAuto()) {
return foa.getContentHeight();
}
}
return super.getViewportHeight(e);
}
public List getDefaultFontFamilyValue() {
return FONT_FAMILY;
}
};
((SVGOMDocument)doc).setSVGContext(dc);
buildTopLevel(doc, svgRoot);
float width =
((SVGSVGElement)svgRoot).getWidth().getBaseVal().getValue();
float height =
((SVGSVGElement)svgRoot).getHeight().getBaseVal().getValue();
SVGArea svg = new SVGArea(fs, width, height);
svg.setSVGDocument(doc);
svg.start();
/* finish off the SVG area */
svg.end();
/* add the SVG area to the containing area */
ForeignObjectArea foa = (ForeignObjectArea)area;
foa.setObject(svg);
foa.setIntrinsicWidth(svg.getWidth());
foa.setIntrinsicHeight(svg.getHeight());
/* return status */
return new Status(Status.OK);
}
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");
}
}
|