Skip to content

Commit 181266a

Browse files
authored
Merge pull request #30 from dotnet-campus/t/lindexi/Update
更新5月版
2 parents 22a98a1 + 81d7d68 commit 181266a

19 files changed

Lines changed: 4210 additions & 34 deletions

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/ElementConverters/CommonElement/CommonElementTransformDataExtensions.cs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,15 @@ public static ITransformData GetOrCreateTransformData(this OpenXmlElement elemen
4242
internal static ITransformData CreateTransformData(this OpenXmlElement element, SlideContext context)
4343
{
4444
var transformData = new TransformData();
45-
// sppr 是 ShapeProperties
46-
var shapeProperties = element.GetFirstChild<ShapeProperties>();
47-
if (shapeProperties?.Transform2D != null)
45+
46+
Transform2D? transform2D = null;
47+
var shapeTransform = element.GetFirstChild<ShapeProperties>()?.GetFirstChild<Transform2D>();
48+
if (shapeTransform is not null)
49+
{
50+
transform2D = shapeTransform;
51+
}
52+
if (transform2D != null)
4853
{
49-
// xfrm 是 Transform2D
50-
var transform2D = shapeProperties.Transform2D;
5154
FillOffset(transform2D.Offset, transformData);
5255
//如果是组合形状得子元素,需要走组合形状算法计算元素大小
5356
if (element.Parent is DocumentFormat.OpenXml.Presentation.GroupShape groupShape)
@@ -93,6 +96,17 @@ internal static ITransformData CreateTransformData(this OpenXmlElement element,
9396
return transformData;
9497
}
9598

99+
//这是SmartArt获取Transform2D逻辑
100+
var officeShapeTransform = element.GetFirstChild<DocumentFormat.OpenXml.Office.Drawing.ShapeProperties>()?.GetFirstChild<Transform2D>();
101+
if (officeShapeTransform is not null)
102+
{
103+
FillOffset(officeShapeTransform.Offset, transformData);
104+
FillExtents(officeShapeTransform.Extents, transformData);
105+
FillRotation(officeShapeTransform.Rotation, transformData);
106+
FillFlip(officeShapeTransform.HorizontalFlip, officeShapeTransform.VerticalFlip, transformData);
107+
return transformData;
108+
}
109+
96110
// 需要考虑从占位符获取坐标等
97111
// 对于形状、图片,需要尝试获取占位符坐标
98112
var transform2DFromPlaceholder = PlaceholderHelper.GetTransform2DFromPlaceholder(element, context);

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/ElementConverters/CommonElement/ExtentsToElementEmuSizeExtensions.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ public static ElementEmuSize GetElementEmuSize(this DocumentFormat.OpenXml.Prese
2626
/// <returns></returns>
2727
public static ElementEmuSize GetElementEmuSize(this ShapeAdapt shape)
2828
{
29-
var shapeProperties = shape.ShapeProperties;
30-
var extents = shapeProperties?.Transform2D?.Extents;
29+
var shapePropertiesAdapt = shape.ShapePropertiesAdapt;
30+
var extents = shapePropertiesAdapt?.GetTransform2D()?.Extents;
3131
if (extents == null)
3232
{
3333
return default;
@@ -50,7 +50,7 @@ public static ElementEmuSize GetGroupExtentsEmuSize(DocumentFormat.OpenXml.Prese
5050
{
5151
if (extents == null) return new ElementEmuSize(new Emu(0), new Emu(0));
5252
if (groupShape == null) return GetElementEmuSize(extents);
53-
ElementEmuSize elementEmuSize = GetElementEmuSize(extents);
53+
var elementEmuSize = GetElementEmuSize(extents);
5454
Extents? groupExtents = null;
5555
ChildExtents? childExtents = null;
5656
var cxFactor = 1d;
@@ -74,8 +74,8 @@ public static ElementEmuSize GetGroupExtentsEmuSize(DocumentFormat.OpenXml.Prese
7474
//先算出第一层组合嵌套组合的Extents的Cx和Cy的变换因子
7575
if (groupShape.Parent is DocumentFormat.OpenXml.Presentation.GroupShape parentGroupShape)
7676
{
77-
Extents? parentGroupExtents = parentGroupShape.GroupShapeProperties?.TransformGroup?.Extents;
78-
ChildExtents? parentChildExtents = parentGroupShape.GroupShapeProperties?.TransformGroup?.ChildExtents;
77+
var parentGroupExtents = parentGroupShape.GroupShapeProperties?.TransformGroup?.Extents;
78+
var parentChildExtents = parentGroupShape.GroupShapeProperties?.TransformGroup?.ChildExtents;
7979
groupExtents = groupShape.GroupShapeProperties?.TransformGroup?.Extents;
8080
childExtents = groupShape.GroupShapeProperties?.TransformGroup?.ChildExtents;
8181
if (groupExtents != null && childExtents != null)
@@ -84,7 +84,7 @@ public static ElementEmuSize GetGroupExtentsEmuSize(DocumentFormat.OpenXml.Prese
8484
groupExtents, childExtents, cxFactor, cyFactor);
8585
}
8686

87-
DocumentFormat.OpenXml.Presentation.GroupShape tempGroupShape = parentGroupShape;
87+
var tempGroupShape = parentGroupShape;
8888
//假如groupShape的Parent的Parent再是组合,则只需要用变化因子分别乘与其自身的变化因子即可
8989
while (tempGroupShape.Parent is DocumentFormat.OpenXml.Presentation.GroupShape tempParentGroupShape)
9090
{

src/DocumentFormat.OpenXml.Flatten/DocumentFormat.OpenXml.Flatten/ElementConverters/Primitive/Colors_/ColorTransform.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,30 @@ private static Color HandleLuminance(Color color, LuminanceModulation? luminance
228228
return HslToColor(hue, sat, lum, alpha);
229229
}
230230

231+
private static Color HandleHue(Color color, HueModulation? hueModulation,
232+
HueOffset? hueOffset)
233+
{
234+
var (hue, sat, lum, alpha) = ColorToHsl(color);
235+
if (hueModulation != null || hueOffset != null)
236+
{
237+
var modulationVal = hueModulation?.Val;
238+
var offsetVal = hueOffset?.Val;
239+
var mod = modulationVal is not null && modulationVal.HasValue
240+
? new Percentage(modulationVal)
241+
: Percentage.FromDouble(1);
242+
var offset = offsetVal is not null && offsetVal.HasValue
243+
? new Angle(offsetVal).ToRadiansValue()
244+
: new Angle(0).ToRadiansValue();
245+
246+
var value = hue.ToRadiansValue() * mod.DoubleValue + offset;
247+
value = value > 2 * System.Math.PI ? 2 * System.Math.PI : value;
248+
value = value < 0 ? 0 : value;
249+
hue = Degree.FromDouble(value * 180 / System.Math.PI - 13);
250+
}
251+
252+
return HslToColor(hue, sat, lum, alpha);
253+
}
254+
231255
private static Color HandleSaturation(Color color, SaturationModulation? saturationModulation,
232256
SaturationOffset? saturationOffset)
233257
{

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

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,15 @@
1-
using DocumentFormat.OpenXml.Presentation;
1+
using DocumentFormat.OpenXml.Flatten.ElementConverters.Text;
2+
using DocumentFormat.OpenXml.Presentation;
3+
4+
using Shape = DocumentFormat.OpenXml.Presentation.Shape;
5+
using ShapeProperties = DocumentFormat.OpenXml.Presentation.ShapeProperties;
6+
using ShapeStyle = DocumentFormat.OpenXml.Presentation.ShapeStyle;
7+
using SmartArtShape = DocumentFormat.OpenXml.Office.Drawing.Shape;
8+
using SmartArtShapeNonVisualProperties = DocumentFormat.OpenXml.Office.Drawing.ShapeNonVisualProperties;
9+
using SmartArtShapeProperties = DocumentFormat.OpenXml.Office.Drawing.ShapeProperties;
10+
using SmartArtShapeStyle = DocumentFormat.OpenXml.Office.Drawing.ShapeStyle;
11+
using SmartArtTextBody = DocumentFormat.OpenXml.Office.Drawing.TextBody;
12+
using TextBody = DocumentFormat.OpenXml.Presentation.TextBody;
213

314
namespace DocumentFormat.OpenXml.Flatten.ElementConverters
415
{
@@ -11,39 +22,66 @@ public class ShapeAdapt
1122
/// 创建形状适配器
1223
/// </summary>
1324
/// <param name="shape"></param>
14-
/// <param name="shapeProperties"></param>
15-
/// <param name="shapeStyle"></param>
25+
/// <param name="shapePropertiesAdapt"></param>
26+
/// <param name="shapeStyleAdapt"></param>
1627
/// <param name="nonVisualShapeProperties"></param>
1728
/// <param name="useBackgroundFill"></param>
1829
/// <param name="textBody"></param>
19-
public ShapeAdapt(OpenXmlCompositeElement? shape, ShapeProperties? shapeProperties,
20-
ShapeStyle? shapeStyle,
30+
public ShapeAdapt(OpenXmlCompositeElement? shape, ShapeProperties? shapePropertiesAdapt,
31+
ShapeStyle? shapeStyleAdapt,
2132
NonVisualShapeProperties? nonVisualShapeProperties = null,
2233
bool? useBackgroundFill = null,
2334
TextBody? textBody = null)
2435
{
25-
ShapeProperties = shapeProperties;
26-
ShapeStyle = shapeStyle;
36+
ShapePropertiesAdapt = shapePropertiesAdapt;
37+
ShapeStyleAdapt = shapeStyleAdapt;
2738
NonVisualShapeProperties = nonVisualShapeProperties;
2839
UseBackgroundFill = useBackgroundFill;
29-
TextBody = textBody;
40+
TextBodyAdapt = textBody;
3041
Shape = shape;
3142
}
3243

3344
/// <summary>
34-
/// 形状元素,可能是 <see cref="ConnectionShape"/> 和 <see cref="DocumentFormat.OpenXml.Presentation.Shape"/>
45+
/// 适配SmartArt的形状构造
46+
/// </summary>
47+
/// <param name="smartArtShape"></param>
48+
/// <param name="smartArtShapeProperties"></param>
49+
/// <param name="shapeStyle"></param>
50+
/// <param name="smartArtShapeNonVisualProperties"></param>
51+
/// <param name="smartArtTextBody"></param>
52+
public ShapeAdapt(SmartArtShape? smartArtShape,
53+
SmartArtShapeProperties? smartArtShapeProperties,
54+
SmartArtShapeStyle? shapeStyle,
55+
SmartArtShapeNonVisualProperties? smartArtShapeNonVisualProperties = null,
56+
SmartArtTextBody? smartArtTextBody = null)
57+
{
58+
ShapePropertiesAdapt = smartArtShapeProperties;
59+
ShapeStyleAdapt = shapeStyle;
60+
SmartArtShapeNonVisualProperties = smartArtShapeNonVisualProperties;
61+
TextBodyAdapt = smartArtTextBody;
62+
Shape = smartArtShape;
63+
}
64+
65+
/// <summary>
66+
/// 形状元素,可能是 <see cref="ConnectionShape"/> 和 <see cref="DocumentFormat.OpenXml.Presentation.Shape"/>和<see cref="DocumentFormat.OpenXml.Office.Drawing.Shape"/>
3567
/// </summary>
3668
public OpenXmlCompositeElement? Shape { get; }
3769

3870
/// <summary>
3971
/// 形状属性
4072
/// </summary>
41-
public ShapeProperties? ShapeProperties { get; }
73+
public ShapePropertiesAdapt? ShapePropertiesAdapt { get; }
74+
75+
/// <summary>
76+
/// SmartArt形状不可见属性
77+
/// </summary>
78+
public SmartArtShapeNonVisualProperties? SmartArtShapeNonVisualProperties { get; }
4279

4380
/// <summary>
4481
/// 形状样式
4582
/// </summary>
46-
public ShapeStyle? ShapeStyle { get; }
83+
public ShapeStyleAdapt? ShapeStyleAdapt { get; }
84+
4785

4886
/// <summary>
4987
/// 形状不可见属性
@@ -59,7 +97,7 @@ public ShapeAdapt(OpenXmlCompositeElement? shape, ShapeProperties? shapeProperti
5997
/// <summary>
6098
/// 形状里面的文本
6199
/// </summary>
62-
public TextBody? TextBody { get; }
100+
public TextBodyAdapt? TextBodyAdapt { get; }
63101

64102
/// <summary>
65103
/// 转换为形状适配器
@@ -74,7 +112,7 @@ public ShapeAdapt(OpenXmlCompositeElement? shape, ShapeProperties? shapeProperti
74112
/// <summary>
75113
/// 转换为形状适配器
76114
/// </summary>
77-
public static implicit operator ShapeAdapt(DocumentFormat.OpenXml.Presentation.Shape shape) =>
115+
public static implicit operator ShapeAdapt(Shape shape) =>
78116
new ShapeAdapt
79117
(
80118
shape,
@@ -84,5 +122,18 @@ public static implicit operator ShapeAdapt(DocumentFormat.OpenXml.Presentation.S
84122
shape.UseBackgroundFill?.Value,
85123
shape.TextBody
86124
);
125+
126+
/// <summary>
127+
/// 转换为形状适配器
128+
/// </summary>
129+
public static implicit operator ShapeAdapt(DocumentFormat.OpenXml.Office.Drawing.Shape shape) =>
130+
new ShapeAdapt
131+
(
132+
shape,
133+
shape.ShapeProperties,
134+
shape.ShapeStyle,
135+
shape.ShapeNonVisualProperties,
136+
shape.TextBody
137+
);
87138
}
88139
}

0 commit comments

Comments
 (0)