102 lines
4.0 KiB
C#
102 lines
4.0 KiB
C#
using System.Diagnostics;
|
|
using System.IO.Compression;
|
|
using GraphicsManager.Enums;
|
|
using SixLabors.Fonts;
|
|
|
|
namespace GraphicsManager.Objects.Core;
|
|
|
|
public class FontFamily
|
|
{
|
|
private static uint index = 0;
|
|
public event Func<Task>? ReloadUI;
|
|
public required string Family { get; init; }
|
|
private FontSize fs = FontSize.Thin;
|
|
internal Dictionary<FontSize, Tuple<string, Font?>> InternalFonts = new();
|
|
internal Dictionary<FontSize, Tuple<string, Font?>> InternalFontsi = new();
|
|
internal bool IsZip = false;
|
|
internal Dictionary<FontSize, Tuple<MemoryStream, Font?>> SInternalFonts = new();
|
|
internal Dictionary<FontSize, Tuple<MemoryStream, Font?>> SInternalFontsi = new();
|
|
internal static List<FontFamily> AllFams = new();
|
|
|
|
public static Task<FontFamily> LoadFontFamilyTask(Stream FamilyZip, string Family, string extra = "-")
|
|
{
|
|
return Task.FromResult(LoadFontFamily(FamilyZip, Family, extra));
|
|
}
|
|
|
|
public static FontFamily LoadFontFamily(Stream FamilyZip, string Family, string extra = "-")
|
|
{
|
|
extra = extra.ToLower();
|
|
FontFamily f = new()
|
|
{
|
|
Family = Family
|
|
};
|
|
AllFams.Add(f);
|
|
using ZipArchive archive = new ZipArchive(FamilyZip, ZipArchiveMode.Read, false);
|
|
f.IsZip = true;
|
|
|
|
foreach (ZipArchiveEntry font in archive.Entries)
|
|
{
|
|
string fd = font.Name.ToLower();
|
|
string[] fda = fd.Split('.');
|
|
fd = fda[fda.Length - 2];
|
|
|
|
void tryadd(FontSize s, bool i = false)
|
|
{
|
|
var memoryStream = new MemoryStream();
|
|
font.Open().CopyTo(memoryStream);
|
|
if (i)
|
|
{
|
|
if (!f.SInternalFontsi.ContainsKey(s))
|
|
f.SInternalFontsi.Add(s, new(memoryStream, null));
|
|
}
|
|
else
|
|
{
|
|
if (!f.SInternalFonts.ContainsKey(s))
|
|
f.SInternalFonts.Add(s, new(memoryStream, null));
|
|
}
|
|
}
|
|
|
|
if (fd.EndsWith("italic"))
|
|
{
|
|
if (fd.EndsWith(extra + "thinitalic")) tryadd(FontSize.Thin, true);
|
|
if (fd.EndsWith(extra + "extralightitalic")) tryadd(FontSize.ExtraLight, true);
|
|
if (fd.EndsWith(extra + "lightitalic")) tryadd(FontSize.Light, true);
|
|
if (fd.EndsWith(extra + "italic")) tryadd(FontSize.Regular, true);
|
|
if (fd.EndsWith(extra + "mediumitalic")) tryadd(FontSize.Medium, true);
|
|
if (fd.EndsWith(extra + "semibolditalic")) tryadd(FontSize.SemiBold, true);
|
|
if (fd.EndsWith(extra + "bolditalic")) tryadd(FontSize.Bold, true);
|
|
if (fd.EndsWith(extra + "extrabolditalic")) tryadd(FontSize.ExtraBold, true);
|
|
if (fd.EndsWith(extra + "blackitalic")) tryadd(FontSize.Black, true);
|
|
}
|
|
else
|
|
{
|
|
if (fd.EndsWith(extra + "thin")) tryadd(FontSize.Thin);
|
|
if (fd.EndsWith(extra + "extralight")) tryadd(FontSize.ExtraLight);
|
|
if (fd.EndsWith(extra + "light")) tryadd(FontSize.Light);
|
|
if (fd.EndsWith(extra + "regular")) tryadd(FontSize.Regular);
|
|
if (fd.EndsWith(extra + "medium")) tryadd(FontSize.Medium);
|
|
if (fd.EndsWith(extra + "semibold")) tryadd(FontSize.SemiBold);
|
|
if (fd.EndsWith(extra + "bold")) tryadd(FontSize.Bold);
|
|
if (fd.EndsWith(extra + "extrabold")) tryadd(FontSize.ExtraBold);
|
|
if (fd.EndsWith(extra + "black")) tryadd(FontSize.Black);
|
|
}
|
|
|
|
}
|
|
|
|
return f;
|
|
}
|
|
|
|
public static Task<FontFamily> LoadFontFamily(string Dir, string Family, string extra = "-")
|
|
{
|
|
FontFamily f = new()
|
|
{
|
|
Family = Family
|
|
};
|
|
AllFams.Add(f);
|
|
return Task.FromResult(f);
|
|
}
|
|
|
|
private FontFamily()
|
|
{
|
|
}
|
|
} |