123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943 |
- using DocumentFormat.OpenXml;
- using DocumentFormat.OpenXml.Drawing;
- using DocumentFormat.OpenXml.Drawing.Charts;
- using DocumentFormat.OpenXml.Packaging;
- using DocumentFormat.OpenXml.Presentation;
- using HTEXLib.Helpers.ShapeHelpers;
- using HTEXLib.Models.Inner;
- using System;
- using System.Collections.Generic;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Xml.Linq;
- namespace HTEXLib.Models.HTEX
- {
- public class HtexChart : HtexElement
- {
- public HtexChart(string id, double rot, double width, double height,
- double top, double left, bool invisible,
- bool animatable, int index, DocumentFormat.OpenXml.Drawing.Charts.ChartReference Chart, PPTSlide slide, string partForm, string timingId)
- {
- base.slide = slide;
- this.rot = rot;
- this.Chart = Chart;
- base.id = id;
- base.top = top;
- base.left = left;
- base.width = width;
- base.height = height;
- base.invisible = invisible;
- base.animatable = animatable;
- base.index = index;
- base.type = "Chart";
- base.partForm = partForm;
- base.sid = timingId;
- }
- public ChartPart chartPart { get; set; }
- public string VerticalAlign { get; set; }
- public DocumentFormat.OpenXml.Drawing.Charts.ChartReference Chart { get; set; }
- public override List<Item> DrawElement()
- {
- var ChartNode = slide.SlidePart.Parts.Where(x => x.RelationshipId == Chart.Id).FirstOrDefault();
- var url = ChartNode.OpenXmlPart.Uri.ToString().Replace("../", "/ppt/");
- var ChartParts = slide.SlidePart.GetPartsOfType<DocumentFormat.OpenXml.Packaging.ChartPart>();
- if (ChartParts != null) {
- var chart = ChartParts.Where(x => x.Uri.ToString() == url).FirstOrDefault() ;
- chartPart = chart;
- if (chart != null) {
- ChartColorStylePart ChartColorStylePart = null;
- ChartStylePart ChartStylePart = null;
- foreach (var idp in chart.Parts) {
- if (idp.OpenXmlPart is ChartColorStylePart ChartColorStyleParts) {
- ChartColorStylePart = ChartColorStyleParts;
- }
- if (idp.OpenXmlPart is ChartStylePart ChartStyleParts)
- {
- ChartStylePart = ChartStyleParts;
- }
- }
- if (ChartStylePart != null) {
- var ChartStyleChildren = ChartStylePart.ChartStyle.ChildElements;
- foreach (var child in ChartStyleChildren)
- {
- if (child is DocumentFormat.OpenXml.Office2013.Drawing.ChartStyle.StyleEntry StyleEntry)
- {
- DoStyleEntry(StyleEntry);
- }
- ///OfficeArtExtensionList cs: extLst
- // MarkerLayoutProperties cs: dataPointMarkerLayout
- }
- }
-
- Chart charts= DrawChart(chart.ChartSpace);
- return new List<Item> { charts };
- }
- }
- return null;
- }
- public Chart DrawChart(DocumentFormat.OpenXml.Drawing.Charts.ChartSpace chartSpace) {
- var Chart = chartSpace.GetFirstChild<DocumentFormat.OpenXml.Drawing.Charts.Chart>();
- var ShapeProperties = chartSpace.GetFirstChild<DocumentFormat.OpenXml.Drawing.Charts.ShapeProperties>();
- HTEXLib.Models.HTEX.ShapeStyle shapeStyleChart= PPTXHelper.DoShapeProperties(ShapeProperties,slide,type,partForm);
- var TextProperties = chartSpace.GetFirstChild<TextProperties>();
- var style = chartSpace.GetFirstChild<DocumentFormat.OpenXml.Drawing.Charts.Style>();
- var ColorMapOverride = chartSpace.GetFirstChild<DocumentFormat.OpenXml.Drawing.Charts.ColorMapOverride>();
- var style2010 = chartSpace.GetFirstChild<DocumentFormat.OpenXml.Office2010.Drawing.Charts.Style>();
- var UserShapesReference = chartSpace.GetFirstChild<UserShapesReference>();
- var charts = DoPlotArea(Chart.PlotArea);
- ShapeStyle shapeStyle = null;
- if (Chart.Title != null)
- {
- var ChartShapeProperties = Chart.Title.ChartShapeProperties;
- shapeStyle = PPTXHelper.DoShapeProperties(ChartShapeProperties, slide, type, partForm, chartPart);
- }
- var(PPTParagraphs, tBody) = DoChartTitle(Chart.Title);
- TextBody textBody = DrawText(PPTParagraphs, tBody);
- Shape shape= new Shape { type = "Sp", shapeType = "rect", textBody = textBody };
- shape.style.fill = shapeStyle != null ? shapeStyle.fill : null;
- shape.style.border = shapeStyle != null ? shapeStyle.border : null;
- var chart= new Chart {sid=sid, id = this.id, charts =charts,title=shape};
- Position position = new Position { cx = width, cy = height, x = left, y = top, rot = rot };
- chart.style.position = position;
- chart.style.fill = shapeStyleChart != null ? shapeStyleChart.fill : null;
- chart.style.border = shapeStyleChart != null ? shapeStyleChart.border : null;
- chart.style.effect= shapeStyleChart != null ? shapeStyleChart.effect : null;
- if (chart.style.fill.type == -1|| chart.style.fill.type == 0)
- {
- chart.style.fill = null;
- }
- if (chart.style.border != null && chart.style.border.type == "none")
- {
- chart.style.border = null;
- }
- return chart;
- }
- public (LinkedList<PPTParagraph> paragraphs, TextBody textBody) DoChartTitle(DocumentFormat.OpenXml.Drawing.Charts.Title title)
- {
- if (title==null ||title.ChartText == null)
- {
- return (null, null);
- }
- LinkedList<PPTParagraph> Texts = new LinkedList<PPTParagraph>();
- var BodyProperties = title.ChartText.RichText.BodyProperties;
-
- var ListStyle = title.ChartText.RichText.ListStyle;
- var Layout = title.Layout;
-
- if (BodyProperties == null)
- {
- BodyProperties = title.TextProperties.BodyProperties;
- }
- if (ListStyle != null&& title.TextProperties!=null)
- {
- ListStyle = title.TextProperties.ListStyle;
- }
- if (ListStyle != null)
- {
- //TODO文本层级内容
- }
- if (BodyProperties.Anchor != null)
- {
- string VerticalAlign = BodyProperties.Anchor.Value.ToString();
- }
- int fontScale = 100000;
- double LineSpaceReduction = 0;
- TextBody textBody= fillPropertiesFromMasterShape(title.ChartText.RichText, ref fontScale ,ref LineSpaceReduction);
- textBody.lnSpRn = LineSpaceReduction;
- var ExtensionList = title.ExtensionList;
- var Paragraphs = title.ChartText.RichText.Elements<DocumentFormat.OpenXml.Drawing.Paragraph>();
- int buIndex = 1;
- BuChar buChar = new BuChar();
- foreach (var paragraph in Paragraphs)
- {
- PlaceholderShape placeholder = null;
- var par = new PPTParagraph(slide, placeholder,partForm)
- {
- Paragraph = index++
- };
- if (paragraph.ParagraphProperties != null)
- {
- int level = paragraph.ParagraphProperties.Level == null ?
- -1 : paragraph.ParagraphProperties.Level.Value;
- par.Level = level;
- }
- par.SetParagraphProperties(paragraph, slide.SlidePart,
- slide.shapeListStyleMaster, slide.shapeListStyleLayout,buChar);
- bool hasText = false;
- foreach (var obj in paragraph.ChildElements)
- {
- hasText = GetParagraphChildElements(title.ChartText.RichText, par, hasText, obj, fontScale);
- }
- //This is because when we set paragraph properties we add the bullet to the text runs.
- //If we don't have text it still outputs the bullet character.
- if (par.bullet != null && hasText)
- {
- par.bullet.FontSize = par.RunPropList[0].FontSize;
- par.bullet.FontColor = par.RunPropList[0].FontColor;
- par.bullet.bullIndex = buChar.index - 1;
- // par.RunPropList.Insert(0, par.bullet);
- }
- else
- {
- if (buChar != null)
- {
- buChar.index = 1;
- }
- }
- Texts.AddLast(par);
- }
- return (Texts,textBody);
- }
- private TextBody fillPropertiesFromMasterShape(DocumentFormat.OpenXml.Drawing.Charts.TextBodyType TxBody/*, bool isLayout, bool addListStyle*/ , ref int fontScale ,ref double LineSpaceReduction)
- {
- TextBody textBody = new TextBody();
- if (null != TxBody)
- {
- // if (TxBody.ListStyle != null && addListStyle)
- // {
- // if (isLayout)
- // slide.shapeListStyleLayout = TxBody.ListStyle;
- // else
- // slide.shapeListStyleMaster = TxBody.ListStyle;
- // }
- if (TxBody.BodyProperties != null && TxBody.BodyProperties.Anchor != null)
- textBody.anchor = TxBody.BodyProperties.Anchor;
- if (TxBody.BodyProperties.TopInset != null)
- {
- textBody.top = System.Math.Round((double)TxBody.BodyProperties.TopInset.Value * 1.0 / 12700, Globals.degree);
- }
- if (TxBody.BodyProperties.BottomInset != null)
- {
- textBody.bottom = System.Math.Round((double)TxBody.BodyProperties.BottomInset.Value * 1.0 / 12700, Globals.degree);
- }
- if (TxBody.BodyProperties.RightInset != null)
- {
- textBody.right = System.Math.Round((double)TxBody.BodyProperties.RightInset.Value * 1.0 / 12700, Globals.degree);
- }
- if (TxBody.BodyProperties.LeftInset != null)
- {
- textBody.left = System.Math.Round((double)TxBody.BodyProperties.LeftInset.Value * 1.0 / 12700, Globals.degree);
- }
- if (TxBody.BodyProperties != null &&
- TxBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>() != null && TxBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>().FontScale != null)
- {
- fontScale = TxBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>().FontScale.Value;
- }
- if (TxBody.BodyProperties != null &&
- TxBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>() != null && TxBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>().LineSpaceReduction != null)
- {
- LineSpaceReduction = TxBody.BodyProperties.GetFirstChild<DocumentFormat.OpenXml.Drawing.NormalAutoFit>().LineSpaceReduction.Value;
- }
- if (TxBody.BodyProperties.AnchorCenter != null)
- {
- textBody.anchorCtr = TxBody.BodyProperties.AnchorCenter;
- }
- if (TxBody.BodyProperties.RightToLeftColumns != null)
- {
- textBody.rtlCol = TxBody.BodyProperties.RightToLeftColumns;
- }
- if (TxBody.BodyProperties.Wrap != null)
- {
- textBody.wrap = TxBody.BodyProperties.Wrap;
- }
- if (TxBody.BodyProperties.Vertical != null)
- {
- textBody.vert = TxBody.BodyProperties.Vertical;
- }
- if (TxBody.BodyProperties.Rotation != null)
- {
- rot = TxBody.BodyProperties.Rotation.Value / 60000.0;
- }
- var elements = TxBody.BodyProperties.ChildElements;
- foreach (var element in elements)
- {
- if (element is NoAutoFit)
- {
- //此元素指定文本主体内的文本不应自动适合于边框。 自动拟合是当文本框内的文本被缩放以保持在文本框内时。
- textBody.autoFit = false;
- }
- if (element is NormalAutoFit normalAutoFit)
- {
- //每个形状的文本都停留在该形状的范围内
- textBody.autoFit = true;
- fontScale = normalAutoFit.FontScale != null ? normalAutoFit.FontScale.Value : 0;
- }
- if (element is ShapeAutoFit)
- {
- //每个形状的文本都停留在该形状的范围内。
- textBody.autoFit = true;
- }
- //TODO 3D Scene3DType Shape3DType FlatText
- }
- }
- return textBody;
- }
- public TextBody DrawText(LinkedList<PPTParagraph> Texts, TextBody textBody)
- {
- if (Texts == null) {
- return null;
- }
- List<Paragraph> Paragraphs = new List<Paragraph>();
- foreach (var par in Texts)
- {
- double paragraphTop = par.getSpaceBeforePoints();
- Paragraph paragraph = new HTEXLib.Paragraph { /*animatable = par.Animatable, invisible = par.Invisible */};
-
- paragraph.style.hori = par.Align;
- paragraph.style.vert = VerticalAlign;
- if (par.bullet != null)
- {
- var bullet = par.bullet;
- paragraph.buChar = new BuChar
- {
- type=bullet.BulletType,
- //left = bullet.Left,
- buchar = bullet.Text,
- color = bullet.FontColor,
- typeface = bullet.FontFamily,
- size = bullet.FontSize,
- };
- }
- double newTop = par.getSpaceBeforePoints();
- int left = 0;
- List<HtexText> textElements = new List<HtexText>();
- List<Text> texts = new List<Text>();
- if (par.RunPropList == null || par.RunPropList.Count == 0 && par.defaultRunProperties != null) //Only paragraph!
- {
- float points = float.Parse(par.defaultRunProperties.FontSize.ToString()) * 72.0F / 96.0F;
- System.Drawing.Font font = new System.Drawing.Font(par.defaultRunProperties.FontFamily.ToString(), points);
- newTop = font.Height;
- }
- List<HtexText> processedElements = new List<HtexText>();
- IEnumerable<PPTRunProperties> pPTRunProperties = breakTextsToShape(par);
- foreach (var text in pPTRunProperties)
- {
- float points = float.Parse(text.FontSize.ToString()) * 72.0F / 96.0F;
- System.Drawing.Font font = new System.Drawing.Font(text.FontFamily.ToString(), points);
- if (text.Bold) font = new System.Drawing.Font(text.FontFamily.ToString(), points, System.Drawing.FontStyle.Bold);
- else if (text.Italic)
- font = new System.Drawing.Font(text.FontFamily.ToString(), points, System.Drawing.FontStyle.Italic);
- else if (text.Underline != null && text.Underline.Equals("Single"))
- font = new System.Drawing.Font(text.FontFamily.ToString(), points, System.Drawing.FontStyle.Underline);
- newTop = font.Height > newTop ? font.Height : newTop;
- newTop = par.getLineSpacingInPointsFromFont(newTop);
- if (text.isBreak)
- {
- top += newTop;
- left = 0;
- fixLeftSpacingForAlignment(processedElements, par, font);
- processedElements.Clear();
- continue;
- }
- String currentString = text.Text.TrimEnd() + getStringFromTextElements(processedElements);
- HtexText t1 = new HtexText(left: left,
- top: top,
- fontFamily: text.FontFamily,
- fontColor: text.FontColor,
- fontSize: text.FontSize,
- isBullet: text.isBullet,
- bold: text.Bold,
- italic: text.Italic,
- underline: text.Underline,
- id: id,
- slideIndex: slide.slideIndex)
- {
- Rotate = this.rot
- };
- t1.width = MeasureString(text.Text, font);
- if (text.isBullet && text.Text != null && text.Text.Contains("rId"))
- {
- // t1.PictureBullet = true;
- t1.width = text.bulletSize;
- t1.bulletSize = text.bulletSize;
- newTop = text.bulletSize;
- }
- t1.Text = text.Text;
- textElements.Add(t1);
- RunStyle runStyle = PPTXHelper.DoRunProperties(text.runProp, slide, "text", partForm);
- if (runStyle.border != null && (runStyle.border.color.type == -1 || runStyle.border.color.type == 0))
- {
- runStyle.border.color = null;
- }
- texts.Add(new Text
- {
-
- content = t1.Text,
- link=text.link,
- linkType=text.linkType,
- style = new FontStyle
- {
- fill = runStyle.fill.type == -1 || runStyle.fill.type == 0 ? null : runStyle.fill,
- border = runStyle.border,
- effect = runStyle.effect,
- align = par.FontAlign,
- spacing = par.defTabSize,
- color = text.FontColor,
- family = text.FontFamily,
- //top = top,
- // left = left,
- size = text.FontSize,
- // isBullet = text.isBullet,
- underline = t1.underline,
- italic = text.Italic,
- bold = text.Bold,
- //rot = this.rot,
- //width = t1.width,
- // pictureBullet = t1.PictureBullet,
- //bulletSize = t1.bulletSize
- }
- });
- processedElements.Add(t1);
- }
- fixLeftSpacingForAlignment(processedElements, par);
- HtexText lastTxt = null;
- List<HtexText> mergedTextElements = new List<HtexText>();
- foreach (HtexText textElement in textElements)
- {
- if (lastTxt == null || !lastTxt.sameProps(textElement))
- mergedTextElements.Add(textElement);
- else
- mergedTextElements[mergedTextElements.Count - 1].Text += textElement.Text;
- lastTxt = textElement;
- }
- //foreach (HtexText textElement in mergedTextElements) {
- // shapeBuilder.Append(textElement.DrawElement());
- //}
- top += newTop;
- top += par.getSpaceAfterPoints(newTop);
- top += par.getSpaceBeforePoints(newTop);
- paragraph.texts = texts;
- Paragraphs.Add(paragraph);
- }
- if (textBody == null)
- {
- return null;
- }
- else {
- textBody.paragraphs = Paragraphs;
- }
- return textBody;
- }
- private String getStringFromTextElements(List<HtexText> elements)
- {
- if (elements == null || elements.Count == 0)
- return "";
- StringBuilder result = new StringBuilder();
- foreach (HtexText el in elements)
- {
- result.Append(el.Text);
- }
- return result.ToString();
- }//We have two similar methods - this one is better because it measures the whole string with the font.
- private void fixLeftSpacingForAlignment(List<HtexText> textElements, PPTParagraph par, System.Drawing.Font font)
- {
- double combinedWidth = 0;
- StringBuilder combinedText = new StringBuilder();
- foreach (HtexText textElement in textElements)
- {
- if (textElement.PictureBullet)
- combinedWidth += textElement.bulletSize;
- else
- combinedText.Append(textElement.Text);
- }
- double bulletOffset = 0;
- if (par.bullet != null && textElements.Count > 0 && !textElements[0].isBullet)
- {
- bulletOffset = par.bullet.bulletSize;
- combinedWidth += par.bullet.bulletSize;
- }
- combinedWidth += MeasureString(combinedText.ToString(), font);
- double firstLeft = 0;
- if ("Center".Equals(par.Align))
- firstLeft = ((this.width - par.Indent - bulletOffset - par.marginLeft - par.marginRight) - combinedWidth) / 2;
- else if ("Right".Equals(par.Align))
- firstLeft = (this.width - par.Indent - bulletOffset - par.marginLeft - par.marginRight) - combinedWidth;
- combinedText = new StringBuilder();
- combinedWidth = 0; //Now used only for picture bullets!
- foreach (HtexText textElement in textElements)
- {
- textElement.setLeft(firstLeft + par.Indent + bulletOffset + par.marginLeft + combinedWidth + MeasureString(combinedText.ToString(), font));
- if (textElement.PictureBullet)
- combinedWidth += textElement.bulletSize;
- else
- combinedText.Append(textElement.Text);
- }
- }//We have two similar methods. This is worse because it uses the Width property of each element instead of measuring the whole string.
- //There is mistake in the calculations coming from that and the difference is bigger when there are more elements.
- private void fixLeftSpacingForAlignment(List<HtexText> textElements, PPTParagraph par)
- {
- double combinedWidth = 0;
- foreach (HtexText textElement in textElements)
- combinedWidth += textElement.width;
- double bulletOffset = 0;
- if (par.bullet != null && textElements.Count > 0 && !textElements[0].isBullet)
- {
- combinedWidth += par.bullet.bulletSize;
- bulletOffset = par.bullet.bulletSize;
- }
- double currentLeft = 0;
- if ("Center".Equals(par.Align))
- currentLeft = ((this.width - par.Indent - bulletOffset - par.marginLeft - par.marginRight) - combinedWidth) / 2;
- else if ("Right".Equals(par.Align))
- currentLeft = (this.width - par.Indent - bulletOffset - par.marginLeft - par.marginRight) - combinedWidth;
- foreach (HtexText textElement in textElements)
- {
- textElement.setLeft(currentLeft + par.Indent + bulletOffset + par.marginLeft);
- currentLeft += textElement.width;
- }
- }
- private IEnumerable<PPTRunProperties> breakTextsToShape(PPTParagraph par)
- {
- List<PPTRunProperties> list = par.RunPropList;
- List<PPTRunProperties> result = new List<PPTRunProperties>();
- String previousToken = null;
- double bulletSize = 0;
- foreach (var text in list)
- {
- float points = float.Parse(text.FontSize.ToString()) * 72.0F / 96.0F;
- System.Drawing.Font font = new System.Drawing.Font(text.FontFamily.ToString(), points);
- if (text.Bold) font = new System.Drawing.Font(text.FontFamily.ToString(), points, System.Drawing.FontStyle.Bold);
- else if (text.Italic) font = new System.Drawing.Font(text.FontFamily.ToString(), points, System.Drawing.FontStyle.Italic);
- else if (text.Underline != null && text.Underline.Equals("Single"))
- font = new System.Drawing.Font(text.FontFamily.ToString(), points, System.Drawing.FontStyle.Underline);
- int size = 0;
- if (text.isBullet && text.Text != null && text.Text.Contains("rId"))
- bulletSize = text.bulletSize;
- else
- size = MeasureString((previousToken == null ? "" : previousToken + " ") + text.Text, font);
- if (text.isBreak || size + bulletSize < this.width - par.Indent - par.marginLeft - par.marginRight)//- Shape.LeftInset - Shape.RightInset)
- {
- if (text.Text != null && text.Text.Trim() != "" && !(text.isBullet && text.Text.Contains("rId")))
- {
- previousToken = (previousToken == null ? "" : previousToken) + text.Text;
- }
- if (text.isBreak)
- previousToken = null;
- result.Add(text);
- continue;
- }
- // previousToken = null;
- string[] tokens = text.Text.Split(' ');
- int index = 0;
- foreach (string token in tokens)
- {
- index++;
- int combinedSize = MeasureString((previousToken == null ? "" : previousToken + " ") + token, font);
- if (combinedSize + bulletSize > this.width - par.Indent - par.marginLeft - par.marginRight)//- Shape.LeftInset - Shape.RightInset)
- {
- PPTRunProperties temp = new PPTRunProperties(text);
- temp.Text = "";
- temp.isBreak = true;
- result.Add(temp);
- temp = new PPTRunProperties(text);
- temp.Text = index < tokens.Length ? token + " " : token;
- result.Add(temp);
- previousToken = token;
- }
- else
- {
- PPTRunProperties temp = new PPTRunProperties(text);
- temp.Text = index < tokens.Length ? token + " " : token;
- result.Add(temp);
- previousToken = (previousToken == null ? "" : previousToken + " ") + token;
- }
- }
- }
- return result;
- }
- public static int MeasureString(string s, System.Drawing.Font font)
- {
- s = s.Replace("\t", "aaaa");//TODO the replace is dirty hack for measuring tabulations
- StringFormat stringFormat = new StringFormat(StringFormat.GenericTypographic);
- CharacterRange[] rng = { new CharacterRange(0, s.Length) };
- stringFormat.SetMeasurableCharacterRanges(rng);
- Graphics g = Graphics.FromImage(new Bitmap(100, 100));
- //Use measure character ranges with a big box because we used this for measurement only
- //Later we might better use this for drawing the text.
- Region[] regions = g.MeasureCharacterRanges(s, font, new System.Drawing.Rectangle(0, 0, 10000, 3000), stringFormat);
- foreach (Region region in regions)
- {
- RectangleF rect = region.GetBounds(g);
- return (int)System.Math.Round(rect.Width);
- }
- return 0;
- //
- // SizeF result;
- // using (var image = new Bitmap(1, 1))
- // {
- // using (var g = Graphics.FromImage(image))
- // {
- // result = g.MeasureString(s, font);
- // }
- // }
- //
- // return result.ToSize();
- }
-
- private bool GetParagraphChildElements(RichText shape, PPTParagraph par, bool hasText, OpenXmlElement obj, int fontScale)
- {
- LinkedList<string> effectShapes = new LinkedList<string>();
- if (obj is Run)
- {
- Run run = (Run)obj;
- hasText = true;
- PPTRunProperties runProp = new PPTRunProperties(par.defaultRunProperties);
- runProp.Text = run.Text.Text;
- runProp.SetRunProperties(run.RunProperties, shape, ref effectShapes);
- runProp.FontSize = System.Math.Round(fontScale * runProp.FontSize / Globals.PercentageConstant);
- par.RunPropList.Add(runProp);
- }
- else if (obj is Field)
- {
- Field run = (Field)obj;
- hasText = true;
- PPTRunProperties runProp = new PPTRunProperties(par.defaultRunProperties);
- runProp.Text = run.Text.Text;
- runProp.SetRunProperties(run.RunProperties, shape, ref effectShapes);
- runProp.FontSize = System.Math.Round(fontScale * runProp.FontSize / Globals.PercentageConstant);
- par.RunPropList.Add(runProp);
- }
- else if (obj is Break)
- {
- Break aBreak = (Break)obj;
- PPTRunProperties runProp = new PPTRunProperties(par.defaultRunProperties);
- runProp.SetRunProperties(aBreak.RunProperties, shape, ref effectShapes);
- runProp.FontSize = System.Math.Round(fontScale * runProp.FontSize / Globals.PercentageConstant);
- runProp.isBreak = true;
- par.RunPropList.Add(runProp);
- }
- return hasText;
- }
- public List<CommonChart> DoPlotArea(PlotArea plotArea) {
- List<CommonChart> charts = new List<CommonChart>();
- foreach (var child in plotArea.ChildElements)
- {
- string key = child.LocalName;
- IEnumerable<XElement> serNodes = null;
- switch (key)
- {
- //break块中不可以随意更换,此条件用于归类不同从Chart
- case "pieChart":
- case "ofPieChart":
- case "pie3DChart":
- case "doughnutChart":
- PieChart pieChart = new PieChart { chartType = "pie" };
- if (key.Equals("pie3DChart"))
- {
- pieChart.is3D = true;
- }
- pieChart.pieType = key;
- if (key.Equals("ofPieChart"))
- {
- var ofPieType = child.GetTextByPath("c:ofPieType");
- if (ofPieType != null)
- {
- //ofPieChart-pie ofPieChart-bar 子母饼图
- pieChart.pieType += "-" + ofPieType.Attribute("val").Value;
- }
- }
- serNodes = child.GetTextByPathList("c:ser");
- pieChart.datas = ExtractChartData(serNodes);
- charts.Add(pieChart);
- break;
- case "lineChart":
- case "line3DChart":
- LineChart lineChart = new LineChart { chartType = "line" };
- if (key.Equals("line3DChart"))
- {
- lineChart.is3D = true;
- }
- lineChart.lineType = key;
- var LineGrouping = child.GetTextByPath("c:grouping");
- if (LineGrouping != null)
- {
- //standard stacked percentStacked
- lineChart.lineType += "-" + LineGrouping.Attribute("val").Value;
- }
- serNodes = child.GetTextByPathList("c:ser");
- lineChart.datas = ExtractChartData(serNodes);
- charts.Add(lineChart);
- break;
- case "barChart":
- case "bar3DChart":
- var barDir = child.GetTextByPath("c:barDir");
- if (barDir != null)
- {
- if (barDir.Attribute("val").Value.Equals("bar"))
- {
- BarChart barChart = new BarChart { chartType = "bar" };
- charts.Add(barChart);
- if (key.Equals("bar3DChart"))
- {
- barChart.is3D = true;
- }
- barChart.barType = key;
- var BarGrouping = child.GetTextByPath("c:grouping");
- if (BarGrouping != null)
- {
- //standard stacked percentStacked
- barChart.barType += "-" + BarGrouping.Attribute("val").Value;
- }
- serNodes = child.GetTextByPathList("c:ser");
- barChart.datas = ExtractChartData(serNodes);
- charts.Add(barChart);
- }
- else if (barDir.Attribute("val").Value.Equals("col"))
- {
- ColChart colChart = new ColChart { chartType = "col" };
- if (key.Equals("bar3DChart"))
- {
- colChart.is3D = true;
- }
- colChart.colType = key.Replace("bar", "col");
- var ColGrouping = child.GetTextByPath("c:grouping");
- if (ColGrouping != null)
- {
- //standard stacked percentStacked
- colChart.colType += "-" + ColGrouping.Attribute("val").Value;
- }
- serNodes = child.GetTextByPathList("c:ser");
- colChart.datas = ExtractChartData(serNodes);
- charts.Add(colChart);
- }
- }
- break;
- case "areaChart":
- case "area3DChart":
- AreaChart areaChart = new AreaChart { chartType = "area" };
- if (key.Equals("area3DChart"))
- {
- areaChart.is3D = true;
- }
- areaChart.areaType = key;
- var AreaGrouping = child.GetTextByPath("c:grouping");
- if (AreaGrouping != null)
- {
- //standard stacked percentStacked
- areaChart.areaType += "-" + AreaGrouping.Attribute("val").Value;
- }
- serNodes = child.GetTextByPathList("c:ser");
- areaChart.datas = ExtractChartData(serNodes);
- charts.Add(areaChart);
- break;
- case "scatterChart":
- case "bubbleChart":
- ScatterChart scatterChart = new ScatterChart { chartType = "scatter" };
- scatterChart.scatterType = key;
- if (key.Equals("scatterChart"))
- {
- var ScatterStyle = child.GetTextByPath("c:scatterStyle");
- if (ScatterStyle != null)
- {
- scatterChart.scatterType += "-" + ScatterStyle.Attribute("val").Value.Replace("Marker", "");
- }
- }
- serNodes = child.GetTextByPathList("c:ser");
- scatterChart.datas = ExtractChartData(serNodes);
- charts.Add(scatterChart);
- break;
- case "radarChart":
- RadarChart radarChart = new RadarChart { chartType = "radar" };
- radarChart.radarType = key;
- var RadarStyle = child.GetTextByPath("c:radarStyle");
- if (RadarStyle != null)
- {
- radarChart.radarType += "-" + RadarStyle.Attribute("val").Value;
- }
- serNodes = child.GetTextByPathList("c:ser");
- radarChart.datas = ExtractChartData(serNodes);
- charts.Add(radarChart);
- break;
- case "plotAreaRegion":
- PlotAreaChart plotAreaChart = new PlotAreaChart { chartType = "plotArea" };
- plotAreaChart.plotAreaType = key;
- var PlotSeries = child.GetTextByPath("cx:series");
- if (PlotSeries != null)
- {
- plotAreaChart.plotAreaType += "-" + PlotSeries.Attribute("val").Value;
- }
- serNodes = child.GetTextByPathList("c:ser");
- plotAreaChart.datas = ExtractChartData(serNodes);
- charts.Add(plotAreaChart);
- break;
- case "stockChart":
- StockChart stockChart = new StockChart { chartType = "stock" };
- stockChart.stockType = key;
- serNodes = child.GetTextByPathList("c:ser");
- stockChart.datas = ExtractChartData(serNodes);
- charts.Add(stockChart);
- break;
- case "surfaceChart":
- case "surface3DChart":
- SurfaceChart surfaceChart = new SurfaceChart { chartType = "surface" };
- if (key.Equals("surface3DChart"))
- {
- surfaceChart.is3D = true;
- }
- surfaceChart.surfaceType = key;
- var Wireframe = child.GetTextByPath("c:wireframe");
- if (Wireframe != null)
- {
- surfaceChart.surfaceType += "-" + Wireframe.Attribute("val").Value;
- }
- serNodes = child.GetTextByPathList("c:ser");
- surfaceChart.datas = ExtractChartData(serNodes);
- charts.Add(surfaceChart);
- break;
- }
- }
- return charts;
- }
- public List<Dictionary<string, object>> ExtractChartData(IEnumerable<XElement> nodes)
- {
- if (nodes != null)
- {
- List<Dictionary<string, object>> listDict = new List<Dictionary<string, object>>();
- foreach (XElement node in nodes)
- {
- if (node.GetTextByPath("c:xVal") != null)
- {
- Dictionary<string, object> dict = new Dictionary<string, object>();
- var xCvNodes = node.GetTextByPathList("c:xVal/c:numRef/c:numCache/c:pt/c:v");
- if (xCvNodes != null)
- {
- List<string> list = new List<string>();
- foreach (XElement cvNode in xCvNodes)
- {
- list.Add(cvNode.Value);
- }
- dict.Add("xAxis", list);
- }
- var yCvNodes = node.GetTextByPathList("c:yVal/c:numRef/c:numCache/c:pt/c:v");
- if (yCvNodes != null)
- {
- List<string> list = new List<string>();
- foreach (XElement cvNode in yCvNodes)
- {
- list.Add(cvNode.Value);
- }
- dict.Add("yAxis", list);
- }
- dict.Add("colName", "-");
- listDict.Add(dict);
- }
- else
- {
- Dictionary<string, object> dict = new Dictionary<string, object>();
- var spPr = node.GetTextByPath("c:spPr");
- if (spPr != null)
- {
- var ChartShapeProperties = new ChartShapeProperties(spPr.ToString());
- ShapeStyle shapeStyle = PPTXHelper.DoShapeProperties(ChartShapeProperties, slide, type, partForm,chartPart);
- dict.Add("colStyle", shapeStyle);
- }
- else {
- dict.Add("colStyle", null);
- }
- var colNameNode = node.GetTextByPath("c:tx/c:strRef/c:strCache/c:pt/c:v");
- if (colNameNode != null)
- {
- dict.Add("colName", colNameNode.Value);
-
- }
- //name
- var catNodes = node.GetTextByPathList("c:cat/c:strRef/c:strCache/c:pt/c:v");
- if (catNodes == null)
- {
- catNodes = node.GetTextByPathList("c:cat/c:numRef/c:numCache/c:pt/c:v");
- }
- if (catNodes != null)
- {
- List<string> list = new List<string>();
- foreach (XElement cvNode in catNodes)
- {
- list.Add(cvNode.Value);
- }
- dict.Add("xAxis", list);
- }
- //value
- var valNodes = node.GetTextByPathList("c:val/c:numRef/c:numCache/c:pt/c:v");
- if (valNodes != null)
- {
- List<string> list = new List<string>();
- foreach (XElement cvNode in valNodes)
- {
- list.Add(cvNode.Value);
- }
- dict.Add("yAxis", list);
- }
- listDict.Add(dict);
- }
- }
- return listDict;
- }
- return null;
- }
- public void DoStyleEntry(DocumentFormat.OpenXml.Office2013.Drawing.ChartStyle.StyleEntry StyleEntry)
- {
- //TODO
- /*
- * LineReference
- LineWidthScale
- FillReference
- EffectReference
- FontReference
- ShapeProperties
- TextCharacterPropertiesType
- TextBodyProperties
- OfficeArtExtensionList
- * */
- var LineReference = StyleEntry.LineReference;
- var LineWidthScale= StyleEntry.LineWidthScale;
- var FillReference= StyleEntry.FillReference;
- var EffectReference = StyleEntry.EffectReference;
- var FontReference= StyleEntry.FontReference;
- var ShapeProperties = StyleEntry.ShapeProperties;
- var TextCharacterPropertiesType = StyleEntry.TextCharacterPropertiesType;
- var TextBodyProperties = StyleEntry.TextBodyProperties;
- var OfficeArtExtensionList = StyleEntry.OfficeArtExtensionList;
- }
- }
-
- public class ChartStyle {
- //
- }
- }
|