B2019-112

Error log font directory info
Error handler for font usage.
This commit is contained in:
Rich 2019-08-06 20:36:30 +00:00
parent 4e730bb092
commit d4a37e2c4c

View File

@ -44,22 +44,21 @@ namespace Volian.Svg.Library
{
if (_FontDir == null)
{
// B2019-112 Error Log Font Dir
// C2019-028, B2019-099 Add call to .Net 4.6.1 method to get the system's windows font folder
_FontDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts);
if (_FontDir != null) _MyLog.WarnFormat("Special Font Folder = {0}", _FontDir); // C2019-028 Add info in the error log
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
// B2019-099 Add more debug
if (obj != null) _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.
{
@ -82,19 +81,34 @@ namespace Volian.Svg.Library
pdfr.Close();
return retval;
}
private static string _PromsFontDir = null;//B2019-099 Consistent Font Retrieve
public static void RegisterFont(string fontName)
{
int profileDepth = ProfileTimer.Push(string.Format(">>>> RegisterFont {0}",fontName));
if (!iTextSharp.text.FontFactory.IsRegistered(fontName))
{
string fntdir = Volian.Base.Library.VlnSettings.GetCommand("PromsFonts", FontFind.FontDir);
int profileDepth1 = ProfileTimer.Push(">>>> RegisterDirectory " + fntdir);
iTextSharp.text.FontFactory.RegisterDirectory(fntdir);
if (_PromsFontDir == null)//B2019-099 Consistent Font Retrieve
{
_PromsFontDir = Volian.Base.Library.VlnSettings.GetCommand("PromsFonts", "");
if (_PromsFontDir == "")
_PromsFontDir = FontFind.FontDir;
else
{
DirectoryInfo di = new DirectoryInfo(_PromsFontDir);
if (!di.Exists)
_PromsFontDir = FontFind.FontDir;
else
_MyLog.WarnFormat("PROMS Font Folder = {0}", _PromsFontDir); // C2019-028 Add info in the error log
}
}
int profileDepth1 = ProfileTimer.Push(">>>> RegisterDirectory " + _PromsFontDir);
iTextSharp.text.FontFactory.RegisterDirectory(_PromsFontDir);
ProfileTimer.Pop(profileDepth1);
if (!iTextSharp.text.FontFactory.IsRegistered(fontName))
{
_MyLog.WarnFormat("Problem with Font {0} in {1}", fontName, fntdir);
if (fntdir != FontFind.FontDir)
//B2019-099 Consistent Font Retrieve
_MyLog.WarnFormat("Problem with Font {0} in {1}", fontName, _PromsFontDir);
if (_PromsFontDir != FontFind.FontDir)
{
int profileDepth2 = ProfileTimer.Push(">>>> RegisterDirectory " + FontFind.FontDir);
iTextSharp.text.FontFactory.RegisterDirectory(FontFind.FontDir);
@ -219,6 +233,7 @@ namespace Volian.Svg.Library
}
public partial class Svg : SvgGroup
{
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
public void DrawMacro(string macroDef, float x, float y, PdfContentByte cb)
{
_MyContentByte = cb;
@ -317,7 +332,9 @@ namespace Volian.Svg.Library
if (name.StartsWith(fontName))
{
string fontFile = (string) FontKey.GetValue(name);
FontFactory.Register(fontFile.Contains("\\") ? fontFile : FontFolder + "\\" + fontFile);
//FontFactory.Register(fontFile.Contains("\\") ? fontFile : FontFolder + "\\" + fontFile);
//B2019-099 Consistent Font Retrieve
FontFactory.Register(fontFile.Contains("\\") ? fontFile : FontFind.FontDir + "\\" + fontFile);
}
}
}
@ -325,9 +342,20 @@ namespace Volian.Svg.Library
private static RegistryKey _FontKey = Registry.LocalMachine.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows NT").OpenSubKey("CurrentVersion").OpenSubKey("Fonts");
public static RegistryKey FontKey
{ get { return _FontKey; } }
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; } }
//private static string _FontFolder = null;
//public static string FontFolder
//{
// get
// {
// if (_FontFolder == null)
// {
// _FontFolder = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Fonts);
// if (_FontFolder == null) _FontFolder = (String)Registry.CurrentUser.OpenSubKey("Software").OpenSubKey("Microsoft").OpenSubKey("Windows").OpenSubKey("CurrentVersion").OpenSubKey("Explorer").OpenSubKey("Shell Folders").GetValue("Fontz");
// _MyLog.WarnFormat("FontFolder set to {0}", _FontFolder);
// }
// return _FontFolder;
// }
//}
private static int _StackTraceSkip = 1;
private static DateTime _LastTime = DateTime.Now;
private static DateTime _StartTime = DateTime.Now;
@ -660,6 +688,7 @@ namespace Volian.Svg.Library
}
public partial class SvgText : SvgShapePart
{
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
private static string ReplaceSpecialCharacter(Match m)
{
int i = int.Parse(m.ToString().Substring(2, m.ToString().Length - 3));
@ -922,6 +951,14 @@ namespace Volian.Svg.Library
}
iTextSharp.text.Font font = Svg.GetFont(Font.Name);
BaseFont baseFont = font.BaseFont;
if (baseFont == null)
{
Svg.RegisterFont(Font.Name);
font = Svg.GetFont(Font.Name);
baseFont = font.BaseFont;
if(baseFont==null)
_MyLog.WarnFormat("Missing Font {0}", Font.Name);
}
tmp.MoveText(scale.X(X), yScale);
tmp.SetFontAndSize(baseFont, scale.M(new SvgMeasurement(Font.SizeInPoints, E_MeasurementUnits.PT)));
switch ((stroke ? 1 : 0) + (fill ? 2 : 0))