/*Copyright 2011 indiandragon

This file is part of j2me2AB.

J2meAB is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

j2me2AB is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with j2me2AB.  If not, see <http://www.gnu.org/licenses/>.
*/

package javax.microedition.lcdui;

import android.graphics.Paint;
import android.graphics.Typeface;

public final class Font extends Object {
	public static final int FACE_MONOSPACE = 32;
	public static final int FACE_PROPORTIONAL = 64;
	public static final int FACE_SYSTEM = 0;
	public static final int FONT_INPUT_TEXT = 1;
	public static final int FONT_STATIC_TEXT = 0;
	
	public static final int SIZE_LARGE = 32;
	public static final int SIZE_MEDIUM = 16;
	public static final int SIZE_SMALL = 8;
	public static final int STYLE_BOLD = 1;
	public static final int STYLE_ITALIC = 2;
	public static final int STYLE_PLAIN = 0;
	public static final int STYLE_UNDERLINED = 4;

	private static Font defaultFont;
	public Typeface typeface;
	private int face;
	private int style;
	private int size;
	private int height;
	private int baselinePosition;
	private Paint paint;

	private Font(int face, int style) {
		this.face = face;
		this.style = style;
		if ((style == STYLE_PLAIN)
				&& (face == FACE_SYSTEM || face == FACE_PROPORTIONAL)) {
			this.typeface = UIGlobal.APP_TYPEFACE;
		} else {
			int androidStyle = Typeface.NORMAL;
			if ((style & STYLE_BOLD) == STYLE_BOLD) {
				if ((style & STYLE_ITALIC) == STYLE_ITALIC) {
					androidStyle = Typeface.BOLD_ITALIC;
				} else {
					androidStyle = Typeface.BOLD;
				}
			} else if ((style & STYLE_ITALIC) == STYLE_ITALIC) {
				androidStyle = Typeface.ITALIC;
			}
			Typeface family = Typeface.DEFAULT;
                        family =Typeface.SERIF;
			this.typeface = Typeface.create(family, androidStyle);
		}
	}

	private Font(int face, int style, int size) {

		this(face, style);
		this.size = size;

		double factor;
		if (size == SIZE_SMALL) {
			factor = 1.5f;
		} else if (size == SIZE_MEDIUM) {
			factor = 1.85f;
		} else {
			factor = 2.35f;
		}
		float ppFactor = 2.35f;
		factor = factor * ppFactor;
		this.paint = new Paint();
		float androidSize = (float) (this.paint.getTextSize() * (factor));
		this.height = (int) androidSize;
		initPaint(this.paint);
		int decent = this.paint.getFontMetricsInt().descent;
		this.baselinePosition = this.height - decent;
	}

	public int charsWidth(char[] ch, int offset, int length) {
		return (int) this.paint.measureText(new String(ch), offset, length
				+ offset);
	}

	public int charWidth(char ch) {
		return (int) this.paint.measureText("" + ch);
	}

	public static Font getFont(int fontSpecifier) {
		return getDefaultFont();
	}

	public static Font getFont(int face, int style, int size) {
		return new Font(face, style, size);
	}

	public int getBaselinePosition() {
		return this.baselinePosition;
	}

	public static Font getDefaultFont() {
		if (defaultFont == null) {
			try {
				
                          defaultFont =  Font.getFont(Font.FACE_SYSTEM, Font.STYLE_ITALIC, Font.SIZE_MEDIUM);
			
                        } catch (Exception e) {
				System.out.println("Unable to create a default font");
			}
		}
		return defaultFont;
	}

	public int getFace() {
		return this.face;
	}

	public int getHeight() {
		return this.height;
	}

	public int getSize() {
		return this.size;
	}

	public int getStyle() {
		return this.style;
	}

	public boolean isPlain() {
		return (this.style == STYLE_PLAIN);
	}

	public boolean isBold() {
		return (this.style & STYLE_BOLD) == STYLE_BOLD;
	}

	public boolean isItalic() {
		return (this.style & STYLE_ITALIC) == STYLE_ITALIC;
	}

	public boolean isUnderlined() {
		return (this.style & STYLE_UNDERLINED) == STYLE_UNDERLINED;
	}

	public int stringWidth(String str) {
		float measureText = this.paint.measureText(str);
		return (int) measureText;
	}

	public int substringWidth(String str, int offset, int len) {
		return (int) this.paint.measureText(str, offset, len + offset);
	}

	public void initPaint(Paint p) {
		p.setTypeface(this.typeface);
		p.setUnderlineText(isUnderlined());
		if (isItalic()) {
			p.setTextSkewX(-0.25f);
		} else {
			p.setTextSkewX(0f);
		}
		p.setTextSize(this.height);
	}

}
