# How to deal with variable size font in XNA 4.0

After searching for a while, I found no way to draw vector/scalable fonts in XNA 4.0 (which would be perfect...) and I am currently in a bind about how to render fonts for my game.
In the game I use fonts of variable size, ranging from very small to quite large, and I have no idea how to efficiently deal with this.

The only way I can currently accomplish what I want is to load a large number of SpriteFonts different sizes, manually pick the font closest to the size I want, and then fine-tune the size with a scale close to 1. (A scale too large or too small will make the font look horrible.) I've also started using the Nuclex font processor which makes the fonts looks somewhat better, but it's obviously not the answer to my problem.

Is there really no other way to deal with this?
It seems very strange to me that something as elaborate and well done as XNA 4.0 has such a profound lack of tools to deal with font rendering.

This is quite a big problem for me currently, so any help and ideas on possible ways to deal with this or even to ease the pain are very welcome.

• +1 I've wondered about this before, and arrived at the same conclusion as you. Would really like to know of a better solution :) Jul 15 '12 at 1:58
• Is it not possible to decompose the vector outlines of a ttf into triangles, then rendering the shapes in 3D? Jul 15 '12 at 4:49
• What's your target platform? If Windows, you can use the content pipeline assemblies at run time to generate assets dynamically. Jul 16 '12 at 5:11

This is not an easy problem to solve. If the "multiple sprite fonts" technique doesn't work for you, there's no other built-in way in XNA to get what you want. So you have to do some custom development.

One method is to use Valve's technique, as described in the paper: Improved Alpha-Tested Magniﬁcation for Vector Textures and Special Effects. This involves specially encoded textures and a special pixel shader. I've implemented this before and it's not too difficult. The results are reasonable, although not perfect.

Another method that is worth considering is to offload font rendering to Windows (rendering fonts to a texture - just like the content pipeline does to produce bitmap fonts). This has the added benefit of allowing you to use any character from your font, rather than only what you have available in the bitmap font. If you render entire strings at once, you'll get reasonable performance and this will solve many internationalisation issues that sprite fonts have.

Another method is to simply use vector fonts (at least above a certain size threshold). This is probably the easiest to get working - on the basis that you could just use Nuclex.Fonts.

public void DrawString ( SpriteFont spriteFont, string text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth )
• The problem is that once the font has passed through the content pipeline, it is transformed into a bitmap. So what the scale parameter does is scale that bitmap, which results in a loss of quality, which he wants to avoid. Jul 15 '12 at 1:57