com.hfg.svg.path.SvgPathCmd 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.path;
import com.hfg.exception.ProgrammingException;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.collection.CollectionUtil;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
//------------------------------------------------------------------------------
/**
* Base representation of an SVG (Scalable Vector Graphics) path command.
*
* @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 abstract class SvgPathCmd
{
private char mCmdChar;
private List mRawNumbers;
private boolean mIsRelative;
private Point2D.Float mStartingPoint;
private int mNumSteps;
private static Map sCmdMap = new HashMap<>(25);
static
{
try
{
sCmdMap.put('A', SvgPathEllipticalArcCmd.class.getConstructor());
sCmdMap.put('C', SvgPathCurveToCmd.class.getConstructor());
sCmdMap.put('H', SvgPathHorizLineToCmd.class.getConstructor());
sCmdMap.put('L', SvgPathLineToCmd.class.getConstructor());
sCmdMap.put('M', SvgPathMoveToCmd.class.getConstructor());
sCmdMap.put('Q', SvgPathQuadCurveToCmd.class.getConstructor());
sCmdMap.put('S', SvgPathSmoothCurveToCmd.class.getConstructor());
sCmdMap.put('T', SvgPathSmoothQuadCurveToCmd.class.getConstructor());
sCmdMap.put('V', SvgPathVertLineToCmd.class.getConstructor());
sCmdMap.put('Z', SvgPathClosePathCmd.class.getConstructor());
}
catch (NoSuchMethodException e)
{
throw new ProgrammingException(e);
}
}
//---------------------------------------------------------------------------
protected SvgPathCmd(char inCmdChar)
{
mCmdChar = inCmdChar;
}
//---------------------------------------------------------------------------
public static SvgPathCmd allocate(char inCmdChar)
{
SvgPathCmd pathCmd = null;
Constructor cmdClassConstructor = sCmdMap.get(Character.toUpperCase(inCmdChar));
if (cmdClassConstructor != null)
{
try
{
pathCmd = (SvgPathCmd) cmdClassConstructor.newInstance();
}
catch (Exception e)
{
throw new ProgrammingException(e);
}
if (Character.isLowerCase(inCmdChar))
{
pathCmd.mIsRelative = true;
}
}
return pathCmd;
}
//---------------------------------------------------------------------------
public char getCmdChar()
{
return (mIsRelative ? Character.toLowerCase(mCmdChar) : mCmdChar);
}
//---------------------------------------------------------------------------
public SvgPathCmd setIsRelative(boolean inValue)
{
mIsRelative = inValue;
return this;
}
//---------------------------------------------------------------------------
public boolean isRelative()
{
return mIsRelative;
}
//---------------------------------------------------------------------------
public SvgPathCmd setRawNumbers(List inValue)
{
mRawNumbers = (inValue != null ? new ArrayList<>(inValue) : null);
return this;
}
//---------------------------------------------------------------------------
public List getRawNumbers()
{
return mRawNumbers;
}
//---------------------------------------------------------------------------
public SvgPathCmd setStartingPoint(Point2D.Float inValue)
{
mStartingPoint = inValue;
return this;
}
//---------------------------------------------------------------------------
public Point2D.Float getStartingPoint()
{
return mStartingPoint;
}
//---------------------------------------------------------------------------
protected SvgPathCmd setNumSteps(int inValue)
{
mNumSteps = inValue;
return this;
}
//---------------------------------------------------------------------------
public int getNumSteps()
{
return mNumSteps;
}
//---------------------------------------------------------------------------
public abstract Point2D.Float draw(Path2D.Float inPolyline);
//---------------------------------------------------------------------------
@Override
public String toString()
{
StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(" ");
if (CollectionUtil.hasValues(mRawNumbers))
{
for (Float num : mRawNumbers)
{
buffer.delimitedAppend(num.floatValue() == num.intValue() ? num.intValue() + "" : num + "");
}
}
buffer.insert(0, getCmdChar());
return buffer.toString();
}
}