0

I'm trying to develop a font application. In a ListView, I have listed all installed fonts. Now I want to show the fonts name written in that font.

InstalledFontCollection installedFontCollection = new InstalledFontCollection();

fontFamilies = installedFontCollection.Families;

int fontCount = fontFamilies.Length;
familyList = new ListViewItem[fontCount];

for (int i = 0; i < fontCount; i++)
{
    familyList[i] = new ListViewItem(fontFamilies[i].Name);
}

listView1.Items.AddRange(familyList);

In the for-loop, I try setting the fonts with

familyList[i].Font = new Font(fontFamilies[i], 12);

However, all text goes blank.

If I hardcode the values, it works fine. Even when I hardcode different fonts based on random conditions, say

if (i % 2 == 0) 
    familyList[i].Font = new Font(fontFamilies[53], 12);
else 
    familyList[i].Font = new Font(fontFamilies[12], 12);

the list alternates the font as expected.

What could be the problem here? Are some fonts maybe undefined or otherwise unusable, making the entire list fail? No error is thrown.

Thanks.

Kjeld Schmidt
  • 771
  • 8
  • 19

1 Answers1

0

The Constructor

new Font(FontFamily fontFamily, Float fontSize);

automatically sets the Font Style as "regular". However, not all Fonts actually support the Regular Style.

Better results can be achieved by checking the FontStyle-enumerator:

if (fontFamilies[i].IsStyleAvailable(FontStyle.Regular)) familyList[i].Font = new Font(fontFamilies[i], 12);
else if (fontFamilies[i].IsStyleAvailable(FontStyle.Bold)) familyList[i].Font = new Font(fontFamilies[i], 12, FontStyle.Bold);
else if (fontFamilies[i].IsStyleAvailable(FontStyle.Italic)) familyList[i].Font = new Font(fontFamilies[i], 12, FontStyle.Italic);
else if (fontFamilies[i].IsStyleAvailable(FontStyle.Strikeout)) familyList[i].Font = new Font(fontFamilies[i], 12, FontStyle.Strikeout);
else if (fontFamilies[i].IsStyleAvailable(FontStyle.Underline)) familyList[i].Font = new Font(fontFamilies[i], 12, FontStyle.Underline);
else familyList[i].BackColor = Color.Red;

Not exactly very nice code. After that, most fonts installed on my machine (many custom fonts) work fine. The only one that fails all of these styles is "Blade Runner Movie Font Bold Italic", a default font.

However, we can actually fix it! And then we hope that no other Font is MORE atrocious with it's styles. The else-block gets replaced by:

else try {
    familyList[i].Font = new Font(fontFamilies[i], 12, FontStyle.Bold | FontStyle.Italic);
} catch {
    familyList[i].BackColor = Color.Red;
}

And boom, all Fonts are shown correctly! Try-catch are used because we can't check in advance whether that will work to begin with. The catch-block will still give a visible warning if something is wrong. Credit to https://stackoverflow.com/a/5350706/2532489

Cross-checking with my installed Fonts in the Fonts-Folder shows that some Fonts are plain missing.

So far, I haven't found a satisfying solution to get all fonts, find at least one possible style or even ALL styles of a installed font. No constructor that might take string-arguments (Ultra, Condensed, Black, Light, Narrow....) for the Font Style seems to exist.

https://stackoverflow.com/a/4662434/2532489 might help, but currently I can't say that with certainty.

Community
  • 1
  • 1
Kjeld Schmidt
  • 771
  • 8
  • 19