Bukkit  1.4.7-R1.0
 All Classes Namespaces Files Functions Variables Enumerator Pages
MapFont.java
Go to the documentation of this file.
1 package org.bukkit.map;
2 
3 import java.util.HashMap;
4 
8 public 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 
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 
38  public CharacterSprite getChar(char ch) {
39  return chars.get(ch);
40  }
41 
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 
65  public int getHeight() {
66  return height;
67  }
68 
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 
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 
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 
120  public int getWidth() {
121  return width;
122  }
123 
129  public int getHeight() {
130  return height;
131  }
132 
133  }
134 
135 }