Example #1
0
 public void DrawText(int x, int y, int width, int height, string str, FontAligns align)
 {
     DrawText(x, y, width, height, str, align, FontStyles.Normal);
 }
Example #2
0
        public void DrawText(int x, int y, int width, int height, string str, FontAligns alignment, FontStyles style, bool blend)
        {
            System.Drawing.FontStyle drawingStyle = System.Drawing.FontStyle.Regular;
            switch (style)
            {
            case FontStyles.Normal: drawingStyle = System.Drawing.FontStyle.Regular; break;

            case FontStyles.Bold: drawingStyle = System.Drawing.FontStyle.Bold; break;

            case FontStyles.Italic: drawingStyle = System.Drawing.FontStyle.Italic; break;

            case FontStyles.Underline: drawingStyle = System.Drawing.FontStyle.Underline; break;

            case FontStyles.StrikeThrough: drawingStyle = System.Drawing.FontStyle.Strikeout; break;
            }

            System.Drawing.Font font = new System.Drawing.Font(FontName, FontSize, drawingStyle);
            Rect rect = new Rect(x, y, width, height);

            //DrawRect(rect, Colors.Black);
            while (TextSize(str, font.Size, font.Name).Width > rect.Width ||
                   TextSize(str, font.Size, font.Name).Height > rect.Height &
                   font.Size >= MIN_FONT_SIZE)
            {
                font = new System.Drawing.Font(font.Name, font.Size - 1, font.Style);
            }

            int n = 0;

            while (TextSize(str, font.Size, font.Name).Height + n * 2 - 1 < rect.Height)
            {
                n++;
            }
            rect.Y += n;

            int length  = TextSize(str, font.Size, font.Name).Width;
            int gLength = (int)Graphics.MeasureString(str, font).Width;

            if (alignment == FontAligns.Right)
            {
                rect.X += rect.Width - gLength;
            }
            if (alignment == FontAligns.Center)
            {
                rect.X += (rect.Width - gLength) / 2;
            }

            //rect.X -= ((int)Graphics.MeasureString(str, font).Width - length) / 2;
            //Graphics.DrawString(str, font, FontColor.ToSolidBrush(), rect.ToSystemRect());

            System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(rect.Width, rect.Height);
            System.Drawing.Graphics.FromImage(bmp).Clear(Colors.Green.ToSystemColor());
            System.Drawing.Graphics.FromImage(bmp).DrawString(str, font, Colors.Red.ToSolidBrush(), new System.Drawing.PointF(0, 0));

            float[] cm = new float[] { FontColor.Red / 255.0f, FontColor.Green / 255.0f, FontColor.Blue / 255.0f };

            float[][] matrix = new float[][]
            {
                new float[] { cm[0], cm[1], cm[2], 1, 0 },
                new float[] { cm[0], cm[1], cm[2], 0, 0 },
                new float[] { cm[0], cm[1], cm[2], 1, 0 },
                new float[] { 0, 0, 0, 0, 0 },
                new float[] { 0, 0, 0, 0, 1 }
            };

            System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes();
            attr.SetColorMatrix(new System.Drawing.Imaging.ColorMatrix(matrix));
            Graphics.DrawImage(bmp, rect.ToSystemRect(), 0, 0, bmp.Width, bmp.Height, System.Drawing.GraphicsUnit.Pixel, attr);
        }
Example #3
0
 public void DrawText(Rect rect, string str, FontAligns align)
 {
     DrawText(rect.X, rect.Y, rect.Width, rect.Height, str, align);
 }
Example #4
0
 /// <summary>
 /// Draws a given string at the coordinates or <see cref="Game_Player.Rect">Rect</see> given.
 /// By default the string will be at the size designated by the FontSize property, but it
 /// will resize to fit inside the Width and Height. Style and alignment can be designated,
 /// but the alignment is defaulted to <see cref="F:Game_Player.FontAligns.Left">Left</see>
 /// and the style to <see cref="F:Game_Player.FontStyles.Normal">Normal</see>.
 /// This method is intended for single-line text.
 /// </summary>
 /// <param name="x">The X-coordinate of the drawn string.</param>
 /// <param name="y">The X-coordinate of the drawn string.</param>
 /// <param name="width">The max Width of the drawn string.</param>
 /// <param name="height">The max Height of the drawn string.</param>
 /// <param name="str">The string to be drawn.</param>
 /// <param name="alignment">The <see cref="Game_Player.FontAligns">FontAligns</see> that designates
 /// how to align the drawn string.</param>
 /// <param name="style">The <see cref="Game_Player.FontStyles">FontStyles</see> that designates
 /// how to style the drawn string.</param>
 public void DrawText(int x, int y, int width, int height, string str, FontAligns alignment, FontStyles style)
 {
     DrawText(x, y, width, height, str, alignment, style, true);
 }