using System; using System.Runtime.InteropServices; using System.Security; using System.IO; namespace SFML { namespace Graphics { //////////////////////////////////////////////////////////// /// /// Font is the low-level class for loading and /// manipulating character fonts. This class is meant to /// be used by String2D /// //////////////////////////////////////////////////////////// public class Font : ObjectBase { //////////////////////////////////////////////////////////// /// /// Construct the font from a file /// /// Font file to load /// //////////////////////////////////////////////////////////// public Font(string filename) : this(filename, 30) { } //////////////////////////////////////////////////////////// /// /// Construct the font from a file, using custom size /// /// Font file to load /// Character size /// //////////////////////////////////////////////////////////// public Font(string filename, uint charSize) : this(filename, charSize, "") { } //////////////////////////////////////////////////////////// /// /// Construct the font from a file, using custom size and characters set /// /// Font file to load /// Character size /// Set of characters to generate /// //////////////////////////////////////////////////////////// public Font(string filename, uint charSize, string charset) : base(IntPtr.Zero) { unsafe { IntPtr ptr; int size; if (Int32.TryParse(charset, out size)) ptr = new IntPtr(&size); else ptr = IntPtr.Zero; SetThis(sfFont_CreateFromFile(filename, charSize, ptr)); } if (This == IntPtr.Zero) throw new LoadingFailedException("font", filename); } //////////////////////////////////////////////////////////// /// /// Construct the font from a file in a stream /// /// Stream containing the file contents /// //////////////////////////////////////////////////////////// public Font(Stream stream) : this(stream, 30) { } //////////////////////////////////////////////////////////// /// /// Construct the font from a file in a stream, using custom size /// /// Stream containing the file contents /// Character size /// //////////////////////////////////////////////////////////// public Font(Stream stream, uint charSize) : this(stream, charSize, "") { } //////////////////////////////////////////////////////////// /// /// Construct the font from a file in a stream /// /// Stream containing the file contents /// Character size /// Set of characters to generate /// //////////////////////////////////////////////////////////// public Font(Stream stream, uint charSize, string charset) : base(IntPtr.Zero) { unsafe { IntPtr ptr; int size; if (Int32.TryParse(charset, out size)) ptr = new IntPtr(&size); else ptr = IntPtr.Zero; stream.Position = 0; byte[] StreamData = new byte[stream.Length]; uint Read = (uint)stream.Read(StreamData, 0, StreamData.Length); fixed (byte* dataPtr = StreamData) { SetThis(sfFont_CreateFromMemory((char*)dataPtr, Read, charSize, ptr)); } } if (This == IntPtr.Zero) throw new LoadingFailedException("font"); } //////////////////////////////////////////////////////////// /// /// Base character size /// //////////////////////////////////////////////////////////// public uint CharacterSize { get { return sfFont_GetCharacterSize(This); } } //////////////////////////////////////////////////////////// /// /// Default built-in font /// //////////////////////////////////////////////////////////// public static Font DefaultFont { get { if (ourDefaultFont == null) ourDefaultFont = new Font(sfFont_GetDefaultFont()); return ourDefaultFont; } } //////////////////////////////////////////////////////////// /// /// Handle the destruction of the object /// /// Is the GC disposing the object, or is it an explicit call ? //////////////////////////////////////////////////////////// protected override void Destroy(bool disposing) { if (this != ourDefaultFont) { if (!disposing) Context.Global.SetActive(true); sfFont_Destroy(This); if (!disposing) Context.Global.SetActive(false); } } //////////////////////////////////////////////////////////// /// /// Internal constructor /// /// Pointer to the object in C library //////////////////////////////////////////////////////////// private Font(IntPtr thisPtr) : base(thisPtr) { } private static Font ourDefaultFont = null; #region Imports [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfFont_CreateFromFile(string Filename, uint CharSize, IntPtr Charset); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] unsafe static extern IntPtr sfFont_CreateFromMemory(char* Data, uint SizeInBytes, uint CharSize, IntPtr Charset); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern void sfFont_Destroy(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern uint sfFont_GetCharacterSize(IntPtr This); [DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity] static extern IntPtr sfFont_GetDefaultFont(); #endregion } } }