As I said in a previous post, I've started to explore the wild world of XNA. XNA just released version 3, which allows you to make games for Windows, Xbox, and Zune. The SDK is free, and you can download it here.

The last post introduced Get-Font, a quick function that lets me see what fonts are on the system. I wrote this function to save me the potential future headache of making a typo in my SpriteFont file, and in this post I'll show you New-SpriteFont, which uses Get-Font and emits a Sprite Font file with a number of parameters for any font on my system.

New-SpriteFont was fairly straightforward. I simply check for any font that is named what I say, and throw an error if one is not found. Then I copied a sprite font file from the sample Platformer project into my function, and added a parameter for each part of the XML I wanted to parameterize in a script. Since font names cannot contain characters that would trouble XML (and I've already checked to make sure the font exists), I don't need to worry about escaping my XML for the font name. Almost everything else was an integer, except for style. The only interesting curve ball that the spritefont XML threw me was that it used a lowercase true. True will read as "True", and since XML is case-sensitive (even though PowerShell is not), I coerced the switch parameter into a string and then turned the string into a lowercase string.

Then I tacked on some inline help, and I had a New-SpriteFont that did the trick. I'm using Write-CommandBlogPost to write up New-SpriteFont for public consumption. Enjoy!


Synopsis:

Creates a New XNA SpriteFont


Syntax:

New-SpriteFont [[-font] [<Object>]] [[-size] [<Int32>]] [[-spacing] [<Int32>]] [[-style] [<Object>]] [<CommonParameters>]


Detailed Description:

Generates an XNA SpriteFont file. Allows you to specify the font name, size, spacing, style, and if kerning is used or not


Examples:

    -------------------------- EXAMPLE 1 --------------------------





# Create a simple New sprite font
New-SpriteFont
    
    -------------------------- EXAMPLE 2 --------------------------





# Create a sprite font using the font Garamond and a fontsize of 12
New-SpriteFont "Garamond" 12
    


Command Parameters:

Name Description
font The font name of the XNA sprite font to use (default Lucida Console)
size The font size of the XNA Sprite Font (default 14)
spacing The spacing between the fonts (default 0)
useKerning If set, the XNA sprite font will use kerning
style The style of the font (e.g. Regular, Bold, Italic)


Here's New-SpriteFont:

function New-SpriteFont {
           
    <#
    .Synopsis
        Creates a New XNA SpriteFont
    .Description
        Generates an XNA SpriteFont file.  Allows you to specify the font name, size, spacing, style, and if kerning is used or not
    .Parameter font
        The font name of the XNA sprite font to use (default Lucida Console)
    .Parameter size
        The font size of the XNA Sprite Font (default 14)
    .Parameter spacing
        The spacing between the fonts (default 0)
    .Parameter useKerning
        If set, the XNA sprite font will use kerning
    .Parameter style
        The style of the font (e.g. Regular, Bold, Italic)
    .Link
        Get-Font
    .Example
        # Create a simple New sprite font
        New-SpriteFont
    .Example
        # Create a sprite font using the font Garamond and a fontsize of 12
        New-SpriteFont "Garamond" 12
    #>
param($font = "Lucida Console",
    [int]$size = 14,
    [int]$spacing = 0,
    [switch]$useKerning,
    $style = "Regular")
if (-not (Get-Command Get-Font -ErrorAction SilentlyContinue )) { throw $error[0] }
    $checkedFont = Get-Font $font
    if (-not $checkedFont) {
        throw "The font name $font is not valid. Pick a new one a-hole."
    }
@"
<?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
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">

    <!--
    Modify this string to change the font that will be imported.
    -->
    <FontName>$font</FontName>

    <!--
    Size is a float value, measured in points. Modify this value to change
    the size of the font.
    -->
    <Size>$size</Size>

    <!--
    Spacing is a float value, measured in pixels. Modify this value to change
    the amount of spacing in between characters.
    -->
    <Spacing>$spacing</Spacing>

    <!--
    UseKerning controls the layout of the font. If this value is true, kerning information
    will be used when placing characters.
    -->
    <UseKerning>$($useKerning.ToString().ToLower())</UseKerning>

    <!--
    Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
    and "Bold, Italic", and are case sensitive.
    -->
    <Style>$style</Style>

    <!--
    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.
    -->
    <CharacterRegions>
      <CharacterRegion>
        <Start>&#32;</Start>
        <End>&#126;</End>
      </CharacterRegion>
    </CharacterRegions>
  </Asset>
</XnaContent>
"@    

}    


Automatically generated with Write-CommandBlogPost