merge -r 54590:54591
[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                 static readonly awt.Container _container = new awt.Container();
48
49                 static FontFamily() {
50                         _installedFonts = new InstalledFontCollection();
51                         _genericMonospace = new FontFamily(GenericFontFamilies.Monospace);
52                         _genericSansSerif = new FontFamily(GenericFontFamilies.SansSerif);
53                         _genericSerif = new FontFamily(GenericFontFamilies.Serif);
54                 }
55                 
56                 private readonly string _name;
57
58                 private awt.FontMetrics _fontMetrics = null;
59                 private FontStyle _lastStyle = FontStyle.Regular;
60                 private readonly awt.Font _font;
61
62                 // this is unavailable through Java API, usually 2048 for TT fonts
63                 const int UnitsPerEm = 2048;
64                 // the margin for text drawing
65                 const int DrawMargin = 571;
66
67                 #region ctors
68                 
69                 // dummy ctors to work around convertor problems
70                 internal FontFamily() {}
71                 internal FontFamily(IntPtr family) {}
72
73                 static string ToGenericFontName(GenericFontFamilies genericFamily) {
74                         switch(genericFamily) {
75                                 case GenericFontFamilies.SansSerif:
76                                         return "SansSerif";
77                                 case GenericFontFamilies.Serif:
78                                         return "Serif";
79                                 default:
80                                         return "Monospaced";
81                         }
82                 }
83                 
84                 public FontFamily(string familyName) : this(familyName, null) {
85                 }
86
87                 public FontFamily(string name, FontCollection fontCollection) {
88                         if (name == null)
89                                 throw new ArgumentNullException("name");
90
91                         if (fontCollection == null)
92                                 fontCollection = _installedFonts;
93
94                         if (fontCollection.Contains(name))
95                                 _name = name;
96                         else {
97                                 _name = ToGenericFontName(GenericFontFamilies.SansSerif);
98                                 fontCollection = _installedFonts;
99                         }
100
101                         _font = fontCollection.GetInitialFont( _name );
102                 }
103
104                 public FontFamily(GenericFontFamilies genericFamily) : this(ToGenericFontName(genericFamily)) {
105                 }
106                 
107                 #endregion
108
109                 public string Name {
110                         get {
111                                 return _name;
112                         }
113                 }
114
115                 internal int GetDrawMargin(FontStyle style) {
116                         return DrawMargin;
117                 }
118
119                 awt.FontMetrics GetMetrics(FontStyle style) {
120                         if ((_lastStyle != style) || (_fontMetrics == null)) {  
121                                 java.util.Map attrib = Font.DeriveStyle( FamilyFont.getAttributes(), style, true);
122                                 attrib.put(TextAttribute.SIZE, new java.lang.Float((float)(UnitsPerEm<<1)));
123                                 _fontMetrics = _container.getFontMetrics( FamilyFont.deriveFont( attrib ) );
124                         }
125                         return _fontMetrics;
126                 }
127
128                 public int GetCellAscent(FontStyle style) {
129                         return GetMetrics(style).getMaxAscent()>>1;
130                 }
131
132                 public int GetCellDescent(FontStyle style) {
133                         return GetMetrics(style).getMaxDecent()>>1;
134                 }
135
136                 public int GetEmHeight(FontStyle style) {
137                         return UnitsPerEm;
138                 }
139
140                 public int GetLineSpacing(FontStyle style) {
141                         return GetMetrics(style).getHeight()>>1;
142                 }
143
144                 public string GetName(int language) {
145                         try {
146                                 CultureInfo culture = new CultureInfo(language, false);
147                                 java.util.Locale locale = vmw.@internal.EnvironmentUtils.getLocaleFromCultureInfo( culture );
148
149                                 return FamilyFont.getFamily( locale );
150                         }
151                         catch {
152                                 return Name;
153                         }
154                 }
155
156                 public bool IsStyleAvailable(FontStyle style) {
157                         //unable to get this infromation from java
158                         return true;
159                 }
160
161                 #region static members
162
163                 public static FontFamily[] Families {
164                         get {
165                                 return _installedFonts.Families;
166                         }
167                 }
168                 
169                 public static FontFamily GenericMonospace {
170                         get {
171                                 return (FontFamily)_genericMonospace.MemberwiseClone();
172                         }
173                 }
174                 
175                 public static FontFamily GenericSansSerif {
176                         get {
177                                 return (FontFamily)_genericSansSerif.MemberwiseClone();
178                         }
179                 }
180                 
181                 public static FontFamily GenericSerif {
182                         get {
183                                 return (FontFamily)_genericSerif.MemberwiseClone();
184                         }
185                 }               
186
187                 public static FontFamily[] GetFamilies(Graphics graphics) {
188                         if (graphics == null) {
189                                 throw new ArgumentNullException("graphics");
190                         }
191                         return _installedFonts.Families;
192                 }
193
194                 #endregion
195
196                 #region Object members
197
198                 public override bool Equals(object obj) {
199                         if (this == obj)
200                                 return true;
201
202                         if (!(obj is FontFamily))
203                                 return false;
204
205                         return string.Compare(Name, ((FontFamily)obj).Name, true) == 0;
206                 }
207
208                 public override int GetHashCode() {
209                         return Name.ToLower().GetHashCode();
210                 }
211
212                 public override string ToString() {
213                         return string.Format("[{0}: Name={1}]", GetType().Name, Name);
214                 }
215
216                 #endregion
217
218                 #region IDisposable Members
219
220                 [MonoTODO]
221                 public void Dispose() {
222                         // TODO:  Add FontFamily.Dispose implementation
223                 }
224
225                 #endregion
226
227                 internal awt.Font FamilyFont {
228                         get {
229                                 return _font;
230                         }
231                 }
232         }
233 }
234