This project is read-only.

SpriteFont.Size is Multiplied by 4/3

Jul 12, 2011 at 4:13 PM

When I load a sprite font, the .Size property is multipled by 4/3.

Let's say I added this .spritefont file:


<?xml version="1.0" encoding="utf-8"?>
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">

    Modify this string to change the font that will be imported.

    Size is a float value, measured in points. Modify this value to change
    the size of the font.

    Spacing is a float value, measured in pixels. Modify this value to change
    the amount of spacing in between characters.

    UseKerning controls the layout of the font. If this value is true, kerning information
    will be used when placing characters.

    Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
    and "Bold, Italic", and are case sensitive.

    If you uncomment this line, the default character will be substituted if you draw
    or measure text that contains characters which were not included in the font.
    <!-- <DefaultCharacter>*</DefaultCharacter> -->

    CharacterRegions control what letters are available in the font. Every
    character from Start to End will be built and made available for drawing. The
    default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
    character set. The characters are ordered according to the Unicode standard.
    See the documentation for more information.

With this code:

SpriteFont s = CurrentGame.Content.Load<SpriteFont>("Arial.spritefont");

s.FontSize reads as 21.333333.

If I change the .spritefont font size to 12, s.FontSize reads as 16.

This is easy to work around (multiply all your font sizes), but it seems like a bug to me. Is it? Why is there this discrepency?

Jul 12, 2011 at 4:22 PM
Edited Jul 12, 2011 at 4:25 PM
This is by design, is it causing you any specific issues? In the spritefont file, the size is measured in points. In Silverlight, the FontSize property is in pixels. So this conversion is done for you so that text is rendered the same size in XNA and Silverlight versions of your game.
Jul 12, 2011 at 5:03 PM

Well, when you explain it that way ... :)

I was working in points, not pixels, so it threw me for a loop, since I'm creating an abstraction layer on top of SilverSprite. But, I'll just modify my abstraction to use pixels. Pixels will probably work better for me; it's more precise when you want to lay things out exactly.