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