public int CreateStreamFromKey(IntPtr fontFileReferenceKey, uint fontFileReferenceKeySize, out IDWriteFontFileStreamMirror fontFileStream) { uint numberOfCharacters = fontFileReferenceKeySize / 2; fontFileStream = null; if ((fontFileReferenceKeySize % 2 != 0) || // The fontFileReferenceKeySize must be divisible by sizeof(WCHAR) (numberOfCharacters <= 1) || // The fontFileReferenceKey cannot be less than or equal 1 character as it has to contain the NULL character. (Marshal.ReadInt16(fontFileReferenceKey, ((int)numberOfCharacters - 1) * 2) != '\0')) // The fontFileReferenceKey must end with the NULL character { return(unchecked ((int)0x80070057)); // E_INVALIDARG } string uriString = Marshal.PtrToStringUni(fontFileReferenceKey); int hr = 0; try { IFontSource fontSource = _fontSourceFactory.Create(uriString); FontFileStream customFontFileStream = new FontFileStream(fontSource); fontFileStream = (IDWriteFontFileStreamMirror)customFontFileStream; } catch (System.Exception exception) { hr = Marshal.GetHRForException(exception); } return(hr); }
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)); }