2009-01-28 16:18:34 +00:00
|
|
|
using System;
|
2009-12-13 15:49:30 +00:00
|
|
|
using System.Collections.Generic;
|
2009-01-28 16:18:34 +00:00
|
|
|
using System.Runtime.InteropServices;
|
|
|
|
using System.Security;
|
|
|
|
using System.IO;
|
|
|
|
|
|
|
|
namespace SFML
|
|
|
|
{
|
|
|
|
namespace Graphics
|
|
|
|
{
|
2009-12-13 15:49:30 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Structure describing a glyph (a visual character)
|
|
|
|
/// </summary>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
|
|
public struct Glyph
|
|
|
|
{
|
|
|
|
/// <summary>Offset to move horizontically to the next character</summary>
|
|
|
|
public int Advance;
|
|
|
|
|
|
|
|
/// <summary>Bounding rectangle of the glyph, in coordinates relative to the baseline</summary>
|
|
|
|
public IntRect Rectangle;
|
|
|
|
|
|
|
|
/// <summary>Texture coordinates of the glyph inside the font's image</summary>
|
|
|
|
public FloatRect TexCoords;
|
|
|
|
}
|
|
|
|
|
2009-01-28 16:18:34 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Font is the low-level class for loading and
|
|
|
|
/// manipulating character fonts. This class is meant to
|
|
|
|
/// be used by String2D
|
|
|
|
/// </summary>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public class Font : ObjectBase
|
|
|
|
{
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Construct the font from a file
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="filename">Font file to load</param>
|
|
|
|
/// <exception cref="LoadingFailedException" />
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public Font(string filename) :
|
2009-12-13 15:49:30 +00:00
|
|
|
base(sfFont_CreateFromFile(filename))
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
2009-12-13 15:49:30 +00:00
|
|
|
if (This == IntPtr.Zero)
|
|
|
|
throw new LoadingFailedException("font", filename);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-12-13 15:49:30 +00:00
|
|
|
/// Construct the font from a file in a stream
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
2009-12-13 15:49:30 +00:00
|
|
|
/// <param name="stream">Stream containing the file contents</param>
|
2009-01-28 16:18:34 +00:00
|
|
|
/// <exception cref="LoadingFailedException" />
|
|
|
|
////////////////////////////////////////////////////////////
|
2009-12-13 15:49:30 +00:00
|
|
|
public Font(Stream stream) :
|
2009-01-28 16:18:34 +00:00
|
|
|
base(IntPtr.Zero)
|
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
2009-12-13 15:49:30 +00:00
|
|
|
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));
|
|
|
|
}
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (This == IntPtr.Zero)
|
2009-12-13 15:49:30 +00:00
|
|
|
throw new LoadingFailedException("font");
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-12-13 15:49:30 +00:00
|
|
|
/// Get a glyph in the font
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
2009-12-13 15:49:30 +00:00
|
|
|
/// <param name="codePoint">Unicode code point of the character to get</param>
|
|
|
|
/// <param name="characterSize">Character size</param>
|
2009-12-14 21:26:30 +00:00
|
|
|
/// <param name="bold">Retrieve the bold version or the regular one?</param>
|
2009-12-13 15:49:30 +00:00
|
|
|
/// <returns>The glyph corresponding to the character</returns>
|
2009-01-28 16:18:34 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
2009-12-14 21:26:30 +00:00
|
|
|
public Glyph GetGlyph(uint codePoint, uint characterSize, bool bold)
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
2009-12-14 21:26:30 +00:00
|
|
|
return sfFont_GetGlyph(This, codePoint, characterSize, bold);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-12-13 15:49:30 +00:00
|
|
|
/// Get the kerning offset between two glyphs
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
2009-12-13 15:49:30 +00:00
|
|
|
/// <param name="first">Unicode code point of the first character</param>
|
|
|
|
/// <param name="second">Unicode code point of the second character</param>
|
|
|
|
/// <param name="characterSize">Character size</param>
|
|
|
|
/// <returns>Kerning offset, in pixels</returns>
|
2009-01-28 16:18:34 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
2009-12-13 15:49:30 +00:00
|
|
|
public int GetKerning(uint first, uint second, uint characterSize)
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
2009-12-13 15:49:30 +00:00
|
|
|
return sfFont_GetKerning(This, first, second, characterSize);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-12-13 15:49:30 +00:00
|
|
|
/// Get spacing between two consecutive lines
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
2009-12-13 15:49:30 +00:00
|
|
|
/// <param name="characterSize">Character size</param>
|
|
|
|
/// <returns>Line spacing, in pixels</returns>
|
2009-01-28 16:18:34 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
2009-12-13 15:49:30 +00:00
|
|
|
public int GetLineSpacing(uint characterSize)
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
2009-12-13 15:49:30 +00:00
|
|
|
return sfFont_GetLineSpacing(This, characterSize);
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
2009-12-13 15:49:30 +00:00
|
|
|
/// Get the image containing the glyphs of a given size
|
2009-01-28 16:18:34 +00:00
|
|
|
/// </summary>
|
2009-12-13 15:49:30 +00:00
|
|
|
/// <param name="characterSize">Character size</param>
|
|
|
|
/// <returns>Image storing the glyphs for the given size</returns>
|
2009-01-28 16:18:34 +00:00
|
|
|
////////////////////////////////////////////////////////////
|
2009-12-13 15:49:30 +00:00
|
|
|
public Image GetImage(uint characterSize)
|
2009-01-28 16:18:34 +00:00
|
|
|
{
|
2009-12-13 15:49:30 +00:00
|
|
|
myImages[characterSize] = new Image(sfFont_GetImage(This, characterSize));
|
|
|
|
return myImages[characterSize];
|
2009-01-28 16:18:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Default built-in font
|
|
|
|
/// </summary>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
public static Font DefaultFont
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
if (ourDefaultFont == null)
|
|
|
|
ourDefaultFont = new Font(sfFont_GetDefaultFont());
|
|
|
|
|
|
|
|
return ourDefaultFont;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Handle the destruction of the object
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="disposing">Is the GC disposing the object, or is it an explicit call ?</param>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
protected override void Destroy(bool disposing)
|
|
|
|
{
|
|
|
|
if (this != ourDefaultFont)
|
|
|
|
{
|
|
|
|
if (!disposing)
|
|
|
|
Context.Global.SetActive(true);
|
|
|
|
|
|
|
|
sfFont_Destroy(This);
|
|
|
|
|
2009-12-13 15:49:30 +00:00
|
|
|
if (disposing)
|
|
|
|
{
|
|
|
|
foreach (Image image in myImages.Values)
|
|
|
|
image.Dispose();
|
|
|
|
}
|
|
|
|
|
2009-01-28 16:18:34 +00:00
|
|
|
if (!disposing)
|
|
|
|
Context.Global.SetActive(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
/// <summary>
|
|
|
|
/// Internal constructor
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="thisPtr">Pointer to the object in C library</param>
|
|
|
|
////////////////////////////////////////////////////////////
|
|
|
|
private Font(IntPtr thisPtr) :
|
|
|
|
base(thisPtr)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-12-13 15:49:30 +00:00
|
|
|
private Dictionary<uint, Image> myImages = new Dictionary<uint, Image>();
|
2009-01-28 16:18:34 +00:00
|
|
|
private static Font ourDefaultFont = null;
|
|
|
|
|
|
|
|
#region Imports
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-12-13 15:49:30 +00:00
|
|
|
static extern IntPtr sfFont_CreateFromFile(string Filename);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-12-13 15:49:30 +00:00
|
|
|
unsafe static extern IntPtr sfFont_CreateFromMemory(char* Data, uint SizeInBytes);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
|
|
|
static extern void sfFont_Destroy(IntPtr This);
|
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
2009-12-14 21:26:30 +00:00
|
|
|
static extern Glyph sfFont_GetGlyph(IntPtr This, uint codePoint, uint characterSize, bool bold);
|
2009-12-13 15:49:30 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
|
|
|
static extern int sfFont_GetKerning(IntPtr This, uint first, uint second, uint characterSize);
|
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
|
|
|
static extern int sfFont_GetLineSpacing(IntPtr This, uint characterSize);
|
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
|
|
|
static extern IntPtr sfFont_GetImage(IntPtr This, uint characterSize);
|
2009-01-28 16:18:34 +00:00
|
|
|
|
|
|
|
[DllImport("csfml-graphics"), SuppressUnmanagedCodeSecurity]
|
|
|
|
static extern IntPtr sfFont_GetDefaultFont();
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|