public FontFile CreateFontFile(Uri filePathUri) { IDWriteFontFile dwriteFontFile = null; try { dwriteFontFile = CreateFontFile(_pFactory, WpfFontFileLoader, filePathUri); } catch { // If DWrite's CreateFontFileReference fails then try opening the file using WPF's logic. // The failures that WPF returns are more granular than the HRESULTs that DWrite returns // thus we use WPF's logic to open the file to throw the same exceptions that // WPF would have thrown before. IFontSource fontSource = _fontSourceFactory.Create(filePathUri.AbsoluteUri); fontSource.TestFileOpenable(); throw; } return(new FontFile(dwriteFontFile)); }
unsafe public FontFace CreateFontFace(Uri filePathUri, uint faceIndex, FontSimulations fontSimulationFlags) { FontFile fontFile = CreateFontFile(filePathUri); FontFileType dwriteFontFileType; FontFaceType dwriteFontFaceType; uint numberOfFaces = 0; int hr; if (fontFile.Analyze( out dwriteFontFileType, out dwriteFontFaceType, out numberOfFaces, out hr )) { if (faceIndex >= numberOfFaces) { throw new ArgumentOutOfRangeException("faceIndex"); } IntPtr dwriteFontFace = IntPtr.Zero; // IDWriteFontFace IntPtr dwriteFontFile = fontFile.DWriteFontFileIface; try { dwriteFontFace = _pFactory.CreateFontFace( dwriteFontFaceType, 1, &dwriteFontFile, faceIndex, fontSimulationFlags ); } finally { Marshal.Release(dwriteFontFile); } return(new FontFace(dwriteFontFace)); } // This path is here because there is a behavior mismatch between DWrite and WPF. // If a directory was given instead of a font uri WPF previously throws // System.UnauthorizedAccessException. We handle most of the exception behavior mismatch // in FontFile^ Factory::CreateFontFile by opening the file using WPF's previous (prior to DWrite integration) logic if // CreateFontFileReference fails (please see comments in FontFile^ Factory::CreateFontFile). // However in this special case DWrite's CreateFontFileReference will succeed if given // a directory instead of a font file and it is the Analyze() call will fail returning DWRITE_E_FILEFORMAT. // Thus, incase the hr returned from Analyze() was DWRITE_E_FILEFORMAT we do as we did in FontFile^ Factory::CreateFontFile // to try and open the file using WPF old logic and throw System.UnauthorizedAccessException as WPF used to do. // If a file format exception is expected then opening the file should succeed and ConvertHresultToException() // Should throw the correct exception. // A final note would be that this overhead is only incurred in error conditions and so the normal execution path should // not be affected. else { if (hr == unchecked ((int)0x88985000)) // DWRITE_E_FILEFORMAT { IFontSource fontSource = _fontSourceFactory.Create(filePathUri.AbsoluteUri); fontSource.TestFileOpenable(); } Marshal.ThrowExceptionForHR(hr); } return(null); }