Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Actually use custom fonts #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 8 additions & 9 deletions PdfSharpCore/Utils/FontResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class FontResolver : IFontResolver

private static readonly Dictionary<string, FontFamilyModel> InstalledFonts = new Dictionary<string, FontFamilyModel>();

private static readonly string[] SSupportedFonts;
private static readonly List<string> SSupportedFonts;

public FontResolver()
{
Expand All @@ -28,30 +28,27 @@ public FontResolver()
static FontResolver()
{
string fontDir;

SSupportedFonts = new List<string>();
bool isOSX = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.OSX);
if (isOSX)
{
fontDir = "/Library/Fonts/";
SSupportedFonts = Directory.GetFiles(fontDir, "*.ttf", SearchOption.AllDirectories);
SetupFontsFiles(SSupportedFonts);
SetupFontsFiles(Directory.GetFiles(fontDir, "*.ttf", SearchOption.AllDirectories));
return;
}

bool isLinux = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Linux);
if (isLinux)
{
SSupportedFonts = resolveLinuxFontFiles();
SetupFontsFiles(SSupportedFonts);
SetupFontsFiles(resolveLinuxFontFiles());
return;
}

bool isWindows = System.Runtime.InteropServices.RuntimeInformation.IsOSPlatform(System.Runtime.InteropServices.OSPlatform.Windows);
if (isWindows)
{
fontDir = Environment.ExpandEnvironmentVariables(@"%SystemRoot%\Fonts");
SSupportedFonts = Directory.GetFiles(fontDir, "*.ttf", SearchOption.AllDirectories);
SetupFontsFiles(SSupportedFonts);
SetupFontsFiles(Directory.GetFiles(fontDir, "*.ttf", SearchOption.AllDirectories));
return;
}

Expand Down Expand Up @@ -89,6 +86,8 @@ public static void SetupFontsFiles(string[] sSupportedFonts)
string fontFamilyName = fontDescription.FontFamily(CultureInfo.InvariantCulture);
Debug.WriteLine(fontPathFile);

SSupportedFonts.Add(fontPathFile);

if (tmpFontFamiliesTtfFilesDict.TryGetValue(fontFamilyName, out List<string> familyTtfFiles))
familyTtfFiles.Add(fontPathFile);
else
Expand Down Expand Up @@ -188,7 +187,7 @@ public byte[] GetFont(string faceFileName)
string ttfPathFile = "";
try
{
ttfPathFile = SSupportedFonts.ToList().First(x => x.ToLower().Contains(Path.GetFileName(faceFileName).ToLower()));
ttfPathFile = SSupportedFonts.First(x => x.ToLower().Contains(Path.GetFileName(faceFileName).ToLower()));
using (var ttf = File.OpenRead(ttfPathFile))
{
ttf.CopyTo(ms);
Expand Down