mirror of https://github.com/ErsatzTV/ErsatzTV.git
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
2.0 KiB
57 lines
2.0 KiB
using System.Collections.Concurrent; |
|
using System.Globalization; |
|
using SixLabors.Fonts; |
|
|
|
namespace ErsatzTV.Infrastructure.Streaming; |
|
|
|
public static class GraphicsEngineFonts |
|
{ |
|
private static readonly FontCollection CustomFontCollection = new(); |
|
private static readonly ConcurrentDictionary<string, FontFamily> CustomFontFamilies |
|
= new(StringComparer.OrdinalIgnoreCase); |
|
private static readonly System.Collections.Generic.HashSet<string> LoadedFontFiles |
|
= new(StringComparer.OrdinalIgnoreCase); |
|
|
|
public static void LoadFonts(string fontsFolder) |
|
{ |
|
foreach (var file in Directory.EnumerateFiles(fontsFolder, "*.*", SearchOption.AllDirectories)) |
|
{ |
|
if (!file.EndsWith(".ttf", StringComparison.OrdinalIgnoreCase) && |
|
!file.EndsWith(".otf", StringComparison.OrdinalIgnoreCase)) |
|
{ |
|
continue; |
|
} |
|
|
|
if (!LoadedFontFiles.Add(file)) |
|
{ |
|
continue; |
|
} |
|
|
|
var fontFamily = CustomFontCollection.Add(file, CultureInfo.CurrentCulture); |
|
CustomFontFamilies.TryAdd(fontFamily.Name, fontFamily); |
|
} |
|
} |
|
|
|
public static Font GetFont(string fontFamilyName, float fontSize, FontStyle style) |
|
{ |
|
// try custom fonts |
|
if (CustomFontFamilies.TryGetValue(fontFamilyName, out var customFamily)) |
|
{ |
|
return customFamily.GetAvailableStyles().Contains(style) |
|
? customFamily.CreateFont(fontSize, style) |
|
: customFamily.CreateFont(fontSize); |
|
} |
|
|
|
// fallback to system fonts |
|
if (SystemFonts.TryGet(fontFamilyName, CultureInfo.CurrentCulture, out var systemFamily)) |
|
{ |
|
return systemFamily.GetAvailableStyles().Contains(style) |
|
? systemFamily.CreateFont(fontSize, style) |
|
: systemFamily.CreateFont(fontSize); |
|
} |
|
|
|
// fallback to default font |
|
var fallback = SystemFonts.Families.First(); |
|
return fallback.CreateFont(fontSize, style); |
|
} |
|
}
|
|
|