New test.
[mono.git] / mcs / class / System.Drawing / System.Drawing / FontFamily.jvm.cs
1 //
2 // System.Drawing.FontFamily.cs
3 //
4 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
5 // Author: Konstantin Triger (kostat@mainsoft.com)
6 //
7
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Drawing.Text;
31 using System.Text;
32 using System.Runtime.InteropServices;
33 using System.Globalization;
34 using awt = java.awt;
35 using geom = java.awt.geom;
36 using font = java.awt.font;
37 using TextAttribute = java.awt.font.TextAttribute;
38
39 namespace System.Drawing {
40
41         public sealed class FontFamily : MarshalByRefObject, IDisposable {
42                 
43                 static readonly FontFamily _genericMonospace;
44                 static readonly FontFamily _genericSansSerif;
45                 static readonly FontFamily _genericSerif;
46                 static readonly FontCollection _installedFonts;
47                 internal static readonly awt.Container Container;
48
49                 static FontFamily() {
50                         Container = new awt.Container();
51                         _installedFonts = new InstalledFontCollection();
52                         _genericMonospace = new FontFamily(GenericFontFamilies.Monospace);
53                         _genericSansSerif = new FontFamily(GenericFontFamilies.SansSerif);
54                         _genericSerif = new FontFamily(GenericFontFamilies.Serif);
55                 }
56                 
57                 private readonly string _name;
58
59                 private awt.FontMetrics _fontMetrics = null;
60                 private FontStyle _lastStyle = FontStyle.Regular;
61                 private readonly awt.Font _font;
62
63                 // this is unavailable through Java API, usually 2048 for TT fonts
64                 const int UnitsPerEm = 2048;
65                 // the margin for text drawing
66                 const int DrawMargin = 571;
67
68                 #region ctors
69                 
70                 // dummy ctors to work around convertor problems
71                 internal FontFamily() {}
72                 internal FontFamily(IntPtr family) {}
73
74                 static string ToGenericFontName(GenericFontFamilies genericFamily) {
75                         switch(genericFamily) {
76                                 case GenericFontFamilies.SansSerif:
77                                         return "SansSerif";
78                                 case GenericFontFamilies.Serif:
79                                         return "Serif";
80                                 default:
81                                         return "Monospaced";
82                         }
83                 }
84                 
85                 public FontFamily(string familyName) : this(familyName, null) {
86                 }
87
88                 public FontFamily(string name, FontCollection fontCollection) {
89                         if (name == null)
90                                 throw new ArgumentNullException("name");
91
92                         if (fontCollection == null)
93                                 fontCollection = _installedFonts;
94
95                         if (fontCollection.Contains(name))
96                                 _name = name;
97                         else {
98                                 _name = ToGenericFontName(GenericFontFamilies.SansSerif);
99                                 fontCollection = _installedFonts;
100                         }
101
102                         _font = fontCollection.GetInitialFont( _name );
103                 }
104
105                 public FontFamily(GenericFontFamilies genericFamily) : this(ToGenericFontName(genericFamily)) {
106                 }
107                 
108                 #endregion
109
110                 public string Name {
111                         get {
112                                 return _name;
113                         }
114                 }
115
116                 internal int GetDrawMargin(FontStyle style) {
117                         return DrawMargin;
118                 }
119
120                 awt.FontMetrics GetMetrics(FontStyle style) {
121                         if ((_lastStyle != style) || (_fontMetrics == null)) {  
122                                 java.util.Map attrib = Font.DeriveStyle( FamilyFont.getAttributes(), style, true);
123                                 attrib.put(TextAttribute.SIZE, new java.lang.Float((float)(UnitsPerEm<<1)));
124                                 _fontMetrics = Container.getFontMetrics( FamilyFont.deriveFont( attrib ) );
125                         }
126                         return _fontMetrics;
127                 }
128
129                 public int GetCellAscent(FontStyle style) {
130                         return GetMetrics(style).getMaxAscent()>>1;
131                 }
132
133                 public int GetCellDescent(FontStyle style) {
134                         return GetMetrics(style).getMaxDecent()>>1;
135                 }
136
137                 public int GetEmHeight(FontStyle style) {
138                         return UnitsPerEm;
139                 }
140
141                 public int GetLineSpacing(FontStyle style) {
142                         return GetMetrics(style).getHeight()>>1;
143                 }
144
145                 public string GetName(int language) {
146                         try {
147                                 CultureInfo culture = new CultureInfo(language, false);
148                                 java.util.Locale locale = vmw.@internal.EnvironmentUtils.getLocaleFromCultureInfo( culture );
149
150                                 return FamilyFont.getFamily( locale );
151                         }
152                         catch {
153                                 return Name;
154                         }
155                 }
156
157                 public bool IsStyleAvailable(FontStyle style) {
158                         //unable to get this infromation from java
159                         return true;
160                 }
161
162                 #region static members
163
164                 public static FontFamily[] Families {
165                         get {
166                                 return _installedFonts.Families;
167                         }
168                 }
169                 
170                 public static FontFamily GenericMonospace {
171                         get {
172                                 return (FontFamily)_genericMonospace.MemberwiseClone();
173                         }
174                 }
175                 
176                 public static FontFamily GenericSansSerif {
177                         get {
178                                 return (FontFamily)_genericSansSerif.MemberwiseClone();
179                         }
180                 }
181                 
182                 public static FontFamily GenericSerif {
183                         get {
184                                 return (FontFamily)_genericSerif.MemberwiseClone();
185                         }
186                 }               
187
188                 public static FontFamily[] GetFamilies(Graphics graphics) {
189                         if (graphics == null) {
190                                 throw new ArgumentNullException("graphics");
191                         }
192                         return _installedFonts.Families;
193                 }
194
195                 #endregion
196
197                 #region Object members
198
199                 public override bool Equals(object obj) {
200                         if (this == obj)
201                                 return true;
202
203                         if (!(obj is FontFamily))
204                                 return false;
205
206                         return string.Compare(Name, ((FontFamily)obj).Name, true) == 0;
207                 }
208
209                 public override int GetHashCode() {
210                         return Name.ToLower().GetHashCode();
211                 }
212
213                 public override string ToString() {
214                         return string.Format("[{0}: Name={1}]", GetType().Name, Name);
215                 }
216
217                 #endregion
218
219                 #region IDisposable Members
220
221                 public void Dispose() {
222                 }
223
224                 #endregion
225
226                 internal awt.Font FamilyFont {
227                         get {
228                                 return _font;
229                         }
230                 }
231         }
232 }
233