/*
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 *
 * "The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations under
 * the License.
 *
 * The Original Code is ICEpdf 3.0 open source software code, released
 * May 1st, 2009. The Initial Developer of the Original Code is ICEsoft
 * Technologies Canada, Corp. Portions created by ICEsoft are Copyright (C)
 * 2004-2009 ICEsoft Technologies Canada, Corp. All Rights Reserved.
 *
 * Contributor(s): _____________________.
 *
 * Alternatively, the contents of this file may be used under the terms of
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"
 * License), in which case the provisions of the LGPL License are
 * applicable instead of those above. If you wish to allow use of your
 * version of this file only under the terms of the LGPL License and not to
 * allow others to use your version of this file under the MPL, indicate
 * your decision by deleting the provisions above and replace them with
 * the notice and other provisions required by the LGPL License. If you do
 * not delete the provisions above, a recipient may use your version of
 * this file under either the MPL or the LGPL License."
 *
 */
package com.softwareimaging.jpdf.core.pobjects.graphics;

import android.graphics.RectF;

import com.softwareimaging.android.graphics.Canvas;
import com.softwareimaging.android.graphics.Color;
import com.softwareimaging.android.graphics.Paint;
import com.softwareimaging.android.graphics.RectangleF;
import com.softwareimaging.jpdf.core.pobjects.fonts.FontFile;
import com.softwareimaging.jpdf.core.util.PDFPrintException;

/**
 * <p>
 * This class represents text which will be rendered to the a graphics context.
 * This class was created to act as a wrapper for painting text using the
 * Phelphs font library as well as painting using java.awt.Font.
 * </p>
 * <p/>
 * <p>
 * Objects of this type are created by the content parser when "TJ" or "Tj"
 * operands are encountered in a page's content stream. Each TextSprite object
 * is comprised of a list of CharSprites which can be painted by the Shapes
 * class at render time.
 * </p>
 * 
 * @since 2.0
 */
public class TextSprite {

	// private Vector textObjects;
	private float[][] textObjects;
	private int count;
	// stroke color
	private Color strokeColor;
	// the write
	private int rmode;
	// Font used to paint text
	private FontFile font;

	/**
	 * <p>
	 * Creates a new TextSprit object.
	 * </p>
	 * 
	 * @param font
	 *            font used when painting glyphs.
	 * @param size
	 *            size of the font in user space
	 */
	public TextSprite(FontFile font, int size) {
		textObjects = new float[size][3];
		this.font = font;
	}

	/**
	 * <p>
	 * Adds a new text char to the TextSprite which will pe painted at x, y
	 * under the current CTM
	 * </p>
	 * 
	 * @param character
	 *            character to paint.
	 * @param x
	 *            x-coordinate to paint.
	 * @param y
	 *            y-coordinate to paint.
	 * @param width
	 *            width of character from font.
	 */
	public void addText(char character, float x, float y, float width) {
		textObjects[count][0] = x;
		textObjects[count][1] = y;
		textObjects[count][2] = (int) character;
		count++;
	}

	/**
	 * <p>
	 * Set the rmode for all the characters being in this object. Rmode can have
	 * the following values:
	 * </p>
	 * <ul>
	 * <li>0 - Fill text.</li>
	 * <li>1 - Stroke text.</li>
	 * <li>2 - fill, then stroke text.</li>
	 * <li>3 - Neither fill nor stroke text (invisible).</li>
	 * <li>4 - Fill text and add to path for clipping.</li>
	 * <li>5 - Stroke text and add to path for clipping.</li>
	 * <li>6 - Fill, then stroke text and add to path for clipping.</li>
	 * <li>7 - Add text to path for clipping.</li>
	 * </ul>
	 * 
	 * @param rmode
	 *            valid rmode from 0-7
	 */
	public void setRMode(int rmode) {
		if (rmode >= 0) {
			this.rmode = rmode;
		}
	}

	public String toString() {
		StringBuffer text = new StringBuffer(textObjects.length);
		for (int i = 0, max = textObjects.length; i < max; i++) {
			text.append(textObjects[i][2]);
		}
		return text.toString();
	}

	public void setStrokeColor(Color color) {
		strokeColor = color;
	}

	/**
	 * <p>
	 * Gets the list of textObjects.
	 * </p>
	 * 
	 * @return list of all CharSprites contained in this TextSprite.
	 */
	public float[][] getTextObjects() {
		return textObjects;
	}

	/**
	 * <p>
	 * Gets the size value in user space associated with this TextSprite.
	 * </p>
	 * 
	 * @return size of font in user space.
	 */
	public int getSize() {
		return textObjects.length;
	}

	/**
	 * <p>
	 * Paints all the character elements in this TextSprite to the graphics
	 * context
	 * </p>
	 * 
	 * @param g
	 *            graphics context to which the characters will be painted to.
	 * @param paint
	 *            paint object to use.
	 * @throws PDFPrintException
	 */
	public void paint(Canvas g, Paint paint) throws PDFPrintException {
		for (int i = textObjects.length - 1; i >= 0; i--) {
			Color color = (strokeColor != null ? strokeColor : new Color(paint.getColor()));

			// draw glyph
			font.drawEstring(g, paint, String.valueOf((char) textObjects[i][2]), textObjects[i][0],
					textObjects[i][1], FontFile.LAYOUT_NONE, rmode, color);
		}

	}

	/**
	 * Dispose this TextSprite Object.
	 */
	public void dispose() {
		textObjects = null;
		strokeColor = null;
		font = null;
	}
}
