Files
ersatztv/ErsatzTV.Infrastructure/Streaming/Graphics/Fonts/CustomFontMapper.cs
T
Jason DoveandGitHub 5d081ceeff fix editorconfig and run code cleanup (#2324)
* fix formatting rules

* reformat ersatztv

* reformat ersatztv.application

* reformat ersatztv.core

* refactor ersatztv.core.tests

* reformat ersatztv.ffmpeg

* reformat ersatztv.ffmpeg.tests

* reformat ersatztv.infrastructure

* cleanup infra mysql

* cleanup infra sqlite

* cleanup infra tests

* cleanup ersatztv.scanner

* cleanup ersatztv.scanner.tests

* sln cleanup

* update dependencies
2025-08-16 14:44:48 +00:00

49 lines
1.5 KiB
C#

using Microsoft.Extensions.Logging;
using SkiaSharp;
using Topten.RichTextKit;
namespace ErsatzTV.Infrastructure.Streaming.Graphics;
public sealed class CustomFontMapper(ILogger<CustomFontMapper> logger) : FontMapper
{
private readonly Dictionary<string, List<SKTypeface>> _customFonts = new();
public void LoadPrivateFont(Stream stream, string familyName)
{
SKTypeface typeface = SKTypeface.FromStream(stream) ??
throw new ArgumentException("Cannot load font from stream", nameof(stream));
string qualifiedName = familyName ?? typeface.FamilyName;
if (typeface.FontSlant != SKFontStyleSlant.Upright)
{
qualifiedName += "-Italic";
}
if (!_customFonts.TryGetValue(qualifiedName, out List<SKTypeface> listFonts))
{
listFonts = [];
_customFonts[qualifiedName] = listFonts;
}
listFonts.Add(typeface);
}
public override SKTypeface TypefaceFromStyle(IStyle style, bool ignoreFontVariants)
{
string qualifiedName = style.FontFamily;
if (style.FontItalic)
{
qualifiedName += "-Italic";
}
if (_customFonts.TryGetValue(qualifiedName, out List<SKTypeface> listFonts) && listFonts.Count != 0)
{
return listFonts.MinBy(font => Math.Abs(font.FontWeight - style.FontWeight))!;
}
logger.LogWarning("Could not find font {Name}; using default", qualifiedName);
return base.TypefaceFromStyle(style, ignoreFontVariants);
}
}