Font spacing issue

When converting my XNA 4.0 project to Monogame, there is an issue with the size of the " " / space character. In XNA it has proper spacing, but in Monogame, the space character is exactly half the size. This makes the text very crowded and hard to read.

Adjusting the Font.Spacing value does not address the issue. The characters are properly spaced.

Notice the space in between “Tournament” and “Mode”. In Monogame that space is 10 pixels, while in XNA 4.0 that space is 20 pixels. This pretty much messes up the readability of all text. Any idea how to fix this?

Weird do you have kerning on or off in your font ?

quick fix maybe try to adjust the spacing after you load the font see if that helps,

        // add a font this ones created thru the pipeline
        font = Content.Load<SpriteFont>("MgFont");

        // prolly should get it then add a little to whatever is already there.
        // i believe the units below are in pixels can't fully remember.
        font.LineSpacing = 10; 
        font.Spacing = 10;

You might be able to find the space character as well and just alter its glyph width so space gets drawn bigger it use to be public if it still is you should be able to.

Changing the UseKerning to either True or False doesn’t seem to do anything at all.

Font.Spacing does not produce the desired effect. The characters are equally spaced in both MG and XNA, its the space character that seems to be off. I am going to try editing the glyph width as you suggested.

Edited this a few times.

Humm i dunno how reliable this will be, but you might try changing the width of the space character, or its other kerned values.

Unfortunately doing this is sort of a pain in the ass,

Here goes i did this in game1 as a test seems to work.

        /// <summary>
        /// Alter a character in this spritefont.
        /// requires ... using System.Collections.Generic;
        /// </summary>
        public static SpriteFont AlterSpriteFont(SpriteFont sf, char chartoalter, float width_amount_to_add)
        {
            Dictionary<char, SpriteFont.Glyph> dgyphs;
            SpriteFont.Glyph defaultglyph;
            char defaultchar = ' ';
            // Alter one of my methods a bit here for this purpose.
            // just drop all the alterd values into a new spritefont
            dgyphs = sf.GetGlyphs();
            defaultglyph = new SpriteFont.Glyph();
            if (sf.DefaultCharacter.HasValue)
            {
                defaultchar = (char)(sf.DefaultCharacter.Value);
                defaultglyph = dgyphs[defaultchar];
            }
            else
            {
                // we could create a default value from like a pixel in the sprite font and add the glyph.
            }
            var altered = dgyphs[chartoalter];
            altered.Width = altered.Width + width_amount_to_add;  // ect 
            dgyphs.Remove(chartoalter);
            dgyphs.Add(chartoalter, altered);

            //sf.Glyphs = _glyphs;  // cant do it as its readonly private that sucks hard we would of been done

            List<Rectangle> glyphBounds = new List<Rectangle>();
            List<Rectangle> cropping = new List<Rectangle>();
            List<char> characters = new List<char>();
            List<Vector3> kerning = new List<Vector3>();
            foreach (var item in dgyphs)
            {
                glyphBounds.Add(item.Value.BoundsInTexture);
                cropping.Add(item.Value.Cropping);
                characters.Add(item.Value.Character);
                kerning.Add(new Vector3(item.Value.LeftSideBearing, item.Value.Width, item.Value.RightSideBearing));
            }
            List<Rectangle> b = new List<Rectangle>();
            sf = new SpriteFont(sf.Texture, glyphBounds, cropping, characters, sf.LineSpacing, sf.Spacing, kerning, defaultchar);
            return sf;
        }

Under load content

    // Load the font.
        font = Content.Load<SpriteFont>("MgFont");
    // Rebuild the font with  new space characters width.
        font = AlterSpriteFont(font, ' ', 1.5f);

// https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/SpriteFont.cs
// https://github.com/MonoGame/MonoGame/blob/develop/MonoGame.Framework/Graphics/SpriteBatch.cs

I’ve noticed this too… I think the problem is this class:

It’s used in the font processor to “optimize” each font glyph, but it is also reducing all whitespace characters to 0 w/h

You should file an Issue on the MonoGame repo.

Something weird is definitely going on, I opened the font and kept resizing the width of the glyph until it produces the intended result. I had to make the space significantly larger than how it is drawn in game. It works, but I wouldn’t consider it a good fix. Will do for now. Thanks for the insights!

Interesting! I will have to play around with this to see if I can produce the desired result.