/*
 * {{{ header & license
 * Copyright (c) 2004, 2005 Joshua Marinacci
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License
 * as published by the Free Software Foundation; either version 2.1
 * of the License, or (at your option) any later version.
 *
 * This program 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * }}}
 */
package org.xhtmlrenderer.layout;

import org.xhtmlrenderer.css.constants.CSSName;
import org.xhtmlrenderer.css.constants.IdentValue;
import org.xhtmlrenderer.css.style.CalculatedStyle;
import org.xhtmlrenderer.util.Uu;


/**
 * Description of the Class
 *
 * @author   empty
 */
public class TextUtil {

    /**
     * Description of the Method
     *
     * @param text   PARAM
     * @param style
     * @return       Returns
     */
    public static String transformText( String text, CalculatedStyle style ) {
        IdentValue transform = style.getIdent( CSSName.TEXT_TRANSFORM );
        if ( transform == IdentValue.LOWERCASE ) {
            text = text.toLowerCase();
        }
        if ( transform == IdentValue.UPPERCASE ) {
            text = text.toUpperCase();
        }
        if ( transform == IdentValue.CAPITALIZE ) {
            text = capitalizeWords( text );
        }
        IdentValue fontVariant = style.getIdent( CSSName.FONT_VARIANT );
        if ( fontVariant == IdentValue.SMALL_CAPS ) {
            text = text.toUpperCase();
        }
        return text;
    }


    /**
     * Description of the Method
     *
     * @param text  PARAM
     * @return      Returns
     */
    private static String capitalizeWords( String text ) {
        //Uu.p("start = -"+text+"-");
        if ( text.length() == 0 ) {
            return text;
        }

        StringBuffer sb = new StringBuffer();
        //Uu.p("text = -" + text + "-");

        // do first letter
        //Uu.p("first = " + text.substring(0,1));
        boolean cap = true;
        for ( int i = 0; i < text.length(); i++ ) {
            String ch = text.substring( i, i + 1 );
            //Uu.p("ch = " + ch + " cap = " + cap);


            if ( cap ) {
                sb.append( ch.toUpperCase() );
            } else {
                sb.append( ch );
            }
            cap = false;
            if ( ch.equals( " " ) ) {
                cap = true;
            }
        }

        //Uu.p("final = -"+sb.toString()+"-");
        if ( sb.toString().length() != text.length() ) {
            Uu.p( "error! to strings arent the same length = -" + sb.toString() + "-" + text + "-" );
        }
        return sb.toString();
    }

}

/*
 * $Id: TextUtil.java,v 1.11 2007/02/07 16:33:33 peterbrant Exp $
 *
 * $Log: TextUtil.java,v $
 * Revision 1.11  2007/02/07 16:33:33  peterbrant
 * Initial commit of rewritten table support and associated refactorings
 *
 * Revision 1.10  2005/01/29 20:18:41  pdoubleya
 * Clean/reformat code. Removed commented blocks, checked copyright.
 *
 * Revision 1.9  2005/01/24 22:46:43  pdoubleya
 * Added support for ident-checks using IdentValue instead of string comparisons.
 *
 * Revision 1.8  2004/12/12 03:32:59  tobega
 * Renamed x and u to avoid confusing IDE. But that got cvs in a twist. See if this does it
 *
 * Revision 1.7  2004/12/06 02:55:43  tobega
 * More cleaning of use of Node, more preparation for Content-based inline generation.
 *
 * Revision 1.6  2004/12/05 00:48:58  tobega
 * Cleaned up so that now all property-lookups use the CalculatedStyle. Also added support for relative values of top, left, width, etc.
 *
 * Revision 1.5  2004/11/22 21:34:03  joshy
 * created new whitespace handler.
 * new whitespace routines only work if you set a special property. it's
 * off by default.
 *
 * turned off fractional font metrics
 *
 * fixed some bugs in Uu and Xx
 *
 * - j
 *
 * Issue number:
 * Obtained from:
 * Submitted by:
 * Reviewed by:
 *
 * Revision 1.4  2004/11/08 21:18:21  joshy
 * preliminary small-caps implementation
 *
 * Issue number:
 * Obtained from:
 * Submitted by:
 * Reviewed by:
 *
 * Revision 1.3  2004/10/23 13:46:48  pdoubleya
 * Re-formatted using JavaStyle tool.
 * Cleaned imports to resolve wildcards except for common packages (java.io, java.util, etc).
 * Added CVS log comments at bottom.
 *
 *
 */

