Merge branch 'master' of github.com:mono/mono into masterwork
[mono.git] / mcs / class / System.Drawing / System.Drawing / FontFamily.jvm.cs
index 92b16d62e8f1baf9df34a0793b1b1cacef79fa1d..900427437620cf1e153b8040883092e6e678f473 100644 (file)
@@ -44,21 +44,21 @@ namespace System.Drawing {
                static readonly FontFamily _genericSansSerif;
                static readonly FontFamily _genericSerif;
                static readonly FontCollection _installedFonts;
-               static readonly awt.Container _container = new awt.Container();
+               internal static readonly awt.Container Container;
 
                static FontFamily() {
+                       Container = new awt.Container();
                        _installedFonts = new InstalledFontCollection();
-                       _genericMonospace = new FontFamily("Monospaced");
-                       _genericSansSerif = new FontFamily("SansSerif");
-                       _genericSerif = new FontFamily("Serif");
+                       _genericMonospace = new FontFamily(GenericFontFamilies.Monospace);
+                       _genericSansSerif = new FontFamily(GenericFontFamilies.SansSerif);
+                       _genericSerif = new FontFamily(GenericFontFamilies.Serif);
                }
                
                private readonly string _name;
-               private readonly FontCollection _fontCollection;
 
                private awt.FontMetrics _fontMetrics = null;
                private FontStyle _lastStyle = FontStyle.Regular;
-               private awt.Font _font;
+               private readonly awt.Font _font;
 
                // this is unavailable through Java API, usually 2048 for TT fonts
                const int UnitsPerEm = 2048;
@@ -70,34 +70,39 @@ namespace System.Drawing {
                // dummy ctors to work around convertor problems
                internal FontFamily() {}
                internal FontFamily(IntPtr family) {}
+
+               static string ToGenericFontName(GenericFontFamilies genericFamily) {
+                       switch(genericFamily) {
+                               case GenericFontFamilies.SansSerif:
+                                       return "SansSerif";
+                               case GenericFontFamilies.Serif:
+                                       return "Serif";
+                               default:
+                                       return "Monospaced";
+                       }
+               }
                
                public FontFamily(string familyName) : this(familyName, null) {
                }
 
                public FontFamily(string name, FontCollection fontCollection) {
+                       if (name == null)
+                               throw new ArgumentNullException("name");
+
                        if (fontCollection == null)
-                               _fontCollection = _installedFonts;
-                       else
-                               _fontCollection = fontCollection;
+                               fontCollection = _installedFonts;
 
-                       if ( !_fontCollection.Contains(name) )
-                               _name = _genericSansSerif._name;
+                       if (fontCollection.Contains(name))
+                               _name = name;
+                       else {
+                               _name = ToGenericFontName(GenericFontFamilies.SansSerif);
+                               fontCollection = _installedFonts;
+                       }
 
-                       _name = name;
+                       _font = fontCollection.GetInitialFont( _name );
                }
 
-               public FontFamily(GenericFontFamilies genericFamily) {
-                       switch(genericFamily) {
-                               case GenericFontFamilies.SansSerif:
-                                       _name = _genericSansSerif._name;
-                                       break;
-                               case GenericFontFamilies.Serif:
-                                       _name = _genericSerif._name;
-                                       break;
-                               default:
-                                       _name = _genericMonospace._name;
-                                       break;
-                       }
+               public FontFamily(GenericFontFamilies genericFamily) : this(ToGenericFontName(genericFamily)) {
                }
                
                #endregion
@@ -116,7 +121,7 @@ namespace System.Drawing {
                        if ((_lastStyle != style) || (_fontMetrics == null)) {  
                                java.util.Map attrib = Font.DeriveStyle( FamilyFont.getAttributes(), style, true);
                                attrib.put(TextAttribute.SIZE, new java.lang.Float((float)(UnitsPerEm<<1)));
-                               _fontMetrics = _container.getFontMetrics( FamilyFont.deriveFont( attrib ) );
+                               _fontMetrics = Container.getFontMetrics( FamilyFont.deriveFont( attrib ) );
                        }
                        return _fontMetrics;
                }
@@ -138,10 +143,15 @@ namespace System.Drawing {
                }
 
                public string GetName(int language) {
-                       CultureInfo culture = new CultureInfo(language, false);
-                       java.util.Locale locale = vmw.@internal.EnvironmentUtils.getLocaleFromCultureInfo( culture );
+                       try {
+                               CultureInfo culture = new CultureInfo(language, false);
+                               java.util.Locale locale = vmw.@internal.EnvironmentUtils.getLocaleFromCultureInfo( culture );
 
-                       return FamilyFont.getFamily( locale );
+                               return FamilyFont.getFamily( locale );
+                       }
+                       catch {
+                               return Name;
+                       }
                }
 
                public bool IsStyleAvailable(FontStyle style) {
@@ -209,16 +219,12 @@ namespace System.Drawing {
                #region IDisposable Members
 
                public void Dispose() {
-                       // TODO:  Add FontFamily.Dispose implementation
                }
 
                #endregion
 
                internal awt.Font FamilyFont {
                        get {
-                               if (_font == null)
-                                       _font = _fontCollection.GetInitialFont( _name );
-
                                return _font;
                        }
                }