Skip to content

Commit cd9608f

Browse files
PhenXtwsouthwick
authored andcommitted
Add support for .NET Standard 2.0 (#238)
1 parent a4232b9 commit cd9608f

File tree

52 files changed

+185
-261
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+185
-261
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,7 @@ TestResults/
3636

3737
# Visual Studio
3838
.vs/
39+
40+
# JetBrains
41+
.idea/
42+
_ReSharper.Caches/

OpenXmlPowerTools.Tests/OpenXmlPowerTools.Tests.csproj

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net452;net46</TargetFrameworks>
4+
<TargetFrameworks>net452;net461;netcoreapp2.0</TargetFrameworks>
55
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
66
<GenerateBindingRedirectsOutputType>true</GenerateBindingRedirectsOutputType>
77
<GenerateRuntimeConfigurationFiles>true</GenerateRuntimeConfigurationFiles>
88
</PropertyGroup>
99

1010
<ItemGroup>
1111
<PackageReference Include="DocumentFormat.OpenXml" Version="2.8.1" />
12-
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.6.0" />
13-
<PackageReference Include="xunit" Version="2.3.1" />
14-
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
15-
<PackageReference Include="xunit.runner.console" Version="2.3.1" />
12+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.7.2" />
13+
<PackageReference Include="xunit" Version="2.4.0" />
14+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
15+
<PackageReference Include="xunit.runner.console" Version="2.4.0" />
1616
</ItemGroup>
1717

1818
<ItemGroup>
1919
<ProjectReference Include="..\OpenXmlPowerTools\OpenXmlPowerTools.csproj" />
2020
</ItemGroup>
21-
21+
2222
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">
2323
<Reference Include="WindowsBase" />
2424
</ItemGroup>

OpenXmlPowerTools.Tests/OpenXmlPowerTools.Tests.csproj.DotSettings

Lines changed: 0 additions & 2 deletions
This file was deleted.

OpenXmlPowerTools.Tests/PresentationBuilderTests.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,11 @@ public void PB005_Formatting()
124124
PresentationBuilder.BuildPresentation(sources, processedDestPptx.FullName);
125125
}
126126

127+
#if NETCOREAPP2_0
128+
[Fact(Skip="Bug in netcore 2.0 : https://github.com/OfficeDev/Open-Xml-PowerTools/pull/238#issuecomment-412375570")]
129+
#else
127130
[Fact]
131+
#endif
128132
public void PB006_VideoFormats()
129133
{
130134
// This presentation contains videos with content types video/mp4, video/quicktime, video/unknown, video/x-ms-asf, and video/x-msvideo.

OpenXmlPowerTools.Tests/WmlComparerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ public void WC001_Consolidate(string testId, string originalName, string revised
171171
return new WmlRevisedDocumentInfo()
172172
{
173173
RevisedDocument = new WmlDocument(revisedCopiedToDestDocx.FullName),
174-
Color = Color.FromName(z.Element("Color").Value),
175-
Revisor = z.Element("Revisor").Value,
174+
Color = ColorParser.FromName(z.Element("Color")?.Value),
175+
Revisor = z.Element("Revisor")?.Value,
176176
};
177177
})
178178
.ToList();

OpenXmlPowerTools.Tests/WmlComparerTests2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ public void WC001_Consolidate(string originalName, string revisedDocumentsXml)
457457
return new WmlRevisedDocumentInfo()
458458
{
459459
RevisedDocument = new WmlDocument(revisedCopiedToDestDocx.FullName),
460-
Color = Color.FromName(z.Element("Color").Value),
460+
Color = ColorParser.FromName(z.Element("Color").Value),
461461
Revisor = z.Element("Revisor").Value,
462462
};
463463
})

OpenXmlPowerTools/ColorParser.cs

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/***************************************************************************
2+
3+
Copyright (c) Microsoft Corporation 2012-2015.
4+
5+
This code is licensed using the Microsoft Public License (Ms-PL). The text of the license can be found here:
6+
7+
http://www.microsoft.com/resources/sharedsource/licensingbasics/publiclicense.mspx
8+
9+
Published at http://OpenXmlDeveloper.org
10+
Resource Center and Documentation: http://openxmldeveloper.org/wiki/w/wiki/powertools-for-open-xml.aspx
11+
12+
Developer: Eric White
13+
Blog: http://www.ericwhite.com
14+
Twitter: @EricWhiteDev
15+
16+
17+
***************************************************************************/
18+
19+
using System.Drawing;
20+
21+
namespace OpenXmlPowerTools
22+
{
23+
public static class ColorParser
24+
{
25+
public static Color FromName(string name)
26+
{
27+
return Color.FromName(name);
28+
}
29+
30+
public static bool TryFromName(string name, out Color color)
31+
{
32+
try
33+
{
34+
color = Color.FromName(name);
35+
36+
return color.IsNamedColor;
37+
}
38+
catch
39+
{
40+
color = default(Color);
41+
42+
return false;
43+
}
44+
}
45+
46+
public static bool IsValidName(string name)
47+
{
48+
return TryFromName(name, out _);
49+
}
50+
}
51+
}

OpenXmlPowerTools/HtmlToWmlConverter.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
using OpenXmlPowerTools.HtmlToWml;
3030
using OpenXmlPowerTools.HtmlToWml.CSS;
3131
using System.Text.RegularExpressions;
32-
using System.Windows.Forms;
3332

3433
namespace OpenXmlPowerTools
3534
{

OpenXmlPowerTools/HtmlToWmlConverterCore.cs

Lines changed: 1 addition & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@
124124
using OpenXmlPowerTools.HtmlToWml;
125125
using OpenXmlPowerTools.HtmlToWml.CSS;
126126
using System.Text.RegularExpressions;
127-
using System.Windows.Forms;
128127

129128
namespace OpenXmlPowerTools.HtmlToWml
130129
{
@@ -1230,65 +1229,7 @@ private static XElement FontMerge(XElement higherPriorityFont, XElement lowerPri
12301229
runText = sb.ToString();
12311230
}
12321231

1233-
try
1234-
{
1235-
using (Font f = new Font(ff, (float)sz / 2f, fs))
1236-
{
1237-
const TextFormatFlags tff = TextFormatFlags.NoPadding;
1238-
var proposedSize = new Size(int.MaxValue, int.MaxValue);
1239-
var sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
1240-
// sf returns size in pixels
1241-
return sf.Width / multiplier;
1242-
}
1243-
}
1244-
catch (ArgumentException)
1245-
{
1246-
try
1247-
{
1248-
const FontStyle fs2 = FontStyle.Regular;
1249-
using (Font f = new Font(ff, (float)sz / 2f, fs2))
1250-
{
1251-
const TextFormatFlags tff = TextFormatFlags.NoPadding;
1252-
var proposedSize = new Size(int.MaxValue, int.MaxValue);
1253-
var sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
1254-
return sf.Width / multiplier;
1255-
}
1256-
}
1257-
catch (ArgumentException)
1258-
{
1259-
const FontStyle fs2 = FontStyle.Bold;
1260-
try
1261-
{
1262-
using (var f = new Font(ff, (float)sz / 2f, fs2))
1263-
{
1264-
const TextFormatFlags tff = TextFormatFlags.NoPadding;
1265-
var proposedSize = new Size(int.MaxValue, int.MaxValue);
1266-
var sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
1267-
// sf returns size in pixels
1268-
return sf.Width / multiplier;
1269-
}
1270-
}
1271-
catch (ArgumentException)
1272-
{
1273-
// if both regular and bold fail, then get metrics for Times New Roman
1274-
// use the original FontStyle (in fs)
1275-
var ff2 = new FontFamily("Times New Roman");
1276-
using (var f = new Font(ff2, (float)sz / 2f, fs))
1277-
{
1278-
const TextFormatFlags tff = TextFormatFlags.NoPadding;
1279-
var proposedSize = new Size(int.MaxValue, int.MaxValue);
1280-
var sf = TextRenderer.MeasureText(runText, f, proposedSize, tff);
1281-
// sf returns size in pixels
1282-
return sf.Width / multiplier;
1283-
}
1284-
}
1285-
}
1286-
}
1287-
catch (OverflowException)
1288-
{
1289-
// This happened on Azure but interestingly enough not while testing locally.
1290-
return 0;
1291-
}
1232+
return MetricsGetter.GetTextWidth(ff, fs, sz, runText) / multiplier;
12921233
}
12931234

12941235
// The algorithm for this method comes from the implementer notes in [MS-OI29500].pdf

OpenXmlPowerTools/HtmlToWmlCssParser.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,10 @@ public bool IsColor
808808
}
809809
if (number) { return false; }
810810

811-
try
811+
if (ColorParser.IsValidName(m_value))
812812
{
813-
KnownColor kc = (KnownColor)Enum.Parse(typeof(KnownColor), m_value, true);
814813
return true;
815814
}
816-
catch { }
817815
}
818816
return false;
819817
}
@@ -835,13 +833,10 @@ public Color ToColor()
835833
}
836834
else
837835
{
838-
try
836+
if (ColorParser.TryFromName(m_value, out var c))
839837
{
840-
KnownColor kc = (KnownColor)Enum.Parse(typeof(KnownColor), m_value, true);
841-
Color c = Color.FromKnownColor(kc);
842838
return c;
843839
}
844-
catch { }
845840
}
846841
int r = ConvertFromHex(hex.Substring(0, 2));
847842
int g = ConvertFromHex(hex.Substring(2, 2));
@@ -1523,8 +1518,7 @@ public bool IsColor
15231518
return false;
15241519
}
15251520

1526-
KnownColor kc;
1527-
if (Enum.TryParse(m_val, true, out kc))
1521+
if (ColorParser.IsValidName(m_val))
15281522
{
15291523
return true;
15301524
}
@@ -1649,13 +1643,10 @@ public Color ToColor()
16491643
}
16501644
else
16511645
{
1652-
try
1646+
if (ColorParser.TryFromName(m_val, out var c))
16531647
{
1654-
KnownColor kc = (KnownColor)Enum.Parse(typeof(KnownColor), m_val, true);
1655-
Color c = Color.FromKnownColor(kc);
16561648
return c;
16571649
}
1658-
catch { }
16591650
}
16601651
if (hex.Length == 3)
16611652
{

0 commit comments

Comments
 (0)