Source code: javax/microedition/lcdui/Font.java
1 /*
2 * MicroEmulator
3 * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
4 * Copyright (C) 2002 3GLab http://www.3glab.com
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 package javax.microedition.lcdui;
22
23 import com.barteo.emulator.device.DeviceFactory;
24
25
26 public final class Font
27 {
28
29 public static final int STYLE_PLAIN = 0;
30 public static final int STYLE_BOLD = 1;
31 public static final int STYLE_ITALIC = 2;
32 public static final int STYLE_UNDERLINED = 4;
33
34 public static final int SIZE_SMALL = 8;
35 public static final int SIZE_MEDIUM = 0;
36 public static final int SIZE_LARGE = 16;
37
38 public static final int FACE_SYSTEM = 0;
39 public static final int FACE_MONOSPACE = 32;
40 public static final int FACE_PROPORTIONAL = 64;
41
42 private static final Font DEFAULT_FONT = new Font(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
43 private int face;
44 private int style;
45 private int size;
46
47
48 private Font(int face, int style, int size)
49 {
50 checkFace(face);
51 checkStyle(style);
52 checkSize(size);
53
54 this.face = face;
55 this.style = style;
56 this.size = size;
57 }
58
59
60 public static Font getFont(int face, int style, int size)
61 throws IllegalArgumentException
62 {
63 return new Font(face, style, size);
64 }
65
66
67 public static Font getDefaultFont()
68 {
69 return DEFAULT_FONT;
70 }
71
72
73 public int getFace()
74 {
75 return face;
76 }
77
78
79 public int getHeight()
80 {
81 return DeviceFactory.getDevice().getFontManager().getHeight(this);
82 }
83
84
85 public int getSize()
86 {
87 return size;
88 }
89
90
91 public int getStyle()
92 {
93 return style;
94 }
95
96
97 public int charWidth(char ch)
98 {
99 return DeviceFactory.getDevice().getFontManager().charWidth(this, ch);
100 }
101
102
103 public int charsWidth(char[] ch, int offset, int length)
104 {
105 return DeviceFactory.getDevice().getFontManager().charsWidth(this, ch, offset, length);
106 }
107
108
109 public int getBaselinePosition()
110 {
111 return DeviceFactory.getDevice().getFontManager().getBaselinePosition(this);
112 }
113
114
115 public boolean isBold()
116 {
117 if ((style & STYLE_BOLD) == STYLE_BOLD) {
118 return true;
119 } else {
120 return false;
121 }
122 }
123
124
125 public boolean isItalic()
126 {
127 if ((style & STYLE_ITALIC) == STYLE_ITALIC) {
128 return true;
129 } else {
130 return false;
131 }
132 }
133
134
135 public boolean isPlain()
136 {
137 if (style == STYLE_PLAIN) {
138 return true;
139 } else {
140 return false;
141 }
142 }
143
144
145 public boolean isUnderlined()
146 {
147 if ((style & STYLE_UNDERLINED) == STYLE_UNDERLINED) {
148 return true;
149 } else {
150 return false;
151 }
152 }
153
154
155 public int stringWidth(String str)
156 {
157 return DeviceFactory.getDevice().getFontManager().stringWidth(this, str);
158 }
159
160
161 public int substringWidth(String str, int offset, int len)
162 {
163 return stringWidth(str.substring(offset, offset + len));
164 }
165
166
167 public boolean equals(Object obj)
168 {
169 if (obj == null || !(obj instanceof Font)) {
170 return false;
171 }
172 if (((Font) obj).face != face) {
173 return false;
174 }
175 if (((Font) obj).style != style) {
176 return false;
177 }
178 if (((Font) obj).size != size) {
179 return false;
180 }
181
182 return true;
183 }
184
185
186 public int hashCode()
187 {
188 return face | style | size;
189 }
190
191
192 private void checkFace(int face)
193 throws IllegalArgumentException
194 {
195 if ((face != FACE_SYSTEM) &&
196 (face != FACE_MONOSPACE) &&
197 (face != FACE_PROPORTIONAL) ) {
198 throw new IllegalArgumentException("Font face is not a known type");
199 }
200 }
201
202
203 private void checkStyle(int style)
204 throws IllegalArgumentException
205 {
206 if (style == STYLE_PLAIN) {
207 return;
208 }
209
210 int allstyles = STYLE_ITALIC|STYLE_BOLD|STYLE_UNDERLINED;
211 if ((style < 0) || (style > allstyles) || ((style&allstyles) == 0)) {
212 throw new IllegalArgumentException("Font style is not a known type");
213 }
214 }
215
216
217 private void checkSize(int size)
218 throws IllegalArgumentException
219 {
220 if ((size != SIZE_SMALL) &&
221 (size != SIZE_MEDIUM) &&
222 (size != SIZE_LARGE) ) {
223 throw new IllegalArgumentException("Font size is not a known type");
224 }
225 }
226
227 }