Error building Spritefont with MGCB

Hi!

I’ve imported an existing content project that contains a lot of textures and a couple of fonts. When it hits the first font an exception is thrown.

Building Font C:\<long path>\DummyContentContent\Segoe UI Mono System.Exception: Could not load C:\<long path>\DummyContentContent\Segoe UI Mono at Microsoft.Xna.Framework.Content.Pipeline.Processors.FontDescriptionProcessor.Process(FontDescription input, ContentProcessorContext context)

Any pointers of what I’m doing wrong? Other fonts work, like Segoe UI or Arial. Why not Mono?

Thanks!

// Johan

Hi, can you show your .spritefont file and build settings?

hudfont.spritefont at https://dl.dropboxusercontent.com/u/48287660/HudFont.spritefont

Could you specify what you need when you say build settings? I’ve just imported the content project. If I replace Segoe UI Mono with Segoe UI it works.

I don’t understand what your error message exactly means, just wanted more info. Your .spritefont is very simple, I suppose it is ok. Maybe “Segoe UI Mono” is not installed in your system?

I have the same error and have looked into this bug some more in the source. For the record, my error is:

Building Font C:\MyProject\MyProject\MyProjectContent\gfx\fonts\Blackoak Std
System.Exception: Could not load C:\MyProject\MyProject\MyProjectContent\gfx\fonts\Blackoak Std
   at Microsoft.Xna.Framework.Content.Pipeline.Processors.FontDescriptionProcessor.Process(FontDescription input, ContentProcessorContext context)

Note that the font is installed in Windows, and the spritefont builds and runs fine under XNA (I’m attempting to port an existing XNA game to Monogame).

The ttf is called BlackoakStd.ttf, the FontName is defined in the Blackoak.spritefont file as:

...
<FontName>Blackoak Std</FontName>
...

I have tracked the bug down to the Mono Pipeline not looking up the font properly in the Windows registry (relevant MGCB source at FontDescriptionProcessor.cs function: SpriteFontContent Process(FontDescription input, ContentProcessorContext context).

The function FontDescriptionProcessor::FindFontFileFromFontName(string fontName, string fontDirectory) correctly identifies the registry key with the font (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts) but searches for:

if (font.StartsWith(fontName, StringComparison.OrdinalIgnoreCase)) 
...

where fontName is being pulled from the .spritefont file (“Blackoak Std”). The font name specified at that registry location however is “BlackoakStd (TrueType)” (pointing at Data value “BlackoakStd.ttf”). The .StartsWith() check fails here, and the returned value from FindFontFileFromFontName is the empty string. When this function returns an empty string, it simply uses the FontName defined in the .spritefont file as the font ttf filename. The exception is being thrown when it can’t find the file.

System.Exception: Could not load C:\MyProject\MyProject\MyProjectContent\gfx\fonts\Blackoak Std

It seems that the fontName specified in the .spritefont file is correctly mapped to the installed system font in XNA, but not in Monogame.

Also:
Just to experiment, I tried changing the FontName specified in the .spritefont file to this:

<FontName>BlackoakStd</FontName>

Now the project won’t build in XNA (XNA will fail with this error:
3> Building gfx\fonts\Blackoak.spritefont -> C:\MyProject\MyProject\bin\x86\Debug\Content\gfx\fonts\Blackoak.xnb
3>C:\MyProject\MyProject\MyProjectContent\gfx\fonts\Blackoak.spritefont : error : The font family “BlackoakStd” could not be found. Please ensure the requested font is installed, and is a TrueType or OpenType font.
)

It seems like this might be a bug in the Mono Pipeline, as the behaviour of it is not consistent with that of XNA.