com.hfg.svg.SvgUse Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of com_hfg Show documentation
Show all versions of com_hfg Show documentation
com.hfg xml, html, svg, and bioinformatics utility library
package com.hfg.svg;
import com.hfg.util.Recursion;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.XMLAttribute;
import com.hfg.xml.XMLContainer;
import com.hfg.xml.XMLNamespace;
import com.hfg.html.HTML;
import com.hfg.xml.XMLNode;
import com.hfg.xml.XMLTag;
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import java.util.List;
//------------------------------------------------------------------------------
/**
Object representation of an SVG (Scalable Vector Graphics) 'use' tag.
See http://www.w3.org/TR/SVG11/struct.html#UseElement
@author J. Alex Taylor, hairyfatguy.com
*/
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------
public class SvgUse extends AbstractSvgNode implements SvgNode
{
//##########################################################################
// CONSTRUCTORS
//##########################################################################
//---------------------------------------------------------------------------
public SvgUse(String inURL)
{
this(inURL, null);
}
//---------------------------------------------------------------------------
public SvgUse(String inURL, Point inLocation)
{
super(SVG.use);
XMLAttribute attr = new XMLAttribute(HTML.HREF, inURL);
attr.setNamespace(XMLNamespace.XLINK);
setAttribute(attr);
if (inLocation != null)
{
setAttribute(SvgAttr.x, (int) inLocation.getX());
setAttribute(SvgAttr.y, (int) inLocation.getY());
}
}
//---------------------------------------------------------------------------
public SvgUse(XMLTag inXMLTag)
{
super(SVG.use);
initFromXMLTag(inXMLTag);
}
//##########################################################################
// PUBLIC METHODS
//##########################################################################
//--------------------------------------------------------------------------
public String getURL()
{
return getAttributeValue(HTML.HREF);
}
//--------------------------------------------------------------------------
@Override
public void draw(Graphics2D g2)
{
// Save settings
Font origFont = g2.getFont();
Paint origPaint = g2.getPaint();
Stroke origStroke = g2.getStroke();
Color origBackground = g2.getBackground();
AffineTransform origTransform = null;
if (getAttributeValue(SvgAttr.transform) != null)
{
origTransform = g2.getTransform();
applyTransform(g2, getAttributeValue(SvgAttr.transform));
}
Font locallyAdjustedFont = getAdjustedFont(origFont);
if (locallyAdjustedFont != null)
{
g2.setFont(locallyAdjustedFont);
}
AbstractSvgNode svgDefNode = null;
String url = getURL();
if (StringUtil.isSet(url)
&& url.startsWith("#"))
{
svgDefNode = getLocallyReferencedDefNode();
}
if (svgDefNode != null)
{
svgDefNode.draw(g2);
}
// Restore settings
g2.setFont(origFont);
g2.setPaint(origPaint);
g2.setStroke(origStroke);
g2.setBackground(origBackground);
if (origTransform != null)
{
g2.setTransform(origTransform);
}
}
//---------------------------------------------------------------------------
@Override
public Rectangle2D getBoundsBox()
{
AbstractSvgNode svgDefNode = getLocallyReferencedDefNode();
Rectangle2D boundsBox = (svgDefNode != null ? svgDefNode.getBoundsBox() : null);
if (boundsBox != null)
{
adjustBoundsForTransform(boundsBox);
}
return boundsBox;
}
//##########################################################################
// PRIVATE METHODS
//##########################################################################
//--------------------------------------------------------------------------
private AbstractSvgNode getLocallyReferencedDefNode()
{
AbstractSvgNode svgDefNode = null;
String url = getURL();
if (StringUtil.isSet(url)
&& url.startsWith("#"))
{
// This is a little tricky. We need to find the def referenced by the URL
// by traversing up the DOM structure and then locating any matching defs.
XMLContainer parentNode = getParentNode();
while (parentNode.getParentNode() != null)
{
parentNode = parentNode.getParentNode();
}
List defNodes = parentNode.getSubtagsByName(SVG.defs, Recursion.ON);
if (CollectionUtil.hasValues(defNodes))
{
for (XMLNode defNode : defNodes)
{
SvgDefs defs = (SvgDefs) defNode;
if (CollectionUtil.hasValues(defs.getSubtags()))
{
for (XMLTag xmlTag : (List) (Object) defs.getSubtags())
{
String id = xmlTag.getAttributeValue(SvgAttr.id);
if (StringUtil.isSet(id)
&& id.equals(url.substring(1)))
{
svgDefNode = (AbstractSvgNode) xmlTag;
break;
}
}
}
if (svgDefNode != null)
{
break;
}
}
}
}
return svgDefNode;
}
}