PPTSlide.cs 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. using System;
  2. using System.Collections.Generic;
  3. using ClearSlideLibrary.Animations;
  4. using ClearSlideLibrary.Dom.PPTTexts;
  5. using ClearSlideLibrary.PPTBuilder;
  6. using DocumentFormat.OpenXml;
  7. using DocumentFormat.OpenXml.Packaging;
  8. using DocumentFormat.OpenXml.Presentation;
  9. namespace ClearSlideLibrary.Dom
  10. {
  11. public class PPTSlide : PPTShapeBase
  12. {
  13. public PPTSlide(SlidePart slidePart, int slideIndex, DefaultTextStyle defaultTextStyle,string fileName, SlideSize SlideSizes)
  14. {
  15. this.advanceAfterTime = -1;
  16. this.slideIndex = slideIndex;
  17. this.fileName = fileName;
  18. this.defaultTextStyle = defaultTextStyle;
  19. SlideLayoutPart = slidePart.SlideLayoutPart;
  20. SetShapeNonVisualProperties(slidePart);
  21. SetSpecificProperties(slidePart);
  22. Id = "s1s0";
  23. Transition = JSONGenerator.GenerateTransitionAnimationObject(slidePart.Slide.Transition);
  24. Animations = new List<IAnimation>();
  25. AddAnimations(slidePart.Slide.Timing, Animations, SlideSizes);
  26. }
  27. //Slide specific properties
  28. public DefaultTextStyle defaultTextStyle { get; set; }
  29. public PPTContainerShape ContainerShape { get; set; }
  30. public TextStyles textStyles { get; set; }
  31. public SlideLayoutPart SlideLayoutPart { get; set; }
  32. public List<IAnimation> Animations { get; set; }
  33. public string Id { get; set; }
  34. public IAnimation Transition { get; set; }
  35. public int slideIndex;
  36. public string fileName;
  37. public int advanceAfterTime { get; set; }
  38. //Petco:Check if there is objects with animation.
  39. public void CheckAndSetAnimatableProperty(string animObjectId)
  40. {
  41. int tryParse = 0;
  42. if (int.TryParse(animObjectId, out tryParse))
  43. {
  44. foreach (PPTShapeBase shape in ContainerShape.Elements)
  45. SetAnimatable(animObjectId, shape);
  46. }
  47. else
  48. {
  49. foreach (PPTShapeBase shapeBase in ContainerShape.Elements)
  50. if (typeof(PPTShape).Equals(shapeBase.GetType()) && ((PPTShape)shapeBase).IsText)
  51. foreach (PPTParagraph text in ((PPTShape)shapeBase).Texts)
  52. {
  53. if ((shapeBase.NonVisualShapeProp.Id + "p" + text.Paragraph).Equals("s1s" + animObjectId))
  54. {
  55. text.Animatable = true;
  56. }
  57. }
  58. }
  59. }
  60. private static void SetAnimatable(string animObjectId, PPTShapeBase bshape)
  61. {
  62. string shapeId = bshape.NonVisualShapeProp.Id;
  63. string shapeObjectId = "s1s" + animObjectId;
  64. if (shapeId.Equals(shapeObjectId))
  65. {
  66. bshape.Animatable = true;
  67. }
  68. }
  69. public void MakeShapeInvisible(String shapeId)
  70. {
  71. foreach (PPTShapeBase shape in ContainerShape.Elements)
  72. if (shape.NonVisualShapeProp.Id.Equals(shapeId))
  73. {
  74. shape.Invisible = true;
  75. return;
  76. }
  77. else if (typeof(PPTShape).Equals(shape.GetType()) && ((PPTShape)shape).IsText)
  78. {
  79. foreach (PPTParagraph text in ((PPTShape)shape).Texts)
  80. if ((shape.NonVisualShapeProp.Id + "p" + text.Paragraph).Equals(shapeId))
  81. {
  82. text.Invisible = true;
  83. return;
  84. }
  85. }
  86. }
  87. public void AddAnimations(OpenXmlCompositeElement element, List<IAnimation> resultList, SlideSize SlideSizes)
  88. {
  89. if (element==null)
  90. return;
  91. List<AnimateMotion> motions = new List<AnimateMotion>();
  92. IAnimation animationForThisNode = null;
  93. if (element.GetType().Equals(typeof(CommonTimeNode)))
  94. {
  95. CommonTimeNode node = (CommonTimeNode)element;
  96. animationForThisNode = new JSONGenerator(this).getSimpleAnimationFromCommonTimeNodePreset(node, SlideSizes);
  97. if (animationForThisNode != null)
  98. {
  99. resultList.Add(animationForThisNode);
  100. //Check if object id is presented in animation list.
  101. CheckAndSetAnimatableProperty(animationForThisNode.ObjectId);
  102. if ((animationForThisNode.InnerAnimations == null ||
  103. animationForThisNode.InnerAnimations.Count == 0) &&
  104. animationForThisNode.IsItEntranceAnimation())
  105. MakeShapeInvisible("s1s" + animationForThisNode.ObjectId);
  106. else if (animationForThisNode.InnerAnimations != null)
  107. foreach (IAnimation anAnimation in animationForThisNode.InnerAnimations)
  108. if (anAnimation.IsItEntranceAnimation())
  109. MakeShapeInvisible("s1s" + animationForThisNode.ObjectId);
  110. return;
  111. }
  112. else
  113. {
  114. /*
  115. * Sometimes there are common time nodes without animations in them. They are used for grouping animations.
  116. * Usually animations are grouped for timing purposes like adding delay to all, or start a group after another.
  117. * It's a tree structure and here we try to follow it as much as possible. Later we will strip the unnecessary nodes
  118. */
  119. animationForThisNode = new SimpleAnimation();
  120. if (node.NodeType != null)
  121. ((SimpleAnimation)animationForThisNode).timingType = node.NodeType;
  122. int delay = 0;
  123. if (node.StartConditionList != null)
  124. foreach (Condition cond in node.StartConditionList)
  125. if (cond.Delay != null && "indefinite" != cond.Delay.Value && cond.Delay.HasValue)
  126. delay = delay + int.Parse(cond.Delay.Value);
  127. if (delay > 0)
  128. {
  129. animationForThisNode.Start = delay;
  130. }
  131. }
  132. if (animationForThisNode != null)
  133. {
  134. animationForThisNode.InnerAnimations = new List<IAnimation>();
  135. resultList.Add(animationForThisNode);
  136. }
  137. }
  138. //Go recursive in the Open XML tree
  139. foreach (OpenXmlElement obj in element.ChildElements)
  140. {
  141. if (obj.GetType().IsSubclassOf(typeof(OpenXmlCompositeElement)))
  142. {
  143. if (animationForThisNode == null)
  144. AddAnimations((OpenXmlCompositeElement)obj, resultList, SlideSizes);
  145. else
  146. AddAnimations((OpenXmlCompositeElement)obj, animationForThisNode.InnerAnimations, SlideSizes);
  147. }
  148. }
  149. }
  150. private void SetSpecificProperties(SlidePart slidePart)
  151. {
  152. textStyles = slidePart.SlideLayoutPart.SlideMasterPart.SlideMaster.TextStyles;
  153. var groupShapeBuilder = new PPTContainerShapeBuilder();
  154. ContainerShape = groupShapeBuilder.GetPPTContainerShape(slidePart, this);
  155. }
  156. private void SetShapeNonVisualProperties(SlidePart slidePart)
  157. {
  158. var nonVisualShapeProp = new PPTNonVisualShapeProp
  159. {
  160. Id = "s1s1",
  161. Name = slidePart.Slide.LocalName,
  162. Type = "PPTSlide"
  163. };
  164. base.NonVisualShapeProp = nonVisualShapeProp;
  165. }
  166. }
  167. }