2005-10-04 Zoltan Varga <vargaz@freemail.hu>
[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
38 namespace System.Drawing {
39
40         public sealed class FontFamily : MarshalByRefObject, IDisposable {
41                 
42                 static readonly FontFamily _genericMonospace;
43                 static readonly FontFamily _genericSansSerif;
44                 static readonly FontFamily _genericSerif;
45                 static readonly FontCollection _installedFonts;
46
47                 static FontFamily() {
48                         _installedFonts = new InstalledFontCollection();
49                         _genericMonospace = new FontFamily("Monospaced");
50                         _genericSansSerif = new FontFamily("SansSerif");
51                         _genericSerif = new FontFamily("Serif");
52                 }
53                 
54                 private readonly string _name;
55
56                 // this is unavailable through Java API, usually 2048 for TT fonts
57                 const int UnitsPerEm = 2048;
58                 
59                 
60 //              ~FontFamily()
61 //              {       
62 //              }
63
64                 // dummy ctors to work around convertor problems
65                 internal FontFamily() {}
66                 internal FontFamily(IntPtr family) {}
67                 
68                 public FontFamily(string familyName) : this(familyName, null)
69                 {}
70
71                 public FontFamily(string name, FontCollection fontCollection) {
72                         if (fontCollection == null)
73                                 fontCollection = _installedFonts;
74
75                         string familyName = fontCollection.GetFamilyName(name);
76                         if (familyName == null)
77                                 throw new ArgumentException(String.Format("Font family '{0}' not found", name));
78
79                         _name = familyName;
80                 }
81
82                 public FontFamily(GenericFontFamilies genericFamily) {
83                         switch(genericFamily) {
84                                 case GenericFontFamilies.SansSerif:
85                                         _name = _genericSansSerif._name;
86                                         break;
87                                 case GenericFontFamilies.Serif:
88                                         _name = _genericSerif._name;
89                                         break;
90                                 default:
91                                         _name = _genericMonospace._name;
92                                         break;
93                         }
94                 }
95                 
96                 
97                 public string Name {
98                         get {
99                                 return _name;
100                         }
101                 }
102
103                 public static FontFamily[] Families {
104                         get {
105                                 return _installedFonts.Families;
106                         }
107                 }
108                 
109                 public static FontFamily GenericMonospace {
110                         get {
111                                 return _genericMonospace;
112                         }
113                 }
114                 
115                 public static FontFamily GenericSansSerif {
116                         get {
117                                 return _genericSansSerif;
118                         }
119                 }
120                 
121                 public static FontFamily GenericSerif {
122                         get {
123                                 return _genericSerif;
124                         }
125                 }               
126
127                 public override bool Equals(object obj) {
128                         if (this == obj)
129                                 return true;
130
131                         if (!(obj is FontFamily))
132                                 return false;
133
134                         return string.Compare(Name, ((FontFamily)obj).Name, true) == 0;
135                 }
136
137                 awt.FontMetrics GetMetrics(FontStyle style) {
138                         awt.Container c = new awt.Container();
139                         return c.getFontMetrics(new Font(this, (float)(UnitsPerEm<<1), style, GraphicsUnit.World).NativeObject);
140                 }
141
142                 public int GetCellAscent(FontStyle style) {
143                         return GetMetrics(style).getMaxAscent()>>1;
144                 }
145
146                 public int GetCellDescent(FontStyle style) {
147                         return GetMetrics(style).getMaxDecent()>>1;
148                 }
149
150                 public int GetEmHeight(FontStyle style) {
151                         return UnitsPerEm;
152                 }
153
154                 public static FontFamily[] GetFamilies(Graphics graphics) {
155                         if (graphics == null) {
156                                 throw new ArgumentNullException("graphics");
157                         }
158                         return _installedFonts.Families;
159                 }
160
161                 public override int GetHashCode() {
162                         return Name.ToLower().GetHashCode();
163                 }
164
165                 public int GetLineSpacing(FontStyle style) {
166                         return GetMetrics(style).getHeight()>>1;
167                 }
168
169                 public string GetName(int language) {
170                         CultureInfo culture = new CultureInfo(language, false);
171                         //TBD: get java locale
172                         return new awt.Font(_name, awt.Font.PLAIN, 1).getFamily(null);
173                 }
174
175                 public bool IsStyleAvailable(FontStyle style) {
176                         return (new Font(this, (float)(UnitsPerEm<<1), style, GraphicsUnit.World).Style & style) == style;
177                 }
178
179                 public override string ToString() {
180                         return string.Format("[{0}: Name={1}]", GetType().Name, Name);
181                 }
182
183
184
185                 #region IDisposable Members
186
187                 public void Dispose() {
188                         // TODO:  Add FontFamily.Dispose implementation
189                 }
190
191                 #endregion
192         }
193 }
194