Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
at develop 135 lines 3.7 kB view raw
1package org.bukkit.map; 2 3import java.util.HashMap; 4 5/** 6 * Represents a bitmap font drawable to a map. 7 */ 8public class MapFont { 9 10 private final HashMap<Character, CharacterSprite> chars = new HashMap<Character, CharacterSprite>(); 11 private int height = 0; 12 protected boolean malleable = true; 13 14 /** 15 * Set the sprite for a given character. 16 * 17 * @param ch The character to set the sprite for. 18 * @param sprite The CharacterSprite to set. 19 * @throws IllegalStateException if this font is static. 20 */ 21 public void setChar(char ch, CharacterSprite sprite) { 22 if (!malleable) { 23 throw new IllegalStateException("this font is not malleable"); 24 } 25 26 chars.put(ch, sprite); 27 if (sprite.getHeight() > height) { 28 height = sprite.getHeight(); 29 } 30 } 31 32 /** 33 * Get the sprite for a given character. 34 * 35 * @param ch The character to get the sprite for. 36 * @return The CharacterSprite associated with the character, or null if there is none. 37 */ 38 public CharacterSprite getChar(char ch) { 39 return chars.get(ch); 40 } 41 42 /** 43 * Get the width of the given text as it would be rendered using this font. 44 * 45 * @param text The text. 46 * @return The width in pixels. 47 */ 48 public int getWidth(String text) { 49 if (!isValid(text)) { 50 throw new IllegalArgumentException("text contains invalid characters"); 51 } 52 53 int result = 0; 54 for (int i = 0; i < text.length(); ++i) { 55 result += chars.get(text.charAt(i)).getWidth(); 56 } 57 return result; 58 } 59 60 /** 61 * Get the height of this font. 62 * 63 * @return The height of the font. 64 */ 65 public int getHeight() { 66 return height; 67 } 68 69 /** 70 * Check whether the given text is valid. 71 * 72 * @param text The text. 73 * @return True if the string contains only defined characters, false otherwise. 74 */ 75 public boolean isValid(String text) { 76 for (int i = 0; i < text.length(); ++i) { 77 char ch = text.charAt(i); 78 if (ch == '\u00A7' || ch == '\n') continue; 79 if (chars.get(ch) == null) return false; 80 } 81 return true; 82 } 83 84 /** 85 * Represents the graphics for a single character in a MapFont. 86 */ 87 public static class CharacterSprite { 88 89 private final int width; 90 private final int height; 91 private final boolean[] data; 92 93 public CharacterSprite(int width, int height, boolean[] data) { 94 this.width = width; 95 this.height = height; 96 this.data = data; 97 98 if (data.length != width * height) { 99 throw new IllegalArgumentException("size of data does not match dimensions"); 100 } 101 } 102 103 /** 104 * Get the value of a pixel of the character. 105 * 106 * @param row The row, in the range [0,8). 107 * @param col The column, in the range [0,8). 108 * @return True if the pixel is solid, false if transparent. 109 */ 110 public boolean get(int row, int col) { 111 if (row < 0 || col < 0 || row >= height || col >= width) return false; 112 return data[row * width + col]; 113 } 114 115 /** 116 * Get the width of the character sprite. 117 * 118 * @return The width of the character. 119 */ 120 public int getWidth() { 121 return width; 122 } 123 124 /** 125 * Get the height of the character sprite. 126 * 127 * @return The height of the character. 128 */ 129 public int getHeight() { 130 return height; 131 } 132 133 } 134 135}