// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// Encapsulates methods to add rounded corners to an image.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System.Globalization;
using System.Text.RegularExpressions;
using ImageProcessor.Imaging;
using ImageProcessor.Processors;
///
/// Encapsulates methods to add rounded corners to an image.
///
public class RoundedCorners : IWebGraphicsProcessor
{
///
/// The regular expression to search strings for.
///
private static readonly Regex QueryRegex = new Regex(@"roundedcorners=[^&]+", RegexOptions.Compiled);
///
/// The regular expression to search strings for the angle attribute.
///
private static readonly Regex RadiusRegex = new Regex(@"(roundedcorners|radius)(=|-)(\d+)", RegexOptions.Compiled);
///
/// The regular expression to search strings for the top left attribute.
///
private static readonly Regex TopLeftRegex = new Regex(@"tl(=|-)(true|false)", RegexOptions.Compiled);
///
/// The regular expression to search strings for the top right attribute.
///
private static readonly Regex TopRightRegex = new Regex(@"tr(=|-)(true|false)", RegexOptions.Compiled);
///
/// The regular expression to search strings for the bottom left attribute.
///
private static readonly Regex BottomLeftRegex = new Regex(@"bl(=|-)(true|false)", RegexOptions.Compiled);
///
/// The regular expression to search strings for the bottom right attribute.
///
private static readonly Regex BottomRightRegex = new Regex(@"br(=|-)(true|false)", RegexOptions.Compiled);
///
/// Initializes a new instance of the class.
///
public RoundedCorners()
{
this.Processor = new ImageProcessor.Processors.RoundedCorners();
}
///
/// Gets the regular expression to search strings for.
///
public Regex RegexPattern
{
get
{
return QueryRegex;
}
}
///
/// Gets the order in which this processor is to be used in a chain.
///
public int SortOrder { get; private set; }
///
/// Gets the associated graphics processor.
///
public IGraphicsProcessor Processor { get; private set; }
///
/// The position in the original string where the first character of the captured substring was found.
///
/// The query string to search.
///
/// The zero-based starting position in the original string where the captured substring was found.
///
public int MatchRegexIndex(string queryString)
{
int index = 0;
// Set the sort order to max to allow filtering.
this.SortOrder = int.MaxValue;
foreach (Match match in this.RegexPattern.Matches(queryString))
{
if (match.Success)
{
if (index == 0)
{
// Set the index on the first instance only.
this.SortOrder = match.Index;
RoundedCornerLayer roundedCornerLayer = new RoundedCornerLayer(
this.ParseRadius(queryString),
this.ParseCorner(TopLeftRegex, queryString),
this.ParseCorner(TopRightRegex, queryString),
this.ParseCorner(BottomLeftRegex, queryString),
this.ParseCorner(BottomRightRegex, queryString));
this.Processor.DynamicParameter = roundedCornerLayer;
}
index += 1;
}
}
return this.SortOrder;
}
#region Private Methods
///
/// Returns the correct containing the radius for the given string.
///
///
/// The input string containing the value to parse.
///
///
/// The correct containing the radius for the given string.
///
private int ParseRadius(string input)
{
foreach (Match match in RadiusRegex.Matches(input))
{
// Split on radius-
int radius;
int.TryParse(match.Value.Split(new[] { '=', '-' })[1], NumberStyles.Any, CultureInfo.InvariantCulture, out radius);
return radius;
}
// No corners - matches the RoundedCorner default.
return 0;
}
///
/// Returns a either true or false.
///
///
/// The corner.
///
///
/// The input string containing the value to parse.
///
///
/// The correct true or false.
///
private bool ParseCorner(Regex corner, string input)
{
foreach (Match match in corner.Matches(input))
{
// Split on corner-
bool cornerRound;
bool.TryParse(match.Value.Split(new[] { '=', '-' })[1], out cornerRound);
return cornerRound;
}
// No corners - matches the RoundedCorner default.
return true;
}
#endregion
}
}