using System; using System.Drawing; using System.Drawing.Drawing2D; using System.Collections; namespace Volian.Svg.Library { public abstract partial class SvgPart { public abstract void Draw(Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent); } public partial class SvgPartInheritance : SvgPart { public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) { } } public partial class Svg : SvgGroup { public void Draw(Graphics graphics) { //SetupInheritance(); //Console.WriteLine("{0}, DPI {1}", graphics.VisibleClipBounds, graphics.DpiX); SvgParts.Draw(graphics, new SvgScale(DeviceDPI(graphics), graphics.VisibleClipBounds, _Width, _Height, _ViewBox), this, this); //SvgParts.Draw(graphics, new SvgScale(100, graphics.VisibleClipBounds, _Width, _Height, _ViewBox)); } public static float DeviceDPI(Graphics g) { PointF[] tmp = { new PointF(g.DpiX, g.DpiY) }; g.TransformPoints(CoordinateSpace.World, CoordinateSpace.Device, tmp); return (float)Math.Round((double)tmp[0].X, 0); } } public partial class SvgArc : SvgShapePart { public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) { SetupInheritance(myParent.MyInheritedSettings); //_MyLineSettings.Scale = scale.M(1F); if (FillColor != Color.Transparent) using (Brush brush = new SolidBrush(FillColor)) graphics.FillEllipse(brush, scale.X(CX - Radius), scale.Y(CY - Radius), scale.M(Radius * 2), scale.M(Radius * 2)); if (OutlineWidth.Value != 0F && OutlineColor != Color.Empty) using (Pen pen = new Pen(OutlineColor, scale.M(OutlineWidth))) graphics.DrawEllipse(pen, scale.X(CX - Radius), scale.Y(CY - Radius), scale.M(Radius * 2), scale.M(Radius * 2)); } } public partial class SvgCircle : SvgShapePart { public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) { SetupInheritance(myParent.MyInheritedSettings); //_MyLineSettings.Scale = scale.M(1F); if (FillColor != Color.Transparent) using (Brush brush = new SolidBrush(FillColor)) graphics.FillEllipse(brush, scale.X(CX - Radius), scale.Y(CY - Radius), scale.M(Radius * 2), scale.M(Radius * 2)); if (OutlineWidth.Value != 0F && OutlineColor != Color.Empty) using (Pen pen = new Pen(OutlineColor, scale.M(OutlineWidth))) graphics.DrawEllipse(pen, scale.X(CX - Radius), scale.Y(CY - Radius), scale.M(Radius * 2), scale.M(Radius * 2)); } } public partial class SvgDefine : SvgGroup { public override void Draw(Graphics graphics, SvgScale myScale, Svg mySvg, SvgPartInheritance myParent) { // Don't draw //_SvgParts.Draw(graphics, myScale); } } public partial class SvgEllipse : SvgShapePart { public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) { SetupInheritance(myParent.MyInheritedSettings); if (FillColor != Color.Transparent) using (Brush brush = new SolidBrush(FillColor)) graphics.FillEllipse(brush, scale.X(CX - RX), scale.Y(CY - RY), scale.M(2 * RX), scale.M(2 * RY)); if (OutlineWidth.Value != 0F && OutlineColor != Color.Empty) using (Pen pen = new Pen(OutlineColor, scale.M(OutlineWidth))) graphics.DrawEllipse(pen, scale.X(CX - RX), scale.Y(CY - RY), scale.M(2 * RX), scale.M(2 * RY)); } } public partial class SvgGroup : SvgPartInheritance { public override void Draw(Graphics graphics, SvgScale myScale, Svg mySvg, SvgPartInheritance myParent) { SetupInheritance(myParent.MyInheritedSettings); _SvgParts.Draw(graphics, myScale, mySvg, this); } } public partial class SvgImage : SvgPart { public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) { SetupInheritance(myParent.MyInheritedSettings); graphics.DrawImage(Image.FromFile(ImagePath), scale.X(X), scale.Y(Y), scale.M(Width), scale.M(Height)); } } public partial class SvgLine : SvgLinePart { public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) { SetupInheritance(myParent.MyInheritedSettings); if (LineColor != Color.Empty) { Color myColor = AdjustColorForWidth(LineColor, scale.M(LineWidth)); using (Pen pen = new Pen(LineColor, scale.M(LineWidth))) graphics.DrawLine(pen, scale.X(X1), scale.Y(Y1), scale.X(X2), scale.Y(Y2)); } } } public partial class SvgParts : CollectionBase { public void Draw(Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) { foreach (SvgPart svgPart in List) svgPart.Draw(graphics,scale,mySvg, myParent); } } public partial class SvgRectangle : SvgShapePart { public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) { SetupInheritance(myParent.MyInheritedSettings); if (FillColor != Color.Transparent) using (Brush brush = new SolidBrush(FillColor)) graphics.FillRectangle(brush, scale.X(X),scale.Y(Y),scale.M(Width),scale.M(Height)); if (OutlineWidth.Value != 0F && OutlineColor != Color.Empty) using (Pen pen = new Pen(OutlineColor, scale.M(OutlineWidth))) graphics.DrawRectangle(pen, scale.X(X),scale.Y(Y), scale.M(Width), scale.M(Height)); } } //public partial class SvgRtf : SvgShapePart //{ // //private static float _Factor = 1.085F; // 20 point Arial Font // public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) // { // SetupInheritance(myParent.MyInheritedSettings); // _MyFontSettings.Scale = scale.M(1F); // } // private float YAdjustment(Graphics gr, Font fnt) // { // int yAsc = fnt.FontFamily.GetCellAscent(fnt.Style); // int yLS = fnt.FontFamily.GetLineSpacing(fnt.Style); // float gth = fnt.GetHeight(gr); // float fAsc = (yAsc * gth) / yLS; // return -fAsc; // } //} public partial class SvgText : SvgShapePart { //private static float _Factor = 1.085F; // 20 point Arial Font public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) { SetupInheritance(myParent.MyInheritedSettings); _MyFontSettings.Scale = scale.M(1F); StringFormat stringFormat = StringFormat.GenericTypographic; float YAdj = YAdjustment(graphics, Font); using (Brush brush = new System.Drawing.SolidBrush(FillColor)) graphics.DrawString(Text, Font, brush, scale.X(X), scale.Y(Y) + YAdj, stringFormat); SizeF sf = graphics.MeasureString(Text, Font, graphics.VisibleClipBounds.Size, stringFormat); if (OutlineWidth.Value != 0 && OutlineColor != Color.Empty) using (GraphicsPath path = GetStringPath(Text, Svg.DeviceDPI(graphics), new PointF(scale.X(X), scale.Y(Y) + YAdj), Font, stringFormat)) using (Pen myPen = new Pen(OutlineColor, scale.M(OutlineWidth))) graphics.DrawPath(myPen, path); } GraphicsPath GetStringPath(string s, float dpi, PointF loc, Font font, StringFormat format) { GraphicsPath path = new GraphicsPath(); // Convert font size into appropriate coordinates float emSize = dpi * font.SizeInPoints / 72; path.AddString(s, font.FontFamily, (int)font.Style, emSize, loc, format); return path; } private float YAdjustment(Graphics gr, Font fnt) { int yAsc = fnt.FontFamily.GetCellAscent(fnt.Style); //int yDesc = fnt.FontFamily.GetCellDescent(fnt.Style); //int yEm = fnt.FontFamily.GetEmHeight(fnt.Style); int yLS = fnt.FontFamily.GetLineSpacing(fnt.Style); float gth = fnt.GetHeight(gr); float fh = fnt.Size; float fAsc = (yAsc * gth) / yLS; return -fAsc; } } public partial class SvgUse : SvgPartInheritance { public override void Draw(System.Drawing.Graphics graphics, SvgScale scale, Svg mySvg, SvgPartInheritance myParent) { SetupInheritance(myParent.MyInheritedSettings); mySvg[_UseID.Substring(1)].Draw(graphics, scale.AdjustOrigin(X, Y), mySvg, this); } } }