Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
Add support for .NET Standard 2.0 (#238)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhenX authored and twsouthwick committed Aug 13, 2018
1 parent a4232b9 commit cd9608f
Show file tree
Hide file tree
Showing 52 changed files with 185 additions and 261 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Expand Up @@ -36,3 +36,7 @@ TestResults/

# Visual Studio
.vs/

# JetBrains
.idea/
_ReSharper.Caches/
12 changes: 6 additions & 6 deletions OpenXmlPowerTools.Tests/OpenXmlPowerTools.Tests.csproj
@@ -1,24 +1,24 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>net452;net46</TargetFrameworks>
<TargetFrameworks>net452;net461;netcoreapp2.0</TargetFrameworks>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.8.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" />
<PackageReference Include="xunit" Version="2.3.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="xunit.runner.console" Version="2.3.1" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="xunit.runner.console" Version="2.4.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\OpenXmlPowerTools\OpenXmlPowerTools.csproj" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
<Reference Include="WindowsBase" />
</ItemGroup>
Expand Down

This file was deleted.

4 changes: 4 additions & 0 deletions OpenXmlPowerTools.Tests/PresentationBuilderTests.cs
Expand Up @@ -124,7 +124,11 @@ public void PB005_Formatting()
PresentationBuilder.BuildPresentation(sources, processedDestPptx.FullName);
}

#if NETCOREAPP2_0
[Fact(Skip="Bug in netcore 2.0 : https://github.com/OfficeDev/Open-Xml-PowerTools/pull/238#issuecomment-412375570")]
#else
[Fact]
#endif
public void PB006_VideoFormats()
{
// This presentation contains videos with content types video/mp4, video/quicktime, video/unknown, video/x-ms-asf, and video/x-msvideo.
Expand Down
4 changes: 2 additions & 2 deletions OpenXmlPowerTools.Tests/WmlComparerTests.cs
Expand Up @@ -171,8 +171,8 @@ public void WC001_Consolidate(string testId, string originalName, string revised
return new WmlRevisedDocumentInfo()
{
RevisedDocument = new WmlDocument(revisedCopiedToDestDocx.FullName),
Color = Color.FromName(z.Element("Color").Value),
Revisor = z.Element("Revisor").Value,
Color = ColorParser.FromName(z.Element("Color")?.Value),
Revisor = z.Element("Revisor")?.Value,
};
})
.ToList();
Expand Down
2 changes: 1 addition & 1 deletion OpenXmlPowerTools.Tests/WmlComparerTests2.cs
Expand Up @@ -457,7 +457,7 @@ public void WC001_Consolidate(string originalName, string revisedDocumentsXml)
return new WmlRevisedDocumentInfo()
{
RevisedDocument = new WmlDocument(revisedCopiedToDestDocx.FullName),
Color = Color.FromName(z.Element("Color").Value),
Color = ColorParser.FromName(z.Element("Color").Value),
Revisor = z.Element("Revisor").Value,
};
})
Expand Down
51 changes: 51 additions & 0 deletions OpenXmlPowerTools/ColorParser.cs
@@ -0,0 +1,51 @@
/***************************************************************************
Copyright (c) Microsoft Corporation 2012-2015.
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
Published at http://OpenXmlDeveloper.org
Resource Center and Documentation: http://openxmldeveloper.org/wiki/w/wiki/powertools-for-open-xml.aspx
Developer: Eric White
Blog: http://www.ericwhite.com
Twitter: @EricWhiteDev
Email: eric@ericwhite.com
***************************************************************************/

using System.Drawing;

namespace OpenXmlPowerTools
{
public static class ColorParser
{
public static Color FromName(string name)
{
return Color.FromName(name);
}

public static bool TryFromName(string name, out Color color)
{
try
{
color = Color.FromName(name);

return color.IsNamedColor;
}
catch
{
color = default(Color);

return false;
}
}

public static bool IsValidName(string name)
{
return TryFromName(name, out _);
}
}
}
1 change: 0 additions & 1 deletion OpenXmlPowerTools/HtmlToWmlConverter.cs
Expand Up @@ -29,7 +29,6 @@
using OpenXmlPowerTools.HtmlToWml;
using OpenXmlPowerTools.HtmlToWml.CSS;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OpenXmlPowerTools
{
Expand Down
61 changes: 1 addition & 60 deletions OpenXmlPowerTools/HtmlToWmlConverterCore.cs
Expand Up @@ -124,7 +124,6 @@
using OpenXmlPowerTools.HtmlToWml;
using OpenXmlPowerTools.HtmlToWml.CSS;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace OpenXmlPowerTools.HtmlToWml
{
Expand Down Expand Up @@ -1230,65 +1229,7 @@ private static XElement FontMerge(XElement higherPriorityFont, XElement lowerPri
runText = sb.ToString();
}

try
{
using (Font f = new Font(ff, (float)sz / 2f, fs))
{
const TextFormatFlags tff = TextFormatFlags.NoPadding;
var proposedSize = new Size(int.MaxValue, int.MaxValue);
var sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
// sf returns size in pixels
return sf.Width / multiplier;
}
}
catch (ArgumentException)
{
try
{
const FontStyle fs2 = FontStyle.Regular;
using (Font f = new Font(ff, (float)sz / 2f, fs2))
{
const TextFormatFlags tff = TextFormatFlags.NoPadding;
var proposedSize = new Size(int.MaxValue, int.MaxValue);
var sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
return sf.Width / multiplier;
}
}
catch (ArgumentException)
{
const FontStyle fs2 = FontStyle.Bold;
try
{
using (var f = new Font(ff, (float)sz / 2f, fs2))
{
const TextFormatFlags tff = TextFormatFlags.NoPadding;
var proposedSize = new Size(int.MaxValue, int.MaxValue);
var sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
// sf returns size in pixels
return sf.Width / multiplier;
}
}
catch (ArgumentException)
{
// if both regular and bold fail, then get metrics for Times New Roman
// use the original FontStyle (in fs)
var ff2 = new FontFamily("Times New Roman");
using (var f = new Font(ff2, (float)sz / 2f, fs))
{
const TextFormatFlags tff = TextFormatFlags.NoPadding;
var proposedSize = new Size(int.MaxValue, int.MaxValue);
var sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
// sf returns size in pixels
return sf.Width / multiplier;
}
}
}
}
catch (OverflowException)
{
// This happened on Azure but interestingly enough not while testing locally.
return 0;
}
return MetricsGetter.GetTextWidth(ff, fs, sz, runText) / multiplier;
}

// The algorithm for this method comes from the implementer notes in [MS-OI29500].pdf
Expand Down
17 changes: 4 additions & 13 deletions OpenXmlPowerTools/HtmlToWmlCssParser.cs
Expand Up @@ -808,12 +808,10 @@ public bool IsColor
}
if (number) { return false; }

try
if (ColorParser.IsValidName(m_value))
{
KnownColor kc = (KnownColor)Enum.Parse(typeof(KnownColor), m_value, true);
return true;
}
catch { }
}
return false;
}
Expand All @@ -835,13 +833,10 @@ public Color ToColor()
}
else
{
try
if (ColorParser.TryFromName(m_value, out var c))
{
KnownColor kc = (KnownColor)Enum.Parse(typeof(KnownColor), m_value, true);
Color c = Color.FromKnownColor(kc);
return c;
}
catch { }
}
int r = ConvertFromHex(hex.Substring(0, 2));
int g = ConvertFromHex(hex.Substring(2, 2));
Expand Down Expand Up @@ -1523,8 +1518,7 @@ public bool IsColor
return false;
}

KnownColor kc;
if (Enum.TryParse(m_val, true, out kc))
if (ColorParser.IsValidName(m_val))
{
return true;
}
Expand Down Expand Up @@ -1649,13 +1643,10 @@ public Color ToColor()
}
else
{
try
if (ColorParser.TryFromName(m_val, out var c))
{
KnownColor kc = (KnownColor)Enum.Parse(typeof(KnownColor), m_val, true);
Color c = Color.FromKnownColor(kc);
return c;
}
catch { }
}
if (hex.Length == 3)
{
Expand Down
60 changes: 60 additions & 0 deletions OpenXmlPowerTools/MetricsGetter.cs
Expand Up @@ -18,6 +18,7 @@

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.IO.Packaging;
using System.Linq;
Expand All @@ -39,6 +40,12 @@ public class MetricsGetterSettings

public class MetricsGetter
{
private static Lazy<Graphics> Graphics { get; } = new Lazy<Graphics>(() =>
{
Image image = new Bitmap(1, 1);
return System.Drawing.Graphics.FromImage(image);
});

public static XElement GetMetrics(string fileName, MetricsGetterSettings settings)
{
FileInfo fi = new FileInfo(fileName);
Expand Down Expand Up @@ -116,6 +123,59 @@ public static XElement GetDocxMetrics(WmlDocument wmlDoc, MetricsGetterSettings
return metrics;
}

private static int _getTextWidth(FontFamily ff, FontStyle fs, decimal sz, string text)
{
try
{
using (var f = new Font(ff, (float)sz / 2f, fs))
{
var proposedSize = new Size(int.MaxValue, int.MaxValue);
var sf = Graphics.Value.MeasureString(text, f, proposedSize);
return (int) sf.Width;
}
}
catch
{
return 0;
}
}

public static int GetTextWidth(FontFamily ff, FontStyle fs, decimal sz, string text)
{
try
{
return _getTextWidth(ff, fs, sz, text);
}
catch (ArgumentException)
{
try
{
const FontStyle fs2 = FontStyle.Regular;
return _getTextWidth(ff, fs2, sz, text);
}
catch (ArgumentException)
{
const FontStyle fs2 = FontStyle.Bold;
try
{
return _getTextWidth(ff, fs2, sz, text);
}
catch (ArgumentException)
{
// if both regular and bold fail, then get metrics for Times New Roman
// use the original FontStyle (in fs)
var ff2 = new FontFamily("Times New Roman");
return _getTextWidth(ff2, fs, sz, text);
}
}
}
catch (OverflowException)
{
// This happened on Azure but interestingly enough not while testing locally.
return 0;
}
}

private static Uri FixUri(string brokenUri)
{
return new Uri("http://broken-link/");
Expand Down
5 changes: 4 additions & 1 deletion OpenXmlPowerTools/OpenXmlPowerTools.csproj
@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>net45;net46</TargetFrameworks>
<TargetFrameworks>net45;net46;netstandard2.0</TargetFrameworks>
</PropertyGroup>

<ItemGroup>
Expand All @@ -18,4 +18,7 @@
<Reference Include="System.Windows.Forms" />
</ItemGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
<PackageReference Include="System.Drawing.Common" Version="4.5.0" />
</ItemGroup>
</Project>
3 changes: 0 additions & 3 deletions OpenXmlPowerTools/OpenXmlPowerTools.csproj.DotSettings

This file was deleted.

4 changes: 2 additions & 2 deletions OpenXmlPowerTools/OxPtHelpers.cs
Expand Up @@ -68,7 +68,7 @@ public static class AddDocxTextHelper

if (!string.IsNullOrEmpty(foreColor))
{
int colorValue = System.Drawing.Color.FromName(foreColor).ToArgb();
int colorValue = ColorParser.FromName(foreColor).ToArgb();
if (colorValue == 0)
throw new OpenXmlPowerToolsException(String.Format("Add-DocxText: The specified color {0} is unsupported, Please specify the valid color. Ex, Red, Green", foreColor));

Expand All @@ -81,7 +81,7 @@ public static class AddDocxTextHelper

if (!string.IsNullOrEmpty(backColor))
{
int colorShade = System.Drawing.Color.FromName(backColor).ToArgb();
int colorShade = ColorParser.FromName(backColor).ToArgb();
if (colorShade == 0)
throw new OpenXmlPowerToolsException(String.Format("Add-DocxText: The specified color {0} is unsupported, Please specify the valid color. Ex, Red, Green", foreColor));

Expand Down

0 comments on commit cd9608f

Please sign in to comment.