001package com.hfg.svg.path; 002 003import java.awt.geom.GeneralPath; 004import java.awt.geom.Path2D; 005import java.awt.geom.Point2D; 006import java.util.List; 007 008//------------------------------------------------------------------------------ 009/** 010 * Object representation of an SVG (Scalable Vector Graphics) path lineTo ('L' or 'l') command. 011 * 012 * @author J. Alex Taylor, hairyfatguy.com 013 */ 014//------------------------------------------------------------------------------ 015// com.hfg XML/HTML Coding Library 016// 017// This library is free software; you can redistribute it and/or 018// modify it under the terms of the GNU Lesser General Public 019// License as published by the Free Software Foundation; either 020// version 2.1 of the License, or (at your option) any later version. 021// 022// This library is distributed in the hope that it will be useful, 023// but WITHOUT ANY WARRANTY; without even the implied warranty of 024// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 025// Lesser General Public License for more details. 026// 027// You should have received a copy of the GNU Lesser General Public 028// License along with this library; if not, write to the Free Software 029// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 030// 031// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 032// jataylor@hairyfatguy.com 033//------------------------------------------------------------------------------ 034 035public class SvgPathLineToCmd extends SvgPathCmd 036{ 037 038 //--------------------------------------------------------------------------- 039 public SvgPathLineToCmd() 040 { 041 super('L'); 042 } 043 044 045 //--------------------------------------------------------------------------- 046 @Override 047 public SvgPathLineToCmd setRawNumbers(List<Float> inValue) 048 { 049 if (inValue.size()%2 != 0) 050 { 051 throw new SvgPathDataException("An even number of numbers must be given to a lineTo path command!"); 052 } 053 054 setNumSteps(inValue.size()/2); 055 056 super.setRawNumbers(inValue); 057 return this; 058 } 059 060 061 //-------------------------------------------------------------------------- 062 // From http://www.w3.org/TR/SVG/paths.html 063 // 064 // Draw a line from the current point to the given (x,y) coordinate which becomes the new current point. 065 // L (uppercase) indicates that absolute coordinates will follow; l (lowercase) indicates that relative coordinates will follow. 066 // A number of coordinates pairs may be specified to draw a polyline. At the end of the command, the new current point is set 067 // to the final set of coordinates provided. 068 public Point2D.Float draw(Path2D.Float inPolyline) 069 { 070 List<Float> rawNumbers = getRawNumbers(); 071 int numIndex = 0; 072 073 Point2D.Float currentPoint = getStartingPoint(); 074 075 while (numIndex < rawNumbers.size() - 1) 076 { 077 Point2D.Float point = new Point2D.Float(rawNumbers.get(numIndex++), rawNumbers.get(numIndex++)); 078 079 if (isRelative()) 080 { 081 point.setLocation(point.getX() + currentPoint.getX(), point.getY() + currentPoint.getY()); 082 } 083 084 inPolyline.lineTo(point.getX(), point.getY()); 085 086 currentPoint = point; 087 } 088 089 // Return the last point. 090 return currentPoint; 091 } 092}