OSDN Git Service

#39080 Fixed not to close DTXMania suddenly when go to CONFIGURATION, in Win8.1.
authoryyagi <yyagi.dtxmania@gmail.com>
Sat, 23 Mar 2019 04:22:03 +0000 (13:22 +0900)
committeryyagi <yyagi.dtxmania@gmail.com>
Sat, 23 Mar 2019 04:22:03 +0000 (13:22 +0900)
The fix avoids IndexOutOfRange() Exception when registering private font without font file name.

DTXMania/コード/全体/CPrivateFont.cs

index 0427a38..8cdc06c 100644 (file)
@@ -105,36 +105,51 @@ namespace DTXMania
                        }
                        else
                        {
-                               try
+                               if (Path.GetFileName(fontpath) == "")
                                {
-                                       this._pfc = new System.Drawing.Text.PrivateFontCollection();    //PrivateFontCollectionオブジェクトを作成する
-                                       this._pfc.AddFontFile(fontpath);                                                                //PrivateFontCollectionにフォントを追加する
-                                       _fontfamily = _pfc.Families[0];
+                                       Trace.TraceWarning($"No font filename is specified (only path is specified, etc). Trying to use MS PGothic as alternative. ({fontpath})");
+                                       _fontfamily = null;
                                }
-                               catch (Exception e) when (e is System.IO.FileNotFoundException || e is System.Runtime.InteropServices.ExternalException)
+                               else
                                {
-                                       Trace.TraceWarning(e.Message);
-                                       Trace.TraceWarning("プライベートフォントの追加に失敗しました({0})。代わりにMS PGothicの使用を試みます。", fontpath);
-                                       //throw new FileNotFoundException( "プライベートフォントの追加に失敗しました。({0})", Path.GetFileName( fontpath ) );
-                                       //return;
+                                       try
+                                       {
+                                               this._pfc = new System.Drawing.Text.PrivateFontCollection();    //PrivateFontCollectionオブジェクトを作成する
+                                               this._pfc.AddFontFile(fontpath);                                //PrivateFontCollectionにフォントを追加する
+                                               _fontfamily = _pfc.Families[0];
+                                       }
+                                       catch (Exception e) when (e is System.IO.FileNotFoundException || e is System.Runtime.InteropServices.ExternalException)
+                                       {
+                                               Trace.TraceWarning(e.Message);
+                                               Trace.TraceWarning("プライベートフォントの追加に失敗しました({0})。代わりにMS PGothicの使用を試みます。", fontpath);
+                                               //throw new FileNotFoundException( "プライベートフォントの追加に失敗しました。({0})", Path.GetFileName( fontpath ) );
+                                               //return;
 
-                                       _fontfamily = null;
+                                               _fontfamily = null;
+                                       }
+                                       catch (IndexOutOfRangeException e)
+                                       {
+                                               Trace.TraceWarning(e.Message);
+                                               Trace.TraceWarning($"AddFontFile() succeeded, but not reflected to the Array of Families. Failed to add PrivateFont({fontpath}).");
+                                               _fontfamily = null;
+                                       }
+
+                                       //foreach ( FontFamily ff in _pfc.Families )
+                                       //{
+                                       //      Debug.WriteLine( "fontname=" + ff.Name );
+                                       //      if ( ff.Name == Path.GetFileNameWithoutExtension( fontpath ) )
+                                       //      {
+                                       //              _fontfamily = ff;
+                                       //              break;
+                                       //      }
+                                       //}
+                                       //if ( _fontfamily == null )
+                                       //{
+                                       //      Trace.TraceError( "プライベートフォントの追加後、検索に失敗しました。({0})", fontpath );
+                                       //      return;
+                                       //}
                                }
 
-                               //foreach ( FontFamily ff in _pfc.Families )
-                               //{
-                               //      Debug.WriteLine( "fontname=" + ff.Name );
-                               //      if ( ff.Name == Path.GetFileNameWithoutExtension( fontpath ) )
-                               //      {
-                               //              _fontfamily = ff;
-                               //              break;
-                               //      }
-                               //}
-                               //if ( _fontfamily == null )
-                               //{
-                               //      Trace.TraceError( "プライベートフォントの追加後、検索に失敗しました。({0})", fontpath );
-                               //      return;
-                               //}
                        }
 
                        // 指定されたフォントスタイルが適用できない場合は、フォント内で定義されているスタイルから候補を選んで使用する