Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Problem showing a symbol from a custom font

Problem showing a symbol from a custom font

Scheduled Pinned Locked Moved C#
csharpwpfcomdata-structureshelp
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Mc_Topaz
    wrote on last edited by
    #1

    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 to Resource. 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. *

    Richard DeemingR 1 Reply Last reply
    0
    • M Mc_Topaz

      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 to Resource. 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. *

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      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, and new 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      M 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        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, and new 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

        M Offline
        M Offline
        Mc_Topaz
        wrote on last edited by
        #3

        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.

        L 1 Reply Last reply
        0
        • M Mc_Topaz

          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.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #4

          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

          M 1 Reply Last reply
          0
          • L Lost User

            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

            M Offline
            M Offline
            Mc_Topaz
            wrote on last edited by
            #5

            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...

            M L 2 Replies Last reply
            0
            • M Mc_Topaz

              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...

              M Offline
              M Offline
              Mc_Topaz
              wrote on last edited by
              #6

              I have solved it! It was the darn TTF-file which was the problem. When I changed to another font, the XAML-code worked straight away!

              1 Reply Last reply
              0
              • M Mc_Topaz

                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...

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                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

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups