1

Hey, I am trying to create a BarCode input box. On it a textblock is showing a preview of what the text input will look like in the specified Barcode type. I have a bunch of .TTF files that are the barcode fonts, they have been used in a WinForms app so I am sure they are correct. I try to load them to memory (don't want to install them) using:

sBarCodeFonts = new PrivateFontCollection();
        unsafe
        {
            fixed (byte* p = Resources.Code39)
            {
                IntPtr MyIntPtr = (IntPtr)p;
                sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.Code39.Length);
            }

            fixed (byte* p = Resources.Code128b)
            {
                IntPtr MyIntPtr = (IntPtr)p;
                sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.Code128b.Length);
            }

            fixed (byte* p = Resources.i2of5)
            {
                IntPtr MyIntPtr = (IntPtr)p;
                sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.i2of5.Length);
            }

            fixed (byte* p = Resources.ean13)
            {
                IntPtr MyIntPtr = (IntPtr)p;
                sBarCodeFonts.AddMemoryFont(MyIntPtr, Resources.ean13.Length);
            }
        }

This seems to load the PrivateFontCollection correctly, quickwatch indicates so.

In the Barcode class, I have a MyFontFamily property, which contains the System.Media.FontFamily that corresponds to the loaded file. This property is loaded like this:

MyFontFamily = new System.Windows.Media.FontFamily(sBarCodeFonts.Families[0].Name);

And it seems to be loaded correctly as well.

Then, I have a Barcode object, and I'm using a TextBlock to display it's text, using it's FontFamily:

<TextBlock Text="{Binding Path=MyBarcode.TextContent, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}, Mode=OneWay}"
                   FontFamily="{Binding Path=MyBarcode.MyFontFamily, RelativeSource={RelativeSource AncestorType={x:Type UserControl}, Mode=FindAncestor}, Mode=OneWay}" 
                   Name="txt"
                   Grid.Row="2" />

The TextBlock displays the text using a default font every time. I've debugged and the FontFamily is correctly set to one of the loaded ones in the previous C# code.

Any clues?

Thanks.

EDIT: Trying to simplify the problem, I've created a very quick and dirty test app to load the TTF and show it, this is the only code (besides the XAML with only a grid):

System.Windows.Media.FontFamily lFamily = new System.Windows.Media.FontFamily(new Uri(@"E:\Prototypes\TestApp\Resources\Code128b.ttf", UriKind.Absolute), "Code128bWin");
            TextBlock lTextBlock = new TextBlock();
            lTextBlock.Text = "jsldkasjLKJOSDIFUEWR32849792837.,´` ";
            lTextBlock.FontFamily = lFamily;
            lTextBlock.FontSize = 50.0;
            grid.Children.Add(lTextBlock);

And it still shows the default font.

SoManyGoblins
  • 5,605
  • 8
  • 45
  • 65

3 Answers3

2
<FontFamily x:Key="MyFont">/WpfTest;component/Resources/#Airplanes in the Night Sky</FontFamily>

<TextBlock FontFamily="{StaticResource MyFont}">Hello world</TextBlock>

Given that when you open the .ttf the "Font name: .." is "Airplanes in the Night Sky" and your project namespace is "WpfTest" and you dragged the font into a folder called "Resources"

Dominic
  • 62,658
  • 20
  • 139
  • 163
  • worked fine for me when i tested it, are you sure you renamed WpfTest to your project namespace name and your font is in a folder called Resources and the Build Action on the font is "Resources" and you put the FontFamily declaration in the Resources section of the xaml file you are using it in and you changed "Airplanes.." to the font name that you see when you open the ttf file? – Dominic May 03 '11 at 17:24
  • Duh! That was it, thank you very much, what I was missing mostly was the Build Action to Resource. – SoManyGoblins May 03 '11 at 20:58
  • I need this because having it as a Static Resource does not allow me to choose which FontFamily I want to use, ideally I could have the TextBlock bind to the FontFamily provided by the Barcode object. – SoManyGoblins May 03 '11 at 22:12
  • text2.FontFamily = new FontFamily(new Uri("pack://application:,,,"), "WpfTest;component/Resources/#Cactus Sandwich Plain FM"); – Dominic May 03 '11 at 22:49
0

I did not find any answer for exactly this. But I found a solution that I did not see on Internet.

I followed a recommendation of doing a folder and marking all the files inside as Resources. But I needed to enumerate them, and that was my main problem, because I need to load all to my screen without recording the name somewhere. I just want to simple drop another font to that folder and list it.

I found this as a solution to list all the files inside my resources/fonts folder

Fonts.GetFontFamilies(new Uri("pack://application:,,,/resources/fonts/#"))

I expect it to help you organize your fonts.

Skwal
  • 2,160
  • 2
  • 20
  • 30
0

I don't know about the method you're using, but I've successfully embedded fonts in a WPF application before by adding the files to the project as resources, and then creating a WPF/XAML resource in markup to reference the font file (using a pack Uri). This doesn't require the fonts to be installed on the machine - they're embedded in your .exe or a .dll file.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
  • 1
    Could you post a code sample? Ideally I'd need to set the FontFamily from code so my control could bind to it, since my barcode object can change in type and thus in the fontfamily it uses. – SoManyGoblins May 03 '11 at 15:07