AddOMathStructs.cs
//
// This code is part of Document Solutions for Word demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Word;

namespace DsWordWeb.Demos
{
    // This example shows how to add the various Office Math structures
    // to the document content using the DsWord OMath API.
    // Note that at this time export of OMath content to PDF/images is not supported.
    public class AddOMathStructs
    {
        public GcWordDocument CreateDocx()
        {
            var doc = new GcWordDocument();
            doc.Body.AddParagraph("OMath content created in DsWord", doc.Styles[BuiltInStyleId.Title]);

            // Style for paragraphs containing OMath elements:
            var styleOmath = doc.Styles.Add("omath", doc.Styles[BuiltInStyleId.Title]);
            styleOmath.ParagraphFormat.Indentation.LeftIndent = 36;
            // Style for narrative text in OMath paragraphs:
            var styleText = doc.Styles.Add("text", StyleType.Character);
            styleText.Font.Size = 14;
            // Captions style:
            var styleCap1 = doc.Styles.Add("styleCap1", doc.Styles[BuiltInStyleId.Heading1]);
            styleCap1.ParagraphFormat.Spacing.SpaceBeforeAuto = false;
            styleCap1.ParagraphFormat.Spacing.SpaceBefore = 18;
            var capA = "\nExample 1:   ";
            var capB = "\nExample 2:   ";

            OMath addA(string caption)
            {
                doc.Body.AddParagraph(caption, styleCap1);
                var p = doc.Body.AddParagraph(styleOmath);
                p.AddRun(capA, styleText);
                return p.AddOMath();
            }
            OMath addB()
            {
                var p = doc.Body.AddParagraph(styleOmath);
                p.AddRun(capB, styleText);
                return p.AddOMath();
            }

            // OMathAccent
            var om = addA("OMathAccent");
            om.AddAccent("a", "\u20d7");
            om = addB();
            var accent = om.AddAccent("", "\u0308");
            accent.Base.AddFraction("2", "5", null);

            // OMathBar
            om = addA("OMathBar");
            om.AddBar("2x+1=3");
            om = addB();
            var bar = om.AddBar();
            bar.Base.AddFunction("cos", "2x");
            bar.Position = OMathVerticalJustification.Top;

            // OMathBorderBox
            om = addA("OMathBorderBox");
            om.AddBorderBox("xyz");
            om = addB();
            var bbox = om.AddBorderBox("2+2*");
            bbox.Base.AddFraction("3", "3", null);
            bbox.Base.AddRun(" = 5");
            bbox.HideLeft = true;
            bbox.HideTop = true;
            bbox.HideRight = true;
            bbox.HideBottom = true;
            bbox.StrikeBottomLeftToTopRight = true;
            bbox.StrikeTopLeftToBottomRight = true;

            // OMathBox
            om = addA("OMathBox");
            om.AddRun("a");
            var box = om.AddBox("==");
            om.AddRun("b");
            box.IsOperatorEmulator = true;
            om = addB();
            var nary = om.AddNary("x", "0", "1", "∭");
            box = nary.Base.AddBox("dx");
            box.IsDifferential = true;

            // OMathDelimiter
            om = addA("OMathDelimiter");
            om.AddDelimiter(new string[] {"a", "b", "c"});
            om = addB();
            var delimiter = om.AddDelimiter(new string[] { "5", "7" }, "[", "]", "\\");
            delimiter.Items.Add().AddRadical("n", "7");
            var styleNorm = doc.Styles.Add("Norm", StyleType.Character);
            styleNorm.Font.Italic = false;
            styleNorm.Font.Color.RGB = Color.Red;
            styleNorm.Font.Size = 20;

            // OMathEquationArray
            om = addA("OMathEquationArray");
            om.AddEquationArray(new string[] { "a", "b", "c" });
            om = addB();
            var ea = om.AddEquationArray();
            ea.Items.Add().AddDelimiter(new string[] { "x", "y", "z" });
            ea.Items.Add().AddFraction("π", "2", OMathFractionType.Skewed);

            // OMathFraction
            om = addA("OMathFraction");
            om.AddFraction("π", "4", null);
            om = addB();
            var fr = om.AddFraction();
            var func = fr.Numerator.AddFunction("sin", "");
            func.Argument.AddFraction("π", "3", null);
            fr.Denominator.AddRadical("e", "3");

            // OMathFunction
            om = addA("OMathFunction");
            om.AddFunction("ln", "2e");
            om = addB();
            var f = om.AddFunction("cos", "");
            f.Argument.AddFraction("π", "2", null);

            // OMathGroupCharacter
            om = addA("OMathGroupCharacter");
            om.AddGroupCharacter("mgh", "^");
            om = addB();
            var lu = om.AddLimitUpper("", "x");
            var gc = lu.Base.AddGroupCharacter("xyz", "\u23DE");
            gc.Position = OMathVerticalJustification.Top;
            gc.VerticalJustification = OMathVerticalJustification.Bottom;

            // OMathLimitLower
            om = addA("OMathLimitLower");
            om.AddLimitLower("abc", "z");
            om = addB();
            var ll = om.AddLimitLower();
            ll.Base.AddBorderBox("2x+3");
            ll.Limit.AddFraction("2", "5", null);

            // OMathLimitUpper
            om = addA("OMathLimitUpper");
            om.AddLimitUpper("abc", "y");
            om = addB();
            lu = om.AddLimitUpper();
            lu.Base.AddRadical("e", "3");
            lu.Limit.AddFunction("cos", "2x");

            // OMathMatrix
            om = addA("OMathMatrix");
            om.AddMatrix(new int?[2, 2] { { 1, 2 }, { 3, 4 } });
            om = addB();
            delimiter = om.AddDelimiter();
            var item = delimiter.Items.Add();
            var matrix = item.AddMatrix(3, 1);
            matrix.Rows[0].Cells[0].AddRun("U");
            matrix.Rows[0].Cells[0].AddDelimiter(new string[] { "t" });
            matrix.Rows[1].Cells[0].AddRun("V");
            matrix.Rows[1].Cells[0].AddDelimiter(new string[] { "t" });
            matrix.Rows[2].Cells[0].AddRun("W");
            matrix.Rows[2].Cells[0].AddDelimiter(new string[] { "t" });

            // OMathNary
            om = addA("OMathNary");
            om.AddNary("xⅆx", "0", "∞", "∭");
            om = addB();
            nary = om.AddNary("", "", "", "∏");
            nary.Subscript.AddFraction("π", "3", null);
            nary.Superscript.AddFraction("2π", "3", null);
            nary.Base.AddFunction("cos", "x");

            // OMathPhantom
            om = addA("OMathPhantom");
            var del = om.AddDelimiter(null, "<", ">", "");
            var ph = del.Items.Add().AddPhantom("argument", false);
            ph.Show = false;
            om = addB();
            var radical = om.AddRadical();
            radical.HideDegree = true;
            radical.HideDegree = true;
            ph = radical.Radicand.AddPhantom();
            ph.Show = true;
            ph.Argument.AddFraction("x", "y", null);
            ph.Smash = true;

            // OMathPreSubSuperscript
            om = addA("OMathPreSubSuperscript");
            om.AddPreSubSuperscript("Y", "1", "n");
            om = addB();
            var pss = om.AddPreSubSuperscript("", "-x", "2x");
            pss.Base.AddRadical("n", "").HideDegree = true;

            // OMathRadical
            om = addA("OMathRadical");
            om.AddRadical("x", "3");
            om = addB();
            radical = om.AddRadical();
            radical.Degree.AddFraction("2", "3", OMathFractionType.Skewed);
            radical.Radicand.AddFunction("cos", "2x");

            // OMathSubscript
            om = addA("OMathSubscript");
            om.AddSubscript("x", "linear");
            om = addB();
            func = om.AddFunction("", "2x+1");
            func.Name.AddSubscript("log", "3");

            // OMathSubSuperscript
            om = addA("OMathSubSuperscript");
            om.AddSubSuperscript("Y", "1", "n");
            om = addB();
            var subs = om.AddSubSuperscript("", "-x", "2x");
            subs.Base.AddRadical("n", "").HideDegree = true;

            // OMathSuperscript
            om = addA("OMathSuperscript");
            om.AddSuperscript("x", "2");
            om = addB();
            func = om.AddFunction();
            func.Name.AddSuperscript("cos", "2");
            var frac = func.Argument.AddFraction();
            frac.Numerator.AddRun("7x");
            frac.Denominator.AddRun("2π");

            // Done:
            return doc;
        }
    }
}