comparison dwt/graphics/Font.d @ 17:4db14dc0bc45

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