// --------------------------------------------------------------------------------------------------------------------
//
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
//
//
// Encapsulates methods to add a watermark text overlay to an image.
//
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System.Collections.Specialized;
using System.Drawing;
using System.Drawing.Text;
using System.Text.RegularExpressions;
using System.Web;
using ImageProcessor.Imaging;
using ImageProcessor.Processors;
using ImageProcessor.Web.Helpers;
///
/// Encapsulates methods to add a watermark text overlay to an image.
///
public class Watermark : IWebGraphicsProcessor
{
///
/// The regular expression to search strings for.
///
private static readonly Regex QueryRegex = new Regex(@"watermark=", RegexOptions.Compiled);
///
/// Initializes a new instance of the class.
///
public Watermark()
{
this.Processor = new ImageProcessor.Processors.Watermark();
}
///
/// 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)
{
this.SortOrder = int.MaxValue;
Match match = this.RegexPattern.Match(queryString);
if (match.Success)
{
this.SortOrder = match.Index;
NameValueCollection queryCollection = HttpUtility.ParseQueryString(queryString);
TextLayer textLayer = new TextLayer
{
Text = this.ParseText(queryCollection),
Position = this.ParsePosition(queryCollection),
FontColor = this.ParseColor(queryCollection),
FontSize = this.ParseFontSize(queryCollection),
FontFamily = this.ParseFontFamily(queryCollection),
Style = this.ParseFontStyle(queryCollection),
DropShadow = this.ParseDropShadow(queryCollection)
};
textLayer.Opacity = this.ParseOpacity(queryCollection, textLayer.FontColor);
this.Processor.DynamicParameter = textLayer;
}
return this.SortOrder;
}
#region Private Methods
///
/// Returns the correct for the given parameter collection.
///
///
/// The of query parameters.
///
///
/// The correct .
///
private string ParseText(NameValueCollection queryCollection)
{
return QueryParamParser.Instance.ParseValue(queryCollection["watermark"]);
}
///
/// Returns the correct for the given parameter collection.
///
///
/// The of query parameters.
///
///
/// The correct
///
private Point? ParsePosition(NameValueCollection queryCollection)
{
return queryCollection["textposition"] != null
? QueryParamParser.Instance.ParseValue(queryCollection["textposition"])
: (Point?)null;
}
///
/// Returns the correct for the given parameter collection.
///
///
/// The of query parameters.
///
///
/// The correct
///
private Color ParseColor(NameValueCollection queryCollection)
{
return queryCollection["color"] != null
? QueryParamParser.Instance.ParseValue(queryCollection["color"])
: Color.Black;
}
///
/// Returns the correct for the given parameter collection.
///
///
/// The of query parameters.
///
///
/// The correct
///
private int ParseFontSize(NameValueCollection queryCollection)
{
return queryCollection["fontsize"] != null
? QueryParamParser.Instance.ParseValue(queryCollection["fontsize"])
: 48;
}
///
/// Returns the correct for the given parameter collection.
///
///
/// The of query parameters.
///
///
/// The correct
///
private FontStyle ParseFontStyle(NameValueCollection queryCollection)
{
return queryCollection["fontstyle"] != null
? QueryParamParser.Instance.ParseValue(queryCollection["fontstyle"])
: FontStyle.Bold;
}
///
/// Returns the correct for the given parameter collection.
///
///
/// The of query parameters.
///
///
/// The correct .
///
private FontFamily ParseFontFamily(NameValueCollection queryCollection)
{
return queryCollection["fontfamily"] != null
? QueryParamParser.Instance.ParseValue(queryCollection["fontfamily"])
: new FontFamily(GenericFontFamilies.SansSerif);
}
///
/// Returns a value indicating whether the watermark is to have a shadow.
///
///
/// The of query parameters.
///
///
/// The true if the watermark is to have a shadow; otherwise false.
///
private bool ParseDropShadow(NameValueCollection queryCollection)
{
return QueryParamParser.Instance.ParseValue(queryCollection["dropshadow"]);
}
///
/// Returns the correct containing the opacity for the parameter collection.
///
///
/// The of query parameters.
///
///
/// The of the current .
///
///
/// The correct .
///
private int ParseOpacity(NameValueCollection queryCollection, Color color)
{
if (color.A < 255)
{
return (color.A / 255) * 100;
}
return queryCollection["fontopacity"] != null
? QueryParamParser.Instance.ParseValue(queryCollection["fontopacity"])
: 100;
}
#endregion
}
}