root/dwt/graphics/FontData.d

Revision 246:fd9c62a2998e, 21.4 kB (checked in by Frank Benoit <benoit@tionex.de>, 6 months ago)

Updater SWT 3.4M7 to 3.4

Line 
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 dwt.graphics.FontData;
14
15
16 import dwt.DWT;
17 import dwt.internal.win32.OS;
18
19 import dwt.dwthelper.utils;
20 static import tango.text.Text;
21 import tango.util.Convert;
22 alias tango.text.Text.Text!(char) StringBuffer;
23
24 /**
25  * Instances of this class describe operating system fonts.
26  * <p>
27  * For platform-independent behaviour, use the get and set methods
28  * corresponding to the following properties:
29  * <dl>
30  * <dt>height</dt><dd>the height of the font in points</dd>
31  * <dt>name</dt><dd>the face name of the font, which may include the foundry</dd>
32  * <dt>style</dt><dd>A bitwise combination of NORMAL, ITALIC and BOLD</dd>
33  * </dl>
34  * If extra, platform-dependent functionality is required:
35  * <ul>
36  * <li>On <em>Windows</em>, the data member of the <code>FontData</code>
37  * corresponds to a Windows <code>LOGFONT</code> structure whose fields
38  * may be retrieved and modified.</li>
39  * <li>On <em>X</em>, the fields of the <code>FontData</code> correspond
40  * to the entries in the font's XLFD name and may be retrieved and modified.
41  * </ul>
42  * Application code does <em>not</em> need to explicitly release the
43  * resources managed by each instance when those instances are no longer
44  * required, and thus no <code>dispose()</code> method is provided.
45  *
46  * @see Font
47  * @see <a href="http://www.eclipse.org/swt/">Sample code and further information</a>
48  */
49
50 public final class FontData {
51
52     /**
53      * A Win32 LOGFONT struct
54      * (Warning: This field is platform dependent)
55      * <p>
56      * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT
57      * public API. It is marked public only so that it can be shared
58      * within the packages provided by DWT. It is not available on all
59      * platforms and should never be accessed from application code.
60      * </p>
61      */
62     public LOGFONT data;
63
64     /**
65      * The height of the font data in points
66      * (Warning: This field is platform dependent)
67      * <p>
68      * <b>IMPORTANT:</b> This field is <em>not</em> part of the DWT
69      * public API. It is marked public only so that it can be shared
70      * within the packages provided by DWT. It is not available on all
71      * platforms and should never be accessed from application code.
72      * </p>
73      */
74     public float height;
75
76     /**
77      * The locales of the font
78      */
79     String lang, country, variant;
80
81     private static FontData s_this;
82
83 /**
84  * Constructs a new uninitialized font data.
85  */
86 public this() {
87     // We set the charset field so that
88     // wildcard searching will work properly
89     // out of the box
90     data.lfCharSet = cast(byte)OS.DEFAULT_CHARSET;
91     height = 12;
92 }
93
94 /**
95  * Constructs a new font data given the Windows <code>LOGFONT</code>
96  * that it should represent.
97  *
98  * @param data the <code>LOGFONT</code> for the result
99  */
100 this(LOGFONT* data, float height) {
101     this.data = *data;
102     this.height = height;
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(String string) {
125     if (string is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
126     int start = 0;
127     int end = string.indexOf('|');
128     if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
129     String version1 = string.substring(start, end);
130     try {
131         if (Integer.parseInt(version1) !is 1) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
132     } catch (NumberFormatException e) {
133         DWT.error(DWT.ERROR_INVALID_ARGUMENT);
134     }
135
136     start = end + 1;
137     end = string.indexOf('|', start);
138     if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
139     String name = string.substring(start, end);
140
141     start = end + 1;
142     end = string.indexOf('|', start);
143     if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
144     float height = 0;
145     try {
146         height = Float.parseFloat(string.substring(start, end));
147     } catch (NumberFormatException e) {
148         DWT.error(DWT.ERROR_INVALID_ARGUMENT);
149     }
150
151     start = end + 1;
152     end = string.indexOf('|', start);
153     if (end is -1) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
154     int style = 0;
155     try {
156         style = Integer.parseInt(string.substring(start, end));
157     } catch (NumberFormatException e) {
158         DWT.error(DWT.ERROR_INVALID_ARGUMENT);
159     }
160
161     start = end + 1;
162     end = string.indexOf('|', start);
163     //data = OS.IsUnicode ? cast(LOGFONT)new LOGFONTW() : new LOGFONTA();
164     data.lfCharSet = cast(byte)OS.DEFAULT_CHARSET;
165     setName(name);
166     setHeight(height);
167     setStyle(style);
168     if (end is -1) return;
169     String platform = string.substring(start, end);
170
171     start = end + 1;
172     end = string.indexOf('|', start);
173     if (end is -1) return;
174     String version2 = string.substring(start, end);
175
176     if (platform ==/*eq*/ "WINDOWS" && version2 ==/*eq*/ "1") {  //$NON-NLS-1$//$NON-NLS-2$
177         LOGFONT newData;// = OS.IsUnicode ? cast(LOGFONT)new LOGFONTW() : new LOGFONTA();
178         try {
179             start = end + 1;
180             end = string.indexOf('|', start);
181             if (end is -1) return;
182             newData.lfHeight = Integer.parseInt(string.substring(start, end));
183             start = end + 1;
184             end = string.indexOf('|', start);
185             if (end is -1) return;
186             newData.lfWidth = Integer.parseInt(string.substring(start, end));
187             start = end + 1;
188             end = string.indexOf('|', start);
189             if (end is -1) return;
190             newData.lfEscapement = Integer.parseInt(string.substring(start, end));
191             start = end + 1;
192             end = string.indexOf('|', start);
193             if (end is -1) return;
194             newData.lfOrientation = Integer.parseInt(string.substring(start, end));
195             start = end + 1;
196             end = string.indexOf('|', start);
197             if (end is -1) return;
198             newData.lfWeight = Integer.parseInt(string.substring(start, end));
199             start = end + 1;
200             end = string.indexOf('|', start);
201             if (end is -1) return;
202             newData.lfItalic = Byte.parseByte(string.substring(start, end));
203             start = end + 1;
204             end = string.indexOf('|', start);
205             if (end is -1) return;
206             newData.lfUnderline = Byte.parseByte(string.substring(start, end));
207             start = end + 1;
208             end = string.indexOf('|', start);
209             if (end is -1) return;
210             newData.lfStrikeOut = Byte.parseByte(string.substring(start, end));
211             start = end + 1;
212             end = string.indexOf('|', start);
213             if (end is -1) return;
214             newData.lfCharSet = Byte.parseByte(string.substring(start, end));
215             start = end + 1;
216             end = string.indexOf('|', start);
217             if (end is -1) return;
218             newData.lfOutPrecision = Byte.parseByte(string.substring(start, end));
219             start = end + 1;
220             end = string.indexOf('|', start);
221             if (end is -1) return;
222             newData.lfClipPrecision = Byte.parseByte(string.substring(start, end));
223             start = end + 1;
224             end = string.indexOf('|', start);
225             if (end is -1) return;
226             newData.lfQuality = Byte.parseByte(string.substring(start, end));
227             start = end + 1;
228             end = string.indexOf('|', start);
229             if (end is -1) return;
230             newData.lfPitchAndFamily = Byte.parseByte(string.substring(start, end));
231             start = end + 1;
232         } catch (NumberFormatException e) {
233             setName(name);
234             setHeight(height);
235             setStyle(style);
236             return;
237         }
238         String buffer = string.substring(start);
239         auto wname = StrToTCHARs(buffer);
240         int len = Math.min(OS.LF_FACESIZE - 1, wname.length);
241         newData.lfFaceName[ 0 .. len ] = wname[ 0 .. len ];
242         newData.lfFaceName[ len .. $ ] = 0;
243         data = newData;
244     }
245 }
246
247 /**
248  * Constructs a new font data given a font name,
249  * the height of the desired font in points,
250  * and a font style.
251  *
252  * @param name the name of the font (must not be null)
253  * @param height the font height in points
254  * @param style a bit or combination of NORMAL, BOLD, ITALIC
255  *
256  * @exception IllegalArgumentException <ul>
257  *    <li>ERROR_NULL_ARGUMENT - when the font name is null</li>
258  *    <li>ERROR_INVALID_ARGUMENT - if the height is negative</li>
259  * </ul>
260  */
261 public this(String name, int height, int style) {
262     if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
263     setName(name);
264     setHeight(height);
265     setStyle(style);
266     // We set the charset field so that
267     // wildcard searching will work properly
268     // out of the box
269     data.lfCharSet = cast(byte)OS.DEFAULT_CHARSET;
270 }
271
272 /*public*/ this(String name, float height, int style) {
273     if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
274     setName(name);
275     setHeight(height);
276     setStyle(style);
277     // We set the charset field so that
278     // wildcard searching will work properly
279     // out of the box
280     data.lfCharSet = cast(byte)OS.DEFAULT_CHARSET;
281 }
282
283 /**
284  * Compares the argument to the receiver, and returns true
285  * if they represent the <em>same</em> object using a class
286  * specific comparison.
287  *
288  * @param object the object to compare with this object
289  * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise
290  *
291  * @see #hashCode
292  */
293 override public int opEquals (Object object) {
294     if (object is this) return true;
295     if( auto fd = cast(FontData)object ){
296         LOGFONT* lf = &fd.data;
297         return data.lfCharSet is lf.lfCharSet &&
298             /*
299             * This code is intentionally commented.  When creating
300             * a FontData, lfHeight is not necessarily set.  Instead
301             * we check the height field which is always set.
302             */
303     //      data.lfHeight is lf.lfHeight &&
304             height is fd.height &&
305             data.lfWidth is lf.lfWidth &&
306             data.lfEscapement is lf.lfEscapement &&
307             data.lfOrientation is lf.lfOrientation &&
308             data.lfWeight is lf.lfWeight &&
309             data.lfItalic is lf.lfItalic &&
310             data.lfUnderline is lf.lfUnderline &&
311             data.lfStrikeOut is lf.lfStrikeOut &&
312             data.lfCharSet is lf.lfCharSet &&
313             data.lfOutPrecision is lf.lfOutPrecision &&
314             data.lfClipPrecision is lf.lfClipPrecision &&
315             data.lfQuality is lf.lfQuality &&
316             data.lfPitchAndFamily is lf.lfPitchAndFamily &&
317             getName() ==/*eq*/ fd.getName();
318     }
319     return false;
320 }
321
322 extern (Windows) static int EnumLocalesProc(TCHAR* lpLocaleString) {
323
324     /* Get the locale ID */
325     int length_ = 8;
326     String str = .TCHARzToStr( cast(TCHAR*)lpLocaleString, length_);
327     int lcid = Integer.parseInt(str, 16);
328
329     TCHAR[] buffer = new TCHAR[length_];
330
331     /* Check the language */
332     int size = OS.GetLocaleInfo(lcid, OS.LOCALE_SISO639LANGNAME, buffer.ptr, length_);
333     if (size <= 0 || s_this.lang !=/*eq*/ .TCHARzToStr( buffer.ptr ).substring(0, size - 1)) return 1;
334
335     /* Check the country */
336     if (s_this.country !is null) {
337         size = OS.GetLocaleInfo(lcid, OS.LOCALE_SISO3166CTRYNAME, buffer.ptr, length_);
338         if (size <= 0 || s_this.country !=/*eq*/ .TCHARzToStr( buffer.ptr ).substring(0, size - 1)) return 1;
339     }
340
341     /* Get the charset */
342     size = OS.GetLocaleInfo(lcid, OS.LOCALE_IDEFAULTANSICODEPAGE, buffer.ptr, length_);
343     if (size <= 0) return 1;
344     int cp = Integer.parseInt(.TCHARzToStr(buffer.ptr).substring(0, size - 1));
345     CHARSETINFO lpCs;
346     OS.TranslateCharsetInfo(cast(DWORD*)cp, &lpCs, OS.TCI_SRCCODEPAGE);
347     s_this.data.lfCharSet = cast(BYTE)lpCs.ciCharset;
348
349     return 0;
350 }
351
352 /**
353  * Returns the height of the receiver in points.
354  *
355  * @return the height of this FontData
356  *
357  * @see #setHeight(int)
358  */
359 public int getHeight() {
360     return cast(int)(0.5f + height);
361 }
362
363 /*public*/ float getHeightF() {
364     return height;
365 }
366
367 /**
368  * Returns the locale of the receiver.
369  * <p>
370  * The locale determines which platform character set this
371  * font is going to use. Widgets and graphics operations that
372  * use this font will convert UNICODE strings to the platform
373  * character set of the specified locale.
374  * </p>
375  * <p>
376  * On platforms where there are multiple character sets for a
377  * given language/country locale, the variant portion of the
378  * locale will determine the character set.
379  * </p>
380  *
381  * @return the <code>String</code> representing a Locale object
382  * @since 3.0
383  */
384 public String getLocale () {
385     StringBuffer buffer = new StringBuffer ();
386     char sep = '_';
387     if (lang !is null) {
388         buffer.append (lang);
389         buffer.append (sep);
390     }
391     if (country !is null) {
392         buffer.append (country);
393         buffer.append (sep);
394     }
395     if (variant !is null) {
396         buffer.append (variant);
397     }
398
399     String result = buffer.toString ();
400     int length_ = result.length;
401     if (length_ > 0) {
402         if (result.charAt (length_ - 1) is sep) {
403             result = result.substring (0, length_ - 1);
404         }
405     }
406     return result;
407 }
408
409 /**
410  * Returns the name of the receiver.
411  * On platforms that support font foundries, the return value will
412  * be the foundry followed by a dash ("-") followed by the face name.
413  *
414  * @return the name of this <code>FontData</code>
415  *
416  * @see #setName
417  */
418 public String getName() {
419     return .TCHARzToStr( data.lfFaceName.ptr, -1 );
420 }
421
422 /**
423  * Returns the style of the receiver which is a bitwise OR of
424  * one or more of the <code>DWT</code> constants NORMAL, BOLD
425  * and ITALIC.
426  *
427  * @return the style of this <code>FontData</code>
428  *
429  * @see #setStyle
430  */
431 public int getStyle() {
432     int style = DWT.NORMAL;
433     if (data.lfWeight is 700) style |= DWT.BOLD;
434     if (data.lfItalic !is 0) style |= DWT.ITALIC;
435     return style;
436 }
437
438 /**
439  * Returns an integer hash code for the receiver. Any two
440  * objects that return <code>true</code> when passed to
441  * <code>equals</code> must return the same value for this
442  * method.
443  *
444  * @return the receiver's hash
445  *
446  * @see #equals
447  */
448 override public hash_t toHash () {
449     String name = getName();
450     return data.lfCharSet ^ getHeight() ^ data.lfWidth ^ data.lfEscapement ^
451         data.lfOrientation ^ data.lfWeight ^ data.lfItalic ^data.lfUnderline ^
452         data.lfStrikeOut ^ data.lfCharSet ^ data.lfOutPrecision ^
453         data.lfClipPrecision ^ data.lfQuality ^ data.lfPitchAndFamily ^
454         typeid(String).getHash(&name);
455 }
456
457 /**
458  * Sets the height of the receiver. The parameter is
459  * specified in terms of points, where a point is one
460  * seventy-second of an inch.
461  *
462  * @param height the height of the <code>FontData</code>
463  *
464  * @exception IllegalArgumentException <ul>
465  *    <li>ERROR_INVALID_ARGUMENT - if the height is negative</li>
466  * </ul>
467  *
468  * @see #getHeight
469  */
470 public void setHeight(int height) {
471     if (height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
472     this.height = height;
473 }
474
475 /*public*/ void setHeight(float height) {
476     if (height < 0) DWT.error(DWT.ERROR_INVALID_ARGUMENT);
477     this.height = height;
478 }
479
480 /**
481  * Sets the locale of the receiver.
482  * <p>
483  * The locale determines which platform character set this
484  * font is going to use. Widgets and graphics operations that
485  * use this font will convert UNICODE strings to the platform
486  * character set of the specified locale.
487  * </p>
488  * <p>
489  * On platforms where there are multiple character sets for a
490  * given language/country locale, the variant portion of the
491  * locale will determine the character set.
492  * </p>
493  *
494  * @param locale the <code>String</code> representing a Locale object
495  * @see java.util.Locale#toString
496  */
497 public void setLocale(String locale) {
498     lang = country = variant = null;
499     if (locale !is null) {
500         char sep = '_';
501         int length_ = locale.length;
502         int firstSep, secondSep;
503
504         firstSep = locale.indexOf(sep);
505         if (firstSep is -1) {
506             firstSep = secondSep = length_;
507         } else {
508             secondSep = locale.indexOf(sep, firstSep + 1);
509             if (secondSep is -1) secondSep = length_;
510         }
511         if (firstSep > 0) lang = locale.substring(0, firstSep);
512         if (secondSep > firstSep + 1) country = locale.substring(firstSep + 1, secondSep);
513         if (length_ > secondSep + 1) variant = locale.substring(secondSep + 1);
514     }
515     if (lang is null) {
516         data.lfCharSet = cast(byte)OS.DEFAULT_CHARSET;
517     } else {
518         synchronized(this.classinfo){
519             s_this = this;
520             OS.EnumSystemLocales(&EnumLocalesProc, OS.LCID_SUPPORTED);
521             s_this = null;
522         }
523     }
524 }
525
526 /**
527  * Sets the name of the receiver.
528  * <p>
529  * Some platforms support font foundries. On these platforms, the name
530  * of the font specified in setName() may have one of the following forms:
531  * <ol>
532  * <li>a face name (for example, "courier")</li>
533  * <li>a foundry followed by a dash ("-") followed by a face name (for example, "adobe-courier")</li>
534  * </ol>
535  * In either case, the name returned from getName() will include the
536  * foundry.
537  * </p>
538  * <p>
539  * On platforms that do not support font foundries, only the face name
540  * (for example, "courier") is used in <code>setName()</code> and
541  * <code>getName()</code>.
542  * </p>
543  *
544  * @param name the name of the font data (must not be null)
545  * @exception IllegalArgumentException <ul>
546  *    <li>ERROR_NULL_ARGUMENT - when the font name is null</li>
547  * </ul>
548  *
549  * @see #getName
550  */
551 public void setName(String name) {
552     if (name is null) DWT.error(DWT.ERROR_NULL_ARGUMENT);
553
554     /* The field lfFaceName must be NULL terminated */
555     auto wname = StrToTCHARs(name);
556     int len = Math.min(OS.LF_FACESIZE - 1, wname.length);
557     data.lfFaceName[0 .. len] = wname[ 0 .. len ];
558     data.lfFaceName[len .. $] = 0;
559 }
560
561 /**
562  * Sets the style of the receiver to the argument which must
563  * be a bitwise OR of one or more of the <code>DWT</code>
564  * constants NORMAL, BOLD and ITALIC.  All other style bits are
565  * ignored.
566  *
567  * @param style the new style for this <code>FontData</code>
568  *
569  * @see #getStyle
570  */
571 public void setStyle(int style) {
572     if ((style & DWT.BOLD) is DWT.BOLD) {
573         data.lfWeight = 700;
574     } else {
575         data.lfWeight = 0;
576     }
577     if ((style & DWT.ITALIC) is DWT.ITALIC) {
578         data.lfItalic = 1;
579     } else {
580         data.lfItalic = 0;
581     }
582 }
583
584 /**
585  * Returns a string representation of the receiver which is suitable
586  * for constructing an equivalent instance using the
587  * <code>FontData(String)</code> constructor.
588  *
589  * @return a string representation of the FontData
590  *
591  * @see FontData
592  */
593 override public String toString() {
594     StringBuffer buffer = new StringBuffer();
595     buffer.append("1|"); //$NON-NLS-1$
596     buffer.append(getName());
597     buffer.append("|"); //$NON-NLS-1$
598     buffer.append(to!(String)(getHeightF()));
599     buffer.append("|"); //$NON-NLS-1$
600     buffer.append(to!(String)(getStyle()));