Multiple Character Ranges in a Spritefont

I recently had a need for a sprite font of mine to include the bullet character in a sprite font for a password box.

The first thing that went through my head was, I hope it allows me to even specify non-ASCII characters. It does, but once I saw the Unicode number for the bullet character, I realized it would be a huge disaster to include everything from A to Bullet. There would be thousands of sprites in the character set, and the generated image would be absolutely massive.

The good news is, you can define your sprite font to contain more than one character ranges to pick up only the characters that your game will actually use.

After you create a SpriteFont source file, open it up and look near the bottom for the section that looks like this:

<CharacterRegions>
  <CharacterRegion>
    <Start>&#32;</Start>
    <End>&#126;</End>
  </CharacterRegion>
</CharacterRegions>

That's the section that defines what characters will be included in the sprite font, and the range 32-126 is the printable ASCII characters, which includes upper and lower case letters, numbers, and the symbols you can find on a standard keyboard.

All we have to do to include other characters is to add a new <CharacterRegion> section:

<CharacterRegions>
  <CharacterRegion>
    <Start>&#32;</Start>
    <End>&#126;</End>
  </CharacterRegion>
  <CharacterRegion>
    <Start></Start>
    <End></End>
  </CharacterRegion>
</CharacterRegions>

You can get the character you want by Googling and hunting down the character on the Internet and then copying and pasting into your SpriteFont file, or you can look it up in a list of XML character entities like this one. Be sure to use the decimal code, not the hexadecimal code. (In the link, you want the number in the parentheses.) That might look more like this:

<CharacterRegions>
  <CharacterRegion>
    <Start>&#32;</Start>
    <End>&#126;</End>
  </CharacterRegion>
  <CharacterRegion>
    <Start>&#8226;</Start>
    <End>&#8226;</End>
  </CharacterRegion>
</CharacterRegions>

(This should work in both XNA and MonoGame, as they both use XNA's content pipeline.)