Beispiel #1
0
        private Atom AttachScripts(
            TexFormula formula,
            SourceSpan value,
            ref int position,
            Atom atom,
            bool skipWhiteSpace,
            ICommandEnvironment environment)
        {
            if (skipWhiteSpace)
            {
                SkipWhiteSpace(value, ref position);
            }

            var initialPosition = position;

            if (position == value.Length)
            {
                return(atom);
            }

            // Check for prime marks.
            var primesRowAtom = new RowAtom(new SourceSpan(value.Source, position, 0));
            int i             = position;

            while (i < value.Length)
            {
                if (value[i] == primeChar)
                {
                    primesRowAtom = primesRowAtom.Add(SymbolAtom.GetAtom("prime", value.Segment(i, 1)));
                    position++;
                }
                else if (!IsWhiteSpace(value[i]))
                {
                    break;
                }
                i++;
            }

            var primesRowSource = new SourceSpan(
                value.Source,
                primesRowAtom.Source.Start,
                position - primesRowAtom.Source.Start);

            primesRowAtom = primesRowAtom.WithSource(primesRowSource);

            if (primesRowAtom.Elements.Count > 0)
            {
                atom = new ScriptsAtom(primesRowAtom.Source, atom, null, primesRowAtom);
            }

            if (position == value.Length)
            {
                return(atom);
            }

            TexFormula superscriptFormula = null;
            TexFormula subscriptFormula   = null;

            var ch = value[position];

            if (ch == superScriptChar)
            {
                // Attach superscript.
                position++;
                superscriptFormula = ReadScript(formula, value, ref position, environment);

                SkipWhiteSpace(value, ref position);
                if (position < value.Length && value[position] == subScriptChar)
                {
                    // Attach subscript also.
                    position++;
                    subscriptFormula = ReadScript(formula, value, ref position, environment);
                }
            }
            else if (ch == subScriptChar)
            {
                // Add subscript.
                position++;
                subscriptFormula = ReadScript(formula, value, ref position, environment);

                SkipWhiteSpace(value, ref position);
                if (position < value.Length && value[position] == superScriptChar)
                {
                    // Attach superscript also.
                    position++;
                    superscriptFormula = ReadScript(formula, value, ref position, environment);
                }
            }

            if (superscriptFormula == null && subscriptFormula == null)
            {
                return(atom);
            }

            // Check whether to return Big Operator or Scripts.
            var subscriptAtom   = subscriptFormula?.RootAtom;
            var superscriptAtom = superscriptFormula?.RootAtom;

            if (atom.GetRightType() == TexAtomType.BigOperator)
            {
                var source = value.Segment(atom.Source.Start, position - atom.Source.Start);
                if (atom is BigOperatorAtom typedAtom)
                {
                    return(new BigOperatorAtom(
                               source,
                               typedAtom.BaseAtom,
                               subscriptAtom,
                               superscriptAtom,
                               typedAtom.UseVerticalLimits));
                }

                return(new BigOperatorAtom(source, atom, subscriptAtom, superscriptAtom));
            }
            else
            {
                var source = new SourceSpan(value.Source, initialPosition, position - initialPosition);
                return(new ScriptsAtom(source, atom, subscriptAtom, superscriptAtom));
            }
        }
Beispiel #2
0
        private Atom AttachScripts(TexFormula formula, string value, ref int position, Atom atom)
        {
            if (position == value.Length)
            {
                return(atom);
            }
            if (value[position] == superScriptChar || value[position] == subScriptChar)
            {
                if (position == value.Length - 1)
                {
                    position++;
                    return(atom);
                }
            }
            else
            {
                return(atom);
            }

            TexFormula superscriptFormula = null;
            TexFormula subscriptFormula   = null;

            //0: Undetermined; 1: Not Yet; 2: Yes, we are; 3: Yes, and make it smaller
            int markAsBig = 0;
            //0: In Beginning;1: We are in _;2: we are in ^
            int lastIsSuper = 0;

            while (position < value.Length)
            {
                var ch = value[position];
                if (ch == superScriptChar || ch == subScriptChar)
                {
                    if (++position < value.Length && (value[position] == ch))
                    {
                        markAsBig = 2;
                        if (++position < value.Length && (value[position] == ch))
                        {
                            markAsBig = 3;
                            position++;
                        }
                    }
                    bool v = ch == superScriptChar;
                    if ((v ? superscriptFormula : subscriptFormula) == null)
                    {
                        lastIsSuper = v ? 2 : 1;
                    }
                    continue;
                }
                else if (ch == rightGroupChar || (value[position - 1] != '^' && value[position - 1] != '_'))
                {
                    break;
                }
                if (lastIsSuper == 2)
                {
                    if (superscriptFormula == null)
                    {
                        superscriptFormula = ReadScript(formula, value, ref position);
                    }
                    else
                    {
                        position--;
                        superscriptFormula.RootAtom = AttachScripts(formula, value, ref position, superscriptFormula.RootAtom);
                    }
                }
                else if (lastIsSuper == 1)
                {
                    if (subscriptFormula == null)
                    {
                        subscriptFormula = ReadScript(formula, value, ref position);
                    }
                    else
                    {
                        position--;
                        subscriptFormula.RootAtom = AttachScripts(formula, value, ref position, subscriptFormula.RootAtom);
                    }
                }
                else
                {
                    break;
                }
            }
            if (superscriptFormula == null && subscriptFormula == null)
            {
                return(atom);
            }

            // Check whether to return Big Operator or Scripts.
            if (atom != null && (atom.GetRightType() == CharType.BigOperator || markAsBig >= 2))
            {
                return(BigOperatorAtom.Get(atom, subscriptFormula == null ? null : subscriptFormula.GetRoot,
                                           superscriptFormula == null ? null : superscriptFormula.GetRoot, markAsBig == 3));
            }
            else
            {
                return(ScriptsAtom.Get(atom, subscriptFormula == null ? null : subscriptFormula.GetRoot,
                                       superscriptFormula == null ? null : superscriptFormula.GetRoot));
            }
        }