コード例 #1
0
        /// <summary>
        /// Provides the size, in pixels, of the specified text when drawn with this font, automatically wrapping to keep within the specified with.
        /// </summary>
        /// <param name="text">The text to measure.</param>
        /// <param name="maxWidth">The maximum width.</param>
        /// <returns>
        /// The <see cref="Size"/>, in pixels, of <paramref name="text"/> drawn with this font.
        /// </returns>
        /// <remarks>The MeasureString method uses the <paramref name="maxWidth"/> parameter to automatically wrap when determining text size.</remarks>
        public Point measureString(ref FontCharacterSource text, float maxWidth = kNoMaxWidth)
        {
            if (text.Length == 0)
            {
                return(Point.Zero);
            }

            var length            = text.Length;
            var previousCharacter = ' ';
            var currentLineWidth  = 0;
            var currentLineHeight = lineHeight;
            var blockWidth        = 0;
            var blockHeight       = 0;
            var lineHeights       = new List <int>();

            for (var i = 0; i < length; i++)
            {
                var character = text[i];
                if (character == '\n' || character == '\r')
                {
                    if (character == '\n' || i + 1 == length || text[i + 1] != '\n')
                    {
                        lineHeights.Add(currentLineHeight);
                        blockWidth        = Math.Max(blockWidth, currentLineWidth);
                        currentLineWidth  = 0;
                        currentLineHeight = lineHeight;
                    }
                }
                else
                {
                    var data  = this[character];
                    var width = data.xAdvance + GetKerning(previousCharacter, character) + spacing.X;
                    if (maxWidth != kNoMaxWidth && currentLineWidth + width >= maxWidth)
                    {
                        lineHeights.Add(currentLineHeight);
                        blockWidth        = Math.Max(blockWidth, currentLineWidth);
                        currentLineWidth  = 0;
                        currentLineHeight = lineHeight;
                    }

                    currentLineWidth += width;
                    currentLineHeight = Math.Max(currentLineHeight, data.bounds.Height + data.offset.Y);
                    previousCharacter = character;
                }
            }

            // finish off the current line if required
            if (currentLineHeight != 0)
            {
                lineHeights.Add(currentLineHeight);
            }

            // reduce any lines other than the last back to the base
            for (var i = 0; i < lineHeights.Count - 1; i++)
            {
                lineHeights[i] = lineHeight;
            }

            return(new Point(Math.Max(currentLineWidth, blockWidth), blockHeight));
        }
コード例 #2
0
ファイル: BitmapFont.cs プロジェクト: shosanna/MonoKuratko
        void IFont.drawInto(Batcher batcher, StringBuilder text, Vector2 position, Color color,
                            float rotation, Vector2 origin, Vector2 scale, SpriteEffects effect, float depth)
        {
            var source = new FontCharacterSource(text);

            drawInto(batcher, ref source, position, color, rotation, origin, scale, effect, depth);
        }
コード例 #3
0
        /// <summary>
        /// Submit a text string of sprites for drawing in the current batch.
        /// </summary>
        /// <param name="spriteFont">A font.</param>
        /// <param name="text">The text which will be drawn.</param>
        /// <param name="position">The drawing location on screen.</param>
        /// <param name="color">A color mask.</param>
        /// <param name="rotation">A rotation of this string.</param>
        /// <param name="origin">Center of the rotation. 0,0 by default.</param>
        /// <param name="scale">A scaling of this string.</param>
        /// <param name="effects">Modificators for drawing. Can be combined.</param>
        /// <param name="layerDepth">A depth of the layer of this string.</param>
        public static void DrawString(this Batcher batcher, BitmapFont bitmapFont, string text, Vector2 position, Color color,
                                      float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth)
        {
            var source = new FontCharacterSource(text);

            bitmapFont.DrawInto(batcher, ref source, position, color, rotation, origin, scale, effects, layerDepth);
        }
コード例 #4
0
        public void DrawInto(Batcher batcher, StringBuilder text, System.Numerics.Vector2 position, Color color,
                             float rotation, System.Numerics.Vector2 origin, System.Numerics.Vector2 scale, SpriteEffects effect, float depth)
        {
            var source = new FontCharacterSource(text);

            DrawInto(batcher, ref source, position, color, rotation, origin, scale, effect, depth);
        }
コード例 #5
0
ファイル: BitmapFont.cs プロジェクト: shosanna/MonoKuratko
        void measureString(ref FontCharacterSource text, out Vector2 size)
        {
            if (text.Length == 0)
            {
                size = Vector2.Zero;
                return;
            }

            var width           = 0.0f;
            var finalLineHeight = (float)lineHeight;
            var fullLineCount   = 0;
            BitmapFontRegion currentFontRegion = null;
            var offset = Vector2.Zero;

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

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

                if (c == '\n')
                {
                    fullLineCount++;
                    finalLineHeight = lineHeight;

                    offset.X          = 0;
                    offset.Y          = lineHeight * fullLineCount;
                    currentFontRegion = null;
                    continue;
                }

                if (currentFontRegion != null)
                {
                    offset.X += spacing + currentFontRegion.xAdvance;
                }

                if (!_characterMap.TryGetValue(c, out currentFontRegion))
                {
                    currentFontRegion = defaultCharacterRegion;
                }

                var proposedWidth = offset.X + currentFontRegion.xAdvance + spacing;
                if (proposedWidth > width)
                {
                    width = proposedWidth;
                }

                if (currentFontRegion.height + currentFontRegion.yOffset > finalLineHeight)
                {
                    finalLineHeight = currentFontRegion.height + currentFontRegion.yOffset;
                }
            }

            size.X = width;
            size.Y = fullLineCount * lineHeight + finalLineHeight;
        }
コード例 #6
0
        /// <summary>
        /// Submit a text string of sprites for drawing in the current batch.
        /// </summary>
        /// <param name="bitmapFont">A font.</param>
        /// <param name="text">The text which will be drawn.</param>
        /// <param name="position">The drawing location on screen.</param>
        /// <param name="color">A color mask.</param>
        public static void DrawString(this Batcher batcher, BitmapFont bitmapFont, StringBuilder text, Vector2 position,
                                      Color color)
        {
            var source = new FontCharacterSource(text);

            bitmapFont.DrawInto(batcher, ref source, position, color, 0, Vector2.Zero, new Vector2(1),
                                SpriteEffects.None, 0f);
        }
コード例 #7
0
ファイル: BitmapFont.cs プロジェクト: shosanna/MonoKuratko
        /// <summary>
        /// Returns the size of the contents of a StringBuilder when rendered in this font.
        /// </summary>
        /// <returns>The string.</returns>
        /// <param name="text">Text.</param>
        public Vector2 measureString(StringBuilder text)
        {
            var     source = new FontCharacterSource(text);
            Vector2 size;

            measureString(ref source, out size);
            return(size);
        }
コード例 #8
0
        /// <summary>
        /// Submit a text string of sprites for drawing in the current batch.
        /// </summary>
        /// <param name="bitmapFont">A font.</param>
        /// <param name="text">The text which will be drawn.</param>
        /// <param name="position">The drawing location on screen.</param>
        /// <param name="color">A color mask.</param>
        /// <param name="rotation">A rotation of this string.</param>
        /// <param name="origin">Center of the rotation. 0,0 by default.</param>
        /// <param name="scale">A scaling of this string.</param>
        /// <param name="effects">Modificators for drawing. Can be combined.</param>
        /// <param name="layerDepth">A depth of the layer of this string.</param>
        public static void DrawString(this Batcher batcher, BitmapFont bitmapFont, StringBuilder text, System.Numerics.Vector2 position,
                                      Color color,
                                      float rotation, System.Numerics.Vector2 origin, float scale, SpriteEffects effects,
                                      float layerDepth)
        {
            var scaleVec = new System.Numerics.Vector2(scale, scale);
            var source   = new FontCharacterSource(text);

            bitmapFont.DrawInto(batcher, ref source, position, color, rotation, origin, scaleVec, effects, layerDepth);
        }
コード例 #9
0
 public BitmapFontEnumerator(BitmapFont font, ref FontCharacterSource text)
 {
     _font            = font;
     _text            = text;
     _currentGlyph    = new BitmapFontGlyph();
     _previousGlyph   = null;
     _previousChar    = null;
     _runningPosition = Vector2.Zero;
     _index           = -1;
 }
コード例 #10
0
 public BitmapFontEnumerator GetGlyphs(ref FontCharacterSource text) => new BitmapFontEnumerator(this, ref text);
コード例 #11
0
        public BitmapFontEnumerator GetGlyphs(StringBuilder text)
        {
            var source = new FontCharacterSource(text);

            return(GetGlyphs(ref source));
        }
コード例 #12
0
        public Point measureString(StringBuilder text, float maxWidth = kNoMaxWidth)
        {
            var source = new FontCharacterSource(text);

            return(measureString(ref source, maxWidth));
        }
コード例 #13
0
ファイル: BitmapFont.cs プロジェクト: JonSnowbd/Ash
        public Point MeasureString(string text, float maxWidth = kNoMaxWidth)
        {
            var source = new FontCharacterSource(text);

            return(MeasureString(ref source, maxWidth));
        }
コード例 #14
0
ファイル: BitmapFont.cs プロジェクト: shosanna/MonoKuratko
        /// <summary>
        /// old SpriteBatch drawing method. This should probably be removed since SpriteBatch
        /// </summary>
        /// <param name="spriteBatch">Sprite batch.</param>
        /// <param name="text">Text.</param>
        /// <param name="position">Position.</param>
        /// <param name="color">Color.</param>
        /// <param name="rotation">Rotation.</param>
        /// <param name="origin">Origin.</param>
        /// <param name="scale">Scale.</param>
        /// <param name="effect">Effect.</param>
        /// <param name="depth">Depth.</param>
        internal void drawInto(SpriteBatch spriteBatch, 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 = lineHeight - size.Y;
                }
            }


            var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != Vector2.One;

            if (requiresTransformation)
            {
                Matrix temp;
                Matrix.CreateTranslation(-origin.X, -origin.Y, 0f, out _transformationMatrix);
                Matrix.CreateScale((flippedHorz ? -scale.X : scale.X), (flippedVert ? -scale.Y : scale.Y), 1f, out temp);
                Matrix.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix.CreateTranslation(flipAdjustment.X, flipAdjustment.Y, 0, out temp);
                Matrix.Multiply(ref temp, ref _transformationMatrix, out _transformationMatrix);
                Matrix.CreateRotationZ(rotation, out temp);
                Matrix.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
                Matrix.CreateTranslation(position.X, position.Y, 0f, out temp);
                Matrix.Multiply(ref _transformationMatrix, ref temp, out _transformationMatrix);
            }

            BitmapFontRegion currentFontRegion = null;
            var offset = requiresTransformation ? Vector2.Zero : position - origin;

            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         += lineHeight;
                    currentFontRegion = null;
                    continue;
                }

                if (currentFontRegion != null)
                {
                    offset.X += spacing + currentFontRegion.xAdvance;
                }

                if (!_characterMap.TryGetValue(c, out currentFontRegion))
                {
                    currentFontRegion = defaultCharacterRegion;
                }


                var p = offset;

                if (flippedHorz)
                {
                    p.X += currentFontRegion.width;
                }
                p.X += currentFontRegion.xOffset;

                if (flippedVert)
                {
                    p.Y += currentFontRegion.height - lineHeight;
                }
                p.Y += currentFontRegion.yOffset;

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

                var destRect = RectangleExt.fromFloats
                               (
                    p.X, p.Y,
                    currentFontRegion.width * scale.X,
                    currentFontRegion.height * scale.Y
                               );

                spriteBatch.Draw(currentFontRegion.subtexture, destRect, currentFontRegion.subtexture.sourceRect, color, rotation, Vector2.Zero, effect, depth);
            }
        }
コード例 #15
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)
            {
                var size = MeasureString(ref text);

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

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


            var requiresTransformation = flippedHorz || flippedVert || rotation != 0f || scale != new Vector2(1);

            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);
            }

            var       previousCharacter = ' ';
            Character currentChar       = null;
            var       offset            = requiresTransformation ? Vector2.Zero : position - origin;

            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   += LineHeight;
                    currentChar = null;
                    continue;
                }

                if (currentChar != null)
                {
                    offset.X += Spacing.X + currentChar.XAdvance;
                }

                currentChar = ContainsCharacter(c) ? this[c] : DefaultCharacter;

                var p = offset;

                if (flippedHorz)
                {
                    p.X += currentChar.Bounds.Width;
                }
                p.X += currentChar.Offset.X + GetKerning(previousCharacter, currentChar.Char);

                if (flippedVert)
                {
                    p.Y += currentChar.Bounds.Height - LineHeight;
                }
                p.Y += currentChar.Offset.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,
                    currentChar.Bounds.Width * scale.X,
                    currentChar.Bounds.Height * scale.Y
                               );

                batcher.Draw(Textures[currentChar.TexturePage], destRect, currentChar.Bounds, color, rotation, Vector2.Zero, effect, depth);
                previousCharacter = c;
            }
        }