Code cleanup – fixed typo

C2019-028 Code Cleanup – Added call to .NET 4.6.1 method to get windows font folder, added null check, and added info to the error log to tell us the location of the fonts being used.
This commit is contained in:
John Jenko 2019-07-18 14:03:53 +00:00
parent b0dc9b83d8
commit 41d6ce9483
2 changed files with 33 additions and 7 deletions

View File

@ -89,7 +89,7 @@ namespace VEPROMS
InitializeComponent();
// if the version number of PROMS is 1.0, then we are running a Demo version.
// When running a Demo version, force a "Sample" watermark when printing.
MyPromsPrinter = new PromsPrinter(myItem, rev, (VlnSettings.ReleaseMode.Equals("DEMO")) ? "Sample" : watermark, debugOutput, origPgBrk, pdfPath + @"\\Compare", false, overWrite, cbd, pdfFile, insertBlankPages, allOrAuto,Prefix,saveLinks,removeTrailingHardReturnsAndManualPageBreaks, blankPageText, DidAll);
MyPromsPrinter = new PromsPrinter(myItem, rev, (VlnSettings.ReleaseMode.Equals("DEMO")) ? "Sample" : watermark, debugOutput, origPgBrk, pdfPath + @"\Compare", false, overWrite, cbd, pdfFile, insertBlankPages, allOrAuto,Prefix,saveLinks,removeTrailingHardReturnsAndManualPageBreaks, blankPageText, DidAll);
MyPromsPrinter.PromsVersion = (showPROMSVer) ? AboutVEPROMS.PROMSVersion : ""; //C2018-009 print PROMS version
PDFPath = pdfPath;
this.Text = "Creating PDF of " + myItem.DisplayNumber;

View File

@ -20,6 +20,7 @@ namespace Volian.Svg.Library
{
public static class FontFind
{
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public static string FileName(string fontName)
{
string baseFont = string.Empty;
@ -38,16 +39,34 @@ namespace Volian.Svg.Library
}
private static string _FontDir = null;
public static string FontDir
{
{
get
{
if (_FontDir == null)
{
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders");
if (regKey != null)
_FontDir = regKey.GetValue("Fonts").ToString();
if (_FontDir == null) // Not allowed or cannot find the fonts folder value in the registry.
_FontDir = @"C:\Windows\Fonts"; // default to C:\Windows\Fonts
if (_FontDir == null)
{
RegistryKey regKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders");
if (regKey != null)
{
object obj = regKey.GetValue("Fonts");
_MyLog.WarnFormat("Font regkey.fonts = {0}", obj); // C2019-028 Add info in the error log
// C2019-028 Add Null check before attempt to convert to string
if (obj != null)
_FontDir = obj.ToString();
else
{
// C2019-028 Add call to .Net 4.6.1 method to get the system's windows font folder
_FontDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts);
_MyLog.WarnFormat("Font Folder = {0}", _FontDir); // C2019-028 Add info in the error log
}
}
if (_FontDir == null) // Not allowed or cannot find the fonts folder value in the registry.
{
_FontDir = @"C:\Windows\Fonts"; // default to C:\Windows\Fonts
_MyLog.WarnFormat("FontDir set to default = {0}", _FontDir); // C2019-028 Add info in the error log
}
}
}
return _FontDir;
}
@ -116,8 +135,15 @@ namespace Volian.Svg.Library
private static string _FontFolder = (String)Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Explorer").OpenSubKey("Shell Folders").GetValue("Fonts");
public static string FontFolder
{ get { return _FontFolder; } }
// keep for debug
//private static List<string> _RegFonts = new List<string>();
public static iTextSharp.text.Font GetFont(string fontName, int size, int style)
{
//if (!_RegFonts.Contains(fontName))
//{
//_MyLog.WarnFormat("Registering Font {0}", fontName);
//_RegFonts.Add(fontName);
//}
RegisterFont(fontName);
return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, size / 2, style);
}