146 lines
4.2 KiB
C#
Raw Normal View History

using GraphicsManager.Enums;
2024-03-27 20:53:07 -04:00
using SharpFont;
namespace GraphicsManager.Objects.Core;
public class FontInteraction
{
2024-03-27 20:53:07 -04:00
public Font[] CurrentFonts { get; private set; }
private FontSize fs = FontSize.Thin;
private uint ph = 12;
private bool i;
private uint epx = 0;
2024-03-27 20:53:07 -04:00
internal List<Face> _Faces = new();
public event Func<Task>? ReloadUI;
2024-03-27 20:53:07 -04:00
internal List<FontFamily> Families_ { get; } = new();
public IReadOnlyList<FontFamily> Families
{
get => Families_.AsReadOnly();
}
public FontInteraction Clone()
{
2024-03-27 20:53:07 -04:00
var c = (FontInteraction)this.MemberwiseClone();
return c;
}
public static FontInteraction Load(FontFamily family)
{
FontInteraction c = new(family);
c.FontSize = FontSize.Regular;
return c;
}
2024-03-27 20:53:07 -04:00
private FontInteraction(FontFamily Family)
{
2024-03-27 20:53:07 -04:00
Families_.Add(Family);
}
public void AddFamily(FontFamily Family)
{
Families_.Add(Family);
FontSize = FontSize;
}
public uint PixelHeight
{
get => ph;
set
{
ph = value;
if (ReloadUI is not null) ReloadUI.Invoke();
}
}
public uint ExtraLinePixels
{
get => epx;
set
{
epx = value;
if (ReloadUI is not null) ReloadUI.Invoke();
}
}
public bool Italic
{
get => i;
set
{
if (i != value)
{
2024-03-27 20:53:07 -04:00
CurrentFonts = new Font[Families_.Count];
for (int j = 0; j < Families_.Count; j++)
{
2024-08-27 10:52:50 -04:00
if (value)
{
2024-08-27 10:52:50 -04:00
if (Families_[j].InternalFontsi.TryGetValue(FontSize, out Font? f))
{
2024-08-27 10:52:50 -04:00
i = value;
CurrentFonts[j] = (Families_[j].InternalFontsi[fs]);
if (ReloadUI is not null) ReloadUI.Invoke();
2024-03-27 20:53:07 -04:00
}
}
2024-03-27 20:53:07 -04:00
else
{
2024-08-27 10:52:50 -04:00
if (Families_[j].InternalFonts.TryGetValue(FontSize, out Font? f))
{
2024-08-27 10:52:50 -04:00
i = value;
CurrentFonts[j] = Families_[j].InternalFonts[fs];
if (ReloadUI is not null) ReloadUI.Invoke();
}
}
}
}
}
}
private static Tuple<string, Font?> gcl(Dictionary<FontSize, Tuple<string, Font?>> thisList, FontSize thisValue, out FontSize fs)
{
Dictionary<FontSize, Tuple<string, Font?>>.KeyCollection keys = thisList.Keys;
2024-08-27 10:52:50 -04:00
FontSize nearest = keys.OrderBy(k => Math.Abs(k - thisValue)).First();
fs = nearest;
return thisList[nearest];
}
2024-03-27 20:53:07 -04:00
private static Tuple<MemoryStream, Font?> Sgcl(Dictionary<FontSize, Tuple<MemoryStream, Font?>> thisList, FontSize thisValue, out FontSize fs)
{
Dictionary<FontSize, Tuple<MemoryStream, Font?>>.KeyCollection keys = thisList.Keys;
2024-08-27 10:52:50 -04:00
FontSize nearest = keys.OrderBy(k => Math.Abs(k - thisValue)).First();
fs = nearest;
return thisList[nearest];
}
private static Font Sgcl(Dictionary<FontSize, Font> thisList, FontSize thisValue, out FontSize fs)
{
Dictionary<FontSize, Font>.KeyCollection keys = thisList.Keys;
FontSize nearest = keys.OrderBy(k => Math.Abs(k - thisValue)).First();
2024-03-27 20:53:07 -04:00
fs = nearest;
return thisList[nearest];
}
public FontSize FontSize
{
get => fs;
set
{
2024-03-27 20:53:07 -04:00
CurrentFonts = new Font[Families_.Count];
for (int j = 0; j < Families_.Count; j++)
{
2024-08-27 10:52:50 -04:00
Font f = Italic switch
2024-03-27 20:53:07 -04:00
{
2024-08-27 10:52:50 -04:00
true => Sgcl(Families_[j].InternalFontsi, value, out fs),
false => Sgcl(Families_[j].InternalFonts, value, out fs),
};
2024-03-27 20:53:07 -04:00
2024-08-27 10:52:50 -04:00
CurrentFonts[j] = Italic switch
{
2024-08-27 10:52:50 -04:00
true => Families_[j].InternalFontsi[fs],
false => Families_[j].InternalFonts[fs],
};
}
if (ReloadUI is not null) ReloadUI.Invoke();
}
}
}