Ejemplo n.º 1
0
 private void MeasureString(ref SpriteFont.CharacterSource text, out Vector2 size)
 {
   if (text.Length == 0)
   {
     size = Vector2.Zero;
   }
   else
   {
     SpriteFont.Glyph? nullable = new SpriteFont.Glyph?();
     if (this.DefaultCharacter.HasValue)
       nullable = new SpriteFont.Glyph?(this._glyphs[this.DefaultCharacter.Value]);
     float num1 = 0.0f;
     float num2 = (float) this.LineSpacing;
     int num3 = 0;
     SpriteFont.Glyph glyph = SpriteFont.Glyph.Empty;
     Vector2 zero = Vector2.Zero;
     bool flag = false;
     for (int index = 0; index < text.Length; ++index)
     {
       char key = text[index];
       switch (key)
       {
         case '\r':
           flag = false;
           break;
         case '\n':
           ++num3;
           num2 = (float) this.LineSpacing;
           zero.X = 0.0f;
           zero.Y = (float) (this.LineSpacing * num3);
           flag = false;
           break;
         default:
           if (flag)
             zero.X += this.Spacing + glyph.WidthIncludingBearings;
           flag = this._glyphs.TryGetValue(key, out glyph);
           if (!flag)
           {
             if (!nullable.HasValue)
               throw new ArgumentException("Text contains characters that cannot be resolved by this SpriteFont.", "text");
             glyph = nullable.Value;
             flag = true;
           }
           float num4 = zero.X + glyph.WidthIncludingBearings;
           if ((double) num4 > (double) num1)
             num1 = num4;
           if ((double) glyph.Cropping.Height > (double) num2)
           {
             num2 = (float) glyph.Cropping.Height;
             break;
           }
           else
             break;
       }
     }
     size.X = num1;
     size.Y = (float) (num3 * this.LineSpacing) + num2;
   }
 }
Ejemplo n.º 2
0
        void measureString(ref FontCharacterSource text, out Vector2 size)
        {
            if (text.Length == 0)
            {
                size = Vector2.Zero;
                return;
            }

            // Get the default glyph here once.
            SpriteFont.Glyph?defaultGlyph = null;
            if (_font.DefaultCharacter.HasValue)
            {
                defaultGlyph = _glyphs[_font.DefaultCharacter.Value];
            }

            var width           = 0.0f;
            var finalLineHeight = (float)_font.LineSpacing;

            var currentGlyph     = SpriteFont.Glyph.Empty;
            var offset           = Vector2.Zero;
            var firstGlyphOfLine = true;

            for (var i = 0; i < text.Length; ++i)
            {
                var c = text[i];

                if (c == '\r')
                {
                    continue;
                }

                if (c == '\n')
                {
                    finalLineHeight = _font.LineSpacing;

                    offset.X         = 0;
                    offset.Y        += _font.LineSpacing;
                    firstGlyphOfLine = true;
                    continue;
                }

                if (!_glyphs.TryGetValue(c, out currentGlyph))
                {
                    if (!defaultGlyph.HasValue)
                    {
                        throw new ArgumentException("Errors.TextContainsUnresolvableCharacters", "text");
                    }

                    currentGlyph = defaultGlyph.Value;
                }

                // The first character on a line might have a negative left side bearing.
                // In this scenario, SpriteBatch/SpriteFont normally offset the text to the right,
                // so that text does not hang off the left side of its rectangle.
                if (firstGlyphOfLine)
                {
                    offset.X         = Math.Max(currentGlyph.LeftSideBearing, 0);
                    firstGlyphOfLine = false;
                }
                else
                {
                    offset.X += _font.Spacing + currentGlyph.LeftSideBearing;
                }

                offset.X += currentGlyph.Width;

                var proposedWidth = offset.X + Math.Max(currentGlyph.RightSideBearing, 0);
                if (proposedWidth > width)
                {
                    width = proposedWidth;
                }

                offset.X += currentGlyph.RightSideBearing;

                if (currentGlyph.Cropping.Height > finalLineHeight)
                {
                    finalLineHeight = currentGlyph.Cropping.Height;
                }
            }

            size.X = width;
            size.Y = offset.Y + finalLineHeight;
        }
Ejemplo n.º 3
0
        public void drawInto(
            Batcher batcher,
            ref FontCharacterSource text,
            Vector2 position,
            Color color,
            float rotation,
            Vector2 origin,
            Vector2 scale,
            SpriteEffects effect,
            float depth)
        {
            var flipAdjustment = Vector2.Zero;

            var flippedVert = (effect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically;
            var flippedHorz = (effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally;

            if (flippedVert || flippedHorz)
            {
                Vector2 size;
                measureString(ref text, out size);

                if (flippedHorz)
                {
                    origin.X        *= -1;
                    flipAdjustment.X = -size.X;
                }

                if (flippedVert)
                {
                    origin.Y        *= -1;
                    flipAdjustment.Y = _font.LineSpacing - size.Y;
                }
            }

            // TODO: This looks excessive... i suspect we could do most of this with simple vector math and avoid this much matrix work.
            var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != Vector2.One;

            if (requiresTransformation)
            {
                Matrix2D temp;
                Matrix2D.createTranslation(-origin.X, -origin.Y, out _transformationMatrix);
                Matrix2D.createScale(flippedHorz ? -scale.X : scale.X, flippedVert ? -scale.Y : scale.Y, out temp);
                Matrix2D.multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.createTranslation(flipAdjustment.X, flipAdjustment.Y, out temp);
                Matrix2D.multiply(ref temp, ref _transformationMatrix, out _transformationMatrix);
                Matrix2D.createRotation(rotation, out temp);
                Matrix2D.multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix2D.createTranslation(position.X, position.Y, out temp);
                Matrix2D.multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
            }

            // Get the default glyph here once.
            SpriteFont.Glyph?defaultGlyph = null;
            if (_font.DefaultCharacter.HasValue)
            {
                defaultGlyph = _glyphs[_font.DefaultCharacter.Value];
            }

            var currentGlyph     = SpriteFont.Glyph.Empty;
            var offset           = requiresTransformation ? Vector2.Zero : position - origin;
            var firstGlyphOfLine = true;

            for (var i = 0; i < text.Length; ++i)
            {
                var c = text[i];

                if (c == '\r')
                {
                    continue;
                }

                if (c == '\n')
                {
                    offset.X         = requiresTransformation ? 0f : position.X - origin.X;
                    offset.Y        += _font.LineSpacing;
                    firstGlyphOfLine = true;
                    continue;
                }

                if (!_glyphs.TryGetValue(c, out currentGlyph))
                {
                    if (!defaultGlyph.HasValue)
                    {
                        throw new ArgumentException("Errors.TextContainsUnresolvableCharacters", "text");
                    }

                    currentGlyph = defaultGlyph.Value;
                }

                // The first character on a line might have a negative left side bearing.
                // In this scenario, SpriteBatch/SpriteFont normally offset the text to the right,
                // so that text does not hang off the left side of its rectangle.
                if (firstGlyphOfLine)
                {
                    offset.X        += Math.Max(currentGlyph.LeftSideBearing, 0);
                    firstGlyphOfLine = false;
                }
                else
                {
                    offset.X += _font.Spacing + currentGlyph.LeftSideBearing;
                }

                var p = offset;

                if (flippedHorz)
                {
                    p.X += currentGlyph.BoundsInTexture.Width;
                }
                p.X += currentGlyph.Cropping.X;

                if (flippedVert)
                {
                    p.Y += currentGlyph.BoundsInTexture.Height - _font.LineSpacing;
                }
                p.Y += currentGlyph.Cropping.Y;

                // transform our point if we need to
                if (requiresTransformation)
                {
                    Vector2Ext.transform(ref p, ref _transformationMatrix, out p);
                }

                var destRect = RectangleExt.fromFloats(
                    p.X,
                    p.Y,
                    currentGlyph.BoundsInTexture.Width * scale.X,
                    currentGlyph.BoundsInTexture.Height * scale.Y);

                batcher.draw(
                    _font.Texture,
                    destRect,
                    currentGlyph.BoundsInTexture,
                    color,
                    rotation,
                    Vector2.Zero,
                    effect,
                    depth);

                offset.X += currentGlyph.Width + currentGlyph.RightSideBearing;
            }
        }
Ejemplo n.º 4
0
 internal void DrawInto(SpriteBatch spriteBatch, ref SpriteFont.CharacterSource text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth)
 {
   Vector2 zero1 = Vector2.Zero;
   bool flag1 = (effect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically;
   bool flag2 = (effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally;
   if (flag1 || flag2)
   {
     Vector2 size;
     this.MeasureString(ref text, out size);
     if (flag2)
     {
       origin.X *= -1f;
       scale.X *= -1f;
       zero1.X = -size.X;
     }
     if (flag1)
     {
       origin.Y *= -1f;
       scale.Y *= -1f;
       zero1.Y = (float) this.LineSpacing - size.Y;
     }
   }
   Matrix result1;
   Matrix.CreateTranslation(-origin.X, -origin.Y, 0.0f, out result1);
   Matrix result2;
   Matrix.CreateScale(scale.X, scale.Y, 1f, out result2);
   Matrix.Multiply(ref result1, ref result2, out result1);
   Matrix.CreateTranslation(zero1.X, zero1.Y, 0.0f, out result2);
   Matrix.Multiply(ref result2, ref result1, out result1);
   Matrix.CreateRotationZ(rotation, out result2);
   Matrix.Multiply(ref result1, ref result2, out result1);
   Matrix.CreateTranslation(position.X, position.Y, 0.0f, out result2);
   Matrix.Multiply(ref result1, ref result2, out result1);
   SpriteFont.Glyph? nullable = new SpriteFont.Glyph?();
   if (this.DefaultCharacter.HasValue)
     nullable = new SpriteFont.Glyph?(this._glyphs[this.DefaultCharacter.Value]);
   SpriteFont.Glyph glyph = SpriteFont.Glyph.Empty;
   Vector2 zero2 = Vector2.Zero;
   bool flag3 = false;
   for (int index = 0; index < text.Length; ++index)
   {
     char key = text[index];
     switch (key)
     {
       case '\r':
         flag3 = false;
         break;
       case '\n':
         zero2.X = 0.0f;
         zero2.Y += (float) this.LineSpacing;
         flag3 = false;
         break;
       default:
         if (flag3)
           zero2.X += this.Spacing + glyph.Width + glyph.RightSideBearing;
         flag3 = this._glyphs.TryGetValue(key, out glyph);
         if (!flag3)
         {
           if (!nullable.HasValue)
             throw new ArgumentException("Text contains characters that cannot be resolved by this SpriteFont.", "text");
           glyph = nullable.Value;
           flag3 = true;
         }
         zero2.X += glyph.LeftSideBearing;
         Vector2 result3 = zero2;
         if (flag2)
           result3.X += (float) glyph.BoundsInTexture.Width;
         result3.X += (float) glyph.Cropping.X;
         if (flag1)
           result3.Y += (float) (glyph.BoundsInTexture.Height - this.LineSpacing);
         result3.Y += (float) glyph.Cropping.Y;
         Vector2.Transform(ref result3, ref result1, out result3);
         Vector4 destinationRectangle = new Vector4(result3.X, result3.Y, (float) glyph.BoundsInTexture.Width * scale.X, (float) glyph.BoundsInTexture.Height * scale.Y);
         spriteBatch.DrawInternal(this._texture, destinationRectangle, new Rectangle?(glyph.BoundsInTexture), color, rotation, Vector2.Zero, effect, depth);
         break;
     }
   }
 }
Ejemplo n.º 5
0
        private void MeasureString(ref SpriteFont.CharacterSource text, out Vector2 size)
        {
            if (text.Length == 0)
            {
                size = Vector2.Zero;
            }
            else
            {
                SpriteFont.Glyph?nullable = new SpriteFont.Glyph?();
                if (this.DefaultCharacter.HasValue)
                {
                    nullable = new SpriteFont.Glyph?(this._glyphs[this.DefaultCharacter.Value]);
                }
                float            num1  = 0.0f;
                float            num2  = (float)this.LineSpacing;
                int              num3  = 0;
                SpriteFont.Glyph glyph = SpriteFont.Glyph.Empty;
                Vector2          zero  = Vector2.Zero;
                bool             flag  = false;
                for (int index = 0; index < text.Length; ++index)
                {
                    char key = text[index];
                    switch (key)
                    {
                    case '\r':
                        flag = false;
                        break;

                    case '\n':
                        ++num3;
                        num2   = (float)this.LineSpacing;
                        zero.X = 0.0f;
                        zero.Y = (float)(this.LineSpacing * num3);
                        flag   = false;
                        break;

                    default:
                        if (flag)
                        {
                            zero.X += this.Spacing + glyph.WidthIncludingBearings;
                        }
                        flag = this._glyphs.TryGetValue(key, out glyph);
                        if (!flag)
                        {
                            if (!nullable.HasValue)
                            {
                                throw new ArgumentException("Text contains characters that cannot be resolved by this SpriteFont.", "text");
                            }
                            glyph = nullable.Value;
                            flag  = true;
                        }
                        float num4 = zero.X + glyph.WidthIncludingBearings;
                        if ((double)num4 > (double)num1)
                        {
                            num1 = num4;
                        }
                        if ((double)glyph.Cropping.Height > (double)num2)
                        {
                            num2 = (float)glyph.Cropping.Height;
                            break;
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                size.X = num1;
                size.Y = (float)(num3 * this.LineSpacing) + num2;
            }
        }
Ejemplo n.º 6
0
        internal void DrawInto(SpriteBatch spriteBatch, ref SpriteFont.CharacterSource text, Vector2 position, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth)
        {
            Vector2 zero1 = Vector2.Zero;
            bool    flag1 = (effect & SpriteEffects.FlipVertically) == SpriteEffects.FlipVertically;
            bool    flag2 = (effect & SpriteEffects.FlipHorizontally) == SpriteEffects.FlipHorizontally;

            if (flag1 || flag2)
            {
                Vector2 size;
                this.MeasureString(ref text, out size);
                if (flag2)
                {
                    origin.X *= -1f;
                    scale.X  *= -1f;
                    zero1.X   = -size.X;
                }
                if (flag1)
                {
                    origin.Y *= -1f;
                    scale.Y  *= -1f;
                    zero1.Y   = (float)this.LineSpacing - size.Y;
                }
            }
            Matrix result1;

            Matrix.CreateTranslation(-origin.X, -origin.Y, 0.0f, out result1);
            Matrix result2;

            Matrix.CreateScale(scale.X, scale.Y, 1f, out result2);
            Matrix.Multiply(ref result1, ref result2, out result1);
            Matrix.CreateTranslation(zero1.X, zero1.Y, 0.0f, out result2);
            Matrix.Multiply(ref result2, ref result1, out result1);
            Matrix.CreateRotationZ(rotation, out result2);
            Matrix.Multiply(ref result1, ref result2, out result1);
            Matrix.CreateTranslation(position.X, position.Y, 0.0f, out result2);
            Matrix.Multiply(ref result1, ref result2, out result1);
            SpriteFont.Glyph?nullable = new SpriteFont.Glyph?();
            if (this.DefaultCharacter.HasValue)
            {
                nullable = new SpriteFont.Glyph?(this._glyphs[this.DefaultCharacter.Value]);
            }
            SpriteFont.Glyph glyph = SpriteFont.Glyph.Empty;
            Vector2          zero2 = Vector2.Zero;
            bool             flag3 = false;

            for (int index = 0; index < text.Length; ++index)
            {
                char key = text[index];
                switch (key)
                {
                case '\r':
                    flag3 = false;
                    break;

                case '\n':
                    zero2.X  = 0.0f;
                    zero2.Y += (float)this.LineSpacing;
                    flag3    = false;
                    break;

                default:
                    if (flag3)
                    {
                        zero2.X += this.Spacing + glyph.Width + glyph.RightSideBearing;
                    }
                    flag3 = this._glyphs.TryGetValue(key, out glyph);
                    if (!flag3)
                    {
                        if (!nullable.HasValue)
                        {
                            throw new ArgumentException("Text contains characters that cannot be resolved by this SpriteFont.", "text");
                        }
                        glyph = nullable.Value;
                        flag3 = true;
                    }
                    zero2.X += glyph.LeftSideBearing;
                    Vector2 result3 = zero2;
                    if (flag2)
                    {
                        result3.X += (float)glyph.BoundsInTexture.Width;
                    }
                    result3.X += (float)glyph.Cropping.X;
                    if (flag1)
                    {
                        result3.Y += (float)(glyph.BoundsInTexture.Height - this.LineSpacing);
                    }
                    result3.Y += (float)glyph.Cropping.Y;
                    Vector2.Transform(ref result3, ref result1, out result3);
                    Vector4 destinationRectangle = new Vector4(result3.X, result3.Y, (float)glyph.BoundsInTexture.Width * scale.X, (float)glyph.BoundsInTexture.Height * scale.Y);
                    spriteBatch.DrawInternal(this._texture, destinationRectangle, new Rectangle?(glyph.BoundsInTexture), color, rotation, Vector2.Zero, effect, depth);
                    break;
                }
            }
        }