Content Pipeline:

- Implemented important features of the FontDescriptionImporter. Importer works now, but still needs some (optional) features
- Fixed some bugs in the FontDescriptionProcessor. Building now works to the point where the ContentCompiler wants a generic writer instance
This commit is contained in:
SND\eagleeyestudios_cp 2012-10-28 18:33:54 +00:00 committed by Konstantin Koch
parent 744c835883
commit 3c5e98b783
4 changed files with 241 additions and 96 deletions

View File

@ -2,7 +2,11 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Xml;
using System.Xml.Linq;
using ANX.Framework.Content.Pipeline.Graphics;
using ANX.Framework.NonXNA.Development;
@ -15,10 +19,10 @@ using ANX.Framework.NonXNA.Development;
namespace ANX.Framework.Content.Pipeline.Importer
{
/// <summary>Provides methods for reading .spritefont files for use in the Content Pipeline.</summary>
[PercentageComplete(40)]
[PercentageComplete(90)]
[Developer("SilentWarrior/Eagle Eye Studios")]
[TestState(TestStateAttribute.TestState.InProgress)] //Works except for the character list, because it seems there is no managed solution for getting a fonts characters
[ContentImporter("Spritefont", ".spritefont", DefaultProcessor = "FontDescriptionProcessor")]
[TestState(TestStateAttribute.TestState.InProgress)] //Works but there should be a check whether the characters are supported
[ContentImporter("Spritefont", ".spritefont", DefaultProcessor = "FontDescriptionProcessor", DisplayName = "FontDescription Importer - ANX Framework")]
public class FontDescriptionImporter : ContentImporter<FontDescription>
{
private static ContentBuildLogger _logger;
@ -37,8 +41,10 @@ namespace ANX.Framework.Content.Pipeline.Importer
return fontDescription;
}
private static FontDescription DeserializeFont(XContainer xDoc)
{
//Check if either the XnaContent or the AnxContent attribute exists.
var tag = xDoc.Element("XnaContent");
if (tag == null)
{
@ -48,42 +54,51 @@ namespace ANX.Framework.Content.Pipeline.Importer
"The Xml file does not contain a valid XnaContent or AnxContent tag.");
}
//Check for asset element to ensure this is XNA content
var assetNode = tag.Element("Asset");
if (assetNode == null)
throw new InvalidContentException("No valid Asset element in xml source.");
//Only accept assets with type FontDescription
var xAttribute = assetNode.Attribute("Type");
if (xAttribute == null || xAttribute.Value != "Graphics:FontDescription")
throw new InvalidContentException("This is not a valid font description!");
throw new InvalidContentException("This is not a font description!");
//Check if the font name was specified
var fontNameElement = assetNode.Element("FontName");
if (fontNameElement == null)
throw new InvalidContentException("No font name specified in xml source.");
var fontName = fontNameElement.Value;
var fontName = fontNameElement.Value; // if there is a font name, store it
//Same as above, except with a conversion of the size.
var sizeElement = assetNode.Element("Size");
if (sizeElement == null)
throw new InvalidContentException("No font size specified in xml source.");
var fontSize = Convert.ToInt32(sizeElement.Value);
//Check and convert the spacing
var spacingElement = assetNode.Element("Spacing");
if (spacingElement == null)
throw new InvalidContentException("Spacing is not defined in xml source.");
var fontSpacing = Convert.ToInt32(spacingElement.Value);
//Check for style element and try to parse it.
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);
//Get the character regions element to iterate through the character regions
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 characters = new List<char>(); //The list that will be passed to the processor
foreach (var fontCharRegion in fontCharRegions)
{
//for each char declaration get the start and end elements
var startElement = fontCharRegion.Element("Start");
if (startElement == null)
throw new InvalidContentException("Character region is invalid. Start element is missing!");
@ -92,19 +107,33 @@ namespace ANX.Framework.Content.Pipeline.Importer
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!
int startChar;
int endChar;
//Convert the chars back to hex values, silly xml importer reads them as chars, int value '32' equals an empty string or hex value #32
startChar = String.IsNullOrEmpty(startElement.Value) ? 32 : Convert.ToInt32(Convert.ToChar(startElement.Value));
endChar = Convert.ToInt32(Convert.ToChar(endElement.Value));
_logger.LogMessage("Start: '" + startElement.Value + "' (" + startChar + ")");
_logger.LogMessage("End: '" + endElement.Value + "' (" + endChar + ")");
//Check if the chars are within ASCII range
if ((startChar >= endChar) ||
(startChar < 0) || (startChar > 0xFFFF) ||
(endChar < 0) || (endChar > 0xFFFF))
{
throw new ArgumentException("Invalid character range " +
startElement.Value + " - " + endElement.Value);
}
//add each char from the range to the list
for (var i = startChar; i < endChar - 1; i++)
{
characters.Add(Convert.ToChar(i));
}
//Debugger.Break();
//TODO: We need a platform independet solution for checking the characters supported by a font! For now we pass it.
}
_logger.LogMessage("Import of SpriteFont finished.");
var result = new FontDescription(fontName, fontSize, fontSpacing, fontStyle)
{
Characters = characters

View File

@ -22,11 +22,12 @@ namespace ANX.Framework.Content.Pipeline.Processors
[PercentageComplete(90)]
[Developer("SilentWarrior/Eagle Eye Studios")]
[TestState(TestStateAttribute.TestState.Untested)] //due to missing importer's font character enumeration
[ContentProcessor]
[ContentProcessor(DisplayName = "FontDescription Processor - ANX Framework")]
public class FontDescriptionProcessor : ContentProcessor<FontDescription, SpriteFontContent>
{
public override SpriteFontContent Process(FontDescription input, ContentProcessorContext context)
{
context.Logger.LogMessage("Processing of FontDescription started.");
if (input == null)
{
throw new ArgumentNullException("input");
@ -104,20 +105,21 @@ namespace ANX.Framework.Content.Pipeline.Processors
spriteFontContent.Texture = ConvertBitmap(bitmap);
spriteFontContent.CharacterMap = input.Characters.ToList();
spriteFontContent.DefaultCharacter = input.DefaultCharacter;
spriteFontContent.Glyphs = CreateOutputGlyphs();
for (int i = 0; i < input.Characters.Count; i++)
spriteFontContent.Glyphs = spriteFontContent.Cropping;
spriteFontContent.Kerning = new List<Vector3>();
for (var i = 0; i < input.Characters.Count; i++)
{
Vector3 value = spriteFontContent.Kerning[i];
if (!input.UseKerning)
var value = Vector3.Zero;
if (input.UseKerning)
{
}
else
{
value.Y = spriteFontContent.Cropping[i].Width;
}
if (!input.UseKerning)
{
value.X = 0f;
value.Z = 0f;
}
spriteFontContent.Kerning[i] = value;
spriteFontContent.Kerning.Add(value);
}
spriteFontContent.LineSpacing = (int) Math.Ceiling(font.GetHeight());
spriteFontContent.Spacing = input.Spacing;
@ -129,7 +131,7 @@ namespace ANX.Framework.Content.Pipeline.Processors
foreach (Bitmap bitmap in bitmaps)
bitmap.Dispose();
}
context.Logger.LogMessage("Processing of FontDescription finished.");
return spriteFontContent;
}
@ -241,9 +243,9 @@ namespace ANX.Framework.Content.Pipeline.Processors
var bitmapContent = new PixelBitmapContent<Color>(bitmap.Width, bitmap.Height);
var destColor = new Color();
for (int x = bitmap.Width; x > 0; x--)
for (int x = 0; x < bitmap.Width; x++)
{
for (int y = bitmap.Height; y > 0; y--)
for (int y = 0; y < bitmap.Height; y++)
{
System.Drawing.Color sourceColor = bitmap.GetPixel(x, y);

View File

@ -29,7 +29,7 @@ namespace ANX.Framework.Content.Pipeline.Processors
internal float Spacing { get; set; }
[ContentSerializer(ElementName = "Kerning", AllowNull = false)]
internal List<Vector3> Kerning { get; private set; }
internal List<Vector3> Kerning { get; set; }
[ContentSerializer(ElementName = "DefaultCharacter", AllowNull = true)]
internal char? DefaultCharacter { get; set; }

View File

@ -1,5 +1,7 @@
#region Using Statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
#endregion
@ -12,53 +14,116 @@ namespace ANX.Framework.Content.Pipeline.Serialization.Compiler
{
public abstract class ContentTypeWriter
{
private bool canDeserializeIntoExistingObject;
private Type targetType;
private List<ContentTypeWriter> _genericArgumentWriters;
internal readonly bool TargetIsValueType;
#region CFPlatformDesc
private static readonly NetCfPlatformDescription[] NetCfDescs = new[]
{
new NetCfPlatformDescription(TargetPlatform.XBox360, new byte[]
{
132,
44,
248,
190,
29,
229,
5,
83
}, new string[]
{
"mscorlib",
"System",
"System.Xml"
}, new byte[]
{
28,
158,
37,
150,
134,
249,
33,
224
}, new Version(3, 7, 0, 0)),
new NetCfPlatformDescription(TargetPlatform.WindowsPhone, new byte[]
{
132,
44,
248,
190,
29,
229,
5,
83
}, new string[]
{
"mscorlib",
"System",
"System.Xml"
}, new byte[]
{
150,
157,
184,
5,
61,
51,
34,
172
}, new Version(3, 7, 0, 0))
};
#endregion
#region PublicKeyToken
private static readonly byte[] WindowsPublicKeyToken = new byte[]
{
132,
44,
248,
190,
29,
229,
5,
83
};
#endregion
protected ContentTypeWriter(Type targetType)
{
this.targetType = targetType;
TargetType = targetType;
TargetIsValueType = targetType.IsValueType;
}
internal static string GetStrongTypeName(Type type, TargetPlatform targetPlatform)
{
string text = ContentTypeWriter.GetTypeName(type);
string text = GetTypeName(type);
if (!string.IsNullOrEmpty(type.Namespace))
{
text = type.Namespace + '.' + text;
}
return text + ", " + ContentTypeWriter.GetAssemblyFullName(type.Assembly, targetPlatform);
return text + ", " + GetAssemblyFullName(type.Assembly, targetPlatform);
}
internal static string GetAssemblyFullName(Assembly assembly, TargetPlatform targetPlatform)
{
AssemblyName assemblyName = assembly.GetName();
//ContentTypeWriter.NetCFPlatformDescription[] netCFDescs = ContentTypeWriter.NetCFDescs;
//for (int i = 0; i < netCFDescs.Length; i++)
//{
// ContentTypeWriter.NetCFPlatformDescription netCFPlatformDescription = netCFDescs[i];
// if (netCFPlatformDescription.TargetPlatform == targetPlatform)
// {
// assemblyName = (AssemblyName)assemblyName.Clone();
// if (ContentTypeWriter.KeysAreEqual(assemblyName.GetPublicKeyToken(), ContentTypeWriter.WindowsPublicKeyToken))
// {
// assemblyName.SetPublicKeyToken(netCFPlatformDescription.PublicKeyToken);
// break;
// }
// string[] netCFAssemblies = netCFPlatformDescription.NetCFAssemblies;
// for (int j = 0; j < netCFAssemblies.Length; j++)
// {
// string value = netCFAssemblies[j];
// if (assemblyName.Name.Equals(value, StringComparison.InvariantCulture))
// {
// assemblyName.Version = netCFPlatformDescription.NetCFAssemblyVersion;
// assemblyName.SetPublicKeyToken(netCFPlatformDescription.NetCFPublicKeyToken);
// break;
// }
// }
// }
//}
NetCfPlatformDescription[] netCfDescs = NetCfDescs;
foreach (var netCfPlatformDescription in netCfDescs.Where(netCfPlatformDescription => netCfPlatformDescription.TargetPlatform == targetPlatform))
{
assemblyName = (AssemblyName)assemblyName.Clone();
if (ContentTypeWriter.KeysAreEqual(assemblyName.GetPublicKeyToken(), WindowsPublicKeyToken))
{
assemblyName.SetPublicKeyToken(netCfPlatformDescription.PublicKeyToken);
break;
}
var netCfAssemblies = netCfPlatformDescription.NetCfAssemblies;
if (netCfAssemblies.Any(value => assemblyName.Name.Equals(value, StringComparison.InvariantCulture)))
{
assemblyName.Version = netCfPlatformDescription.NetCfAssemblyVersion;
assemblyName.SetPublicKeyToken(netCfPlatformDescription.NetCfPublicKeyToken);
}
}
return assemblyName.FullName;
}
@ -68,50 +133,86 @@ namespace ANX.Framework.Content.Pipeline.Serialization.Compiler
Type declaringType = type.DeclaringType;
if (declaringType != null)
{
text = ContentTypeWriter.GetTypeName(declaringType) + '+' + text;
text = GetTypeName(declaringType) + '+' + text;
}
return text;
}
private static bool KeysAreEqual(byte[] a, byte[] b)
{
if (a.Length != b.Length)
{
return false;
}
for (int i = 0; i < a.Length; i++)
{
if (a[i] != b[i])
{
return false;
}
}
return true;
}
internal string GetGenericArgumentRuntimeTypes(TargetPlatform targetPlatform)
{
//TODO: implement
System.Diagnostics.Debugger.Break();
return "";
//System.Diagnostics.Debugger.Break();
//return "";
//if (this.genericArgumentWriters == null)
//{
// return string.Empty;
//}
//string text = string.Empty;
//for (int i = 0; i < this.genericArgumentWriters.Count; i++)
//{
// if (i > 0)
// {
// text += ',';
// }
// object obj = text;
// text = string.Concat(new object[]
// {
// obj,
// '[',
// this.genericArgumentWriters[i].GetRuntimeType(targetPlatform),
// ']'
// });
//}
//return '[' + text + ']';
if (_genericArgumentWriters == null)
{
return string.Empty;
}
var text = string.Empty;
for (var i = 0; i < _genericArgumentWriters.Count; i++)
{
if (i > 0)
{
text += ',';
}
object obj = text;
text = string.Concat(new[]
{
obj,
'[',
_genericArgumentWriters[i].GetRuntimeType(targetPlatform),
']'
});
}
return '[' + text + ']';
}
internal void DoInitialize(ContentCompiler compiler)
{
Initialize(compiler);
if (TargetType.IsGenericType)
{
_genericArgumentWriters = new List<ContentTypeWriter>();
Type[] genericArguments = TargetType.GetGenericArguments();
foreach (var type in genericArguments)
{
_genericArgumentWriters.Add(compiler.GetTypeWriter(type));
}
}
}
public abstract string GetRuntimeReader(TargetPlatform targetPlatform);
public virtual string GetRuntimeType(TargetPlatform targetPlatform)
{
throw new NotImplementedException();
var text = GetTypeName(TargetType);
if (!string.IsNullOrEmpty(TargetType.Namespace))
{
text = TargetType.Namespace + '.' + text;
}
text += GetGenericArgumentRuntimeTypes(targetPlatform);
return text + ", " + GetAssemblyFullName(TargetType.Assembly, targetPlatform);
}
protected virtual void Initialize(ContentCompiler compiler)
{
throw new NotImplementedException();
}
protected internal virtual bool ShouldCompressContent(TargetPlatform targetPlatform, Object value)
@ -121,20 +222,16 @@ namespace ANX.Framework.Content.Pipeline.Serialization.Compiler
protected internal abstract void Write(ContentWriter output, Object value);
public virtual bool CanDeserializeIntoExistingObject
public virtual bool CanDeserializeIntoExistingObject
{
get
{
return canDeserializeIntoExistingObject;
}
get;
set;
}
public Type TargetType
{
get
{
return this.targetType;
}
get;
set;
}
public virtual int TypeVersion
@ -144,5 +241,22 @@ namespace ANX.Framework.Content.Pipeline.Serialization.Compiler
return 0;
}
}
internal class NetCfPlatformDescription
{
public TargetPlatform TargetPlatform;
public byte[] PublicKeyToken;
public string[] NetCfAssemblies;
public byte[] NetCfPublicKeyToken;
public Version NetCfAssemblyVersion;
public NetCfPlatformDescription(TargetPlatform targetPlatform, byte[] publicKeyToken, string[] netCfAssemblies, byte[] netCfPublicKeyToken, Version netCfAssemblyVersion)
{
TargetPlatform = targetPlatform;
PublicKeyToken = publicKeyToken;
NetCfAssemblies = netCfAssemblies;
NetCfPublicKeyToken = netCfPublicKeyToken;
NetCfAssemblyVersion = netCfAssemblyVersion;
}
}
}
}