I have a custom font in my project. But WPF is not rendering it properly.
<TextBlock Text="This is a test sentence"
Foreground="Black" FontSize="50"
FontFamily="Assets/Fonts/#Custom Font"/>
Now I have two font files named:
Custom Font Medium Italic.ttf
Custom Font Bold Extended Italic.ttf
When I open these two files, the font name shown is only Custom Font. In the above textblock, if I use full file name,i.e., Custom Font Medium Italic.ttf, it doesn't work. So how to use these fonts in the textblock differently.
You need to reference the name of the font and not the name of the font file. I think that you also need to add a '#' character as well... try something like this:
<FontFamily x:Key="testfont">Assets/Fonts#Custom Font Bold Italic</FontFamily>
From the FontFamily Class page on MSDN:
XAML Values
fontFamilyFolderReference
A string specifying a folder containing the font, along with a font family name. The folder and font family name are delimited by a # character. The folder reference may be absolute, or relative. For example, "Custom Fonts#My Custom Font".
you can put the font files into different folders. e.g.
/Resources/Font/Medium/Custom Font Medium Italic.ttf
/Resources/Font/Bold_Extended/Custom Font Bold Extended Italic.ttf
then they can be referenced separately.
Related
I've a font name abc.ttf which gives bold in html web if font weight is set to bold but in codenameone, it never gives bold font. How can I achieve that?
The following code doesn't gives the bold font.
Font fnt = Font.createTrueTypeFont("abc.ttf", 3).derive(40, Font.STYLE_BOLD);
serviceTypeLabel.getAllStyles().setFont(fnt);
In theme how can we set bold font to .ttf file
Update 1:
Using bold ttf works fine but only half of the letters are seen. Have a look at the image below. Here service no 12 is only half visible:
You should use a bold TTF font. When applying the font you need to apply it to all states. If you do it after layout you need to animate the layout or revalidate. Since the bold font has different measurements.
Is there a way to load a font as a resource and set its baseline?
The following works:
<FontFamily x:Key="DefaultFontFamily">/Swift.UiResources;component/Resources/Fonts/#Meta Offc</FontFamily>
But adding the baseline like so:
<FontFamily x:Key="DefaultFontFamily" Baseline="0.9">/Swift.UiResources;component/Resources/Fonts/#Meta Offc</FontFamily>
Results in the following error:
TypeConverter syntax error encountered while processing intialization string '/Swift.UiResources;component/Resources/Fonts/#Meta Offc'. Element attributes are not allowed on objects created via TypeConverter.
I should point out that the problem I'm trying to solve is that the font handed down from marketing, when centered vertically, is render higher than the previous font.
You could create a composite font, and have all Unicode ranges map to your embedded font. You'll need to create a new file with a .CompositeFont extension, and give it a Build Action of "Resource". Its contents should be:
<!-- File: "Resources/Fonts/Custom Font.CompositeFont" in Swift.UiResources -->
<FontFamily xmlns="http://schemas.microsoft.com/winfx/2006/xaml/composite-font"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:s="clr-namespace:System;assembly=mscorlib"
Baseline="0.9">
<FontFamily.FamilyNames>
<s:String x:Key="en-US">Custom Font</s:String>
</FontFamily.FamilyNames>
<FontFamily.FamilyMaps>
<FontFamilyMap Scale="1.0"
Target="/Swift.UiResources;component/Resources/Fonts/#Meta Offc"
Unicode="0000-007F" />
</FontFamily.FamilyMaps>
</FontFamily>
Then, when referencing your font in Xaml, use this format:
/Swift.UiResources;component/Resources/Fonts/#Custom Font
The font name following the # should match the value you set in FontFamily.FamilyNames above.
If you want to map more than the standard ASCII range, you can add more comma-separated character ranges to the Unicode property.
In the past, I have noticed that when you create a composite font that uses a resource URI in one or more of its mappings, it doesn't display properly in the Visual Studio designer, though it works properly at runtime. They may have fixed this since I reported it.
I cannot figure out how to determine how to render a specific OpenType font correctly in WPF (.NET 4.5 / VS2012 / Windows 8). I have a font installed on my computer called "DINPro-BlackItalic".
The file name is: DINPro-BlackItalic.otf
Windows reports the font name is: DINPro-BlackItalic
In WPF, I do something simple, like this:
<TextBlock Text="This is a sample sentence in DINPro-BlackItalic"
FontFamily="DinPro-BlackItalic" FontSize="24" />
or FontFamily="#DinPro-BlackItalic"
This doesn't work (clearly not italic) and it falls back to the default font. I've tried lots of other variations which do work for other fonts in the font directory.
For example, Comic Sans works: FontFamily="Comic Sans MS" (awesome...)
Now, this is why I am really confused: If I just guess at a different way to punctuate the font name, it renders correctly!
This works: FontFamily="Din Pro Black Italic"
In summary, here is a screen shot of different FontFamily settings in WPF:
Two questions:
1) How am I supposed to guess at the right way to tell WPF to render this font? It must have a canonical name registered somewhere but it isn't visible in the filename, file properties, or the Font Name that Windows reports after it has parsed the file.
2) What's worse is that if I'm attempting to render the same font in a desktop application and on the Web, browsers like Chrome on Windows and even IE expect "DINPro-BlackItalic" and will not render the spaced out version of the name. It seems that WPF and windows browsers make mutually exclusive decisions about which Font Name to use to index fonts. How do I work around this for arbitrary fonts?
You can pack the font with the Application, and you can refer it from the project directory.
Say if you place the font inside Fonts Directory in the Project.
You can add as follows,
<TextBlock FontFamily=”/Fonts/DINPro-BlackItalic.otf#DinPro-BlackItalic”/>
So you can be sure the, font gets deployed with the application.
Okay so I think i found the solution..
<TextBlock FontFamily=”/Fonts/DINPro-BlackItalic.otf#Din Pro”/>
or
<TextBlock FontFamily=”/Fonts/DINPro-BlackItalic.otf#Din Pro Black Italic”/>
Havn't tried your code, but This seemed to work for me.. Referencing the path of the file+fontname
Opentype files have a commen name they share. And this is where it gets tricky, because you can only see this name if you install the font on your pc, and check it in the familytree. Like you showed before in your question:
FontFamily="Din Pro Black Italic"
This is my experience, not 100% sure that i'm correct, only that it works for me :)
For WPF, Better to try it with designer for selecting the font for Textblock
For Browsers, It can be tried with Label or any other UI controls to display.
The syntax that works for me is:
/{Application Name};component/{Font Folder in Project}/#{Font Name (not filename)}
Example:
/ApplicationName;component/Fonts/#Proxima Nova Lt
I have applied SmallCaps as shown below, but it doesn't seem to have any effect on in the browser or in a design window.
<TextBlock Text="Text Here !" Typography.Capitals="SmallCaps"/>
Why is Typography.Capitals parameter ignored? Are there any settings that need to be enabled for this ?
UPDATE
It seems that for these properties to work, the font used must support them. Silverlight can not perform magic with the font, it can only work with the features built into the font itself. And it seems that there are some differences between different versions of Windows, which made this even more confusing. I have tried this on Windows 7 and 8 using the following fonts:
Gabriola, Georgia, Verdana, Arial, Comic Sans MS, Calibri, Segoe UI, Portable User Interface
On both Win7 and 8 the only properties that ever worked were SmallCaps and AllSmallCaps. None of the other settings made any difference whatsoever, neither on Win7 or Win8. On Windows 8 these two properties worked for all the fonts listed above. On Windows 7 the only fonts where they did work were Calibri and Gabriola. I then started looking into the versions of the fonts installed on the two different machines. It turns out they are different. For example, on my Win7 machine both Verdana and Segoe UI is of version 5.05. On the Win8 machine Verdana is version 5.31 and Segoe UI is version 5.28.
So I think this is why we get different results on different machines. It has nothing to do with Silverlight, but with the versions of the fonts installed on the client machine. The version of Verdana installed on Win7 has no support for SmallCaps and AllSmallCaps, but the version that comes with Win8 does have that support.
END UPDATE
I am definitely seeing a difference with SmallCaps and AllSmallCaps. The rest of the values don't seem to do anything. It could depend on the FontFamily used I suppose. Any way, the following code renders like the screen shot below.
<ContentControl FontSize="18"
FontFamily="Segoe UI">
<StackPanel>
<TextBlock Text="Writing Some Text Here in the Text Block. AllPetiteCaps"
Typography.Capitals="AllPetiteCaps"></TextBlock>
<TextBlock Text="Writing Some Text Here in the Text Block. AllSmallCaps"
Typography.Capitals="AllSmallCaps"></TextBlock>
<TextBlock Text="Writing Some Text Here in the Text Block. Normal"
Typography.Capitals="Normal"></TextBlock>
<TextBlock Text="Writing Some Text Here in the Text Block. PetiteCaps"
Typography.Capitals="PetiteCaps"></TextBlock>
<TextBlock Text="Writing Some Text Here in the Text Block. SmallCaps"
Typography.Capitals="SmallCaps"></TextBlock>
<TextBlock Text="Writing Some Text Here in the Text Block. Titling"
Typography.Capitals="Titling"></TextBlock>
<TextBlock Text="Writing Some Text Here in the Text Block. Unicase"
Typography.Capitals="Unicase"></TextBlock>
</StackPanel>
</ContentControl>
I'm not sure how much difference there is between WPF and Silverlight on this, but apparently for WPF the font has to be an OpenType font. According to wpf.2000things.com:
WPF includes a Typography class, which allows setting various attached properties for textual elements. These properties only affect text that is rendered using an OpenType font.
And in Programming WPF, 2nd Edition:
WPF supports both TrueType and OpenType fonts. OpenType fonts often contain
many alternates to the basic set of character shapes in order to support advanced
typographical functionality. If you are using a low-level text-handling feature such as
GlyphRun, you can use these alternates directly, referring to them by glyph index. But
if you are using the higher-level elements, such as TextBlock or the FlowDocument
viewers, these can locate and use the appropriate glyphs for you. You can control
which character shapes are used with the attached properties defined by the
Typography class.
A composite font family is a collection of FontFamilies. On applying the composite font to a run, appropriate font from its font-collection is applied depending on text.
How can we get which actual font is being used in a run on which a composite font has been applied?
EDIT2: Suppose I have a run
<Run Text="Some text with different unicode characters not available in applied FontFamily" FontFamily="Global User Interface"/>
Now if I check run.FontFamily it returns "Global User Interface", but actual font applied is one from the FontFamily collection of this composite font. So I want to find out which font it is?
EDIT1: One way I can think of is - to find it the way WPF applies a font. For each language, the composite font specifies a set of fonts corresponding to specified range of characters. So we can find each of the character in a run of text, then lookup the font corresponding to this character in the fontfamilymap for current language.
Is this the right way? Are there any cases where it may fail?
Is it the only way? Is there any API\straightforward way instead?
Thanks