|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 package javax.microedition.lcdui; |
|
20 |
|
21 import java.util.Hashtable; |
|
22 import com.nokia.mj.impl.rt.legacy.NativeError; |
|
23 |
|
24 public final class Font |
|
25 { |
|
26 // |
|
27 // Font are displayed in this order, FACE,STYLE,SIZE |
|
28 // |
|
29 public static final int FACE_SYSTEM = 0; |
|
30 public static final int FACE_MONOSPACE = 32; |
|
31 public static final int FACE_PROPORTIONAL = 64; |
|
32 // |
|
33 public static final int STYLE_PLAIN = 0; |
|
34 public static final int STYLE_BOLD = 1; |
|
35 public static final int STYLE_ITALIC = 2; |
|
36 public static final int STYLE_UNDERLINED = 4; |
|
37 // |
|
38 public static final int SIZE_SMALL = 8; |
|
39 public static final int SIZE_MEDIUM = 0; |
|
40 public static final int SIZE_LARGE = 16; |
|
41 // |
|
42 public static final int FONT_INPUT_TEXT = 1; |
|
43 public static final int FONT_STATIC_TEXT = 0; |
|
44 // |
|
45 private static final int STYLE_TOTAL = STYLE_BOLD + STYLE_ITALIC + STYLE_UNDERLINED; |
|
46 // |
|
47 private int iFace; |
|
48 private int iStyle; |
|
49 private int iSize; |
|
50 // |
|
51 Toolkit iToolkit; |
|
52 int iHandle; |
|
53 // |
|
54 boolean iIsFreeSizeFont; |
|
55 |
|
56 private Font(Toolkit aToolkit, int aFace,int aStyle,int aSize,boolean aIsFreeSizeFont) |
|
57 { |
|
58 iFace = aFace; |
|
59 iStyle = aStyle; |
|
60 iSize = aSize; |
|
61 iToolkit = aToolkit; |
|
62 iIsFreeSizeFont = aIsFreeSizeFont; |
|
63 synchronized (iToolkit) |
|
64 { |
|
65 iHandle = NativeError.check(_create(iToolkit.getHandle(),aFace,aStyle,aSize,aIsFreeSizeFont)); |
|
66 } |
|
67 } |
|
68 |
|
69 public static Font getDefaultFont() |
|
70 { |
|
71 return getFont(FACE_SYSTEM,STYLE_BOLD,SIZE_MEDIUM); |
|
72 } |
|
73 |
|
74 public static Font getFont(int aFontSpecifier) |
|
75 { |
|
76 if ((aFontSpecifier != FONT_INPUT_TEXT) && (aFontSpecifier != FONT_STATIC_TEXT)) |
|
77 throw new IllegalArgumentException(); |
|
78 // |
|
79 final Toolkit toolkit = Toolkit.getToolkit(); |
|
80 final int fontSpec; |
|
81 synchronized (toolkit) |
|
82 { |
|
83 fontSpec = _getFontSpec(toolkit.getHandle(), aFontSpecifier); |
|
84 } |
|
85 final int face = (fontSpec>>16 & 0x000000ff); |
|
86 final int style = (fontSpec>>8 & 0x000000ff); |
|
87 final int size = (fontSpec & 0x000000ff); |
|
88 return getFont(face,style,size); |
|
89 } |
|
90 |
|
91 public static Font getFont(int aFace,int aStyle,int aSize) |
|
92 { |
|
93 final Toolkit toolkit = Toolkit.getToolkit(); |
|
94 final Hashtable cache = toolkit.getFontCache(); |
|
95 if ((aFace != FACE_SYSTEM && aFace != FACE_MONOSPACE && aFace != FACE_PROPORTIONAL) || |
|
96 aStyle < STYLE_PLAIN || aStyle > STYLE_TOTAL || |
|
97 (aSize != SIZE_MEDIUM && aSize != SIZE_SMALL && aSize != SIZE_LARGE)) |
|
98 { |
|
99 throw new IllegalArgumentException(); |
|
100 } |
|
101 final Integer key = new Integer(aFace|aStyle|aSize); |
|
102 Font font = (Font)cache.get(key); |
|
103 if (font == null) |
|
104 { |
|
105 font = new Font(toolkit,aFace,aStyle,aSize,false); |
|
106 cache.put(key,font); |
|
107 } |
|
108 return font; |
|
109 } |
|
110 |
|
111 static Font getFreeSizeFont(int aFace, int aStyle, int aHeight) |
|
112 { |
|
113 final Toolkit toolkit = Toolkit.getToolkit(); |
|
114 if ((aFace != FACE_SYSTEM && aFace != FACE_MONOSPACE && aFace != FACE_PROPORTIONAL) || |
|
115 aStyle < STYLE_PLAIN || aStyle > STYLE_TOTAL || |
|
116 (aHeight < 0)) |
|
117 { |
|
118 throw new IllegalArgumentException(); |
|
119 } |
|
120 // we don't use font cache for fonts with custom height |
|
121 // because too many different fonts stored in cache |
|
122 // could consume too much memeory |
|
123 return new Font(toolkit, aFace, aStyle, aHeight, true); |
|
124 } |
|
125 |
|
126 public int getStyle() |
|
127 { |
|
128 return iStyle; |
|
129 }; |
|
130 |
|
131 public int getSize() |
|
132 { |
|
133 return iSize; |
|
134 } |
|
135 |
|
136 public int getFace() |
|
137 { |
|
138 return iFace; |
|
139 } |
|
140 |
|
141 public boolean isPlain() |
|
142 { |
|
143 return iStyle == STYLE_PLAIN; |
|
144 } |
|
145 |
|
146 public boolean isBold() |
|
147 { |
|
148 return (iStyle & STYLE_BOLD) == STYLE_BOLD; |
|
149 } |
|
150 |
|
151 public boolean isItalic() |
|
152 { |
|
153 return (iStyle & STYLE_ITALIC) == STYLE_ITALIC; |
|
154 } |
|
155 |
|
156 public boolean isUnderlined() |
|
157 { |
|
158 return (iStyle & STYLE_UNDERLINED) == STYLE_UNDERLINED; |
|
159 } |
|
160 |
|
161 public int getHeight() |
|
162 { |
|
163 synchronized (iToolkit) |
|
164 { |
|
165 return NativeError.check(_height(iHandle,iToolkit.getHandle())); |
|
166 } |
|
167 } |
|
168 |
|
169 public int getBaselinePosition() |
|
170 { |
|
171 synchronized (iToolkit) |
|
172 { |
|
173 return NativeError.check(_baseline(iHandle, iToolkit.getHandle())); |
|
174 } |
|
175 } |
|
176 |
|
177 public int charWidth(char aChar) |
|
178 { |
|
179 synchronized (iToolkit) |
|
180 { |
|
181 return NativeError.check(_width(iHandle, iToolkit.getHandle(), new Character(aChar).toString())); |
|
182 } |
|
183 } |
|
184 |
|
185 public int charsWidth(char[] aChars,int aOffset,int aLength) |
|
186 { |
|
187 try |
|
188 { |
|
189 final String string = new String(aChars,aOffset,aLength); |
|
190 synchronized (iToolkit) |
|
191 { |
|
192 return NativeError.check(_width(iHandle,iToolkit.getHandle(),string)); |
|
193 } |
|
194 } |
|
195 catch (StringIndexOutOfBoundsException ex) |
|
196 { |
|
197 throw new ArrayIndexOutOfBoundsException(); |
|
198 } |
|
199 } |
|
200 |
|
201 public int stringWidth(String aString) |
|
202 { |
|
203 if (aString == null) |
|
204 { |
|
205 throw new NullPointerException(); |
|
206 } |
|
207 synchronized (iToolkit) |
|
208 { |
|
209 return _width(iHandle,iToolkit.getHandle(),aString); |
|
210 } |
|
211 } |
|
212 |
|
213 public int substringWidth(String aString,int aOffset,int aLength) |
|
214 { |
|
215 synchronized (iToolkit) |
|
216 { |
|
217 return _width(iHandle,iToolkit.getHandle(),aString.substring(aOffset,aOffset+aLength)); |
|
218 } |
|
219 } |
|
220 |
|
221 private native int _create(int aToolkit,int aFace,int aStyle,int aSize,boolean aIsFreeSizeFont); |
|
222 private native int _width(int aFont,int aToolkit,String aString); |
|
223 private native int _height(int aFont,int aToolkit); |
|
224 private native int _baseline(int aFont,int aToolkit); |
|
225 private static native int _getFontSpec(int aToolkit,int aFontSpecifier); |
|
226 } |
|
227 |