Skip to content

Commit c6f53ed

Browse files
committed
从私有项目更新2月版本更改
1 parent 83bb28b commit c6f53ed

10 files changed

Lines changed: 135 additions & 27 deletions

File tree

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/Contexts/GeometryPath.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,15 @@ public readonly struct ShapePath
1818
/// <param name="isExtrusionOk">指定使用 3D 拉伸可能在此路径</param>
1919
/// <param name="emuWidth">指定的宽度或在路径坐标系统中应在使用的最大的 x 坐标</param>
2020
/// <param name="emuHeight">指定框架的高度或在路径坐标系统中应在使用的最大的 y 坐标</param>
21-
public ShapePath(string path, PathFillModeValues fillMode = PathFillModeValues.Norm, bool isStroke = true, bool isExtrusionOk = false, double emuWidth = 0, double emuHeight = 0)
21+
public ShapePath(string path, PathFillModeValues fillMode = PathFillModeValues.Norm, bool isStroke = true, bool isExtrusionOk = false, double? emuWidth = null, double? emuHeight = null)
2222
{
2323
Path = path;
2424
IsStroke = isStroke;
2525
FillMode = fillMode;
2626
IsFilled = fillMode is not PathFillModeValues.None;
2727
IsExtrusionOk = isExtrusionOk;
28-
Width = new Emu(emuWidth);
29-
Height = new Emu(emuHeight);
28+
Width = emuWidth.HasValue ? new Emu(emuWidth.Value) : null;
29+
Height = emuHeight.HasValue ? new Emu(emuHeight.Value) : null;
3030
}
3131

3232
/// <summary>
@@ -68,11 +68,11 @@ public ShapePath(string path, double eumWidth, double eumHeight) : this(path, Pa
6868
/// <summary>
6969
/// 指定的宽度或在路径坐标系统中应在使用的最大的 x 坐标。默认是 0 的值
7070
/// </summary>
71-
public Emu Width { get; }
71+
public Emu? Width { get; }
7272

7373
/// <summary>
7474
/// 指定框架的高度或在路径坐标系统中应在使用的最大的 y 坐标。默认是 0 的值
7575
/// </summary>
76-
public Emu Height { get; }
76+
public Emu? Height { get; }
7777
}
7878
}

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/Contexts/SvgPath.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,14 @@ public class SvgPath
1414
/// <param name="svgPathString">Svg Path字符串</param>
1515
/// <param name="shapeTextRectangle">形状文本框</param>
1616
/// <param name="multiShapePaths">Svg 多路径</param>
17-
public SvgPath(ShapeTypeValues? geometryShapeType, string? svgPathString, EmuShapeTextRectangle shapeTextRectangle, ShapePath[]? multiShapePaths)
17+
/// <param name="customGeometryInfo">自定义形状上下文信息</param>
18+
public SvgPath(ShapeTypeValues? geometryShapeType, string? svgPathString, EmuShapeTextRectangle shapeTextRectangle, ShapePath[]? multiShapePaths, CustomGeometryInfo? customGeometryInfo = null)
1819
{
1920
ShapeType = geometryShapeType;
2021
SvgPathString = svgPathString;
2122
ShapeTextRectangle = shapeTextRectangle;
2223
MultiShapePaths = multiShapePaths;
24+
CustomGeometryInfo = customGeometryInfo;
2325
}
2426

2527
/// <summary>
@@ -41,5 +43,10 @@ public SvgPath(ShapeTypeValues? geometryShapeType, string? svgPathString, EmuSha
4143
///Shape Path 多路径
4244
/// </summary>
4345
public ShapePath[]? MultiShapePaths { get; }
46+
47+
/// <summary>
48+
/// 自定义形状上下文信息
49+
/// </summary>
50+
public CustomGeometryInfo? CustomGeometryInfo { get; }
4451
}
4552
}

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/ElementConverters/Shape/CustomGeometryConverters/CustomGeometryConverter.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,13 +138,14 @@ void TryClosePath()
138138
stringPath.Clear();
139139
}
140140

141+
var customGeometryInfo = new CustomGeometryInfo(_customGeometry.ShapeGuideList, _customGeometry.AdjustValueList, _customGeometry.PathList, _customGeometry.Rectangle);
141142
if (svgPathList.Count == 1)
142143
{
143-
return new SvgPath(null, svgPathList.First().Path, ShapeTextRectangle, default);
144+
return new SvgPath(null, svgPathList.First().Path, ShapeTextRectangle, default, customGeometryInfo);
144145
}
145146
else if (svgPathList.Count > 1)
146147
{
147-
return new SvgPath(null, svgPathList.First().Path, ShapeTextRectangle, svgPathList.ToArray());
148+
return new SvgPath(null, svgPathList.First().Path, ShapeTextRectangle, svgPathList.ToArray(), customGeometryInfo);
148149
}
149150
else
150151
{

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/ElementConverters/Shape/ShapeFlattenConverter.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ protected override Shape Convert(Shape element, ElementContext context)
6565
masterPlaceholderShape?.NonVisualShapeProperties?.NonVisualShapeDrawingProperties!);
6666

6767
FlattenTransformData(shapeProperties, layoutPlaceholderShape, masterPlaceholderShape, context);
68+
69+
70+
FlattenPresetGeometryFormData(shapeProperties, layoutPlaceholderShape, masterPlaceholderShape, context);
71+
6872
}
6973
}
7074

@@ -74,6 +78,33 @@ protected override Shape Convert(Shape element, ElementContext context)
7478
return element;
7579
}
7680

81+
private void FlattenPresetGeometryFormData(ShapeProperties shapeProperties, Shape? layoutPlaceholderShape, Shape? masterPlaceholderShape, ElementContext context)
82+
{
83+
var layoutPresetGeometry = layoutPlaceholderShape?.ShapeProperties?.GetFirstChild<PresetGeometry>();
84+
var masterPresetGeometry = masterPlaceholderShape?.ShapeProperties?.GetFirstChild<PresetGeometry>();
85+
var geometry = shapeProperties.GetFirstChild<PresetGeometry>();
86+
if (geometry is null && layoutPresetGeometry is null && masterPresetGeometry is null)
87+
{
88+
// 对于草绘的形状,尽管也是逻辑上也是 PresetGeometry 对象
89+
// 但是在 XML 里面,使用 CustomGeometry 存放
90+
// 不能在拍平加上 PresetGeometry 的值,否则将会丢失
91+
return;
92+
}
93+
94+
var provider = new OpenXmlCompositeElementFlattenProvider<PresetGeometry>
95+
(
96+
shapeProperties.GetOrCreateElement<PresetGeometry>(),
97+
layoutPresetGeometry,
98+
masterPresetGeometry
99+
);
100+
101+
provider.FlattenMainElementProperty(presetGeometry => presetGeometry.AdjustValueList!);
102+
103+
var elementPresetGeometry = provider.Main;
104+
elementPresetGeometry.Preset = provider.GetFlattenProperty(presetGeometry => presetGeometry.Preset);
105+
elementPresetGeometry.AdjustValueList = provider.GetFlattenProperty(presetGeometry => presetGeometry.AdjustValueList);
106+
}
107+
77108
private void FlattenTransformData(ShapeProperties shapeProperties, Shape? layoutPlaceholderShape, Shape? masterPlaceholderShape, ElementContext context)
78109
{
79110
var layoutTransform2D = layoutPlaceholderShape?.ShapeProperties?.Transform2D;

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/ElementConverters/Shape/ShapeGeometryConverters/FlowChartPunchedTapeGeometry.cs

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,49 +51,51 @@ public class FlowChartPunchedTapeGeometry : ShapeGeometryBase
5151
// </path>
5252
//</pathLst>
5353

54+
//设置Emu转Pixel的精度为小数点4位,防止精度不够被转为0
55+
UnitPrecision = 4;
5456
var shapePaths = new ShapePath[1];
5557
// <path w="20" h="20">
5658
// <moveTo>
5759
// <pt x="0" y="2" />
5860
// </moveTo>
59-
var widthFactor = w / 20;
60-
var heightFactor = h / 20;
61-
var currentPoint = new EmuPoint(0, heightFactor * 2);
61+
var shapePathWidth = 20d;
62+
var shapePathHeight = 20d;
63+
var currentPoint = new EmuPoint(0, 2);
6264
var stringPath = new StringBuilder();
6365
stringPath.Append($"M {EmuToPixelString(currentPoint.X)},{EmuToPixelString(currentPoint.Y)} ");
6466
// <arcTo wR="5" hR="2" stAng="cd2" swAng="-10800000" />
65-
var wR = widthFactor * 5;
66-
var hR = heightFactor * 2;
67+
var wR = 5;
68+
var hR = 2;
6769
var stAng = cd2;
6870
var swAng = -10800000d;
6971
currentPoint = ArcToToString(stringPath, currentPoint, wR, hR, stAng, swAng);
7072
// <arcTo wR="5" hR="2" stAng="cd2" swAng="cd2" />
71-
wR = widthFactor * 5;
72-
hR = heightFactor * 2;
73+
wR = 5;
74+
hR = 2;
7375
stAng = cd2;
7476
swAng = cd2;
7577
currentPoint = ArcToToString(stringPath, currentPoint, wR, hR, stAng, swAng);
7678
// <lnTo>
7779
// <pt x="20" y="18" />
7880
// </lnTo>
79-
currentPoint = LineToToString(stringPath, widthFactor * 20, heightFactor * 18);
81+
currentPoint = LineToToString(stringPath, 20, 18);
8082
// <arcTo wR="5" hR="2" stAng="0" swAng="-10800000" />
81-
wR = widthFactor * 5;
82-
hR = heightFactor * 2;
83+
wR = 5;
84+
hR = 2;
8385
stAng = 0;
8486
swAng = -10800000;
8587
currentPoint = ArcToToString(stringPath, currentPoint, wR, hR, stAng, swAng);
8688
// <arcTo wR="5" hR="2" stAng="0" swAng="cd2" />
87-
wR = widthFactor * 5;
88-
hR = heightFactor * 2;
89+
wR = 5;
90+
hR = 2;
8991
stAng = 0;
9092
swAng = cd2;
9193
currentPoint = ArcToToString(stringPath, currentPoint, wR, hR, stAng, swAng);
9294
// <close />
9395
// </path>
9496
stringPath.Append("z ");
9597

96-
shapePaths[0] = new ShapePath(stringPath.ToString());
98+
shapePaths[0] = new ShapePath(stringPath.ToString(), emuWidth: shapePathWidth, emuHeight: shapePathHeight);
9799

98100
//<rect l="l" t="hd5" r="r" b="ib" xmlns="http://schemas.openxmlformats.org/drawingml/2006/main" />
99101
InitializeShapeTextRectangle(l, hd5, r, ib);

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/ElementConverters/Shape/ShapeGeometryConverters/MathEqualGeometry.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ public class MathEqualGeometry : ShapeGeometryBase
3535
var customAdj2 = adjusts?.GetAdjustValue("adj2");
3636
var adj2 = customAdj2 ?? 11760d;
3737

38+
//当adj1为最低为0,导致一些值为0,参与公式乘除运算,导致路径有误
39+
adj1 = System.Math.Max(adj1, 1);
40+
adj2 = System.Math.Max(adj2, 1);
41+
42+
3843
//<gdLst xmlns="http://schemas.openxmlformats.org/drawingml/2006/main">
3944
// <gd name="a1" fmla="pin 0 adj1 36745" />
4045
// <gd name="2a1" fmla="*/ a1 2 1" />

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/ElementConverters/Shape/ShapeGeometryConverters/MathPlusGeometry.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@ public class MathPlusGeometry : ShapeGeometryBase
3232
var customAdj1 = adjusts?.GetAdjustValue("adj1");
3333
var adj1 = customAdj1 ?? 23520d;
3434

35+
//当adj1为最低为0,导致一些值为0,参与公式乘除运算,导致路径有误
36+
adj1 = System.Math.Max(adj1, 1);
37+
3538
//<gdLst xmlns="http://schemas.openxmlformats.org/drawingml/2006/main">
3639
// <gd name="a1" fmla="pin 0 adj1 73490" />
3740
// <gd name="dx1" fmla="*/ w 73490 200000" />

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/ElementConverters/Shape/ShapeGeometryConverters/ShapeGeometryConverterHelper.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,18 @@ public static class ShapeGeometryConverterHelper
240240
ShapeTypeValues.MathDivide => CreateShapeGeometry<MathDivideGeometry>(),
241241
ShapeTypeValues.MathEqual => CreateShapeGeometry<MathEqualGeometry>(),
242242
ShapeTypeValues.MathNotEqual => CreateShapeGeometry<MathNotEqualGeometry>(),
243+
ShapeTypeValues.ActionButtonBackPrevious => CreateShapeGeometry<ActionButtonBackPreviousGeometry>(),
244+
ShapeTypeValues.ActionButtonBlank => CreateShapeGeometry<ActionButtonBlankGeometry>(),
245+
ShapeTypeValues.ActionButtonDocument => CreateShapeGeometry<ActionButtonDocumentGeometry>(),
246+
ShapeTypeValues.ActionButtonBeginning => CreateShapeGeometry<ActionButtonBeginningGeometry>(),
247+
ShapeTypeValues.ActionButtonEnd => CreateShapeGeometry<ActionButtonEndGeometry>(),
248+
ShapeTypeValues.ActionButtonForwardNext => CreateShapeGeometry<ActionButtonForwardNextGeometry>(),
249+
ShapeTypeValues.ActionButtonHelp => CreateShapeGeometry<ActionButtonHelpGeometry>(),
250+
ShapeTypeValues.ActionButtonHome => CreateShapeGeometry<ActionButtonHomeGeometry>(),
251+
ShapeTypeValues.ActionButtonInformation => CreateShapeGeometry<ActionButtonInformationGeometry>(),
252+
ShapeTypeValues.ActionButtonMovie => CreateShapeGeometry<ActionButtonMovieGeometry>(),
253+
ShapeTypeValues.ActionButtonReturn => CreateShapeGeometry<ActionButtonReturnGeometry>(),
254+
ShapeTypeValues.ActionButtonSound => CreateShapeGeometry<ActionButtonSoundGeometry>(),
243255
_ => null
244256
};
245257

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/Framework/ElementFlattenProvider.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,16 @@
55

66
namespace DocumentFormat.OpenXml.Flatten.Framework
77
{
8+
/// <summary>
9+
/// 提供 OpenXML 元素拍平的辅助功能
10+
/// </summary>
11+
/// <typeparam name="TElement"></typeparam>
812
class ElementFlattenProvider<TElement> where TElement : OpenXmlElement
913
{
14+
/// <summary>
15+
/// 创建 OpenXML 元素拍平的辅助功能类
16+
/// </summary>
17+
/// <param name="elementList">要求首个元素为主元素,主元素是待继承的元素</param>
1018
public ElementFlattenProvider(params TElement?[] elementList)
1119
{
1220
if (elementList[0] is null)
@@ -17,14 +25,23 @@ public ElementFlattenProvider(params TElement?[] elementList)
1725
ElementList = elementList;
1826
}
1927

28+
/// <summary>
29+
/// 获取主元素,主元素是待继承的元素,主元素是传入列表的首个元素
30+
/// </summary>
2031
public TElement Main => ElementList[0]!;
2132

33+
/// <summary>
34+
/// 获取继承后的主元素的属性,此属性可以继续进行多级继承
35+
/// </summary>
2236
public ElementFlattenProvider<TElement> FlattenMainElementProperty<TProperty>()
2337
where TProperty : OpenXmlElement
2438
{
2539
return FlattenMainElementProperty(e => e.GetFirstChild<TProperty>());
2640
}
2741

42+
/// <summary>
43+
/// 获取继承后的主元素的属性,此属性可以继续进行多级继承
44+
/// </summary>
2845
public ElementFlattenProvider<TElement> FlattenMainElementProperty<TProperty>(Func<TElement, TProperty?> func) where TProperty : OpenXmlElement
2946
{
3047
return FlattenMainElementProperty(e =>
@@ -39,6 +56,9 @@ public ElementFlattenProvider<TElement> FlattenMainElementProperty<TProperty>(Fu
3956
});
4057
}
4158

59+
/// <summary>
60+
/// 获取继承后的主元素的属性,此属性可以继续进行多级继承
61+
/// </summary>
4262
public ElementFlattenProvider<TElement> FlattenMainElementProperty<TProperty>(Func<TElement, (bool success, TProperty result)> func) where TProperty : OpenXmlElement
4363
{
4464
var mainResult = func(Main);
@@ -69,12 +89,21 @@ public ElementFlattenProvider<TElement> FlattenMainElementProperty<TProperty>(Fu
6989
return this;
7090
}
7191

92+
/// <summary>
93+
/// 获取基础的继承属性值
94+
/// </summary>
95+
/// <typeparam name="TProperty"></typeparam>
96+
/// <param name="func"></param>
97+
/// <returns></returns>
7298
[return: MaybeNull]
7399
public TProperty GetFlattenProperty<TProperty>(Func<TElement, (bool success, TProperty result)> func)
74100
{
75101
return ElementFlattenExtension.GetFlattenProperty<TElement, TProperty>(func, ElementList!);
76102
}
77103

104+
/// <summary>
105+
/// 获取继承后的主元素的属性,此属性可以继续进行多级继承
106+
/// </summary>
78107
public ElementFlattenProvider<TProperty>? GetSubFlattenProperty<TProperty>(
79108
Func<TElement, TProperty> func) where TProperty : OpenXmlElement
80109
=> GetSubFlattenProperty(e =>
@@ -90,6 +119,9 @@ public TProperty GetFlattenProperty<TProperty>(Func<TElement, (bool success, TPr
90119
}
91120
});
92121

122+
/// <summary>
123+
/// 获取继承后的主元素的属性,此属性可以继续进行多级继承
124+
/// </summary>
93125
public ElementFlattenProvider<TProperty>? GetSubFlattenProperty<TProperty>(
94126
Func<TElement, (bool success, TProperty result)> func) where TProperty : OpenXmlElement
95127
{
@@ -116,12 +148,18 @@ IEnumerable<TProperty> GetPropertyList()
116148
}
117149
}
118150

151+
/// <summary>
152+
/// 获取基础的继承属性值
153+
/// </summary>
119154
[return: MaybeNull]
120155
public TProperty GetFlattenProperty<TProperty>(Func<TElement, TProperty> func)
121156
{
122157
return ElementFlattenExtension.GetFlattenProperty<TElement, TProperty>(func, ElementList!);
123158
}
124159

160+
/// <summary>
161+
/// 继承的元素列表
162+
/// </summary>
125163
protected TElement?[] ElementList { get; }
126164
}
127165
}

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/Utils/PlaceholderHelper.cs

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -363,19 +363,28 @@ private static (OpenXmlElement? layoutPlaceholderElement, OpenXmlElement? master
363363
/// </summary>
364364
/// 这个规则通过 文本占位符没有type和id的值,获取第一个占位符作为坐标 和 WPS 对比测试拿到
365365
/// 测试课件:文本占位符没有type和id的值.pptx
366-
/// <param name="placeholder1"></param>
366+
/// <param name="placeholder1">Shape的PlaceholderShape</param>
367367
/// <param name="placeholder2"></param>
368368
/// <returns></returns>
369369
private static bool Equals(PlaceholderShape? placeholder1, PlaceholderShape? placeholder2)
370370
{
371-
// 如果 placeholder1.Type 存在值,要求 2 一定存在值
372-
if (placeholder1?.Type != null &&
373-
placeholder1.Type.Value != placeholder2?.Type?.Value)
371+
//目前摸到的逻辑是:
372+
//Side假如存在PlaceholderShape,优先级是idx,假如PlaceholderShape存在idx,SlideLayout和SlideMaster找不到对应的值,则不匹配
373+
//假如Side的PlaceholderShape不存在idx,只要SlideLayout和SlideMaster存在PlaceholderShape就行,跟type关系不大,假如SlideLayout和SlideMaster存在多个PlaceholderShape,取第一个
374+
//假如SlideLayout和SlideMaster的PlaceholderShape存在idx,且在Side的PlaceholderShape找不到对应idx,则不匹配
375+
376+
//先判断idx
377+
//假如Shape的PlaceholderShape的idx有值,则查找SlideLayout和SlideMaster是否对应其值,查找不到则不匹配
378+
if (placeholder1?.Index is not null && placeholder1.Index.Value != placeholder2?.Index?.Value)
379+
{
374380
return false;
381+
}
375382

376-
if (placeholder1?.Index is not null && placeholder1.Index.Value !=
377-
placeholder2?.Index?.Value)
383+
//假如Shape的PlaceholderShape的idx没值,且SlideLayout和SlideMaster有值,则不匹配
384+
if (placeholder1?.Index is null && placeholder2?.Index is not null)
385+
{
378386
return false;
387+
}
379388

380389
return true;
381390
}

0 commit comments

Comments
 (0)