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