using System;
using System.Collections.Generic;
using System.Drawing;
namespace DesktopMagicPluginAPI.Drawing
{
///
/// Extentions for the class.
///
public static class GraphicsExtentions
{
private static readonly Dictionary fonts = new Dictionary(new FontComparer());
///
/// Draws the specified text string at the specified location with the specified and objects.
///
/// Graphics object.
/// String to draw.
/// that defines the text format of the string.
/// that determines the color and texture of the drawn text.
/// The x-coordinate of the upper-left corner of the drawn text.
/// The y-coordinate of the upper-left corner of the drawn text.
public static void DrawStringMonospace(this Graphics graphics, string s, Font font, Brush brush, float x, float y)
{
int widest = GetWidestChar(font);
for (int i = 0; i < s.Length; i++)
{
graphics.DrawString(s[i].ToString(), font, brush, x, y);
x += widest;
}
}
///
/// Draws the specified text string at the specified location with the specified and objects.
///
/// Graphics object.
/// String to draw.
/// that defines the text format of the string.
/// that determines the color and texture of the drawn text.
/// structure that specifies the upper-left corner of the drawn text.
public static void DrawStringMonospace(this Graphics graphics, string s, Font font, Brush brush, PointF point)
{
int widest = GetWidestChar(font);
for (int i = 0; i < s.Length; i++)
{
graphics.DrawString(s[i].ToString(), font, brush, point);
point.X += widest;
}
}
///
/// Draws the specified text string at the specified location with the specified and objects.
///
/// Graphics object.
/// String to draw.
/// that defines the text format of the string.
/// that determines the color and texture of the drawn text.
/// The x-coordinate of the upper-left corner of the drawn text.
/// The y-coordinate of the upper-left corner of the drawn text.
/// /// The specified width.
public static void DrawStringFixedWidth(this Graphics graphics, string s, Font font, Brush brush, float x, float y, float width)
{
for (int i = 0; i < s.Length; i++)
{
graphics.DrawString(s[i].ToString(), font, brush, x, y);
x += width;
}
}
///
/// Draws the specified text string at the specified location with the specified and objects.
///
/// Graphics object.
/// String to draw.
/// that defines the text format of the string.
/// that determines the color and texture of the drawn text.
/// structure that specifies the upper-left corner of the drawn text.
/// The specified width.
public static void DrawStringFixedWidth(this Graphics graphics, string s, Font font, Brush brush, PointF point, float width)
{
for (int i = 0; i < s.Length; i++)
{
graphics.DrawString(s[i].ToString(), font, brush, point);
point.X += width;
}
}
private static int GetWidestChar(Font font)
{
if (fonts.ContainsKey(font))
return fonts[font];
Graphics graphics = Graphics.FromImage(new Bitmap(1, 1));
float max = 0;
char maxx = ' ';
for (int i = 0; i <= 255; i++)
{
char c = (char)i;
if (char.IsLetterOrDigit(c))
{
float neww = graphics.MeasureString(c.ToString(), font).Width;
if (neww >= max)
{
max = neww;
maxx = c;
}
}
}
Console.WriteLine((int)Math.Round(max, 0));
Console.WriteLine(maxx);
fonts.Add(font, (int)Math.Round(max, 0));
return (int)Math.Round(max, 0);
}
}
}