Path: blob/master/SLICK_HOME/src/org/newdawn/slick/font/HieroSettings.java
1459 views
1package org.newdawn.slick.font;23import java.io.BufferedReader;4import java.io.File;5import java.io.FileOutputStream;6import java.io.IOException;7import java.io.InputStreamReader;8import java.io.PrintStream;9import java.util.ArrayList;10import java.util.Iterator;11import java.util.List;1213import org.newdawn.slick.SlickException;14import org.newdawn.slick.UnicodeFont;15import org.newdawn.slick.font.effects.ConfigurableEffect;16import org.newdawn.slick.font.effects.ConfigurableEffect.Value;17import org.newdawn.slick.util.ResourceLoader;1819/**20* Holds the settings needed to configure a UnicodeFont.21*22* @author Nathan Sweet <[email protected]>23*/24public class HieroSettings {25/** The size of the font to be generated */26private int fontSize = 12;27/** True if the font is rendered bold */28private boolean bold = false;29/** True fi the font if rendered italic */30private boolean italic = false;31/** The padding applied in pixels to the top of the glyph rendered area */32private int paddingTop;33/** The padding applied in pixels to the left of the glyph rendered area */34private int paddingLeft;35/** The padding applied in pixels to the bottom of the glyph rendered area */36private int paddingBottom;37/** The padding applied in pixels to the right of the glyph rendered area */38private int paddingRight;39/** The padding applied in pixels to horizontal advance for each glyph */40private int paddingAdvanceX;41/** The padding applied in pixels to vertical advance for each glyph */42private int paddingAdvanceY;43/** The width of the glyph page generated */44private int glyphPageWidth = 512;45/** The height of the glyph page generated */46private int glyphPageHeight = 512;47/** The list of effects applied */48private final List effects = new ArrayList();4950/**51* Default constructor for injection52*/53public HieroSettings() {54}5556/**57* Create a new set of configuration from a file58*59* @param hieroFileRef The file system or classpath location of the Hiero settings file.60* @throws SlickException if the file could not be read.61*/62public HieroSettings(String hieroFileRef) throws SlickException {63try {64BufferedReader reader = new BufferedReader(new InputStreamReader(ResourceLoader.getResourceAsStream(hieroFileRef)));65while (true) {66String line = reader.readLine();67if (line == null) break;68line = line.trim();69if (line.length() == 0) continue;70String[] pieces = line.split("=", 2);71String name = pieces[0].trim();72String value = pieces[1];73if (name.equals("font.size")) {74fontSize = Integer.parseInt(value);75} else if (name.equals("font.bold")) {76bold = Boolean.valueOf(value).booleanValue();77} else if (name.equals("font.italic")) {78italic = Boolean.valueOf(value).booleanValue();79} else if (name.equals("pad.top")) {80paddingTop = Integer.parseInt(value);81} else if (name.equals("pad.right")) {82paddingRight = Integer.parseInt(value);83} else if (name.equals("pad.bottom")) {84paddingBottom = Integer.parseInt(value);85} else if (name.equals("pad.left")) {86paddingLeft = Integer.parseInt(value);87} else if (name.equals("pad.advance.x")) {88paddingAdvanceX = Integer.parseInt(value);89} else if (name.equals("pad.advance.y")) {90paddingAdvanceY = Integer.parseInt(value);91} else if (name.equals("glyph.page.width")) {92glyphPageWidth = Integer.parseInt(value);93} else if (name.equals("glyph.page.height")) {94glyphPageHeight = Integer.parseInt(value);95} else if (name.equals("effect.class")) {96try {97effects.add(Class.forName(value).newInstance());98} catch (Exception ex) {99throw new SlickException("Unable to create effect instance: " + value, ex);100}101} else if (name.startsWith("effect.")) {102// Set an effect value on the last added effect.103name = name.substring(7);104ConfigurableEffect effect = (ConfigurableEffect)effects.get(effects.size() - 1);105List values = effect.getValues();106for (Iterator iter = values.iterator(); iter.hasNext();) {107Value effectValue = (Value)iter.next();108if (effectValue.getName().equals(name)) {109effectValue.setString(value);110break;111}112}113effect.setValues(values);114}115}116reader.close();117} catch (Exception ex) {118throw new SlickException("Unable to load Hiero font file: " + hieroFileRef, ex);119}120}121122/**123* @see UnicodeFont#getPaddingTop()124*125* @return The padding for the top of the glyph area in pixels126*/127public int getPaddingTop () {128return paddingTop;129}130131/**132* @see UnicodeFont#setPaddingTop(int)133*134* @param paddingTop The padding for the top of the glyph area in pixels135*/136public void setPaddingTop(int paddingTop) {137this.paddingTop = paddingTop;138}139140/**141* @see UnicodeFont#getPaddingLeft()142*143* @return The padding for the left of the glyph area in pixels144*/145public int getPaddingLeft() {146return paddingLeft;147}148149/**150* @see UnicodeFont#setPaddingLeft(int)151*152* @param paddingLeft The padding for the left of the glyph area in pixels153*/154public void setPaddingLeft(int paddingLeft) {155this.paddingLeft = paddingLeft;156}157158/**159* @see UnicodeFont#getPaddingBottom()160*161* @return The padding for the bottom of the glyph area in pixels162*/163public int getPaddingBottom() {164return paddingBottom;165}166167/**168* @see UnicodeFont#setPaddingBottom(int)169*170* @param paddingBottom The padding for the bottom of the glyph area in pixels171*/172public void setPaddingBottom(int paddingBottom) {173this.paddingBottom = paddingBottom;174}175176/**177* @see UnicodeFont#getPaddingRight()178*179* @return The padding for the right of the glyph area in pixels180*/181public int getPaddingRight() {182return paddingRight;183}184185/**186* @see UnicodeFont#setPaddingRight(int)187*188* @param paddingRight The padding for the right of the glyph area in pixels189*/190public void setPaddingRight(int paddingRight) {191this.paddingRight = paddingRight;192}193194/**195* @see UnicodeFont#getPaddingAdvanceX()196*197* @return The padding for the horizontal advance of each glyph198*/199public int getPaddingAdvanceX() {200return paddingAdvanceX;201}202203/**204* @see UnicodeFont#setPaddingAdvanceX(int)205*206* @param paddingAdvanceX The padding for the horizontal advance of each glyph207*/208public void setPaddingAdvanceX(int paddingAdvanceX) {209this.paddingAdvanceX = paddingAdvanceX;210}211212/**213* @see UnicodeFont#getPaddingAdvanceY()214*215* @return The padding for the vertical advance of each glyph216*/217public int getPaddingAdvanceY() {218return paddingAdvanceY;219}220221/**222* @see UnicodeFont#setPaddingAdvanceY(int)223*224* @param paddingAdvanceY The padding for the vertical advance of each glyph225*/226public void setPaddingAdvanceY(int paddingAdvanceY) {227this.paddingAdvanceY = paddingAdvanceY;228}229230/**231* @see UnicodeFont#getGlyphPageWidth()232*233* @return The width of the generate glyph pages234*/235public int getGlyphPageWidth() {236return glyphPageWidth;237}238239/**240* @see UnicodeFont#setGlyphPageWidth(int)241*242* @param glyphPageWidth The width of the generate glyph pages243*/244public void setGlyphPageWidth(int glyphPageWidth) {245this.glyphPageWidth = glyphPageWidth;246}247248/**249* @see UnicodeFont#getGlyphPageHeight()250*251* @return The height of the generate glyph pages252*/253public int getGlyphPageHeight() {254return glyphPageHeight;255}256257/**258* @see UnicodeFont#setGlyphPageHeight(int)259*260* @param glyphPageHeight The height of the generate glyph pages261*/262public void setGlyphPageHeight(int glyphPageHeight) {263this.glyphPageHeight = glyphPageHeight;264}265266/**267* @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)268* @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)269*270* @return The point size of the font generated271*/272public int getFontSize() {273return fontSize;274}275276/**277* @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)278* @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)279*280* @param fontSize The point size of the font generated281*/282public void setFontSize (int fontSize) {283this.fontSize = fontSize;284}285286/**287* @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)288* @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)289*290* @return True if the font was generated in bold typeface291*/292public boolean isBold () {293return bold;294}295296/**297* @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)298* @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)299*300* @param bold True if the font was generated in bold typeface301*/302public void setBold (boolean bold) {303this.bold = bold;304}305306/**307* @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)308* @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)309*310* @return True if the font was generated in italic typeface311*/312public boolean isItalic () {313return italic;314}315316/**317* @see UnicodeFont#UnicodeFont(String, int, boolean, boolean)318* @see UnicodeFont#UnicodeFont(java.awt.Font, int, boolean, boolean)319*320* @param italic True if the font was generated in italic typeface321*/322public void setItalic (boolean italic) {323this.italic = italic;324}325326/**327* @see UnicodeFont#getEffects()328*329* @return The list of effects applied to the text330*/331public List getEffects() {332return effects;333}334335/**336* Saves the settings to a file.337*338* @param file The file we're saving to339* @throws IOException if the file could not be saved.340*/341public void save(File file) throws IOException {342PrintStream out = new PrintStream(new FileOutputStream(file));343out.println("font.size=" + fontSize);344out.println("font.bold=" + bold);345out.println("font.italic=" + italic);346out.println();347out.println("pad.top=" + paddingTop);348out.println("pad.right=" + paddingRight);349out.println("pad.bottom=" + paddingBottom);350out.println("pad.left=" + paddingLeft);351out.println("pad.advance.x=" + paddingAdvanceX);352out.println("pad.advance.y=" + paddingAdvanceY);353out.println();354out.println("glyph.page.width=" + glyphPageWidth);355out.println("glyph.page.height=" + glyphPageHeight);356out.println();357for (Iterator iter = effects.iterator(); iter.hasNext();) {358ConfigurableEffect effect = (ConfigurableEffect)iter.next();359out.println("effect.class=" + effect.getClass().getName());360for (Iterator iter2 = effect.getValues().iterator(); iter2.hasNext();) {361Value value = (Value)iter2.next();362out.println("effect." + value.getName() + "=" + value.getString());363}364out.println();365}366out.close();367}368}369370371