From 93a7e70ca3d0b435bd05e6d297e1ed0f88e556b5 Mon Sep 17 00:00:00 2001 From: Flamefire Date: Wed, 10 Sep 2014 18:10:34 +0200 Subject: [PATCH] Add Assertions for empty fontName Simplify Dispose of fonts Check for existing fonts when none found --- Vocaluxe/Base/Fonts/CFonts.cs | 59 ++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 29 deletions(-) diff --git a/Vocaluxe/Base/Fonts/CFonts.cs b/Vocaluxe/Base/Fonts/CFonts.cs index 3888214f..a617dfd5 100644 --- a/Vocaluxe/Base/Fonts/CFonts.cs +++ b/Vocaluxe/Base/Fonts/CFonts.cs @@ -17,6 +17,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.Drawing; using System.IO; using System.Linq; @@ -49,6 +50,14 @@ namespace Vocaluxe.Base.Fonts public CFontStyle Italic; public CFontStyle Bold; public CFontStyle BoldItalic; + + public void Dispose() + { + Normal.Dispose(); + Italic.Dispose(); + Bold.Dispose(); + BoldItalic.Dispose(); + } } static class CFonts @@ -70,12 +79,7 @@ namespace Vocaluxe.Base.Fonts public static void Close() { foreach (SFontFamily font in _FontFamilies) - { - font.Normal.Dispose(); - font.Bold.Dispose(); - font.Italic.Dispose(); - font.BoldItalic.Dispose(); - } + font.Dispose(); _FontFamilies.Clear(); _IsInitialized = false; } @@ -227,35 +231,36 @@ namespace Vocaluxe.Base.Fonts private static int _GetFontIndex(string fontName) { - int index = _GetPartyFontIndex(fontName, PartyModeID); - if (index < 0) - index = _GetThemeFontIndex(fontName, CConfig.Theme); - if (index >= 0) - return index; - for (int i = 0; i < _FontFamilies.Count; i++) - { - if (_FontFamilies[i].Name == fontName) - return i; - } - if (index < 0) + if (fontName != "") { + int index = _GetPartyFontIndex(fontName, PartyModeID); + if (index < 0) + index = _GetThemeFontIndex(fontName, CConfig.Theme); + if (index >= 0) + return index; + for (int i = 0; i < _FontFamilies.Count; i++) + { + if (_FontFamilies[i].Name == fontName) + return i; + } if (!_LoggedMissingFonts.Contains(fontName)) { _LoggedMissingFonts.Add(fontName); CLog.LogError("Font \"" + fontName + "\" not found!"); } - if (_FontFamilies.Count == 0) - CLog.LogError("No fonts found!", true, true); - index = 0; } + else + CLog.LogError("Empty fontName requested", false, false, new Exception()); - return index; + if (_FontFamilies.Count == 0) + CLog.LogError("No fonts found!", true, true); + + return 0; } private static int _GetThemeFontIndex(string fontName, string themeName) { - if (themeName == "" || fontName == "") - return -1; + Debug.Assert(fontName != "" && themeName != ""); for (int i = 0; i < _FontFamilies.Count; i++) { @@ -268,8 +273,7 @@ namespace Vocaluxe.Base.Fonts private static int _GetPartyFontIndex(string fontName, int partyModeID) { - if (partyModeID == -1 || fontName == "") - return -1; + Debug.Assert(fontName != ""); for (int i = 0; i < _FontFamilies.Count; i++) { @@ -405,10 +409,7 @@ namespace Vocaluxe.Base.Fonts { if (_FontFamilies[index].ThemeName == themeName) { - _FontFamilies[index].Normal.Dispose(); - _FontFamilies[index].Italic.Dispose(); - _FontFamilies[index].Bold.Dispose(); - _FontFamilies[index].BoldItalic.Dispose(); + _FontFamilies[index].Dispose(); _FontFamilies.RemoveAt(index); } else