Beispiel #1
0
        void Layout()
        {
            float scale = (fontSize * surface.Dpi) / (72 * font.UnitsPerEm);

            int   currentLineStart  = 0;
            int   currentLineEnd    = 0;
            float currentLineLength = 0;

            int   currentWordStart  = 0;
            int   currentWordEnd    = 0;
            float currentWordLength = 0;

            var lines       = new List <string>();
            var currentLine = new StringBuilder();

            for (int i = 0; i < text.Length; i++)
            {
                if (char.IsControl(text[i]))
                {
                }
                else
                {
                    Font.Glyph glyph = font.GetGlyph(text[i]);

                    currentWordLength += glyph.AdvanceWidth * scale;
                }
            }
        }
        /// <summary>
        /// Draws text.
        /// </summary>
        /// <param name="text">The text.</param>
        /// <param name="location">The location the text will be displayed at.</param>
        /// <param name="font">The font of the text.</param>
        /// <param name="size">The font size in points.</param>
        /// <param name="brush">The brush used to draw the text.</param>
        public void DrawText(string text, Vector2 location, Font font, float size, Brush brush)
        {
            if (string.IsNullOrWhiteSpace(text))
            {
                return;
            }

            float scale = (size * Dpi) / (72 * font.UnitsPerEm);

            SetBrush(brush);

            int horizontalPosition = 0;
            int verticalPosition   = font.Ascend;

            for (int i = 0; i < text.Length; i++)
            {
                if (char.IsControl(text[i]))
                {
                    switch (text[i])
                    {
                    case '\n':
                        verticalPosition  += font.Ascend - font.Descend + font.LineGap;
                        horizontalPosition = 0;
                        break;
                    }
                }
                else
                {
                    Font.Glyph glyph = font.GetGlyph(text[i]);
                    if (glyph.Vertices.Length > 0)
                    {
                        textMatrix = Matrix.Transpose(Matrix.Translation(horizontalPosition + glyph.LeftSideBearing, -verticalPosition, 0)
                                                      * Matrix.Scaling(scale, -scale, 1)
                                                      * Matrix.Translation(location.X, location.Y, 0));
                        UpdateMatrixBuffer();

                        brush.UpdateVertices(glyph.Vertices);
                        DrawVertices(glyph.Indices, glyph.Vertices);
                    }

                    horizontalPosition += glyph.AdvanceWidth;
                }
            }

            textMatrix = Matrix.Identity;
            UpdateMatrixBuffer();
        }