From 6776ed9f1a81e517139d85eb6d2e28911fd0fc35 Mon Sep 17 00:00:00 2001 From: kitfox Date: Tue, 19 Mar 2013 05:20:37 +0000 Subject: Adding support for style sheets. git-svn-id: https://svn.java.net/svn/svgsalamander~svn/trunk/svg-core@153 7dc7fa77-23fb-e6ad-8e2e-c86bd48ed22b --- src/main/java/com/kitfox/svg/Gradient.java | 182 ++++++++++++++++++----------- 1 file changed, 115 insertions(+), 67 deletions(-) (limited to 'src/main/java/com/kitfox/svg/Gradient.java') diff --git a/src/main/java/com/kitfox/svg/Gradient.java b/src/main/java/com/kitfox/svg/Gradient.java index 5cd17e9..8901930 100644 --- a/src/main/java/com/kitfox/svg/Gradient.java +++ b/src/main/java/com/kitfox/svg/Gradient.java @@ -33,17 +33,14 @@ * * Created on January 26, 2004, 3:25 AM */ - package com.kitfox.svg; -import java.net.*; -import java.util.*; -import java.awt.geom.*; -import java.awt.*; - -import com.kitfox.svg.xml.*; -import java.io.BufferedReader; -import java.io.IOException; +import com.kitfox.svg.xml.StyleAttribute; +import java.awt.Color; +import java.awt.geom.AffineTransform; +import java.net.URI; +import java.util.ArrayList; +import java.util.Iterator; import java.util.logging.Level; import java.util.logging.Logger; @@ -53,34 +50,37 @@ import java.util.logging.Logger; */ abstract public class Gradient extends FillElement { - + public static final String TAG_NAME = "gradient"; + public static final int SM_PAD = 0; public static final int SM_REPEAT = 1; public static final int SM_REFLECT = 2; - int spreadMethod = SM_PAD; - public static final int GU_OBJECT_BOUNDING_BOX = 0; public static final int GU_USER_SPACE_ON_USE = 1; - protected int gradientUnits = GU_OBJECT_BOUNDING_BOX; - //Either this gradient contains a list of stops, or it will take it's // stops from the referenced gradient ArrayList stops = new ArrayList(); URI stopRef = null; -// Gradient stopRef = null; - protected AffineTransform gradientTransform = null; - + //Cache arrays of stop values here float[] stopFractions; Color[] stopColors; - /** Creates a new instance of Gradient */ - public Gradient() { + /** + * Creates a new instance of Gradient + */ + public Gradient() + { } - + + public String getTagName() + { + return TAG_NAME; + } + /** * Called after the start element but before the end element to indicate * each child tag that has been processed @@ -89,70 +89,97 @@ abstract public class Gradient extends FillElement { super.loaderAddChild(helper, child); - if (!(child instanceof Stop)) return; - appendStop((Stop)child); + if (!(child instanceof Stop)) + { + return; + } + appendStop((Stop) child); } protected void build() throws SVGException { super.build(); - + StyleAttribute sty = new StyleAttribute(); String strn; - + if (getPres(sty.setName("spreadMethod"))) { strn = sty.getStringValue().toLowerCase(); - if (strn.equals("repeat")) spreadMethod = SM_REPEAT; - else if (strn.equals("reflect")) spreadMethod = SM_REFLECT; - else spreadMethod = SM_PAD; + if (strn.equals("repeat")) + { + spreadMethod = SM_REPEAT; + } else if (strn.equals("reflect")) + { + spreadMethod = SM_REFLECT; + } else + { + spreadMethod = SM_PAD; + } } if (getPres(sty.setName("gradientUnits"))) { strn = sty.getStringValue().toLowerCase(); - if (strn.equals("userspaceonuse")) gradientUnits = GU_USER_SPACE_ON_USE; - else gradientUnits = GU_OBJECT_BOUNDING_BOX; + if (strn.equals("userspaceonuse")) + { + gradientUnits = GU_USER_SPACE_ON_USE; + } else + { + gradientUnits = GU_OBJECT_BOUNDING_BOX; + } } - if (getPres(sty.setName("gradientTransform"))) gradientTransform = parseTransform(sty.getStringValue()); + if (getPres(sty.setName("gradientTransform"))) + { + gradientTransform = parseTransform(sty.getStringValue()); + } //If we still don't have one, set it to identity - if (gradientTransform == null) gradientTransform = new AffineTransform(); + if (gradientTransform == null) + { + gradientTransform = new AffineTransform(); + } + - //Check to see if we're using our own stops or referencing someone else's if (getPres(sty.setName("xlink:href"))) { - try { + try + { stopRef = sty.getURIValue(getXMLBase()); //System.err.println("Gradient: " + sty.getStringValue() + ", " + getXMLBase() + ", " + src); // URI src = getXMLBase().resolve(href); // stopRef = (Gradient)diagram.getUniverse().getElement(src); - } - catch (Exception e) + } catch (Exception e) { throw new SVGException("Could not resolve relative URL in Gradient: " + sty.getStringValue() + ", " + getXMLBase(), e); } } } - + public float[] getStopFractions() { if (stopRef != null) { - Gradient grad = (Gradient)diagram.getUniverse().getElement(stopRef); + Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef); return grad.getStopFractions(); } - if (stopFractions != null) return stopFractions; + if (stopFractions != null) + { + return stopFractions; + } stopFractions = new float[stops.size()]; int idx = 0; for (Iterator it = stops.iterator(); it.hasNext();) { - Stop stop = (Stop)it.next(); + Stop stop = (Stop) it.next(); float val = stop.offset; - if (idx != 0 && val < stopFractions[idx - 1]) val = stopFractions[idx - 1]; + if (idx != 0 && val < stopFractions[idx - 1]) + { + val = stopFractions[idx - 1]; + } stopFractions[idx++] = val; } @@ -163,44 +190,53 @@ abstract public class Gradient extends FillElement { if (stopRef != null) { - Gradient grad = (Gradient)diagram.getUniverse().getElement(stopRef); + Gradient grad = (Gradient) diagram.getUniverse().getElement(stopRef); return grad.getStopColors(); } - if (stopColors != null) return stopColors; + if (stopColors != null) + { + return stopColors; + } stopColors = new Color[stops.size()]; int idx = 0; for (Iterator it = stops.iterator(); it.hasNext();) { - Stop stop = (Stop)it.next(); + Stop stop = (Stop) it.next(); int stopColorVal = stop.color.getRGB(); - Color stopColor = new Color((stopColorVal >> 16) & 0xff, (stopColorVal >> 8) & 0xff, stopColorVal & 0xff, clamp((int)(stop.opacity * 255), 0, 255)); + Color stopColor = new Color((stopColorVal >> 16) & 0xff, (stopColorVal >> 8) & 0xff, stopColorVal & 0xff, clamp((int) (stop.opacity * 255), 0, 255)); stopColors[idx++] = stopColor; } return stopColors; } - + public void setStops(Color[] colors, float[] fractions) { if (colors.length != fractions.length) { throw new IllegalArgumentException(); } - + this.stopColors = colors; this.stopFractions = fractions; stopRef = null; } - + private int clamp(int val, int min, int max) { - if (val < min) return min; - if (val > max) return max; + if (val < min) + { + return min; + } + if (val > max) + { + return max; + } return val; } - + public void setStopRef(URI grad) { stopRef = grad; @@ -212,8 +248,9 @@ abstract public class Gradient extends FillElement } /** - * Updates all attributes in this diagram associated with a time event. - * Ie, all attributes with track information. + * Updates all attributes in this diagram associated with a time event. Ie, + * all attributes with track information. + * * @return - true if this node has changed state as a result of the time * update */ @@ -226,28 +263,40 @@ abstract public class Gradient extends FillElement StyleAttribute sty = new StyleAttribute(); boolean shapeChange = false; String strn; - + if (getPres(sty.setName("spreadMethod"))) { int newVal; strn = sty.getStringValue().toLowerCase(); - if (strn.equals("repeat")) newVal = SM_REPEAT; - else if (strn.equals("reflect")) newVal = SM_REFLECT; - else newVal = SM_PAD; + if (strn.equals("repeat")) + { + newVal = SM_REPEAT; + } else if (strn.equals("reflect")) + { + newVal = SM_REFLECT; + } else + { + newVal = SM_PAD; + } if (spreadMethod != newVal) { spreadMethod = newVal; stateChange = true; } } - + if (getPres(sty.setName("gradientUnits"))) { int newVal; strn = sty.getStringValue().toLowerCase(); - if (strn.equals("userspaceonuse")) newVal = GU_USER_SPACE_ON_USE; - else newVal = GU_OBJECT_BOUNDING_BOX; + if (strn.equals("userspaceonuse")) + { + newVal = GU_USER_SPACE_ON_USE; + } else + { + newVal = GU_OBJECT_BOUNDING_BOX; + } if (newVal != gradientUnits) { gradientUnits = newVal; @@ -265,21 +314,21 @@ abstract public class Gradient extends FillElement } } - + //Check to see if we're using our own stops or referencing someone else's if (getPres(sty.setName("xlink:href"))) { - try { + try + { URI newVal = sty.getURIValue(getXMLBase()); if ((newVal == null && stopRef != null) || !newVal.equals(stopRef)) { stopRef = newVal; stateChange = true; } - } - catch (Exception e) + } catch (Exception e) { - Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, + Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, "Could not parse xlink:href", e); } } @@ -287,7 +336,7 @@ abstract public class Gradient extends FillElement //Check stops, if any for (Iterator it = stops.iterator(); it.hasNext();) { - Stop stop = (Stop)it.next(); + Stop stop = (Stop) it.next(); if (stop.updateTime(curTime)) { stateChange = true; @@ -295,8 +344,7 @@ abstract public class Gradient extends FillElement stopColors = null; } } - + return stateChange; } - } -- cgit v1.2.3-55-g7522