Пример #1
0
        public void ReadTextKernelDuplicate()
        {
            const char first    = 'A';
            const char second   = 'B';
            const int  expected = 1;

            using (var memoryStream = new MemoryStream())
            {
                var kerningPair = new KerningPair(first, second);

                using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8, 1024, true))
                {
                    streamWriter.Write("kernings");
                    TextFormatUtility.WriteInt("count", 2, streamWriter);
                    streamWriter.WriteLine();

                    for (var i = 0; i < 2; i++)
                    {
                        streamWriter.Write("kerning");
                        kerningPair.WriteText(streamWriter, expected + i);
                        streamWriter.WriteLine();
                    }
                }

                memoryStream.Seek(0, SeekOrigin.Begin);

                var result = BitmapFont.FromStream(memoryStream, FormatHint.Text, true);
                Assert.AreEqual(expected, result.GetKerningAmount(first, second));
            }
        }
Пример #2
0
        public Task LoadFontAsync() => fontLoadTask ??= Task.Factory.StartNew(() =>
        {
            try
            {
                BitmapFont font;

                using (var s = Store.GetStream($@"{AssetName}"))
                {
                    string hash = s.ComputeMD5Hash();

                    if (font_cache.TryGetValue(hash, out font))
                    {
                        Logger.Log($"Cached font load for {AssetName}");
                    }
                    else
                    {
                        font_cache.TryAdd(hash, font = BitmapFont.FromStream(s, FormatHint.Binary, false));
                    }
                }

                completionSource.SetResult(font);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, $"Couldn't load font asset from {AssetName}.");
                completionSource.SetResult(null);
                throw;
            }
        }, TaskCreationOptions.PreferFairness);
Пример #3
0
        public void ReadXMLMissingRoot()
        {
            var settings = new XmlWriterSettings
            {
                Indent      = true,
                IndentChars = "  ",
                CloseOutput = false
            };

            using (var memoryStream = new MemoryStream())
            {
                using (var xmlWriter = XmlWriter.Create(memoryStream, settings))
                {
                    var document = new XDocument();

                    var fontElement = new XElement("nothing");
                    document.Add(fontElement);

                    document.WriteTo(xmlWriter);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);

                BitmapFont.FromStream(memoryStream, FormatHint.XML, true);
            }
        }
Пример #4
0
        public void XMLCharsetNull()
        {
            using (var memoryStream = new MemoryStream())
            {
                var bitmapFont = new BitmapFont {
                    Info = new BitmapFontInfo {
                        Charset = null
                    }
                };

                var settings = new XmlWriterSettings
                {
                    Indent      = true,
                    IndentChars = "  ",
                    CloseOutput = false,
                    Encoding    = Encoding.UTF8
                };

                using (var xmlWriter = XmlWriter.Create(memoryStream, settings))
                {
                    bitmapFont.WriteXML(xmlWriter);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);

                var result = BitmapFont.FromStream(memoryStream, FormatHint.XML, true);
                Assert.AreEqual(string.Empty, result.Info.Charset);
            }
        }
Пример #5
0
        /// <summary>
        /// Create a sprite font.
        /// </summary>
        /// <param name="filePath">File path.</param>
        public SpriteFont(string filePath)
        {
            //Load the font.
            Font = BitmapFont.FromStream(FileSystem.OpenFileStream(filePath), false);

            //Load textures.
            Pages = new Dictionary <int, Texture>();
            string dirPath = Path.GetDirectoryName(filePath) + "/";

            if (dirPath.Equals("/"))
            {
                dirPath = "";
            }
            foreach (var p in Font.Pages)
            {
                Pages.Add(p.Key, new Texture(dirPath + p.Value));
            }

            //For each character.
            Bounds = new Dictionary <int, Rectangle>();
            foreach (var c in Font.Characters)
            {
                Bounds.Add(c.Key, new Rectangle(c.Value.X, c.Value.Y, c.Value.Width, c.Value.Height));
            }

            //Render target.
            RenderTarget = new RenderTarget2D(GameHelper.Graphics, 1, 1);
        }
Пример #6
0
        public void ReadBinaryKerningDuplicate()
        {
            const char first    = 'A';
            const char second   = 'B';
            const int  expected = 1;

            using (var memoryStream = new MemoryStream())
            {
                var kerningPair = new KerningPair(first, second);

                using (var binaryWriter = new BinaryWriter(memoryStream, Encoding.UTF8, true))
                {
                    binaryWriter.Write(BitmapFont.MagicOne);
                    binaryWriter.Write(BitmapFont.MagicTwo);
                    binaryWriter.Write(BitmapFont.MagicThree);
                    binaryWriter.Write((byte)BitmapFont.ImplementedVersion);
                    binaryWriter.Write((byte)BlockID.KerningPairs);
                    binaryWriter.Write(KerningPair.SizeInBytes * 2);
                    kerningPair.WriteBinary(binaryWriter, expected);
                    kerningPair.WriteBinary(binaryWriter, expected + 1);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);

                var result = BitmapFont.FromStream(memoryStream, FormatHint.Binary, true);
                Assert.AreEqual(expected, result.GetKerningAmount(first, second));
            }
        }
Пример #7
0
 public void ReadStreamLeaveOpenFalse()
 {
     using (var fileStream = File.Open("TestFontBinary.fnt", FileMode.Open))
     {
         BitmapFont.FromStream(fileStream, false);
         Assert.IsFalse(fileStream.CanRead);
     }
 }
Пример #8
0
        public void ReadBinaryWrongMagic()
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var binaryWriter = new BinaryWriter(memoryStream, Encoding.UTF8, true))
                {
                    binaryWriter.Write(BitmapFont.MagicOne - 1);
                    binaryWriter.Write(BitmapFont.MagicTwo + 1);
                    binaryWriter.Write(BitmapFont.MagicThree);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);

                BitmapFont.FromStream(memoryStream, FormatHint.Binary, true);
            }
        }
Пример #9
0
        public Task LoadFontAsync() => fontLoadTask ?? (fontLoadTask = Task.Factory.StartNew(() =>
        {
            try
            {
                BitmapFont font;
                using (var s = store.GetStream($@"{assetName}.bin"))
                    font = BitmapFont.FromStream(s, FormatHint.Binary, false);

                completionSource.SetResult(font);
            }
            catch (Exception ex)
            {
                Logger.Error(ex, $"Couldn't load font asset from {assetName}.");
                throw;
            }
        }, TaskCreationOptions.PreferFairness));
Пример #10
0
        public void ReadXMLKerningDuplicate()
        {
            const char first    = 'A';
            const char second   = 'B';
            const int  expected = 1;

            var settings = new XmlWriterSettings
            {
                Indent      = true,
                IndentChars = "  ",
                CloseOutput = false
            };

            using (var memoryStream = new MemoryStream())
            {
                var kerningPair = new KerningPair(first, second);

                using (var xmlWriter = XmlWriter.Create(memoryStream, settings))
                {
                    var document = new XDocument();

                    var fontElement = new XElement("font");
                    document.Add(fontElement);

                    var kerningsElement = new XElement("kernings");
                    kerningsElement.SetAttributeValue("count", 2);

                    for (var i = 0; i < 2; i++)
                    {
                        var kerningElement = new XElement("kerning");
                        kerningPair.WriteXML(kerningElement, expected + i);
                        kerningsElement.Add(kerningElement);
                    }

                    fontElement.Add(kerningsElement);

                    document.WriteTo(xmlWriter);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);

                var result = BitmapFont.FromStream(memoryStream, FormatHint.XML, true);
                Assert.AreEqual(expected, result.GetKerningAmount(first, second));
            }
        }
Пример #11
0
        public void ReadBinaryInvalidBlock()
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var binaryWriter = new BinaryWriter(memoryStream, Encoding.UTF8, true))
                {
                    binaryWriter.Write(BitmapFont.MagicOne);
                    binaryWriter.Write(BitmapFont.MagicTwo);
                    binaryWriter.Write(BitmapFont.MagicThree);
                    binaryWriter.Write((byte)BitmapFont.ImplementedVersion);
                    binaryWriter.Write((byte)6);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);

                BitmapFont.FromStream(memoryStream, FormatHint.Binary, true);
            }
        }
Пример #12
0
        public void ReadBinaryKerningWrongBlockSize()
        {
            using (var memoryStream = new MemoryStream())
            {
                using (var binaryWriter = new BinaryWriter(memoryStream, Encoding.UTF8, true))
                {
                    binaryWriter.Write(BitmapFont.MagicOne);
                    binaryWriter.Write(BitmapFont.MagicTwo);
                    binaryWriter.Write(BitmapFont.MagicThree);
                    binaryWriter.Write((byte)BitmapFont.ImplementedVersion);
                    binaryWriter.Write((byte)BlockID.KerningPairs);
                    binaryWriter.Write(KerningPair.SizeInBytes / 2);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);

                BitmapFont.FromStream(memoryStream, FormatHint.Binary, true);
            }
        }
Пример #13
0
        public void TextCharsetNull()
        {
            using (var memoryStream = new MemoryStream())
            {
                var bitmapFont = new BitmapFont {
                    Info = new BitmapFontInfo {
                        Charset = null
                    }
                };

                using (var streamWriter = new StreamWriter(memoryStream, Encoding.UTF8, 1024, true))
                {
                    bitmapFont.WriteText(streamWriter);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);

                var result = BitmapFont.FromStream(memoryStream, FormatHint.Text, true);
                Assert.AreEqual(string.Empty, result.Info.Charset);
            }
        }
Пример #14
0
        public void BinaryCharsetNull()
        {
            var bitmapFont = new BitmapFont {
                Info = new BitmapFontInfo {
                    Charset = null
                }
            };

            using (var memoryStream = new MemoryStream())
            {
                using (var binaryWriter = new BinaryWriter(memoryStream, Encoding.UTF8, true))
                {
                    bitmapFont.WriteBinary(binaryWriter);
                }

                memoryStream.Seek(0, SeekOrigin.Begin);

                var result = BitmapFont.FromStream(memoryStream, FormatHint.Binary, true);
                Assert.AreEqual(CharacterSet.ANSI.ToString(), result.Info.Charset);
            }
        }
Пример #15
0
        public void InitValues()
        {
            ImageWidth         = imageWidth;
            ImageHeight        = imageHeight;
            KeyLength          = keyLength ?? random.NextInt(5, 7);
            OverlayEnabled     = overlayEnabled;
            WavesFilterEnabled = wavesFilterEnabled;

            if (foregroundColor != Color.Empty)
            {
                ForegroundColor = foregroundColor;

                // if only foreground color configured
                if (backgroundColor == Color.Empty)
                {
                    BackgroundColor = GetDerivedColor(foregroundColor);
                }
                // else both colors specifed; do nothing
            }
            else if (backgroundColor != Color.Empty) // if only background is specifed
            {
                ForegroundColor = GetDerivedColor(backgroundColor);
                BackgroundColor = backgroundColor;
            }
            else
            {
                ForegroundColor = Color.FromArgb(
                    random.NextInt(0x20, 0x40),
                    random.NextInt(0x20, 0x40),
                    random.NextInt(0x20, 0x40));

                BackgroundColor = GetDerivedColor(ForegroundColor);
            }

            using (var stream = GetBitmapFontStream(fontPath))
            {
                BitmapFont = BitmapFont.FromStream(stream);
            }
        }
Пример #16
0
 public void FromStreamBadFormatHint()
 {
     BitmapFont.FromStream(null, (FormatHint)3, true);
 }