2 // System.Drawing.FontFamily.cs
4 // (C) 2005 Mainsoft Corporation (http://www.mainsoft.com)
5 // Author: Konstantin Triger (kostat@mainsoft.com)
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
30 using System.Drawing.Text;
32 using System.Runtime.InteropServices;
33 using System.Globalization;
35 using geom = java.awt.geom;
36 using font = java.awt.font;
38 namespace System.Drawing {
40 public sealed class FontFamily : MarshalByRefObject, IDisposable {
42 static readonly FontFamily _genericMonospace;
43 static readonly FontFamily _genericSansSerif;
44 static readonly FontFamily _genericSerif;
45 static readonly FontCollection _installedFonts;
48 _installedFonts = new InstalledFontCollection();
49 _genericMonospace = new FontFamily("Monospaced");
50 _genericSansSerif = new FontFamily("SansSerif");
51 _genericSerif = new FontFamily("Serif");
54 private readonly string _name;
56 // this is unavailable through Java API, usually 2048 for TT fonts
57 const int UnitsPerEm = 2048;
64 // dummy ctors to work around convertor problems
65 internal FontFamily() {}
66 internal FontFamily(IntPtr family) {}
68 public FontFamily(string familyName) : this(familyName, null)
71 public FontFamily(string name, FontCollection fontCollection) {
72 if (fontCollection == null)
73 fontCollection = _installedFonts;
75 string familyName = fontCollection.GetFamilyName(name);
76 if (familyName == null)
77 throw new ArgumentException(String.Format("Font family '{0}' not found", name));
82 public FontFamily(GenericFontFamilies genericFamily) {
83 switch(genericFamily) {
84 case GenericFontFamilies.SansSerif:
85 _name = _genericSansSerif._name;
87 case GenericFontFamilies.Serif:
88 _name = _genericSerif._name;
91 _name = _genericMonospace._name;
103 public static FontFamily[] Families {
105 return _installedFonts.Families;
109 public static FontFamily GenericMonospace {
111 return _genericMonospace;
115 public static FontFamily GenericSansSerif {
117 return _genericSansSerif;
121 public static FontFamily GenericSerif {
123 return _genericSerif;
127 public override bool Equals(object obj) {
131 if (!(obj is FontFamily))
134 return string.Compare(Name, ((FontFamily)obj).Name, true) == 0;
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);
142 public int GetCellAscent(FontStyle style) {
143 return GetMetrics(style).getMaxAscent()>>1;
146 public int GetCellDescent(FontStyle style) {
147 return GetMetrics(style).getMaxDecent()>>1;
150 public int GetEmHeight(FontStyle style) {
154 public static FontFamily[] GetFamilies(Graphics graphics) {
155 if (graphics == null) {
156 throw new ArgumentNullException("graphics");
158 return _installedFonts.Families;
161 public override int GetHashCode() {
162 return Name.ToLower().GetHashCode();
165 public int GetLineSpacing(FontStyle style) {
166 return GetMetrics(style).getHeight()>>1;
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);
175 public bool IsStyleAvailable(FontStyle style) {
176 return (new Font(this, (float)(UnitsPerEm<<1), style, GraphicsUnit.World).Style & style) == style;
179 public override string ToString() {
180 return string.Format("[{0}: Name={1}]", GetType().Name, Name);
185 #region IDisposable Members
187 public void Dispose() {
188 // TODO: Add FontFamily.Dispose implementation