Mercurial > projects > dwt-addons
comparison dwtx/jface/resource/FontDescriptor.d @ 7:8a302fdb4140
Jface some window and resource classes
author | Frank Benoit <benoit@tionex.de> |
---|---|
date | Fri, 28 Mar 2008 23:32:40 +0100 |
parents | |
children | ea8ff534f622 |
comparison
equal
deleted
inserted
replaced
6:1a6747be662d | 7:8a302fdb4140 |
---|---|
1 /******************************************************************************* | |
2 * Copyright (c) 2004, 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 * Port to the D programming language: | |
11 * Frank Benoit <benoit@tionex.de> | |
12 *******************************************************************************/ | |
13 module dwtx.jface.resource.FontDescriptor; | |
14 | |
15 import dwtx.jface.resource.DeviceResourceDescriptor; | |
16 import dwtx.jface.resource.ArrayFontDescriptor; | |
17 | |
18 import dwt.graphics.Device; | |
19 import dwt.graphics.Font; | |
20 import dwt.graphics.FontData; | |
21 import dwt.widgets.Display; | |
22 | |
23 import dwt.dwthelper.utils; | |
24 | |
25 /** | |
26 * Lightweight descriptor for a font. Creates the described font on demand. | |
27 * Subclasses can implement different ways of describing a font. These objects | |
28 * will be compared, so hashCode(...) and equals(...) must return something | |
29 * meaningful. | |
30 * | |
31 * @since 3.1 | |
32 */ | |
33 public abstract class FontDescriptor : DeviceResourceDescriptor { | |
34 | |
35 /** | |
36 * Creates a FontDescriptor that describes an existing font. The resulting | |
37 * descriptor depends on the Font. Disposing the Font while the descriptor | |
38 * is still in use may throw a graphic disposed exception. | |
39 * | |
40 * @since 3.1 | |
41 * | |
42 * @deprecated use {@link FontDescriptor#createFrom(Font)} | |
43 * | |
44 * @param font a font to describe | |
45 * @param originalDevice must be the same Device that was passed into | |
46 * the font's constructor when it was first created. | |
47 * @return a newly created FontDescriptor. | |
48 */ | |
49 public static FontDescriptor createFrom(Font font, Device originalDevice) { | |
50 return new ArrayFontDescriptor(font); | |
51 } | |
52 | |
53 /** | |
54 * Creates a FontDescriptor that describes an existing font. The resulting | |
55 * descriptor depends on the original Font, and disposing the original Font | |
56 * while the descriptor is still in use may cause DWT to throw a graphic | |
57 * disposed exception. | |
58 * | |
59 * @since 3.1 | |
60 * | |
61 * @param font font to create | |
62 * @return a newly created FontDescriptor that describes the given font | |
63 */ | |
64 public static FontDescriptor createFrom(Font font) { | |
65 return new ArrayFontDescriptor(font); | |
66 } | |
67 | |
68 /** | |
69 * Creates a new FontDescriptor given the an array of FontData that describes | |
70 * the font. | |
71 * | |
72 * @since 3.1 | |
73 * | |
74 * @param data an array of FontData that describes the font (will be passed into | |
75 * the Font's constructor) | |
76 * @return a FontDescriptor that describes the given font | |
77 */ | |
78 public static FontDescriptor createFrom(FontData[] data) { | |
79 return new ArrayFontDescriptor(data); | |
80 } | |
81 | |
82 /** | |
83 * Creates a new FontDescriptor given the associated FontData | |
84 * | |
85 * @param data FontData describing the font to create | |
86 * @return a newly created FontDescriptor | |
87 */ | |
88 public static FontDescriptor createFrom(FontData data) { | |
89 return new ArrayFontDescriptor( [data] ); | |
90 } | |
91 | |
92 /** | |
93 * Creates a new FontDescriptor given an OS-specific font name, height, and style. | |
94 * | |
95 * @see Font#Font(dwt.graphics.Device, java.lang.String, int, int) | |
96 * | |
97 * @param name os-specific font name | |
98 * @param height height (pixels) | |
99 * @param style a bitwise combination of NORMAL, BOLD, ITALIC | |
100 * @return a new FontDescriptor | |
101 */ | |
102 public static FontDescriptor createFrom(String name, int height, int style) { | |
103 return createFrom(new FontData(name, height, style)); | |
104 } | |
105 | |
106 /** | |
107 * Returns the set of FontData associated with this font. Modifying the elements | |
108 * in the returned array has no effect on the original FontDescriptor. | |
109 * | |
110 * @return the set of FontData associated with this font | |
111 * @since 3.3 | |
112 */ | |
113 public FontData[] getFontData() { | |
114 Font tempFont = createFont(Display.getCurrent()); | |
115 FontData[] result = tempFont.getFontData(); | |
116 destroyFont(tempFont); | |
117 return result; | |
118 } | |
119 | |
120 /** | |
121 * Returns an array of FontData containing copies of the FontData | |
122 * from the original. | |
123 * | |
124 * @param original array to copy | |
125 * @return a deep copy of the original array | |
126 * @since 3.3 | |
127 */ | |
128 public static FontData[] copy(FontData[] original) { | |
129 FontData[] result = new FontData[original.length]; | |
130 for (int i = 0; i < original.length; i++) { | |
131 FontData next = original[i]; | |
132 | |
133 result[i] = copy(next); | |
134 } | |
135 | |
136 return result; | |
137 } | |
138 | |
139 /** | |
140 * Returns a copy of the original FontData | |
141 * | |
142 * @param next FontData to copy | |
143 * @return a copy of the given FontData | |
144 * @since 3.3 | |
145 */ | |
146 public static FontData copy(FontData next) { | |
147 FontData result = new FontData(next.getName(), next.getHeight(), next.getStyle()); | |
148 result.setLocale(next.getLocale()); | |
149 return result; | |
150 } | |
151 | |
152 /** | |
153 * Returns a FontDescriptor that is equivalent to the reciever, but uses | |
154 * the given style bits. | |
155 * | |
156 * <p>Does not modify the reciever.</p> | |
157 * | |
158 * @param style a bitwise combination of DWT.NORMAL, DWT.ITALIC and DWT.BOLD | |
159 * @return a new FontDescriptor with the given style | |
160 * | |
161 * @since 3.3 | |
162 */ | |
163 public final FontDescriptor setStyle(int style) { | |
164 FontData[] data = getFontData(); | |
165 | |
166 for (int i = 0; i < data.length; i++) { | |
167 FontData next = data[i]; | |
168 | |
169 next.setStyle(style); | |
170 } | |
171 | |
172 // Optimization: avoid holding onto extra instances by returning the reciever if | |
173 // if it is exactly the same as the result | |
174 FontDescriptor result = new ArrayFontDescriptor(data); | |
175 if (result.opEquals(this)) { | |
176 return this; | |
177 } | |
178 | |
179 return result; | |
180 } | |
181 | |
182 /** | |
183 * <p>Returns a FontDescriptor that is equivalent to the reciever, but | |
184 * has the given style bits, in addition to any styles the reciever already has.</p> | |
185 * | |
186 * <p>Does not modify the reciever.</p> | |
187 * | |
188 * @param style a bitwise combination of DWT.NORMAL, DWT.ITALIC and DWT.BOLD | |
189 * @return a new FontDescriptor with the given additional style bits | |
190 * @since 3.3 | |
191 */ | |
192 public final FontDescriptor withStyle(int style) { | |
193 FontData[] data = getFontData(); | |
194 | |
195 for (int i = 0; i < data.length; i++) { | |
196 FontData next = data[i]; | |
197 | |
198 next.setStyle(next.getStyle() | style); | |
199 } | |
200 | |
201 // Optimization: avoid allocating extra instances by returning the reciever if | |
202 // if it is exactly the same as the result | |
203 FontDescriptor result = new ArrayFontDescriptor(data); | |
204 if (result.opEquals(this)) { | |
205 return this; | |
206 } | |
207 | |
208 return result; | |
209 } | |
210 | |
211 /** | |
212 * <p>Returns a new FontDescriptor that is equivalent to the reciever, but | |
213 * has the given height.</p> | |
214 * | |
215 * <p>Does not modify the reciever.</p> | |
216 * | |
217 * @param height a height, in points | |
218 * @return a new FontDescriptor with the height, in points | |
219 * @since 3.3 | |
220 */ | |
221 public final FontDescriptor setHeight(int height) { | |
222 FontData[] data = getFontData(); | |
223 | |
224 for (int i = 0; i < data.length; i++) { | |
225 FontData next = data[i]; | |
226 | |
227 next.setHeight(height); | |
228 } | |
229 | |
230 // Optimization: avoid holding onto extra instances by returning the reciever if | |
231 // if it is exactly the same as the result | |
232 FontDescriptor result = new ArrayFontDescriptor(data); | |
233 if (result.opEquals(this)) { | |
234 return this; | |
235 } | |
236 | |
237 return result; | |
238 } | |
239 | |
240 /** | |
241 * <p>Returns a FontDescriptor that is equivalent to the reciever, but whose height | |
242 * is larger by the given number of points.</p> | |
243 * | |
244 * <p>Does not modify the reciever.</p> | |
245 * | |
246 * @param heightDelta a change in height, in points. Negative values will return smaller | |
247 * fonts. | |
248 * @return a FontDescriptor whose height differs from the reciever by the given number | |
249 * of points. | |
250 * @since 3.3 | |
251 */ | |
252 public final FontDescriptor increaseHeight(int heightDelta) { | |
253 if (heightDelta is 0) { | |
254 return this; | |
255 } | |
256 FontData[] data = getFontData(); | |
257 | |
258 for (int i = 0; i < data.length; i++) { | |
259 FontData next = data[i]; | |
260 | |
261 next.setHeight(next.getHeight() + heightDelta); | |
262 } | |
263 | |
264 return new ArrayFontDescriptor(data); | |
265 } | |
266 | |
267 /** | |
268 * Creates the Font described by this descriptor. | |
269 * | |
270 * @since 3.1 | |
271 * | |
272 * @param device device on which to allocate the font | |
273 * @return a newly allocated Font (never null) | |
274 * @throws DeviceResourceException if unable to allocate the Font | |
275 */ | |
276 public abstract Font createFont(Device device); | |
277 | |
278 /** | |
279 * Deallocates anything that was allocated by createFont, given a font | |
280 * that was allocated by an equal FontDescriptor. | |
281 * | |
282 * @since 3.1 | |
283 * | |
284 * @param previouslyCreatedFont previously allocated font | |
285 */ | |
286 public abstract void destroyFont(Font previouslyCreatedFont); | |
287 | |
288 /* (non-Javadoc) | |
289 * @see dwtx.jface.resource.DeviceResourceDescriptor#create(dwt.graphics.Device) | |
290 */ | |
291 public final Object createResource(Device device) { | |
292 return createFont(device); | |
293 } | |
294 | |
295 /* (non-Javadoc) | |
296 * @see dwtx.jface.resource.DeviceResourceDescriptor#destroy(java.lang.Object) | |
297 */ | |
298 public final void destroyResource(Object previouslyCreatedObject) { | |
299 destroyFont(cast(Font)previouslyCreatedObject); | |
300 } | |
301 } |