示例#1
0
        public TextLayout(string text, Vector2 bounds, Font font, float fontSize, TextFormat format)
        {
            surface = GameEngine.TryQueryComponent<ISurface>();
            if (surface == null) throw new InvalidOperationException("A surface must be registered before a text layout can be created.");
            surface.DpiChanged += OnSurfaceDpiChanged;

            this.text = text;
            this.bounds = bounds;
            this.font = font;
            this.fontSize = fontSize;
            this.format = format;

            dpiChanged = false;
            Glyphs = new List<PositionedGlyph>();
            Layout();
        }
示例#2
0
 /// <summary>
 /// Draws formatted text.
 /// </summary>
 /// <param name="text">The text.</param>
 /// <param name="bounds">The bounds the text will be displayed in.</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>
 /// <param name="format">The format of the text.</param>
 public void DrawText(string text, Rectangle bounds, Font font, float size, TextFormat format, Brush brush)
 {
     var layout = new TextLayout(text, bounds.Size, font, size, format);
     DrawTextLayout(layout, bounds.Location, brush);
 }
示例#3
0
        /// <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();
        }