/*
 * Copyright (c) 2006-2008 MiniMe. Code released under The MIT/X Window System
 * License. Full license text can be found in license.txt
 */
package minime;

import java.util.Vector;

import minime.Font;

/**
 * The TextPanelUtil class is a utility class used to handle text for text
 * panel.
 * 
 * @author yishu
 * 
 */
public class TextPanelUtil
{

    private final static int MARGIN = 10;

    /**
     * Calculates how many rows the string can be slit with given width and
     * font. Each row string's width is the given width, the last row string
     * width may be less then this given width.
     * 
     * @param font
     *            the font instance
     * @param width
     *            the width, the string based on this width to slit
     * @param s
     *            the string to be slit
     * @return the number of rows
     */
    public static int slitRow(Font font, int width, String s)
    {
        return font.getStringWidth(s) / width + 1;
    }

    /**
     * Slit the string into an array, Each row string's width is the given
     * width, the last row string width may be less then this given width.
     * 
     * @param font
     *            the font instance
     * @param text
     *            the string to be slit
     * @param width
     *            the width, the string based on this width to slit
     * @return the array of string after slit
     */
    public static String[] slitStrIntoArrayStr(Font font, String text, int width)
    {
        Vector vector = new Vector();
        String[] slitArray = null;
        StringBuffer sb = new StringBuffer();
        char[] ch = text.toCharArray();
        int w = 0;
        for (int i = 0; i < ch.length; i++)
        {
            if (ch[i] == '\n')
            { // newline
                vector.addElement(sb.toString());
                sb.delete(0, sb.length());
                w = 0;
            }
            else
            {
                w = w + font.getCharWidth(ch[i]);
                sb.append(ch[i]);
                if (w >= width - MARGIN)
                {
                    vector.addElement(sb.toString());
                    sb.delete(0, sb.length());
                    w = 0;
                }
            }
        }
        if(sb.length() != 0){
        	vector.addElement(sb.toString());
        }
        slitArray = new String[vector.size()];
        vector.copyInto(slitArray);
        vector.removeAllElements();
        vector = null;
        return slitArray;
    }

    /**
     * If the text width is less than the given width, return the text,
     * otherwise return the substring whose width is the given width.
     * 
     * @param font
     *            the font instance
     * @param text
     *            the string to be slit
     * @param width
     *            the width, the string based on this width to slit
     * @return the substring whose width is the given width.
     */
    public static String subString(Font font, String text, int width)
    {
        if (font.getStringWidth(text) < width)
        {
            return text;
        }
        char[] ch = text.toCharArray();
        StringBuffer sb = new StringBuffer();
        int w = 0;
        for (int i = 0; i < ch.length; i++)
        {
            w = w + font.getCharWidth(ch[i]);
            sb.append(ch[i]);
            if (w >= width)
            {
                return sb.toString();
            }
        }
        return sb.toString();
    }

    /**
     * calculate how many rows the string can be displayed
     * 
     * @param font
     * @param height
     *            the display area hight
     * @return the count of rows can be displayed
     */
    public static int calRowCount(Font font, int height)
    {
        return height / font.getHeight();
    }

}
