コード例 #1
0
        protected override BitmapFont Read(ContentReader reader, BitmapFont existingInstance)
        {
            var textureAssetCount = reader.ReadInt32();
            var assets            = new List <string>();

            for (var i = 0; i < textureAssetCount; i++)
            {
                var assetName = reader.ReadString();
                assets.Add(assetName);
            }

            var textures = assets
                           .Select(textureName => reader.ContentManager.Load <Texture2D>(reader.GetRelativeAssetName(textureName)))
                           .ToArray();

            var lineHeight  = reader.ReadInt32();
            var regionCount = reader.ReadInt32();
            var regions     = new BitmapFontRegion[regionCount];

            for (var r = 0; r < regionCount; r++)
            {
                var character     = reader.ReadInt32();
                var textureIndex  = reader.ReadInt32();
                var x             = reader.ReadInt32();
                var y             = reader.ReadInt32();
                var width         = reader.ReadInt32();
                var height        = reader.ReadInt32();
                var xOffset       = reader.ReadInt32();
                var yOffset       = reader.ReadInt32();
                var xAdvance      = reader.ReadInt32();
                var textureRegion = new TextureRegion2D(textures[textureIndex], x, y, width, height);
                regions[r] = new BitmapFontRegion(textureRegion, character, xOffset, yOffset, xAdvance);
            }

            var characterMap  = regions.ToDictionary(r => r.Character);
            var kerningsCount = reader.ReadInt32();

            for (var k = 0; k < kerningsCount; k++)
            {
                var first  = reader.ReadInt32();
                var second = reader.ReadInt32();
                var amount = reader.ReadInt32();

                // Find region
                BitmapFontRegion region;

                if (!characterMap.TryGetValue(first, out region))
                {
                    continue;
                }

                region.Kernings[second] = amount;
            }

            return(new BitmapFont(reader.AssetName, regions, lineHeight));
        }
コード例 #2
0
        public SizeF GetGlyphs(ICharIterator iterator, ICollection <Glyph> output)
        {
            if (iterator.Length <= 0)
            {
                return(SizeF.Empty);
            }

            float   largestW      = 0;
            Vector2 positionDelta = new Vector2(0, 0);
            Glyph   previousGlyph = new Glyph();

            for (int i = 0; i < iterator.Length; i++)
            {
                int character           = iterator.GetCharacter32(ref i);
                BitmapFontRegion region = GetCharacterRegion(character);
                Vector2          newPos = positionDelta;

                if (region != null)
                {
                    newPos.X        += region.XOffset;
                    newPos.Y        += region.YOffset;
                    positionDelta.X += region.XAdvance + LetterSpacing;
                }

                if (UseKernings && previousGlyph.FontRegion != null)
                {
                    if (previousGlyph.FontRegion.Kernings.TryGetValue(character, out int amount))
                    {
                        positionDelta.X += amount;
                    }
                }

                // use previousGlyph to store the new Glyph
                previousGlyph = new Glyph(character, newPos, region);
                output.Add(previousGlyph);

                if (positionDelta.X > largestW)
                {
                    largestW = positionDelta.X;
                }

                if (character == '\n')
                {
                    positionDelta.Y += LineHeight;
                    positionDelta.X  = 0;
                    previousGlyph    = default;
                }
            }

            return(new SizeF(largestW, positionDelta.Y + LineHeight));
        }
コード例 #3
0
        protected override BitmapFont Read(ContentReader reader, BitmapFont existingInstance)
        {
            int textureAssetCount = reader.ReadInt32();

            Texture2D[] textures = new Texture2D[textureAssetCount];

            for (var i = 0; i < textureAssetCount; i++)
            {
                string assetName = reader.GetRelativeAssetName(reader.ReadString());
                textures[i] = reader.ContentManager.Load <Texture2D>(assetName);
            }

            int lineHeight   = reader.ReadInt32();
            int regionCount  = reader.ReadInt32();
            var characterMap = new Dictionary <int, BitmapFontRegion>(regionCount);

            for (int r = 0; r < regionCount; r++)
            {
                int character     = reader.ReadInt32();
                int textureIndex  = reader.ReadInt32();
                int x             = reader.ReadInt32();
                int y             = reader.ReadInt32();
                int width         = reader.ReadInt32();
                int height        = reader.ReadInt32();
                int xOffset       = reader.ReadInt32();
                int yOffset       = reader.ReadInt32();
                int xAdvance      = reader.ReadInt32();
                var textureRegion = new TextureRegion2D(textures[textureIndex], x, y, width, height);

                var bmpReg = new BitmapFontRegion(textureRegion, character, xOffset, yOffset, xAdvance);
                characterMap.Add(character, bmpReg);
            }

            int kerningsCount = reader.ReadInt32();

            for (int k = 0; k < kerningsCount; k++)
            {
                int first  = reader.ReadInt32();
                int second = reader.ReadInt32();
                int amount = reader.ReadInt32();

                if (characterMap.TryGetValue(first, out BitmapFontRegion region))
                {
                    region.Kernings.Add(second, amount);
                }
            }

            return(new BitmapFont(reader.AssetName, characterMap, lineHeight));
        }
コード例 #4
0
        private static Dictionary<char, BitmapFontRegion> BuildCharacterMap(Texture2D[] textures, BitmapFontFile fontFile)
        {
            var characterMap = new Dictionary<char, BitmapFontRegion>();

            foreach (var fontChar in fontFile.Chars)
            {
                var pageIndex = fontChar.Page;
                var character = (char)fontChar.ID;
                var texture = textures[pageIndex];
                var region = new TextureRegion2D(texture, fontChar.X, fontChar.Y, fontChar.Width, fontChar.Height);
                var fontRegion = new BitmapFontRegion(region, fontChar);
                characterMap.Add(character, fontRegion);
            }

            return characterMap;
        }
コード例 #5
0
        private static Dictionary <char, BitmapFontRegion> BuildCharacterMap(Texture2D[] textures, BitmapFontFile fontFile)
        {
            var characterMap = new Dictionary <char, BitmapFontRegion>();

            foreach (var fontChar in fontFile.Chars)
            {
                var pageIndex  = fontChar.Page;
                var character  = (char)fontChar.ID;
                var texture    = textures[pageIndex];
                var region     = new TextureRegion2D(texture, fontChar.X, fontChar.Y, fontChar.Width, fontChar.Height);
                var fontRegion = new BitmapFontRegion(region, fontChar);
                characterMap.Add(character, fontRegion);
            }

            return(characterMap);
        }
コード例 #6
0
        public bool MoveNext()
        {
            _index++;
            if (_index >= _iterator.Length)
            {
                return(false);
            }

            int character           = _iterator.GetCharacter32(ref _index);
            BitmapFontRegion region = _font.GetCharacterRegion(character);
            Vector2          newPos = _position + _positionDelta;

            if (region != null)
            {
                newPos.X         += region.XOffset;
                newPos.Y         += region.YOffset;
                _positionDelta.X += region.XAdvance + _font.LetterSpacing;
            }

            if (_hasPreviousGlyph)
            {
                if (BitmapFont.UseKernings && _previousGlyph.FontRegion != null)
                {
                    if (_previousGlyph.FontRegion.Kernings.TryGetValue(character, out int amount))
                    {
                        _positionDelta.X += amount;
                    }
                }
            }

            CurrentGlyph      = new Glyph(character, newPos, region);
            _previousGlyph    = CurrentGlyph;
            _hasPreviousGlyph = true;

            if (character == '\n')
            {
                _positionDelta.Y += _font.LineHeight;
                _positionDelta.X  = 0;
                _hasPreviousGlyph = false;
            }
            return(true);
        }
コード例 #7
0
        protected override BitmapFont Read(ContentReader reader, BitmapFont existingInstance)
        {
            var relativeAssetFolder = GetRelativeAssetFolder(reader.AssetName);
            var textureAssetCount   = reader.ReadInt32();
            var assets = new List <string>();

            for (var i = 0; i < textureAssetCount; i++)
            {
                var assetName = reader.ReadString();
                assets.Add(assetName);
            }

            var textures = assets
                           .Select(assetName => reader.ContentManager.Load <Texture2D>(relativeAssetFolder + assetName))
                           .ToArray();

            var lineHeight  = reader.ReadInt32();
            var regionCount = reader.ReadInt32();
            var regions     = new BitmapFontRegion[regionCount];

            for (var r = 0; r < regionCount; r++)
            {
                var character     = (char)reader.ReadInt32();
                var textureIndex  = reader.ReadInt32();
                var x             = reader.ReadInt32();
                var y             = reader.ReadInt32();
                var width         = reader.ReadInt32();
                var height        = reader.ReadInt32();
                var xOffset       = reader.ReadInt32();
                var yOffset       = reader.ReadInt32();
                var xAdvance      = reader.ReadInt32();
                var textureRegion = new TextureRegion2D(textures[textureIndex], x, y, width, height);
                regions[r] = new BitmapFontRegion(textureRegion, character, xOffset, yOffset, xAdvance);
            }

            return(new BitmapFont(regions, lineHeight));
        }
コード例 #8
0
 public Glyph(int character, Vector2 position, BitmapFontRegion fontRegion)
 {
     Character  = character;
     Position   = position;
     FontRegion = fontRegion;
 }
コード例 #9
0
 public bool GetCharacterRegion(int character, out BitmapFontRegion region)
 {
     return(_characterMap.TryGetValue(character, out region));
 }