001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.extension.svg;
020:
021: import java.awt.geom.GeneralPath;
022:
023: import org.apache.batik.bridge.Bridge;
024: import org.apache.batik.bridge.BridgeContext;
025: import org.apache.batik.bridge.BridgeException;
026: import org.apache.batik.bridge.SVGDecoratedShapeElementBridge;
027: import org.apache.batik.bridge.SVGUtilities;
028: import org.apache.batik.bridge.UnitProcessor;
029: import org.apache.batik.gvt.ShapeNode;
030: import org.w3c.dom.Element;
031:
032: /**
033: * Bridge class for a regular polygon element.
034: *
035: * @author <a href="mailto:thomas.deweese@kodak.com">Thomas Deweese</a>
036: * @version $Id: BatikRegularPolygonElementBridge.java 501922 2007-01-31 17:47:47Z dvholten $
037: */
038: public class BatikRegularPolygonElementBridge extends
039: SVGDecoratedShapeElementBridge implements BatikExtConstants {
040:
041: /**
042: * Constructs a new bridge for the <rect> element.
043: */
044: public BatikRegularPolygonElementBridge() { /* nothing */
045: }
046:
047: /**
048: * Returns the SVG namespace URI.
049: */
050: public String getNamespaceURI() {
051: return BATIK_EXT_NAMESPACE_URI;
052: }
053:
054: /**
055: * Returns 'rect'.
056: */
057: public String getLocalName() {
058: return BATIK_EXT_REGULAR_POLYGON_TAG;
059: }
060:
061: /**
062: * Returns a new instance of this bridge.
063: */
064: public Bridge getInstance() {
065: return new BatikRegularPolygonElementBridge();
066: }
067:
068: /**
069: * Constructs a regular polygone according to the specified parameters.
070: *
071: * @param ctx the bridge context to use
072: * @param e the element that describes a rect element
073: * @param shapeNode the shape node to initialize
074: */
075: protected void buildShape(BridgeContext ctx, Element e,
076: ShapeNode shapeNode) {
077:
078: UnitProcessor.Context uctx = UnitProcessor
079: .createContext(ctx, e);
080: String s;
081:
082: // 'cx' attribute - default is 0
083: s = e.getAttributeNS(null, SVG_CX_ATTRIBUTE);
084: float cx = 0;
085: if (s.length() != 0) {
086: cx = UnitProcessor.svgHorizontalCoordinateToUserSpace(s,
087: SVG_CX_ATTRIBUTE, uctx);
088: }
089:
090: // 'cy' attribute - default is 0
091: s = e.getAttributeNS(null, SVG_CY_ATTRIBUTE);
092: float cy = 0;
093: if (s.length() != 0) {
094: cy = UnitProcessor.svgVerticalCoordinateToUserSpace(s,
095: SVG_CY_ATTRIBUTE, uctx);
096: }
097:
098: // 'r' attribute - required
099: s = e.getAttributeNS(null, SVG_R_ATTRIBUTE);
100: float r;
101: if (s.length() != 0) {
102: r = UnitProcessor.svgOtherLengthToUserSpace(s,
103: SVG_R_ATTRIBUTE, uctx);
104: } else {
105: throw new BridgeException(ctx, e, ERR_ATTRIBUTE_MISSING,
106: new Object[] { SVG_R_ATTRIBUTE, s });
107: }
108:
109: // 'sides' attribute - default is 3
110: int sides = convertSides(e, BATIK_EXT_SIDES_ATTRIBUTE, 3, ctx);
111:
112: GeneralPath gp = new GeneralPath();
113: for (int i = 0; i < sides; i++) {
114: double angle = (i + 0.5) * (2 * Math.PI / sides)
115: - (Math.PI / 2);
116: double x = cx + r * Math.cos(angle);
117: double y = cy - r * Math.sin(angle);
118: if (i == 0)
119: gp.moveTo((float) x, (float) y);
120: else
121: gp.lineTo((float) x, (float) y);
122: }
123: gp.closePath();
124:
125: shapeNode.setShape(gp);
126: }
127:
128: /**
129: * Stolen from AbstractSVGFilterPrimitiveElementBridge.
130: * Converts on the specified filter primitive element, the specified
131: * attribute that represents an integer and with the specified
132: * default value.
133: *
134: * @param filterElement the filter primitive element
135: * @param attrName the name of the attribute
136: * @param defaultValue the default value of the attribute
137: * @param ctx the BridgeContext to use for error information
138: */
139: protected static int convertSides(Element filterElement,
140: String attrName, int defaultValue, BridgeContext ctx) {
141: String s = filterElement.getAttributeNS(null, attrName);
142: if (s.length() == 0) {
143: return defaultValue;
144: } else {
145: int ret = 0;
146: try {
147: ret = SVGUtilities.convertSVGInteger(s);
148: } catch (NumberFormatException nfEx) {
149: throw new BridgeException(ctx, filterElement, nfEx,
150: ERR_ATTRIBUTE_VALUE_MALFORMED, new Object[] {
151: attrName, s });
152: }
153:
154: if (ret < 3)
155: throw new BridgeException(ctx, filterElement,
156: ERR_ATTRIBUTE_VALUE_MALFORMED, new Object[] {
157: attrName, s });
158: return ret;
159: }
160: }
161: }
|