Skip to content

Commit cc04670

Browse files
authored
Merge pull request OpenXmlDev#24 from Codeuctivity/EricWhiteDev-vNext
Eric white dev v next
2 parents 1b1ddad + 29c26db commit cc04670

File tree

3 files changed

+55
-25
lines changed

3 files changed

+55
-25
lines changed

OpenXmlPowerTools/FormattingAssembler.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -699,7 +699,7 @@ private static void AddTabAtLeftIndent(XElement pPr)
699699
tabs = new XElement(W.tabs);
700700
pPr.Add(tabs);
701701
}
702-
var tabAtLeft = tabs.Elements(W.tab).FirstOrDefault(t => (int)t.Attribute(W.pos) == left);
702+
var tabAtLeft = tabs.Elements(W.tab).FirstOrDefault(t => WordprocessingMLUtil.StringToTwips((string)t.Attribute(W.pos)) == left);
703703
if (tabAtLeft == null)
704704
{
705705
tabs.Add(
@@ -2243,15 +2243,15 @@ private static XElement TabsMerge(XElement higherPriorityElement, XElement lower
22432243
var hps = higherPriorityElement.Elements().Select(e =>
22442244
new
22452245
{
2246-
Pos = (int)e.Attribute(W.pos),
2246+
Pos = WordprocessingMLUtil.StringToTwips((string)e.Attribute(W.pos)),
22472247
Pri = 1,
22482248
Element = e,
22492249
}
22502250
);
22512251
var lps = lowerPriorityElement.Elements().Select(e =>
22522252
new
22532253
{
2254-
Pos = (int)e.Attribute(W.pos),
2254+
Pos = WordprocessingMLUtil.StringToTwips((string)e.Attribute(W.pos)),
22552255
Pri = 2,
22562256
Element = e,
22572257
}
@@ -2260,7 +2260,7 @@ private static XElement TabsMerge(XElement higherPriorityElement, XElement lower
22602260
.GroupBy(s => s.Pos)
22612261
.Select(g => g.OrderBy(s => s.Pri).First().Element)
22622262
.Where(e => (string)e.Attribute(W.val) != "clear")
2263-
.OrderBy(e => (int)e.Attribute(W.pos));
2263+
.OrderBy(e => WordprocessingMLUtil.StringToTwips((string)e.Attribute(W.pos)));
22642264
var newTabs = new XElement(W.tabs, newTabElements);
22652265
return newTabs;
22662266
}

OpenXmlPowerTools/OpenXMLWordprocessingMLToHtmlConverter/WmlToHtmlConverter.cs

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,15 +1141,15 @@ private static void CreateStyleFromInd(Dictionary<string, string> style, XElemen
11411141
: "0");
11421142
}
11431143

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

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

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

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

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

1922-
var left = (int?)ind.Attribute(W.left);
1923+
var left = WordprocessingMLUtil.AttributeToTwips(ind.Attribute(W.left));
19231924
if (left != null)
19241925
{
19251926
leftInTwips = (int)left;
19261927
}
19271928

19281929
var firstLine = 0;
1929-
var firstLineAtt = (int?)ind.Attribute(W.firstLine);
1930+
var firstLineAtt = WordprocessingMLUtil.AttributeToTwips(ind.Attribute(W.firstLine));
19301931
if (firstLineAtt != null)
19311932
{
19321933
firstLine = (int)firstLineAtt;
19331934
}
19341935

1935-
var hangingAtt = (int?)ind.Attribute(W.hanging);
1936+
var hangingAtt = WordprocessingMLUtil.AttributeToTwips(ind.Attribute(W.hanging));
19361937
if (hangingAtt != null)
19371938
{
19381939
firstLine = -(int)hangingAtt;
@@ -2010,7 +2011,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
20102011

20112012
var tabAfterText = tabs
20122013
.Elements(W.tab)
2013-
.FirstOrDefault(t => (int)t.Attribute(W.pos) > testAmount);
2014+
.FirstOrDefault(t => WordprocessingMLUtil.StringToTwips((string)t.Attribute(W.pos)) > testAmount);
20142015

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

20512052
var widthOfTextAfterTab = CalcWidthOfRunInTwips(dummyRun2);
2052-
var delta2 = (int)tabAfterText.Attribute(W.pos) - widthOfTextAfterTab - twipCounter;
2053+
var delta2 = WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) - widthOfTextAfterTab - twipCounter;
20532054
if (delta2 < 0)
20542055
{
20552056
delta2 = 0;
@@ -2059,7 +2060,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
20592060
new XAttribute(PtOpenXml.TabWidth,
20602061
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.000}", delta2 / 1440m)),
20612062
GetLeader(tabAfterText));
2062-
twipCounter = Math.Max((int)tabAfterText.Attribute(W.pos), twipCounter + widthOfTextAfterTab);
2063+
twipCounter = Math.Max(WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)), twipCounter + widthOfTextAfterTab);
20632064

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

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

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

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

21472148
var widthOfTextAfterTab = CalcWidthOfRunInTwips(dummyRun2);
2148-
var delta2 = (int)tabAfterText.Attribute(W.pos) - widthOfTextAfterTab - twipCounter;
2149+
var delta2 = WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) - widthOfTextAfterTab - twipCounter;
21492150
if (delta2 < 0)
21502151
{
21512152
delta2 = 0;
@@ -2155,7 +2156,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
21552156
new XAttribute(PtOpenXml.TabWidth,
21562157
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.000}", delta2 / 1440m)),
21572158
GetLeader(tabAfterText));
2158-
twipCounter = Math.Max((int)tabAfterText.Attribute(W.pos), twipCounter + widthOfTextAfterTab);
2159+
twipCounter = Math.Max(WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)), twipCounter + widthOfTextAfterTab);
21592160

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

21982199
var widthOfText = CalcWidthOfRunInTwips(dummyRun4);
2199-
var delta2 = (int)tabAfterText.Attribute(W.pos) - (widthOfText / 2) - twipCounter;
2200+
var delta2 = WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) - (widthOfText / 2) - twipCounter;
22002201
if (delta2 < 0)
22012202
{
22022203
delta2 = 0;
@@ -2206,7 +2207,7 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
22062207
new XAttribute(PtOpenXml.TabWidth,
22072208
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.000}", delta2 / 1440m)),
22082209
GetLeader(tabAfterText));
2209-
twipCounter = Math.Max((int)tabAfterText.Attribute(W.pos) + widthOfText / 2, twipCounter + widthOfText);
2210+
twipCounter = Math.Max(WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) + widthOfText / 2, twipCounter + widthOfText);
22102211

22112212
var lastElement = textElementsToMeasure.LastOrDefault();
22122213
if (lastElement == null)
@@ -2224,12 +2225,12 @@ private static object CalculateSpanWidthTransform(XNode node, int defaultTabStop
22242225
}
22252226
if (tabVal == "left" || tabVal == "start" || tabVal == "num")
22262227
{
2227-
var delta = (int)tabAfterText.Attribute(W.pos) - twipCounter;
2228+
var delta = WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos)) - twipCounter;
22282229
currentElement.Add(
22292230
new XAttribute(PtOpenXml.TabWidth,
22302231
string.Format(NumberFormatInfo.InvariantInfo, "{0:0.000}", delta / 1440m)),
22312232
GetLeader(tabAfterText));
2232-
twipCounter = (int)tabAfterText.Attribute(W.pos);
2233+
twipCounter = WordprocessingMLUtil.StringToTwips((string)tabAfterText.Attribute(W.pos));
22332234

22342235
currentElementIdx++;
22352236
if (currentElementIdx >= contentToMeasure.Length)
@@ -2284,7 +2285,7 @@ private static XElement AddDefaultTabsAfterLastTab(XElement tabs, int defaultTab
22842285
var lastTabElement = tabs
22852286
.Elements(W.tab)
22862287
.Where(t => (string)t.Attribute(W.val) != "clear" && (string)t.Attribute(W.val) != "bar")
2287-
.OrderBy(t => (int)t.Attribute(W.pos))
2288+
.OrderBy(t => WordprocessingMLUtil.StringToTwips((string)t.Attribute(W.pos)))
22882289
.LastOrDefault();
22892290
if (lastTabElement != null)
22902291
{
@@ -2293,15 +2294,15 @@ private static XElement AddDefaultTabsAfterLastTab(XElement tabs, int defaultTab
22932294
defaultTabStop = 720;
22942295
}
22952296

2296-
var rangeStart = (int)lastTabElement.Attribute(W.pos) / defaultTabStop + 1;
2297+
var rangeStart = WordprocessingMLUtil.StringToTwips((string)lastTabElement.Attribute(W.pos)) / defaultTabStop + 1;
22972298
var tempTabs = new XElement(W.tabs,
22982299
tabs.Elements().Where(t => (string)t.Attribute(W.val) != "clear" && (string)t.Attribute(W.val) != "bar"),
22992300
Enumerable.Range(rangeStart, 100)
23002301
.Select(r => new XElement(W.tab,
23012302
new XAttribute(W.val, "left"),
23022303
new XAttribute(W.pos, r * defaultTabStop))));
23032304
tempTabs = new XElement(W.tabs,
2304-
tempTabs.Elements().OrderBy(t => (int)t.Attribute(W.pos)));
2305+
tempTabs.Elements().OrderBy(t => WordprocessingMLUtil.StringToTwips((string)t.Attribute(W.pos))));
23052306
return tempTabs;
23062307
}
23072308
else

OpenXmlPowerTools/PtOpenXmlUtil.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,35 @@ public static bool GetBoolProp(XElement runProps, XName xName)
900900
return false;
901901
}
902902

903+
public static int StringToTwips(string twipsOrPoints)
904+
{
905+
// if the pos value is in points, not twips
906+
if (twipsOrPoints.EndsWith("pt"))
907+
{
908+
decimal decimalValue = decimal.Parse(twipsOrPoints.Substring(0, twipsOrPoints.Length - 2));
909+
return (int)(decimalValue * 20);
910+
}
911+
return int.Parse(twipsOrPoints);
912+
}
913+
914+
public static int? AttributeToTwips(XAttribute attribute)
915+
{
916+
if (attribute == null)
917+
{
918+
return null;
919+
}
920+
921+
string twipsOrPoints = (string)attribute;
922+
923+
// if the pos value is in points, not twips
924+
if (twipsOrPoints.EndsWith("pt"))
925+
{
926+
decimal decimalValue = decimal.Parse(twipsOrPoints.Substring(0, twipsOrPoints.Length - 2));
927+
return (int)(decimalValue * 20);
928+
}
929+
return int.Parse(twipsOrPoints);
930+
}
931+
903932
private static readonly List<XName> AdditionalRunContainerNames = new List<XName>
904933
{
905934
W.w + "bdo",

0 commit comments

Comments
 (0)