Skip to main content
GameDev.net gamedev.net
🔒 Locked

SlimDX - Fonts from files

Started by wykasz May 20, 2012 at 1:44 AM 0 replies 2.9k views
Original Post
wykasz
wykasz
Hi,

I'm usind Direct2D for drawind 2D graphics and DirectWrite with Direct2D for text rendering.
I'm developing simple game engine, and i encountered a problem.

I need to use font from file, but i can't handle implementation of IFontFileLoader interface and other necessary classes.
I was trying to imitate solution from SharpDX Custom Font sample but SharpDX is slightly diffrent than SlimDX at this point.
Unfortunalety, ther is no example of that issue in google.
I'm hard-pressed and i need to find solution as fast as possible.
Can anyone help with that? Maybe sombady has a solution that can be published ;)

Sorry for my english,
Thanks, for help.
wizzy121
wizzy121
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.