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

/**
 * FSKerning describes the spacing between a pair of glyphs.
 * 
 * <p>
 * The kerning is used to fine-tune the spacing between specific pairs of
 * characters to make them visually more appealing. The glyphs are identified by
 * an index into the glyph table for the font. The adjustment is specified
 * relative to the advance define for the left glyph.
 * </p>
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>leftIndex</td>
 * <td>The index into the code table that identifies the glyph on the left side
 * of the pair.</td>
 * </tr>
 * 
 * <tr>
 * <td>rightIndex</td>
 * <td>The index into a code table that identifies the glyph on the right side
 * of the pair.</td>
 * </tr>
 * 
 * <tr>
 * <td>adjustment</td>
 * <td>The adjustment in twips that will be added to the advance for the glyph
 * on the left side of the pair to obtain the final spacing between the glyphs.</td>
 * </tr>
 * 
 * </table>
 * 
 * <p>
 * FSKerning objects are only used within FSDefineFont2 objects and provide more
 * precise control over the layout of a font's glyph than was possible using the
 * FSDefineFont and FSFontInfo objects.
 * </p>
 * 
 * <h1 class="datasheet">History</h1>
 * 
 * <p>
 * FSKerning class represents the KerningRecord from the Macromedia Flash (SWF)
 * File Format Specification. It was introduced in Flash 2.
 * </p>
 */
public class FSKerning extends FSTransformObject
{
	private int leftGlyphIndex = 0;
	private int rightGlyphIndex = 0;
	private int adjustment = 0;

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

	/**
	 * Constructs an FSKerning object specifying the glyph indexes and
	 * adjustment. The value for the adjustment must be specified in twips.
	 * 
	 * @param leftIndex
	 *            the index in a code table for the glyph on the left side of
	 *            the pair.
	 * @param rightIndex
	 *            the index in a code table for the glyph on the right side of
	 *            the pair.
	 * @param adjust
	 *            the adjustment that will be added to the advance defined for
	 *            the left glyph.
	 */
	public FSKerning(int leftIndex, int rightIndex, int adjust)
	{
		setLeftGlyphIndex(leftIndex);
		setRightGlyphIndex(rightIndex);
		setAdjustment(adjust);
	}

	/**
	 * Constructs an FSKerning object by copying values from an existing object.
	 * 
	 * @param obj
	 *            an FSKerning object.
	 */
	public FSKerning(FSKerning obj)
	{
		leftGlyphIndex = obj.leftGlyphIndex;
		rightGlyphIndex = obj.rightGlyphIndex;
		adjustment = obj.adjustment;
	}

	/**
	 * Gets the index of the left glyph in the kerning pair.
	 * 
	 * @return the index in a code table for the glyph on the left side of the
	 *         pair.
	 */
	public int getLeftGlyphIndex()
	{
		return leftGlyphIndex;
	}

	/**
	 * Gets the index of the right glyph in the kerning pair.
	 * 
	 * @return the index in a code table for the glyph on the right side of the
	 *         pair.
	 */
	public int getRightGlyphIndex()
	{
		return rightGlyphIndex;
	}

	/**
	 * Gets the adjustment, in twips, to the advance of the left glyph.
	 * 
	 * @return the adjustment that will be added to the advance defined for the
	 *         left glyph.
	 */
	public int getAdjustment()
	{
		return adjustment;
	}

	/**
	 * Sets the index of the left glyph in the kerning pair.
	 * 
	 * @param anIndex
	 *            the index in a code table for the glyph on the left side of
	 *            the pair.
	 */
	public void setLeftGlyphIndex(int anIndex)
	{
		leftGlyphIndex = anIndex;
	}

	/**
	 * Sets the index of the right glyph in the kerning pair.
	 * 
	 * @param anIndex
	 *            the index in a code table for the glyph on the right side of
	 *            the pair.
	 */
	public void setRightGlyphIndex(int anIndex)
	{
		rightGlyphIndex = anIndex;
	}

	/**
	 * Sets the adjustment, in twips, to the advance of the left glyph.
	 * 
	 * @param aNumber
	 *            the adjustment that will be added to the advance defined for
	 *            the left glyph.
	 */
	public void setAdjustment(int aNumber)
	{
		adjustment = aNumber;
	}

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

			result = leftGlyphIndex == typedObject.leftGlyphIndex;
			result = result && rightGlyphIndex == typedObject.rightGlyphIndex;
			result = result && adjustment == typedObject.adjustment;
		}
		return result;
	}

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

		if (depth > 0)
		{
			buffer.append(": { ");
			Transform.append(buffer, "leftGlyphIndex", leftGlyphIndex);
			Transform.append(buffer, "rightGlyphIndex", rightGlyphIndex);
			Transform.append(buffer, "adjustment", adjustment);
			buffer.append("}");
		}
	}

	public int length(FSCoder coder)
	{
		int length = (coder.context[FSCoder.WideCodes] != 0) ? 4 : 2;

		length += 2;

		return length;
	}

	public void encode(FSCoder coder)
	{
		int numberOfBytes = (coder.context[FSCoder.WideCodes] != 0) ? 2 : 1;

		coder.writeWord(leftGlyphIndex, numberOfBytes);
		coder.writeWord(rightGlyphIndex, numberOfBytes);
		coder.writeWord(adjustment, 2);
	}

	public void decode(FSCoder coder)
	{
		int numberOfBytes = (coder.context[FSCoder.WideCodes] != 0) ? 2 : 1;

		leftGlyphIndex = coder.readWord(numberOfBytes, false);
		rightGlyphIndex = coder.readWord(numberOfBytes, false);
		adjustment = coder.readWord(2, true);
	}
}
