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/ImageSVG.java | 197 ++++++++++++++++++----------- 1 file changed, 124 insertions(+), 73 deletions(-) (limited to 'src/main/java/com/kitfox/svg/ImageSVG.java') diff --git a/src/main/java/com/kitfox/svg/ImageSVG.java b/src/main/java/com/kitfox/svg/ImageSVG.java index 5ac93d3..5b4c66f 100644 --- a/src/main/java/com/kitfox/svg/ImageSVG.java +++ b/src/main/java/com/kitfox/svg/ImageSVG.java @@ -33,16 +33,19 @@ * * Created on February 20, 2004, 10:00 PM */ - package com.kitfox.svg; import com.kitfox.svg.app.data.Handler; -import com.kitfox.svg.xml.*; - -import java.awt.*; -import java.awt.geom.*; -import java.awt.image.*; -import java.net.*; +import com.kitfox.svg.xml.StyleAttribute; +import java.awt.AlphaComposite; +import java.awt.Composite; +import java.awt.Graphics2D; +import java.awt.geom.AffineTransform; +import java.awt.geom.Point2D; +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; +import java.net.URI; +import java.net.URL; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; @@ -55,66 +58,84 @@ import java.util.logging.Logger; */ public class ImageSVG extends RenderableElement { + public static final String TAG_NAME = "image"; + float x = 0f; float y = 0f; float width = 0f; float height = 0f; - // BufferedImage href = null; URL imageSrc = null; - AffineTransform xform; Rectangle2D bounds; - /** Creates a new instance of Font */ + /** + * Creates a new instance of Font + */ public ImageSVG() { } - + + public String getTagName() + { + return TAG_NAME; + } + protected void build() throws SVGException { super.build(); - + StyleAttribute sty = new StyleAttribute(); - - if (getPres(sty.setName("x"))) x = sty.getFloatValueWithUnits(); - if (getPres(sty.setName("y"))) y = sty.getFloatValueWithUnits(); + if (getPres(sty.setName("x"))) + { + x = sty.getFloatValueWithUnits(); + } - if (getPres(sty.setName("width"))) width = sty.getFloatValueWithUnits(); + if (getPres(sty.setName("y"))) + { + y = sty.getFloatValueWithUnits(); + } + + if (getPres(sty.setName("width"))) + { + width = sty.getFloatValueWithUnits(); + } - if (getPres(sty.setName("height"))) height = sty.getFloatValueWithUnits(); + if (getPres(sty.setName("height"))) + { + height = sty.getFloatValueWithUnits(); + } - try { + try + { if (getPres(sty.setName("xlink:href"))) { URI src = sty.getURIValue(getXMLBase()); if ("data".equals(src.getScheme())) { imageSrc = new URL(null, src.toASCIIString(), new Handler()); - } - else + } else { - try { + try + { imageSrc = src.toURL(); - } - 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); // e.printStackTrace(); imageSrc = null; } } } - } - catch (Exception e) + } catch (Exception e) { throw new SVGException(e); } diagram.getUniverse().registerImage(imageSrc); - + //Set widths if not set BufferedImage img = diagram.getUniverse().getImage(imageSrc); if (img == null) @@ -123,26 +144,45 @@ public class ImageSVG extends RenderableElement bounds = new Rectangle2D.Float(); return; } - - if (width == 0) width = img.getWidth(); - if (height == 0) height = img.getHeight(); - + + if (width == 0) + { + width = img.getWidth(); + } + if (height == 0) + { + height = img.getHeight(); + } + //Determine image xform xform = new AffineTransform(); // xform.setToScale(this.width / img.getWidth(), this.height / img.getHeight()); // xform.translate(this.x, this.y); xform.translate(this.x, this.y); xform.scale(this.width / img.getWidth(), this.height / img.getHeight()); - + bounds = new Rectangle2D.Float(this.x, this.y, this.width, this.height); } - - - - public float getX() { return x; } - public float getY() { return y; } - public float getWidth() { return width; } - public float getHeight() { return height; } + + public float getX() + { + return x; + } + + public float getY() + { + return y; + } + + public float getWidth() + { + return width; + } + + public float getHeight() + { + return height; + } void pick(Point2D point, boolean boundingBox, List retVec) throws SVGException { @@ -165,50 +205,63 @@ public class ImageSVG extends RenderableElement StyleAttribute styleAttrib = new StyleAttribute(); if (getStyle(styleAttrib.setName("visibility"))) { - if (!styleAttrib.getStringValue().equals("visible")) return; + if (!styleAttrib.getStringValue().equals("visible")) + { + return; + } } - + beginLayer(g); - + float opacity = 1f; if (getStyle(styleAttrib.setName("opacity"))) { opacity = styleAttrib.getRatioValue(); } - - if (opacity <= 0) return; + + if (opacity <= 0) + { + return; + } Composite oldComp = null; - + if (opacity < 1) { oldComp = g.getComposite(); Composite comp = AlphaComposite.getInstance(AlphaComposite.SRC_OVER, opacity); g.setComposite(comp); } - + BufferedImage img = diagram.getUniverse().getImage(imageSrc); - if (img == null) return; - + if (img == null) + { + return; + } + AffineTransform curXform = g.getTransform(); g.transform(xform); - + g.drawImage(img, 0, 0, null); - + g.setTransform(curXform); - if (oldComp != null) g.setComposite(oldComp); - + if (oldComp != null) + { + g.setComposite(oldComp); + } + finishLayer(g); } - + public Rectangle2D getBoundingBox() { return boundsToParent(bounds); } /** - * 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 */ @@ -220,7 +273,7 @@ public class ImageSVG extends RenderableElement //Get current values for parameters StyleAttribute sty = new StyleAttribute(); boolean shapeChange = false; - + if (getPres(sty.setName("x"))) { float newVal = sty.getFloatValueWithUnits(); @@ -230,7 +283,7 @@ public class ImageSVG extends RenderableElement shapeChange = true; } } - + if (getPres(sty.setName("y"))) { float newVal = sty.getFloatValueWithUnits(); @@ -240,7 +293,7 @@ public class ImageSVG extends RenderableElement shapeChange = true; } } - + if (getPres(sty.setName("width"))) { float newVal = sty.getFloatValueWithUnits(); @@ -250,7 +303,7 @@ public class ImageSVG extends RenderableElement shapeChange = true; } } - + if (getPres(sty.setName("height"))) { float newVal = sty.getFloatValueWithUnits(); @@ -260,8 +313,9 @@ public class ImageSVG extends RenderableElement shapeChange = true; } } - - try { + + try + { if (getPres(sty.setName("xlink:href"))) { URI src = sty.getURIValue(getXMLBase()); @@ -270,32 +324,29 @@ public class ImageSVG extends RenderableElement if ("data".equals(src.getScheme())) { newVal = new URL(null, src.toASCIIString(), new Handler()); - } - else + } else { newVal = src.toURL(); } - + if (!newVal.equals(imageSrc)) { imageSrc = newVal; shapeChange = true; } } - } - catch (IllegalArgumentException ie) + } catch (IllegalArgumentException ie) { - Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, - "Image provided with illegal value for href: \"" + Logger.getLogger(SVGConst.SVG_LOGGER).log(Level.WARNING, + "Image provided with illegal value for href: \"" + sty.getStringValue() + '"', ie); - } - 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); } - + if (shapeChange) { build(); @@ -325,7 +376,7 @@ public class ImageSVG extends RenderableElement // // return true; } - + return changeState || shapeChange; } } -- cgit v1.2.3-55-g7522