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

+4
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

+6-6
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

-2
This file was deleted.

OpenXmlPowerTools.Tests/PresentationBuilderTests.cs

+4
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

+2-2
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

+1-1
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

+51
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

-1
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

+1-60
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

+4-13
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
{

OpenXmlPowerTools/MetricsGetter.cs

+60
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
using System;
2020
using System.Collections.Generic;
21+
using System.Drawing;
2122
using System.IO;
2223
using System.IO.Packaging;
2324
using System.Linq;
@@ -39,6 +40,12 @@ public class MetricsGetterSettings
3940

4041
public class MetricsGetter
4142
{
43+
private static Lazy<Graphics> Graphics { get; } = new Lazy<Graphics>(() =>
44+
{
45+
Image image = new Bitmap(1, 1);
46+
return System.Drawing.Graphics.FromImage(image);
47+
});
48+
4249
public static XElement GetMetrics(string fileName, MetricsGetterSettings settings)
4350
{
4451
FileInfo fi = new FileInfo(fileName);
@@ -116,6 +123,59 @@ public static XElement GetDocxMetrics(WmlDocument wmlDoc, MetricsGetterSettings
116123
return metrics;
117124
}
118125

126+
private static int _getTextWidth(FontFamily ff, FontStyle fs, decimal sz, string text)
127+
{
128+
try
129+
{
130+
using (var f = new Font(ff, (float)sz / 2f, fs))
131+
{
132+
var proposedSize = new Size(int.MaxValue, int.MaxValue);
133+
var sf = Graphics.Value.MeasureString(text, f, proposedSize);
134+
return (int) sf.Width;
135+
}
136+
}
137+
catch
138+
{
139+
return 0;
140+
}
141+
}
142+
143+
public static int GetTextWidth(FontFamily ff, FontStyle fs, decimal sz, string text)
144+
{
145+
try
146+
{
147+
return _getTextWidth(ff, fs, sz, text);
148+
}
149+
catch (ArgumentException)
150+
{
151+
try
152+
{
153+
const FontStyle fs2 = FontStyle.Regular;
154+
return _getTextWidth(ff, fs2, sz, text);
155+
}
156+
catch (ArgumentException)
157+
{
158+
const FontStyle fs2 = FontStyle.Bold;
159+
try
160+
{
161+
return _getTextWidth(ff, fs2, sz, text);
162+
}
163+
catch (ArgumentException)
164+
{
165+
// if both regular and bold fail, then get metrics for Times New Roman
166+
// use the original FontStyle (in fs)
167+
var ff2 = new FontFamily("Times New Roman");
168+
return _getTextWidth(ff2, fs, sz, text);
169+
}
170+
}
171+
}
172+
catch (OverflowException)
173+
{
174+
// This happened on Azure but interestingly enough not while testing locally.
175+
return 0;
176+
}
177+
}
178+
119179
private static Uri FixUri(string brokenUri)
120180
{
121181
return new Uri("http://broken-link/");

OpenXmlPowerTools/OpenXmlPowerTools.csproj

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
3-
<TargetFrameworks>net45;net46</TargetFrameworks>
3+
<TargetFrameworks>net45;net46;netstandard2.0</TargetFrameworks>
44
</PropertyGroup>
55

66
<ItemGroup>
@@ -18,4 +18,7 @@
1818
<Reference Include="System.Windows.Forms" />
1919
</ItemGroup>
2020

21+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0'">
22+
<PackageReference Include="System.Drawing.Common" Version="4.5.0" />
23+
</ItemGroup>
2124
</Project>

OpenXmlPowerTools/OpenXmlPowerTools.csproj.DotSettings

-3
This file was deleted.

OpenXmlPowerTools/OxPtHelpers.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static WmlDocument AppendParagraphToDocument(
6868

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

@@ -81,7 +81,7 @@ public static WmlDocument AppendParagraphToDocument(
8181

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

0 commit comments

Comments
 (0)