5
|
1 /*******************************************************************************
|
|
2 * Copyright (c) 2000, 2007 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 org.eclipse.swt.graphics.FontData;
|
|
12
|
|
13
|
|
14 import org.eclipse.swt.SWT;
|
|
15
|
|
16 import tango.text.convert.Format;
|
|
17 import tango.text.Util : locate;
|
|
18 import tango.util.Convert;
|
|
19
|
|
20 /**
|
|
21 * Instances of this class describe operating system fonts.
|
|
22 * <p>
|
|
23 * For platform-independent behaviour, use the get and set methods
|
|
24 * corresponding to the following properties:
|
|
25 * <dl>
|
|
26 * <dt>height</dt><dd>the height of the font in points</dd>
|
|
27 * <dt>name</dt><dd>the face name of the font, which may include the foundry</dd>
|
|
28 * <dt>style</dt><dd>A bitwise combination of NORMAL, ITALIC and BOLD</dd>
|
|
29 * </dl>
|
|
30 * If extra, platform-dependent functionality is required:
|
|
31 * <ul>
|
|
32 * <li>On <em>Windows</em>, the data member of the <code>FontData</code>
|
|
33 * corresponds to a Windows <code>LOGFONT</code> structure whose fields
|
|
34 * may be retrieved and modified.</li>
|
|
35 * <li>On <em>X</em>, the fields of the <code>FontData</code> correspond
|
|
36 * to the entries in the font's XLFD name and may be retrieved and modified.
|
|
37 * </ul>
|
|
38 * Application code does <em>not</em> need to explicitly release the
|
|
39 * resources managed by each instance when those instances are no longer
|
|
40 * required, and thus no <code>dispose()</code> method is provided.
|
|
41 *
|
|
42 * @see Font
|
|
43 */
|
|
44 public final class FontData {
|
|
45 /**
|
|
46 * the font name
|
|
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 char[] name;
|
|
56
|
|
57 /**
|
|
58 * The height of the font data in points
|
|
59 * (Warning: This field is platform dependent)
|
|
60 * <p>
|
|
61 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
|
|
62 * public API. It is marked public only so that it can be shared
|
|
63 * within the packages provided by SWT. It is not available on all
|
|
64 * platforms and should never be accessed from application code.
|
|
65 * </p>
|
|
66 */
|
|
67 public float height;
|
|
68
|
|
69 /**
|
|
70 * the font style
|
|
71 * (Warning: This field is platform dependent)
|
|
72 * <p>
|
|
73 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
|
|
74 * public API. It is marked public only so that it can be shared
|
|
75 * within the packages provided by SWT. It is not available on all
|
|
76 * platforms and should never be accessed from application code.
|
|
77 * </p>
|
|
78 */
|
|
79 public int style;
|
|
80
|
|
81 /**
|
|
82 * the Pango string
|
|
83 * (Warning: This field is platform dependent)
|
|
84 * <p>
|
|
85 * <b>IMPORTANT:</b> This field is <em>not</em> part of the SWT
|
|
86 * public API. It is marked public only so that it can be shared
|
|
87 * within the packages provided by SWT. It is not available on all
|
|
88 * platforms and should never be accessed from application code.
|
|
89 * </p>
|
|
90 */
|
|
91 public byte[] str;
|
|
92
|
|
93 /**
|
|
94 * The locales of the font
|
|
95 */
|
|
96 char[] lang, country, variant;
|
|
97
|
|
98 /**
|
|
99 * Constructs a new uninitialized font data.
|
|
100 */
|
|
101 public this () {
|
|
102 this("", 12, SWT.NORMAL);
|
|
103 }
|
|
104
|
|
105 /**
|
|
106 * Constructs a new FontData given a string representation
|
|
107 * in the form generated by the <code>FontData.toString</code>
|
|
108 * method.
|
|
109 * <p>
|
|
110 * Note that the representation varies between platforms,
|
|
111 * and a FontData can only be created from a string that was
|
|
112 * generated on the same platform.
|
|
113 * </p>
|
|
114 *
|
|
115 * @param string the string representation of a <code>FontData</code> (must not be null)
|
|
116 *
|
|
117 * @exception IllegalArgumentException <ul>
|
|
118 * <li>ERROR_NULL_ARGUMENT - if the argument is null</li>
|
|
119 * <li>ERROR_INVALID_ARGUMENT - if the argument does not represent a valid description</li>
|
|
120 * </ul>
|
|
121 *
|
|
122 * @see #toString
|
|
123 */
|
|
124 public this(char[] str) {
|
|
125 if (str is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
126 int start = 0;
|
|
127 int end = locate( str, '|' );
|
|
128 if (end == str.length ) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
129 char[] version1 = str[ start .. end ];
|
|
130 try {
|
|
131 if (to!(int)(version1) != 1) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
132 } catch (ConversionException e) {
|
|
133 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
134 }
|
|
135
|
|
136 start = end + 1;
|
|
137 end = locate( str, '|', start );
|
|
138 if (end == str.length ) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
139 char[] name = str[start .. end ];
|
|
140
|
|
141 start = end + 1;
|
|
142 end = locate( str, '|', start );
|
|
143 if (end == str.length ) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
144 float height = 0;
|
|
145 try {
|
|
146 height = to!(float)(str[start .. end]);
|
|
147 } catch (ConversionException e) {
|
|
148 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
149 }
|
|
150
|
|
151 start = end + 1;
|
|
152 end = locate( str, '|', start );
|
|
153 if (end == str.length ) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
154 int style = 0;
|
|
155 try {
|
|
156 style = to!(int)( str[start .. end ]);
|
|
157 } catch (ConversionException e) {
|
|
158 SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
159 }
|
|
160
|
|
161 start = end + 1;
|
|
162 end = locate( str, '|', start );
|
|
163 setName(name);
|
|
164 setHeight(height);
|
|
165 setStyle(style);
|
|
166 if (end == str.length) return;
|
|
167 char[] platform = str[ start .. end ];
|
|
168
|
|
169 start = end + 1;
|
|
170 end = locate( str, '|', start );
|
|
171 if (end == str.length) return;
|
|
172 char[] version2 = str[ start .. end ];
|
|
173
|
|
174 if (platform == "GTK" && version2 == "1" ) {
|
|
175 return;
|
|
176 }
|
|
177 }
|
|
178
|
|
179 /**
|
|
180 * Constructs a new font data given a font name,
|
|
181 * the height of the desired font in points,
|
|
182 * and a font style.
|
|
183 *
|
|
184 * @param name the name of the font (must not be null)
|
|
185 * @param height the font height in points
|
|
186 * @param style a bit or combination of NORMAL, BOLD, ITALIC
|
|
187 *
|
|
188 * @exception IllegalArgumentException <ul>
|
|
189 * <li>ERROR_NULL_ARGUMENT - when the font name is null</li>
|
|
190 * <li>ERROR_INVALID_ARGUMENT - if the height is negative</li>
|
|
191 * </ul>
|
|
192 */
|
|
193 public this(char[] name, int height, int style) {
|
|
194 setName(name);
|
|
195 setHeight(height);
|
|
196 setStyle(style);
|
|
197 }
|
|
198
|
|
199 /*public*/ this(char[] name, float height, int style) {
|
|
200 setName(name);
|
|
201 setHeight(height);
|
|
202 setStyle(style);
|
|
203 }
|
|
204
|
|
205 /**
|
|
206 * Compares the argument to the receiver, and returns true
|
|
207 * if they represent the <em>same</em> object using a class
|
|
208 * specific comparison.
|
|
209 *
|
|
210 * @param object the object to compare with this object
|
|
211 * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
|
|
212 *
|
|
213 * @see #hashCode
|
|
214 */
|
|
215 public override int opEquals (Object object) {
|
|
216 if (object is this) return true;
|
|
217 if( auto data = cast(FontData)object ){
|
|
218 return name == data.name && height == data.height && style == data.style;
|
|
219 }
|
|
220 return false;
|
|
221 }
|
|
222
|
|
223 /**
|
|
224 * Returns the height of the receiver in points.
|
|
225 *
|
|
226 * @return the height of this FontData
|
|
227 *
|
|
228 * @see #setHeight(int)
|
|
229 */
|
|
230 public int getHeight() {
|
|
231 return cast(int)(0.5f + height);
|
|
232 }
|
|
233
|
|
234 /*public*/ float getHeightF() {
|
|
235 return height;
|
|
236 }
|
|
237
|
|
238 /**
|
|
239 * Returns the locale of the receiver.
|
|
240 * <p>
|
|
241 * The locale determines which platform character set this
|
|
242 * font is going to use. Widgets and graphics operations that
|
|
243 * use this font will convert UNICODE strings to the platform
|
|
244 * character set of the specified locale.
|
|
245 * </p>
|
|
246 * <p>
|
|
247 * On platforms where there are multiple character sets for a
|
|
248 * given language/country locale, the variant portion of the
|
|
249 * locale will determine the character set.
|
|
250 * </p>
|
|
251 *
|
|
252 * @return the <code>String</code> representing a Locale object
|
|
253 * @since 3.0
|
|
254 */
|
|
255 public char[] getLocale () {
|
|
256 char[] result;
|
|
257 const char sep = '_';
|
|
258 if (lang != null) {
|
|
259 result ~= lang;
|
|
260 result ~= sep;
|
|
261 }
|
|
262 if (country != null) {
|
|
263 result ~= country;
|
|
264 result ~= sep;
|
|
265 }
|
|
266 if (variant != null) {
|
|
267 result ~= variant;
|
|
268 }
|
|
269
|
|
270 if (result) {
|
|
271 if (result[$-1] == sep) {
|
|
272 result = result[0 .. $ - 1];
|
|
273 }
|
|
274 }
|
|
275 return result;
|
|
276 }
|
|
277
|
|
278 /**
|
|
279 * Returns the name of the receiver.
|
|
280 * On platforms that support font foundries, the return value will
|
|
281 * be the foundry followed by a dash ("-") followed by the face name.
|
|
282 *
|
|
283 * @return the name of this <code>FontData</code>
|
|
284 *
|
|
285 * @see #setName
|
|
286 */
|
|
287 public char[] getName() {
|
|
288 return name;
|
|
289 }
|
|
290
|
|
291 /**
|
|
292 * Returns the style of the receiver which is a bitwise OR of
|
|
293 * one or more of the <code>SWT</code> constants NORMAL, BOLD
|
|
294 * and ITALIC.
|
|
295 *
|
|
296 * @return the style of this <code>FontData</code>
|
|
297 *
|
|
298 * @see #setStyle
|
|
299 */
|
|
300 public int getStyle() {
|
|
301 return style;
|
|
302 }
|
|
303
|
|
304 /**
|
|
305 * Returns an integer hash code for the receiver. Any two
|
|
306 * objects that return <code>true</code> when passed to
|
|
307 * <code>equals</code> must return the same value for this
|
|
308 * method.
|
|
309 *
|
|
310 * @return the receiver's hash
|
|
311 *
|
|
312 * @see #equals
|
|
313 */
|
|
314 public hash_t toHash () {
|
|
315 return typeid(char[]).getHash(&name) ^ getHeight() ^ style;
|
|
316 }
|
|
317
|
|
318 /**
|
|
319 * Sets the height of the receiver. The parameter is
|
|
320 * specified in terms of points, where a point is one
|
|
321 * seventy-second of an inch.
|
|
322 *
|
|
323 * @param height the height of the <code>FontData</code>
|
|
324 *
|
|
325 * @exception IllegalArgumentException <ul>
|
|
326 * <li>ERROR_INVALID_ARGUMENT - if the height is negative</li>
|
|
327 * </ul>
|
|
328 *
|
|
329 * @see #getHeight
|
|
330 */
|
|
331 public void setHeight(int height) {
|
|
332 if (height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
333 this.height = height;
|
|
334 this.str = null;
|
|
335 }
|
|
336
|
|
337 /*public*/ void setHeight(float height) {
|
|
338 if (height < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT);
|
|
339 this.height = height;
|
|
340 this.str = null;
|
|
341 }
|
|
342
|
|
343 /**
|
|
344 * Sets the locale of the receiver.
|
|
345 * <p>
|
|
346 * The locale determines which platform character set this
|
|
347 * font is going to use. Widgets and graphics operations that
|
|
348 * use this font will convert UNICODE strings to the platform
|
|
349 * character set of the specified locale.
|
|
350 * </p>
|
|
351 * <p>
|
|
352 * On platforms where there are multiple character sets for a
|
|
353 * given language/country locale, the variant portion of the
|
|
354 * locale will determine the character set.
|
|
355 * </p>
|
|
356 *
|
|
357 * @param locale the <code>String</code> representing a Locale object
|
|
358 * @see java.util.Locale#toString
|
|
359 */
|
|
360 public void setLocale(char[] locale) {
|
|
361 lang = country = variant = null;
|
|
362 if (locale !is null) {
|
|
363 char sep = '_';
|
|
364 int length = locale.length;
|
|
365 int firstSep, secondSep;
|
|
366
|
|
367 firstSep = locate( locale, sep );
|
|
368 if (firstSep == locale.length) {
|
|
369 firstSep = secondSep = length;
|
|
370 } else {
|
|
371 secondSep = locate( locale, sep, firstSep + 1);
|
|
372 if (secondSep == locale.length) secondSep = length;
|
|
373 }
|
|
374 if (firstSep > 0) lang = locale[0 .. firstSep];
|
|
375 if (secondSep > firstSep + 1) country = locale[firstSep + 1 .. secondSep ];
|
|
376 if (length > secondSep + 1) variant = locale[secondSep + 1 .. $ ];
|
|
377 }
|
|
378 }
|
|
379
|
|
380 /**
|
|
381 * Sets the name of the receiver.
|
|
382 * <p>
|
|
383 * Some platforms support font foundries. On these platforms, the name
|
|
384 * of the font specified in setName() may have one of the following forms:
|
|
385 * <ol>
|
|
386 * <li>a face name (for example, "courier")</li>
|
|
387 * <li>a foundry followed by a dash ("-") followed by a face name (for example, "adobe-courier")</li>
|
|
388 * </ol>
|
|
389 * In either case, the name returned from getName() will include the
|
|
390 * foundry.
|
|
391 * </p>
|
|
392 * <p>
|
|
393 * On platforms that do not support font foundries, only the face name
|
|
394 * (for example, "courier") is used in <code>setName()</code> and
|
|
395 * <code>getName()</code>.
|
|
396 * </p>
|
|
397 *
|
|
398 * @param name the name of the font data (must not be null)
|
|
399 * @exception IllegalArgumentException <ul>
|
|
400 * <li>ERROR_NULL_ARGUMENT - when the font name is null</li>
|
|
401 * </ul>
|
|
402 *
|
|
403 * @see #getName
|
|
404 */
|
|
405 public void setName(char[] name) {
|
|
406 if (name is null) SWT.error(SWT.ERROR_NULL_ARGUMENT);
|
|
407 this.name = name;
|
|
408 this.str = null;
|
|
409 }
|
|
410
|
|
411 /**
|
|
412 * Sets the style of the receiver to the argument which must
|
|
413 * be a bitwise OR of one or more of the <code>SWT</code>
|
|
414 * constants NORMAL, BOLD and ITALIC. All other style bits are
|
|
415 * ignored.
|
|
416 *
|
|
417 * @param style the new style for this <code>FontData</code>
|
|
418 *
|
|
419 * @see #getStyle
|
|
420 */
|
|
421 public void setStyle(int style) {
|
|
422 this.style = style;
|
|
423 this.str = null;
|
|
424 }
|
|
425
|
|
426 /**
|
|
427 * Returns a string representation of the receiver which is suitable
|
|
428 * for constructing an equivalent instance using the
|
|
429 * <code>FontData(String)</code> constructor.
|
|
430 *
|
|
431 * @return a string representation of the FontData
|
|
432 *
|
|
433 * @see FontData
|
|
434 */
|
|
435 public char[] toString() {
|
|
436 return Format( "1|{}|{}|{}|GTK|1|", getName, getHeightF, getStyle );
|
|
437 }
|
|
438
|
|
439 }
|