PPTMath.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. using DocumentFormat.OpenXml;
  2. using DocumentFormat.OpenXml.Packaging;
  3. using DocumentFormat.OpenXml.Presentation;
  4. using HTEXLib.Helpers.ShapeHelpers;
  5. using HTEXLib.Models.Inner;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Text;
  9. namespace HTEXLib.Models.PPTX
  10. {
  11. public class PPTMath : PPTShapeBase
  12. {
  13. private const double RotationIndex = 60000;
  14. private PlaceholderShape placeholder { get; set; }
  15. public PPTMath(OpenXmlPart openXmlPart, AlternateContentChoice AlternateContentChoice, AlternateContentFallback AlternateContentFallback, PPTSlide slide)
  16. {
  17. if (openXmlPart is SlidePart slidePart)
  18. {
  19. PartForm = "slide";
  20. }
  21. else if (openXmlPart is SlideLayoutPart layoutPart)
  22. {
  23. PartForm = "layout";
  24. }
  25. base.slide = slide;
  26. this.AlternateContentChoice = AlternateContentChoice;
  27. this.AlternateContentFallback = AlternateContentFallback;
  28. suid = slide.slideIndex + "-" + ShaHashHelper.GetSHA1((AlternateContentChoice.GetType().Name + AlternateContentChoice.OuterXml).Trim().ToLower());
  29. SetMathNonVisualProperties(openXmlPart, AlternateContentChoice, AlternateContentFallback);
  30. SetMathVisualProperties(openXmlPart, AlternateContentChoice, AlternateContentFallback);
  31. }
  32. public AlternateContentChoice AlternateContentChoice { get; set; }
  33. public AlternateContentFallback AlternateContentFallback { get; set; }
  34. public DocumentFormat.OpenXml.Presentation.Shape element { get; set; }
  35. private void SetMathNonVisualProperties(OpenXmlPart openXmlPart, AlternateContentChoice AlternateContentChoice, AlternateContentFallback AlternateContentFallback)
  36. {
  37. var shape = AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
  38. if (shape.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnClick != null)
  39. {
  40. foreach (HyperlinkRelationship link in openXmlPart.HyperlinkRelationships)
  41. {
  42. if (link.Id.Equals(shape.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnClick.Id))
  43. {
  44. ClickLinkUrl = link.Uri.IsAbsoluteUri ? link.Uri.AbsoluteUri : link.Uri.OriginalString;
  45. }
  46. }
  47. }
  48. if (shape.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnHover != null)
  49. {
  50. foreach (HyperlinkRelationship link in openXmlPart.HyperlinkRelationships)
  51. {
  52. if (link.Id.Equals(shape.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnHover.Id))
  53. {
  54. HoverLinkUrl = link.Uri.IsAbsoluteUri ? link.Uri.AbsoluteUri : link.Uri.OriginalString;
  55. }
  56. }
  57. }
  58. var nonVisualShapeProp = new PPTNonVisualShapeProp
  59. {
  60. TimingId = shape.NonVisualShapeProperties.NonVisualDrawingProperties.Id,
  61. Id = PartForm + "PPTMathShape" +
  62. shape.NonVisualShapeProperties.NonVisualDrawingProperties.Id,
  63. Name = shape.LocalName,
  64. Type = "PPTMathShape"
  65. };
  66. base.NonVisualShapeProp = nonVisualShapeProp;
  67. if (AlternateContentFallback != null)
  68. {
  69. if (AlternateContentFallback.FirstChild is DocumentFormat.OpenXml.Presentation.Shape shapea)
  70. {
  71. if (string.IsNullOrEmpty(ClickLinkUrl))
  72. {
  73. if (shapea.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnClick != null)
  74. {
  75. foreach (HyperlinkRelationship link in openXmlPart.HyperlinkRelationships)
  76. {
  77. if (link.Id.Equals(shapea.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnClick.Id))
  78. {
  79. ClickLinkUrl = link.Uri.IsAbsoluteUri ? link.Uri.AbsoluteUri : link.Uri.OriginalString;
  80. }
  81. }
  82. }
  83. }
  84. if (string.IsNullOrEmpty(HoverLinkUrl))
  85. {
  86. if (shapea.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnHover != null)
  87. {
  88. foreach (HyperlinkRelationship link in openXmlPart.HyperlinkRelationships)
  89. {
  90. if (link.Id.Equals(shapea.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnHover.Id))
  91. {
  92. HoverLinkUrl = link.Uri.IsAbsoluteUri ? link.Uri.AbsoluteUri : link.Uri.OriginalString;
  93. }
  94. }
  95. }
  96. }
  97. if (base.NonVisualShapeProp == null) {
  98. base.NonVisualShapeProp = new PPTNonVisualShapeProp
  99. {
  100. TimingId = shape.NonVisualShapeProperties.NonVisualDrawingProperties.Id,
  101. Id = PartForm + "PPTMathShape" +
  102. shapea.NonVisualShapeProperties.NonVisualDrawingProperties.Id,
  103. Name = shapea.LocalName,
  104. Type = "PPTMathShape"
  105. };
  106. }
  107. }
  108. }
  109. }
  110. private void SetMathVisualProperties(OpenXmlPart openXmlPart, AlternateContentChoice AlternateContentChoice, AlternateContentFallback AlternateContentFallback)
  111. {
  112. var shape = AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
  113. if (shape != null) {
  114. element = shape;
  115. }
  116. if (shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null)
  117. {
  118. placeholder = shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.
  119. PlaceholderShape;
  120. if (placeholder.Type == null)
  121. placeholder.Type = PlaceholderValues.Body;
  122. }
  123. base.VisualShapeProp = new PPTVisualPPTShapeProp();
  124. base.SetSlideLayoutVisualShapeProperties(openXmlPart, shape);
  125. if (shape.ShapeProperties.Transform2D != null)
  126. {
  127. Int32Value rot = shape.ShapeProperties.Transform2D.Rotation;
  128. if (rot != null)
  129. {
  130. double degrees = System.Math.Round(rot / RotationIndex);
  131. if (degrees < 0)
  132. {
  133. degrees = 360 + degrees;
  134. }
  135. base.VisualShapeProp.Rotate = degrees;
  136. }
  137. base.VisualShapeProp.Extents = shape.ShapeProperties.Transform2D.Extents;
  138. base.VisualShapeProp.Offset = shape.ShapeProperties.Transform2D.Offset;
  139. }
  140. foreach (var element in shape.ShapeProperties.ChildElements)
  141. {
  142. if (typeof(DocumentFormat.OpenXml.Drawing.PresetGeometry).Equals(element.GetType()))
  143. {
  144. base.VisualShapeProp.PresetGeometry = (DocumentFormat.OpenXml.Drawing.PresetGeometry)element;
  145. }
  146. if (typeof(DocumentFormat.OpenXml.Drawing.BlipFill).Equals(element.GetType()))
  147. {
  148. base.VisualShapeProp.BlipFill = (DocumentFormat.OpenXml.Drawing.BlipFill)element;
  149. }
  150. if (typeof(DocumentFormat.OpenXml.Drawing.NoFill).Equals(element.GetType()))
  151. {
  152. base.VisualShapeProp.NoFill = (DocumentFormat.OpenXml.Drawing.NoFill)element;
  153. }
  154. if (typeof(DocumentFormat.OpenXml.Drawing.SolidFill).Equals(element.GetType()))
  155. {
  156. base.VisualShapeProp.SolidFill = (DocumentFormat.OpenXml.Drawing.SolidFill)element;
  157. }
  158. if (typeof(DocumentFormat.OpenXml.Drawing.GradientFill).Equals(element.GetType()))
  159. {
  160. base.VisualShapeProp.GradientFill = (DocumentFormat.OpenXml.Drawing.GradientFill)element;
  161. }
  162. if (typeof(DocumentFormat.OpenXml.Drawing.PatternFill).Equals(element.GetType()))
  163. {
  164. base.VisualShapeProp.PatternFill = (DocumentFormat.OpenXml.Drawing.PatternFill)element;
  165. }
  166. if (typeof(DocumentFormat.OpenXml.Drawing.GroupFill).Equals(element.GetType()))
  167. {
  168. base.VisualShapeProp.GroupFill = (DocumentFormat.OpenXml.Drawing.GroupFill)element;
  169. }
  170. }
  171. if (AlternateContentFallback != null)
  172. {
  173. if (AlternateContentFallback.FirstChild is DocumentFormat.OpenXml.Presentation.Shape shapea) {
  174. if (element == null) {
  175. element = shapea;
  176. }
  177. base.SetSlideLayoutVisualShapeProperties(openXmlPart, shapea);
  178. if (placeholder == null) {
  179. if (shapea.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null)
  180. {
  181. placeholder = shapea.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.
  182. PlaceholderShape;
  183. if (placeholder.Type == null)
  184. placeholder.Type = PlaceholderValues.Body;
  185. }
  186. }
  187. if (base.VisualShapeProp.Rotate <= 0) {
  188. if (shapea.ShapeProperties.Transform2D != null)
  189. {
  190. Int32Value rot = shapea.ShapeProperties.Transform2D.Rotation;
  191. if (rot != null)
  192. {
  193. double degrees = System.Math.Round(rot / RotationIndex);
  194. if (degrees < 0)
  195. {
  196. degrees = 360 + degrees;
  197. }
  198. base.VisualShapeProp.Rotate = degrees;
  199. }
  200. }
  201. if (base.VisualShapeProp.Extents == null) {
  202. base.VisualShapeProp.Extents = shapea.ShapeProperties.Transform2D.Extents;
  203. }
  204. if (base.VisualShapeProp.Offset == null)
  205. {
  206. base.VisualShapeProp.Offset = shapea.ShapeProperties.Transform2D.Offset;
  207. }
  208. }
  209. foreach (var element in shapea.ShapeProperties.ChildElements)
  210. {
  211. if (base.VisualShapeProp.PresetGeometry == null) {
  212. if (typeof(DocumentFormat.OpenXml.Drawing.PresetGeometry).Equals(element.GetType()))
  213. {
  214. base.VisualShapeProp.PresetGeometry = (DocumentFormat.OpenXml.Drawing.PresetGeometry)element;
  215. }
  216. }
  217. if (base.VisualShapeProp.BlipFill == null)
  218. if (typeof(DocumentFormat.OpenXml.Drawing.BlipFill).Equals(element.GetType()))
  219. {
  220. base.VisualShapeProp.BlipFill = (DocumentFormat.OpenXml.Drawing.BlipFill)element;
  221. }
  222. if (base.VisualShapeProp.NoFill == null)
  223. if (typeof(DocumentFormat.OpenXml.Drawing.NoFill).Equals(element.GetType()))
  224. {
  225. base.VisualShapeProp.NoFill = (DocumentFormat.OpenXml.Drawing.NoFill)element;
  226. }
  227. if (base.VisualShapeProp.SolidFill == null)
  228. if (typeof(DocumentFormat.OpenXml.Drawing.SolidFill).Equals(element.GetType()))
  229. {
  230. base.VisualShapeProp.SolidFill = (DocumentFormat.OpenXml.Drawing.SolidFill)element;
  231. }
  232. if (base.VisualShapeProp.GradientFill == null)
  233. if (typeof(DocumentFormat.OpenXml.Drawing.GradientFill).Equals(element.GetType()))
  234. {
  235. base.VisualShapeProp.GradientFill = (DocumentFormat.OpenXml.Drawing.GradientFill)element;
  236. }
  237. if (base.VisualShapeProp.PatternFill == null)
  238. if (typeof(DocumentFormat.OpenXml.Drawing.PatternFill).Equals(element.GetType()))
  239. {
  240. base.VisualShapeProp.PatternFill = (DocumentFormat.OpenXml.Drawing.PatternFill)element;
  241. }
  242. if (base.VisualShapeProp.GroupFill == null)
  243. if (typeof(DocumentFormat.OpenXml.Drawing.GroupFill).Equals(element.GetType()))
  244. {
  245. base.VisualShapeProp.GroupFill = (DocumentFormat.OpenXml.Drawing.GroupFill)element;
  246. }
  247. }
  248. }
  249. }
  250. }
  251. }
  252. }