/*
 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
 */

package com.sun.rain.graphics;

import com.sun.glass.ui.Platform;
import com.sun.rain.shapes.Rectangle;

public class Font
{
    static native void _initIDs();
    static
    {
        System.loadLibrary("rain");
        _initIDs();
        Font.platform = Platform.DeterminePlatform();
    }
    
    public enum Type {PLAIN, BOLD, ITALIC};
    
    private long ptr;
    
    static String platform;

    final String name;
    final Type type;
    final float size;
    final Metrics metrics;
    
    native private long _createFont(String name, int type, float size, Metrics metrics);
    public Font(final String name, final Type type, final float size)
    {
        this.ptr = 0L;
        this.name = name;
        this.type = type;
        this.size = size;
        
        this.metrics = new Metrics(0, 0, size * 2 / 3, size, 0, 0, 0); // on Mac it's set in native code
        ptr = _createFont(name, 0, size, this.metrics); // need to support type
    }
    
    public String getName()
    {
        return this.name;
    }
    
    public Type getType()
    {
        return this.type;
    }
    
    public float getSize()
    {
        return this.size;
    }
    
    public Metrics getMetrics()
    {
        return this.metrics;
    }
    
    native private Rectangle _getBoundingBox(String string, Rectangle bbox);
    public Rectangle getBoundingBox(String string)
    {
        Rectangle rect = new Rectangle(0, 0, string.length() * metrics.getWidth(), metrics.getHeight());
        if (Font.platform == Platform.MAC)
        {
            _getBoundingBox(string, rect);
        }
        return rect;
    }
    
    public class Metrics
    {
        private float x, y, width, height, ascent, descent, leading;
        Metrics()
        {
            this(0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f);
        }
        Metrics(float x, float y, float width, float height, float ascent, float descent, float leading)
        {
            this.x = x;
            this.y = y;
            this.width = width;
            this.height = height;
            this.ascent = ascent;
            this.descent = descent;
            this.leading = leading;
        }
        public float getX()
        {
            return this.x;
        }
        public float getY()
        {
            return this.y;
        }
        public float getWidth()
        {
            return this.width;
        }
        public float getHeight()
        {
            return this.height;
        }
        public float getAscent()
        {
            return this.ascent;
        }
        public float getDescent()
        {
            return this.descent;
        }
        public float getLeading()
        {
            return this.leading;
        }
        @Override
        public String toString()
        {
            return  "Metrics:"+"\n"+
                    "    x:"+getX()+"\n"+
                    "    y:"+getY()+"\n"+
                    "    width:"+getWidth()+"\n"+
                    "    height:"+getHeight()+"\n"+
                    "    ascent:"+getAscent()+"\n"+
                    "    descent:"+getDescent()+"\n"+
                    "    leading:"+getLeading()+"\n";
        }
    }
    
    @Override
    public String toString()
    {
        return  "Font:"+"\n"+
                "    ptr:"+this.ptr+"\n"+
                "    name:"+getName()+"\n"+
                "    type:"+getType()+"\n"+
                "    size:"+getSize()+"\n"+
                "    "+getMetrics();
    }
}
