Compare commits

..

3 Commits

Author SHA1 Message Date
Elias Fierke
013bd4a070 [file:] added font-asset with example font 2026-01-18 11:36:41 +01:00
Elias Fierke
2c4eb1fcef [proj:] including assets/fonts to build directory 2026-01-18 11:35:15 +01:00
Elias Fierke
0395537a55 [init:] added StableFontResolver.cs 2026-01-18 09:20:13 +01:00
6 changed files with 158 additions and 14 deletions

View File

@@ -9,25 +9,25 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Avalonia" Version="11.3.2" />
<PackageReference Include="Avalonia.Controls.DataGrid" Version="11.3.2" />
<PackageReference Include="Avalonia.Desktop" Version="11.3.2" />
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.2" />
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.2" />
<PackageReference Include="Avalonia" Version="11.3.2"/>
<PackageReference Include="Avalonia.Controls.DataGrid" Version="11.3.2"/>
<PackageReference Include="Avalonia.Desktop" Version="11.3.2"/>
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.3.2"/>
<PackageReference Include="Avalonia.Fonts.Inter" Version="11.3.2"/>
<!--Condition below is needed to remove Avalonia.Diagnostics package from build output in Release configuration.-->
<PackageReference Include="Avalonia.Diagnostics" Version="11.3.2">
<IncludeAssets Condition="'$(Configuration)' != 'Debug'">None</IncludeAssets>
<PrivateAssets Condition="'$(Configuration)' != 'Debug'">All</PrivateAssets>
</PackageReference>
<PackageReference Include="ISO3166" Version="1.0.4" />
<PackageReference Include="Lucide.Avalonia" Version="0.1.35" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.4" />
<PackageReference Include="PdfSharp" Version="6.1.1" />
<PackageReference Include="ISO3166" Version="1.0.4"/>
<PackageReference Include="Lucide.Avalonia" Version="0.1.35"/>
<PackageReference Include="Newtonsoft.Json" Version="13.0.4"/>
<PackageReference Include="PdfSharp" Version="6.1.1"/>
</ItemGroup>
<ItemGroup>
<PackageReference Include="Markdig" Version="0.30.3" />
<PackageReference Include="AvaloniaEdit" Version="0.10.12" />
<PackageReference Include="Markdig" Version="0.30.3"/>
<PackageReference Include="AvaloniaEdit" Version="0.10.12"/>
</ItemGroup>
<ItemGroup>
@@ -37,17 +37,25 @@
</ItemGroup>
<ItemGroup>
<None Remove="assets\icon.ico" />
<None Remove="assets\icon.ico"/>
<AvaloniaResource Include="assets\icon.ico">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AvaloniaResource>
<None Remove="assets\calc_man.png" />
<None Remove="assets\calc_man.png"/>
<AvaloniaResource Include="assets\calc_man.png">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AvaloniaResource>
<None Remove="assets\loading.mp4" />
<None Remove="assets\loading.mp4"/>
<AvaloniaResource Include="assets\loading.mp4">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</AvaloniaResource>
</ItemGroup>
<ItemGroup>
<Content Include="assets\fonts\**\*.*">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Folder Include="assets\fonts\"/>
</ItemGroup>
</Project>

136
Tasks/StableFontResolver.cs Normal file
View File

@@ -0,0 +1,136 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using PdfSharp.Fonts;
using System.Globalization;
namespace Logof_Client;
public class StableFontResolver : IFontResolver
{
// family -> style -> bytes
private readonly Dictionary<string, Dictionary<string, byte[]>> _families = new(StringComparer.OrdinalIgnoreCase);
private readonly List<string> _orderedFamilies = new();
public StableFontResolver(string fontsDirectory)
{
try
{
if (string.IsNullOrEmpty(fontsDirectory)) return;
if (!Directory.Exists(fontsDirectory)) return;
var files = Directory.EnumerateFiles(fontsDirectory, "*.ttf");
foreach (var f in files)
{
try
{
var baseName = Path.GetFileNameWithoutExtension(f);
if (string.IsNullOrEmpty(baseName)) continue;
// Try to split family and style by hyphen or underscore
var family = baseName;
var style = "Regular";
var idx = baseName.IndexOf('-');
if (idx < 0) idx = baseName.IndexOf('_');
if (idx > 0)
{
family = baseName.Substring(0, idx);
style = baseName.Substring(idx + 1);
}
// Normalize common style names
style = NormalizeStyle(style);
var data = File.ReadAllBytes(f);
if (data == null || data.Length == 0) continue;
if (!_families.TryGetValue(family, out var dict))
{
dict = new Dictionary<string, byte[]>(StringComparer.OrdinalIgnoreCase);
_families[family] = dict;
_orderedFamilies.Add(family);
}
if (!dict.ContainsKey(style))
dict[style] = data;
}
catch
{
// ignore individual font load errors
}
}
}
catch
{
// ignore resolver init errors
}
}
private static string NormalizeStyle(string s)
{
if (string.IsNullOrEmpty(s)) return "Regular";
s = s.ToLowerInvariant();
if (s.Contains("bold") && s.Contains("italic")) return "BoldItalic";
if (s.Contains("bold")) return "Bold";
if (s.Contains("italic") || s.Contains("oblique")) return "Italic";
if (s.Contains("regular") || s == "r") return "Regular";
return CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s);
}
public FontResolverInfo ResolveTypeface(string familyName, bool isBold, bool isItalic)
{
// If requested family exists, pick corresponding style if available
string familyToUse = null;
if (!string.IsNullOrEmpty(familyName) && _families.ContainsKey(familyName))
familyToUse = familyName;
if (familyToUse == null && _orderedFamilies.Count > 0)
familyToUse = _orderedFamilies[0];
if (familyToUse == null)
return new FontResolverInfo("Arial");
var style = "Regular";
if (isBold && isItalic) style = "BoldItalic";
else if (isBold) style = "Bold";
else if (isItalic) style = "Italic";
// Face name format: Family#Style
return new FontResolverInfo($"{familyToUse}#{style}");
}
public byte[] GetFont(string faceName)
{
if (string.IsNullOrEmpty(faceName)) return null;
// faceName expected as "Family#Style" or just "Family"
var family = faceName;
var style = "Regular";
var idx = faceName.IndexOf('#');
if (idx >= 0)
{
family = faceName.Substring(0, idx);
style = faceName.Substring(idx + 1);
}
style = NormalizeStyle(style);
if (_families.TryGetValue(family, out var dict))
{
// Try exact style
if (dict.TryGetValue(style, out var data)) return data;
// Fallback order
if (style != "Regular" && dict.TryGetValue("Regular", out data)) return data;
if (dict.TryGetValue("Bold", out data)) return data;
if (dict.TryGetValue("Italic", out data)) return data;
// Any available
foreach (var kv in dict) return kv.Value;
}
return null;
}
}

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.