0

I'm using UWP for a front end GUI and a full trust background process written in .NetCore 3.1 for some heavy lifting tasks. However, I'm having an odd issue when it comes to pulling back all the installed fonts as the list returned is not consistent between the two frameworks.

In UWP the following code (based on the answer to this SO question) is used to populate a dropdown list and returns approximately 241 font families:

public List<string> FontFamilies
{
    get
    {
        return CanvasTextFormat.GetSystemFontFamilies(ApplicationLanguages.Languages).OrderBy(x => x).ToList();
    }
}

The selected fontfamily is then passed as a string to the .NetCore background process which attempts to get the font. However, var fonts = System.Drawing.Text.InstalledFontCollection() returns a very different list (477 elements) to the UWP function. Many of these additional items seem to be because the variations on the font families (e.g. regular, bold and italic) are not being collapsed into a single family. But in some cases the name is different; for example an OpenType font Playlist Script in UWP becomes Playlist in NetCore.

I've tried to seeing if the language IDs has something to do with the disparity but without success and wondered if anyone else has had to deal with a similar issue?

Dan
  • 816
  • 9
  • 14

1 Answers1

0

I hate unanswered questions on the internet, so I've posted what I came up with in solving this problem. I'm not going to mark it as the answer though in case someone else comes up with a more elegant solution at a later date.

It would appear that the various font libraries/frameworks in C# pull back different Name ID's when populating the FontFamily name. Normally, all the font names will match and this won't matter, but sometimes they don't and (given a large number of OTF fonts in play for my particular use case) this is what was causing my problem.

Using dp4 font viewer I was able to inspect the font metadata and cross reference the fields to the data being returned by the libraries. Using the Playlist Script font from my above example it turns out there were actually 3 names in play in my code.

  1. Playlist Script - I think that UWP uses a combination of the Family name and Sub-Family name.
  2. Playlist - The Family name presented by System.Drawing.Text.InstalledFontCollection
  3. Playlist-Script - The Full Font Name required by the ImageMagick.NET library I am using to render some images.

d4p Font Information

In the end I used the now abandoned SharpDX libraries as this was the only library I could find that gave me access to the font meta data to be able to extract the exact names I needed consistently.

fontFamily.GetFont(0).GetInformationalStrings(InformationalStringId.FullName, out var localizedStrings);
if (!familyNames.FindLocaleName(CultureInfo.CurrentCulture.Name, out int index))
{
    if (!familyNames.FindLocaleName("en-us", out index))
    {
        index = 0;
    }
}

var coreName = localizedStrings.Count > 0 ? localizedStrings.GetString(0) : familyNames.GetString(index);

I was then able to build a mapping of the UWP compatible names to the .NETCore compatible names and render the images as intended.

Dan
  • 816
  • 9
  • 14