FontResolver.SetupFontsFiles won't work
I'm trying out PdfSharpCore in a Linux based Docker container with no system fonts. I added a custom font (tahoma.ttf copied from my Windows system) in a folder named fonts in my solution.
I called the resolver to add my custom font:
var customFonts = Directory.GetFiles("fonts/", "*.ttf", SearchOption.AllDirectories); FontResolver.SetupFontsFiles(customFonts);
So far it works. The font is added to the InstalledFonts Dictionary in the FontResolver. When the ResolveTypeface method is called the custom font is found.
However, the GetFont method fails. This method tries to find the file in the SSupportedFonts array. But this array is static read-only and already set when SetupFontsFiles are called. Perhaps SetupFontsFiles is not intended to be public.
Removing read-only from SSupportedFonts and adding to it in SetupFontFiles resolves this for me.
Hi,
I don't know if it is exactly what you are looking for - but at way to do it would be with a custom font resolver.
GlobalFontSettings.FontResolver = new CustomFontResolver();
This is just an example that loads calibri, but you could extend the default resolver and just add the ttf files and then fallback to the default implementation ` public class CustomFontResolver : IFontResolver { public string DefaultFontName => throw new NotImplementedException();
public byte[] GetFont(string faceName)
{
return LoadFontData("nameoffont.ttf");
}
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
if (isBold)
{
if (isItalic)
return new FontResolverInfo("Calibri#bi");
return new FontResolverInfo("Calibri#b");
}
if (isItalic)
return new FontResolverInfo("Calibri#i");
return new FontResolverInfo("Calibri#");
}
private byte[] LoadFontData(string name)
{
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(name))
{
if (stream == null)
throw new ArgumentException("No resource with name " + name);
int count = (int)stream.Length;
byte[] data = new byte[count];
stream.Read(data, 0, count);
return data;
}
}`
Thanks. I did something similar but thought it should be addressed in future versions.
Den 7 aug. 2022 19:32, kI 19:32, ChristianS-j @.***> skrev:
Hi, I don't know if it is exactly what you are looking for - but at way to do it would be with a custom font resolver.
GlobalFontSettings.FontResolver = new CustomFontResolver();This is just an example that loads calibri, but you could extend the default resolver and just add the ttf files and then fallback to the default implementation `public class CustomFontResolver : IFontResolver { public string DefaultFontName => throw new NotImplementedException();
public byte[] GetFont(string faceName) { return LoadFontData("nameoffont.ttf"); }public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic) {
if (isBold) { if (isItalic) return new FontResolverInfo("Calibri#bi"); return new FontResolverInfo("Calibri#b"); } if (isItalic) return new FontResolverInfo("Calibri#i"); return new FontResolverInfo("Calibri#"); } private byte[] LoadFontData(string name) { var assembly = Assembly.GetExecutingAssembly(); using (Stream stream = assembly.GetManifestResourceStream(name)) { if (stream == null) throw new ArgumentException("No resource with name " + name); int count = (int)stream.Length; byte[] data = new byte[count]; stream.Read(data, 0, count); return data; } }`-- Reply to this email directly or view it on GitHub: https://github.com/ststeiger/PdfSharpCore/issues/281#issuecomment-1207451915 You are receiving this because you authored the thread.
Message ID: @.***>
Actually I i noticed, that on linux container, while using HtmlToPdf the ResolveTypeface is always called with familyName "Segoe UI", no matter what font is defined in html. Is it possible to resolve different html fonts to different real ttf files while using HtmlToPdf ? So far i only managed to force one font for whole document by ignoring 'familyName' parameter.