/*
 * Copyright (c) 1996-2001 Sun Microsystems, Inc. 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.
 *
 * - Redistribution 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 Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that Software is not designed,licensed or intended
 * for use in the design, construction, operation or maintenance of
 * any nuclear facility.
 */
// adapted from Text2D in package com.sun.j3d.utils.geometry;

import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.awt.image.DataBufferInt;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.util.Hashtable;

import javax.media.j3d.*;
import javax.vecmath.*;


/**
 * This class creates a texture-mapped rectangle which displays the
 * text string sent in by the user, given the appearance parameters
 * also supplied by the user.  The size of the rectangle (and its
 * texture map) is determined by the font parameters passed in to
 * the constructor.
 * The resulting Shape3D object is a transparent (except for the text)
 * rectangle located at (0, 0, 0) and extending up the positive y-axis and
 * out the positive x-axis.
 */
public class OrientedText2D extends OrientedShape3D {

    // This table caches FontMetrics objects to avoid the huge cost
    // of re-retrieving metrics for a font we've already seen.
    private static Hashtable metricsTable = new Hashtable(); 
    float rectangleScaleFactor = 1f/256f;

    Color3f   color = new Color3f();
    float     height;
    String    fontName;
    int       fontSize, fontStyle;
    Point3f   textPt = new Point3f(); 
    BufferedImage bImage; 	   // the image for the text
    int	      imgWidth, imgHeight; // size of the text image
    int	      imgAscent, imgDescent;// offset above and below height
    int	      texWidth, texHeight; // size of texture holding image

    String text;


    /**
     * Creates a Shape3D object which holds a
     * rectangle that is texture-mapped with an image that has
     * the specified text written with the specified font
     * parameters.
     *
     * @param text The string to be written into the texture map.
     * @param color The color of the text string.
     * @param fontName The name of the Java font to be used for
     *  the text string.
     * @param fontSize The size of the Java font to be used.
     * @param fontStyle The style of the Java font to be used.
     */
    public OrientedText2D(String text, Point3f textPt, float height, 
		Color3f color, String fontName, int fontSize, int fontStyle) {

	this.textPt.set(textPt);
	this.text = text;
	this.height = height;
        this.color.set(color);
        this.fontName = fontName;
        this.fontSize = fontSize;
        this.fontStyle = fontStyle;

        updateText2D(text, color, fontName, fontSize, fontStyle);
    }

    public OrientedText2D(String text, Color3f color, String fontName, 
		int fontSize, int fontStyle) {
	this.text = text;
	this.textPt.set(0.0f, 0.0f, 0.0f);
	this.height = fontSize * rectangleScaleFactor;
        this.color.set(color);
        this.fontName = fontName;
        this.fontSize = fontSize;
        this.fontStyle = fontStyle;

        updateText2D(text, color, fontName, fontSize, fontStyle);
    }

    /*
     * Changes text of this Text2D to 'text'. All other
     * parameters (color, fontName, fontSize, fontStyle
     * remain the same.
     * @param text The string to be set.
     */
    public void setString(String text){
	this.text = text;
        updateText2D(text, color, fontName, fontSize, fontStyle);
    }

    private void updateText2D(String text, Color3f color, String fontName,
                  int fontSize, int fontStyle) {
        setupImage(text, color, fontName, fontSize, fontStyle);

        Texture2D t2d = setupTexture(bImage);

        QuadArray rect = setupGeometry();

        Appearance appearance = setupAppearance(t2d);

        setGeometry(rect);
        setAppearance(appearance);
    }


    /**
     * Sets the scale factor used in converting the image width/height
     * to width/height values in 3D.
     *
     * @param newScaleFactor The new scale factor.
     */
    public void setRectangleScaleFactor(float newScaleFactor) {
	rectangleScaleFactor = newScaleFactor;
	height = rectangleScaleFactor * fontSize;
    }

    /**
     * Gets the current scale factor being used in converting the image
     * width/height to width/height values in 3D.
     *
     * @return The current scale factor.
     */
    public float getRectangleScaleFactor() {
	return rectangleScaleFactor;
    }
    
    /**
     * Create the ImageComponent and Texture object.
     */
    private Texture2D setupTexture(BufferedImage bImage) {

	ImageComponent imageComponent =
	    new ImageComponent2D(ImageComponent.FORMAT_RGBA, 
				 bImage);
	Texture2D t2d = new Texture2D(Texture2D.BASE_LEVEL,
				      Texture.RGBA,
				      bImage.getWidth(),
				      bImage.getHeight());
	t2d.setMinFilter(t2d.BASE_LEVEL_LINEAR);
	t2d.setMagFilter(t2d.BASE_LEVEL_LINEAR);
	t2d.setImage(0, imageComponent);
	t2d.setEnable(true);

	return t2d;
    }

    /**
     * Creates a bImage, a BufferedImage of the correct dimensions for the
     * given font attributes.  Draw the given text into the image in
     * the given color.  The background of the image is transparent
     * (alpha = 0). Initializes imgWidth, imgHeight, texWidth, texHeight to
     * the image size and texture size for the image.
     */
    private void setupImage(String text, Color3f color,
				     String fontName,
				     int fontSize, int fontStyle) {
	Toolkit toolkit = Toolkit.getDefaultToolkit();
	Font font = new java.awt.Font(fontName, fontStyle, fontSize);

	FontMetrics metrics;
	if ((metrics = (FontMetrics)metricsTable.get(font)) == null) {
	    metrics = toolkit.getFontMetrics(font);
	    metricsTable.put(font, metrics);
	}
	imgDescent = metrics.getMaxDescent();
	imgAscent = metrics.getMaxAscent();

	imgWidth = metrics.stringWidth(text);
	imgHeight = imgDescent + imgAscent;

	System.out.println("fontSize = " + fontSize);
	System.out.println("imgDescent = " + imgDescent);
	System.out.println("imgAscent = " + imgAscent);
	System.out.println("imgHeight = " + imgHeight);

	// Need to make width/height powers of 2 because of Java3d texture
	// size restrictions
	int pow = 1;
	for (int i = 1; i < 32; ++i) {
	    pow *= 2;
	    if (imgWidth <= pow)
		break;
	}
	texWidth = pow;
	pow = 1;
	for (int i = 1; i < 32; ++i) {
	    pow *= 2;
	    if (imgHeight <= pow)
		break;
	}
	texHeight = pow;

	// For now, jdk 1.2 only handles ARGB format, not the RGBA we want
	bImage = new BufferedImage(texWidth, texHeight,
						 BufferedImage.TYPE_INT_ARGB);
	Graphics offscreenGraphics = bImage.createGraphics();

	// First, erase the background to the text panel - set alpha to 0
	Color myFill = new Color(0f, 0f, 0f, 0f);
	offscreenGraphics.setColor(myFill);
	offscreenGraphics.fillRect(0, 0, imgWidth, imgHeight);

	// Next, set desired text properties (font, color) and draw String
	offscreenGraphics.setFont(font);
	Color myTextColor = new Color(color.x, color.y, color.z, 1f);
	offscreenGraphics.setColor(myTextColor);
	offscreenGraphics.drawString(text, 0, texHeight - imgDescent);
    }

    /**
     * Creates a rectangle of the given width and height and sets up
     * texture coordinates to map the text image onto the whole surface
     * of the rectangle (the rectangle is the same size as the text image)

height = 2.0
fontSize = 12
imgHeight = 16
imgDescent = 4
rectHeight = 2.6666667
rectDescent = 0.5
rectWidth = 4.5


     */
    private QuadArray setupGeometry() {
	float rectDescent = height * imgDescent / fontSize;
	float rectHeight = height * (imgHeight - imgDescent) / fontSize;
	float rectWidth = 
		height * imgWidth / fontSize;
	float[] verts1 = {
	    textPt.x, 		textPt.y-rectDescent, 	textPt.z,
	    rectWidth+textPt.x, textPt.y-rectDescent, 	textPt.z,
	    rectWidth+textPt.x, textPt.y+rectHeight, 	textPt.z,
	    textPt.x, 		textPt.y+rectHeight, 	textPt.z
	};
	float[] texCoords = {
	    0f, 0f,
	    1f * imgWidth / texWidth, 0f,
	    1f * imgWidth / texWidth, 1f * imgHeight / texHeight,
	    0f, 1f * imgHeight / texHeight
	};
	
	QuadArray rect = new QuadArray(4, QuadArray.COORDINATES |
				       QuadArray.TEXTURE_COORDINATE_2);
	rect.setCoordinates(0, verts1);
	rect.setTextureCoordinates(0, 0, texCoords);
	
	return rect;
    }

    /**
     * Creates Appearance for this Shape3D.  This sets transparency
     * for the object (we want the text to be "floating" in space,
     * so only the text itself should be non-transparent.  Also, the
     * appearance disables lighting for the object; the text will
     * simply be colored, not lit.
     */
    private Appearance setupAppearance(Texture2D t2d) {
	TransparencyAttributes transp = new TransparencyAttributes();
	transp.setTransparencyMode(TransparencyAttributes.BLENDED);
	transp.setTransparency(0f);
	Appearance appearance = new Appearance();
	appearance.setTransparencyAttributes(transp);
	appearance.setTexture(t2d);

	Material m = new Material();
	m.setLightingEnable(false);
	appearance.setMaterial(m);
	
	return appearance;
    }

    /**
     * Returns the text string
     *
     * @since Java 3D 1.2.1
     */
    public String getString() {
	return text;
    }

    /**
     * Returns the color of the text
     *
     * @since Java 3D 1.2.1
     */
    public Color3f getColor() {
	return color;
    }

    /**
     * Returns the font
     *
     * @since Java 3D 1.2.1
     */
    public String getFontName() {
	return fontName;
    }

    /**
     * Returns the font size
     *
     * @since Java 3D 1.2.1
     */
    public int getFontSize() {
	return fontSize;
    }
    
    /**
     * Returns the font style
     *
     * @since Java 3D 1.2.1
     */
    public int getFontStyle() {
	return fontStyle;
    }
    
}





