package org.xith3d.shapegeom;

import org.xith3d.shapegeom.Char3D;

import java.util.HashMap;
import java.awt.*;

/**
 * <p>
 * A class to cache 3d representations of characters. This may be faster than creating new characters each time.
 * A Char3D is created for each character passed in the parameter indexedChars. These are then stored in a HashMap.
 * A copy of each char can be retrieved using the getCharacter(c) method. If the character is not already indexed
 * it will be created and indexed for further use.  
 *
 * Date: 26/04/2005
 * @author Alistair Dickie
 */
public class CachedText3D {
  private HashMap charMap;
  private String indexedChars;
  private Font font;
  private double flatness;
  private int vertexFormat;
  private float depth;
  

  public CachedText3D(String indexedChars, Font font, double flatness, int vertexFormat, float depth) {
    this.indexedChars = indexedChars;
    this.font = font;
    this.flatness = flatness;
    this.vertexFormat = vertexFormat;
    this.depth = depth;
    char[] chars = indexedChars.toCharArray();
    charMap = new HashMap();
    for (int i = 0; i < chars.length; i++) {
      char c = chars[i];
      Char3D c3d = new Char3D(font,c,flatness,vertexFormat,depth);
      charMap.put(new Character(c),c3d);
    }
  }

  public Char3D getCharacter(char c){
    Object ret = charMap.get(new Character(c));
    if(ret == null){
      ret = new Char3D(font,c,flatness,vertexFormat,depth);
      charMap.put(new Character(c),ret);
    }
    Char3D aChar = (Char3D)ret;
    return new Char3D(aChar);
  }

  public String getIndexedChars() {
    return indexedChars;
  }

}
