commits
tags
using PdfSharp.Fonts;
namespace Remiss.Core.Pdf;
/// <summary>
/// Minimal font-resolver för Windows. PdfSharp 6.1 resolvar inte namngivna
/// typsnitt automatiskt – vi pekar ut TTF-filerna i C:\Windows\Fonts själva.
/// Appen är Windows-only, så det räcker gott.
///
/// Endast de typsnitt försättsbladet använder stöds; allt annat faller tillbaka
/// på Arial. (Remissen sida 2 renderas aldrig av oss – den importeras bara.)
/// </summary>
internal sealed class WindowsFontResolver : IFontResolver
{
private static readonly string FontDir = Environment.GetFolderPath(Environment.SpecialFolder.Fonts);
private readonly Dictionary<string, byte[]> _cache = new(StringComparer.OrdinalIgnoreCase);
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
var isConsolas = familyName.Trim().Equals("Consolas", StringComparison.OrdinalIgnoreCase);
var face = isConsolas
? "consola" + (isBold, isItalic) switch { (true, true) => "z", (true, false) => "b", (false, true) => "i", _ => "" }
: "arial" + (isBold, isItalic) switch { (true, true) => "bi", (true, false) => "bd", (false, true) => "i", _ => "" };
return new FontResolverInfo(face);
}
public byte[] GetFont(string faceName)
{
if (_cache.TryGetValue(faceName, out var cached))
return cached;
var bytes = File.ReadAllBytes(Path.Combine(FontDir, faceName + ".ttf"));
_cache[faceName] = bytes;
return bytes;
}
}