105 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
			
		
		
	
	
			105 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C#
		
	
	
	
	
	
| using System;
 | |
| using System.Diagnostics.CodeAnalysis;
 | |
| using System.Drawing;
 | |
| using System.IO;
 | |
| using System.Reflection.PortableExecutable;
 | |
| 
 | |
| namespace Dashboard.Drawing
 | |
| {
 | |
|     /// <summary>
 | |
|     /// Interface for registered typesetters.
 | |
|     /// </summary>
 | |
|     public interface ITypeSetter
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// Name of the typesetter.
 | |
|         /// </summary>
 | |
|         string Name { get; }
 | |
| 
 | |
|         SizeF MeasureString(IFont font, string value);
 | |
| 
 | |
|         IFont LoadFont(Stream stream);
 | |
|         IFont LoadFont(string path);
 | |
|         IFont LoadFont(NamedFont font);
 | |
|     }
 | |
| 
 | |
|     /// <summary>
 | |
|     /// Class for typesetting related functions.
 | |
|     /// </summary>
 | |
|     public static class Typesetter
 | |
|     {
 | |
|         /// <summary>
 | |
|         /// The typesetting backend for this instance.
 | |
|         /// </summary>
 | |
|         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;
 | |
|             }
 | |
|         }
 | |
|     }
 | |
| }
 |