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;
22  
23  import java.io.UnsupportedEncodingException;
24  
25  import org.gnu.jfiglet.core.FIGCharacter;
26  import org.gnu.jfiglet.core.FIGFont;
27  import org.gnu.jfiglet.core.FIGFontLayout;
28  import org.gnu.jfiglet.core.FIGure;
29  
30  /***
31   * A class that draws FIGures...
32   * @TODO : make it an helper class ?
33   * 
34   * @version $Id: FIGDriver.java,v 1.7 2004/05/11 19:23:01 sbrunot Exp $
35   *
36   * @author <a href="mailto:sebastien.brunot@club-internet.fr">Sebastien Brunot</a>
37   * 
38   */
39  public class FIGDriver
40  {
41      //////////////////////////////////////
42      // Constants
43      //////////////////////////////////////
44  
45      /***
46       * LATIN 1 Charset name
47       */
48      public static final String LATIN_1_CHARSET = "ISO-8859-1";
49  
50      //////////////////////////////////////
51      // Constructor
52      //////////////////////////////////////
53  
54      /***
55       * No args constructor
56       */
57      public FIGDriver()
58      {
59          super();
60      }
61  
62      //////////////////////////////////////
63      // Public methods
64      //////////////////////////////////////
65  
66      /***
67       * @TODO : DOCUMENT ME !
68       * Use the LATIN 1 Charset
69       */
70      public FIGure getBannerFIGure(
71          String theStringToFigletize,
72          FIGFont theFIGFontToUse) throws UnsupportedEncodingException
73      {
74          FIGure returnedFIGure = new FIGure();
75          byte[] theStringToFigletizeCharacterCodes =
76              theStringToFigletize.getBytes(LATIN_1_CHARSET);
77          addLineToFIGure(
78              returnedFIGure,
79              theStringToFigletizeCharacterCodes,
80              theFIGFontToUse);
81          return returnedFIGure;
82      }
83  
84      /***
85       * @TODO : DOCUMENT ME !
86       */
87      public void addLineToFIGure(
88          FIGure theFIGure,
89          byte[] theLineContent,
90          FIGFont theFIGFontToUse)
91      {
92          // Validate arguments
93          if ((theLineContent == null)
94              || (theFIGFontToUse == null)
95              || (theFIGure == null))
96          {
97              throw new IllegalArgumentException("The FIGure, the line content and the FIGFont to use musn't be null.");
98          }
99          FIGFontLayout layout = theFIGFontToUse.getInfo().getLayout();
100         String[] newLine = null;
101         // Render the line with the specified horizontal layout mode
102         switch (layout.getHorizontalMode())
103         {
104             case FIGFontLayout.HORIZONTAL_LAYOUT_MODE_FULL_SIZE :
105                 newLine = getFullWidthLine(theLineContent, theFIGFontToUse);
106                 break;
107             case FIGFontLayout.HORIZONTAL_LAYOUT_MODE_FITTING_ONLY :
108                 newLine = getFittingLine(theLineContent, theFIGFontToUse);
109                 break;
110             default :
111                 // @TODO : add support for smushing
112                 newLine = getFittingLine(theLineContent, theFIGFontToUse);
113         }
114         // Add the new line to the figure
115         theFIGure.addFigCharactersLine(newLine);
116     }
117 
118     /***
119      * @TODO : DOCUMENT ME !
120      * @TODO : AUTHOR ONLY ONE RENDERING METHOD ?
121      * @param theLineContent
122      * @param theFIGFontToUse
123      * @return
124      */
125     private String[] getFullWidthLine(
126         byte[] theLineContent,
127         FIGFont theFIGFontToUse)
128     {
129         // Create an empty new line
130         String[] newLine = new String[theFIGFontToUse.getInfo().getHeight()];
131         // Initialize each subline with the empty String
132         for (int sublineNumber = 0;
133             sublineNumber < newLine.length;
134             sublineNumber++)
135         {
136             newLine[sublineNumber] = "";
137         }
138         // Render theLineContent as a line of FIGCharacter (layout = FULL WIDTH)
139         if (theLineContent.length > 0)
140         {
141             for (int sublineNumber = 0;
142                 sublineNumber < newLine.length;
143                 sublineNumber++)
144             {
145                 for (int currentCharIndex = 0;
146                     currentCharIndex < theLineContent.length;
147                     // @TODO : ERROR IF EMPTY CONTENT HERE ?
148                 currentCharIndex++)
149                 {
150                     // Get the code of the current character
151                     Byte currentCharacterCode =
152                         new Byte(theLineContent[currentCharIndex]);
153                     // Get the FIGCharacter for this code
154                     FIGCharacter currentFIGCharacter =
155                         theFIGFontToUse.getFIGCharacter(
156                             currentCharacterCode.intValue());
157                     // @TODO : What if the FIGCharacter is null (undefined) ? If there is an exception ?
158                     String currentFIGCharacterLine =
159                         currentFIGCharacter.getCharacterLine(sublineNumber);
160                     newLine[sublineNumber]
161                         += currentFIGCharacterLine.substring(
162                             0,
163                             (currentFIGCharacterLine.length()));
164                 }
165                 // Replace all hardblank character with the space character
166                 newLine[sublineNumber] =
167                     newLine[sublineNumber].replace(
168                         theFIGFontToUse.getInfo().getHardblank(),
169                         ' ');
170             }
171         }
172         return newLine;
173     }
174 
175     /***
176      * @TODO : DOCUMENT ME !
177      * @TODO : AUTHOR ONLY ONE RENDERING METHOD ?
178      * @param theLineContent
179      * @param theFIGFontToUse
180      * @return
181      */
182     private String[] getFittingLine(
183         byte[] theLineContent,
184         FIGFont theFIGFontToUse)
185     {
186         // Create an empty new line of FIGCharacters
187         String[] newLine = new String[theFIGFontToUse.getInfo().getHeight()];
188         // Initialize each subline with the empty String
189         for (int sublineNumber = 0;
190             sublineNumber < newLine.length;
191             sublineNumber++)
192         {
193             newLine[sublineNumber] = "";
194         }
195         // Render theLineContent as a line of FIGCharacter (layout = FITTING)
196         if (theLineContent.length > 0)
197         {
198             // Put the first FIGCharacter int the new line
199             FIGCharacter lastFIGCharacterInNewLine =
200                 theFIGFontToUse.getFIGCharacter(
201                     new Byte(theLineContent[0]).intValue());
202             for (int lineNumber = 0; lineNumber < newLine.length; lineNumber++)
203             {
204                 newLine[lineNumber] =
205                     lastFIGCharacterInNewLine.getCharacterLine(lineNumber);
206             }
207             // Do the same for each character, fitting
208             for (int charIndex = 1;
209                 charIndex < theLineContent.length;
210                 charIndex++)
211             {
212                 FIGCharacter figCharacterToPutInNewLine =
213                     theFIGFontToUse.getFIGCharacter(
214                         new Byte(theLineContent[charIndex]).intValue());
215                 // Calculate how many spaces should be trimed in each line
216                 // between the last FIGCharacter and this one
217                 int numberOfSpacesToTrim = 0;
218                 for (int lineNumber = 0;
219                     lineNumber < newLine.length;
220                     lineNumber++)
221                 {
222                     int numberOfSpacesBetweenCharsInThisLine =
223                         lastFIGCharacterInNewLine.getNumberOfBackSpacesForLine(
224                             lineNumber)
225                             + figCharacterToPutInNewLine
226                                 .getNumberOfFrontSpacesForLine(
227                                 lineNumber);
228                     if ((numberOfSpacesToTrim == 0)
229                         || (numberOfSpacesBetweenCharsInThisLine
230                             < numberOfSpacesToTrim))
231                     {
232                         numberOfSpacesToTrim =
233                             numberOfSpacesBetweenCharsInThisLine;
234                     }
235                 }
236                 // Put the FIGCharacter in the new line, triming the number of spaces
237                 // calculated just before
238                 for (int lineNumber = 0;
239                     lineNumber < newLine.length;
240                     lineNumber++)
241                 {
242                     int numberOfSpacesToTrimInFrontOfNewChar =
243                         Math.min(
244                             figCharacterToPutInNewLine
245                                 .getNumberOfFrontSpacesForLine(
246                                 lineNumber),
247                             numberOfSpacesToTrim);
248                     int numberOfSpacesToTrimAtEndOfNewLine =
249                         numberOfSpacesToTrim
250                             - numberOfSpacesToTrimInFrontOfNewChar;
251                     newLine[lineNumber] =
252                         newLine[lineNumber].substring(
253                             0,
254                             (newLine[lineNumber].length()
255                                 - numberOfSpacesToTrimAtEndOfNewLine))
256                             + figCharacterToPutInNewLine.getCharacterLine(
257                                 lineNumber).substring(
258                                 numberOfSpacesToTrimInFrontOfNewChar);
259                 }
260                 // Set the last character in new line to the character we just
261                 // put in the new line
262                 lastFIGCharacterInNewLine = figCharacterToPutInNewLine;
263             }
264             // replace hardblank with space
265             for (int lineNumber = 0; lineNumber < newLine.length; lineNumber++)
266             {
267                 newLine[lineNumber] =
268                     newLine[lineNumber].replace(
269                         theFIGFontToUse.getInfo().getHardblank(),
270                         ' ');
271             }
272         }
273         return newLine;
274     }
275 
276 }