package code.kalter.longflight; import java.io.IOException; import javax.microedition.lcdui.Graphics; import javax.microedition.lcdui.Image; /** * Костыль - и этим всё сказанно * * @author KalterFive */ public class Font { private static Font instance; public static Font getInstance() throws IOException { if (instance == null) { instance = new Font(); } return instance; } private final Image[] ascii; private Font() throws IOException { Image font = Loader.getInstance().getImage("/gfx/font.png"); ascii = new Image[0x7F]; int x = 0; for (int i = 0x41; i <= 0x5A; i++, x += 5) { ascii[i] = Image.createImage(font, x, 0, 5, 9, 0); } for (int i = 0x61; i <= 0x7A; i++, x += 5) { ascii[i] = Image.createImage(font, x, 0, 5, 9, 0); } for (int i = 0x30; i <= 0x39; i++, x += 5) { ascii[i] = Image.createImage(font, x, 0, 5, 9, 0); } ascii[0x21] = Image.createImage(font, x, 0, 5, 9, 0); ascii[0x22] = Image.createImage(font, x += 5, 0, 5, 9, 0); ascii[0x27] = Image.createImage(font, x += 5, 0, 5, 9, 0); ascii[0x2C] = Image.createImage(font, x += 5, 0, 5, 9, 0); ascii[0x2D] = Image.createImage(font, x += 5, 0, 5, 9, 0); ascii[0x2E] = Image.createImage(font, x += 5, 0, 5, 9, 0); ascii[0x3A] = Image.createImage(font, x += 5, 0, 5, 9, 0); ascii[0x28] = Image.createImage(font, x += 5, 0, 5, 9, 0); ascii[0x29] = Image.createImage(font, x += 5, 0, 4, 9, 0); } public int getWidth(String text) { return text.length() * 6; } public void paint(Graphics graphics, String text, int x, int y) { for (int i = 0, px = x; i < text.length(); i++) { final char c = text.charAt(i); switch (c) { case '\r': x = px; break; case '\n': y += 10; break; case ' ': x += 6; break; default: graphics.drawImage(ascii[c], x, y, 0); x += 6; break; } } } public void paint(Graphics graphics, String text, int x, int y, int width) { String[] word = split(text); int px = x; for (int i = 0; i < word.length; i++) { paint(graphics, word[i], px, y); int length = (getWidth(word[i])) + 6; if ((px += length) + length > width) { px = x; y += 10; } } } public int getHeight(String text, int width) { String[] word = split(text); int result = 0; for (int i = 0, x = 0; i < word.length; i++) { int length = (getWidth(word[i])) + 6; if ((x += length) + length > width) { x = 0; result += 10; } } return result; } private String[] split(String text) { String[] result; int length = 1; for (int i = 0; i < text.length(); i++) { if (text.charAt(i) == ' ') { length++; } } result = new String[length]; StringBuffer word = new StringBuffer(); int j = 0; for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (c == ' ') { result[j++] = word.toString(); word = new StringBuffer(); } else { word.append(c); } } result[j] = word.toString(); return result; } }