25
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2008 IBM Corporation and others.
|
|
3 * All rights reserved. This program and the accompanying materials
|
|
4 * are made available under the terms of the Eclipse Public License v1.0
|
|
5 * which accompanies this distribution, and is available at
|
|
6 * http://www.eclipse.org/legal/epl-v10.html
|
|
7 *
|
|
8 * Contributors:
|
|
9 * IBM Corporation - initial API and implementation
|
|
10 * Port to the D programming language:
|
|
11 * Frank Benoit <benoit@tionex.de>
|
|
12 *******************************************************************************/
|
|
13 module org.eclipse.swt.graphics.Font;
|
|
14
|
|
15 import java.lang.all;
|
|
16
|
|
17
|
|
18 import org.eclipse.swt.SWT;
|
|
19 import org.eclipse.swt.graphics.Resource;
|
|
20 import org.eclipse.swt.graphics.FontData;
|
|
21 import org.eclipse.swt.graphics.Device;
|
|
22 import org.eclipse.swt.internal.Converter;
|
|
23 import org.eclipse.swt.internal.gtk.OS;
|
|
24
|
|
25
|
|
26 /**
|
|
27 * Instances of this class manage operating system resources that
|
|
28 * define how text looks when it is displayed. Fonts may be constructed
|
|
29 * by providing a device and either name, size and style information
|
|
30 * or a <code>FontData</code> object which encapsulates this data.
|
|
31 * <p>
|
|
32 * Application code must explicitly invoke the <code>Font.dispose()</code>
|
|
33 * method to release the operating system resources managed by each instance
|
|
34 * when those instances are no longer required.
|
|
35 * </p>
|
|
36 *
|
|
37 * @see FontData
|
|
38 * @see <a href="http://www.eclipse.org/swt/snippets/#font">Font snippets</a>
|
|
39 * @see <a href="http://www.eclipse.org/swt/examples.php">SWT Examples: GraphicsExample, PaintExample</a>
|
|
40 * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
|
|
41 */
|
|
42 public final class Font : Resource {
|
|
43
|
|
44 alias Resource.init_ init_;
|
|
45 /**
|
|
46 * the handle to the OS font resource
|
|
47 * (Warning: This field is platform dependent)
|
|
48 * <p>
|
|
49 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
|
|
50 * public API. It is marked public only so that it can be shared
|
|
51 * within the packages provided by SWT. It is not available on all
|
|
52 * platforms and should never be accessed from application code.
|
|
53 * </p>
|
|
54 */
|
|
55 public PangoFontDescription* handle;
|
|
56
|
|
57 this(Device device) {
|
|
58 super(device);
|
|
59 }
|
|
60
|
|
61 /**
|
|
62 * Constructs a new font given a device and font data
|
|
63 * which describes the desired font's appearance.
|
|
64 * <p>
|
|
65 * You must dispose the font when it is no longer required.
|
|
66 * </p>
|
|
67 *
|
|
68 * @param device the device to create the font on
|
|
69 * @param fd the FontData that describes the desired font (must not be null)
|
|
70 *
|
|
71 * @exception IllegalArgumentException <ul>
|
|
72 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
73 * <li>ERROR_NULL_ARGUMENT - if the fd argument is null</li>
|
|
74 * </ul>
|
|
75 * @exception SWTError <ul>
|
|
76 * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li>
|
|
77 * </ul>
|
|
78 */
|
|
79 public this(Device device, FontData fd) {
|
|
80 super(device);
|
|
81 if (fd is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
82 init_(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.str);
|
|
83 init_();
|
|
84 }
|
|
85
|
|
86 /**
|
|
87 * Constructs a new font given a device and an array
|
|
88 * of font data which describes the desired font's
|
|
89 * appearance.
|
|
90 * <p>
|
|
91 * You must dispose the font when it is no longer required.
|
|
92 * </p>
|
|
93 *
|
|
94 * @param device the device to create the font on
|
|
95 * @param fds the array of FontData that describes the desired font (must not be null)
|
|
96 *
|
|
97 * @exception IllegalArgumentException <ul>
|
|
98 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
99 * <li>ERROR_NULL_ARGUMENT - if the fds argument is null</li>
|
|
100 * <li>ERROR_INVALID_ARGUMENT - if the length of fds is zero</li>
|
|
101 * <li>ERROR_NULL_ARGUMENT - if any fd in the array is null</li>
|
|
102 * </ul>
|
|
103 * @exception SWTError <ul>
|
|
104 * <li>ERROR_NO_HANDLES - if a font could not be created from the given font data</li>
|
|
105 * </ul>
|
|
106 *
|
|
107 * @since 2.1
|
|
108 */
|
|
109 public this(Device device, FontData[] fds) {
|
|
110 super(device);
|
|
111 if (fds is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
112 if (fds.length is 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
113 for (int i=0; i<fds.length; i++) {
|
|
114 if (fds[i] is null) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
115 }
|
|
116 FontData fd = fds[0];
|
|
117 init_(fd.getName(), fd.getHeightF(), fd.getStyle(), fd.str);
|
|
118 init_();
|
|
119 }
|
|
120
|
|
121 /**
|
|
122 * Constructs a new font given a device, a font name,
|
|
123 * the height of the desired font in points, and a font
|
|
124 * style.
|
|
125 * <p>
|
|
126 * You must dispose the font when it is no longer required.
|
|
127 * </p>
|
|
128 *
|
|
129 * @param device the device to create the font on
|
|
130 * @param name the name of the font (must not be null)
|
|
131 * @param height the font height in points
|
|
132 * @param style a bit or combination of NORMAL, BOLD, ITALIC
|
|
133 *
|
|
134 * @exception IllegalArgumentException <ul>
|
|
135 * <li>ERROR_NULL_ARGUMENT - if device is null and there is no current device</li>
|
|
136 * <li>ERROR_NULL_ARGUMENT - if the name argument is null</li>
|
|
137 * <li>ERROR_INVALID_ARGUMENT - if the height is negative</li>
|
|
138 * </ul>
|
|
139 * @exception SWTError <ul>
|
|
140 * <li>ERROR_NO_HANDLES - if a font could not be created from the given arguments</li>
|
|
141 * </ul>
|
|
142 */
|
54
|
143 public this(Device device, in String name, int height, int style) {
|
25
|
144 super(device);
|
|
145 init_(name, height, style, null);
|
|
146 init_();
|
|
147 }
|
|
148
|
54
|
149 /*public*/ this(Device device, in String name, float height, int style) {
|
25
|
150 super(device);
|
|
151 init_(name, height, style, null);
|
|
152 init_();
|
|
153 }
|
|
154
|
|
155 void destroy() {
|
|
156 OS.pango_font_description_free(handle);
|
|
157 handle = null;
|
|
158 }
|
|
159
|
|
160 /**
|
|
161 * Compares the argument to the receiver, and returns true
|
|
162 * if they represent the <em>same</em> object using a class
|
|
163 * specific comparison.
|
|
164 *
|
|
165 * @param object the object to compare with this object
|
|
166 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
|
|
167 *
|
|
168 * @see #hashCode
|
|
169 */
|
26
|
170 public override equals_t opEquals(Object object) {
|
25
|
171 if (object is this) return true;
|
|
172 if ( auto font = cast(Font)object ){
|
|
173 return handle is font.handle;
|
|
174 }
|
|
175 return false;
|
|
176 }
|
|
177
|
|
178
|
|
179 /**
|
|
180 * Returns an array of <code>FontData</code>s representing the receiver.
|
|
181 * On Windows, only one FontData will be returned per font. On X however,
|
|
182 * a <code>Font</code> object <em>may</em> be composed of multiple X
|
|
183 * fonts. To support this case, we return an array of font data objects.
|
|
184 *
|
|
185 * @return an array of font data objects describing the receiver
|
|
186 *
|
|
187 * @exception SWTException <ul>
|
|
188 * <li>ERROR_GRAPHIC_DISPOSED - if the receiver has been disposed</li>
|
|
189 * </ul>
|
|
190 */
|
|
191 public FontData[] getFontData() {
|
|
192 if (isDisposed()) SWT.error(SWT.ERROR_GRAPHIC_DISPOSED);
|
|
193
|
|
194 auto family = OS.pango_font_description_get_family(handle);
|
54
|
195 String name = fromStringz( family )._idup();
|
25
|
196 float height = cast(float)OS.pango_font_description_get_size(handle) / OS.PANGO_SCALE;
|
|
197 int pangoStyle = OS.pango_font_description_get_style(handle);
|
|
198 int pangoWeight = OS.pango_font_description_get_weight(handle);
|
|
199 int style = SWT.NORMAL;
|
|
200 if (pangoStyle is OS.PANGO_STYLE_ITALIC) style |= SWT.ITALIC;
|
|
201 if (pangoStyle is OS.PANGO_STYLE_OBLIQUE) style |= SWT.ROMAN;
|
|
202 if (pangoWeight >= OS.PANGO_WEIGHT_BOLD) style |= SWT.BOLD;
|
|
203 auto fontString = OS.pango_font_description_to_string (handle);
|
54
|
204 auto buffer = fromStringz( fontString )._idup();
|
25
|
205 FontData data = new FontData( buffer , height, style);
|
|
206 OS.g_free (fontString);
|
|
207 data.str = buffer;
|
|
208 return [data];
|
|
209 }
|
|
210
|
|
211 /**
|
|
212 * Invokes platform specific functionality to allocate a new font.
|
|
213 * <p>
|
|
214 * <b>IMPORTANT:</b> This method is <em>not</em> part of the public
|
|
215 * API for <code>Font</code>. It is marked public only so that it
|
|
216 * can be shared within the packages provided by SWT. It is not
|
|
217 * available on all platforms, and should never be called from
|
|
218 * application code.
|
|
219 * </p>
|
|
220 *
|
|
221 * @param device the device on which to allocate the color
|
|
222 * @param handle the handle for the font
|
|
223 *
|
|
224 * @private
|
|
225 */
|
|
226 public static Font gtk_new(Device device, PangoFontDescription* handle) {
|
|
227 Font font = new Font(device);
|
|
228 font.handle = handle;
|
|
229 return font;
|
|
230 }
|
|
231
|
|
232 /**
|
|
233 * Returns an integer hash code for the receiver. Any two
|
|
234 * objects that return <code>true</code> when passed to
|
|
235 * <code>equals</code> must return the same value for this
|
|
236 * method.
|
|
237 *
|
|
238 * @return the receiver's hash
|
|
239 *
|
|
240 * @see #equals
|
|
241 */
|
|
242 public override hash_t toHash() {
|
|
243 return cast(hash_t)/*64*/handle;
|
|
244 }
|
|
245
|
54
|
246 void init_( in String name, float height, int style, in String fontString) {
|
25
|
247 if (name is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
248 if (height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
249 if (fontString !is null) {
|
|
250 handle = OS.pango_font_description_from_string (toStringz(fontString));
|
|
251 if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
252 } else {
|
|
253 handle = OS.pango_font_description_new();
|
|
254 if (handle is null) SWT.error(SWT.ERROR_NO_HANDLES);
|
|
255 //byte[] buffer = Converter.wcsToMbcs(null, name, true);
|
|
256 OS.pango_font_description_set_family(handle, toStringz(name) );
|
|
257 if (height > 0) {
|
|
258 OS.pango_font_description_set_size(handle, cast(int)(0.5f + height * OS.PANGO_SCALE));
|
|
259 }
|
|
260 OS.pango_font_description_set_stretch(handle, OS.PANGO_STRETCH_NORMAL);
|
|
261 int pangoStyle = OS.PANGO_STYLE_NORMAL;
|
|
262 int pangoWeight = OS.PANGO_WEIGHT_NORMAL;
|
|
263 if ((style & SWT.ITALIC) !is 0) pangoStyle = OS.PANGO_STYLE_ITALIC;
|
|
264 if ((style & SWT.ROMAN) !is 0) pangoStyle = OS.PANGO_STYLE_OBLIQUE;
|
|
265 if ((style & SWT.BOLD) !is 0) pangoWeight = OS.PANGO_WEIGHT_BOLD;
|
|
266 OS.pango_font_description_set_style(handle, pangoStyle);
|
|
267 OS.pango_font_description_set_weight(handle, pangoWeight);
|
|
268 }
|
|
269 }
|
|
270
|
|
271 /**
|
|
272 * Returns <code>true</code> if the font has been disposed,
|
|
273 * and <code>false</code> otherwise.
|
|
274 * <p>
|
|
275 * This method gets the dispose state for the font.
|
|
276 * When a font has been disposed, it is an error to
|
|
277 * invoke any other method using the font.
|
|
278 *
|
|
279 * @return <code>true</code> when the font is disposed and <code>false</code> otherwise
|
|
280 */
|
|
281 public override bool isDisposed() {
|
|
282 return handle is null;
|
|
283 }
|
|
284
|
|
285 /**
|
|
286 * Returns a string containing a concise, human-readable
|
|
287 * description of the receiver.
|
|
288 *
|
|
289 * @return a string representation of the receiver
|
|
290 */
|
|
291 public String toString () {
|
|
292 if (isDisposed()) return "Font {*DISPOSED*}";
|
|
293 return Format( "Font {{{}}", handle );
|
|
294 }
|
|
295
|
|
296 }
|