2012-10-22 17:18:04 +00:00
#region Using Statements
using System ;
using System.Collections.Generic ;
using System.Diagnostics ;
2012-10-19 21:59:35 +00:00
using System.IO ;
2012-10-22 17:18:04 +00:00
using System.Xml.Linq ;
2012-10-19 21:59:35 +00:00
using ANX.Framework.Content.Pipeline.Graphics ;
using ANX.Framework.NonXNA.Development ;
2012-10-22 17:18:04 +00:00
#endregion
// This file is part of the ANX.Framework created by the
// "ANX.Framework developer group" and released under the Ms-PL license.
// For details see: http://anxframework.codeplex.com/license
2012-10-19 21:59:35 +00:00
namespace ANX.Framework.Content.Pipeline.Importer
{
/// <summary>Provides methods for reading .spritefont files for use in the Content Pipeline.</summary>
2012-10-22 17:18:04 +00:00
[PercentageComplete(40)]
2012-10-19 21:59:35 +00:00
[Developer("SilentWarrior/Eagle Eye Studios")]
2012-10-22 17:18:04 +00:00
[TestState(TestStateAttribute.TestState.InProgress)] //Works except for the character list, because it seems there is no managed solution for getting a fonts characters
2012-10-19 21:59:35 +00:00
[ContentImporter("Spritefont", ".spritefont", DefaultProcessor = "FontDescriptionProcessor")]
public class FontDescriptionImporter : ContentImporter < FontDescription >
{
2012-10-22 17:18:04 +00:00
private static ContentBuildLogger _logger ;
2012-10-19 21:59:35 +00:00
/// <summary>Called by the Framework when importing a .spritefont file to be used as a game asset. This is the method called by the Framework when an asset is to be imported into an object that can be recognized by the Content Pipeline.</summary>
/// <param name="filename">Name of a game asset file.</param>
/// <param name="context">Contains information for importing a game asset, such as a logger interface.</param>
public override FontDescription Import ( string filename , ContentImporterContext context )
{
2012-10-22 17:18:04 +00:00
_logger = context . Logger ;
var doc = XDocument . Load ( filename ) ;
FontDescription fontDescription = DeserializeFont ( doc ) ;
fontDescription . Identity = new ContentIdentity ( new FileInfo ( filename ) . FullName ,
"FontDescriptionImporter" ) ;
2012-10-19 21:59:35 +00:00
return fontDescription ;
}
2012-10-22 17:18:04 +00:00
private static FontDescription DeserializeFont ( XContainer xDoc )
2012-10-19 21:59:35 +00:00
{
2012-10-22 17:18:04 +00:00
var tag = xDoc . Element ( "XnaContent" ) ;
if ( tag = = null )
{
tag = xDoc . Element ( "AnxContent" ) ;
if ( tag = = null )
throw new InvalidContentException (
"The Xml file does not contain a valid XnaContent or AnxContent tag." ) ;
}
var assetNode = tag . Element ( "Asset" ) ;
if ( assetNode = = null )
throw new InvalidContentException ( "No valid Asset element in xml source." ) ;
var xAttribute = assetNode . Attribute ( "Type" ) ;
if ( xAttribute = = null | | xAttribute . Value ! = "Graphics:FontDescription" )
throw new InvalidContentException ( "This is not a valid font description!" ) ;
var fontNameElement = assetNode . Element ( "FontName" ) ;
if ( fontNameElement = = null )
throw new InvalidContentException ( "No font name specified in xml source." ) ;
var fontName = fontNameElement . Value ;
var sizeElement = assetNode . Element ( "Size" ) ;
if ( sizeElement = = null )
throw new InvalidContentException ( "No font size specified in xml source." ) ;
var fontSize = Convert . ToInt32 ( sizeElement . Value ) ;
var spacingElement = assetNode . Element ( "Spacing" ) ;
if ( spacingElement = = null )
throw new InvalidContentException ( "Spacing is not defined in xml source." ) ;
var fontSpacing = Convert . ToInt32 ( spacingElement . Value ) ;
var styleElement = assetNode . Element ( "Style" ) ;
if ( styleElement = = null )
throw new InvalidContentException ( "No style specified in xml source." ) ;
FontDescriptionStyle fontStyle ;
Enum . TryParse ( styleElement . Value , out fontStyle ) ;
var charRegionsElement = assetNode . Element ( "CharacterRegions" ) ;
if ( charRegionsElement = = null )
throw new InvalidContentException ( "No character region specified in xml source" ) ;
var fontCharRegions = charRegionsElement . Elements ( "CharacterRegion" ) ;
var characters = new List < char > ( ) ;
foreach ( var fontCharRegion in fontCharRegions )
{
var startElement = fontCharRegion . Element ( "Start" ) ;
if ( startElement = = null )
throw new InvalidContentException ( "Character region is invalid. Start element is missing!" ) ;
var endElement = fontCharRegion . Element ( "End" ) ;
if ( endElement = = null )
throw new InvalidContentException ( "Character region is invalid. Start element is missing!" ) ;
char startChar ;
char endChar ;
_logger . LogMessage ( "Start: " + startElement . Value ) ;
_logger . LogMessage ( "End: " + endElement . Value ) ;
Debugger . Break ( ) ;
startChar = Convert . ToChar ( startElement . Value ) ;
endChar = Convert . ToChar ( endElement . Value ) ;
Debugger . Break ( ) ;
//TODO: We need a platform independet solution for getting the characters supported by a font!
}
var result = new FontDescription ( fontName , fontSize , fontSpacing , fontStyle )
{
Characters = characters
} ;
return result ;
2012-10-19 21:59:35 +00:00
}
}
}