Merge branch 'master' of github.com:mono/mono into masterwork
[mono.git] / mcs / class / System.Drawing / System.Drawing / FontFamily.jvm.cs
index af3d5272cb626118c7de220dc22f375998df783e..900427437620cf1e153b8040883092e6e678f473 100644 (file)
@@ -44,58 +44,65 @@ 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;
+               // the margin for text drawing
+               const int DrawMargin = 571;
 
                #region ctors
                
                // 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
@@ -106,11 +113,15 @@ namespace System.Drawing {
                        }
                }
 
+               internal int GetDrawMargin(FontStyle style) {
+                       return DrawMargin;
+               }
+
                awt.FontMetrics GetMetrics(FontStyle style) {
                        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;
                }
@@ -132,16 +143,15 @@ namespace System.Drawing {
                }
 
                public string GetName(int language) {
-                       CultureInfo culture;
-                       java.util.Locale locale;
                        try {
-                               culture = new CultureInfo(language, false);
-                               locale = vmw.@internal.EnvironmentUtils.getLocaleFromCultureInfo( culture );
+                               CultureInfo culture = new CultureInfo(language, false);
+                               java.util.Locale locale = vmw.@internal.EnvironmentUtils.getLocaleFromCultureInfo( culture );
+
+                               return FamilyFont.getFamily( locale );
                        }
-                       catch(Exception) {
-                               locale = new java.util.Locale("en");
+                       catch {
+                               return Name;
                        }
-                       return FamilyFont.getFamily( locale );
                }
 
                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;
                        }
                }