WPF app custom ttf font not being used? - c#

I downloaded a custom font which displays EAN 128 barcode.
I also downloaded some source code and sample program which converts a string to an encoded string to use with the font (includes check sum etc).
I installed the font, opened my WPF app and set a textblock font to this new font and added some text. The text comes out as if it was Arial or something.
I tried adding the font as a resource and referencing it that way but still not joy.
So I then ran the program that came with the font. The interesting part I found was that when you entered text, the program encoded it and set a label's text and the barcode showed. However, that app allowed you to print preview and print the bar code but when you did the font of the barcode in the print preview and printout changed to like Arial or what ever it was, same issue as I am seeing in my app.
This is how the xaml looks at the moment but as I say, I have tried just setting the font in expression blend.
#Code 128 is the font name and not the file name.
<TextBlock x:Name="tbkBarCode" FontFamily="/Fonts/#Code 128" FontSize="24" HorizontalAlignment="Center"/>
Any ideas?

Try this:
<TextBlock x:Name="tbkBarCode" FontFamily="pack://application:,,,/Fonts/#Code 128" FontSize="24" HorizontalAlignment="Center"/>
EDIT: I tried a different font first and this way of doing it worked.
I downloaded and tried it with a Code 128 font and it didn't work right away. To get it to work I had to change the Build Action to "Content" and the Copy to Output Directory to "Copy if newer".

Here is the method I used to do that...
I add the Font on my project
I Set property "Build action" of the Font on "Resource"
Finally, I used the Font like that :
TextBlock Grid.Column="2" HorizontalAlignment="Center" FontSize="32" VerticalAlignment="Stretch" FontFamily="./#Code 128" Grid.Row="5" Margin="1" Text="{Binding Checksum}"/

If you create a test Windows Forms app, add a TextBox and select the "Code 128" font as the font to use, then it correctly shows the Barcode font.
With WPF it looks the rendering system inside that that deals with fonts doesn't like that particular font for some reason...maybe some information it expects in the file isn't there.
Even "Glyphs" has trouble getting anything sensible out of that font file:
<Glyphs UnicodeString="ABCD1234567890" FontUri="c:\windows\fonts\code128.ttf" Height="50" Fill="#FF000000" FontRenderingEmSize="25"/>
it just returns "blocks".....which means no-character.
When you use a TextBox/TextBlock etc....the fallback font is used to display the content (because it is unable to display it with the Code123 font)......which is why you see the text reverting to the Arial font (or whatever your fallback font is defined to be).
From where did you download the custom font and sample program...was it this place?
http://www.barcoderesource.com/wpfbarcode.shtml
Or from here?:
http://grandzebu.net/informatique/codbar-en/code128.htm
Are you sure the font name is #Code 128 ?
If you copy the .ttf font into your \Windows\Fonts directory and then use CharacterMap, what font names can you see?
After installation with the barcoderesource font, I have these fonts listed:
That would mean using:
FontFamily="CCode128_S3_Trial"
to refer to the font IF it was installed in Windows
or
FontFamily="/Fonts/#CCode128_S3_Trial"
to refer to a .ttf font file embedded into your application.
Did you add the .TTF font file to your project in a folder called "Fonts" and set the Build Type="Resource"?
http://msdn.microsoft.com/en-us/library/ms753303.aspx

Related

How to apply custom font icon shapes in UWP

I want to show some custom shapes through font icons in my UWP project. I have created my own .ttf file for custom shapes. But I don't know how to apply it programmatically.
TextBlock text = new TextBlock();
string font = "Assets/mycustomfont.webfont.ttf#MyCustomFont";
text.FontFamily= new Windows.UI.Xaml.Media.FontFamily(font);
this.grid1.Children.Add(text);
Anyone please help me on this.
The approach you are using is practically correct, but make sure to check the following:
The .ttf file must be included in the project and have Build Action set to Content in the Properties window
Start the path to the font with / to make sure it begins in root.
Ensure the # suffix actually matches the font metadata. A font viewer app like dp4 Font Viewer can help you with that. Use the Font Family name as the suffix.
I have written an article on my blog about using custom fonts in UWP so check it out to see if you haven't missed some of the steps there.

Xamarin.Forms font family not working for UWP

I have been following the Xamarin guide to adding fonts to my app. I have not had any issues with iOS or Android, but my UWP app is ignoring the font and using the default font.
I added my font to my UWP project in Assets\Fonts\FiraSans-Regular.otf.
My XAML markup to apply the font is as follows:
<Label HeightRequest="50" Text="My sample text">
<Label.FontFamily>
<OnPlatform x:TypeArguments="x:String">
<OnPlatform.iOS>FiraSans-Regular</OnPlatform.iOS>
<OnPlatform.Android>FiraSans-Regular.otf#FiraSans-Regular</OnPlatform.Android>
<OnPlatform.WinPhone>Assets/Fonts/FiraSans-Regular.otf#FiraSans</OnPlatform.WinPhone>
</OnPlatform>
</Label.FontFamily>
</Label>
Does this appear to be the correct way to apply a font for my UWP app?
I've downloaded the Font and you did almost correct. Add the space in font name:
<OnPlatform.WinPhone>Assets/Fonts/FiraSans-Regular.otf#Fira Sans</OnPlatform.WinPhone>
You have to use correct font name which you can find during font installation.

Defining a custom FontFamily with several font files in XAML/C#

I'm trying to define a custom font in a C# WinRT app. I can define a single-file font in XAML like so:
<Page.Resources>
<FontFamily x:Key="Cousine">/Fonts/Cousine-Regular.ttf#Cousine</FontFamily>
</Page.Resources>
But I have several other font files that I would like to be used where appropriate:
Cousine-Bold.ttf
Cousine-BoldItalic.ttf
Cousine-Italic.ttf
How do I define a font family that uses the different fonts for the different font weights/variants?
They should all be in the same ttf file and you reference them like:
/Fonts/Cousine-Regular.ttf#Cousine
/Fonts/Cousine-Regular.ttf#Cousine Bold
/Fonts/Cousine-Regular.ttf#Cousine Italic
/Fonts/Cousine-Regular.ttf#Cousine Bold Italic
I don't know if the last one is right. I came here looking for the answer to that question. It might be BoldItalic. Whatever it is, I believe you create that name when you create the ttf file so you would be able to figure it out.

C# WinForms FontDialog can't be initialized with some fonts?

I am using VS C# 2008 Express.
I am adding the capability to store and retrieve a user's font selection to my app using the standard FontDialog control.
During my testing I have noticed that certain fonts do not initialize the dialog fully -- the name does not appear in the "Font" box, and the sample is blank.
These are fonts in which part of the name appears in the "Font Style" list on the dialog: "Arial Black", "Segoe UI Light", "Segoe UI Semibold", etc. (Although, interestingly, "Arial Narrow" does work as expected.)
I see the same problem whether I initialize the dialog's font in code or if I set the properties of the FontDialog control to one of the above problem fonts in the IDE.
It is easy to reproduce by just creating a simple form, adding a FontDialog control launched by a button, and setting its font property to "Arial Black" and size to 16.
Any way to resolve this?
Added information:
This problem exhibits on a Windows 7 64-bit system. On Vista and XP, Arial Black and Arial Narrow are listed as separate fonts, rather than just Arial with Black and Narrow styles. So it seems Microsoft is doing something different with the common font dialog in Win7.
I can confirm that behavior, you should wait and see if someone offers a definitive explanation for why this is correct, and if nobody offers one, report it through Microsoft Connect.
Here's how to reproduce the problem, use LINQPad and execute the following program:
void Main()
{
using (var dlg = new FontDialog())
{
dlg.Font = new Font("Arial Black", 16);
dlg.Font.Dump("before");
dlg.ShowDialog();
dlg.Font.Dump("between");
dlg.ShowDialog();
dlg.Font.Dump("after");
}
}
use F4 to add references to System.Drawing and System.Windows.Forms, and hit Ctrl+. on Font and FontDialog to add the required using clauses
Then observe that:
Arial Black is not chosen the first time it is shown
If you, during that first time, pick Arial, and then pick Black in the font style picker, click OK, the dialog comes back up with no font selected.
The only thing different between the 3 dumps of the font (provided you picked Arial, Black, 16 both times) is that the size is slightly off after the dialogs (15.75pt vs. 16pt). Also, the OriginalFontName is left as null after the dialogs. The font name, however, is the same, "Arial Black".
The reason I feel this is a bug is that if the same dialog pops up twice, with the second time showing the results of picking values the first time, it should show the same information.

Cannot load a spriteFont in XNA4

Im goofing around with XNA and I've come to the part where I need to load a font in. Easy enough right?
Font1 = Content.Load<SpriteFont>("Arial");
Is the code that I'm using to load the font.
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
<Asset Type="Graphics:FontDescription">
<!--
Modify this string to change the font that will be imported.
-->
<FontName>Arial</FontName>
<!--
Size is a float value, measured in points. Modify this value to change
the size of the font.
-->
<Size>14</Size>
<!--
Spacing is a float value, measured in pixels. Modify this value to change
the amount of spacing in between characters.
-->
<Spacing>0</Spacing>
<!--
UseKerning controls the layout of the font. If this value is true, kerning information
will be used when placing characters.
-->
<UseKerning>true</UseKerning>
<!--
Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
and "Bold, Italic", and are case sensitive.
-->
<Style>Regular</Style>
<!--
If you uncomment this line, the default character will be substituted if you draw
or measure text that contains characters which were not included in the font.
-->
<!-- <DefaultCharacter>*</DefaultCharacter> -->
<!--
CharacterRegions control what letters are available in the font. Every
character from Start to End will be built and made available for drawing. The
default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
character set. The characters are ordered according to the Unicode standard.
See the documentation for more information.
-->
<CharacterRegions>
<CharacterRegion>
<Start> </Start>
<End>~</End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>
is the spritefont file im using. its located at Content/Arial.spritefont.
Regardless of what font I choose, it is unable to load that font.
Error loading "Arial". File not found.
The code you have posted appears fine, providing you are setting the content root directory (this line is part of the default template):
Content.RootDirectory = "Content";
And providing you have correctly added the .spritefont file to your content project and it is building correctly. Check that an Arial.xnb file is being created in the Content directory beside your executable (in bin/Debug or bin/Release depending on your build target).
If you still have problems, try creating a fresh XNA project and seeing if you can get a font to work in that.
why not using SpriteFont Texture, where you have all letters in one single image. by this you can show font that other computers does not have. Just remember to change ContentProcessor to Font Txture after you add texture to project.
SpriteFont Editor
Some Tutorial
Be sure your content project is added as a content reference to your main project. Right click your project and choose "Add Content Reference." Then just choose the content project(s) listed. This will ensure your content is actually copied.

Categories