1 |
/******************************************************************************* |
= |
1 |
/******************************************************************************* |
2 |
* Copyright (c) 2000, 2005 IBM Corporation and others. |
|
2 |
* Copyright (c) 2000, 2005 IBM Corporation and others. |
3 |
* All rights reserved. This program and the accompanying materials |
|
3 |
* All rights reserved. This program and the accompanying materials |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
|
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
5 |
* which accompanies this distribution, and is available at |
|
5 |
* which accompanies this distribution, and is available at |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
|
6 |
* http://www.eclipse.org/legal/epl-v10.html |
7 |
* |
|
7 |
* |
8 |
* Contributors: |
|
8 |
* Contributors: |
9 |
* IBM Corporation - initial API and implementation |
|
9 |
* IBM Corporation - initial API and implementation |
10 |
*******************************************************************************/ |
|
10 |
*******************************************************************************/ |
|
|
<> |
11 |
|
|
|
|
12 |
/******************************************************************************* |
|
|
|
13 |
* Additions/modifications to this source file by Oracle America, Inc. 2011 |
|
|
|
14 |
*******************************************************************************/ |
|
|
|
15 |
|
11 |
package org.eclipse.swt.graphics; |
= |
16 |
package org.eclipse.swt.graphics; |
12 |
|
|
17 |
|
13 |
|
|
18 |
|
14 |
import org.eclipse.swt.*; |
|
19 |
import org.eclipse.swt.*; |
15 |
|
|
20 |
|
16 |
import com.ibm.ugl.eswt.OS; |
|
21 |
import com.ibm.ugl.eswt.OS; |
17 |
|
|
22 |
|
18 |
/** |
|
23 |
/** |
19 |
* Instances of this class manage operating system resources that |
|
24 |
* Instances of this class manage operating system resources that |
20 |
* define how text looks when it is displayed. Fonts may be constructed |
|
25 |
* define how text looks when it is displayed. Fonts may be constructed |
21 |
* by providing a device and either name, size and style information |
|
26 |
* by providing a device and either name, size and style information |
22 |
* or a <code>FontData</code> object which encapsulates this data. |
|
27 |
* or a <code>FontData</code> object which encapsulates this data. |
23 |
* <p> |
|
28 |
* <p> |
24 |
* Application code must explicitly invoke the <code>Font.dispose()</code> |
|
29 |
* Application code must explicitly invoke the <code>Font.dispose()</code> |
25 |
* method to release the operating system resources managed by each instance |
|
30 |
* method to release the operating system resources managed by each instance |
26 |
* when those instances are no longer required. |
|
31 |
* when those instances are no longer required. |
27 |
* </p> |
|
32 |
* </p> |
28 |
* |
|
33 |
* |
29 |
* @see FontData |
|
34 |
* @see FontData |
30 |
*/ |
|
35 |
*/ |
31 |
public final class Font { |
|
36 |
public final class Font { |
32 |
/** |
|
37 |
/** |
33 |
* the handle to the OS font resource |
|
38 |
* the handle to the OS font resource |
34 |
* (Warning: This field is platform dependent) |
|
39 |
* (Warning: This field is platform dependent) |
35 |
*/ |
|
40 |
*/ |
36 |
public int internal_handle; |
|
41 |
public int internal_handle; |
37 |
|
|
42 |
|
38 |
private FontData fontData; |
|
43 |
private FontData fontData; |
39 |
|
|
44 |
|
40 |
/* |
|
45 |
/* |
41 |
* Used by Device.getSystemFont(). |
|
46 |
* Used by Device.getSystemFont(). |
42 |
*/ |
|
47 |
*/ |
43 |
Font(int handle) { |
|
48 |
Font(int handle) { |
44 |
this.internal_handle = handle; |
|
49 |
this.internal_handle = handle; |
45 |
} |
|
50 |
} |
46 |
|
|
51 |
|
47 |
/** |
|
52 |
/** |
48 |
* Constructs a new font given a device and font data |
|
53 |
* Constructs a new font given a device and font data |
49 |
* which describes the desired font's appearance. |
|
54 |
* which describes the desired font's appearance. |
50 |
* <p> |
|
55 |
* <p> |
51 |
* You must dispose the font when it is no longer required. |
|
56 |
* You must dispose the font when it is no longer required. |
52 |
* </p> |
|
57 |
* </p> |
53 |
* |
|
58 |
* |
54 |
* @param device the device to create the font on |
|
59 |
* @param device the device to create the font on |
55 |
* @param fd the FontData that describes the desired font (must not be null) |
|
60 |
* @param fd the FontData that describes the desired font (must not be null) |
56 |
* |
|
61 |
* |
57 |
* @exception IllegalArgumentException <ul> |
|
62 |
* @exception IllegalArgumentException <ul> |
58 |
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> |
|
63 |
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> |
59 |
* <li>ERROR_NULL_ARGUMENT - if the fd argument is null</li> |
|
64 |
* <li>ERROR_NULL_ARGUMENT - if the fd argument is null</li> |
60 |
* </ul> |
|
65 |
* </ul> |
61 |
* @exception SWTError <ul> |
|
66 |
* @exception SWTError <ul> |
62 |
* <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li> |
|
67 |
* <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li> |
63 |
* </ul> |
|
68 |
* </ul> |
64 |
*/ |
|
69 |
*/ |
65 |
public Font(Device device, FontData fd) { |
|
70 |
public Font(Device device, FontData fd) { |
66 |
if (device == null) device = Device.getDevice(); |
|
71 |
if (device == null) device = Device.getDevice(); |
67 |
if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
72 |
if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
68 |
if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
73 |
if (fd == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
69 |
if (fd.getName() == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
74 |
if (fd.getName() == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
70 |
init(device, fd.getName(), fd.getHeight(), fd.getStyle()); |
|
75 |
init(device, fd.getName(), fd.getHeight(), fd.getStyle()); |
71 |
} |
|
76 |
} |
72 |
/** |
|
77 |
/** |
73 |
* Constructs a new font given a device and an array |
|
78 |
* Constructs a new font given a device and an array |
74 |
* of font data which describes the desired font's |
|
79 |
* of font data which describes the desired font's |
75 |
* appearance. |
|
80 |
* appearance. |
76 |
* <p> |
|
81 |
* <p> |
77 |
* You must dispose the font when it is no longer required. |
|
82 |
* You must dispose the font when it is no longer required. |
78 |
* </p> |
|
83 |
* </p> |
79 |
* |
|
84 |
* |
80 |
* @param device the device to create the font on |
|
85 |
* @param device the device to create the font on |
81 |
* @param fds the array of FontData that describes the desired font (must not be null) |
|
86 |
* @param fds the array of FontData that describes the desired font (must not be null) |
82 |
* |
|
87 |
* |
83 |
* @exception IllegalArgumentException <ul> |
|
88 |
* @exception IllegalArgumentException <ul> |
84 |
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> |
|
89 |
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> |
85 |
* <li>ERROR_NULL_ARGUMENT - if the fds argument is null</li> |
|
90 |
* <li>ERROR_NULL_ARGUMENT - if the fds argument is null</li> |
86 |
* <li>ERROR_INVALID_ARGUMENT - if the length of fds is zero</li> |
|
91 |
* <li>ERROR_INVALID_ARGUMENT - if the length of fds is zero</li> |
87 |
* <li>ERROR_NULL_ARGUMENT - if any fd in the array is null</li> |
|
92 |
* <li>ERROR_NULL_ARGUMENT - if any fd in the array is null</li> |
88 |
* </ul> |
|
93 |
* </ul> |
89 |
* @exception SWTError <ul> |
|
94 |
* @exception SWTError <ul> |
90 |
* <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li> |
|
95 |
* <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li> |
91 |
* </ul> |
|
96 |
* </ul> |
92 |
* |
|
97 |
* |
93 |
* @since 2.1 |
|
98 |
* @since 2.1 |
94 |
*/ |
|
99 |
*/ |
95 |
public Font (Device device, FontData[] fds) { |
|
100 |
public Font (Device device, FontData[] fds) { |
96 |
if (fds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
101 |
if (fds == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
97 |
if (fds.length == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
|
102 |
if (fds.length == 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); |
98 |
for (int i = 0; i < fds.length; i++) { |
|
103 |
for (int i = 0; i < fds.length; i++) { |
99 |
if (fds[i] == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
104 |
if (fds[i] == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
100 |
} |
|
105 |
} |
101 |
// UGL only supports one FontData per Font |
|
106 |
// UGL only supports one FontData per Font |
102 |
init(device, fds[0].getName(), fds[0].getHeight(), fds[0].getStyle()); |
|
107 |
init(device, fds[0].getName(), fds[0].getHeight(), fds[0].getStyle()); |
103 |
} |
|
108 |
} |
104 |
/** |
|
109 |
/** |
105 |
* Constructs a new font given a device, a font name, |
|
110 |
* Constructs a new font given a device, a font name, |
106 |
* the height of the desired font in points, and a font |
|
111 |
* the height of the desired font in points, and a font |
107 |
* style. |
|
112 |
* style. |
108 |
* <p> |
|
113 |
* <p> |
109 |
* You must dispose the font when it is no longer required. |
|
114 |
* You must dispose the font when it is no longer required. |
110 |
* </p> |
|
115 |
* </p> |
111 |
* |
|
116 |
* |
112 |
* @param device the device to create the font on |
|
117 |
* @param device the device to create the font on |
113 |
* @param name the name of the font (must not be null) |
|
118 |
* @param name the name of the font (must not be null) |
114 |
* @param height the font height in points |
|
119 |
* @param height the font height in points |
115 |
* @param style a bit or combination of NORMAL, BOLD, ITALIC |
<> |
120 |
* @param style a bit or combination of NORMAL, BOLD, ITALIC, UNDERLINE |
116 |
* |
= |
121 |
* |
117 |
* @exception IllegalArgumentException <ul> |
|
122 |
* @exception IllegalArgumentException <ul> |
118 |
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> |
|
123 |
* <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li> |
119 |
* <li>ERROR_NULL_ARGUMENT - if the name argument is null</li> |
|
124 |
* <li>ERROR_NULL_ARGUMENT - if the name argument is null</li> |
120 |
* <li>ERROR_INVALID_ARGUMENT - if the height is negative</li> |
|
125 |
* <li>ERROR_INVALID_ARGUMENT - if the height is negative</li> |
121 |
* </ul> |
|
126 |
* </ul> |
122 |
* @exception SWTError <ul> |
|
127 |
* @exception SWTError <ul> |
123 |
* <li>ERROR_NO_HANDLES - if a font could not be created from the given arguments</li> |
|
128 |
* <li>ERROR_NO_HANDLES - if a font could not be created from the given arguments</li> |
124 |
* </ul> |
|
129 |
* </ul> |
125 |
*/ |
|
130 |
*/ |
126 |
public Font(Device device, String name, int height, int style) { |
|
131 |
public Font(Device device, String name, int height, int style) { |
127 |
if (device == null) device = Device.getDevice(); |
|
132 |
if (device == null) device = Device.getDevice(); |
128 |
if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
133 |
if (device == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
129 |
if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
134 |
if (name == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
130 |
init(device, name, height, style); |
|
135 |
init(device, name, height, style); |
131 |
} |
|
136 |
} |
132 |
/** |
|
137 |
/** |
133 |
* Disposes of the operating system resources associated with |
|
138 |
* Disposes of the operating system resources associated with |
134 |
* the font. Applications must dispose of all fonts which |
|
139 |
* the font. Applications must dispose of all fonts which |
135 |
* they allocate. |
|
140 |
* they allocate. |
136 |
*/ |
|
141 |
*/ |
137 |
public void dispose() { |
|
142 |
public void dispose() { |
138 |
if (internal_handle == 0) return; |
|
143 |
if (internal_handle == 0) return; |
139 |
|
|
144 |
|
140 |
OS.Font_Dispose(internal_handle); |
|
145 |
OS.Font_Dispose(internal_handle); |
141 |
internal_handle = 0; |
|
146 |
internal_handle = 0; |
142 |
} |
|
147 |
} |
143 |
/** |
|
148 |
/** |
144 |
* Compares the argument to the receiver, and returns true |
|
149 |
* Compares the argument to the receiver, and returns true |
145 |
* if they represent the <em>same</em> object using a class |
|
150 |
* if they represent the <em>same</em> object using a class |
146 |
* specific comparison. |
|
151 |
* specific comparison. |
147 |
* |
|
152 |
* |
148 |
* @param object the object to compare with this object |
|
153 |
* @param object the object to compare with this object |
149 |
* @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise |
|
154 |
* @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise |
150 |
* |
|
155 |
* |
151 |
* @see #hashCode |
|
156 |
* @see #hashCode |
152 |
*/ |
|
157 |
*/ |
153 |
public boolean equals(Object object) { |
|
158 |
public boolean equals(Object object) { |
154 |
if (object == this) return true; |
|
159 |
if (object == this) return true; |
155 |
if (!(object instanceof Font)) return false; |
|
160 |
if (!(object instanceof Font)) return false; |
156 |
Font font = (Font) object; |
|
161 |
Font font = (Font) object; |
157 |
return this.internal_handle == font.internal_handle; |
|
162 |
return this.internal_handle == font.internal_handle; |
158 |
} |
|
163 |
} |
159 |
/** |
|
164 |
/** |
160 |
* Returns an array of <code>FontData</code>s representing the receiver. |
|
165 |
* Returns an array of <code>FontData</code>s representing the receiver. |
161 |
* On Windows, only one FontData will be returned per font. On X however, |
|
166 |
* On Windows, only one FontData will be returned per font. On X however, |
162 |
* a <code>Font</code> object <em>may</em> be composed of multiple X |
|
167 |
* a <code>Font</code> object <em>may</em> be composed of multiple X |
163 |
* fonts. To support this case, we return an array of font data objects. |
|
168 |
* fonts. To support this case, we return an array of font data objects. |
164 |
* |
|
169 |
* |
165 |
* @return an array of font data objects describing the receiver |
|
170 |
* @return an array of font data objects describing the receiver |
166 |
* |
|
171 |
* |
167 |
* @exception SWTException <ul> |
|
172 |
* @exception SWTException <ul> |
168 |
* <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> |
|
173 |
* <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li> |
169 |
* </ul> |
|
174 |
* </ul> |
170 |
*/ |
|
175 |
*/ |
171 |
public FontData[] getFontData() { |
|
176 |
public FontData[] getFontData() { |
172 |
if (internal_handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); |
|
177 |
if (internal_handle == 0) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED); |
173 |
|
|
178 |
|
174 |
// Lazy create the font data so that the name, size and |
|
179 |
// Lazy create the font data so that the name, size and |
175 |
// style are only fetched when absolutely needed |
|
180 |
// style are only fetched when absolutely needed |
176 |
if (fontData == null) { |
|
181 |
if (fontData == null) { |
177 |
fontData = new FontData(); |
|
182 |
fontData = new FontData(); |
178 |
fontData.height = OS.Font_GetSizeInPoints(internal_handle); |
|
183 |
fontData.height = OS.Font_GetSizeInPoints(internal_handle); |
179 |
fontData.style = FontData.internal_convertToSWTStyle(OS.Font_GetStyle(internal_handle)); |
|
184 |
fontData.style = FontData.internal_convertToSWTStyle(OS.Font_GetStyle(internal_handle)); |
180 |
fontData.name = OS.Font_GetName(internal_handle); |
|
185 |
fontData.name = OS.Font_GetName(internal_handle); |
181 |
} |
|
186 |
} |
182 |
|
|
187 |
|
183 |
return new FontData[]{fontData}; |
|
188 |
return new FontData[]{fontData}; |
184 |
} |
|
189 |
} |
185 |
/** |
|
190 |
/** |
186 |
* Returns an integer hash code for the receiver. Any two |
|
191 |
* Returns an integer hash code for the receiver. Any two |
187 |
* objects that return <code>true</code> when passed to |
|
192 |
* objects that return <code>true</code> when passed to |
188 |
* <code>equals</code> must return the same value for this |
|
193 |
* <code>equals</code> must return the same value for this |
189 |
* method. |
|
194 |
* method. |
190 |
* |
|
195 |
* |
191 |
* @return the receiver's hash |
|
196 |
* @return the receiver's hash |
192 |
* |
|
197 |
* |
193 |
* @see #equals |
|
198 |
* @see #equals |
194 |
*/ |
|
199 |
*/ |
195 |
public int hashCode() { |
|
200 |
public int hashCode() { |
196 |
return internal_handle; |
|
201 |
return internal_handle; |
197 |
} |
|
202 |
} |
198 |
void init(Device device, String name, int height, int style) { |
|
203 |
void init(Device device, String name, int height, int style) { |
199 |
if (height < 0) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
|
204 |
if (height < 0) SWT.error(SWT.ERROR_NULL_ARGUMENT); |
200 |
fontData = new FontData(name,height,style); |
|
205 |
fontData = new FontData(name,height,style); |
201 |
internal_handle = OS.Font_New(device.internal_handle, name, height, FontData.internal_convertToNativeStyle(style)); |
|
206 |
internal_handle = OS.Font_New(device.internal_handle, name, height, FontData.internal_convertToNativeStyle(style)); |
202 |
} |
|
207 |
} |
203 |
|
|
208 |
|
204 |
/** |
|
209 |
/** |
205 |
* Returns <code>true</code> if the font has been disposed, |
|
210 |
* Returns <code>true</code> if the font has been disposed, |
206 |
* and <code>false</code> otherwise. |
|
211 |
* and <code>false</code> otherwise. |
207 |
* <p> |
|
212 |
* <p> |
208 |
* This method gets the dispose state for the font. |
|
213 |
* This method gets the dispose state for the font. |
209 |
* When a font has been disposed, it is an error to |
|
214 |
* When a font has been disposed, it is an error to |
210 |
* invoke any other method using the font. |
|
215 |
* invoke any other method using the font. |
211 |
* |
|
216 |
* |
212 |
* @return <code>true</code> when the font is disposed and <code>false</code> otherwise |
|
217 |
* @return <code>true</code> when the font is disposed and <code>false</code> otherwise |
213 |
*/ |
|
218 |
*/ |
214 |
public boolean isDisposed() { |
|
219 |
public boolean isDisposed() { |
215 |
return internal_handle == 0; |
|
220 |
return internal_handle == 0; |
216 |
} |
|
221 |
} |
217 |
/** |
|
222 |
/** |
218 |
* Returns a string containing a concise, human-readable |
|
223 |
* Returns a string containing a concise, human-readable |
219 |
* description of the receiver. |
|
224 |
* description of the receiver. |
220 |
* |
|
225 |
* |
221 |
* @return a string representation of the receiver |
|
226 |
* @return a string representation of the receiver |
222 |
*/ |
|
227 |
*/ |
223 |
public String toString () { |
|
228 |
public String toString () { |
224 |
if (isDisposed()) return "Font {*DISPOSED*}"; |
|
229 |
if (isDisposed()) return "Font {*DISPOSED*}"; |
225 |
return "Font {" + internal_handle + "}"; |
|
230 |
return "Font {" + internal_handle + "}"; |
226 |
} |
|
231 |
} |
227 |
} |
|
232 |
} |