/*
 * FSLine.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;

/**
 * FSLine defines a straight line. The line is drawn from the current drawing
 * point to the end point specified in the FSLine object which is specified
 * relative to the current drawing point. Once the line is drawn, the end of the
 * line is now the current drawing point.
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>x</td>
 * <td>The x-coordinate of the end point of the line, relative to the current
 * drawing point.</td>
 * </tr>
 * 
 * <tr>
 * <td>y</td>
 * <td>The y-coordinate of the end point of the line, relative to the current
 * drawing 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>
 * Lines are drawn with rounded corners and line ends. Different join and line
 * end styles can be created by drawing line segments as a sequence of filled
 * shapes. With 1 twip equal to 1/20th of a pixel this technique can easily be
 * used to draw the narrowest of visible lines.
 * </p>
 * 
 * <h1 class="datasheet">History</h1>
 * <p>
 * The FSLine class represents the StraightEdge record from the Macromedia Flash
 * (SWF) File Format Specification. It was introduced in Flash 1.
 * </p>
 */
public class FSLine extends FSTransformObject
{
	private int x = 0;
	private int y = 0;

	/**
	 * Construct an FSLine object and initialise it with the values decoded from
	 * an encoded line.
	 * 
	 * @param coder
	 *            an FSCoder object containing the encoded colour transform.
	 */
	public FSLine(FSCoder coder)
	{
		decode(coder);
	}

	/**
	 * Constructs an FSLine with the specified relative coordinates.
	 * 
	 * @param xValue
	 *            the x-coordinate of the end point, specified relative to the
	 *            current drawing point.
	 * @param yValue
	 *            the y-coordinate of the end point, specified relative to the
	 *            current drawing point.
	 */
	public FSLine(int xValue, int yValue)
	{
		setX(xValue);
		setY(yValue);
	}

	/**
	 * Construct an FSCoordTransform object by copying an existing object.
	 */
	public FSLine(FSLine obj)
	{
		x = obj.x;
		y = obj.y;
	}

	/**
	 * Gets the relative x-coordinate.
	 * 
	 * @return the x-coordinate of the end point.
	 */
	public int getX()
	{
		return x;
	}

	/**
	 * Gets the relative y-coordinate.
	 * 
	 * @return the y-coordinate of the end point.
	 */
	public int getY()
	{
		return y;
	}

	/**
	 * Sets the relative x-coordinate.
	 * 
	 * @param aNumber
	 *            the x-coordinate of the end point.
	 */
	public void setX(int aNumber)
	{
		x = aNumber;
	}

	/**
	 * Sets the relative y-coordinate.
	 * 
	 * @param aNumber
	 *            the y-coordinate of the end point.
	 */
	public void setY(int aNumber)
	{
		y = aNumber;
	}

	/**
	 * Sets the relative x and y coordinates.
	 * 
	 * @param xValue
	 *            the x-coordinate of the end point.
	 * @param yValue
	 *            the y-coordinate of the end point.
	 */
	public void setPoint(int xValue, int yValue)
	{
		x = xValue;
		y = yValue;
	}

	/**
	 * 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))
		{
			FSLine typedObject = (FSLine) anObject;

			result = x == typedObject.x;
			result = result && y == typedObject.y;
		}
		return result;
	}

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

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "x", x);
			Transform.append(buffer, "y", y);
			buffer.append("}");
		}
	}

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

		int _fieldSize = FSCoder.size(new int[]
		{
			x, y, 1 }, true);

		if (containsGeneralLine())
			numberOfBits += _fieldSize * 2;
		else
		{
			numberOfBits += 1 + _fieldSize;
		}

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

		return numberOfBits;
	}

	public void encode(FSCoder coder)
	{
		boolean _containsGeneralLine = containsGeneralLine();
		boolean _containsVerticalLine = containsVerticalLine();

		int _fieldSize = FSCoder.size(new int[]
		{
			x, y, 1 }, true);

		coder.writeBits(1, 1);
		coder.writeBits(1, 1);
		coder.writeBits(_fieldSize - 2, 4);
		coder.writeBits(_containsGeneralLine ? 1 : 0, 1);

		if (_containsGeneralLine)
		{
			coder.writeBits(x, _fieldSize);
			coder.writeBits(y, _fieldSize);
		} else
		{
			coder.writeBits(_containsVerticalLine ? 1 : 0, 1);
			coder.writeBits(_containsVerticalLine ? y : x, _fieldSize);
		}
	}

	public void decode(FSCoder coder)
	{
		/* shapeType */coder.readBits(1, false);
		/* edgeType */coder.readBits(1, false);
		int fieldSize = coder.readBits(4, false) + 2;

		boolean _containsGeneralLine = coder.readBits(1, false) != 0 ? true
						: false;

		if (_containsGeneralLine)
		{
			x = coder.readBits(fieldSize, true);
			y = coder.readBits(fieldSize, true);
		} else
		{
			boolean _containsVerticalLine = coder.readBits(1, false) != 0;

			if (_containsVerticalLine)
			{
				y = coder.readBits(fieldSize, true);
			} else
			{
				x = coder.readBits(fieldSize, true);
			}
		}
	}

	private boolean containsGeneralLine()
	{
		return x != 0 && y != 0;
	}

	private boolean containsVerticalLine()
	{
		return x == 0;
	}
}
