Changeset 6002 in josm for trunk/src/com/kitfox/svg/LinearGradient.java
- Timestamp:
- 2013-06-11T01:01:28+02:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/com/kitfox/svg/LinearGradient.java
r4256 r6002 1 1 /* 2 * LinearGradient.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 2 * SVG Salamander 3 * Copyright (c) 2004, Mark McKay 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or 7 * without modification, are permitted provided that the following 8 * conditions are met: 9 * 10 * - Redistributions of source code must retain the above 11 * copyright notice, this list of conditions and the following 12 * disclaimer. 13 * - Redistributions in binary form must reproduce the above 14 * copyright notice, this list of conditions and the following 15 * disclaimer in the documentation and/or other materials 16 * provided with the distribution. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 21 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 22 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 23 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 25 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 27 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 29 * OF THE POSSIBILITY OF SUCH DAMAGE. 30 * 31 * Mark McKay can be contacted at mark@kitfox.com. Salamander and other 32 * projects can be found at http://www.kitfox.com 24 33 * 25 34 * Created on January 26, 2004, 1:54 AM 26 35 */ 27 28 36 package com.kitfox.svg; 29 37 30 38 import com.kitfox.svg.xml.StyleAttribute; 31 import java.awt.geom.*; 32 import java.awt.*; 33 34 import com.kitfox.svg.xml.*; 35 import org.xml.sax.*; 36 37 //import org.apache.batik.ext.awt.*; 38 import com.kitfox.svg.batik.*; 39 39 import java.awt.Color; 40 import java.awt.Paint; 41 import java.awt.geom.AffineTransform; 42 import java.awt.geom.Point2D; 43 import java.awt.geom.Rectangle2D; 40 44 41 45 /** … … 43 47 * @author <a href="mailto:mark@kitfox.com">Mark McKay</a> 44 48 */ 45 public class LinearGradient extends Gradient { 46 49 public class LinearGradient extends Gradient 50 { 51 public static final String TAG_NAME = "lineargradient"; 52 47 53 float x1 = 0f; 48 54 float y1 = 0f; … … 50 56 float y2 = 0f; 51 57 52 /** Creates a new instance of LinearGradient */ 53 public LinearGradient() { 54 } 55 /* 56 public void loaderStartElement(SVGLoaderHelper helper, Attributes attrs, SVGElement parent) 57 { 58 //Load style string 59 super.loaderStartElement(helper, attrs, parent); 60 61 String x1 = attrs.getValue("x1"); 62 String x2 = attrs.getValue("x2"); 63 String y1 = attrs.getValue("y1"); 64 String y2 = attrs.getValue("y2"); 65 66 if (x1 != null) this.x1 = (float)XMLParseUtil.parseRatio(x1); 67 if (y1 != null) this.y1 = (float)XMLParseUtil.parseRatio(y1); 68 if (x2 != null) this.x2 = (float)XMLParseUtil.parseRatio(x2); 69 if (y2 != null) this.y2 = (float)XMLParseUtil.parseRatio(y2); 70 } 71 */ 72 /* 73 public void loaderEndElement(SVGLoaderHelper helper) 74 { 75 super.loaderEndElement(helper); 76 77 build(); 78 } 79 */ 80 58 /** 59 * Creates a new instance of LinearGradient 60 */ 61 public LinearGradient() 62 { 63 } 64 65 public String getTagName() 66 { 67 return TAG_NAME; 68 } 69 81 70 protected void build() throws SVGException 82 71 { 83 72 super.build(); 84 73 85 74 StyleAttribute sty = new StyleAttribute(); 86 87 if (getPres(sty.setName("x1"))) x1 = sty.getFloatValueWithUnits(); 88 89 if (getPres(sty.setName("y1"))) y1 = sty.getFloatValueWithUnits(); 90 91 if (getPres(sty.setName("x2"))) x2 = sty.getFloatValueWithUnits(); 92 93 if (getPres(sty.setName("y2"))) y2 = sty.getFloatValueWithUnits(); 94 } 95 75 76 if (getPres(sty.setName("x1"))) 77 { 78 x1 = sty.getFloatValueWithUnits(); 79 } 80 81 if (getPres(sty.setName("y1"))) 82 { 83 y1 = sty.getFloatValueWithUnits(); 84 } 85 86 if (getPres(sty.setName("x2"))) 87 { 88 x2 = sty.getFloatValueWithUnits(); 89 } 90 91 if (getPres(sty.setName("y2"))) 92 { 93 y2 = sty.getFloatValueWithUnits(); 94 } 95 } 96 96 97 public Paint getPaint(Rectangle2D bounds, AffineTransform xform) 97 98 { … … 111 112 } 112 113 113 com.kitfox.svg.batik.LinearGradientPaint paint; 114 if (gradientUnits == GU_USER_SPACE_ON_USE) 115 { 116 // paint = new LinearGradientPaint(x1, y1, x2, y2, getStopFractions(), getStopColors(), method); 114 Paint paint; 115 Point2D.Float pt1 = new Point2D.Float(x1, y1); 116 Point2D.Float pt2 = new Point2D.Float(x2, y2); 117 if (pt1.equals(pt2)) 118 { 119 Color[] colors = getStopColors(); 120 paint = colors.length > 0 ? colors[0] : Color.black; 121 } else if (gradientUnits == GU_USER_SPACE_ON_USE) 122 { 117 123 paint = new com.kitfox.svg.batik.LinearGradientPaint( 118 new Point2D.Float(x1, y1),119 new Point2D.Float(x2, y2),124 pt1, 125 pt2, 120 126 getStopFractions(), 121 127 getStopColors(), 122 128 method, 123 129 com.kitfox.svg.batik.MultipleGradientPaint.SRGB, 124 gradientTransform); 125 } 126 else 130 gradientTransform == null 131 ? new AffineTransform() 132 : gradientTransform); 133 } else 127 134 { 128 135 AffineTransform viewXform = new AffineTransform(); 129 136 viewXform.translate(bounds.getX(), bounds.getY()); 130 137 131 138 //This is a hack to get around shapes that have a width or height of 0. Should be close enough to the true answer. 132 double width = bounds.getWidth(); 133 double height = bounds.getHeight(); 134 if (width == 0) width = 1; 135 if (height == 0) height = 1; 139 double width = Math.max(1, bounds.getWidth()); 140 double height = Math.max(1, bounds.getHeight()); 136 141 viewXform.scale(width, height); 137 142 138 viewXform.concatenate(gradientTransform); 143 if (gradientTransform != null) 144 { 145 viewXform.concatenate(gradientTransform); 146 } 139 147 140 148 paint = new com.kitfox.svg.batik.LinearGradientPaint( 141 new Point2D.Float(x1, y1),142 new Point2D.Float(x2, y2),149 pt1, 150 pt2, 143 151 getStopFractions(), 144 152 getStopColors(), … … 150 158 return paint; 151 159 } 152 160 153 161 /** 154 * Updates all attributes in this diagram associated with a time event. 155 * Ie, all attributes with track information. 162 * Updates all attributes in this diagram associated with a time event. Ie, 163 * all attributes with track information. 164 * 156 165 * @return - true if this node has changed state as a result of the time 157 166 * update … … 165 174 StyleAttribute sty = new StyleAttribute(); 166 175 boolean shapeChange = false; 167 176 168 177 if (getPres(sty.setName("x1"))) 169 178 {
Note:
See TracChangeset
for help on using the changeset viewer.