Domanda

How do I edit a compiled spritefont? I'd like to change the kerning of some characters, erase a pixel or two, change aliasing, but I couldn't find any official tools that are still supported or are widely known.

If there is no way (or if it's too hard) to edit a compiled spritefont, how do I create spritefont manually?

È stato utile?

Soluzione

There is no official way to do this. You will notice that if you look at the SpriteFont type there is precious little you can modify (DefaultCharacter, Spacing and LineSpacing).

And if you look at its content-pipeline counterpart, SpriteFontContent, there are no public members at all.


Your best bet is to use ILSpy to see what private members those classes have (and how they work). And then modify them directly using reflection. SpriteFont is basically a bunch of metrics data and a Texture2D.

I recommend you make a content pipeline extension and modify the SpriteFontContent when you build it (this becomes trickier if you've got an XNB file that you didn't build yourself). It will look something like this:

[ContentProcessor(DisplayName = "Custom Font Processor")]
public class CustomFontProcessor : FontDescriptionProcessor
{
    public override SpriteFontContent Process(FontDescription input, ContentProcessorContext context)
    {
        SpriteFontContent output = base.Process(input, context);
        // ... your modifications here ...
        return output;
    }
}

To make your life easier, you could use Exposed Object which gives you a dynamic that you can then access private members directly on (making it easier to write this reflected code).

You could look at the Nuclex TrueType Importer, which implements its own sprite font processor. It's open source (albiet C++/CLI) so you can examine what it does and perhaps modify it. It includes its own version of SpriteFontContent, but sadly it is similarly locked down. The most important thing Nuclex will give you is it has comments that describe what the different metrics in the sprite font mean.

Obviously you're messing with the private parts of XNA - so don't expect it to keep working between different XNA versions. (The reason it is all private in the first place is so that the XNA team is free to modify it in the future.)


The other thing you could do is to use an alternate tool to build your font as a "Font Texture" (described here - although this article is quite old).

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top