How to read font file stream from WinRT platform? I need to get font file content from C# UWP. As far as you probably know there is no way to read files from Fonts folder directly. FilePicker is also not an option for me, since it's not a user responsibility to choose this folder. I found the way to enumerate font names using DirectWrite (C++) and then wrapping it with COM component which will be available in C# (https://code.msdn.microsoft.com/FontExplorer-lets-you-f01d415e#content), I wonder if the similar thing can be done to read font file content as byte[] or Stream?
Asked
Active
Viewed 773 times
0
-
What font information are you looking for specifically? You won't be able to directly open the font file, but using DirectWrite, you can access a lot of details. – WiredPrairie Nov 05 '15 at 11:53
-
I need all bytes of ttf. I'm writing PDF export and I need to embed fonts. – Access Denied Nov 05 '15 at 11:56
-
I don't think it's doable. Not to mention the legal implications of distributing (commercial) fonts this way that you need to take into consideration. Plus the whole propose of these fancy apps is to make them self-contained and prevent access to external files afaik. Worst case... bitmap them. :) – CodeAngry Nov 05 '15 at 15:09
-
Yes, it would be great if they provide api to read fonts. So that I don't have to use file api for that. My app will be self contained in that case and I will be happy : ) – Access Denied Nov 06 '15 at 03:57
3 Answers
0
You cannot directly read the TTF file from a UWP app without the user navigating to the file manually. The UWP application is not allowed to open files without the user being prompted unless they are in specific locations.
Also, as mentioned in a comment, many fonts may not be distributed or embedded without special licenses.

WiredPrairie
- 58,954
- 17
- 116
- 143
-
1. I know that it's imposible to read file directly, I mentioned that in my question BTW. But is there any chance to capture font content during loading or load font content using native code? For example in sharpdx there are plenty interesting interfaces like FontFileLoader, factory.RegisterFontLoader. I don't see any sense to restrict read permissions to fonts directory. If microsoft sees it, it must provide alternative API to get fonts, but there is no winrt api for that nowadays. – Access Denied Nov 06 '15 at 03:47
-
2. It's not a big dial to check whether font is allowed for distribution or not. This information is stored in ttf, what is needed for that is just to read one bite. So, don't worry about licensing, it's not a problem if you have ttf bytes. – Access Denied Nov 06 '15 at 03:47
-
3. It was OK to write PDF export in Windows Forms, WPF, and I don't understand what is bad to implement it in WinRT. My app requires PDF export and it's not an option not to implement it, because somebody decided that it's not necessary to have access to fonts. Even font enumerating API is not available in winRT, which is necessary for many applications. – Access Denied Nov 06 '15 at 03:50
-
4. ios apps also use sandbox approach and there is a way to get ttf file: read ttf table data and then write it to ttf file. https://gist.github.com/Jyczeal/1892760 – Access Denied Nov 06 '15 at 05:38
0
Good news: PDF export doesn't make much sense in windows 10. Windows 10 has build-in PDF printer. So, it's better to kill 2 birds with one stone: implement printing and get PDF export free of charge.

Access Denied
- 8,723
- 4
- 42
- 72
0
Assuming you already got as far as you have created IDWriteFontFile
instance, then it's easy to read arbitrary file fragment:
- Get file reference key with
IDwriteFontFile::GetReferenceKey();
. - Get loader interface with
IDWriteFontFile::GetLoader();
- Create stream instance with
IDWriteFontFileLoader::CreateStreamFromKey()
using key from step 1. - Use
IDWriteFontFileStream::ReadFileFragment/ReleaseFileFragment
to read from file stream to your buffer.

J0e3gan
- 8,740
- 10
- 53
- 80

bunglehead
- 1,104
- 1
- 14
- 22