View Javadoc

1   /* ========================================================================
2    * JFiglet, a free open source java implementation of figlet and the
3    * figfont specification (see http://www.figlet.org)
4    * Copyright (C) 2004 Sebastien Brunot
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2.1 of the License, or (at your option) any later version.
10   *
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14   * Lesser General Public License for more details.
15   *
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19   * ========================================================================
20   */
21  package org.gnu.jfiglet.core;
22  
23  /***
24   * A character defined by a FIGFont. For example, the character 'A' defined by
25   * the <em>alphabet</em> FIGFont is the following :
26   * <PRE>
27   *   AA
28   *  A  A
29   *  AAAA
30   *  A  A
31   *  A  A
32   * </PRE>
33   *
34   * @TODO : change statistics names
35   * 
36   * @version $Id: FIGCharacter.java,v 1.3 2004/05/02 15:24:48 sbrunot Exp $
37   *
38   * @author <a href="mailto:sebastien.brunot@club-internet.fr">
39   *         Sebastien Brunot</a>
40   * 
41   */
42  public class FIGCharacter
43  {
44      /////////////////////////////////////
45      // Attributes
46      /////////////////////////////////////
47  
48      /***
49       * The lines of subcharacters the character is defined by.
50       */
51      private String[] characterLines = null;
52  
53      /***
54       * @TODO : DOCUMENT ME !
55       */
56      private int[] numbersOfFrontSpaces = null;
57  
58      /***
59       * @TODO : DOCUMENT ME !
60       */
61      private int[] numbersOfBackSpaces = null;
62  
63      /////////////////////////////////////
64      // Constructor
65      /////////////////////////////////////
66  
67      /***
68       * A constructor which argument is an array of
69       * the lines of subcharacters the character is
70       * defined by. For example, the character 'A' in the
71       * <em>alphabet</em> FIGFont is defined by the seven
72       * following lines :
73       * <PRE>
74       * <em>line 0 :</em>"  AA  "
75       * <em>line 1 :</em>" A  A "
76       * <em>line 2 :</em>" AAAA "
77       * <em>line 3 :</em>" A  A "
78       * <em>line 4 :</em>" A  A "
79       * <em>line 5 :</em>"      "
80       * <em>line 6 :</em>"      "
81       * </PRE>
82       * If any &#64; subcharacters are present in a line, it
83       * is automaticaly suppressed : this is because characters
84       * definitions in FIGFont files are including final
85       * &#64; subcharacter at the end of each line.
86       * An IllegalArgumentException is thrown if theCharacterLines
87       * is empty or null.
88       * @param theCharacterLines an array of the lines
89       * of subcharacters the character is defined by.
90       */
91      public FIGCharacter(String[] theCharacterLines)
92      {
93          // verify parameter
94          if ((theCharacterLines == null) || (theCharacterLines.length == 0))
95          {
96              throw new IllegalArgumentException("the character lines musn't be null or empty");
97          }
98          this.characterLines = theCharacterLines;
99          // remove extra '@' subcharacters if any
100         trimArobase();
101         this.numbersOfFrontSpaces = new int[theCharacterLines.length];
102         this.numbersOfBackSpaces = new int[theCharacterLines.length];
103         // calculate numeric informations about the FIGCharacter
104         initFIGCharacterInformations();
105     }
106 
107     /////////////////////////////////////
108     // Accessors
109     /////////////////////////////////////
110 
111     /***
112      * Get an array of the lines of subcharacters the character
113      * is defined by. For example, the character 'A' in the
114      * <em>alphabet</em> FIGFont is defined by the seven
115      * following lines :
116      * <PRE>
117      * <em>line 0 :</em>"  AA  "
118      * <em>line 1 :</em>" A  A "
119      * <em>line 2 :</em>" AAAA "
120      * <em>line 3 :</em>" A  A "
121      * <em>line 4 :</em>" A  A "
122      * <em>line 5 :</em>"      "
123      * <em>line 6 :</em>"      "
124      * </PRE>
125      * @return an array of the lines of subcharacters the character
126      * is defined by.
127      */
128     public String[] getCharacterLines()
129     {
130         return this.characterLines;
131     }
132 
133     /***
134      * Get one of the lines of subcharacters the character is defined by.
135      * @param theLineNumber the line number, the first line beeing numbered
136      * 0. For example, the last line of subcharacters the character 'A' is
137      * defined by in the <em>alphabet</em> FIGFont is line number 6, and
138      * its value is :
139      * <PRE>
140      * <em>line 6 :</em>"      "
141      * </PRE>
142      * @return the line of subcharacters.
143      */
144     public String getCharacterLine(int theLineNumber)
145     {
146         try
147         {
148             return this.characterLines[theLineNumber];
149         }
150         catch (IndexOutOfBoundsException e)
151         {
152             throw new IllegalArgumentException();
153         }
154     }
155 
156     /***
157      * @TODO : DOCUMENT ME !
158      * @param theLineNumber
159      * @return
160      */
161     public int getNumberOfFrontSpacesForLine(int theLineNumber)
162     {
163         try
164         {
165             return this.numbersOfFrontSpaces[theLineNumber];
166         }
167         catch (IndexOutOfBoundsException e)
168         {
169             throw new IllegalArgumentException();
170         }
171     }
172 
173     /***
174      * @TODO : DOCUMENT ME !
175      * @param theLineNumber
176      * @return
177      */
178     public int getNumberOfBackSpacesForLine(int theLineNumber)
179     {
180         try
181         {
182             return this.numbersOfBackSpaces[theLineNumber];
183         }
184         catch (IndexOutOfBoundsException e)
185         {
186             throw new IllegalArgumentException();
187         }
188     }
189 
190     /***
191      * @TODO : DOCUMENT ME !
192      */
193     private void initFIGCharacterInformations()
194     {
195         // Init informations about FIGCharacter front
196         // and back spaces
197         for (int lineNumber = 0;
198             lineNumber < this.characterLines.length;
199             lineNumber++)
200         {
201             // Count the number of front spaces of this line
202             this.numbersOfFrontSpaces[lineNumber] = 0;
203             String currentLine = this.characterLines[lineNumber];
204             while ((this.numbersOfFrontSpaces[lineNumber]
205                 < currentLine.length())
206                 && (currentLine.charAt(this.numbersOfFrontSpaces[lineNumber])
207                     == ' '))
208             {
209                 this.numbersOfFrontSpaces[lineNumber]++;
210             }
211             // Count the number of back spaces of this line
212             int lengthOftrimedLine = currentLine.trim().length();
213             this.numbersOfBackSpaces[lineNumber] =
214                 currentLine.length()
215                     - lengthOftrimedLine
216                     - this.numbersOfFrontSpaces[lineNumber];
217         }
218     }
219 
220     /***
221      * TODO : DOCUMENT ME !
222      *
223      */
224     private void trimArobase()
225     {
226         for (int lineNumber = 0;
227             lineNumber < this.characterLines.length;
228             lineNumber++)
229         {
230             this.characterLines[lineNumber] =
231                 this.characterLines[lineNumber].replaceAll("@", "");
232         }
233     }
234 }