[chore:] font resolver included + various changes

This commit is contained in:
Elias Fierke
2026-01-18 11:38:16 +01:00
parent 013bd4a070
commit 10b0eb5bcd
4 changed files with 120 additions and 22 deletions

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using PdfSharp;
using PdfSharp.Drawing;
using PdfSharp.Fonts;
using PdfSharp.Pdf;
namespace Logof_Client;
@@ -12,7 +14,7 @@ public class PdfBuilder
// Table layout
private const int CellsPerRow = 3;
private const int CellsPerPage = 21; // 3 columns × 7 rows
private readonly XFont _boldFont = new("Arial", 9, XFontStyleEx.Bold);
private readonly XFont _boldFont;
private readonly double _cellHeight = 42.43; // mm
private readonly double _cellPaddingBottom = 5; // mm
@@ -32,8 +34,50 @@ public class PdfBuilder
private readonly double _marginLeft = 0; // mm
private readonly double _marginRight = 0; // mm
private readonly double _marginTop = 0; // mm
private readonly XFont _regularFont = new("Arial", 9, XFontStyleEx.Regular);
private readonly XFont _smallFont = new("Arial", 6, XFontStyleEx.Regular);
private readonly XFont _regularFont;
private readonly XFont _smallFont;
public PdfBuilder()
{
EnsureFontResolverRegistered();
// Select first font from build output fonts folder (AppContext.BaseDirectory/fonts)
var chosenFamily = "Arial";
try
{
if (Directory.Exists(Global._instance.font_path))
{
var first = Directory.EnumerateFiles(Global._instance.font_path, "*.ttf").FirstOrDefault();
if (!string.IsNullOrEmpty(first))
chosenFamily = StripStyleSuffix(Path.GetFileNameWithoutExtension(first)) ?? chosenFamily;
}
}
catch
{
chosenFamily = "Arial";
}
_boldFont = new XFont(chosenFamily, 9, XFontStyleEx.Bold);
_regularFont = new XFont(chosenFamily, 9, XFontStyleEx.Regular);
_smallFont = new XFont(chosenFamily, 6, XFontStyleEx.Regular);
}
private static void EnsureFontResolverRegistered()
{
if (GlobalFontSettings.FontResolver != null) return;
//var fontsDir = Path.Combine(AppContext.BaseDirectory, "fonts");
GlobalFontSettings.FontResolver = new StableFontResolver(Global._instance.font_path);
}
private static string StripStyleSuffix(string name)
{
if (string.IsNullOrEmpty(name)) return name;
var idx = name.IndexOf('-');
if (idx < 0) idx = name.IndexOf('_');
if (idx > 0)
return name.Substring(0, idx);
return name;
}
/// <summary>