Skip to content

Commit

Permalink
Bumps DocumentFormat.OpenXml from 2.13.0 to 2.13.1.
Browse files Browse the repository at this point in the history
Bump DocumentFormat.OpenXml from 2.13.0 to 2.13.1
  • Loading branch information
stesee authored Oct 12, 2021
2 parents 8f648ed + cc04670 commit d09cef5
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 28 deletions.
2 changes: 1 addition & 1 deletion OpenXmlPowerTools.Tests/OpenXmlPowerTools.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="BitmapCompare" Version="1.0.8" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="xunit" Version="2.4.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PrivateAssets>all</PrivateAssets>
Expand Down
8 changes: 4 additions & 4 deletions OpenXmlPowerTools/FormattingAssembler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -699,7 +699,7 @@ private static void AddTabAtLeftIndent(XElement pPr)
tabs = new XElement(W.tabs);
pPr.Add(tabs);
}
var tabAtLeft = tabs.Elements(W.tab).FirstOrDefault(t => (int)t.Attribute(W.pos) == left);
var tabAtLeft = tabs.Elements(W.tab).FirstOrDefault(t => WordprocessingMLUtil.StringToTwips((string)t.Attribute(W.pos)) == left);
if (tabAtLeft == null)
{
tabs.Add(
Expand Down Expand Up @@ -2243,15 +2243,15 @@ private static XElement TabsMerge(XElement higherPriorityElement, XElement lower
var hps = higherPriorityElement.Elements().Select(e =>
new
{
Pos = (int)e.Attribute(W.pos),
Pos = WordprocessingMLUtil.StringToTwips((string)e.Attribute(W.pos)),
Pri = 1,
Element = e,
}
);
var lps = lowerPriorityElement.Elements().Select(e =>
new
{
Pos = (int)e.Attribute(W.pos),
Pos = WordprocessingMLUtil.StringToTwips((string)e.Attribute(W.pos)),
Pri = 2,
Element = e,
}
Expand All @@ -2260,7 +2260,7 @@ private static XElement TabsMerge(XElement higherPriorityElement, XElement lower
.GroupBy(s => s.Pos)
.Select(g => g.OrderBy(s => s.Pri).First().Element)
.Where(e => (string)e.Attribute(W.val) != "clear")
.OrderBy(e => (int)e.Attribute(W.pos));
.OrderBy(e => WordprocessingMLUtil.StringToTwips((string)e.Attribute(W.pos)));
var newTabs = new XElement(W.tabs, newTabElements);
return newTabs;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1141,15 +1141,15 @@ private static void CreateStyleFromInd(Dictionary<string, string> style, XElemen
: "0");
}

var firstLine = (decimal?)ind.Attribute(W.firstLine);
var firstLine = WordprocessingMLUtil.AttributeToTwips(ind.Attribute(W.firstLine));
if (firstLine != null && elementName != Xhtml.span)
{
var firstLineInInches = (decimal)firstLine / 1440m;
style.AddIfMissing("text-indent",
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.00}in", firstLineInInches));
}

var hanging = (decimal?)ind.Attribute(W.hanging);
var hanging = WordprocessingMLUtil.AttributeToTwips(ind.Attribute(W.hanging));
if (hanging != null && elementName != Xhtml.span)
{
var hangingInInches = (decimal)-hanging / 1440m;
Expand Down Expand Up @@ -1225,7 +1225,7 @@ private static void CreateStyleFromSpacing(Dictionary<string, string> style, XEl
}
}

var spacingAfter = suppressTrailingWhiteSpace ? 0m : (decimal?)spacing.Attribute(W.after);
var spacingAfter = suppressTrailingWhiteSpace ? 0 : WordprocessingMLUtil.AttributeToTwips(spacing.Attribute(W.after));
if (spacingAfter != null)
{
style.AddIfMissing("margin-bottom",
Expand Down Expand Up @@ -1880,7 +1880,8 @@ private static void CalculateSpanWidthForTabs(WordprocessingDocument wordDoc)

// w:defaultTabStop in settings
var sxd = wordDoc.MainDocumentPart.DocumentSettingsPart.GetXDocument();
var defaultTabStop = (int?)sxd.Descendants(W.defaultTabStop).Attributes(W.val).FirstOrDefault() ?? 720;
var defaultTabStopValue = (string)sxd.Descendants(W.defaultTabStop).Attributes(W.val).FirstOrDefault();
var defaultTabStop = defaultTabStopValue != null ? WordprocessingMLUtil.StringToTwips(defaultTabStopValue) : 720;

var pxd = wordDoc.MainDocumentPart.GetXDocument();
var root = pxd.Root;
Expand Down Expand Up @@ -1919,20 +1920,20 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
{
// todo need to handle start and end attributes

var left = (int?)ind.Attribute(W.left);
var left = WordprocessingMLUtil.AttributeToTwips(ind.Attribute(W.left));
if (left != null)
{
leftInTwips = (int)left;
}

var firstLine = 0;
var firstLineAtt = (int?)ind.Attribute(W.firstLine);
var firstLineAtt = WordprocessingMLUtil.AttributeToTwips(ind.Attribute(W.firstLine));
if (firstLineAtt != null)
{
firstLine = (int)firstLineAtt;
}

var hangingAtt = (int?)ind.Attribute(W.hanging);
var hangingAtt = WordprocessingMLUtil.AttributeToTwips(ind.Attribute(W.hanging));
if (hangingAtt != null)
{
firstLine = -(int)hangingAtt;
Expand Down Expand Up @@ -2010,7 +2011,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop

var tabAfterText = tabs
.Elements(W.tab)
.FirstOrDefault(t => (int)t.Attribute(W.pos) > testAmount);
.FirstOrDefault(t => WordprocessingMLUtil.StringToTwips((string)t.Attribute(W.pos)) > testAmount);

if (tabAfterText == null)
{
Expand Down Expand Up @@ -2049,7 +2050,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
new XElement(W.t, textAfterTab));

var widthOfTextAfterTab = CalcWidthOfRunInTwips(dummyRun2);
var delta2 = (int)tabAfterText.Attribute(W.pos) - widthOfTextAfterTab - twipCounter;
var delta2 = WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) - widthOfTextAfterTab - twipCounter;
if (delta2 < 0)
{
delta2 = 0;
Expand All @@ -2059,7 +2060,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
new XAttribute(PtOpenXml.TabWidth,
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.000}", delta2 / 1440m)),
GetLeader(tabAfterText));
twipCounter = Math.Max((int)tabAfterText.Attribute(W.pos), twipCounter + widthOfTextAfterTab);
twipCounter = Math.Max(WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)), twipCounter + widthOfTextAfterTab);

var lastElement = textElementsToMeasure.LastOrDefault();
if (lastElement == null)
Expand Down Expand Up @@ -2103,7 +2104,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
new XElement(W.t, mantissa));

var widthOfMantissa = CalcWidthOfRunInTwips(dummyRun4);
var delta2 = (int)tabAfterText.Attribute(W.pos) - widthOfMantissa - twipCounter;
var delta2 = WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) - widthOfMantissa - twipCounter;
if (delta2 < 0)
{
delta2 = 0;
Expand All @@ -2121,7 +2122,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
new XElement(W.t, decims));

var widthOfDecims = CalcWidthOfRunInTwips(dummyRun4);
twipCounter = Math.Max((int)tabAfterText.Attribute(W.pos) + widthOfDecims, twipCounter + widthOfMantissa + widthOfDecims);
twipCounter = Math.Max(WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) + widthOfDecims, twipCounter + widthOfMantissa + widthOfDecims);

var lastElement = textElementsToMeasure.LastOrDefault();
if (lastElement == null)
Expand All @@ -2145,7 +2146,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
new XElement(W.t, textAfterTab));

var widthOfTextAfterTab = CalcWidthOfRunInTwips(dummyRun2);
var delta2 = (int)tabAfterText.Attribute(W.pos) - widthOfTextAfterTab - twipCounter;
var delta2 = WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) - widthOfTextAfterTab - twipCounter;
if (delta2 < 0)
{
delta2 = 0;
Expand All @@ -2155,7 +2156,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
new XAttribute(PtOpenXml.TabWidth,
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.000}", delta2 / 1440m)),
GetLeader(tabAfterText));
twipCounter = Math.Max((int)tabAfterText.Attribute(W.pos), twipCounter + widthOfTextAfterTab);
twipCounter = Math.Max(WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)), twipCounter + widthOfTextAfterTab);

var lastElement = textElementsToMeasure.LastOrDefault();
if (lastElement == null)
Expand Down Expand Up @@ -2196,7 +2197,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
new XElement(W.t, textAfterTab));

var widthOfText = CalcWidthOfRunInTwips(dummyRun4);
var delta2 = (int)tabAfterText.Attribute(W.pos) - (widthOfText / 2) - twipCounter;
var delta2 = WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) - (widthOfText / 2) - twipCounter;
if (delta2 < 0)
{
delta2 = 0;
Expand All @@ -2206,7 +2207,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
new XAttribute(PtOpenXml.TabWidth,
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.000}", delta2 / 1440m)),
GetLeader(tabAfterText));
twipCounter = Math.Max((int)tabAfterText.Attribute(W.pos) + widthOfText / 2, twipCounter + widthOfText);
twipCounter = Math.Max(WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) + widthOfText / 2, twipCounter + widthOfText);

var lastElement = textElementsToMeasure.LastOrDefault();
if (lastElement == null)
Expand All @@ -2224,12 +2225,12 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
}
if (tabVal == "left" || tabVal == "start" || tabVal == "num")
{
var delta = (int)tabAfterText.Attribute(W.pos) - twipCounter;
var delta = WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) - twipCounter;
currentElement.Add(
new XAttribute(PtOpenXml.TabWidth,
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.000}", delta / 1440m)),
GetLeader(tabAfterText));
twipCounter = (int)tabAfterText.Attribute(W.pos);
twipCounter = WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos));

currentElementIdx++;
if (currentElementIdx >= contentToMeasure.Length)
Expand Down Expand Up @@ -2284,7 +2285,7 @@ private static XElement AddDefaultTabsAfterLastTab(XElement tabs, int defaultTab
var lastTabElement = tabs
.Elements(W.tab)
.Where(t => (string)t.Attribute(W.val) != "clear" && (string)t.Attribute(W.val) != "bar")
.OrderBy(t => (int)t.Attribute(W.pos))
.OrderBy(t => WordprocessingMLUtil.StringToTwips((string)t.Attribute(W.pos)))
.LastOrDefault();
if (lastTabElement != null)
{
Expand All @@ -2293,15 +2294,15 @@ private static XElement AddDefaultTabsAfterLastTab(XElement tabs, int defaultTab
defaultTabStop = 720;
}

var rangeStart = (int)lastTabElement.Attribute(W.pos) / defaultTabStop + 1;
var rangeStart = WordprocessingMLUtil.StringToTwips((string)lastTabElement.Attribute(W.pos)) / defaultTabStop + 1;
var tempTabs = new XElement(W.tabs,
tabs.Elements().Where(t => (string)t.Attribute(W.val) != "clear" && (string)t.Attribute(W.val) != "bar"),
Enumerable.Range(rangeStart, 100)
.Select(r => new XElement(W.tab,
new XAttribute(W.val, "left"),
new XAttribute(W.pos, r * defaultTabStop))));
tempTabs = new XElement(W.tabs,
tempTabs.Elements().OrderBy(t => (int)t.Attribute(W.pos)));
tempTabs.Elements().OrderBy(t => WordprocessingMLUtil.StringToTwips((string)t.Attribute(W.pos))));
return tempTabs;
}
else
Expand Down
4 changes: 2 additions & 2 deletions OpenXmlPowerTools/OpenXmlPowerTools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
<None Remove="Properties\**" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="DocumentFormat.OpenXml" Version="2.13.0" />
<PackageReference Include="DocumentFormat.OpenXml" Version="2.13.1" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.24.0.32949">
<PackageReference Include="SonarAnalyzer.CSharp" Version="8.30.0.37606">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
Expand Down
29 changes: 29 additions & 0 deletions OpenXmlPowerTools/PtOpenXmlUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -900,6 +900,35 @@ public static bool GetBoolProp(XElement runProps, XName xName)
return false;
}

public static int StringToTwips(string twipsOrPoints)
{
// if the pos value is in points, not twips
if (twipsOrPoints.EndsWith("pt"))
{
decimal decimalValue = decimal.Parse(twipsOrPoints.Substring(0, twipsOrPoints.Length - 2));
return (int)(decimalValue * 20);
}
return int.Parse(twipsOrPoints);
}

public static int? AttributeToTwips(XAttribute attribute)
{
if (attribute == null)
{
return null;
}

string twipsOrPoints = (string)attribute;

// if the pos value is in points, not twips
if (twipsOrPoints.EndsWith("pt"))
{
decimal decimalValue = decimal.Parse(twipsOrPoints.Substring(0, twipsOrPoints.Length - 2));
return (int)(decimalValue * 20);
}
return int.Parse(twipsOrPoints);
}

private static readonly List<XName> AdditionalRunContainerNames = new List<XName>
{
W.w + "bdo",
Expand Down

0 comments on commit d09cef5

Please sign in to comment.