1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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
43
44
45 /***
46 * LATIN 1 Charset name
47 */
48 public static final String LATIN_1_CHARSET = "ISO-8859-1";
49
50
51
52
53
54 /***
55 * No args constructor
56 */
57 public FIGDriver()
58 {
59 super();
60 }
61
62
63
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
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
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
112 newLine = getFittingLine(theLineContent, theFIGFontToUse);
113 }
114
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
130 String[] newLine = new String[theFIGFontToUse.getInfo().getHeight()];
131
132 for (int sublineNumber = 0;
133 sublineNumber < newLine.length;
134 sublineNumber++)
135 {
136 newLine[sublineNumber] = "";
137 }
138
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
148 currentCharIndex++)
149 {
150
151 Byte currentCharacterCode =
152 new Byte(theLineContent[currentCharIndex]);
153
154 FIGCharacter currentFIGCharacter =
155 theFIGFontToUse.getFIGCharacter(
156 currentCharacterCode.intValue());
157
158 String currentFIGCharacterLine =
159 currentFIGCharacter.getCharacterLine(sublineNumber);
160 newLine[sublineNumber]
161 += currentFIGCharacterLine.substring(
162 0,
163 (currentFIGCharacterLine.length()));
164 }
165
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
187 String[] newLine = new String[theFIGFontToUse.getInfo().getHeight()];
188
189 for (int sublineNumber = 0;
190 sublineNumber < newLine.length;
191 sublineNumber++)
192 {
193 newLine[sublineNumber] = "";
194 }
195
196 if (theLineContent.length > 0)
197 {
198
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
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
216
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
237
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
261
262 lastFIGCharacterInNewLine = figCharacterToPutInNewLine;
263 }
264
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 }