Example #1
0
        public virtual void Visit(StringBuilder sb, BuildContext context)
        {
            bool containsFunct = false;
            int  numFuncts     = 0;

            if (sb == null || context == null || contents == null || contents.Length == 0)
            {
                return;
            }

            // copy contents into list
            List <IBuildable> ctxCopy = new List <IBuildable>(contents);

            // check if the Sigma operator acts as a sum
            if (context.Options.TreatSigmaAsSum)
            {
                // look for the plain sigma operator
                int index;
                while ((index = ctxCopy.FindIndex(a => (a is Mo && (a as Mo).IsSigma))) != -1)
                {
                    // so long as it is not the last element
                    if (index != ctxCopy.Count - 1)
                    {
                        BuildablePlainSum bps = new BuildablePlainSum(ctxCopy[index + 1]);
                        ctxCopy.RemoveAt(index + 1);
                        ctxCopy[index] = bps;

                        context.AddSum(bps);
                    }
                }
            }

            // check if delta <mo> appears before an <mi>
            if (context.Options.DeltaPartOfIdent)
            {
                int index;
                while ((index = ctxCopy.FindIndex(a => (a is Mo && (a as Mo).IsDelta))) != -1)
                {
                    // check that it's not the last element
                    if (index != ctxCopy.Count - 1)
                    {
                        // check that the next element is <mi>
                        Mi mi = ctxCopy[index + 1] as Mi;
                        if (mi != null)
                        {
                            // change Mi's content to incorporate the delta
                            Mi newMi = new Mi("∆" + mi.Content);
                            ctxCopy[index + 1] = newMi;
                            // remove the delta
                            ctxCopy.RemoveAt(index);
                            Trace.WriteLine(newMi.Content);
                        }
                    }
                }
            }
            else
            {
                // change delta from mo to mi
                int index;
                while ((index = ctxCopy.FindIndex(a => (a is Mo && (a as Mo).IsDelta))) != -1)
                {
                    ctxCopy[index] = new Mi("∆");
                }
            }

            // Scan for functions.
            // If one is found, increment the number. Because nested
            // functions which do not contain parentheses (e.g. sin sin x)
            // are contained in the same row element (in non-Word generated markup),
            // this allows us to determine how many close parens we need.
            foreach (IBuildable v in ctxCopy)
            {
                if (v is Mi && Semantics.knownFuncts.Contains((v as Mi).Content))
                {
                    containsFunct = true;
                    numFuncts++;
                }
            }

            if (containsFunct)
            {
                // Only process if this is not the spurious function.
                if (ctxCopy.Count != 1 || (!Semantics.knownFuncts.Contains((ctxCopy[0] as Mi).Content)))
                {
                    foreach (IBuildable v in ctxCopy)
                    {
                        v.Visit(sb, context);
                    }

                    for (; numFuncts > 0; numFuncts--)
                    {
                        sb.Append(")");
                    }
                }
            }
            // No function was found, so process in the normal fashion.
            else
            {
                foreach (var v in ctxCopy)
                {
                    v.Visit(sb, context);
                }
            }
        }