コード例 #1
0
        public void Run(string[] args)
        {
            // Print the exe header
            PrintHeader();

            // Parse the command line
            if (!ParseCommandLine(args))
            {
                Environment.Exit(-1);
            }

            // Loads the description
            var filePath = Path.Combine(Environment.CurrentDirectory, XmlFontFile);

            var fontDescription = FontDescription.Load(XmlFontFile);

            var defaultOutputFile = Path.Combine(Path.GetDirectoryName(filePath), Path.GetFileNameWithoutExtension(filePath));

            // Compiles to SpriteData
            OutputFile = OutputFile ?? defaultOutputFile;

            string dependencyFile = null;

            if (CompileOnlyIfNewer)
            {
                dependencyFile = Path.Combine(OutputDependencyDirectory, FileDependencyList.GetDependencyFileNameFromSourcePath(Path.GetFileName(filePath)));
            }

            bool forceCompilation = (DebugOutputSpriteSheet != null && !File.Exists(DebugOutputSpriteSheet));

            var result = FontCompiler.CompileAndSave(filePath, OutputFile, dependencyFile);

            if (result.IsContentGenerated || forceCompilation)
            {
                Console.WriteLine("Writing [{0}] ({1} format)", OutputFile, fontDescription.Format);

                // Save output files.
                if (!string.IsNullOrEmpty(DebugOutputSpriteSheet))
                {
                    Console.WriteLine("Saving debug output spritesheet {0}", DebugOutputSpriteSheet);

                    var spriteFontData = SpriteFontData.Load(OutputFile);

                    if (spriteFontData.Bitmaps.Length > 0 && spriteFontData.Bitmaps[0].Data is SpriteFontData.BitmapData)
                    {
                        var bitmapData = (SpriteFontData.BitmapData)spriteFontData.Bitmaps[0].Data;
                        using (var image = Image.New2D(bitmapData.Width, bitmapData.Height, 1, bitmapData.PixelFormat))
                        {
                            Utilities.Write(image.DataPointer, bitmapData.Data, 0, bitmapData.Data.Length);
                            image.Save(DebugOutputSpriteSheet, ImageFileType.Dds);
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("No need to write [{0}]. File is up-to-date from XML description", OutputFile);
            }
        }
コード例 #2
0
        /// <summary>
        /// Loads an <see cref="EffectData"/> from the specified stream.
        /// </summary>
        /// <param name="device">The graphics device</param>
        /// <param name="stream">The stream.</param>
        /// <param name="bitmapDataLoader">A delegate to load bitmap data that are not stored in the buffer.</param>
        /// <returns>An <see cref="EffectData"/>. Null if the stream is not a serialized <see cref="EffectData"/>.</returns>
        /// <remarks>
        /// </remarks>
        public static SpriteFont Load(GraphicsDevice device, Stream stream, SpriteFontBitmapDataLoaderDelegate bitmapDataLoader = null)
        {
            var spriteFontData = SpriteFontData.Load(stream, bitmapDataLoader);

            if (spriteFontData == null)
            {
                return(null);
            }
            return(New(device, spriteFontData));
        }
コード例 #3
0
        /// <summary>
        /// Compiles the specified font description into a <see cref="SpriteFontData"/> object.
        /// </summary>
        /// <param name="fontDescription">The font description.</param>
        /// <returns>A SpriteFontData object.</returns>
        public static SpriteFontData Compile(FontDescription fontDescription)
        {
            // We are using a MemoryStream, this is not efficient
            // but this was a quickiest way to use existing from MakeSpriteFont from DirectXTk
            var stream = new MemoryStream();

            MakeSpriteFont(fontDescription, stream);
            stream.Position = 0;
            return(SpriteFontData.Load(stream));
        }
コード例 #4
0
        /// <summary>
        /// Loads an <see cref="EffectData"/> from the specified stream.
        /// </summary>
        /// <param name="device">The graphics device</param>
        /// <param name="stream">The stream.</param>
        /// <param name="bitmapDataLoader">A delegate to load bitmap data that are not stored in the buffer.</param>
        /// <returns>An <see cref="EffectData"/>. Null if the stream is not a serialized <see cref="EffectData"/>.</returns>
        /// <remarks>
        /// </remarks>
        public static SpriteFont Load(GraphicsDevice device, Stream stream, SpriteFontBitmapDataLoaderDelegate bitmapDataLoader = null)
        {
            var spriteFontData = SpriteFontData.Load(stream, bitmapDataLoader);

            if (spriteFontData == null)
            {
                return(null);
            }
            // the SpriteFontData is loaded here and will be used only in the current SpriteFont instance
            // therefore it is safe to dispose it's Texture2D instances, if any.
            return(New(device, spriteFontData, true));
        }
コード例 #5
0
        protected override SpriteFont ReadContent(IContentManager readerManager, GraphicsDevice device, ref ContentReaderParameters parameters)
        {
            SpriteFont spriteFont = null;
            var        assetPath  = Path.GetDirectoryName(parameters.AssetName);

            // Load the sprite font data
            var spriteFontData = SpriteFontData.Load(parameters.Stream, name => readerManager.Load <Texture2D>(Path.Combine(assetPath ?? string.Empty, name)));

            // If sprite font was fine, then instantiate SpriteFont graphics object.
            if (spriteFontData != null)
            {
                spriteFont = SpriteFont.New(device, spriteFontData);
            }

            return(spriteFont);
        }
コード例 #6
0
        /// <summary>
        /// Loads a <see cref="SpriteFontData"/> from the specified stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="bitmapDataLoader">A delegate to load bitmap data that are not stored in the buffer.</param>
        /// <returns>An <see cref="SpriteFontData"/>. Null if the stream is not a serialized <see cref="SpriteFontData"/>.</returns>
        public static SpriteFontData Load(Stream stream, SpriteFontBitmapDataLoaderDelegate bitmapDataLoader = null)
        {
            var serializer = new BinarySerializer(stream, SerializerMode.Read, Text.Encoding.ASCII)
            {
                ArrayLengthType = ArrayLengthType.Int
            };

            var data = new SpriteFontData();

            var magicCode = FourCC.Empty;

            serializer.Serialize(ref magicCode);

            if (magicCode == "DXTK")
            {
                data.SerializeMakeSpriteFont(serializer);
            }
            else if (magicCode == new FourCC(0x03464D42)) // BMF\3
            {
                data.SerializeBMFFont(serializer);
            }
            else
            {
                return(null);
            }

            // Glyphs are null, then this is not a SpriteFondData
            if (data.Glyphs == null)
            {
                return(null);
            }

            if (bitmapDataLoader != null)
            {
                foreach (var bitmap in data.Bitmaps)
                {
                    // If the bitmap data is a string, then this is a texture to load
                    if (bitmap.Data is string)
                    {
                        bitmap.Data = bitmapDataLoader((string)bitmap.Data);
                    }
                }
            }

            return(data);
        }
コード例 #7
0
ファイル: SpriteFontData.cs プロジェクト: GrafSeismo/SharpDX
        /// <summary>
        /// Loads a <see cref="SpriteFontData"/> from the specified stream.
        /// </summary>
        /// <param name="stream">The stream.</param>
        /// <param name="bitmapDataLoader">A delegate to load bitmap data that are not stored in the buffer.</param>
        /// <returns>An <see cref="SpriteFontData"/>. Null if the stream is not a serialized <see cref="SpriteFontData"/>.</returns>
        public static SpriteFontData Load(Stream stream, SpriteFontBitmapDataLoaderDelegate bitmapDataLoader = null)
        {
            var serializer = new BinarySerializer(stream, SerializerMode.Read, Encoding.ASCII) {ArrayLengthType = ArrayLengthType.Int};

            var data = new SpriteFontData();

            var magicCode = FourCC.Empty;
            serializer.Serialize(ref magicCode);

            if (magicCode == "DXTK")
            {
                data.SerializeMakeSpriteFont(serializer);
            }
            else if (magicCode == new FourCC(0x03464D42)) // BMF\3
            {
                data.SerializeBMFFont(serializer);
            }
            else
            {
                return null;
            }

            // Glyphs are null, then this is not a SpriteFondData
            if (data.Glyphs == null)
                return null;

            if (bitmapDataLoader != null)
            {
                foreach (var bitmap in data.Bitmaps)
                {
                    // If the bitmap data is a string, then this is a texture to load
                    if (bitmap.Data is string)
                    {
                        bitmap.Data = bitmapDataLoader((string) bitmap.Data);
                    }
                }
            }

            return data;
        }
コード例 #8
0
        internal SpriteFont(GraphicsDevice device, SpriteFontData spriteFontData, bool disposeSpriteFontDataResources)
            : base(device)
        {
            drawGlyphDelegate    = InternalDrawGlyph;
            measureGlyphDelegate = MeasureStringGlyph;

            // Read the glyph data.
            globalBaseOffsetY = spriteFontData.BaseOffset;
            glyphs            = spriteFontData.Glyphs;
            characterMap      = new Dictionary <char, int>(glyphs.Length * 2);

            // Prebuild the character map
            var characterList = new List <char>(glyphs.Length);

            for (int i = 0; i < glyphs.Length; i++)
            {
                var charItem = (char)glyphs[i].Character;
                characterMap.Add(charItem, i);
                characterList.Add(charItem);
            }

            // Prepare kernings if they are available.
            var kernings = spriteFontData.Kernings;

            if (kernings != null)
            {
                kerningMap = new Dictionary <int, float>(spriteFontData.Kernings.Length);
                for (int i = 0; i < kernings.Length; i++)
                {
                    int key = (kernings[i].First << 16) | kernings[i].Second;
                    kerningMap.Add(key, kernings[i].Offset);
                }
            }

            Characters = new ReadOnlyCollection <char>(characterList);

            // Read font properties.
            LineSpacing = spriteFontData.LineSpacing;

            if (spriteFontData.DefaultCharacter > 0)
            {
                DefaultCharacter = (char)spriteFontData.DefaultCharacter;
                if (!characterMap.TryGetValue(DefaultCharacter.Value, out defaultGlyphIndex))
                {
                    defaultGlyphIndex = -1;
                }
            }

            // Read the texture data.
            textures = new Texture2D[spriteFontData.Bitmaps.Length];
            for (int i = 0; i < textures.Length; i++)
            {
                var bitmap = spriteFontData.Bitmaps[i];
                if (bitmap.Data is SpriteFontData.BitmapData)
                {
                    var image = (SpriteFontData.BitmapData)bitmap.Data;
                    textures[i] = ToDispose(Texture2D.New(device, image.Width, image.Height, image.PixelFormat, image.Data));
                }
                else if (bitmap.Data is Texture2D)
                {
                    textures[i] = (Texture2D)bitmap.Data;
                    if (disposeSpriteFontDataResources)
                    {
                        ToDispose(textures[i]);
                    }
                }
                else
                {
                    throw new NotSupportedException(string.Format("SpriteFontData.Bitmap of type [{0}] is not supported. Only SpriteFontData.BitmapData or Texture2D", bitmap == null ? "null" : bitmap.GetType().Name));
                }
            }
        }
コード例 #9
0
 /// <summary>
 /// Creates a new instance of the <see cref="SpriteFont"/> class from the specified <see cref="SpriteFontData"/>.
 /// </summary>
 /// <param name="device">The graphics device which will manage graphics resources of the created instance.</param>
 /// <param name="spriteFontData">The font data to load from.</param>
 /// <param name="disposeSpriteFontDataResources">true - if disposal of the created instance should dispose the unmanaged resources from the <paramref name="spriteFontData"/>,
 ///  false - if the unmanaged resources should be disposed manually, default is false.</param>
 /// <returns>The loaded <see cref="SpriteFont"/> instance.</returns>
 public static SpriteFont New(GraphicsDevice device, SpriteFontData spriteFontData, bool disposeSpriteFontDataResources = false)
 {
     return(new SpriteFont(device, spriteFontData, disposeSpriteFontDataResources));
 }
コード例 #10
0
 public static SpriteFont New(GraphicsDevice device, SpriteFontData spriteFontData)
 {
     return(new SpriteFont(device, spriteFontData));
 }
コード例 #11
0
ファイル: SpriteFont.cs プロジェクト: s33m3/SharpDX
        internal SpriteFont(GraphicsDevice device, SpriteFontData spriteFontData)
            : base(device)
        {
            // Read the glyph data.
            globalBaseOffsetY = spriteFontData.BaseOffset;
            glyphs = spriteFontData.Glyphs;
            characterMap = new Dictionary<char, int>(glyphs.Length * 2);

            // Prebuild the character map
            var characterList = new List<char>(glyphs.Length);
            for (int i = 0; i < glyphs.Length; i++)
            {
                var charItem = (char)glyphs[i].Character;
                characterMap.Add(charItem, i);
                characterList.Add(charItem);
            }

            // Prepare kernings if they are available.
            var kernings = spriteFontData.Kernings;
            if (kernings != null)
            {
                kerningMap = new Dictionary<int, float>(spriteFontData.Kernings.Length);
                for (int i = 0; i < kernings.Length; i++)
                {
                    int key = (kernings[i].First << 16) | kernings[i].Second;
                    kerningMap.Add(key, kernings[i].Offset);
                }
            }

            Characters = new ReadOnlyCollection<char>(characterList);

            // Read font properties.
            LineSpacing = spriteFontData.LineSpacing;

            if (spriteFontData.DefaultCharacter > 0)
            {
                DefaultCharacter = (char)spriteFontData.DefaultCharacter;
                if (!characterMap.TryGetValue(DefaultCharacter.Value, out defaultGlyphIndex))
                {
                    defaultGlyphIndex = -1;
                }
            }

            // Read the texture data.
            textures = new Texture2D[spriteFontData.Bitmaps.Length];
            for(int i = 0; i < textures.Length; i++)
            {
                var bitmap = spriteFontData.Bitmaps[i];
                if (bitmap.Data is SpriteFontData.BitmapData)
                {
                    var image = (SpriteFontData.BitmapData) bitmap.Data;
                    textures[i] = ToDispose(Texture2D.New(device, image.Width, image.Height, image.PixelFormat, image.Data));
                }
                else if (bitmap.Data is Texture2D)
                {
                    textures[i] = (Texture2D) bitmap.Data;
                }
                else
                {
                    throw new NotSupportedException(string.Format("SpriteFontData.Bitmap of type [{0}] is not supported. Only SpriteFontData.BitmapData or Texture2D", bitmap == null ? "null" : bitmap.GetType().Name));
                }
            }
        }
コード例 #12
0
ファイル: SpriteFont.cs プロジェクト: s33m3/SharpDX
 public static SpriteFont New(GraphicsDevice device, SpriteFontData spriteFontData)
 {
     return new SpriteFont(device, spriteFontData);
 }
コード例 #13
0
 public object ReadContent(IContentManager readerManager, string assetName, Stream stream, out bool keepStreamOpen, object options)
 {
     keepStreamOpen = false;
     return(SpriteFontData.Load(stream));
 }
コード例 #14
0
 public object ReadContent(IContentManager readerManager, ref ContentReaderParameters parameters)
 {
     parameters.KeepStreamOpen = false;
     return(SpriteFontData.Load(parameters.Stream));
 }