using System.Collections.Generic; using System.Drawing; using System.Drawing.Text; using System.Security.Policy; using OpenTK; using OpenTK.Graphics.OpenGL4; using OpenTK.Input; using SM.Base.Textures; using SM.Data.Fonts; using SM.OGL.Texture; namespace SM.Base.Text { /// /// Represents a font. /// public class Font : Texture { /// public override TextureWrapMode WrapMode { get; set; } = TextureWrapMode.ClampToEdge; /// /// The font family, that is used to find the right font. /// public FontFamily FontFamily; /// /// The font style. /// Default: /// public FontStyle FontStyle = FontStyle.Regular; /// /// The font size. /// Default: 12 /// public float FontSize = 12; /// /// The char set for the font. /// Default: /// public ICollection CharSet = FontCharStorage.SimpleUTF8; /// /// This contains all information for the different font character. /// public Dictionary Positions = new Dictionary(); /// /// Generates a font from a font family from the specified path. /// /// The specified path public Font(string path) { PrivateFontCollection pfc = new PrivateFontCollection(); pfc.AddFontFile(path); FontFamily = pfc.Families[0]; } /// /// Generates a font from a specified font family. /// /// Font-Family public Font(FontFamily font) { FontFamily = font; } /// /// Regenerates the texture. /// public void RegenerateTexture() { Width = 0; Height = 0; Positions = new Dictionary(); Bitmap map = new Bitmap(1000, 20); Dictionary charParams = new Dictionary(); using (System.Drawing.Font f = new System.Drawing.Font(FontFamily, FontSize, FontStyle)) { using (Graphics g = Graphics.FromImage(map)) { g.Clear(Color.Transparent); foreach (char c in CharSet) { string s = c.ToString(); SizeF size = g.MeasureString(s, f); try { charParams.Add(c, new[] {size.Width, Width }); } catch { // ignored } if (Height < size.Height) Height = (int)size.Height; Width += (int)size.Width; } } map = new Bitmap(Width, Height); using (Graphics g = Graphics.FromImage(map)) { foreach (KeyValuePair keyValuePair in charParams) { float normalizedX = (keyValuePair.Value[1] + 0.00001f) / Width; float normalizedWidth = (keyValuePair.Value[0]) / Width; CharParameter parameter; Positions.Add(keyValuePair.Key, parameter = new CharParameter() { NormalizedWidth = normalizedWidth, NormalizedX = normalizedX, Width = keyValuePair.Value[0], X = (int)keyValuePair.Value[1] }); g.DrawString(keyValuePair.Key.ToString(), f, Brushes.White, parameter.X, 0); } } } Map = map; Recompile(); } /// protected override void Compile() { RegenerateTexture(); base.Compile(); } } }