001/* ----------------------------------------------------------------------------
002   The Kiwi Toolkit - A Java Class Library
003   Copyright (C) 1998-2004 Mark A. Lindner
004
005   This library is free software; you can redistribute it and/or
006   modify it under the terms of the GNU General Public License as
007   published by the Free Software Foundation; either version 2 of the
008   License, or (at your option) any later version.
009
010   This library is distributed in the hope that it will be useful,
011   but WITHOUT ANY WARRANTY; without even the implied warranty of
012   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013   General Public License for more details.
014
015   You should have received a copy of the GNU General Public License
016   along with this library; if not, write to the Free Software
017   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
018   02111-1307, USA.
019 
020   The author may be contacted at: mark_a_lindner@yahoo.com
021   ----------------------------------------------------------------------------
022   $Log: FontFormatter.java,v $
023   Revision 1.5  2004/05/05 22:47:37  markl
024   comment block updates
025
026   Revision 1.4  2003/01/19 09:34:27  markl
027   Javadoc & comment header updates.
028
029   Revision 1.3  2001/03/12 02:18:28  markl
030   Source code cleanup.
031
032   Revision 1.2  2000/07/30 10:44:56  markl
033   Added missing ',' to font format.
034
035   Revision 1.1  1999/06/08 07:01:53  markl
036   Initial revision
037   ----------------------------------------------------------------------------
038*/
039
040package kiwi.text;
041
042import java.awt.Font;
043
044import kiwi.util.*;
045
046/** This class provides methods for converting fonts to and from string
047  * representations.
048  *
049  * @see java.awt.Font
050  *
051  * @author Mark Lindner
052  */
053
054public class FontFormatter
055  {
056  private static final int styleCodes[]
057    = { Font.PLAIN, Font.BOLD, Font.ITALIC, (Font.BOLD | Font.ITALIC) };
058  private static final String styleNames[]
059    = { "Plain", "Bold", "Italic", "BoldItalic" };
060
061  /*
062   */
063  
064  private FontFormatter() {}
065
066  /*
067   */
068  
069  private static String nameForStyle(int style)
070    {
071    for(int i = 0; i < styleCodes.length; i++)
072      if(style == styleCodes[i])
073        return(styleNames[i]);
074
075    return(null);
076    }
077
078  /*
079   */
080  
081  private static int styleForName(String style)
082    {
083    for(int i = 0; i < styleCodes.length; i++)
084      if(style.equalsIgnoreCase(styleNames[i]))
085        return(styleCodes[i]);
086
087    return(-1);
088    }
089
090  /** Format a <code>Font</code> as a string. The format is "Name,Style,Size"
091   * where <i>Style</i> is one of <i>Plain</i>, <i>Bold</i>, <i>Italic</i>, or
092   * <i>BoldItalic</i>.
093   *
094   * @param font The <code>Font</code> to format.
095   * @return A string representation of the font.
096   * @see #parse
097   */
098   
099  public static String format(Font font)
100    {
101    String styleName = nameForStyle(font.getStyle());
102    if(styleName == null)
103      return(null);
104
105    return(font.getName() + "," + styleName + ","
106           + String.valueOf(font.getSize()));
107    }
108
109  /** Parse a font representation, returning an appropriate <code>Font</code>
110    * object.
111    *
112    * @param font The string representation of the font.
113    * @return An appropriate <code>Font</code> object.
114    * @exception kiwi.text.ParsingException If <code>font</code> is an
115    * invalid font representation.
116    *
117    * @see #format
118    */
119
120  public static Font parse(String font) throws ParsingException
121    {
122    String def[] = StringUtils.split(font, ",");
123    if(def.length != 3) return(null);
124    int styleCode = -1, sz = 0;
125
126    try
127      {
128      styleCode = styleForName(def[1]);
129      sz = Integer.parseInt(def[2]);
130      }
131    catch(NumberFormatException ex)
132      {
133      sz = 0;
134      }
135    
136    if((styleCode < 0) || (sz == 0))
137        throw(new ParsingException("Invalid Font specification."));
138
139    return(new Font(def[0], styleCode, sz));
140    }
141  
142  }
143
144/* end of source file */