From 934949725b3e17e43d34dddc936e3cc315e0cea6 Mon Sep 17 00:00:00 2001 From: Clint Rutkas Date: Mon, 21 Sep 2020 19:44:12 -0700 Subject: [PATCH] FxCop and StyleCop for SVG thumbnail (#6757) * FxCop work for SvgThumbnail * enabling stylecop --- installer/PowerToysSetup/Product.wxs | 6 +- .../SvgThumbnailProvider.cs | 117 +++++++++--------- .../SvgThumbnailProvider.csproj | 74 ++++++++++- .../SvgThumbnailProvider/packages.config | 9 ++ .../SvgThumbnailProviderTests.cs | 19 +-- 5 files changed, 156 insertions(+), 69 deletions(-) create mode 100644 src/modules/previewpane/SvgThumbnailProvider/packages.config diff --git a/installer/PowerToysSetup/Product.wxs b/installer/PowerToysSetup/Product.wxs index 59c1b28f89..8d57f38edd 100644 --- a/installer/PowerToysSetup/Product.wxs +++ b/installer/PowerToysSetup/Product.wxs @@ -438,18 +438,18 @@ - + - + - + diff --git a/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs b/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs index 8930516a37..925091ae7b 100644 --- a/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs +++ b/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.cs @@ -1,24 +1,22 @@ // Copyright (c) Microsoft Corporation // The Microsoft Corporation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. +using System; +using System.Drawing; +using System.Drawing.Drawing2D; +using System.Globalization; +using System.IO; +using System.Linq; +using System.Runtime.InteropServices; +using System.Runtime.InteropServices.ComTypes; +using System.Windows.Forms; +using Common.ComInterlop; +using Common.Utilities; +using Microsoft.PowerToys.Telemetry; +using PreviewHandlerCommon; -namespace SvgThumbnailProvider +namespace Microsoft.PowerToys.ThumbnailHandler.Svg { - using System; - using System.Drawing; - using System.Drawing.Drawing2D; - using System.Drawing.Imaging; - using System.IO; - using System.Linq; - using System.Runtime.InteropServices; - using System.Runtime.InteropServices.ComTypes; - using System.Windows.Forms; - using Common; - using Common.ComInterlop; - using Common.Utilities; - using Microsoft.PowerToys.Telemetry; - using PreviewHandlerCommon; - /// /// SVG Thumbnail Provider. /// @@ -64,7 +62,7 @@ namespace SvgThumbnailProvider { deviceContextHandle = graphics.GetHdc(); - IViewObject viewObject = browser.ActiveXInstance as IViewObject; + IViewObject viewObject = browser?.ActiveXInstance as IViewObject; viewObject.Draw(1, -1, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, deviceContextHandle, ref rect, IntPtr.Zero, IntPtr.Zero, 0); } finally @@ -100,30 +98,16 @@ namespace SvgThumbnailProvider // Wrap the SVG content in HTML in IE Edge mode so we can ensure // we render properly. string wrappedContent = WrapSVGInHTML(content); - WebBrowserExt browser = new WebBrowserExt(); - browser.Dock = DockStyle.Fill; - browser.IsWebBrowserContextMenuEnabled = false; - browser.ScriptErrorsSuppressed = true; - browser.ScrollBarsEnabled = false; - browser.AllowNavigation = false; - browser.Width = (int)cx; - browser.Height = (int)cx; - browser.DocumentText = wrappedContent; - - // Wait for the browser to render the content. - while (browser.IsBusy || browser.ReadyState != WebBrowserReadyState.Complete) + using (WebBrowserExt browser = new WebBrowserExt()) { - Application.DoEvents(); - } - - // Check size of the rendered SVG. - var svg = browser.Document.GetElementsByTagName("svg").Cast().FirstOrDefault(); - if (svg != null) - { - // Update the size of the browser control to fit the SVG - // in the visible viewport. - browser.Width = svg.OffsetRectangle.Width; - browser.Height = svg.OffsetRectangle.Height; + browser.Dock = DockStyle.Fill; + browser.IsWebBrowserContextMenuEnabled = false; + browser.ScriptErrorsSuppressed = true; + browser.ScrollBarsEnabled = false; + browser.AllowNavigation = false; + browser.Width = (int)cx; + browser.Height = (int)cx; + browser.DocumentText = wrappedContent; // Wait for the browser to render the content. while (browser.IsBusy || browser.ReadyState != WebBrowserReadyState.Complete) @@ -131,16 +115,32 @@ namespace SvgThumbnailProvider Application.DoEvents(); } - // Capture the image of the SVG from the browser. - thumbnail = GetBrowserContentImage(browser, svg.OffsetRectangle, Color.White); - if (thumbnail.Width != cx && thumbnail.Height != cx) + // Check size of the rendered SVG. + var svg = browser.Document.GetElementsByTagName("svg").Cast().FirstOrDefault(); + if (svg != null) { - // We are not the appropriate size for caller. Resize now while - // respecting the aspect ratio. - float scale = Math.Min((float)cx / thumbnail.Width, (float)cx / thumbnail.Height); - int scaleWidth = (int)(thumbnail.Width * scale); - int scaleHeight = (int)(thumbnail.Height * scale); - thumbnail = ResizeImage(thumbnail, scaleWidth, scaleHeight); + // Update the size of the browser control to fit the SVG + // in the visible viewport. + browser.Width = svg.OffsetRectangle.Width; + browser.Height = svg.OffsetRectangle.Height; + + // Wait for the browser to render the content. + while (browser.IsBusy || browser.ReadyState != WebBrowserReadyState.Complete) + { + Application.DoEvents(); + } + + // Capture the image of the SVG from the browser. + thumbnail = GetBrowserContentImage(browser, svg.OffsetRectangle, Color.White); + if (thumbnail.Width != cx && thumbnail.Height != cx) + { + // We are not the appropriate size for caller. Resize now while + // respecting the aspect ratio. + float scale = Math.Min((float)cx / thumbnail.Width, (float)cx / thumbnail.Height); + int scaleWidth = (int)(thumbnail.Width * scale); + int scaleHeight = (int)(thumbnail.Height * scale); + thumbnail = ResizeImage(thumbnail, scaleWidth, scaleHeight); + } } } @@ -166,7 +166,7 @@ namespace SvgThumbnailProvider {0} "; - return string.Format(html, svg); + return string.Format(CultureInfo.CurrentCulture, html, svg); } /// @@ -178,8 +178,11 @@ namespace SvgThumbnailProvider /// The resized image. public static Bitmap ResizeImage(Image image, int width, int height) { - if (width <= 0 || height <= 0 || - width > MaxThumbnailSize || height > MaxThumbnailSize) + if (width <= 0 || + height <= 0 || + width > MaxThumbnailSize || + height > MaxThumbnailSize || + image == null) { return null; } @@ -232,11 +235,13 @@ namespace SvgThumbnailProvider if (svgData != null) { - Bitmap thumbnail = GetThumbnail(svgData, cx); - if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0) + using (Bitmap thumbnail = GetThumbnail(svgData, cx)) { - phbmp = thumbnail.GetHbitmap(); - pdwAlpha = WTS_ALPHATYPE.WTSAT_RGB; + if (thumbnail != null && thumbnail.Size.Width > 0 && thumbnail.Size.Height > 0) + { + phbmp = thumbnail.GetHbitmap(); + pdwAlpha = WTS_ALPHATYPE.WTSAT_RGB; + } } } } diff --git a/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.csproj b/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.csproj index 56a26dce10..8b1170d7be 100644 --- a/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.csproj +++ b/src/modules/previewpane/SvgThumbnailProvider/SvgThumbnailProvider.csproj @@ -1,17 +1,58 @@  + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Debug AnyCPU {8FFE09DA-FA4F-4EE1-B3A2-AD5497FBD1AD} Library - SVGThumbnailProvider + Microsoft.PowerToys.ThumbnailHandler.Svg SVGThumbnailProvider + SVGThumbnailProvider + PowerToys SvgPreviewHandler + Microsoft Corporation + Copyright (C) 2020 Microsoft Corporation + PowerToys v4.7.2 512 true true + + x64 @@ -40,10 +81,12 @@ + true x64 ..\..\..\..\x64\Debug\modules\FileExplorerPreview\ + true x64 ..\..\..\..\x64\Release\modules\FileExplorerPreview\ @@ -65,6 +108,9 @@ GlobalSuppressions.cs + + Code + @@ -84,5 +130,31 @@ StyleCop.json + + + + + + + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Use NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + + + \ No newline at end of file diff --git a/src/modules/previewpane/SvgThumbnailProvider/packages.config b/src/modules/previewpane/SvgThumbnailProvider/packages.config new file mode 100644 index 0000000000..727f3814ea --- /dev/null +++ b/src/modules/previewpane/SvgThumbnailProvider/packages.config @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs b/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs index 92b2db8d7c..06ca112f27 100644 --- a/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs +++ b/src/modules/previewpane/UnitTests-SvgThumbnailProvider/SvgThumbnailProviderTests.cs @@ -9,6 +9,7 @@ using System.Runtime.InteropServices.ComTypes; using System.Text; using Common.ComInterlop; using Microsoft.VisualStudio.TestTools.UnitTesting; +using Microsoft.PowerToys.ThumbnailHandler.Svg; using Moq; namespace SvgThumbnailProviderUnitTests @@ -25,7 +26,7 @@ namespace SvgThumbnailProviderUnitTests svgBuilder.AppendLine("\t"); svgBuilder.AppendLine(""); - Bitmap thumbnail = SvgThumbnailProvider.SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); + Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); Assert.IsTrue(thumbnail != null); } @@ -39,7 +40,7 @@ namespace SvgThumbnailProviderUnitTests svgBuilder.AppendLine("\t"); svgBuilder.AppendLine(""); - Bitmap thumbnail = SvgThumbnailProvider.SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); + Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); Assert.IsTrue(thumbnail != null); } @@ -49,21 +50,21 @@ namespace SvgThumbnailProviderUnitTests var svgBuilder = new StringBuilder(); svgBuilder.AppendLine("

foo

"); - Bitmap thumbnail = SvgThumbnailProvider.SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); + Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); Assert.IsTrue(thumbnail == null); } [TestMethod] public void CheckNoSvgEmptyStringShouldReturnNullBitmap() { - Bitmap thumbnail = SvgThumbnailProvider.SvgThumbnailProvider.GetThumbnail(string.Empty, 256); + Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(string.Empty, 256); Assert.IsTrue(thumbnail == null); } [TestMethod] public void CheckNoSvgNullStringShouldReturnNullBitmap() { - Bitmap thumbnail = SvgThumbnailProvider.SvgThumbnailProvider.GetThumbnail(null, 256); + Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(null, 256); Assert.IsTrue(thumbnail == null); } @@ -71,7 +72,7 @@ namespace SvgThumbnailProviderUnitTests public void CheckZeroSizedThumbnailShouldReturnNullBitmap() { string content = ""; - Bitmap thumbnail = SvgThumbnailProvider.SvgThumbnailProvider.GetThumbnail(content, 0); + Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(content, 0); Assert.IsTrue(thumbnail == null); } @@ -92,7 +93,7 @@ namespace SvgThumbnailProviderUnitTests svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); - Bitmap thumbnail = SvgThumbnailProvider.SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); + Bitmap thumbnail = SvgThumbnailProvider.GetThumbnail(svgBuilder.ToString(), 256); Assert.IsTrue(thumbnail != null); } @@ -105,7 +106,7 @@ namespace SvgThumbnailProviderUnitTests svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); - SvgThumbnailProvider.SvgThumbnailProvider provider = new SvgThumbnailProvider.SvgThumbnailProvider(); + SvgThumbnailProvider provider = new SvgThumbnailProvider(); provider.Initialize(GetMockStream(svgBuilder.ToString()), 0); @@ -134,7 +135,7 @@ namespace SvgThumbnailProviderUnitTests svgBuilder.AppendLine(""); svgBuilder.AppendLine(""); - SvgThumbnailProvider.SvgThumbnailProvider provider = new SvgThumbnailProvider.SvgThumbnailProvider(); + SvgThumbnailProvider provider = new SvgThumbnailProvider(); provider.Initialize(GetMockStream(svgBuilder.ToString()), 0);