Problem showing a symbol from a custom font
-
I have a custom font. The font contains symbols at specific addresses. I want to display one of the symbols in a TextBlock. The font (ttf-file) is located in my applications's folder: Fonts. I have included the font in the application and set the
BuildAction
toResource
. I have verified the font's name (not file name) to reference it in my code. I have verified which "address" the symbol should be fetched from in the font. I have tried this both in XAML and with C#. But it won't work. XAML<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#MyFont" />
Notice: Never mind the space in the middle of "address" for the first and second TextBlock. The code-view here at CodeProject failes to display the address. It tries, and fails, to fetch the correct symbols by that address - if I remove the space in the addresses... C# Here is the handler for the
Loaded event
:private void Window_Loaded(object sender, RoutedEventArgs e)
{
txt1.FontFamily = new FontFamily("pack://application:,,,/Main;component/Fonts/#MyFont");
txt1.Text = "\xe905";txt2.FontFamily = new FontFamily("Segoe MDL2 Assets"); txt2.Text = "\\xe7777";
}
Result The result are displayed in the four TextBlocks. From top to bottom: * First: This fails. I get an empty rectangle, like these rectangles. * Second: This line works. You should be able to copy this line into your own application with success. * Third: Same as the first TextBlock. An empty rectangle. *
-
I have a custom font. The font contains symbols at specific addresses. I want to display one of the symbols in a TextBlock. The font (ttf-file) is located in my applications's folder: Fonts. I have included the font in the application and set the
BuildAction
toResource
. I have verified the font's name (not file name) to reference it in my code. I have verified which "address" the symbol should be fetched from in the font. I have tried this both in XAML and with C#. But it won't work. XAML<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#MyFont" />
Notice: Never mind the space in the middle of "address" for the first and second TextBlock. The code-view here at CodeProject failes to display the address. It tries, and fails, to fetch the correct symbols by that address - if I remove the space in the addresses... C# Here is the handler for the
Loaded event
:private void Window_Loaded(object sender, RoutedEventArgs e)
{
txt1.FontFamily = new FontFamily("pack://application:,,,/Main;component/Fonts/#MyFont");
txt1.Text = "\xe905";txt2.FontFamily = new FontFamily("Segoe MDL2 Assets"); txt2.Text = "\\xe7777";
}
Result The result are displayed in the four TextBlocks. From top to bottom: * First: This fails. I get an empty rectangle, like these rectangles. * Second: This line works. You should be able to copy this line into your own application with success. * Third: Same as the first TextBlock. An empty rectangle. *
As far as I can see, you can't use a full
pack:
URI to reference an embedded font:Packaging Fonts with Applications - WPF .NET Framework | Microsoft Docs[^]:
In order to reference font resource items from code, you must supply a two-part font resource reference: the base uniform resource identifier (URI); and the font location reference.
If it's a single application, try using
./Fonts/#MyFont
from XAML, andnew FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#MyFont")
from code.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
As far as I can see, you can't use a full
pack:
URI to reference an embedded font:Packaging Fonts with Applications - WPF .NET Framework | Microsoft Docs[^]:
In order to reference font resource items from code, you must supply a two-part font resource reference: the base uniform resource identifier (URI); and the font location reference.
If it's a single application, try using
./Fonts/#MyFont
from XAML, andnew FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#MyFont")
from code.
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Thanks for the reply. Unfortunately it didn't work. Changing the syntax for the reference of the Font didn't solve the problem. Thanks for suggesting that anyway. It was worth a try. For this question I have created a simple application where the Fonts are stored in the same project as my executable. I can reproduce the error in this small application for myself and to post in this forum. In my real project I have all this is in a WPF User Control Library. That is way I have the exact path to the font with the assembly reference.
-
Thanks for the reply. Unfortunately it didn't work. Changing the syntax for the reference of the Font didn't solve the problem. Thanks for suggesting that anyway. It was worth a try. For this question I have created a simple application where the Fonts are stored in the same project as my executable. I can reproduce the error in this small application for myself and to post in this forum. In my real project I have all this is in a WPF User Control Library. That is way I have the exact path to the font with the assembly reference.
-
Show the code you tried; maybe you didn't get it right.
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food
I have tried this:
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#MyFont" /> <Setter Property="TextElement.FontFamily" Value="./Fonts/#MyFont" /> ./Fonts/#MyFont
In the code, I have tried these different ways to set the FontFamily property. I have just listed them here. But have run them separately. All failed...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
txt1.FontFamily = new FontFamily("pack://application:,,,/Main;component/Fonts/#MyFont");
txt1.FontFamily = new FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#MyFont");
txt1.FontFamily = new FontFamily("MyFont");
txt1.Text = "\xe905";
}Notice the
txt1.FontFamily = new FontFamily("MyFont");
I have installed the font from it's ttf-file. If I open the Character map application in Windows I can see all symbols that I require. I just can't understand what is going on... -
I have tried this:
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#MyFont" /> <Setter Property="TextElement.FontFamily" Value="./Fonts/#MyFont" /> ./Fonts/#MyFont
In the code, I have tried these different ways to set the FontFamily property. I have just listed them here. But have run them separately. All failed...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
txt1.FontFamily = new FontFamily("pack://application:,,,/Main;component/Fonts/#MyFont");
txt1.FontFamily = new FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#MyFont");
txt1.FontFamily = new FontFamily("MyFont");
txt1.Text = "\xe905";
}Notice the
txt1.FontFamily = new FontFamily("MyFont");
I have installed the font from it's ttf-file. If I open the Character map application in Windows I can see all symbols that I require. I just can't understand what is going on... -
I have tried this:
<Setter Property="TextElement.FontFamily" Value="pack://application:,,,/Main;component/Fonts/#MyFont" /> <Setter Property="TextElement.FontFamily" Value="./Fonts/#MyFont" /> ./Fonts/#MyFont
In the code, I have tried these different ways to set the FontFamily property. I have just listed them here. But have run them separately. All failed...
private void Window_Loaded(object sender, RoutedEventArgs e)
{
txt1.FontFamily = new FontFamily("pack://application:,,,/Main;component/Fonts/#MyFont");
txt1.FontFamily = new FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#MyFont");
txt1.FontFamily = new FontFamily("MyFont");
txt1.Text = "\xe905";
}Notice the
txt1.FontFamily = new FontFamily("MyFont");
I have installed the font from it's ttf-file. If I open the Character map application in Windows I can see all symbols that I require. I just can't understand what is going on...What does setting FontFamily "3 times" accomplish? (Doesn't work as an example of what you tried in Richard's case)
txt1.FontFamily = new FontFamily("pack://application:,,,/Main;component/Fonts/#MyFont");
txt1.FontFamily = new FontFamily(new Uri("pack://application:,,,/Fonts/"), "./#MyFont");
txt1.FontFamily = new FontFamily("MyFont");See what this does for you (re: installed fonts): [How to: Enumerate Installed Fonts - Windows Forms .NET Framework | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/desktop/winforms/advanced/how-to-enumerate-installed-fonts?view=netframeworkdesktop-4.8) [How to: Enumerate System Fonts - WPF .NET Framework | Microsoft Docs](https://docs.microsoft.com/en-us/dotnet/desktop/wpf/advanced/how-to-enumerate-system-fonts?view=netframeworkdesktop-4.8)
It was only in wine that he laid down no limit for himself, but he did not allow himself to be confused by it. ― Confucian Analects: Rules of Confucius about his food