From b466d400f9fd6e41f9768eab6dd30d7dfa49df8a Mon Sep 17 00:00:00 2001 From: James South Date: Mon, 25 Aug 2014 17:46:06 +0100 Subject: [PATCH] Tweaking Flip and Watermark Former-commit-id: 2b324f703944194940e98c5957433424c30deea2 --- .../ImageFactoryUnitTests.cs | 2 +- .../RegularExpressionUnitTests.cs | 10 +++--- .../Processors/Watermark.cs | 17 +++++----- src/ImageProcessor/ImageFactory.cs | 29 +++++++++++++++- .../Imaging/RoundedCornerLayer.cs | 2 +- src/ImageProcessor/Imaging/TextLayer.cs | 33 ++++++++++++------- src/ImageProcessor/Processors/Watermark.cs | 14 ++++---- 7 files changed, 72 insertions(+), 35 deletions(-) diff --git a/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs b/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs index 4d905d3ba..6412226b5 100644 --- a/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs +++ b/src/ImageProcessor.UnitTests/ImageFactoryUnitTests.cs @@ -246,7 +246,7 @@ namespace ImageProcessor.UnitTests Image original = (Image)imageFactory.Image.Clone(); imageFactory.Watermark(new Imaging.TextLayer { - Font = "Arial", + FontFamily = new FontFamily("Arial"), FontSize = 10, Position = new Point(10, 10), Text = "Lorem ipsum dolor" diff --git a/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs b/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs index de544edc1..2f66b1057 100644 --- a/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs +++ b/src/ImageProcessor.Web.UnitTests/RegularExpressionUnitTests.cs @@ -437,27 +437,27 @@ namespace ImageProcessor.Web.UnitTests new TextLayer { Text = "watermark goodness", - TextColor = ColorTranslator.FromHtml("#" + "ffffff"), + FontColor = ColorTranslator.FromHtml("#" + "ffffff"), FontSize = 36, Style = FontStyle.Italic, Opacity = 80, Position = new Point(30, 150), DropShadow = true, - Font = "arial" + FontFamily = new FontFamily("arial") } }, { - "watermark=watermark goodness&color=fff&fontsize=36&fontstyle=italic&fontopacity=80&textposition=30,150&textshadow=true&font=arial", + "watermark=watermark goodness&color=fff&fontsize=36&fontstyle=italic&fontopacity=80&textposition=30,150&textshadow=true&fontfamily=arial", new TextLayer { Text = "watermark goodness", - TextColor = ColorTranslator.FromHtml("#" + "ffffff"), + FontColor = ColorTranslator.FromHtml("#" + "ffffff"), FontSize = 36, Style = FontStyle.Italic, Opacity = 80, Position = new Point(30, 150), DropShadow = true, - Font = "arial" + FontFamily = new FontFamily("arial") } } }; diff --git a/src/ImageProcessor.Web/Processors/Watermark.cs b/src/ImageProcessor.Web/Processors/Watermark.cs index a2cc28770..0e2afa126 100644 --- a/src/ImageProcessor.Web/Processors/Watermark.cs +++ b/src/ImageProcessor.Web/Processors/Watermark.cs @@ -11,6 +11,7 @@ namespace ImageProcessor.Web.Processors { using System.Drawing; + using System.Drawing.Text; using System.Globalization; using System.Linq; using System.Text.RegularExpressions; @@ -53,7 +54,7 @@ namespace ImageProcessor.Web.Processors /// /// The regular expression to search strings for the font family attribute. /// - private static readonly Regex FontFamilyRegex = new Regex(@"font(=|-)[^/:?#\[\]@!$&'()*%\|,;=0-9]+", RegexOptions.Compiled); + private static readonly Regex FontFamilyRegex = new Regex(@"font(family)?(=|-)[^/:?#\[\]@!$&'()*%\|,;=0-9]+", RegexOptions.Compiled); /// /// The regular expression to search strings for the opacity attribute. @@ -63,7 +64,7 @@ namespace ImageProcessor.Web.Processors /// /// The regular expression to search strings for the shadow attribute. /// - private static readonly Regex ShadowRegex = new Regex(@"((text)?)shadow(=|-)true", RegexOptions.Compiled); + private static readonly Regex ShadowRegex = new Regex(@"((text|font|drop)?)shadow(=|-)true", RegexOptions.Compiled); /// /// Initializes a new instance of the class. @@ -123,14 +124,14 @@ namespace ImageProcessor.Web.Processors { Text = this.ParseText(queryString), Position = this.ParsePosition(queryString), - TextColor = this.ParseColor(queryString), + FontColor = this.ParseColor(queryString), FontSize = this.ParseFontSize(queryString), - Font = this.ParseFontFamily(queryString), + FontFamily = this.ParseFontFamily(queryString), Style = this.ParseFontStyle(queryString), DropShadow = this.ParseDropShadow(queryString) }; - textLayer.Opacity = this.ParseOpacity(queryString, textLayer.TextColor); + textLayer.Opacity = this.ParseOpacity(queryString, textLayer.FontColor); this.Processor.DynamicParameter = textLayer; } @@ -279,17 +280,17 @@ namespace ImageProcessor.Web.Processors /// /// The correct containing the font family for the given string. /// - private string ParseFontFamily(string input) + private FontFamily ParseFontFamily(string input) { foreach (Match match in FontFamilyRegex.Matches(input)) { // split on font- string font = match.Value.Split(new[] { '=', '-' })[1].Replace("+", " "); - return font; + return new FontFamily(font); } - return string.Empty; + return new FontFamily(GenericFontFamilies.SansSerif); } /// diff --git a/src/ImageProcessor/ImageFactory.cs b/src/ImageProcessor/ImageFactory.cs index ab4494956..6b87c0ca9 100644 --- a/src/ImageProcessor/ImageFactory.cs +++ b/src/ImageProcessor/ImageFactory.cs @@ -470,7 +470,7 @@ namespace ImageProcessor /// /// The current instance of the class. /// - public ImageFactory Flip(bool flipVertically) + public ImageFactory Flip(bool flipVertically = false) { if (this.ShouldProcess) { @@ -687,6 +687,33 @@ namespace ImageProcessor return this; } + /// + /// Adds rounded corners to the current image. + /// + /// + /// The radius at which the corner will be rounded. + /// + /// + /// The current instance of the class. + /// + public ImageFactory RoundedCorners(int radius) + { + if (this.ShouldProcess) + { + if (radius < 0) + { + radius = 0; + } + + RoundedCornerLayer roundedCornerLayer = new RoundedCornerLayer(radius); + + RoundedCorners roundedCorners = new RoundedCorners { DynamicParameter = roundedCornerLayer }; + this.CurrentImageFormat.ApplyProcessor(roundedCorners.ProcessImage, this); + } + + return this; + } + /// /// Adds rounded corners to the current image. /// diff --git a/src/ImageProcessor/Imaging/RoundedCornerLayer.cs b/src/ImageProcessor/Imaging/RoundedCornerLayer.cs index 19e514f77..05606d4b2 100644 --- a/src/ImageProcessor/Imaging/RoundedCornerLayer.cs +++ b/src/ImageProcessor/Imaging/RoundedCornerLayer.cs @@ -19,7 +19,7 @@ namespace ImageProcessor.Imaging /// Initializes a new instance of the class. /// /// - /// The radius at which the corner will be done. + /// The radius at which the corner will be rounded. /// /// /// Set if top left is rounded diff --git a/src/ImageProcessor/Imaging/TextLayer.cs b/src/ImageProcessor/Imaging/TextLayer.cs index 501bb2aa3..490b7f95e 100644 --- a/src/ImageProcessor/Imaging/TextLayer.cs +++ b/src/ImageProcessor/Imaging/TextLayer.cs @@ -10,11 +10,8 @@ namespace ImageProcessor.Imaging { - #region Using - - using System; using System.Drawing; - #endregion + using System.Drawing.Text; /// /// Encapsulates the properties required to add a layer of text to an image. @@ -37,6 +34,11 @@ namespace ImageProcessor.Imaging /// private FontStyle fontStyle = FontStyle.Regular; + /// + /// The font family to render the text. + /// + private FontFamily fontFamily = new FontFamily(GenericFontFamilies.SansSerif); + /// /// The font size to render the text. /// @@ -55,21 +57,28 @@ namespace ImageProcessor.Imaging public string Text { get; set; } /// - /// Gets or sets the Color to render the font. + /// Gets or sets the to render the font. /// /// Defaults to black. /// /// - public Color TextColor + public Color FontColor { get { return this.textColor; } set { this.textColor = value; } } /// - /// Gets or sets the name of the font. + /// Gets or sets the name of the font family. + /// + /// Defaults to generic sans-serif font family. + /// /// - public string Font { get; set; } + public FontFamily FontFamily + { + get { return this.fontFamily; } + set { this.fontFamily = value; } + } /// /// Gets or sets the size of the font in pixels. @@ -141,8 +150,8 @@ namespace ImageProcessor.Imaging } return this.Text == textLayer.Text - && this.TextColor == textLayer.TextColor - && this.Font == textLayer.Font + && this.FontColor == textLayer.FontColor + && this.FontFamily.Equals(textLayer.FontFamily) && this.FontSize == textLayer.FontSize && this.Style == textLayer.Style && this.DropShadow == textLayer.DropShadow @@ -159,8 +168,8 @@ namespace ImageProcessor.Imaging public override int GetHashCode() { return this.Text.GetHashCode() + - this.TextColor.GetHashCode() + - this.Font.GetHashCode() + + this.FontColor.GetHashCode() + + this.FontFamily.GetHashCode() + this.FontSize.GetHashCode() + this.Style.GetHashCode() + this.DropShadow.GetHashCode() + diff --git a/src/ImageProcessor/Processors/Watermark.cs b/src/ImageProcessor/Processors/Watermark.cs index 7e449281c..e14ed7cb5 100644 --- a/src/ImageProcessor/Processors/Watermark.cs +++ b/src/ImageProcessor/Processors/Watermark.cs @@ -75,11 +75,11 @@ namespace ImageProcessor.Processors using (Graphics graphics = Graphics.FromImage(newImage)) { - using (Font font = this.GetFont(textLayer.Font, fontSize, fontStyle)) + using (Font font = this.GetFont(textLayer.FontFamily, fontSize, fontStyle)) { using (StringFormat drawFormat = new StringFormat()) { - using (Brush brush = new SolidBrush(Color.FromArgb(opacity, textLayer.TextColor))) + using (Brush brush = new SolidBrush(Color.FromArgb(opacity, textLayer.FontColor))) { Point origin = textLayer.Position; @@ -148,7 +148,7 @@ namespace ImageProcessor.Processors /// /// Returns the correct for the given parameters. /// - /// + /// /// The name of the font. /// /// @@ -160,20 +160,20 @@ namespace ImageProcessor.Processors /// /// The correct /// - private Font GetFont(string font, int fontSize, FontStyle fontStyle) + private Font GetFont(FontFamily fontFamily, int fontSize, FontStyle fontStyle) { try { - using (FontFamily fontFamily = new FontFamily(font)) + using (fontFamily) { return new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Pixel); } } catch { - using (FontFamily fontFamily = FontFamily.GenericSansSerif) + using (FontFamily genericFontFamily = FontFamily.GenericSansSerif) { - return new Font(fontFamily, fontSize, fontStyle, GraphicsUnit.Pixel); + return new Font(genericFontFamily, fontSize, fontStyle, GraphicsUnit.Pixel); } } }