001package com.hfg.svg.path; 002 003import java.awt.geom.Path2D; 004import java.awt.geom.Point2D; 005import java.util.List; 006 007//------------------------------------------------------------------------------ 008/** 009 * Object representation of an SVG (Scalable Vector Graphics) path smooth quadratic curveTo ('T' or 't') command. 010 * 011 * @author J. Alex Taylor, hairyfatguy.com 012 */ 013//------------------------------------------------------------------------------ 014// com.hfg XML/HTML Coding Library 015// 016// This library is free software; you can redistribute it and/or 017// modify it under the terms of the GNU Lesser General Public 018// License as published by the Free Software Foundation; either 019// version 2.1 of the License, or (at your option) any later version. 020// 021// This library is distributed in the hope that it will be useful, 022// but WITHOUT ANY WARRANTY; without even the implied warranty of 023// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 024// Lesser General Public License for more details. 025// 026// You should have received a copy of the GNU Lesser General Public 027// License along with this library; if not, write to the Free Software 028// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 029// 030// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 031// jataylor@hairyfatguy.com 032//------------------------------------------------------------------------------ 033 034public class SvgPathSmoothQuadCurveToCmd extends SvgPathQuadCurveToCmd 035{ 036 private Point2D.Float mPrevCmdControlPoint; 037 038 //--------------------------------------------------------------------------- 039 public SvgPathSmoothQuadCurveToCmd() 040 { 041 super('T'); 042 } 043 044 045 //--------------------------------------------------------------------------- 046 @Override 047 protected int getExpectedNumPoints() 048 { 049 return 2; 050 } 051 052 //--------------------------------------------------------------------------- 053 public void setPrevCmdControlPoint(Point2D.Float inValue) 054 { 055 mPrevCmdControlPoint = inValue; 056 } 057 058 059 //-------------------------------------------------------------------------- 060 // From http://www.w3.org/TR/SVG/paths.html 061 // 062 // Draws a quadratic Bézier curve from the current point to (x,y). 063 // The control point is assumed to be the reflection of the control point on the previous command relative to the current point. 064 // (If there is no previous command or if the previous command was not a Q, q, T or t, assume the control point is coincident 065 // with the current point.) T (uppercase) indicates that absolute coordinates will follow; t (lowercase) indicates that 066 // relative coordinates will follow. At the end of the command, the new current point becomes the final (x,y) coordinate pair 067 // used in the polybézier. 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 Point2D.Float ctrlPoint = currentPoint; 085 if (mPrevCmdControlPoint != null) 086 { 087 // Calculate reflection of the prev second control point 088 ctrlPoint = new Point2D.Float((float) (currentPoint.getX() + (2 * (currentPoint.getX() - mPrevCmdControlPoint.getX()))), 089 (float) (currentPoint.getY() + (2 * (currentPoint.getY() - mPrevCmdControlPoint.getY())))); 090 } 091 092 inPolyline.quadTo(ctrlPoint.getX(), ctrlPoint.getY(), point.getX(), point.getY()); 093 094 setControlPoint(ctrlPoint); 095 096 currentPoint = point; 097 } 098 099 // Return the last point. 100 return currentPoint; 101 } 102}