DEADSOFTWARE

Initial commit.
[LongFlight.git] / src / code / kalter / longflight / Font.java
1 package code.kalter.longflight;
3 import java.io.IOException;
4 import javax.microedition.lcdui.Graphics;
5 import javax.microedition.lcdui.Image;
7 /**
8 * Костыль - и этим всё сказанно
9 *
10 * @author KalterFive
11 */
12 public class Font {
14 private static Font instance;
16 public static Font getInstance() throws IOException {
17 if (instance == null) {
18 instance = new Font();
19 }
20 return instance;
21 }
23 private final Image[] ascii;
25 private Font() throws IOException {
26 Image font = Loader.getInstance().getImage("/gfx/font.png");
27 ascii = new Image[0x7F];
28 int x = 0;
29 for (int i = 0x41; i <= 0x5A; i++, x += 5) {
30 ascii[i] = Image.createImage(font, x, 0, 5, 9, 0);
31 }
32 for (int i = 0x61; i <= 0x7A; i++, x += 5) {
33 ascii[i] = Image.createImage(font, x, 0, 5, 9, 0);
34 }
35 for (int i = 0x30; i <= 0x39; i++, x += 5) {
36 ascii[i] = Image.createImage(font, x, 0, 5, 9, 0);
37 }
38 ascii[0x21] = Image.createImage(font, x, 0, 5, 9, 0);
39 ascii[0x22] = Image.createImage(font, x += 5, 0, 5, 9, 0);
40 ascii[0x27] = Image.createImage(font, x += 5, 0, 5, 9, 0);
41 ascii[0x2C] = Image.createImage(font, x += 5, 0, 5, 9, 0);
42 ascii[0x2D] = Image.createImage(font, x += 5, 0, 5, 9, 0);
43 ascii[0x2E] = Image.createImage(font, x += 5, 0, 5, 9, 0);
44 ascii[0x3A] = Image.createImage(font, x += 5, 0, 5, 9, 0);
45 ascii[0x28] = Image.createImage(font, x += 5, 0, 5, 9, 0);
46 ascii[0x29] = Image.createImage(font, x += 5, 0, 4, 9, 0);
47 }
49 public int getWidth(String text) {
50 return text.length() * 6;
51 }
53 public void paint(Graphics graphics, String text, int x, int y) {
54 for (int i = 0, px = x; i < text.length(); i++) {
55 final char c = text.charAt(i);
56 switch (c) {
57 case '\r':
58 x = px;
59 break;
61 case '\n':
62 y += 10;
63 break;
65 case ' ':
66 x += 6;
67 break;
69 default:
70 graphics.drawImage(ascii[c], x, y, 0);
71 x += 6;
72 break;
73 }
74 }
75 }
77 public void paint(Graphics graphics, String text,
78 int x, int y, int width) {
79 String[] word = split(text);
80 int px = x;
81 for (int i = 0; i < word.length; i++) {
82 paint(graphics, word[i], px, y);
83 int length = (getWidth(word[i])) + 6;
84 if ((px += length) + length > width) {
85 px = x;
86 y += 10;
87 }
88 }
89 }
91 public int getHeight(String text, int width) {
92 String[] word = split(text);
93 int result = 0;
94 for (int i = 0, x = 0; i < word.length; i++) {
95 int length = (getWidth(word[i])) + 6;
96 if ((x += length) + length > width) {
97 x = 0;
98 result += 10;
99 }
101 return result;
104 private String[] split(String text) {
105 String[] result;
106 int length = 1;
107 for (int i = 0; i < text.length(); i++) {
108 if (text.charAt(i) == ' ') {
109 length++;
112 result = new String[length];
113 StringBuffer word = new StringBuffer();
114 int j = 0;
115 for (int i = 0; i < text.length(); i++) {
116 char c = text.charAt(i);
117 if (c == ' ') {
118 result[j++] = word.toString();
119 word = new StringBuffer();
120 } else {
121 word.append(c);
124 result[j] = word.toString();
125 return result;