Introduction

Manufaktura.Controls now supports SMuFL-compliant music fonts (https://www.smufl.org/). Each  SMuFL font is distributed as font file (ttf, otf, svg, woff, etc.) and JSON metadata file (<fontname>_metadata.json). Only metadata version 1.2 was tested so far so fonts with the previous version of metadata may not work.

Using SMuFL fonts in WPF

You can load your font into NoteViewer control with this method:

noteViewerTest.LoadFont(family, metadataJson);

The first parameter is font family. You can add the font as a Resource to your project and create a font family like that:

new FontFamily(new Uri("pack://application:,,,/<Assembly name>;component/Assets;component/"), "./#Bravura")

The next parameter is font metadata which you can supply as SMuFLFontMetadata object or string with JSON metadata.  You can read the metadata from disk as stream or add it to your project as Embedded Resource.

If you want to manually deserialize the metadata, you can accomplish it with Newtonsoft.Json library which you can get from Nuget:

                using (var stream = assembly.GetManifestResourceStream(resourceName))

                using (var reader = new StreamReader(stream))

                {

                    string result = reader.ReadToEnd();

                    var metadataJson = JsonConvert.DeserializeObject<SMuFLFontMetadata>(result);

                }

Deserializing metadata can be a time-consuming task  (JSON.Net does it in a few seconds) so you should consider running it asynchronously and only once in application lifetime.

The next three optional parameters are font sizes:

  • musicFontSize – size of regular noteheads and all other symbols like clefs, etc.
  • graceNoteFontSize – size of grace and cue notes,
  • staffFontSize – size of elements that cover whole staff, ie. repeat barlines.

These settings will vary depending on font that you use. You have to choose the right values so that the noteheads will not go out of staff fields.

You can always revert to default Polihymnia font with LoadDefaultFont method.

Using SMuFL fonts in UWP

Loading SMuFL fonts is similar as in WPF but with one difference. UWP API can’t calculate font metrics so LoadFontMethods has one additional parameter: cellAscent which defaults to 24.8. You will have to make sure if notes appear in appropriate lines and change this parameter if they are misplaced vertically.

Using SMuFL fonts in Winforms

It’s similar as in WPF version but you don’t create a FontFamily object. You can either load a font that is installed on the machine (LoadFont) or load it from a path on the disk (LoadFontFromPath). As in WPF version, you can enter font sizes for noteheads, grace notes and staff elements.

Using SMuFL fonts in Web (ASP.NET MVC, ASP.NET Core, etc)

In web version you have to create a HtmlScoreRendererSettings object (see Web Tutorial). Then you just load the metadata and set proper font paths:

                   settings.LoadSMuFLFont(metadataJson, MusicFontName, 20, MusicFontPath);

                   settings.Fonts[MusicFontStyles.GraceNoteFont] = new HtmlFontInfo(MusicFontName, 14, MusicFontPath);

                   settings.Fonts[MusicFontStyles.StaffFont] = new HtmlFontInfo(MusicFontName, 18, MusicFontPath);

Note that deserializing metadata can be a time-consuming task  (JSON.Net does it in a few seconds for the first time) so you should consider running it asynchronously and only once in  application lifetime.