123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258 |
- using DocumentFormat.OpenXml;
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Presentation;
- using HTEXLib.Helpers.ShapeHelpers;
- using HTEXLib.Models.Inner;
- using System;
- using System.Collections.Generic;
- using System.Text;
- namespace HTEXLib.Models.PPTX
- {
- public class PPTMath : PPTShapeBase
- {
- private const double RotationIndex = 60000;
- private PlaceholderShape placeholder { get; set; }
- public PPTMath(OpenXmlPart openXmlPart, AlternateContentChoice AlternateContentChoice, AlternateContentFallback AlternateContentFallback, PPTSlide slide)
- {
- if (openXmlPart is SlidePart slidePart)
- {
- PartForm = "slide";
- }
- else if (openXmlPart is SlideLayoutPart layoutPart)
- {
- PartForm = "layout";
- }
- base.slide = slide;
- this.AlternateContentChoice = AlternateContentChoice;
- this.AlternateContentFallback = AlternateContentFallback;
- suid = slide.slideIndex + "-" + ShaHashHelper.GetSHA1((AlternateContentChoice.GetType().Name + AlternateContentChoice.OuterXml).Trim().ToLower());
- SetMathNonVisualProperties(openXmlPart, AlternateContentChoice, AlternateContentFallback);
- SetMathVisualProperties(openXmlPart, AlternateContentChoice, AlternateContentFallback);
- }
- public AlternateContentChoice AlternateContentChoice { get; set; }
- public AlternateContentFallback AlternateContentFallback { get; set; }
- public DocumentFormat.OpenXml.Presentation.Shape element { get; set; }
- private void SetMathNonVisualProperties(OpenXmlPart openXmlPart, AlternateContentChoice AlternateContentChoice, AlternateContentFallback AlternateContentFallback)
- {
- var shape = AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
- if (shape.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnClick != null)
- {
- foreach (HyperlinkRelationship link in openXmlPart.HyperlinkRelationships)
- {
- if (link.Id.Equals(shape.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnClick.Id))
- {
- ClickLinkUrl = link.Uri.IsAbsoluteUri ? link.Uri.AbsoluteUri : link.Uri.OriginalString;
- }
- }
- }
- if (shape.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnHover != null)
- {
- foreach (HyperlinkRelationship link in openXmlPart.HyperlinkRelationships)
- {
- if (link.Id.Equals(shape.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnHover.Id))
- {
- HoverLinkUrl = link.Uri.IsAbsoluteUri ? link.Uri.AbsoluteUri : link.Uri.OriginalString;
- }
- }
- }
- var nonVisualShapeProp = new PPTNonVisualShapeProp
- {
- TimingId = shape.NonVisualShapeProperties.NonVisualDrawingProperties.Id,
- Id = PartForm + "PPTMathShape" +
- shape.NonVisualShapeProperties.NonVisualDrawingProperties.Id,
- Name = shape.LocalName,
- Type = "PPTMathShape"
- };
- base.NonVisualShapeProp = nonVisualShapeProp;
- if (AlternateContentFallback != null)
- {
- if (AlternateContentFallback.FirstChild is DocumentFormat.OpenXml.Presentation.Shape shapea)
- {
- if (string.IsNullOrEmpty(ClickLinkUrl))
- {
- if (shapea.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnClick != null)
- {
- foreach (HyperlinkRelationship link in openXmlPart.HyperlinkRelationships)
- {
- if (link.Id.Equals(shapea.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnClick.Id))
- {
- ClickLinkUrl = link.Uri.IsAbsoluteUri ? link.Uri.AbsoluteUri : link.Uri.OriginalString;
- }
- }
- }
- }
- if (string.IsNullOrEmpty(HoverLinkUrl))
- {
- if (shapea.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnHover != null)
- {
- foreach (HyperlinkRelationship link in openXmlPart.HyperlinkRelationships)
- {
- if (link.Id.Equals(shapea.NonVisualShapeProperties.NonVisualDrawingProperties.HyperlinkOnHover.Id))
- {
- HoverLinkUrl = link.Uri.IsAbsoluteUri ? link.Uri.AbsoluteUri : link.Uri.OriginalString;
- }
- }
- }
- }
- if (base.NonVisualShapeProp == null) {
- base.NonVisualShapeProp = new PPTNonVisualShapeProp
- {
- TimingId = shape.NonVisualShapeProperties.NonVisualDrawingProperties.Id,
- Id = PartForm + "PPTMathShape" +
- shapea.NonVisualShapeProperties.NonVisualDrawingProperties.Id,
- Name = shapea.LocalName,
- Type = "PPTMathShape"
- };
- }
- }
- }
- }
- private void SetMathVisualProperties(OpenXmlPart openXmlPart, AlternateContentChoice AlternateContentChoice, AlternateContentFallback AlternateContentFallback)
- {
- var shape = AlternateContentChoice.GetFirstChild<DocumentFormat.OpenXml.Presentation.Shape>();
- if (shape != null) {
- element = shape;
- }
- if (shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null)
- {
- placeholder = shape.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.
- PlaceholderShape;
- if (placeholder.Type == null)
- placeholder.Type = PlaceholderValues.Body;
- }
- base.VisualShapeProp = new PPTVisualPPTShapeProp();
- base.SetSlideLayoutVisualShapeProperties(openXmlPart, shape);
- if (shape.ShapeProperties.Transform2D != null)
- {
- Int32Value rot = shape.ShapeProperties.Transform2D.Rotation;
- if (rot != null)
- {
- double degrees = System.Math.Round(rot / RotationIndex);
- if (degrees < 0)
- {
- degrees = 360 + degrees;
- }
- base.VisualShapeProp.Rotate = degrees;
- }
- base.VisualShapeProp.Extents = shape.ShapeProperties.Transform2D.Extents;
- base.VisualShapeProp.Offset = shape.ShapeProperties.Transform2D.Offset;
- }
- foreach (var element in shape.ShapeProperties.ChildElements)
- {
- if (typeof(DocumentFormat.OpenXml.Drawing.PresetGeometry).Equals(element.GetType()))
- {
- base.VisualShapeProp.PresetGeometry = (DocumentFormat.OpenXml.Drawing.PresetGeometry)element;
- }
- if (typeof(DocumentFormat.OpenXml.Drawing.BlipFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.BlipFill = (DocumentFormat.OpenXml.Drawing.BlipFill)element;
- }
- if (typeof(DocumentFormat.OpenXml.Drawing.NoFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.NoFill = (DocumentFormat.OpenXml.Drawing.NoFill)element;
- }
- if (typeof(DocumentFormat.OpenXml.Drawing.SolidFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.SolidFill = (DocumentFormat.OpenXml.Drawing.SolidFill)element;
- }
- if (typeof(DocumentFormat.OpenXml.Drawing.GradientFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.GradientFill = (DocumentFormat.OpenXml.Drawing.GradientFill)element;
- }
- if (typeof(DocumentFormat.OpenXml.Drawing.PatternFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.PatternFill = (DocumentFormat.OpenXml.Drawing.PatternFill)element;
- }
- if (typeof(DocumentFormat.OpenXml.Drawing.GroupFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.GroupFill = (DocumentFormat.OpenXml.Drawing.GroupFill)element;
- }
- }
- if (AlternateContentFallback != null)
- {
- if (AlternateContentFallback.FirstChild is DocumentFormat.OpenXml.Presentation.Shape shapea) {
- if (element == null) {
- element = shapea;
- }
- base.SetSlideLayoutVisualShapeProperties(openXmlPart, shapea);
- if (placeholder == null) {
- if (shapea.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.PlaceholderShape != null)
- {
- placeholder = shapea.NonVisualShapeProperties.ApplicationNonVisualDrawingProperties.
- PlaceholderShape;
- if (placeholder.Type == null)
- placeholder.Type = PlaceholderValues.Body;
- }
- }
- if (base.VisualShapeProp.Rotate <= 0) {
- if (shapea.ShapeProperties.Transform2D != null)
- {
- Int32Value rot = shapea.ShapeProperties.Transform2D.Rotation;
- if (rot != null)
- {
- double degrees = System.Math.Round(rot / RotationIndex);
- if (degrees < 0)
- {
- degrees = 360 + degrees;
- }
- base.VisualShapeProp.Rotate = degrees;
- }
-
- }
- if (base.VisualShapeProp.Extents == null) {
- base.VisualShapeProp.Extents = shapea.ShapeProperties.Transform2D.Extents;
- }
- if (base.VisualShapeProp.Offset == null)
- {
- base.VisualShapeProp.Offset = shapea.ShapeProperties.Transform2D.Offset;
- }
- }
- foreach (var element in shapea.ShapeProperties.ChildElements)
- {
- if (base.VisualShapeProp.PresetGeometry == null) {
- if (typeof(DocumentFormat.OpenXml.Drawing.PresetGeometry).Equals(element.GetType()))
- {
- base.VisualShapeProp.PresetGeometry = (DocumentFormat.OpenXml.Drawing.PresetGeometry)element;
- }
- }
- if (base.VisualShapeProp.BlipFill == null)
- if (typeof(DocumentFormat.OpenXml.Drawing.BlipFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.BlipFill = (DocumentFormat.OpenXml.Drawing.BlipFill)element;
- }
- if (base.VisualShapeProp.NoFill == null)
- if (typeof(DocumentFormat.OpenXml.Drawing.NoFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.NoFill = (DocumentFormat.OpenXml.Drawing.NoFill)element;
- }
- if (base.VisualShapeProp.SolidFill == null)
- if (typeof(DocumentFormat.OpenXml.Drawing.SolidFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.SolidFill = (DocumentFormat.OpenXml.Drawing.SolidFill)element;
- }
- if (base.VisualShapeProp.GradientFill == null)
- if (typeof(DocumentFormat.OpenXml.Drawing.GradientFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.GradientFill = (DocumentFormat.OpenXml.Drawing.GradientFill)element;
- }
- if (base.VisualShapeProp.PatternFill == null)
- if (typeof(DocumentFormat.OpenXml.Drawing.PatternFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.PatternFill = (DocumentFormat.OpenXml.Drawing.PatternFill)element;
- }
- if (base.VisualShapeProp.GroupFill == null)
- if (typeof(DocumentFormat.OpenXml.Drawing.GroupFill).Equals(element.GetType()))
- {
- base.VisualShapeProp.GroupFill = (DocumentFormat.OpenXml.Drawing.GroupFill)element;
- }
- }
- }
- }
- }
- }
- }
|