| 1 | /*
|
|---|
| 2 | * BoundedElement.java
|
|---|
| 3 | *
|
|---|
| 4 | *
|
|---|
| 5 | * The Salamander Project - 2D and 3D graphics libraries in Java
|
|---|
| 6 | * Copyright (C) 2004 Mark McKay
|
|---|
| 7 | *
|
|---|
| 8 | * This library is free software; you can redistribute it and/or
|
|---|
| 9 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 10 | * License as published by the Free Software Foundation; either
|
|---|
| 11 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 12 | *
|
|---|
| 13 | * This library is distributed in the hope that it will be useful,
|
|---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 16 | * Lesser General Public License for more details.
|
|---|
| 17 | *
|
|---|
| 18 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 19 | * License along with this library; if not, write to the Free Software
|
|---|
| 20 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|---|
| 21 | *
|
|---|
| 22 | * Mark McKay can be contacted at mark@kitfox.com. Salamander and other
|
|---|
| 23 | * projects can be found at http://www.kitfox.com
|
|---|
| 24 | *
|
|---|
| 25 | * Created on January 26, 2004, 9:00 AM
|
|---|
| 26 | */
|
|---|
| 27 |
|
|---|
| 28 | package com.kitfox.svg;
|
|---|
| 29 |
|
|---|
| 30 | import com.kitfox.svg.xml.StyleAttribute;
|
|---|
| 31 | import java.awt.Graphics2D;
|
|---|
| 32 | import java.awt.Shape;
|
|---|
| 33 | import java.awt.geom.AffineTransform;
|
|---|
| 34 | import java.awt.geom.Area;
|
|---|
| 35 | import java.awt.geom.Point2D;
|
|---|
| 36 | import java.awt.geom.Rectangle2D;
|
|---|
| 37 | import java.net.URI;
|
|---|
| 38 | import java.util.List;
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 | /**
|
|---|
| 43 | * Maintains bounding box for this element
|
|---|
| 44 | *
|
|---|
| 45 | * @author Mark McKay
|
|---|
| 46 | * @author <a href="mailto:mark@kitfox.com">Mark McKay</a>
|
|---|
| 47 | */
|
|---|
| 48 | abstract public class RenderableElement extends TransformableElement
|
|---|
| 49 | {
|
|---|
| 50 |
|
|---|
| 51 | AffineTransform cachedXform = null;
|
|---|
| 52 | Shape cachedClip = null;
|
|---|
| 53 |
|
|---|
| 54 | public static final int VECTOR_EFFECT_NONE = 0;
|
|---|
| 55 | public static final int VECTOR_EFFECT_NON_SCALING_STROKE = 1;
|
|---|
| 56 | int vectorEffect;
|
|---|
| 57 |
|
|---|
| 58 | /** Creates a new instance of BoundedElement */
|
|---|
| 59 | public RenderableElement() {
|
|---|
| 60 | }
|
|---|
| 61 |
|
|---|
| 62 | public RenderableElement(String id, SVGElement parent)
|
|---|
| 63 | {
|
|---|
| 64 | super(id, parent);
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | protected void build() throws SVGException
|
|---|
| 68 | {
|
|---|
| 69 | super.build();
|
|---|
| 70 |
|
|---|
| 71 | StyleAttribute sty = new StyleAttribute();
|
|---|
| 72 |
|
|---|
| 73 | if (getPres(sty.setName("vector-effect")))
|
|---|
| 74 | {
|
|---|
| 75 | if ("non-scaling-stroke".equals(sty.getStringValue()))
|
|---|
| 76 | {
|
|---|
| 77 | vectorEffect = VECTOR_EFFECT_NON_SCALING_STROKE;
|
|---|
| 78 | }
|
|---|
| 79 | else
|
|---|
| 80 | {
|
|---|
| 81 | vectorEffect = VECTOR_EFFECT_NONE;
|
|---|
| 82 | }
|
|---|
| 83 | }
|
|---|
| 84 | else
|
|---|
| 85 | {
|
|---|
| 86 | vectorEffect = VECTOR_EFFECT_NONE;
|
|---|
| 87 | }
|
|---|
| 88 | }
|
|---|
| 89 |
|
|---|
| 90 | abstract public void render(Graphics2D g) throws SVGException;
|
|---|
| 91 |
|
|---|
| 92 | abstract void pick(Point2D point, boolean boundingBox, List retVec) throws SVGException;
|
|---|
| 93 |
|
|---|
| 94 | abstract void pick(Rectangle2D pickArea, AffineTransform ltw, boolean boundingBox, List retVec) throws SVGException;
|
|---|
| 95 |
|
|---|
| 96 | abstract public Rectangle2D getBoundingBox() throws SVGException;
|
|---|
| 97 | /*
|
|---|
| 98 | public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent)
|
|---|
| 99 | {
|
|---|
| 100 | super.loaderStartElement(helper, attrs, parent);
|
|---|
| 101 | }
|
|---|
| 102 | */
|
|---|
| 103 | /**
|
|---|
| 104 | * Pushes transform stack, transforms to local coordinates and sets up
|
|---|
| 105 | * clipping mask.
|
|---|
| 106 | */
|
|---|
| 107 | protected void beginLayer(Graphics2D g) throws SVGException
|
|---|
| 108 | {
|
|---|
| 109 | if (xform != null)
|
|---|
| 110 | {
|
|---|
| 111 | cachedXform = g.getTransform();
|
|---|
| 112 | g.transform(xform);
|
|---|
| 113 | }
|
|---|
| 114 |
|
|---|
| 115 | StyleAttribute styleAttrib = new StyleAttribute();
|
|---|
| 116 |
|
|---|
| 117 | //Get clipping path
|
|---|
| 118 | // StyleAttribute styleAttrib = getStyle("clip-path", false);
|
|---|
| 119 | Shape clipPath = null;
|
|---|
| 120 | int clipPathUnits = ClipPath.CP_USER_SPACE_ON_USE;
|
|---|
| 121 | if (getStyle(styleAttrib.setName("clip-path")))
|
|---|
| 122 | {
|
|---|
| 123 | URI uri = styleAttrib.getURIValue(getXMLBase());
|
|---|
| 124 | if (uri != null)
|
|---|
| 125 | {
|
|---|
| 126 | ClipPath ele = (ClipPath)diagram.getUniverse().getElement(uri);
|
|---|
| 127 | clipPath = ele.getClipPathShape();
|
|---|
| 128 | clipPathUnits = ele.getClipPathUnits();
|
|---|
| 129 | }
|
|---|
| 130 | }
|
|---|
| 131 |
|
|---|
| 132 | //Return if we're out of clipping range
|
|---|
| 133 | if (clipPath != null)
|
|---|
| 134 | {
|
|---|
| 135 | if (clipPathUnits == ClipPath.CP_OBJECT_BOUNDING_BOX && (this instanceof ShapeElement))
|
|---|
| 136 | {
|
|---|
| 137 | Rectangle2D rect = ((ShapeElement)this).getBoundingBox();
|
|---|
| 138 | AffineTransform at = new AffineTransform();
|
|---|
| 139 | at.scale(rect.getWidth(), rect.getHeight());
|
|---|
| 140 | clipPath = at.createTransformedShape(clipPath);
|
|---|
| 141 | }
|
|---|
| 142 |
|
|---|
| 143 | cachedClip = g.getClip();
|
|---|
| 144 | Area newClip = new Area(cachedClip);
|
|---|
| 145 | newClip.intersect(new Area(clipPath));
|
|---|
| 146 | g.setClip(newClip);
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 |
|
|---|
| 150 | /**
|
|---|
| 151 | * Restores transform and clipping values to the way they were before
|
|---|
| 152 | * this layer was drawn.
|
|---|
| 153 | */
|
|---|
| 154 | protected void finishLayer(Graphics2D g)
|
|---|
| 155 | {
|
|---|
| 156 | if (cachedClip != null)
|
|---|
| 157 | {
|
|---|
| 158 | g.setClip(cachedClip);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | if (cachedXform != null)
|
|---|
| 162 | {
|
|---|
| 163 | g.setTransform(cachedXform);
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | }
|
|---|