private Atom AttachScripts(TexFormula formula, string value, ref int position, Atom atom, bool skipWhiteSpace = true)
        {
            if (skipWhiteSpace)
            {
                SkipWhiteSpace(value, ref position);
            }

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

            // Check for prime marks.
            var primesRowAtom = new RowAtom();
            int i             = position;

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

            if (primesRowAtom.Elements.Count > 0)
            {
                atom = new ScriptsAtom(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);

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

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

            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)
            {
                if (atom is BigOperatorAtom)
                {
                    var typedAtom = (BigOperatorAtom)atom;
                    return(new BigOperatorAtom(typedAtom.BaseAtom, subscriptAtom, superscriptAtom,
                                               typedAtom.UseVerticalLimits));
                }

                return(new BigOperatorAtom(atom, subscriptAtom, superscriptAtom));
            }
            else
            {
                return(new ScriptsAtom(atom, subscriptAtom, superscriptAtom));
            }
        }
Exemple #2
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));
            }
        }
Exemple #3
0
        private Atom AttachScripts(TexFormula formula, string value, ref int position, Atom atom)
        {
            SkipWhiteSpace(value, ref position);

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

            // Check for prime marks.
            var primesRowAtom = new RowAtom();
            int i = position;
            while (i < value.Length)
            {
                if (value[i] == primeChar)
                {
                    primesRowAtom.Add(SymbolAtom.GetAtom("prime"));
                    position++;
                }
                else if (!IsWhiteSpace(value[i]))
                    break;
                i++;
            }

            if (primesRowAtom.Elements.Count > 0)
                atom = new ScriptsAtom(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);

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

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

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

            // Check whether to return Big Operator or Scripts.
            if (atom.GetRightType() == TexAtomType.BigOperator)
                return new BigOperatorAtom(atom, subscriptFormula == null ? null : subscriptFormula.RootAtom,
                    superscriptFormula == null ? null : superscriptFormula.RootAtom);
            else
                return new ScriptsAtom(atom, subscriptFormula == null ? null : subscriptFormula.RootAtom,
                    superscriptFormula == null ? null : superscriptFormula.RootAtom);
        }