01: /*
02:
03: Licensed to the Apache Software Foundation (ASF) under one or more
04: contributor license agreements. See the NOTICE file distributed with
05: this work for additional information regarding copyright ownership.
06: The ASF licenses this file to You under the Apache License, Version 2.0
07: (the "License"); you may not use this file except in compliance with
08: the License. You may obtain a copy of the License at
09:
10: http://www.apache.org/licenses/LICENSE-2.0
11:
12: Unless required by applicable law or agreed to in writing, software
13: distributed under the License is distributed on an "AS IS" BASIS,
14: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15: See the License for the specific language governing permissions and
16: limitations under the License.
17:
18: */
19: package org.apache.batik.svggen;
20:
21: import java.awt.Polygon;
22: import java.awt.Shape;
23: import java.awt.geom.Arc2D;
24: import java.awt.geom.Ellipse2D;
25: import java.awt.geom.Line2D;
26: import java.awt.geom.Rectangle2D;
27: import java.awt.geom.RoundRectangle2D;
28:
29: import org.w3c.dom.Element;
30:
31: /**
32: * Utility class that converts a Shape object into the corresponding
33: * SVG element. Note that this class analyzes the input Shape class
34: * to generate the most appropriate corresponding SVG element:
35: * + Polygon is mapped to polygon
36: * + Rectangle2D and RoundRectangle2D are mapped to rect
37: * + Ellipse2D is mapped to circle or ellipse
38: * + Line2D is mapped to line
39: * + Arc2D, CubicCurve2D, Area, GeneralPath and QuadCurve2D are mapped to
40: * path.
41: * + Any custom Shape implementation is mapped to path as well.
42: *
43: * @author <a href="mailto:vincent.hardy@eng.sun.com">Vincent Hardy</a>
44: * @version $Id: SVGShape.java 475477 2006-11-15 22:44:28Z cam $
45: */
46: public class SVGShape extends SVGGraphicObjectConverter {
47: /*
48: * Subconverts, for each type of Shape class
49: */
50: private SVGArc svgArc;
51: private SVGEllipse svgEllipse;
52: private SVGLine svgLine;
53: private SVGPath svgPath;
54: private SVGPolygon svgPolygon;
55: private SVGRectangle svgRectangle;
56:
57: /**
58: * @param generatorContext used to build Elements
59: */
60: public SVGShape(SVGGeneratorContext generatorContext) {
61: super (generatorContext);
62: svgArc = new SVGArc(generatorContext);
63: svgEllipse = new SVGEllipse(generatorContext);
64: svgLine = new SVGLine(generatorContext);
65: svgPath = new SVGPath(generatorContext);
66: svgPolygon = new SVGPolygon(generatorContext);
67: svgRectangle = new SVGRectangle(generatorContext);
68: }
69:
70: /**
71: * @param shape Shape object to be converted
72: */
73: public Element toSVG(Shape shape) {
74: if (shape instanceof Polygon)
75: return svgPolygon.toSVG((Polygon) shape);
76: else if (shape instanceof Rectangle2D)
77: return svgRectangle.toSVG((Rectangle2D) shape);
78: else if (shape instanceof RoundRectangle2D)
79: return svgRectangle.toSVG((RoundRectangle2D) shape);
80: else if (shape instanceof Ellipse2D)
81: return svgEllipse.toSVG((Ellipse2D) shape);
82: else if (shape instanceof Line2D)
83: return svgLine.toSVG((Line2D) shape);
84: else if (shape instanceof Arc2D)
85: return svgArc.toSVG((Arc2D) shape);
86: else
87: return svgPath.toSVG(shape);
88: }
89: }
|