다음을 통해 공유


Microcode: PowerShell Scripting Tricks: Get-Font

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. It comes with a nifty little platformer game (think a 3-screen Super Mario Brothers), which helps a lot with the process of learning XNA.

XNA involves a lot of little resource files, including one called a SpriteFont. The format for a SpriteFont file is really simple XML, so I decided to make a quick PowerShell function that would create a sprite font file. I could be very lazy, and simply accept whatever font name they gave me on faith, but I'm sure I would be very frustrated the first time I typed New-SpriteFont Arrial, and spent the next 15 minutes of my life wondering why my project wasn't compiling when all that was wrong was an extra 'r'. To save myself this future pain, I've made Get-Font, so New-SpriteFont can check what fonts exist before creating the file.

Making Get-Font wasn't all that hard. I simply used my Get-Type function to find all of the types named fonts (Get-Type | Where-Object { .Name -eq "Fonts" } | Select FullName) and then explored static member properties. I wrote Get-Font in the Windows PowerShell Integrated Scripting Editor, so I added a check to load up the right assembly (which I also found out by using Get-Type) in case I was trying to use Get-Font from the PowerShell console. All told, Get-Font took about 5 minutes to write, and ended up being a quick PowerShell lesson for my girlfriend. I added inline help, and then I was done. Here's Get-Font. I'll show New-SpriteFont in a separate post.

Get-Font

Synopsis:

Gets the fonts currently loaded on the system

Syntax:

Get-Font [[-font] [<Object>]] [<CommonParameters>]

Detailed Description:

Uses the type System.Windows.Media.Fonts static property SystemFontFamilies,
to retrieve all of the fonts loaded by the system. If the Fonts type is not found,
the PresentationCore assembly will be automatically loaded

Examples:

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





# Get All Fonts
Get-Font
    
     -------------------------- EXAMPLE 2 --------------------------





# Get All Lucida Fonts
Get-Font *Lucida*
    

Command Parameters:

Name Description
font A wildcard to search for font names

Here's Get-Font:

 function Get-Font {
           
    <#
    .Synopsis
        Gets the fonts currently loaded on the system
    .Description
        Uses the type System.Windows.Media.Fonts static property SystemFontFamilies,
        to retrieve all of the fonts loaded by the system.  If the Fonts type is not found,
        the PresentationCore assembly will be automatically loaded
    .Parameter font
        A wildcard to search for font names
    .Example
        # Get All Fonts
        Get-Font
    .Example
        # Get All Lucida Fonts
        Get-Font *Lucida*
    #>
    param($font = "*")
if (-not ("Windows.Media.Fonts" -as [Type])) {
        Add-Type -AssemblyName "PresentationCore"
    }       

    [Windows.Media.Fonts]::SystemFontFamilies |
        Where-Object { $_.Source -like "$font" } 

}
    

Hope this Helps,

James Brundage [MSFT]

Automatically generated with Write-CommandBlogPost

Comments

  • Anonymous
    December 23, 2008
    As I said in a previous post, I've started to explore the wild world of XNA . XNA just released version