View Javadoc
1   
2   package org.newdawn.slick.font;
3   
4   import java.io.BufferedReader;
5   import java.io.File;
6   import java.io.FileOutputStream;
7   import java.io.IOException;
8   import java.io.InputStream;
9   import java.io.InputStreamReader;
10  import java.io.PrintStream;
11  import java.util.ArrayList;
12  import java.util.Iterator;
13  import java.util.List;
14  
15  import org.newdawn.slick.SlickException;
16  import org.newdawn.slick.UnicodeFont;
17  import org.newdawn.slick.font.effects.ConfigurableEffect;
18  import org.newdawn.slick.font.effects.ConfigurableEffect.Value;
19  import org.newdawn.slick.font.effects.Effect;
20  import org.newdawn.slick.util.ResourceLoader;
21  
22  import javax.annotation.Nonnull;
23  
24  /**
25   * Holds the settings needed to configure a UnicodeFont.
26   * 
27   * @author Nathan Sweet <misc@n4te.com>
28   */
29  public class HieroSettings {
30      /** The size of the font to be generated */
31      private int fontSize = 12;
32      /** True if the font is rendered bold */
33      private boolean bold = false;
34      /** True fi the font if rendered italic */
35      private boolean italic = false;
36      /** The padding applied in pixels to the top of the glyph rendered area */
37      private int paddingTop;
38      /** The padding applied in pixels to the left of the glyph rendered area */
39      private int paddingLeft;
40      /** The padding applied in pixels to the bottom of the glyph rendered area */
41      private int paddingBottom;
42      /** The padding applied in pixels to the right of the glyph rendered area */
43      private int paddingRight;
44      /** The padding applied in pixels to horizontal advance for each glyph */
45      private int paddingAdvanceX;
46      /** The padding applied in pixels to vertical advance for each glyph */
47      private int paddingAdvanceY;
48      /** The width of the glyph page generated */
49      private int glyphPageWidth = 512;
50      /** The height of the glyph page generated */
51      private int glyphPageHeight = 512;
52      /** The list of effects applied */
53      private final List<Effect> effects = new ArrayList<>();
54  
55      /**
56       * Default constructor for injection
57       */
58      public HieroSettings() {
59      }
60  
61      /**
62       * Create a new set of configuration from a file
63       *
64       * @param hieroFileRef The file system or classpath location of the Hiero settings file.
65       * @throws SlickException if the file could not be read.
66       */
67      public HieroSettings(String hieroFileRef) throws SlickException {
68          this(ResourceLoader.getResourceAsStream(hieroFileRef));
69      }
70  
71      /**
72       * Create a new set of configuration from a file
73       *
74       * @param in The stream from which to read the settings from
75       * @throws SlickException if the file could not be read.
76       */
77      private HieroSettings(@Nonnull InputStream in) throws SlickException {
78          try {
79              BufferedReader reader = new BufferedReader(new InputStreamReader(in));
80              while (true) {
81                  String line = reader.readLine();
82                  if (line == null) break;
83                  line = line.trim();
84                  if (line.length() == 0) continue;
85                  String[] pieces = line.split("=", 2);
86                  String name = pieces[0].trim();
87                  String value = pieces[1];
88                  if (name.equals("font.size")) {
89                      fontSize = Integer.parseInt(value);
90                  } else if (name.equals("font.bold")) {
91                      bold = Boolean.valueOf(value);
92                  } else if (name.equals("font.italic")) {
93                      italic = Boolean.valueOf(value);
94                  } else if (name.equals("pad.top")) {
95                      paddingTop = Integer.parseInt(value);
96                  } else if (name.equals("pad.right")) {
97                      paddingRight = Integer.parseInt(value);
98                  } else if (name.equals("pad.bottom")) {
99                      paddingBottom = Integer.parseInt(value);
100                 } else if (name.equals("pad.left")) {
101                     paddingLeft = Integer.parseInt(value);
102                 } else if (name.equals("pad.advance.x")) {
103                     paddingAdvanceX = Integer.parseInt(value);
104                 } else if (name.equals("pad.advance.y")) {
105                     paddingAdvanceY = Integer.parseInt(value);
106                 } else if (name.equals("glyph.page.width")) {
107                     glyphPageWidth = Integer.parseInt(value);
108                 } else if (name.equals("glyph.page.height")) {
109                     glyphPageHeight = Integer.parseInt(value);
110                 } else if (name.equals("effect.class")) {
111                     try {
112                         effects.add((Effect) Class.forName(value).newInstance());
113                     } catch (ClassNotFoundException e) {
114                         throw new SlickException("Unable to create effect instance: " + value, e);
115                     } catch (Exception ex) {
116                         throw new SlickException("Unable to create effect instance: " + value, ex);
117                     }
118                 } else if (name.startsWith("effect.")) {
119                     // Set an effect value on the last added effect.
120                     name = name.substring(7);
121                     ConfigurableEffect effect = (ConfigurableEffect)effects.get(effects.size() - 1);
122                     List<Value> values = effect.getValues();
123                     for (Value effectValue : values) {
124                         if (effectValue.getName().equals(name)) {
125                             effectValue.setString(value);
126                             break;
127                         }
128                     }
129                     effect.setValues(values);
130                 }
131             }
132             reader.close();
133         } catch (Exception ex) {
134             throw new SlickException("Unable to load Hiero font file", ex);
135         }
136     }
137 
138     /**
139      * @see UnicodeFont#getPaddingTop()
140      *
141      * @return The padding for the top of the glyph area in pixels
142      */
143     public int getPaddingTop () {
144         return paddingTop;
145     }
146 
147     /**
148      * @see UnicodeFont#setPaddingTop(int)
149      *
150      * @param paddingTop The padding for the top of the glyph area in pixels
151      */
152     public void setPaddingTop(int paddingTop) {
153         this.paddingTop = paddingTop;
154     }
155 
156     /**
157      * @see UnicodeFont#getPaddingLeft()
158      *
159      * @return The padding for the left of the glyph area in pixels
160      */
161     public int getPaddingLeft() {
162         return paddingLeft;
163     }
164 
165     /**
166      * @see UnicodeFont#setPaddingLeft(int)
167      *
168      * @param paddingLeft The padding for the left of the glyph area in pixels
169      */
170     public void setPaddingLeft(int paddingLeft) {
171         this.paddingLeft = paddingLeft;
172     }
173 
174     /**
175      * @see UnicodeFont#getPaddingBottom()
176      *
177      * @return The padding for the bottom of the glyph area in pixels
178      */
179     public int getPaddingBottom() {
180         return paddingBottom;
181     }
182 
183     /**
184      * @see UnicodeFont#setPaddingBottom(int)
185      *
186      * @param paddingBottom The padding for the bottom of the glyph area in pixels
187      */
188     public void setPaddingBottom(int paddingBottom) {
189         this.paddingBottom = paddingBottom;
190     }
191 
192     /**
193      * @see UnicodeFont#getPaddingRight()
194      *
195      * @return The padding for the right of the glyph area in pixels
196      */
197     public int getPaddingRight() {
198         return paddingRight;
199     }
200 
201     /**
202      * @see UnicodeFont#setPaddingRight(int)
203      *
204      * @param paddingRight The padding for the right of the glyph area in pixels
205      */
206     public void setPaddingRight(int paddingRight) {
207         this.paddingRight = paddingRight;
208     }
209 
210     /**
211      * @see UnicodeFont#getPaddingAdvanceX()
212      *
213      * @return The padding for the horizontal advance of each glyph
214      */
215     public int getPaddingAdvanceX() {
216         return paddingAdvanceX;
217     }
218 
219     /**
220      * @see UnicodeFont#setPaddingAdvanceX(int)
221      *
222      * @param paddingAdvanceX The padding for the horizontal advance of each glyph
223      */
224     public void setPaddingAdvanceX(int paddingAdvanceX) {
225         this.paddingAdvanceX = paddingAdvanceX;
226     }
227 
228     /**
229      * @see UnicodeFont#getPaddingAdvanceY()
230      *
231      * @return The padding for the vertical advance of each glyph
232      */
233     public int getPaddingAdvanceY() {
234         return paddingAdvanceY;
235     }
236 
237     /**
238      * @see UnicodeFont#setPaddingAdvanceY(int)
239      *
240      * @param paddingAdvanceY The padding for the vertical advance of each glyph
241      */
242     public void setPaddingAdvanceY(int paddingAdvanceY) {
243         this.paddingAdvanceY = paddingAdvanceY;
244     }
245 
246     /**
247      * @see UnicodeFont#getGlyphPageWidth()
248      *
249      * @return The width of the generate glyph pages
250      */
251     public int getGlyphPageWidth() {
252         return glyphPageWidth;
253     }
254 
255     /**
256      * @see UnicodeFont#setGlyphPageWidth(int)
257      *
258      * @param glyphPageWidth The width of the generate glyph pages
259      */
260     public void setGlyphPageWidth(int glyphPageWidth) {
261         this.glyphPageWidth = glyphPageWidth;
262     }
263 
264     /**
265      * @see UnicodeFont#getGlyphPageHeight()
266      *
267      * @return The height of the generate glyph pages
268      */
269     public int getGlyphPageHeight() {
270         return glyphPageHeight;
271     }
272 
273     /**
274      * @see UnicodeFont#setGlyphPageHeight(int)
275      *
276      * @param glyphPageHeight The height of the generate glyph pages
277      */
278     public void setGlyphPageHeight(int glyphPageHeight) {
279         this.glyphPageHeight = glyphPageHeight;
280     }
281 
282     /**
283      * @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)
284      * @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)
285      *
286      * @return The point size of the font generated
287      */
288     public int getFontSize() {
289         return fontSize;
290     }
291 
292     /**
293      * @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)
294      * @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)
295      *
296      * @param fontSize The point size of the font generated
297      */
298     public void setFontSize (int fontSize) {
299         this.fontSize = fontSize;
300     }
301 
302     /**
303      * @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)
304      * @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)
305      *
306      * @return True if the font was generated in bold typeface
307      */
308     public boolean isBold () {
309         return bold;
310     }
311 
312     /**
313      * @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)
314      * @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)
315      *
316      * @param bold True if the font was generated in bold typeface
317      */
318     public void setBold (boolean bold) {
319         this.bold = bold;
320     }
321 
322     /**
323      * @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)
324      * @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)
325      *
326      * @return True if the font was generated in italic typeface
327      */
328     public boolean isItalic () {
329         return italic;
330     }
331 
332     /**
333      * @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)
334      * @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)
335      *
336      * @param italic True if the font was generated in italic typeface
337      */
338     public void setItalic (boolean italic) {
339         this.italic = italic;
340     }
341 
342     /**
343      * @see UnicodeFont#getEffects()
344      *
345      * @return The list of effects applied to the text
346      */
347     @Nonnull
348     public List<Effect> getEffects() {
349         return effects;
350     }
351 
352     /**
353      * Saves the settings to a file.
354      *
355      * @param file The file we're saving to
356      * @throws IOException if the file could not be saved.
357      */
358     public void save(@Nonnull File file) throws SlickException, IOException {
359         try(
360             final FileOutputStream fileOutputStream = new FileOutputStream(file);
361             final PrintStream out = new PrintStream(fileOutputStream)
362         ) {
363             out.println("font.size=" + fontSize);
364             out.println("font.bold=" + bold);
365             out.println("font.italic=" + italic);
366             out.println();
367             out.println("pad.top=" + paddingTop);
368             out.println("pad.right=" + paddingRight);
369             out.println("pad.bottom=" + paddingBottom);
370             out.println("pad.left=" + paddingLeft);
371             out.println("pad.advance.x=" + paddingAdvanceX);
372             out.println("pad.advance.y=" + paddingAdvanceY);
373             out.println();
374             out.println("glyph.page.width=" + glyphPageWidth);
375             out.println("glyph.page.height=" + glyphPageHeight);
376             out.println();
377             for (Iterator<Effect> iter = effects.iterator(); iter.hasNext();) {
378                 if(!(iter.next() instanceof ConfigurableEffect)) {
379                     throw new SlickException("Effect is not org.newdawn.slick.font.effects.ConfigurableEffect");
380                 }
381                 ConfigurableEffect effect = (ConfigurableEffect) iter.next();            
382                 out.println("effect.class=" + effect.getClass().getName());
383                 for (Value value : effect.getValues()) {
384                     out.println("effect." + value.getName() + "=" + value.getString());
385                 }
386                 out.println();
387             }
388         }
389     }
390 }