B2019-116 Consolidated and Genericized font registration for iTextSharp

This commit is contained in:
Rich 2019-08-15 15:59:59 +00:00
parent da3a87e892
commit a804f2f716
4 changed files with 49 additions and 203 deletions

View File

@ -1534,6 +1534,10 @@ namespace VEPROMS
private bool _frmVEPROMSloading = false; private bool _frmVEPROMSloading = false;
private void frmVEPROMS_Load(object sender, EventArgs e) private void frmVEPROMS_Load(object sender, EventArgs e)
{ {
// B2019-116 Use Volian.Base.Library.VlnItextFont
// This is a generic class for dealing with iTextSharp Fonts
// Code moved and consolidated from Volian.Print.Library, Volian PDF.Library and VG
Volian.Base.Library.VlnItextFont.RegisterPromsFonts();
InitializeSecurity(); InitializeSecurity();
UpdateUser(); UpdateUser();

View File

@ -5,6 +5,7 @@ using System.Drawing;
using iTextSharp.text.pdf; using iTextSharp.text.pdf;
using iTextSharp.text.factories; using iTextSharp.text.factories;
using Microsoft.Win32; using Microsoft.Win32;
using Volian.Base.Library;
namespace VG namespace VG
{ {
@ -149,36 +150,13 @@ namespace VG
{ get { return 1; } } // Changed to adjust for Screen DPI Setting { get { return 1; } } // Changed to adjust for Screen DPI Setting
public static iTextSharp.text.Font GetFont(string fontName) public static iTextSharp.text.Font GetFont(string fontName)
{ {
RegisterFont(fontName); /// <summary>
/// B2019-116 Volian.Base.Library This is a generic class for dealing with iTextSharp Fonts
/// Code moved and consolidated from Volian.Print.Library Volian PDF.Library and VG
/// </summary>
VlnItextFont.RegisterFont(fontName);
return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED); return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
} }
//private void RegisterFonts()
// {
// //if (!FontFactory.IsRegistered("Arial"))
// //{
// // RegisterFont("Prestige Elite Tall");
// //}
//}
public static void RegisterFont(string fontName)
{
if (!iTextSharp.text.FontFactory.IsRegistered(fontName))
{
foreach (string name in FontKey.GetValueNames())
{
if (name.StartsWith(fontName))
{
string fontFile = (string)FontKey.GetValue(name);
iTextSharp.text.FontFactory.Register(fontFile.Contains("\\") ? fontFile : FontFolder + "\\" + fontFile);
}
}
}
}
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 float ScaleY(float y) private float ScaleY(float y)
{ {
return _VGOutput.PdfWriter.PageSize.Height - y; return _VGOutput.PdfWriter.PageSize.Height - y;

View File

@ -5,6 +5,7 @@ using iTextSharp.text;
using iTextSharp.text.pdf; using iTextSharp.text.pdf;
using System.IO; using System.IO;
using Microsoft.Win32; using Microsoft.Win32;
using Volian.Base.Library;
namespace Volian.Print.Library namespace Volian.Print.Library
{ {
@ -118,28 +119,11 @@ namespace Volian.Print.Library
} }
public static iTextSharp.text.Font GetFont(string fontName, float fontSize, int fontStyle, Color fontColor) public static iTextSharp.text.Font GetFont(string fontName, float fontSize, int fontStyle, Color fontColor)
{ {
RegisterFont(fontName); // B2019-116 Use Volian.Base.Library.VlnItextFont
// This is a generic class for dealing with iTextSharp Fonts
// Code moved and consolidated from Volian.Print.Library, Volian PDF.Library and VG
VlnItextFont.RegisterFont(fontName);
return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, fontSize, fontStyle, fontColor); return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, fontSize, fontStyle, fontColor);
} }
public static void RegisterFont(string fontName)
{
if (!iTextSharp.text.FontFactory.IsRegistered(fontName))
{
foreach (string name in FontKey.GetValueNames())
{
if (name.StartsWith(fontName))
{
string fontFile = (string)FontKey.GetValue(name);
iTextSharp.text.FontFactory.Register(fontFile.Contains("\\") ? fontFile : FontFolder + "\\" + fontFile);
}
}
}
}
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; } }
} }
} }

View File

@ -18,59 +18,6 @@ using Volian.Base.Library;
namespace Volian.Svg.Library 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;
Regex find = new Regex(fontName + @" ?\(.*\)", RegexOptions.IgnoreCase);
RegistryKey regKey = Registry.LocalMachine.OpenSubKey(@"Software\Microsoft\Windows NT\CurrentVersion\Fonts");
string[] values = regKey.GetValueNames();
baseFont = regKey.GetValue(values[0]).ToString();
foreach (string valueName in regKey.GetValueNames())
if (find.IsMatch(valueName))
return regKey.GetValue(valueName).ToString();
return baseFont;
}
public static string FullFileName(string fontName)
{
return FontDir + @"\" + FileName(fontName);
}
private static string _FontDir = null;
public static string FontDir
{
get
{
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");
// 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();
}
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;
}
}
}
public static class VolianPdf public static class VolianPdf
{ {
private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType); private static readonly log4net.ILog _MyLog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType);
@ -81,74 +28,6 @@ namespace Volian.Svg.Library
pdfr.Close(); pdfr.Close();
return retval; 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))
{
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))
{
//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);
ProfileTimer.Pop(profileDepth2);
if (!iTextSharp.text.FontFactory.IsRegistered(fontName))
{
_MyLog.WarnFormat("Problem with Font {0} in {1}", fontName, FontFind.FontDir);
int profileDepth3 = ProfileTimer.Push(">>>> RegisterDirectories");
iTextSharp.text.FontFactory.RegisterDirectories();
ProfileTimer.Pop(profileDepth3);
}
}
else
{
int profileDepth4 = ProfileTimer.Push(">>>> RegisterDirectories");
iTextSharp.text.FontFactory.RegisterDirectories();
ProfileTimer.Pop(profileDepth4);
}
}
if (!iTextSharp.text.FontFactory.IsRegistered(fontName))
{
_MyLog.WarnFormat("Font {0} could not be found!", fontName);
}
else
{
iTextSharp.text.Font fnt = iTextSharp.text.FontFactory.GetFont(fontName, 10);
if (fnt.BaseFont == null)
{
_MyLog.WarnFormat("Font {0} is not installed properly!", fontName);
}
}
}
ProfileTimer.Pop(profileDepth);
}
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; } }
// keep for debug // keep for debug
//private static List<string> _RegFonts = new List<string>(); //private static List<string> _RegFonts = new List<string>();
public static iTextSharp.text.Font GetFont(string fontName, int size, int style) public static iTextSharp.text.Font GetFont(string fontName, int size, int style)
@ -158,12 +37,18 @@ namespace Volian.Svg.Library
//_MyLog.WarnFormat("Registering Font {0}", fontName); //_MyLog.WarnFormat("Registering Font {0}", fontName);
//_RegFonts.Add(fontName); //_RegFonts.Add(fontName);
//} //}
RegisterFont(fontName); // B2019-116 Use Volian.Base.Library.VlnItextFont
// This is a generic class for dealing with iTextSharp Fonts
// Code moved and consolidated from Volian.Print.Library, Volian PDF.Library and VG
VlnItextFont.RegisterFont(fontName);
return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, size / 2, style); return iTextSharp.text.FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, size / 2, style);
} }
public static iTextSharp.text.Font GetFont(System.Drawing.Font font) // fontName, int size, int style) public static iTextSharp.text.Font GetFont(System.Drawing.Font font) // fontName, int size, int style)
{ {
RegisterFont(font.Name); // B2019-116 Use Volian.Base.Library.VlnItextFont
// This is a generic class for dealing with iTextSharp Fonts
// Code moved and consolidated from Volian.Print.Library, Volian PDF.Library and VG
VlnItextFont.RegisterFont(font.Name);
int style = (font.Bold ? iTextSharp.text.Font.BOLD : 0) + (font.Italic ? iTextSharp.text.Font.ITALIC : 0); int style = (font.Bold ? iTextSharp.text.Font.BOLD : 0) + (font.Italic ? iTextSharp.text.Font.ITALIC : 0);
return iTextSharp.text.FontFactory.GetFont(font.Name, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, font.Size, style); return iTextSharp.text.FontFactory.GetFont(font.Name, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, font.Size, style);
} }
@ -315,33 +200,20 @@ namespace Volian.Svg.Library
} }
public static iTextSharp.text.Font GetFont(string fontName) public static iTextSharp.text.Font GetFont(string fontName)
{ {
RegisterFont(fontName); // B2019-116 Use Volian.Base.Library.VlnItextFont
// This is a generic class for dealing with iTextSharp Fonts
// Code moved and consolidated from Volian.Print.Library, Volian PDF.Library and VG
VlnItextFont.RegisterFont(fontName);
return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10); return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, 10);
} }
public static iTextSharp.text.Font GetFont(string fontName, float size, int style, System.Drawing.Color color) public static iTextSharp.text.Font GetFont(string fontName, float size, int style, System.Drawing.Color color)
{ {
RegisterFont(fontName); // B2019-116 Use Volian.Base.Library.VlnItextFont
// This is a generic class for dealing with iTextSharp Fonts
// Code moved and consolidated from Volian.Print.Library, Volian PDF.Library and VG
VlnItextFont.RegisterFont(fontName);
return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, size, style, new iTextSharp.text.Color(color)); return FontFactory.GetFont(fontName, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, size, style, new iTextSharp.text.Color(color));
} }
public static void RegisterFont(string fontName)
{
if (!FontFactory.IsRegistered(fontName))
{
foreach (string name in FontKey.GetValueNames())
{
if (name.StartsWith(fontName))
{
string fontFile = (string) FontKey.GetValue(name);
//FontFactory.Register(fontFile.Contains("\\") ? fontFile : FontFolder + "\\" + fontFile);
//B2019-099 Consistent Font Retrieve
FontFactory.Register(fontFile.Contains("\\") ? fontFile : FontFind.FontDir + "\\" + fontFile);
}
}
}
}
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 = null; //private static string _FontFolder = null;
//public static string FontFolder //public static string FontFolder
//{ //{
@ -758,11 +630,10 @@ namespace Volian.Svg.Library
float fontSize = scale.M(new SvgMeasurement(Font.SizeInPoints, E_MeasurementUnits.PT)); float fontSize = scale.M(new SvgMeasurement(Font.SizeInPoints, E_MeasurementUnits.PT));
int fontStyle = (Font.Bold ? iTextSharp.text.Font.BOLD : 0) + (Font.Italic ? iTextSharp.text.Font.ITALIC : 0); int fontStyle = (Font.Bold ? iTextSharp.text.Font.BOLD : 0) + (Font.Italic ? iTextSharp.text.Font.ITALIC : 0);
VolianPdf.RegisterFont(Font.Name); // B2019-116 Use Volian.Base.Library.VlnItextFont
//if (iTextSharp.text.FontFactory.IsRegistered(Font.Name) == false) // This is a generic class for dealing with iTextSharp Fonts
//{ // Code moved and consolidated from Volian.Print.Library, Volian PDF.Library and VG
// //iTextSharp.text.FontFactory.RegisterDirectories(); VlnItextFont.RegisterFont(Font.Name);
//}
iTextSharp.text.Font font = FontFactory.GetFont(Font.Name, BaseFont.IDENTITY_H,BaseFont.EMBEDDED, fontSize, fontStyle, new Color(FillColor)); iTextSharp.text.Font font = FontFactory.GetFont(Font.Name, BaseFont.IDENTITY_H,BaseFont.EMBEDDED, fontSize, fontStyle, new Color(FillColor));
Phrase ph = BuildPhrase(text, fontSize, fontStyle, font); Phrase ph = BuildPhrase(text, fontSize, fontStyle, font);
@ -903,14 +774,20 @@ namespace Volian.Svg.Library
fs.AddFont(font); fs.AddFont(font);
if (font.BaseFont != null && font.BaseFont.GetWidthPoint("m", 12) == font.BaseFont.GetWidthPoint(".", 12)) if (font.BaseFont != null && font.BaseFont.GetWidthPoint("m", 12) == font.BaseFont.GetWidthPoint(".", 12))
{ {
VolianPdf.RegisterFont("VESymbFix"); // B2019-116 Use Volian.Base.Library.VlnItextFont
// This is a generic class for dealing with iTextSharp Fonts
// Code moved and consolidated from Volian.Print.Library, Volian PDF.Library and VG
VlnItextFont.RegisterFont("VESymbFix");
fs.AddFont(FontFactory.GetFont("VESymbFix", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, fontSize, fontStyle, new Color(FillColor))); fs.AddFont(FontFactory.GetFont("VESymbFix", BaseFont.IDENTITY_H, BaseFont.EMBEDDED, fontSize, fontStyle, new Color(FillColor)));
} }
else else
{ {
// C2017-036 get best available proportional font for symbols // C2017-036 get best available proportional font for symbols
VolianPdf.RegisterFont(Volian.Base.Library.vlnFont.ProportionalSymbolFont); // B2019-116 Use Volian.Base.Library.VlnItextFont
fs.AddFont(FontFactory.GetFont(Volian.Base.Library.vlnFont.ProportionalSymbolFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, fontSize, fontStyle, new Color(FillColor))); // This is a generic class for dealing with iTextSharp Fonts
// Code moved and consolidated from Volian.Print.Library, Volian PDF.Library and VG
VlnItextFont.RegisterFont(Volian.Base.Library.vlnFont.ProportionalSymbolFont);
fs.AddFont(FontFactory.GetFont(Volian.Base.Library.vlnFont.ProportionalSymbolFont, BaseFont.IDENTITY_H, BaseFont.EMBEDDED, fontSize, fontStyle, new Color(FillColor)));
} }
Phrase ph = fs.Process(text); Phrase ph = fs.Process(text);
return ph; return ph;
@ -953,7 +830,10 @@ namespace Volian.Svg.Library
BaseFont baseFont = font.BaseFont; BaseFont baseFont = font.BaseFont;
if (baseFont == null) if (baseFont == null)
{ {
Svg.RegisterFont(Font.Name); // B2019-116 Use Volian.Base.Library.VlnItextFont
// This is a generic class for dealing with iTextSharp Fonts
// Code moved and consolidated from Volian.Print.Library, Volian PDF.Library and VG
VlnItextFont.RegisterFont(Font.Name);
font = Svg.GetFont(Font.Name); font = Svg.GetFont(Font.Name);
baseFont = font.BaseFont; baseFont = font.BaseFont;
if(baseFont==null) if(baseFont==null)