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

import java.util.*;

/**
 * FSDefineFont defines the glyphs that are drawn when text characters are
 * rendered in a particular font.
 * 
 * <table class="datasheet">
 * 
 * <tr>
 * <th align="left" colspan="2">Attributes</th>
 * </tr>
 * 
 * <tr>
 * <td>type</td>
 * <td>Identifies the data structure when it is encoded. Read-only.</td>
 * </tr>
 * 
 * <tr>
 * <td>identifier</td>
 * <td>An unique identifier for this object in the range 1..65535.</td>
 * </tr>
 * 
 * <tr>
 * <td>shapes</td>
 * <td>An array of FSShape objects contain the sequences of FSLine, FSCurve and
 * FSShapeStyle objects used to trace the outlines for each glyph.</td>
 * </tr>
 * 
 * </table>
 * 
 * <p>
 * A complete definition of a font is created using the FSDefineFont object for
 * the glyphs along with an FSFontInfo object which contains the name of the
 * font, whether the font face is bold or italics and a table that maps
 * character codes to the glyphs that is drawn to represent the character.
 * </p>
 * 
 * <p>
 * When defining a font only the glyphs used from a particular font are
 * included. Unused glyphs can be omitted greatly reducing the amount of
 * information that is encoded.
 * </p>
 * 
 * <h2 class="datasheet">History</h2>
 * 
 * <p>
 * The FSDefineFont class represents the DefineFont structure from the
 * Macromedia Flash (SWF) File Format Specification. It was introduced in Flash
 * 1.
 * </p>
 */
public class FSDefineFont extends FSDefineObject
{
	private ArrayList shapes = null;

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

	/**
	 * Constructs an FSDefineFont object setting the unique identifier for the
	 * object and the array of glyphs used to render the characters used from
	 * the font.
	 * 
	 * @param anIdentifier
	 *            the unique identifier for this object.
	 * @param anArray
	 *            an array of FSShape objects that define the outlines for each
	 *            glyph in the font.
	 */
	public FSDefineFont(int anIdentifier, ArrayList anArray)
	{
		super(FSMovieObject.DefineFont, anIdentifier);
		setShapes(anArray);
	}

	/**
	 * Constructs an FSDefineFont object by copying values from an existing
	 * object.
	 * 
	 * @param obj
	 *            an FSDefineFont object.
	 */
	public FSDefineFont(FSDefineFont obj)
	{
		super(obj);

		shapes = new ArrayList();

		for (Iterator i = obj.shapes.iterator(); i.hasNext();)
			shapes.add(((FSShape) i.next()).clone());
	}

	/**
	 * Add a shape to the array of shapes.
	 * 
	 * @param anObject
	 *            a shape.
	 */
	public void add(FSShape anObject)
	{
		shapes.add(anObject);
	}

	/**
	 * Gets the array of shapes that define the outline for each glyph.
	 * 
	 * @return an array of FSShape objects.
	 */
	public ArrayList getShapes()
	{
		return shapes;
	}

	/**
	 * Sets the array of shapes that describe each glyph.
	 * 
	 * @param anArray
	 *            an array of FSShape objects that define the outlines for each
	 *            glyph in the font.
	 */
	public void setShapes(ArrayList anArray)
	{
		shapes = anArray;
	}

	public Object clone()
	{
		FSDefineFont anObject = (FSDefineFont) super.clone();

		anObject.shapes = new ArrayList();

		for (Iterator i = shapes.iterator(); i.hasNext();)
			anObject.shapes.add(((FSShape) i.next()).clone());

		return anObject;
	}

	public boolean equals(Object anObject)
	{
		boolean result = false;

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

			if (shapes != null)
				result = shapes.equals(typedObject.shapes);
			else
				result = shapes == typedObject.shapes;
		}
		return result;
	}

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

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

	public int length(FSCoder coder)
	{
		super.length(coder);

		coder.context[FSCoder.NumberOfFillBits] = 1;
		coder.context[FSCoder.NumberOfLineBits] = 0;

		length += shapes.size() * 2;

		for (Iterator shapeIterator = shapes.iterator(); shapeIterator
						.hasNext();)
			length += ((FSTransformObject) shapeIterator.next()).length(coder);

		coder.context[FSCoder.NumberOfFillBits] = 0;
		coder.context[FSCoder.NumberOfLineBits] = 0;

		return length;
	}

	public void encode(FSCoder coder)
	{
		super.encode(coder);

		coder.context[FSCoder.NumberOfFillBits] = 1;
		coder.context[FSCoder.NumberOfLineBits] = 0;

		int currentLocation;
		int offset;

		int tableStart = coder.getPointer();
		int tableEntry = tableStart;

		for (int i = 0; i < shapes.size(); i++)
			coder.writeWord(0, 2);

		for (Iterator i = shapes.iterator(); i.hasNext(); tableEntry += 16)
		{
			currentLocation = coder.getPointer();
			offset = (coder.getPointer() - tableStart) >> 3;

			coder.setPointer(tableEntry);
			coder.writeWord(offset, 2);
			coder.setPointer(currentLocation);

			((FSTransformObject) i.next()).encode(coder);
		}

		coder.context[FSCoder.NumberOfFillBits] = 0;
		coder.context[FSCoder.NumberOfLineBits] = 0;

		coder.endObject(name());
	}

	public void decode(FSCoder coder)
	{
		shapes = new ArrayList();

		super.decode(coder);

		int offsetStart = coder.getPointer();
		int shapeCount = coder.scanWord(2, false) / 2;

		coder.setPointer(offsetStart);

		int[] offset = new int[shapeCount + 1];

		for (int i = 0; i < shapeCount; i++)
			offset[i] = coder.readWord(2, false);

		offset[shapeCount] = length - 2;

		for (int i = 0; i < shapeCount; i++)
		{
			coder.setPointer(offsetStart + (offset[i] << 3));

			if (coder.context[FSCoder.DecodeGlyphs] == 1)
				shapes.add(new FSShape(coder));
			else
				shapes.add(new FSShape(coder, offset[i + 1] - offset[i]));
		}

		coder.endObject(name());
	}
}
