Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
MapFont.java
1 package org.bukkit.map;
2 
3 import java.util.HashMap;
4 
5 /**
6  * Represents a bitmap font drawable to a map.
7  */
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 
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
37  * there is none.
38  */
39  public CharacterSprite getChar(char ch) {
40  return chars.get(ch);
41  }
42 
43  /**
44  * Get the width of the given text as it would be rendered using this
45  * font.
46  *
47  * @param text The text.
48  * @return The width in pixels.
49  */
50  public int getWidth(String text) {
51  if (!isValid(text)) {
52  throw new IllegalArgumentException("text contains invalid characters");
53  }
54 
55  if (text.length() == 0){
56  return 0;
57  }
58 
59  int result = 0;
60  for (int i = 0; i < text.length(); ++i) {
61  result += chars.get(text.charAt(i)).getWidth();
62  }
63  result += text.length() - 1; // Account for 1px spacing between characters
64 
65  return result;
66  }
67 
68  /**
69  * Get the height of this font.
70  *
71  * @return The height of the font.
72  */
73  public int getHeight() {
74  return height;
75  }
76 
77  /**
78  * Check whether the given text is valid.
79  *
80  * @param text The text.
81  * @return True if the string contains only defined characters, false
82  * otherwise.
83  */
84  public boolean isValid(String text) {
85  for (int i = 0; i < text.length(); ++i) {
86  char ch = text.charAt(i);
87  if (ch == '\u00A7' || ch == '\n') continue;
88  if (chars.get(ch) == null) return false;
89  }
90  return true;
91  }
92 
93  /**
94  * Represents the graphics for a single character in a MapFont.
95  */
96  public static class CharacterSprite {
97 
98  private final int width;
99  private final int height;
100  private final boolean[] data;
101 
102  public CharacterSprite(int width, int height, boolean[] data) {
103  this.width = width;
104  this.height = height;
105  this.data = data;
106 
107  if (data.length != width * height) {
108  throw new IllegalArgumentException("size of data does not match dimensions");
109  }
110  }
111 
112  /**
113  * Get the value of a pixel of the character.
114  *
115  * @param row The row, in the range [0,8).
116  * @param col The column, in the range [0,8).
117  * @return True if the pixel is solid, false if transparent.
118  */
119  public boolean get(int row, int col) {
120  if (row < 0 || col < 0 || row >= height || col >= width) return false;
121  return data[row * width + col];
122  }
123 
124  /**
125  * Get the width of the character sprite.
126  *
127  * @return The width of the character.
128  */
129  public int getWidth() {
130  return width;
131  }
132 
133  /**
134  * Get the height of the character sprite.
135  *
136  * @return The height of the character.
137  */
138  public int getHeight() {
139  return height;
140  }
141 
142  }
143 
144 }
int getWidth(String text)
Definition: MapFont.java:50
void setChar(char ch, CharacterSprite sprite)
Definition: MapFont.java:21
boolean isValid(String text)
Definition: MapFont.java:84
CharacterSprite getChar(char ch)
Definition: MapFont.java:39