// Copyright (c) Six Labors.
// Licensed under the Six Labors Split License.
using System;
using System.Collections;
using System.Collections.Generic;
using System.Numerics;
namespace SixLabors.ImageSharp.Drawing {
///
/// A aggregate of s to apply common operations to them.
///
///
public class PathCollection : IPathCollection
{
private readonly IPath[] paths;
private RectangleF? bounds;
///
/// Initializes a new instance of the class.
///
/// The collection of paths
public PathCollection(IEnumerable paths)
: this(GetPathArray(paths))
{
}
///
/// Initializes a new instance of the class.
///
/// The collection of paths
public PathCollection(params IPath[] paths)
{
Guard.NotNull(paths, nameof(paths));
this.paths = paths;
if (paths.Length == 0)
{
this.bounds = new RectangleF(0, 0, 0, 0);
}
}
///
public RectangleF Bounds => this.bounds ??= this.CalcBounds();
private RectangleF CalcBounds()
{
float minX, minY, maxX, maxY;
minX = minY = float.MaxValue;
maxX = maxY = float.MinValue;
foreach (IPath path in this.paths)
{
RectangleF bounds = path.Bounds;
minX = Math.Min(bounds.Left, minX);
minY = Math.Min(bounds.Top, minY);
maxX = Math.Max(bounds.Right, maxX);
maxY = Math.Max(bounds.Bottom, maxY);
}
return new RectangleF(minX, minY, maxX - minX, maxY - minY);
}
///
public IEnumerator GetEnumerator() => ((IEnumerable)this.paths).GetEnumerator();
///
public IPathCollection Transform(Matrix4x4 matrix)
{
IPath[] result = new IPath[this.paths.Length];
for (int i = 0; i < this.paths.Length && i < result.Length; i++)
{
result[i] = this.paths[i].Transform(matrix);
}
return new PathCollection(result);
}
///
IEnumerator IEnumerable.GetEnumerator() => ((IEnumerable)this.paths).GetEnumerator();
private static IPath[] GetPathArray(IEnumerable paths)
{
Guard.NotNull(paths, nameof(paths));
return paths as IPath[] ?? [.. paths];
}
}
}