using System; using System.Diagnostics.CodeAnalysis; using System.Drawing; using System.IO; using System.Reflection.PortableExecutable; namespace Dashboard.Drawing { /// /// Interface for registered typesetters. /// public interface ITypeSetter { /// /// Name of the typesetter. /// string Name { get; } SizeF MeasureString(IFont font, string value); IFont LoadFont(Stream stream); IFont LoadFont(string path); IFont LoadFont(NamedFont font); } /// /// Class for typesetting related functions. /// public static class Typesetter { /// /// The typesetting backend for this instance. /// public static ITypeSetter Backend { get; set; } = new UndefinedTypeSetter(); public static string Name => Backend.Name; public static SizeF MeasureString(IFont font, string value) { return Backend.MeasureString(font, value); } public static IFont LoadFont(Stream stream) { return Backend.LoadFont(stream); } public static IFont LoadFont(string path) { return Backend.LoadFont(path); } public static IFont LoadFont(FileInfo file) { return Backend.LoadFont(file.FullName); } public static IFont LoadFont(NamedFont font) { return Backend.LoadFont(font); } public static IFont LoadFont(string family, float size, FontWeight weight = FontWeight.Normal, FontSlant slant = FontSlant.Normal, FontStretch stretch = FontStretch.Normal) { return LoadFont(new NamedFont(family, size, weight, slant, stretch)); } private class UndefinedTypeSetter : ITypeSetter { public string Name { get; } = "Undefined"; [DoesNotReturn] private void Except() { throw new InvalidOperationException("No typesetting backend is loaded."); } public SizeF MeasureString(IFont font, string value) { Except(); return default; } public IFont LoadFont(Stream stream) { Except(); return default; } public IFont LoadFont(string path) { Except(); return default; } public IFont LoadFont(NamedFont font) { Except(); return default; } } } }