KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > javax > microedition > lcdui > Font


1
2 /*
3  * MicroEmulator
4  * Copyright (C) 2001 Bartek Teodorczyk <barteo@it.pl>
5  * Copyright (C) 2002 3GLab http://www.3glab.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */

21  
22 package javax.microedition.lcdui;
23
24 import com.barteo.emulator.device.DeviceFactory;
25
26
27 public final class Font
28 {
29
30     public static final int STYLE_PLAIN = 0;
31     public static final int STYLE_BOLD = 1;
32     public static final int STYLE_ITALIC = 2;
33     public static final int STYLE_UNDERLINED = 4;
34
35     public static final int SIZE_SMALL = 8;
36     public static final int SIZE_MEDIUM = 0;
37     public static final int SIZE_LARGE = 16;
38
39     public static final int FACE_SYSTEM = 0;
40     public static final int FACE_MONOSPACE = 32;
41     public static final int FACE_PROPORTIONAL = 64;
42
43     private static final Font DEFAULT_FONT = new Font(Font.FACE_SYSTEM, Font.STYLE_PLAIN, Font.SIZE_MEDIUM);
44     private int face;
45     private int style;
46     private int size;
47
48
49     private Font(int face, int style, int size)
50     {
51     checkFace(face);
52     checkStyle(style);
53     checkSize(size);
54
55     this.face = face;
56         this.style = style;
57         this.size = size;
58     }
59
60
61     public static Font getFont(int face, int style, int size)
62       throws IllegalArgumentException
63     {
64         return new Font(face, style, size);
65     }
66
67
68     public static Font getDefaultFont()
69     {
70         return DEFAULT_FONT;
71     }
72
73
74   public int getFace()
75   {
76     return face;
77   }
78
79
80   public int getHeight()
81   {
82     return DeviceFactory.getDevice().getFontManager().getHeight(this);
83   }
84
85
86   public int getSize()
87   {
88     return size;
89   }
90
91
92   public int getStyle()
93   {
94     return style;
95   }
96
97
98   public int charWidth(char ch)
99   {
100     return DeviceFactory.getDevice().getFontManager().charWidth(this, ch);
101   }
102
103
104   public int charsWidth(char[] ch, int offset, int length)
105   {
106     return DeviceFactory.getDevice().getFontManager().charsWidth(this, ch, offset, length);
107   }
108
109
110   public int getBaselinePosition()
111   {
112     return DeviceFactory.getDevice().getFontManager().getBaselinePosition(this);
113   }
114
115
116   public boolean isBold()
117   {
118     if ((style & STYLE_BOLD) == STYLE_BOLD) {
119       return true;
120     } else {
121       return false;
122     }
123   }
124
125
126   public boolean isItalic()
127   {
128     if ((style & STYLE_ITALIC) == STYLE_ITALIC) {
129       return true;
130     } else {
131       return false;
132     }
133   }
134
135
136   public boolean isPlain()
137   {
138     if (style == STYLE_PLAIN) {
139       return true;
140     } else {
141       return false;
142     }
143   }
144
145
146   public boolean isUnderlined()
147   {
148     if ((style & STYLE_UNDERLINED) == STYLE_UNDERLINED) {
149       return true;
150     } else {
151       return false;
152     }
153   }
154
155
156     public int stringWidth(String str)
157     {
158         return DeviceFactory.getDevice().getFontManager().stringWidth(this, str);
159     }
160
161
162     public int substringWidth(String str, int offset, int len)
163     {
164         return stringWidth(str.substring(offset, offset + len));
165     }
166     
167     
168     public boolean equals(Object obj)
169     {
170         if (obj == null || !(obj instanceof Font)) {
171             return false;
172         }
173         if (((Font) obj).face != face) {
174             return false;
175         }
176         if (((Font) obj).style != style) {
177             return false;
178         }
179         if (((Font) obj).size != size) {
180             return false;
181         }
182         
183         return true;
184     }
185
186
187     public int hashCode()
188     {
189         return face | style | size;
190     }
191
192
193   private void checkFace(int face)
194       throws IllegalArgumentException
195   {
196     if ((face != FACE_SYSTEM) &&
197         (face != FACE_MONOSPACE) &&
198         (face != FACE_PROPORTIONAL) ) {
199       throw new IllegalArgumentException("Font face is not a known type");
200     }
201   }
202
203   
204   private void checkStyle(int style)
205       throws IllegalArgumentException
206   {
207     if (style == STYLE_PLAIN) {
208       return;
209     }
210
211     int allstyles = STYLE_ITALIC|STYLE_BOLD|STYLE_UNDERLINED;
212     if ((style < 0) || (style > allstyles) || ((style&allstyles) == 0)) {
213         throw new IllegalArgumentException("Font style is not a known type");
214     }
215   }
216
217   
218   private void checkSize(int size)
219       throws IllegalArgumentException
220   {
221     if ((size != SIZE_SMALL) &&
222         (size != SIZE_MEDIUM) &&
223         (size != SIZE_LARGE) ) {
224       throw new IllegalArgumentException("Font size is not a known type");
225     }
226   }
227   
228 }
229
Popular Tags