Skip to content

Commit

Permalink
Merge pull request #9 from jetelain/rounded-rectangle
Browse files Browse the repository at this point in the history
Support for rounded rectangle in drawing primitives
  • Loading branch information
jetelain authored May 11, 2024
2 parents cb93b13 + 47f24bc commit cbbe420
Show file tree
Hide file tree
Showing 9 changed files with 120 additions and 4 deletions.
2 changes: 2 additions & 0 deletions MapToolkit.Drawing/IDrawSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ IDrawTextStyle AllocateTextStyle(
void DrawText(Vector point, string text, IDrawTextStyle style);

void DrawIcon(Vector center, IDrawIcon icon);

void DrawRoundedRectangle(Vector topLeft, Vector bottomRight, IDrawStyle style, float radius);
}
}
22 changes: 22 additions & 0 deletions MapToolkit.Drawing/ImageRender/ImageSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,27 @@ public void DrawIcon(Vector center, IDrawIcon icon)
var iicon = (ImageIcon)icon;
target.DrawImage(iicon.Image, new Point( (int)(center.X - (iicon.Image.Width / 2)), (int)(center.Y - (iicon.Image.Height / 2))), 1);
}

public void DrawRoundedRectangle(Vector topLeft, Vector bottomRight, IDrawStyle style, float radius)
{
var istyle = (ImageStyle)style;

var path = new PathBuilder()
.AddArc((float)topLeft.X + radius, (float)topLeft.Y + radius, radius, radius, 0, -90, -90)
.AddArc((float)topLeft.X + radius, (float)bottomRight.Y - radius, radius, radius, 180, 0, -90)
.AddArc((float)bottomRight.X - radius, (float)bottomRight.Y - radius, radius, radius, 0, 90, -90)
.AddArc((float)bottomRight.X - radius, (float)topLeft.Y + radius, radius, radius, 180, 180, -90)
.CloseFigure()
.Build();

if (istyle.Brush != null)
{
target.Fill(istyle.Brush, path);
}
if (istyle.Pen != null)
{
target.Draw(istyle.Pen, path);
}
}
}
}
48 changes: 48 additions & 0 deletions MapToolkit.Drawing/MemoryRender/DrawRoundedRectangle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@



namespace MapToolkit.Drawing.MemoryRender
{
internal class DrawRoundedRectangle : IDrawOperation
{
public DrawRoundedRectangle(Vector topLeft, Vector bottomRight, MemDrawStyle style, float radius)
{
TopLeft = topLeft;
BottomRight = bottomRight;
Style = style;
Radius = radius;
}

public Vector TopLeft { get; }

public Vector BottomRight { get; }

public MemDrawStyle Style { get; }

public float Radius { get; }

public Vector Min => TopLeft;

public Vector Max => BottomRight;

public void Draw(MemDrawContext context)
{
context.Target.DrawRoundedRectangle(TopLeft, BottomRight, context.MapStyle(Style), Radius);
}

public void DrawClipped(MemDrawClipped context)
{
context.Target.DrawRoundedRectangle(TopLeft, BottomRight, context.MapStyle(Style), Radius);
}

public IDrawOperation Scale(MemDrawScale context)
{
return new DrawRoundedRectangle(TopLeft * context.Scale, BottomRight * context.Scale, context.MapStyle(Style), (float)(Radius * context.Scale));
}

public IEnumerable<IDrawOperation> Simplify(double lengthSquared)
{
yield return this;
}
}
}
5 changes: 5 additions & 0 deletions MapToolkit.Drawing/MemoryRender/MemorySurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -127,5 +127,10 @@ public void DrawIcon(Vector center, IDrawIcon icon)
{
Operations.Add(new DrawIcon(center, (MemDrawIcon)icon));
}

public void DrawRoundedRectangle(Vector topLeft, Vector bottomRight, IDrawStyle style, float radius)
{
Operations.Add(new DrawRoundedRectangle(topLeft, bottomRight, (MemDrawStyle)style, radius));
}
}
}
5 changes: 5 additions & 0 deletions MapToolkit.Drawing/MemoryRender/TranslateStylesSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ public void DrawPolyline(IEnumerable<Vector> points, IDrawStyle style)
throw new System.NotImplementedException();
}

public void DrawRoundedRectangle(Vector topLeft, Vector bottomRight, IDrawStyle style, float radius)
{
s.DrawRoundedRectangle(topLeft, bottomRight, memDrawContext.MapStyle((MemDrawStyle)style), radius);
}

public void DrawText(Vector point, string text, IDrawTextStyle style)
{
throw new System.NotImplementedException();
Expand Down
14 changes: 14 additions & 0 deletions MapToolkit.Drawing/PdfRender/PdfSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -231,5 +231,19 @@ public void DrawIcon(Vector center, IDrawIcon icon)
var top = center - (picon.Size / 2);
picon.Draw(new TranslateDraw(this, top.X, top.Y));
}

public void DrawRoundedRectangle(Vector topLeft, Vector bottomRight, IDrawStyle style, float radius)
{
var pstyle = (PdfStyle)style;

graphics.DrawRoundedRectangle(pstyle.Pen,
pstyle.Brush,
topLeft.X * pixelSize,
topLeft.Y * pixelSize,
(bottomRight.X - topLeft.X) * pixelSize,
(bottomRight.Y - topLeft.Y) * pixelSize,
radius * pixelSize,
radius * pixelSize);
}
}
}
4 changes: 2 additions & 2 deletions MapToolkit.Drawing/Render.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ namespace MapToolkit.Drawing
{
public static class Render
{
public static void ToSvg(string file, Vector size, Action<IDrawSurface> draw)
public static void ToSvg(string file, Vector size, Action<IDrawSurface> draw, string stylePrefix = "")
{
using (var surface = new SvgRender.SvgSurface(XmlWriter.Create(File.CreateText(file)), size, file))
using (var surface = new SvgRender.SvgSurface(XmlWriter.Create(File.CreateText(file), new XmlWriterSettings() { CloseOutput = true }), size, file, stylePrefix))
{
draw(surface);
}
Expand Down
19 changes: 17 additions & 2 deletions MapToolkit.Drawing/SvgRender/SvgSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ internal sealed class SvgSurface : IDrawSurface, IDisposable
private readonly int rounding = 1;
private bool isWrittingStyle = false;
private readonly StringBuilder styles = new StringBuilder();
private readonly string stylePrefix;

public SvgSurface(XmlWriter writer, Vector size, string? path = null)
public SvgSurface(XmlWriter writer, Vector size, string? path = null, string stylePrefix = "")
{
this.writer = writer;
this.path = path;
this.stylePrefix = stylePrefix;
StartSvg(size);
}

Expand All @@ -56,7 +58,7 @@ public IDrawStyle AllocateStyle(IBrush? fill, Pen? pen)

private string TakeStyleId()
{
return "s" + (nextStyleId++).ToString("x");
return stylePrefix + "s" + (nextStyleId++).ToString("x");
}

private void EndClass()
Expand Down Expand Up @@ -428,5 +430,18 @@ public void DrawIcon(Vector center, IDrawIcon icon)
var top = center - (sicon.Size / 2);
sicon.Draw(new TranslateDraw(this, top.X, top.Y));
}

public void DrawRoundedRectangle(Vector topLeft, Vector bottomRight, IDrawStyle style, float radius)
{
FlushStyles();
writer.WriteStartElement("rect", SvgXmlns);
writer.WriteAttributeString("x", ToString(topLeft.X));
writer.WriteAttributeString("y", ToString(topLeft.Y));
writer.WriteAttributeString("width", ToString(bottomRight.X - topLeft.X));
writer.WriteAttributeString("height", ToString(bottomRight.Y - topLeft.Y));
writer.WriteAttributeString("rx", ToString(radius));
writer.WriteAttributeString("class", ((SvgStyle)style).Name);
writer.WriteEndElement();
}
}
}
5 changes: 5 additions & 0 deletions MapToolkit.Drawing/TranslateDraw.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,10 @@ public void DrawIcon(Vector center, IDrawIcon icon)
{
drawSurface.DrawIcon(Translate(center), icon);
}

public void DrawRoundedRectangle(Vector topLeft, Vector bottomRight, IDrawStyle style, float radius)
{
drawSurface.DrawRoundedRectangle(Translate(topLeft), Translate(bottomRight), style, radius);
}
}
}

0 comments on commit cbbe420

Please sign in to comment.