3

Where does the fontdialog get its list of fontstyles?

FontStyles consists only of Normal, Oblique and Italic. Yet, going thru the font styles for the various fonts, there are combinations of: medium, light, demi, bold, roman, narrow, heavy, cond, ... and more.

The fontdialog for Arial shows:

Narrow Italic, Italic, Regular, Narrow Bold, Narrow Bold Italic, Bold, Bold Italic, Black, Black Oblique

Where are they getting this list from? Some of those names don't seem to appear anywhere. Is there some list of FamilyTypeface weight, style and stretch combinations that have other assigned names or something?

Thanks for any help!

1 Answer 1

10

For system-fonts, you can get list of typefaces for a FontFamily. The typeface itself has all the details for attributes such as weight, style, stretch etc.

Sample code

<Grid Margin="20">
    <Grid.RowDefinitions>
        <RowDefinition />
        <RowDefinition Height="100" />
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition />
        <ColumnDefinition />
    </Grid.ColumnDefinitions>

    <ListBox x:Name="fontSelector" 
             ItemsSource="{x:Static Fonts.SystemFontFamilies}" />

    <ListBox x:Name="typefaceSelector" 
             ItemsSource="{Binding SelectedItem.FamilyTypefaces, ElementName=fontSelector}" DisplayMemberPath="AdjustedFaceNames[en-US]" 
             Grid.Column="1" />

    <TextBlock FontFamily="{Binding SelectedItem.Source, ElementName=fontSelector}"
               FontStretch="{Binding SelectedItem.Stretch, ElementName=typefaceSelector}"
               FontStyle="{Binding SelectedItem.Style, ElementName=typefaceSelector}"
               FontWeight="{Binding SelectedItem.Weight, ElementName=typefaceSelector}"
               Grid.ColumnSpan="2" Grid.Row="1"
               Text="Sample" 
               FontSize="30"
               HorizontalAlignment="Center"
               VerticalAlignment="Center" />
</Grid>

enter image description here

To further expand this list in WPF, you can use InstalledFontCollection from win-forms to get the list of installed fonts, and convert them to WPF font type(s).

var installedFontCollection = new System.Drawing.Text.InstalledFontCollection();

// Get the array of FontFamily objects.
var fontFamilies = installedFontCollection.Families;
foreach(var fontFamily in fontFamilies)
{
    var mfont = new FontFamily(fontFamily.Name);
    fontSelector.Items.Add(mfont);
}

enter image description here

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.