/*
 * FSCurve.java
 * Transform
 * 
 * Copyright (c) 2001-2008 Flagstone Software Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, 
 * are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 *  * Neither the name of Flagstone Software Ltd. nor the names of its contributors 
 *    may be used to endorse or promote products derived from this software 
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE 
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package com.flagstone.transform;

/**
 * FSCurve is used to define a curve. Curved lines are constructed using a
 * Quadratic Bezier curve.
 * 
 * <p>
 * The curve is specified using two points relative to the current drawing
 * position, an off-curve control point and an on-curve anchor point which
 * defines the end-point of the curve.
 * </p>
 * 
 * <img src="doc-files/quadratic.gif">
 * 
 * <p>
 * To define a curve the points are defined as pairs of relative coordinates.
 * The control point is specified relative to the current drawing point and the
 * anchor point is specified relative to the control point. Once the line is
 * drawn, the anchor point becomes the current drawing point.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>controlX</td>
 * <td>The x-coordinate of the control point relative to the current drawing
 * point.</td>
 * </tr>
 * 
 * <tr>
 * <td>controlY</td>
 * <td>The y-coordinate of the control point relative to the current drawing
 * point.</td>
 * </tr>
 * 
 * <tr>
 * <td>anchorX</td>
 * <td>The x-coordinate of the anchor point relative to the control point.</td>
 * </tr>
 * 
 * <tr>
 * <td>anchorY</td>
 * <td>The y-coordinate of the anchor point relative to the control point.</td>
 * </tr>
 * 
 * </table>
 * 
 * <p>
 * The relative coordinates are specified in twips (where 20 twips = 1 pixel)
 * and must be in the range -65536..65535.
 * </p>
 * 
 * <p>
 * Flash does not directly support Cubic Bezier curves. Converting a Cubic
 * Bezier curve to a Quadratic curve is a non trivial process, however the
 * FSShapeConstructor class in the Transform Utilities framework contains an
 * algorithm to perform the conversion simplifying the create of Shape outlines
 * in Flash from other graphics formats.
 * </p>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * The FSCurve class represents the CurvedEdge record from the Macromedia Flash
 * (SWF) File Format Specification. It was introduced in Flash 1.
 * </p>
 */
public class FSCurve extends FSTransformObject
{
	private int controlX = 0;
	private int controlY = 0;
	private int anchorX = 0;
	private int anchorY = 0;

	/**
	 * Construct an FSCurve object, initalizing it with values decoded from an
	 * encoded object.
	 * 
	 * @param coder
	 *            an FSCoder containing the binary data.
	 */
	public FSCurve(FSCoder coder)
	{
		decode(coder);
	}

	/**
	 * Constructs an FSCurve object specifying the anchor and control point
	 * coordinates - specified in twips.
	 * 
	 * @param cx
	 *            the x-coordinate of the control point, specified relative to
	 *            the current drawing point.
	 * @param cy
	 *            the y-coordinate of the control point, specified relative to
	 *            the current drawing point.
	 * @param ax
	 *            the x-coordinate of the anchor point, specified relative to
	 *            the control point.
	 * @param ay
	 *            the y-coordinate of the anchor point, specified relative to
	 *            the control point.
	 */
	public FSCurve(int cx, int cy, int ax, int ay)
	{
		setControlX(cx);
		setControlY(cy);
		setAnchorX(ax);
		setAnchorY(ay);
	}

	/**
	 * Constructs an FSCurve object by copying values from an existing object.
	 * 
	 * @param obj
	 *            an FSCurve object.
	 */
	public FSCurve(FSCurve obj)
	{
		controlX = obj.controlX;
		controlY = obj.controlY;
		anchorX = obj.anchorX;
		anchorY = obj.anchorY;
	}

	/**
	 * Gets the x-coordinate of the control point relative to the current
	 * drawing point.
	 * 
	 * @return the x-coordinate of the control point.
	 */
	public int getControlX()
	{
		return controlX;
	}

	/**
	 * Gets the y-coordinate of the control point relative to the current
	 * drawing point.
	 * 
	 * @return the y-coordinate of the control point.
	 */
	public int getControlY()
	{
		return controlY;
	}

	/**
	 * Gets the x-coordinate of the anchor point relative to the control point.
	 * 
	 * @return the x-coordinate of the anchor point.
	 */
	public int getAnchorX()
	{
		return anchorX;
	}

	/**
	 * Gets the y-coordinate of the anchor point relative to the control point.
	 * 
	 * @return the y-coordinate of the anchor point.
	 */
	public int getAnchorY()
	{
		return anchorY;
	}

	/**
	 * Sets the x-coordinate of the control point relative to the current
	 * drawing point.
	 * 
	 * @param cx
	 *            the x-coordinate of the control point.
	 */
	public void setControlX(int cx)
	{
		controlX = cx;
	}

	/**
	 * Sets the y-coordinate of the control point relative to the current
	 * drawing point.
	 * 
	 * @param cy
	 *            the y-coordinate of the control point.
	 */
	public void setControlY(int cy)
	{
		controlY = cy;
	}

	/**
	 * Sets the x-coordinate of the anchor point relative to the control point.
	 * 
	 * @param ax
	 *            the x-coordinate of the anchor point.
	 */
	public void setAnchorX(int ax)
	{
		anchorX = ax;
	}

	/**
	 * Sets the y-coordinate of the anchor point relative to the control point.
	 * 
	 * @param ay
	 *            the y-coordinate of the anchor point.
	 */
	public void setAnchorY(int ay)
	{
		anchorY = ay;
	}

	/**
	 * Sets the x and y coordinates of control point, relative to the current
	 * drawing point.
	 * 
	 * @param cx
	 *            the x-coordinate of the control point.
	 * @param cy
	 *            the y-coordinate of the control point.
	 */
	public void setControl(int cx, int cy)
	{
		setControlX(cx);
		setControlY(cy);
	}

	/**
	 * Sets the x and y coordinates of anchor point relative to the control
	 * point.
	 * 
	 * @param ax
	 *            the x-coordinate of the anchor point.
	 * @param ay
	 *            the y-coordinate of the anchor point.
	 */
	public void setAnchor(int ax, int ay)
	{
		setAnchorX(ax);
		setAnchorY(ay);
	}

	/**
	 * Sets the x and y coordinates of the control and anchor points.
	 * 
	 * @param cx
	 *            the x-coordinate of the control point.
	 * @param cy
	 *            the y-coordinate of the control point.
	 * @param ax
	 *            the x-coordinate of the anchor point.
	 * @param ay
	 *            the y-coordinate of the anchor point.
	 */
	public void setPoints(int cx, int cy, int ax, int ay)
	{
		setControlX(cx);
		setControlY(cy);
		setAnchorX(ax);
		setAnchorY(ay);
	}

	/**
	 * Returns true if anObject is equal to this one. Objects are considered
	 * equal if they would generate identical binary data when they are encoded
	 * to a Flash file.
	 * 
	 * @return true if this object would be identical to anObject when encoded.
	 */
	public boolean equals(Object anObject)
	{
		boolean result = false;

		if (super.equals(anObject))
		{
			FSCurve typedObject = (FSCurve) anObject;

			result = controlX == typedObject.controlX;
			result = result && controlY == typedObject.controlY;
			result = result && anchorX == typedObject.anchorX;
			result = result && anchorY == typedObject.anchorY;
		}
		return result;
	}

	public void appendDescription(StringBuffer buffer, int depth)
	{
		buffer.append(name());

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "controlX", controlX);
			Transform.append(buffer, "controlY", controlY);
			Transform.append(buffer, "anchorX", anchorX);
			Transform.append(buffer, "anchorY", anchorY);
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		int numberOfBits = 6;

		int _fieldSize = FSCoder.size(new int[]
		{
			controlX, controlY, anchorX, anchorY, 1 }, true);

		numberOfBits += _fieldSize * 4;

		coder.context[FSCoder.NumberOfShapeBits] += numberOfBits;

		return numberOfBits;
	}

	public void encode(FSCoder coder)
	{
		int _fieldSize = FSCoder.size(new int[]
		{
			controlX, controlY, anchorX, anchorY, 1 }, true);

		coder.writeBits(1, 1);
		coder.writeBits(0, 1);
		coder.writeBits(_fieldSize - 2, 4);
		coder.writeBits(controlX, _fieldSize);
		coder.writeBits(controlY, _fieldSize);
		coder.writeBits(anchorX, _fieldSize);
		coder.writeBits(anchorY, _fieldSize);
	}

	public void decode(FSCoder coder)
	{
		int fieldSize = 0;

		/* shapeType */coder.readBits(1, false);
		/* edgeType */coder.readBits(1, false);
		fieldSize = coder.readBits(4, false) + 2;
		controlX = coder.readBits(fieldSize, true);
		controlY = coder.readBits(fieldSize, true);
		anchorX = coder.readBits(fieldSize, true);
		anchorY = coder.readBits(fieldSize, true);
	}
}
