// -------------------------------------------------------------------------------------------------------------------- // // 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.Drawing; using System.Drawing.Text; using System.Globalization; using System.Linq; using System.Text.RegularExpressions; using ImageProcessor.Imaging; using ImageProcessor.Processors; using ImageProcessor.Web.Extensions; 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); /// /// The regular expression to search strings for the text attribute. /// private static readonly Regex TextRegex = new Regex(@"(watermark=[^text-]|text-)[^/:?#\[\]@!$&'()*%\|,;=&]+", RegexOptions.Compiled); /// /// The regular expression to search strings for the position attribute. /// private static readonly Regex PositionRegex = new Regex(@"(text)?position(=|-)\d+[-,]\d+", RegexOptions.Compiled); /// /// The regular expression to search strings for the font size attribute. /// private static readonly Regex FontSizeRegex = new Regex(@"((font)?)size(=|-)\d{1,3}", RegexOptions.Compiled); /// /// The regular expression to search strings for the font style attribute. /// private static readonly Regex FontStyleRegex = new Regex(@"((font)?)style(=|-)(bold|italic|regular|strikeout|underline)", RegexOptions.Compiled); /// /// The regular expression to search strings for the font family attribute. /// private static readonly Regex FontFamilyRegex = new Regex(@"font(family)?(=|-)[^/:?#\[\]@!$&'()*%\|,;=0-9]+", RegexOptions.Compiled); /// /// The regular expression to search strings for the opacity attribute. /// private static readonly Regex OpacityRegex = new Regex(@"((font)?)opacity(=|-)(?:100|[1-9]?[0-9])", RegexOptions.Compiled); /// /// The regular expression to search strings for the shadow attribute. /// private static readonly Regex ShadowRegex = new Regex(@"((text|font|drop)?)shadow(=|-)true", RegexOptions.Compiled); /// /// The regular expression to search strings for the color attribute. /// private static readonly Regex ColorRegex = new Regex(@"color(=|-)[^&]+", 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) { 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; TextLayer textLayer = new TextLayer { Text = this.ParseText(queryString), Position = this.ParsePosition(queryString), FontColor = this.ParseColor(queryString), FontSize = this.ParseFontSize(queryString), FontFamily = this.ParseFontFamily(queryString), Style = this.ParseFontStyle(queryString), DropShadow = this.ParseDropShadow(queryString) }; textLayer.Opacity = this.ParseOpacity(queryString, textLayer.FontColor); this.Processor.DynamicParameter = textLayer; } index += 1; } } return this.SortOrder; } #region Private Methods /// /// Returns the correct for the given string. /// /// /// The input string containing the value to parse. /// /// /// The correct for the given string. /// private string ParseText(string input) { foreach (Match match in TextRegex.Matches(input)) { // split on text- return match.Value.Split(new[] { '=', '-' })[1].Replace("+", " "); } return string.Empty; } /// /// Returns the correct for the given string. /// /// /// The input string containing the value to parse. /// /// /// The correct /// private Point ParsePosition(string input) { foreach (Match match in PositionRegex.Matches(input)) { int[] position = match.Value.ToPositiveIntegerArray(); if (position != null) { int x = position[0]; int y = position[1]; return new Point(x, y); } } return Point.Empty; } /// /// Returns the correct for the given string. /// /// /// The input string containing the value to parse. /// /// /// The correct /// private Color ParseColor(string input) { foreach (Match match in ColorRegex.Matches(input)) { string value = match.Value.Split(new[] { '=', '-' })[1]; Color textColor = CommonParameterParserUtility.ParseColor(value); if (!textColor.Equals(Color.Transparent)) { return textColor; } } return Color.Black; } /// /// Returns the correct for the given string. /// /// /// The input string containing the value to parse. /// /// /// The correct /// private int ParseFontSize(string input) { foreach (Match match in FontSizeRegex.Matches(input)) { // split on size-value return int.Parse(match.Value.Split(new[] { '=', '-' })[1], CultureInfo.InvariantCulture); } // Matches the default number in TextLayer. return 48; } /// /// Returns the correct for the given string. /// /// /// The string containing the respective font style. /// /// /// The correct /// private FontStyle ParseFontStyle(string input) { FontStyle fontStyle = FontStyle.Bold; foreach (Match match in FontStyleRegex.Matches(input)) { // split on style- switch (match.Value.Split(new[] { '=', '-' })[1]) { case "italic": fontStyle = FontStyle.Italic; break; case "regular": fontStyle = FontStyle.Regular; break; case "strikeout": fontStyle = FontStyle.Strikeout; break; case "underline": fontStyle = FontStyle.Underline; break; } } return fontStyle; } /// /// Returns the correct containing the font family for the given string. /// /// /// The input string containing the value to parse. /// /// /// The correct containing the font family for the given string. /// private FontFamily ParseFontFamily(string input) { foreach (Match match in FontFamilyRegex.Matches(input)) { // split on font- string font = match.Value.Split(new[] { '=', '-' })[1].Replace("+", " "); return new FontFamily(font); } return new FontFamily(GenericFontFamilies.SansSerif); } /// /// Returns the correct containing the opacity for the given string. /// /// /// The input string containing the value to parse. /// /// /// The of the current . /// /// /// The correct containing the opacity for the given string. /// private int ParseOpacity(string input, Color color) { if (color.A < 255) { return (color.A / 255) * 100; } foreach (Match match in OpacityRegex.Matches(input)) { // Split on opacity- return int.Parse(match.Value.Split(new[] { '=', '-' })[1], CultureInfo.InvariantCulture); } // Full opacity - matches the TextLayer default. return 100; } /// /// Returns a value indicating whether the watermark is to have a shadow. /// /// /// The input string containing the value to parse. /// /// /// The true if the watermark is to have a shadow; otherwise false. /// private bool ParseDropShadow(string input) { return ShadowRegex.Matches(input).Cast().Any(); } #endregion } }