Path: blob/master/SLICK_HOME/src/org/newdawn/slick/SpriteSheetFont.java
1456 views
package org.newdawn.slick;12import java.io.UnsupportedEncodingException;34import org.newdawn.slick.util.Log;56/**7* A font implementation that will use the graphics inside a SpriteSheet for its data.8* This is useful when your font has a fixed width and height for each character as9* opposed to the more complex AngelCodeFont that allows different sizes and kerning10* for each character.11*12* @author Onno Scheffers13*/14public class SpriteSheetFont implements Font {15/** The SpriteSheet containing the bitmap font */16private SpriteSheet font;17/** First character in the SpriteSheet */18private char startingCharacter;19/** Width of each character in pixels */20private int charWidth;21/** Height of each character in pixels */22private int charHeight;23/** Number of characters in SpriteSheet horizontally */24private int horizontalCount;25/** Total number of characters in SpriteSheet */26private int numChars;2728/**29* Create a new font based on a SpriteSheet. The SpriteSheet should hold your30* fixed-width character set in ASCII order. To only get upper-case characters31* working you would usually set up a SpriteSheet with characters for these values:32* <pre>33* !"#$%&'()*+,-./34* 0123456789:;<=>?35* @ABCDEFGHIJKLMNO36* PQRSTUVWXYZ[\]^_<pre>37* In this set, ' ' (SPACE) would be the startingCharacter of your characterSet.38*39* @param font The SpriteSheet holding the font data.40* @param startingCharacter The first character that is defined in the SpriteSheet.41*/42public SpriteSheetFont(SpriteSheet font, char startingCharacter) {43this.font = font;44this.startingCharacter = startingCharacter;45horizontalCount = font.getHorizontalCount();46int verticalCount = font.getVerticalCount();47charWidth = font.getWidth() / horizontalCount;48charHeight = font.getHeight() / verticalCount;49numChars = horizontalCount * verticalCount;50}5152/**53* @see org.newdawn.slick.Font#drawString(float, float, java.lang.String)54*/55public void drawString(float x, float y, String text) {56drawString(x, y, text, Color.white);57}5859/**60* @see org.newdawn.slick.Font#drawString(float, float, java.lang.String, org.newdawn.slick.Color)61*/62public void drawString(float x, float y, String text, Color col) {63drawString(x,y,text,col,0,text.length()-1);64}6566/**67* @see Font#drawString(float, float, String, Color, int, int)68*/69public void drawString(float x, float y, String text, Color col, int startIndex, int endIndex) {70try {71byte[] data = text.getBytes("US-ASCII");72for (int i = 0; i < data.length; i++) {73int index = data[i] - startingCharacter;74if (index < numChars) {75int xPos = (index % horizontalCount);76int yPos = (index / horizontalCount);7778if ((i >= startIndex) || (i <= endIndex)) {79font.getSprite(xPos, yPos)80.draw(x + (i * charWidth), y, col);81}82}83}84} catch (UnsupportedEncodingException e) {85// Should never happen, ASCII is supported pretty much anywhere86Log.error(e);87}88}8990/**91* @see org.newdawn.slick.Font#getHeight(java.lang.String)92*/93public int getHeight(String text) {94return charHeight;95}9697/**98* @see org.newdawn.slick.Font#getWidth(java.lang.String)99*/100public int getWidth(String text) {101return charWidth * text.length();102}103104/**105* @see org.newdawn.slick.Font#getLineHeight()106*/107public int getLineHeight() {108return charHeight;109}110}111112113