Browse Source

Adding watermark and tweaking regexes

Former-commit-id: c5876edc4982cef520bac0029d1e0e3d0aa82037
pull/17/head
James South 12 years ago
parent
commit
c0744b3df2
  1. 5
      src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs
  2. 1
      src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj
  3. 2
      src/ImageProcessor.Web/NET45/Processors/Alpha.cs
  4. 2
      src/ImageProcessor.Web/NET45/Processors/BackgroundColor.cs
  5. 2
      src/ImageProcessor.Web/NET45/Processors/Brightness.cs
  6. 2
      src/ImageProcessor.Web/NET45/Processors/Contrast.cs
  7. 2
      src/ImageProcessor.Web/NET45/Processors/Crop.cs
  8. 2
      src/ImageProcessor.Web/NET45/Processors/GaussianBlur.cs
  9. 2
      src/ImageProcessor.Web/NET45/Processors/GaussianSharpen.cs
  10. 2
      src/ImageProcessor.Web/NET45/Processors/Quality.cs
  11. 2
      src/ImageProcessor.Web/NET45/Processors/Rotate.cs
  12. 2
      src/ImageProcessor.Web/NET45/Processors/Saturation.cs
  13. 2
      src/ImageProcessor.Web/NET45/Processors/Tint.cs
  14. 2
      src/ImageProcessor.Web/NET45/Processors/Vignette.cs
  15. 339
      src/ImageProcessor.Web/NET45/Processors/Watermark.cs
  16. 3
      src/ImageProcessor/Imaging/Formats/FormatUtilities.cs
  17. 317
      src/ImageProcessor/Processors/Watermark.cs

5
src/ImageProcessor.Web/NET45/HttpModules/ImageProcessingModule.cs

@ -49,7 +49,7 @@ namespace ImageProcessor.Web.HttpModules
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex PresetRegex = new Regex(@"preset=[^&]*", RegexOptions.Compiled);
private static readonly Regex PresetRegex = new Regex(@"preset=[^&]+", RegexOptions.Compiled);
/// <summary>
/// The assembly version.
@ -407,6 +407,9 @@ namespace ImageProcessor.Web.HttpModules
{
responseStream.CopyTo(memoryStream);
// Reset the position of the stream to ensure we're reading the correct part.
memoryStream.Position = 0;
// Process the Image
imageFactory.Load(memoryStream)
.AddQueryString(queryString)

1
src/ImageProcessor.Web/NET45/ImageProcessor.Web_NET45.csproj

@ -78,6 +78,7 @@
<Compile Include="Processors\Saturation.cs" />
<Compile Include="Processors\Tint.cs" />
<Compile Include="Processors\Vignette.cs" />
<Compile Include="Processors\Watermark.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>

2
src/ImageProcessor.Web/NET45/Processors/Alpha.cs

@ -13,7 +13,7 @@
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"alpha=[^&|,]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"alpha=[^&|,]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Alpha"/> class.

2
src/ImageProcessor.Web/NET45/Processors/BackgroundColor.cs

@ -22,7 +22,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"bgcolor(=|-)[^&]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"bgcolor(=|-)[^&]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="BackgroundColor"/> class.

2
src/ImageProcessor.Web/NET45/Processors/Brightness.cs

@ -22,7 +22,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"brightness=[^&|,]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"brightness=[^&|,]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Brightness"/> class.

2
src/ImageProcessor.Web/NET45/Processors/Contrast.cs

@ -22,7 +22,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"contrast=[^&|,]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"contrast=[^&|,]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Contrast"/> class.

2
src/ImageProcessor.Web/NET45/Processors/Crop.cs

@ -25,7 +25,7 @@ namespace ImageProcessor.Web.Processors
/// The regular expression to search strings for.
/// <see cref="http://stackoverflow.com/a/6400969/427899"/>
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"(crop=|cropmode=)[^&]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"(crop=|cropmode=)[^&]+", RegexOptions.Compiled);
/// <summary>
/// The coordinate regex.

2
src/ImageProcessor.Web/NET45/Processors/GaussianBlur.cs

@ -23,7 +23,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"blur=[^&]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"blur=[^&]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="GaussianBlur"/> class.

2
src/ImageProcessor.Web/NET45/Processors/GaussianSharpen.cs

@ -23,7 +23,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"sharpen=[^&]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"sharpen=[^&]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="GaussianSharpen"/> class.

2
src/ImageProcessor.Web/NET45/Processors/Quality.cs

@ -23,7 +23,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"quality=[^&|,]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"quality=[^&|,]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Quality"/> class.

2
src/ImageProcessor.Web/NET45/Processors/Rotate.cs

@ -22,7 +22,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"(rotate|angle)(=|-)[^&|,]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"(rotate|angle)(=|-)[^&|,]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Rotate"/> class.

2
src/ImageProcessor.Web/NET45/Processors/Saturation.cs

@ -25,7 +25,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"saturation=[^&|,]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"saturation=[^&|,]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Saturation"/> class.

2
src/ImageProcessor.Web/NET45/Processors/Tint.cs

@ -22,7 +22,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"tint=[^&]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"tint=[^&]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Tint"/> class.

2
src/ImageProcessor.Web/NET45/Processors/Vignette.cs

@ -23,7 +23,7 @@ namespace ImageProcessor.Web.Processors
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"vignette=(true)?[^&]*", RegexOptions.Compiled);
private static readonly Regex QueryRegex = new Regex(@"vignette(=true)?[^&]+", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Vignette"/> class.

339
src/ImageProcessor.Web/NET45/Processors/Watermark.cs

@ -0,0 +1,339 @@
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Watermark.cs" company="James South">
// Copyright (c) James South.
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods to add a watermark text overlay to an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Web.Processors
{
using System.Drawing;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using ImageProcessor.Core.Common.Extensions;
using ImageProcessor.Imaging;
using ImageProcessor.Processors;
using ImageProcessor.Web.Helpers;
/// <summary>
/// Encapsulates methods to add a watermark text overlay to an image.
/// </summary>
public class Watermark : IWebGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"watermark=[^&]+", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the text attribute.
/// </summary>
private static readonly Regex TextRegex = new Regex(@"(watermark=[^text-]|text-)[^/:?#\[\]@!$&'()*%\|,;=&]+", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the position attribute.
/// </summary>
private static readonly Regex PositionRegex = new Regex(@"(text)?position(=|-)\d+[-,]\d+", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the font size attribute.
/// </summary>
private static readonly Regex FontSizeRegex = new Regex(@"((font)?)size(=|-)\d{1,3}", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the font style attribute.
/// </summary>
private static readonly Regex FontStyleRegex = new Regex(@"((font)?)style(=|-)(bold|italic|regular|strikeout|underline)", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the font family attribute.
/// </summary>
private static readonly Regex FontFamilyRegex = new Regex(@"font(=|-)[^/:?#\[\]@!$&'()*%\|,;=0-9]+", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the opacity attribute.
/// </summary>
private static readonly Regex OpacityRegex = new Regex(@"((font)?)opacity(=|-)(?:100|[1-9]?[0-9])", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the shadow attribute.
/// </summary>
private static readonly Regex ShadowRegex = new Regex(@"((text)?)shadow(=|-)true", RegexOptions.Compiled);
/// <summary>
/// Initializes a new instance of the <see cref="Watermark"/> class.
/// </summary>
public Watermark()
{
this.Processor = new ImageProcessor.Processors.Watermark();
}
/// <summary>
/// Gets the regular expression to search strings for.
/// </summary>
public Regex RegexPattern
{
get
{
return QueryRegex;
}
}
/// <summary>
/// Gets the order in which this processor is to be used in a chain.
/// </summary>
public int SortOrder { get; private set; }
/// <summary>
/// Gets the associated graphics processor.
/// </summary>
public IGraphicsProcessor Processor { get; private set; }
/// <summary>
/// The position in the original string where the first character of the captured substring was found.
/// </summary>
/// <param name="queryString">
/// The query string to search.
/// </param>
/// <returns>
/// The zero-based starting position in the original string where the captured substring was found.
/// </returns>
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),
TextColor = this.ParseColor(queryString),
FontSize = this.ParseFontSize(queryString),
Font = this.ParseFontFamily(queryString),
Style = this.ParseFontStyle(queryString),
DropShadow = this.ParseDropShadow(queryString)
};
textLayer.Opacity = this.ParseOpacity(queryString, textLayer.TextColor);
this.Processor.DynamicParameter = textLayer;
}
index += 1;
}
}
return this.SortOrder;
}
#region Private Methods
/// <summary>
/// Returns the correct <see cref="T:System.String"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.String"/> for the given string.
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="T:System.Drawing.Point"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Drawing.Point"/>
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="T:System.Drawing.Color"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Drawing.Color"/>
/// </returns>
private Color ParseColor(string input)
{
Color textColor = CommonParameterParserUtility.ParseColor(input);
if (!textColor.Equals(Color.Transparent))
{
return textColor;
}
return Color.Black;
}
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Int32"/>
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="T:System.Drawing.FontStyle"/> for the given string.
/// </summary>
/// <param name="input">
/// The string containing the respective font style.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Drawing.FontStyle"/>
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="T:System.String"/> containing the font family for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.String"/> containing the font family for the given string.
/// </returns>
private string ParseFontFamily(string input)
{
foreach (Match match in FontFamilyRegex.Matches(input))
{
// split on font-
string font = match.Value.Split(new[] { '=', '-' })[1].Replace("+", " ");
return font;
}
return string.Empty;
}
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> containing the opacity for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <param name="color">
/// The <see cref="T:System.Drawing.Color"/> of the current <see cref="TextLayer"/>.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Int32"/> containing the opacity for the given string.
/// </returns>
private int ParseOpacity(string input, Color color)
{
if (color.A < 255)
{
return color.A;
}
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;
}
/// <summary>
/// Returns a value indicating whether the watermark is to have a shadow.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The true if the watermark is to have a shadow; otherwise false.
/// </returns>
private bool ParseDropShadow(string input)
{
return ShadowRegex.Matches(input).Cast<Match>().Any();
}
#endregion
}
}

3
src/ImageProcessor/Imaging/Formats/FormatUtilities.cs

@ -34,6 +34,9 @@ namespace ImageProcessor.Imaging.Formats
/// </returns>
public static ISupportedImageFormat GetFormat(Stream stream)
{
// Reset the position of the stream to ensure we're reading the correct part.
stream.Position = 0;
IEnumerable<ISupportedImageFormat> supportedImageFormats =
ImageProcessorBootstrapper.Instance.SupportedImageFormats;

317
src/ImageProcessor/Processors/Watermark.cs

@ -4,88 +4,23 @@
// Licensed under the Apache License, Version 2.0.
// </copyright>
// <summary>
// Encapsulates methods to change the alpha component of the image to effect its transparency.
// Encapsulates methods to add a watermark text overlay to an image.
// </summary>
// --------------------------------------------------------------------------------------------------------------------
namespace ImageProcessor.Processors
{
#region Using
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Text;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using ImageProcessor.Core.Common.Extensions;
using ImageProcessor.Imaging;
#endregion
/// <summary>
/// Encapsulates methods to change the alpha component of the image to effect its transparency.
/// Encapsulates methods to add a watermark text overlay to an image.
/// </summary>
public class Watermark : IGraphicsProcessor
{
/// <summary>
/// The regular expression to search strings for.
/// </summary>
private static readonly Regex QueryRegex = new Regex(@"watermark=[^&]*", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the text attribute.
/// </summary>
private static readonly Regex TextRegex = new Regex(@"text-[^/:?#\[\]@!$&'()*%\|,;=]+", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the position attribute.
/// </summary>
private static readonly Regex PositionRegex = new Regex(@"position-\d+[-,]\d+", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the color attribute.
/// </summary>
private static readonly Regex ColorRegex = new Regex(@"color-([0-9a-fA-F]{3}){1,2}", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the font size attribute.
/// </summary>
private static readonly Regex FontSizeRegex = new Regex(@"size-\d{1,3}", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the font style attribute.
/// </summary>
private static readonly Regex FontStyleRegex = new Regex(@"style-(bold|italic|regular|strikeout|underline)", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the font family attribute.
/// </summary>
private static readonly Regex FontFamilyRegex = new Regex(@"font-[^/:?#\[\]@!$&'()*%\|,;=0-9]+", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the opacity attribute.
/// </summary>
private static readonly Regex OpacityRegex = new Regex(@"opacity-(?:100|[1-9]?[0-9])", RegexOptions.Compiled);
/// <summary>
/// The regular expression to search strings for the shadow attribute.
/// </summary>
private static readonly Regex ShadowRegex = new Regex(@"shadow-true", RegexOptions.Compiled);
#region IGraphicsProcessor Members
/// <summary>
/// Gets the regular expression to search strings for.
/// </summary>
public Regex RegexPattern
{
get
{
return QueryRegex;
}
}
/// <summary>
/// Gets or sets DynamicParameter.
/// </summary>
@ -95,15 +30,6 @@ namespace ImageProcessor.Processors
set;
}
/// <summary>
/// Gets the order in which this processor is to be used in a chain.
/// </summary>
public int SortOrder
{
get;
private set;
}
/// <summary>
/// Gets or sets any additional settings required by the processor.
/// </summary>
@ -113,54 +39,6 @@ namespace ImageProcessor.Processors
set;
}
/// <summary>
/// The position in the original string where the first character of the captured substring was found.
/// </summary>
/// <param name="queryString">
/// The query string to search.
/// </param>
/// <returns>
/// The zero-based starting position in the original string where the captured substring was found.
/// </returns>
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();
string toParse = match.Value;
textLayer.Text = this.ParseText(toParse);
textLayer.Position = this.ParsePosition(toParse);
textLayer.TextColor = this.ParseColor(toParse);
textLayer.FontSize = this.ParseFontSize(toParse);
textLayer.Font = this.ParseFontFamily(toParse);
textLayer.Style = this.ParseFontStyle(toParse);
textLayer.Opacity = this.ParseOpacity(toParse);
textLayer.DropShadow = this.ParseDropShadow(toParse);
this.DynamicParameter = textLayer;
}
index += 1;
}
}
return this.SortOrder;
}
/// <summary>
/// Processes the image.
/// </summary>
@ -254,20 +132,18 @@ namespace ImageProcessor.Processors
return image;
}
#endregion
#region Private Methods
/// <summary>
/// Returns the correct <see cref="T:System.Drawing.Font"/> for the given parameters
/// Returns the correct <see cref="T:System.Drawing.Font"/> for the given parameters.
/// </summary>
/// <param name="font">
/// The font.
/// The name of the font.
/// </param>
/// <param name="fontSize">
/// The font size.
/// </param>
/// <param name="fontStyle">
/// The font style.
/// The <see cref="T:System.Drawing.FontStyle"/> style.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Drawing.Font"/>
@ -289,188 +165,5 @@ namespace ImageProcessor.Processors
}
}
}
/// <summary>
/// Returns the correct <see cref="T:System.String"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.String"/> for the given string.
/// </returns>
private string ParseText(string input)
{
foreach (Match match in TextRegex.Matches(input))
{
// split on text-
return match.Value.Split('-')[1].Replace("+", " ");
}
return string.Empty;
}
/// <summary>
/// Returns the correct <see cref="T:System.Drawing.Point"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Drawing.Point"/>
/// </returns>
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;
}
/// <summary>
/// Returns the correct <see cref="T:System.Drawing.Color"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Drawing.Color"/>
/// </returns>
private Color ParseColor(string input)
{
foreach (Match match in ColorRegex.Matches(input))
{
// split on color-hex
return ColorTranslator.FromHtml("#" + match.Value.Split('-')[1]);
}
return Color.Black;
}
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Int32"/>
/// </returns>
private int ParseFontSize(string input)
{
foreach (Match match in FontSizeRegex.Matches(input))
{
// split on size-value
return int.Parse(match.Value.Split('-')[1], CultureInfo.InvariantCulture);
}
// Matches the default number in TextLayer.
return 48;
}
/// <summary>
/// Returns the correct <see cref="T:System.Drawing.FontStyle"/> for the given string.
/// </summary>
/// <param name="input">
/// The string containing the respective font style.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Drawing.FontStyle"/>
/// </returns>
private FontStyle ParseFontStyle(string input)
{
FontStyle fontStyle = FontStyle.Bold;
foreach (Match match in FontStyleRegex.Matches(input))
{
// split on style-
switch (match.Value.Split('-')[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;
}
/// <summary>
/// Returns the correct <see cref="T:System.String"/> containing the font family for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.String"/> containing the font family for the given string.
/// </returns>
private string ParseFontFamily(string input)
{
foreach (Match match in FontFamilyRegex.Matches(input))
{
// split on font-
string font = match.Value.Split('-')[1].Replace("+", " ");
return font;
}
return string.Empty;
}
/// <summary>
/// Returns the correct <see cref="T:System.Int32"/> containing the opacity for the given string.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The correct <see cref="T:System.Int32"/> containing the opacity for the given string.
/// </returns>
private int ParseOpacity(string input)
{
foreach (Match match in OpacityRegex.Matches(input))
{
// split on opacity-
return int.Parse(match.Value.Split('-')[1], CultureInfo.InvariantCulture);
}
// full opacity - matches the Textlayer default.
return 100;
}
/// <summary>
/// Returns a value indicating whether the watermark is to have a shadow.
/// </summary>
/// <param name="input">
/// The input string containing the value to parse.
/// </param>
/// <returns>
/// The true if the watermark is to have a shadow; otherwise false.
/// </returns>
private bool ParseDropShadow(string input)
{
return ShadowRegex.Matches(input).Cast<Match>().Any();
}
#endregion
}
}

Loading…
Cancel
Save