Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion ACadSharp.Image.Tests/SvgExportTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void RenderSvgProducesSvgPage()
}

[Fact]
public void PointRadiusIsConvertedFromPixelsIntoDrawingUnits()
public void ScalingPointRadiusIsConvertedFromPixelsIntoDrawingUnits()
{
BlockRecord block = new("points");
block.Entities.Add(new Line(new XYZ(0, 0, 0), new XYZ(100, 50, 0)));
Expand All @@ -55,6 +55,7 @@ public void PointRadiusIsConvertedFromPixelsIntoDrawingUnits()
exporter.Configuration.Width = 1000;
exporter.Configuration.Height = 500;
exporter.Configuration.SetPadding(0);
exporter.Configuration.Svg.NonScalingStroke = false;
exporter.Add(block);

using RenderedPage page = Assert.Single(exporter.Render(ImageExportFormat.Svg));
Expand All @@ -66,6 +67,32 @@ public void PointRadiusIsConvertedFromPixelsIntoDrawingUnits()
Assert.Equal("0.2", (string?)circle.Attribute("r"));
}

[Theory]
[InlineData(100)]
[InlineData(216101.36407904)]
public void NonScalingPointMarkerKeepsItsPixelDiameterIndependentOfDrawingExtents(double extent)
{
BlockRecord block = new("point-marker");
block.Entities.Add(new Line(new XYZ(0, 0, 0), new XYZ(extent, 100, 0)));
block.Entities.Add(new Point { Location = new XYZ(extent / 2, 50, 0) });
ImageExporter exporter = new();
exporter.Configuration.Width = 2000;
exporter.Configuration.Height = 2000;
exporter.Configuration.SetPadding(0);
exporter.Add(block);

using RenderedPage page = Assert.Single(exporter.Render(ImageExportFormat.Svg));
XElement marker = Assert.Single(XDocument.Parse(((RenderedSvgPage)page).Content).Descendants(),
element => element.Attribute("data-type")?.Value == "POINT");
Assert.Equal("line", marker.Name.LocalName);
Assert.Equal(marker.Attribute("x1")?.Value, marker.Attribute("x2")?.Value);
Assert.Equal(marker.Attribute("y1")?.Value, marker.Attribute("y2")?.Value);
Assert.Equal("4", marker.Attribute("stroke-width")?.Value);
Assert.Equal("round", marker.Attribute("stroke-linecap")?.Value);
Assert.Equal("none", marker.Attribute("stroke-dasharray")?.Value);
Assert.Equal("non-scaling-stroke", marker.Attribute("vector-effect")?.Value);
}

[Fact]
public void YAxisIsFlipped()
{
Expand Down
2 changes: 1 addition & 1 deletion ACadSharp.Image/Rendering/EntityRenderDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,7 @@ private void DrawPoint(ImageRenderContext context, ImageStyle style, ACadSharp.E
{
// DotSizePixels is a pixel size; SVG surface units are drawing units, so it has to be converted.
float radius = Math.Max(1f, this._configuration.DotSizePixels / 2f);
context.Surface.FillCircle(style, context.ToSurfacePoint(point.Location), context.ToSurfacePixels(radius));
context.Surface.DrawPointMarker(style, context.ToSurfacePoint(point.Location), context.ToSurfacePixels(radius), radius);
}

/// <summary>
Expand Down
10 changes: 10 additions & 0 deletions ACadSharp.Image/Rendering/IDrawingSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@ internal interface IDrawingSurface : IDisposable

void FillCircle(ImageStyle style, SurfacePoint center, double radius);

/// <summary>
/// Draws a point marker, retaining its pixel size on interactive SVG surfaces.
/// </summary>
/// <param name="style">Marker colour and opacity.</param>
/// <param name="center">Marker centre in surface coordinates.</param>
/// <param name="radius">Radius in surface units for raster and scaling output.</param>
/// <param name="radiusPixels">Radius in display pixels for non-scaling output.</param>
void DrawPointMarker(ImageStyle style, SurfacePoint center, double radius, double radiusPixels)
=> this.FillCircle(style, center, radius);

void DrawText(ImageStyle style, SurfaceText text);

/// <summary>
Expand Down
27 changes: 27 additions & 0 deletions ACadSharp.Image/Rendering/Svg/SvgDrawingSurface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,33 @@ public void FillCircle(ImageStyle style, SurfacePoint center, double radius)
new XAttribute("cx", this.N(center.X)), new XAttribute("cy", this.N(center.Y)), new XAttribute("r", this.N(Math.Abs(radius)))), style));
}

public void DrawPointMarker(ImageStyle style, SurfacePoint center, double radius, double radiusPixels)
{
if (!this._options.NonScalingStroke)
{
this.FillCircle(style, center, radius);
return;
}

if (!IsFinite(center) || !IsFinite(radiusPixels) || radiusPixels <= 0d)
{
this.NotifyNonFinite();
return;
}

// A zero-length round-capped stroke is a dot. Unlike a filled circle,
// its non-scaling stroke keeps the configured diameter during zoom.
ImageStyle markerStyle = style with { StrokeWidth = (float)(2d * radiusPixels), DashPattern = null };
XElement marker = new(Ns + "line",
new XAttribute("x1", this.N(center.X)), new XAttribute("y1", this.N(center.Y)),
new XAttribute("x2", this.N(center.X)), new XAttribute("y2", this.N(center.Y)),
new XAttribute("stroke-linecap", "round"));
marker = this.Stroked(marker, markerStyle);
marker.SetAttributeValue("stroke-width", this.S(markerStyle.StrokeWidth));
marker.SetAttributeValue("stroke-dasharray", "none");
this.Append(marker);
}

public void DrawText(ImageStyle style, SurfaceText text)
{
if (string.IsNullOrWhiteSpace(text.Text))
Expand Down
Loading