title: How do I load a custom font from an embedded file into DirectWrite in a Metro app?
body: <p>I have been fighting a losing battle against loading fonts from an embedded file for use with DirectWrite.  I am writing a simple puzzle game that has a C#/XAML interface but also uses SurfaceImageSource to add some DirectX content.  </p>
  
  <p>I have written a WinRT component that handles all of the DirectX code, and it works quite nicely.  Some of my DirectX content is text drawn using the DirectWrite API.  I can draw all the text I like so long as I'm loading an installed font from the system using <strong>IDWriteFactory::GetSystemFontCollection()</strong>, etc.  But, I cannot seem to find a way to load a custom font from an embedded file.</p>
  
  <p>From what I can tell Metro apps are not allowed to load files from the filesystem in the same way as a traditional app.  So, the <strong>IDWriteFactory::CreateFontFileReference()</strong> method that takes a normal file path is worthless to me, right?  I need to load my file from an ms-appx URL.</p>
  
  <p>So, I wrote a custom font loader in my WinRT component that implements the <strong>IDWriteFontCollectionLoader</strong> interface (which is a ton of work if you've never done it before btw) that loads the font from an ms-appx URL using the new <strong>StorageFile</strong> API.  Now, I can load my <strong>IDWriteFontFile</strong> and I can get a <strong>IDWriteFontFace</strong>, but if I try to call any of the truly useful methods on the font face it returns <strong>E_UNEXPECTED</strong>.  I can get the number of glyphs and the glyph indices, but if I try to call something like <strong>GetGlyphRunOutline()</strong> or <strong>GetDesignGlyphMetrics()</strong>, it fails with <strong>E_UNEXPECTED</strong>.  Using the same drawing code that generates an <strong>ID2D1PathGeometry</strong> using <strong>GetGlyphRunOutline()</strong> works great as long as I install the font file and get the <strong>IDWriteFontFace</strong> through the series of calls starting with <strong>IDWriteFactory::GetSystemFontCollection()</strong>.  I am working with a normal true type font.</p>
  
  <p>So, how do I load a custom font from an embedded file into DirectWrite in a Metro app?  I'm probably just missing something easy, because I am certain that other people will want to be able to load custom fonts in this way.</p>
  
  <p>I have a sample project (or could prepare one easily) for anyone who can help me identify my problem.</p>
  
  <p>I have loaded the two <strong>IDWriteFontFace</strong> objects side by side, and I tried to figure out what is different between the one that works and the one that breaks.  What I need to see in order to find out why it is failing is opaque to me hidden behind inside the <strong>IDWriteFontFace</strong> interface.  HELP PLEASE!</p>
  
  <p>Question also posted here: <a href="http://social.msdn.microsoft.com/Forums/en-US/wingameswithdirectx/thread/3bc3f37d-4826-4df3-9748-2ea60ae23709" rel="nofollow">Building Metro style games with DirectX Forum</a></p>