Jump to content

  • Log In with Google      Sign In   
  • Create Account

Banner advertising on our site currently available from just $5!


1. Learn about the promo. 2. Sign up for GDNet+. 3. Set up your advert!


wizzy121

Member Since 28 Sep 2007
Offline Last Active Apr 19 2013 03:15 AM

Posts I've Made

In Topic: SlimDX - Fonts from files

21 May 2012 - 08:10 AM

Here is something I use to load TTF (TrueType) fonts. However, OTF (OpenType) fonts wont work with this at all. This just shows you which of the GDI methods to import from gdi32.dll, and how to create/release the font handles.


[DllImport("gdi32.dll")]
public static extern IntPtr AddFontMemResourceEx(IntPtr pbFont, uint cbFont, IntPtr pdv, [In] ref uint pcFonts);

[DllImport("gdi32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RemoveFontMemResourceEx(IntPtr fh);


public FontBank()
{
	if (ifc == null)
	{
		  ifc = new System.Drawing.Text.InstalledFontCollection();
	}

	 if (pfc == null)
	 {
		  pfc = new System.Drawing.Text.PrivateFontCollection();

		  DirectoryInfo baseDir = new DirectoryInfo(Axiom.Utilities.IO.GetApplicationPath());
		  foreach (var ttf in baseDir.EnumerateFiles("*.ttf", SearchOption.TopDirectoryOnly))
		  {
	   	  this.LoadStoredFont(ttf.FullName);
		  }
	 }
}


static System.Drawing.Text.PrivateFontCollection pfc = null;

public System.Drawing.Text.PrivateFontCollection PrivateFonts
{
	get { return pfc; }
}

Dictionary<string, IntPtr> mFontHandles = new Dictionary<string, IntPtr>();

public virtual void LoadStoredFont(string fontFilename)
{
	if (mFontHandles.ContainsKey(fontFilename) == false)
	{
		  string fn = System.IO.Path.GetFullPath(fontFilename);
		  Stream fontStream = null;
		  try
		  {
			   fontStream = new FileStream(fn, FileMode.Open, FileAccess.ReadWrite);

			   System.IntPtr data = Marshal.AllocCoTaskMem((int)fontStream.Length);
			   byte[] fontData = new byte[fontStream.Length];
			   fontStream.Read(fontData, 0, (int)fontStream.Length);
			   Marshal.Copy(fontData, 0, data, (int)fontStream.Length);

			   uint resourceCount = 1;
			   IntPtr hFont = AddFontMemResourceEx(data, (uint)fontStream.Length, IntPtr.Zero, ref resourceCount);
			   mFontHandles.Add(fontFilename, hFont);

			   pfc.AddMemoryFont(data, (int)fontStream.Length);

			   fontStream.Close();
			   Marshal.FreeCoTaskMem(data);
		  }
		  catch (System.IO.FileNotFoundException fnfex)
		  {
		  }
		  catch (System.Exception ex)
		  {
		  }
		  finally
		  {
			   if (fontStream != null)
			   {
					fontStream.Close();
					fontStream.Dispose();
			   }
		  }
	 }  
}

public virtual void ReleaseEmbeddedFontHandles()
{
	if (this.mFontHandles != null)
	{
		  if (this.mFontHandles.Keys.Count > 0)
		  {
			   foreach (var fk in this.mFontHandles.Keys)
			   {
					RemoveFontMemResourceEx(this.mFontHandles[fk]);
			   }
		  }
		  this.mFontHandles.Clear();
	 }
}


public virtual SlimDX.Direct3D9.Font CreateFontObject_DX9(FontItemDescription desc)
{
	try
	{
return new SlimDX.Direct3D9.Font(
   this.D3D9Device,
   Convert.ToInt32(desc.Size),
   0,
   (SlimDX.Direct3D9.FontWeight)desc.Weight,
   0,
   desc.Italic,
   SlimDX.Direct3D9.CharacterSet.Default, SlimDX.Direct3D9.Precision.TrueType,
   (SlimDX.Direct3D9.FontQuality)desc.Quality,
   SlimDX.Direct3D9.PitchAndFamily.Default,
   desc.Name);
	}
	catch (System.Exception ex)
	{		
	}

	return null;
}


public virtual SlimDX.DirectWrite.TextFormat CreateFontObject(FontItemDescription desc)
{
	 SlimDX.DirectWrite.TextFormat fmt = this.DWriteFactory.CreateTextFormat(
				desc.Name,
				(SlimDX.DirectWrite.FontWeight)desc.Weight,
				desc.Italic ? SlimDX.DirectWrite.FontStyle.Italic : SlimDX.DirectWrite.FontStyle.Normal,
				SlimDX.DirectWrite.FontStretch.Normal,
				desc.Size,
				"en-us");
	 return fmt;
}


Once you have created your SlimDX.Direct3D9.Font then you can go ahead and use the SlimDX.Direct3D9.Font.DrawString() methods.

Or for D2D/DW, once you have created a DirectWrite.TextFormat object, you then have to go ahead and create your TextLayout and Brushes to use for drawing your text :)

NOTE: You need to add all your own error checking etc.

In Topic: Sprites not recieveing Anti-Aliasing

08 March 2012 - 02:01 AM

How did you solve it?

Are you using D3DXSprite or roll-your-own??

PARTNERS