using GraphicsManager.Enums; using SharpFont; namespace GraphicsManager.Objects.Core; public class FontInteraction { public Font[] CurrentFonts { get; private set; } private FontSize fs = FontSize.Thin; private uint ph = 12; private bool i; private uint epx = 0; internal List _Faces = new(); public event Func? ReloadUI; internal List Families_ { get; } = new(); public IReadOnlyList Families { get => Families_.AsReadOnly(); } public FontInteraction Clone() { var c = (FontInteraction)this.MemberwiseClone(); return c; } public static FontInteraction Load(FontFamily family) { FontInteraction c = new(family); c.FontSize = FontSize.Regular; return c; } private FontInteraction(FontFamily Family) { 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) { CurrentFonts = new Font[Families_.Count]; for (int j = 0; j < Families_.Count; j++) { if (value) { if (Families_[j].InternalFontsi.TryGetValue(FontSize, out Font? f)) { i = value; CurrentFonts[j] = (Families_[j].InternalFontsi[fs]); if (ReloadUI is not null) ReloadUI.Invoke(); } } else { if (Families_[j].InternalFonts.TryGetValue(FontSize, out Font? f)) { i = value; CurrentFonts[j] = Families_[j].InternalFonts[fs]; if (ReloadUI is not null) ReloadUI.Invoke(); } } } } } } private static Tuple gcl(Dictionary> thisList, FontSize thisValue, out FontSize fs) { Dictionary>.KeyCollection keys = thisList.Keys; FontSize nearest = keys.OrderBy(k => Math.Abs(k - thisValue)).First(); fs = nearest; return thisList[nearest]; } private static Tuple Sgcl(Dictionary> thisList, FontSize thisValue, out FontSize fs) { Dictionary>.KeyCollection keys = thisList.Keys; FontSize nearest = keys.OrderBy(k => Math.Abs(k - thisValue)).First(); fs = nearest; return thisList[nearest]; } private static Font Sgcl(Dictionary thisList, FontSize thisValue, out FontSize fs) { Dictionary.KeyCollection keys = thisList.Keys; FontSize nearest = keys.OrderBy(k => Math.Abs(k - thisValue)).First(); fs = nearest; return thisList[nearest]; } public FontSize FontSize { get => fs; set { CurrentFonts = new Font[Families_.Count]; for (int j = 0; j < Families_.Count; j++) { Font f = Italic switch { true => Sgcl(Families_[j].InternalFontsi, value, out fs), false => Sgcl(Families_[j].InternalFonts, value, out fs), }; CurrentFonts[j] = Italic switch { true => Families_[j].InternalFontsi[fs], false => Families_[j].InternalFonts[fs], }; } if (ReloadUI is not null) ReloadUI.Invoke(); } } }