//
// Copyright (c) James Jackson-South and contributors.
// Licensed under the Apache License, Version 2.0.
//
namespace ImageSharp.Drawing
{
using System;
using System.Buffers;
using System.Numerics;
using SixLabors.Shapes;
using Rectangle = ImageSharp.Rectangle;
///
/// A mapping between a and a region.
///
internal class ShapeRegion : Region
{
///
/// Initializes a new instance of the class.
///
/// The shape.
public ShapeRegion(IPath shape)
{
this.Shape = shape.AsClosedPath();
this.Bounds = shape.Bounds.Convert();
}
///
/// Gets the fillable shape
///
public IPath Shape { get; }
///
public override int MaxIntersections => this.Shape.MaxIntersections;
///
public override Rectangle Bounds { get; }
///
public override int Scan(float y, Span buffer)
{
Vector2 start = new Vector2(this.Bounds.Left - 1, y);
Vector2 end = new Vector2(this.Bounds.Right + 1, y);
Vector2[] innerbuffer = ArrayPool.Shared.Rent(buffer.Length);
try
{
int count = this.Shape.FindIntersections(start, end, innerbuffer, buffer.Length, 0);
for (int i = 0; i < count; i++)
{
buffer[i] = innerbuffer[i].X;
}
return count;
}
finally
{
ArrayPool.Shared.Return(innerbuffer);
}
}
}
}