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.utils;
22  
23  import java.io.IOException;
24  import java.io.OutputStream;
25  import java.io.OutputStreamWriter;
26  import java.util.Iterator;
27  
28  import org.gnu.jfiglet.core.FIGFontInfo;
29  import org.gnu.jfiglet.core.FIGFontLayout;
30  import org.gnu.jfiglet.core.FIGure;
31  
32  /***
33   * An helper class that provides utilities methods.
34   * TODO : add a method to list available fonts ?
35   * TODO : add a method that calculate the length of a FIGure line
36   * in subcharacters, giving a String and a FIGFont ?
37   * 
38   * @version $Id: FIGUtils.java,v 1.2 2004/05/01 10:54:16 sbrunot Exp $
39   *
40   * @author <a href="mailto:sebastien.brunot@club-internet.fr">
41   *         Sebastien Brunot</a>
42   * 
43   */
44  public final class FIGUtils
45  {
46      /////////////////////////////////////////
47      // Constants
48      /////////////////////////////////////////
49  
50      /***
51       * The line separator for the platform FIGUtils is running on.
52       */
53      private static String lineSeparator = System.getProperty("line.separator");
54  
55      /////////////////////////////////////////
56      // Constructor
57      /////////////////////////////////////////
58  
59      /***
60       * This class is an Helper, so it only has a private constructor
61       */
62      private FIGUtils()
63      {
64          super();
65      }
66  
67      /////////////////////////////////////////
68      // Helper public methods
69      /////////////////////////////////////////
70  
71      /***
72       * Build a String a String providing a multi lines rendering of the
73       * {@link FIGFont} informations hold by a {@link FIGFontInfo}.
74       * TODO : add an example of rendering.
75       * @param theInfo the FIGFontInfo to build the String from
76       * @return a String providing a multi lines rendering of the
77       * FIGFont informations hold by theInfo   
78       */
79      public static String getFIGFontInfoDescription(FIGFontInfo theInfo)
80      {
81          String[] printDirectionsDescriptions =
82              { "LEFT TO RIGHT", "RIGHT TO LEFT" };
83          String[] layoutModesDescriptions =
84              {
85                  "FULL SIZE",
86                  "FITTING",
87                  "SMUSHING",
88                  "FULL SIZE",
89                  "FITTING",
90                  "SMUSHING" };
91          String info = "";
92          Iterator commentsIt = theInfo.getCommentLines();
93          while (commentsIt.hasNext())
94          {
95              info += commentsIt.next() + lineSeparator;
96          }
97          info += "Font name = " + theInfo.getName() + lineSeparator;
98          info += "Font height = " + theInfo.getHeight() + lineSeparator;
99          info += "Font baseline = " + theInfo.getBaseline() + lineSeparator;
100         info += "Font max length = " + theInfo.getMaxLength() + lineSeparator;
101         info += "Font print direction = "
102             + printDirectionsDescriptions[theInfo.getPrintDirection()]
103             + lineSeparator;
104         info += "Font hardblank = " + theInfo.getHardblank() + lineSeparator;
105         info += "Font layout : " + lineSeparator;
106         info += "\t Horizontal mode = "
107             + layoutModesDescriptions[theInfo.getLayout().getHorizontalMode()]
108             + lineSeparator;
109         if (theInfo.getLayout().getHorizontalMode()
110             == FIGFontLayout.HORIZONTAL_LAYOUT_MODE_SMUSHING)
111         {
112             info += "\t\t Smushing rules = ";
113             boolean isSmushingModeUniversal = true;
114             if (theInfo
115                 .getLayout()
116                 .hasHorizontalSmushingRule(
117                     FIGFontLayout.HORIZONTAL_SMUSHING_RULE_BIG_X))
118             {
119                 info += "RULE_BIG_X ";
120                 isSmushingModeUniversal = false;
121             }
122             if (theInfo
123                 .getLayout()
124                 .hasHorizontalSmushingRule(
125                     FIGFontLayout.HORIZONTAL_SMUSHING_RULE_EQUALS_CHARACTER))
126             {
127                 info += "EQUALS_CHARACTER ";
128                 isSmushingModeUniversal = false;
129             }
130             if (theInfo
131                 .getLayout()
132                 .hasHorizontalSmushingRule(
133                     FIGFontLayout.HORIZONTAL_SMUSHING_RULE_HARDBLANK))
134             {
135                 info += "HARDBLANK ";
136                 isSmushingModeUniversal = false;
137             }
138             if (theInfo
139                 .getLayout()
140                 .hasHorizontalSmushingRule(
141                     FIGFontLayout.HORIZONTAL_SMUSHING_RULE_HIERARCHY))
142             {
143                 info += "HIERARCHY ";
144                 isSmushingModeUniversal = false;
145             }
146             if (theInfo
147                 .getLayout()
148                 .hasHorizontalSmushingRule(
149                     FIGFontLayout.HORIZONTAL_SMUSHING_RULE_OPPOSITE_PAIR))
150             {
151                 info += "OPPOSITE_PAIR ";
152                 isSmushingModeUniversal = false;
153             }
154             if (theInfo
155                 .getLayout()
156                 .hasHorizontalSmushingRule(
157                     FIGFontLayout.HORIZONTAL_SMUSHING_RULE_UNDERSCORE))
158             {
159                 info += "UNDERSCORE ";
160                 isSmushingModeUniversal = false;
161             }
162             if (isSmushingModeUniversal)
163             {
164                 info += "UNIVERSAL";
165             }
166             info += lineSeparator;
167         }
168         info += "\t Vertical mode = "
169             + layoutModesDescriptions[theInfo.getLayout().getVerticalMode()]
170             + lineSeparator;
171         if (theInfo.getLayout().getVerticalMode()
172             == FIGFontLayout.VERTICAL_LAYOUT_MODE_SMUSHING)
173         {
174             info += "\t\t Smushing rules = ";
175             boolean isSmushingModeUniversal = true;
176             if (theInfo
177                 .getLayout()
178                 .hasVerticalSmushingRule(
179                     FIGFontLayout.VERTICAL_SMUSHING_RULE_EQUALS_CHARACTER))
180             {
181                 info += "EQUALS_CHARACTER ";
182                 isSmushingModeUniversal = false;
183             }
184             if (theInfo
185                 .getLayout()
186                 .hasVerticalSmushingRule(
187                     FIGFontLayout.VERTICAL_SMUSHING_RULE_HIERARCHY))
188             {
189                 info += "HIERARCHY ";
190                 isSmushingModeUniversal = false;
191             }
192             if (theInfo
193                 .getLayout()
194                 .hasVerticalSmushingRule(
195                     FIGFontLayout.VERTICAL_SMUSHING_RULE_HORIZONTAL_LINE))
196             {
197                 info += "HORIZONTAL_LINE ";
198                 isSmushingModeUniversal = false;
199             }
200             if (theInfo
201                 .getLayout()
202                 .hasVerticalSmushingRule(
203                     FIGFontLayout.VERTICAL_SMUSHING_RULE_UNDERSCORE))
204             {
205                 info += "UNDERSCORE ";
206                 isSmushingModeUniversal = false;
207             }
208             if (theInfo
209                 .getLayout()
210                 .hasVerticalSmushingRule(
211                     FIGFontLayout.VERTICAL_SMUSHING_RULE_VERTICAL_LINE))
212             {
213                 info += "VERTICAL_LINE ";
214                 isSmushingModeUniversal = false;
215             }
216             if (isSmushingModeUniversal)
217             {
218                 info += "UNIVERSAL";
219             }
220             info += lineSeparator;
221         }
222         return info;
223     }
224 
225     /***
226      * @TODO : DOCUMENTS ME !
227      * if theLineSeparator is null, the environment variable
228      * line.separator is used
229      */
230     public static void renderFIGureToOutputStream(
231         FIGure theFIGure,
232         OutputStream theOutputStream,
233         String theLineSeparator)
234         throws IOException
235     {
236         // Verify arguments
237         if ((theFIGure == null) || (theOutputStream == null))
238         {
239             throw new IllegalArgumentException("the FIGure and the OuptutStream musn't be null");
240         }
241         String lineSeparatorToUse =
242             (theLineSeparator == null) ? lineSeparator : theLineSeparator;
243         // Creates an OutputStreamWriter that uses the default charset
244         OutputStreamWriter writer = new OutputStreamWriter(theOutputStream);
245         String[] linesToDisplay = theFIGure.getSubcharactersLines();
246         for (int lineIndex = 0; lineIndex < linesToDisplay.length; lineIndex++)
247         {
248             writer.write(linesToDisplay[lineIndex]);
249             writer.write(lineSeparatorToUse);
250             writer.flush();
251         }
252     }
253 }