Back in 2011 in my early days of Windows Phone 7 development, I created Alarm Clock app. Over a period of time, I upgraded it to SL8x and made it available on Windows 8x devices. It has seen many iterations and it made sense to rework it for UWP so I don’t have 2 distinct apps any longer – just one that scales across devices.
What was unique about this app ? It has some custom font and alarm sounds.. weird and wonderful – lets just say unusual.
This time around I thought that maybe just maybe I should also allow user to select system fonts!. So how does one go about it ? Well SO #FTW 😛 Filip Skakun replied on http://Loading list of available fonts using c# in winrt mentioned DirectWrite and linked to a git repos
https://github.com/christophwille/winrt-snippets/blob/master/EnumerateFonts/EnumerateFonts/InstalledFont.cs
and
https://github.com/sharpdx/SharpDX-Samples/blob/master/Desktop/DirectWrite/FontEnumeration/Program.cs
For the sake of showing time, I wasn’t interested in symbol fonts otherwise the source is identical.
private void LoadInstalledFonts() { var factory = new SharpDX.DirectWrite.Factory(); var fontCollection = factory.GetSystemFontCollection(false); var familyCount = fontCollection.FontFamilyCount; for (int i = 0; i < familyCount; i++) { var fontFamily = fontCollection.GetFontFamily(i); var familyNames = fontFamily.FamilyNames; int index; if (!familyNames.FindLocaleName(CultureInfo.CurrentCulture.Name, out index)) familyNames.FindLocaleName("en-us", out index); string name = familyNames.GetString(index); using (var font = fontFamily.GetFont(index)) { if (font.IsSymbolFont) { continue; } } this.AvailableFonts.Add(new FontFamily(name)); } }
The DirectWrite API is exposed by SharpDX and you need to add a reference to SharpDX.Direct2D1 using Nuget Package Manager
That is all that is required. Now the system fonts are available for use however you please. I myself allow user to chose what font they want to use.
Happy Coding
Pingback: Dew Drop - July 10, 2017 (#2516) - Morning Dew