All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.svg.path.SvgPathMoveToCmd Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.svg.path;

import java.awt.*;
import java.awt.geom.GeneralPath;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

//------------------------------------------------------------------------------
/**
 * Object representation of an SVG (Scalable Vector Graphics) path moveTo ('M' or 'm') 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 class SvgPathMoveToCmd extends SvgPathCmd
{

   //---------------------------------------------------------------------------
   public SvgPathMoveToCmd()
   {
      super('M');
   }


   //---------------------------------------------------------------------------
   @Override
   public SvgPathMoveToCmd setIsRelative(boolean inValue)
   {
      return (SvgPathMoveToCmd) super.setIsRelative(inValue);
   }

   //---------------------------------------------------------------------------
   @Override
   public SvgPathMoveToCmd setRawNumbers(List inValue)
   {
      if (inValue.size()%2 != 0)
      {
         throw new SvgPathDataException("An even number of numbers must be given to a moveTo path command!");
      }

      setNumSteps(inValue.size()/2);

      super.setRawNumbers(inValue);
      return this;
   }


   //---------------------------------------------------------------------------
   public SvgPathMoveToCmd setRawNumbers(Float... inValues)
   {
      if (inValues.length%2 != 0)
      {
         throw new SvgPathDataException("An even number of numbers must be given to a moveTo path command!");
      }

      setNumSteps(inValues.length/2);

      super.setRawNumbers(Arrays.asList(inValues));
      return this;
   }

   //--------------------------------------------------------------------------
   public SvgPathMoveToCmd addPoint(Point inValue)
   {
      if (inValue != null)
      {
         List rawNumbers = getRawNumbers();
         if (null == rawNumbers)
         {
            rawNumbers = new ArrayList<>(10);
         }

         rawNumbers.add((float) inValue.getX());
         rawNumbers.add((float) inValue.getY());

         setRawNumbers(rawNumbers);
         setNumSteps(getNumSteps() + 1);
      }

      return this;
   }

   //--------------------------------------------------------------------------
   public SvgPathMoveToCmd addPoint(Point2D inValue)
   {
      if (inValue != null)
      {
         List rawNumbers = getRawNumbers();
         if (null == rawNumbers)
         {
            rawNumbers = new ArrayList<>(10);
         }

         rawNumbers.add((float) inValue.getX());
         rawNumbers.add((float) inValue.getY());

         setRawNumbers(rawNumbers);
         setNumSteps(getNumSteps() + 1);
      }

      return this;
   }

   //--------------------------------------------------------------------------
   // From http://www.w3.org/TR/SVG/paths.html
   //
   // Start a new sub-path at the given (x,y) coordinate. M (uppercase) indicates that absolute coordinates will follow;
   // m (lowercase) indicates that relative coordinates will follow. If a moveto is followed by multiple pairs of coordinates,
   // the subsequent pairs are treated as implicit lineto commands. Hence, implicit lineto commands will be relative if
   // the moveto is relative, and absolute if the moveto is absolute. If a relative moveto (m) appears as the first
   // element of the path, then it is treated as a pair of absolute coordinates. In this case, subsequent pairs of
   // coordinates are treated as relative even though the initial moveto is interpreted as an absolute moveto.
   public Point2D.Float draw(Path2D.Float inPolyline)
   {
      List rawNumbers = getRawNumbers();
      int numIndex = 0;

      Point2D.Float currentPoint = getStartingPoint();

      while (numIndex < rawNumbers.size() - 1)
      {
         Point2D.Float point = new Point2D.Float(rawNumbers.get(numIndex++), rawNumbers.get(numIndex++));

         if (2 == numIndex)
         {
            // First point.
            if (isRelative()
                && getStartingPoint() != null)
            {
               point.setLocation(point.getX() + currentPoint.getX(), point.getY() + currentPoint.getY());
            }

            inPolyline.moveTo(point.getX(), point.getY());
         }
         else
         {
            // Subsequent x,y pairs are treated as implicit lineTo commands.

            if (isRelative())
            {
               point.setLocation(point.getX() + currentPoint.getX(), point.getY() + currentPoint.getY());
            }

            inPolyline.lineTo(point.getX(), point.getY());
         }

         currentPoint = point;
      }

      // Return the last point.
      return currentPoint;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy