Exemplo n.º 1
0
        /// <summary>
        /// Measure a string.
        /// </summary>
        /// <returns>The measurements.</returns>
        /// <param name="text">Text.</param>
        public Point MeasureString(string text)
        {
            // Skip empty strings
            if (text == "")
            {
                return(Point.Empty);
            }

            // Get the codepoint iterator
            var iter = new CodepointIterator(text);

            // Initialize coordinates
            float penX = 0, penY = 0;

            // Iterate using the codepoint iterator
            while (iter.Iterate())
            {
                // Get the current codepoint
                var c = iter.Codepoint;

                // Increment the y coords if the char is a linefeed
                if (c == (uint)'\n')
                {
                    penY += lineHeight;
                    penX  = 0;
                    continue;
                }

                // Get the glyph
                var glyph = GetGlyph(c);

                if (glyph.Render)
                {
                    penX += glyph.HorizontalAdvance;
                    penY += glyph.AdvanceY;
                }
                else
                {
                    penX += glyph.AdvanceX;
                    penY += glyph.AdvanceY;
                }

                if (kerning && iter.Index < (iter.Count - 1))
                {
                    var          g2 = GetGlyph(iter.PeekNext());
                    FT.FT_Vector vec;
                    FT.FT_Get_Kerning(facePtr, glyph.CharIndex, g2.CharIndex, 2, out vec);
                    var krn = FTMath.From26Dot6(vec.x);
                    penX += krn;
                }
            }

            penY = Math.Max(penY, LineHeight);
            return(new Point((int)penX, (int)penY));
        }
Exemplo n.º 2
0
        /// <summary>
        /// Draw a string.
        /// </summary>
        /// <param name="spriteBatch">Sprite batch.</param>
        /// <param name="text">Text.</param>
        /// <param name="x">The x coordinate.</param>
        /// <param name="y">The y coordinate.</param>
        /// <param name="color">Color.</param>
        public void DrawString(SpriteBatch spriteBatch, string text, int x, int y, Color4 color)
        {
            if (text == "")             //Skip empty strings
            {
                return;
            }
            var   iter = new CodepointIterator(text);
            float penX = x, penY = y;

            while (iter.Iterate())
            {
                uint c = iter.Codepoint;
                if (c == (uint)'\n')
                {
                    penY += lineHeight;
                    penX  = x;
                    continue;
                }
                var glyph = GetGlyph(c);
                if (glyph.Render)
                {
                    spriteBatch.Draw(
                        glyph.Texture,
                        glyph.Rectangle,
                        new Rectangle(
                            (int)penX + glyph.XOffset,
                            (int)penY + (LineHeight - glyph.YOffset),
                            glyph.Rectangle.Width,
                            glyph.Rectangle.Height
                            ),
                        color
                        );
                    penX += glyph.HorizontalAdvance;
                    penY += glyph.AdvanceY;
                }
                else
                {
                    penX += glyph.AdvanceX;
                    penY += glyph.AdvanceY;
                }
                if (iter.Index < iter.Count - 1)
                {
                    var          g2 = GetGlyph(iter.PeekNext());
                    FT.FT_Vector vec;
                    FT.FT_Get_Kerning(facePtr, glyph.CharIndex, g2.CharIndex, 2, out vec);
                    var krn = FTMath.From26Dot6(vec.x);
                    penX += krn;
                }
            }
        }
Exemplo n.º 3
0
        /// <summary>
        /// Measure a string.
        /// </summary>
        /// <returns>The measurements.</returns>
        /// <param name="text">Text.</param>
        public Point MeasureString(string text)
        {
            // Skip empty strings
            if (text == "")
                return Point.Empty;

            // Get the codepoint iterator
            var iter = new CodepointIterator (text);

            // Initialize coordinates
            float penX = 0, penY = 0;

            // Iterate using the codepoint iterator
            while (iter.Iterate ()) {

                // Get the current codepoint
                var c = iter.Codepoint;

                // Increment the y coords if the char is a linefeed
                if (c == (uint) '\n') {
                    penY += lineHeight;
                    penX = 0;
                    continue;
                }

                // Get the glyph
                var glyph = GetGlyph (c);

                if (glyph.Render) {
                    penX += glyph.HorizontalAdvance;
                    penY += glyph.AdvanceY;
                } else {
                    penX += glyph.AdvanceX;
                    penY += glyph.AdvanceY;
                }

                if (kerning && iter.Index < (iter.Count - 1)) {
                    var g2 = GetGlyph (iter.PeekNext ());
                    FT.FT_Vector vec;
                    FT.FT_Get_Kerning (facePtr, glyph.CharIndex, g2.CharIndex, 2, out vec);
                    var krn = FTMath.From26Dot6 (vec.x);
                    penX += krn;
                }
            }

            penY = Math.Max (penY, LineHeight);
            return new Point ((int) penX, (int) penY);
        }
Exemplo n.º 4
0
 /// <summary>
 /// Draw a string.
 /// </summary>
 /// <param name="spriteBatch">Sprite batch.</param>
 /// <param name="text">Text.</param>
 /// <param name="x">The x coordinate.</param>
 /// <param name="y">The y coordinate.</param>
 /// <param name="color">Color.</param>
 public void DrawString(SpriteBatch spriteBatch, string text, int x, int y, Color4 color)
 {
     if (text == "") //Skip empty strings
         return;
     var iter = new CodepointIterator (text);
     float penX = x, penY = y;
     while (iter.Iterate ()) {
         uint c = iter.Codepoint;
         if (c == (uint) '\n') {
             penY += lineHeight;
             penX = x;
             continue;
         }
         var glyph = GetGlyph (c);
         if (glyph.Render) {
             spriteBatch.Draw (
                 glyph.Texture,
                 glyph.Rectangle,
                 new Rectangle (
                     (int) penX + glyph.XOffset,
                     (int) penY + (LineHeight - glyph.YOffset),
                     glyph.Rectangle.Width,
                     glyph.Rectangle.Height
                 ),
                 color
             );
             penX += glyph.HorizontalAdvance;
             penY += glyph.AdvanceY;
         } else {
             penX += glyph.AdvanceX;
             penY += glyph.AdvanceY;
         }
         if (iter.Index < iter.Count - 1) {
             var g2 = GetGlyph (iter.PeekNext ());
             FT.FT_Vector vec;
             FT.FT_Get_Kerning (facePtr, glyph.CharIndex, g2.CharIndex, 2, out vec);
             var krn = FTMath.From26Dot6 (vec.x);
             penX += krn;
         }
     }
 }