Переглянути джерело

Extracted out Graphics functionality.

Kenric Nugteren 5 місяців тому
батько
коміт
46ff898796
2 змінених файлів з 100 додано та 7 видалено
  1. 75 2
      inabox.dxf/DrawData.cs
  2. 25 5
      inabox.dxf/Objects.cs

+ 75 - 2
inabox.dxf/DrawData.cs

@@ -8,6 +8,7 @@ using System.Linq;
 using System.Numerics;
 using System.Text;
 using System.Threading.Tasks;
+using FontStyle = System.Drawing.FontStyle;
 using Vector2 = System.Numerics.Vector2;
 using Vector3 = System.Numerics.Vector3;
 using Vector4 = System.Numerics.Vector4;
@@ -159,11 +160,83 @@ internal class TransformData
 
 internal class DrawData : TransformData
 {
-    public Graphics Graphics { get; set; }
+    public IGraphics Graphics { get; set; }
 
     protected override void UpdateTransform()
     {
         base.UpdateTransform();
-        Graphics.Transform = ProjectMatrix(Transform);
+        Graphics.SetTransform(ProjectMatrix(Transform));
+    }
+}
+
+public interface IGraphics
+{
+    void SetTransform(Matrix transform);
+
+    void DrawLine(Color color, float thickness, params PointF[] points);
+
+    void FillPolygon(Color color, params PointF[] points);
+
+    /// <summary>
+    /// Set the current font to be the default font at the given <paramref name="fontSize"/>.
+    /// </summary>
+    /// <param name="fontSize">Size of the new font.</param>
+    void SetFont(float fontSize, FontStyle fontStyle = FontStyle.Regular);
+
+    void SetFont(string fontName, float fontSize, FontStyle fontStyle = FontStyle.Regular);
+
+    void DrawText(string text, Color color, PointF position);
+}
+
+public class GdiGraphics : IGraphics
+{
+    public Graphics Graphics { get; set; }
+
+    private Font Font { get; set; }
+
+    public GdiGraphics(Graphics graphics)
+    {
+        Graphics = graphics;
+    }
+
+    public void DrawLine(Color color, float thickness, params PointF[] points)
+    {
+        if(points.Length == 2)
+        {
+            Graphics.DrawLine(new Pen(color, thickness), points[0], points[1]);
+        }
+        else
+        {
+            Graphics.DrawLines(new Pen(color, thickness), points);
+        }
+    }
+
+    public void DrawText(string text, Color color, PointF position)
+    {
+        Graphics.DrawString(text, Font, new SolidBrush(color), position, StringFormat.GenericTypographic);
+    }
+
+    public void FillPolygon(Color color, params PointF[] points)
+    {
+        Graphics.FillPolygon(new SolidBrush(color), points);
+    }
+
+    public void SetFont(string fontName, float fontSize, FontStyle fontStyle)
+    {
+        var fontFamily = new FontFamily(fontName);
+        var font = new Font(fontFamily, fontSize, fontStyle);
+        Font = font;
+    }
+
+    public void SetFont(float fontSize, FontStyle fontStyle)
+    {
+        var fontFamily = SystemFonts.DefaultFont.FontFamily;
+        var font = new Font(fontFamily, fontSize, fontStyle);
+        Font = font;
+    }
+
+    public void SetTransform(Matrix transform)
+    {
+        Graphics.Transform = transform;
     }
 }

+ 25 - 5
inabox.dxf/Objects.cs

@@ -28,7 +28,9 @@ internal class DxfLine : IDxfObject
 
         data.PushTransform();
         //data.ArbitraryAxis(Line.Normal);
-        data.Graphics.DrawLine(new Pen(Color.Black, data.ConvertThickness((float)Line.Thickness)), DrawData.ConvertPoint(Line.StartPoint), DrawData.ConvertPoint(Line.EndPoint));
+        data.Graphics.DrawLine(
+            Color.Black, data.ConvertThickness((float)Line.Thickness),
+            DrawData.ConvertPoint(Line.StartPoint), DrawData.ConvertPoint(Line.EndPoint));
         data.PopTransform();
     }
 
@@ -162,7 +164,7 @@ internal class DxfSolid : IDxfObject
             Solid.FourthVertex, // Apparently the third and fourth are the wrong way round, so I've mirrored that here.
             Solid.ThirdVertex
         };
-        data.Graphics.FillPolygon(new SolidBrush(Color.Black), vertices.ToArray(x => DrawData.ConvertPoint(x)));
+        data.Graphics.FillPolygon(Color.Black, vertices.ToArray(x => DrawData.ConvertPoint(x)));
     }
 
     public RectangleF? GetBounds(TransformData data)
@@ -317,12 +319,30 @@ internal class DxfMText : IDxfObject
     {
         if (!data.Data.ShouldDraw(MText)) return;
 
-        var font = GetFont();
         var text = GetText();
+        if (MText.Style.FontFamilyName.IsNullOrWhiteSpace())
+        {
+            data.Graphics.SetFont((float)MText.Height, MText.Style.FontStyle switch
+            {
+                netDxf.Tables.FontStyle.Bold => FontStyle.Bold,
+                netDxf.Tables.FontStyle.Italic => FontStyle.Italic,
+                netDxf.Tables.FontStyle.Regular or _ => FontStyle.Regular,
+            });
+        }
+        else
+        {
+            data.Graphics.SetFont(MText.Style.FontFamilyName, (float)MText.Height, MText.Style.FontStyle switch
+            {
+                netDxf.Tables.FontStyle.Bold => FontStyle.Bold,
+                netDxf.Tables.FontStyle.Italic => FontStyle.Italic,
+                netDxf.Tables.FontStyle.Regular or _ => FontStyle.Regular,
+            });
+        }
 
         data.PushTransform();
-        Transform(data, font, text, data.Graphics);
-        data.Graphics.DrawString(text, font, new SolidBrush(Color.Black), new PointF(0, 0), StringFormat.GenericTypographic);
+        using var g = Graphics.FromImage(_placeholderBitmap);
+        Transform(data, GetFont(), text, g);
+        data.Graphics.DrawText(text, Color.Black, new PointF(0, 0));
         data.PopTransform();
     }