Exemplo n.º 1
0
        private Atom ConvertCharacter(TexFormula formula, string value, ref int position, char character)
        {
            position++;
            if (IsSymbol(character))
            {
                // Character is symbol.
                var symbolName = symbols.ElementAtOrDefault(character);
                if (string.IsNullOrEmpty(symbolName))
                {
                    throw new TexParseException("Unknown character : '" + character.ToString() + "'");
                }

                try
                {
                    return(SymbolAtom.GetAtom(symbolName));
                }
                catch (SymbolNotFoundException e)
                {
                    throw new TexParseException("The character '"
                                                + character.ToString()
                                                + "' was mapped to an unknown symbol with the name '"
                                                + (string)symbolName + "'!", e);
                }
            }
            else
            {
                // Character is alpha-numeric.
                return(new CharAtom(character, formula.TextStyle));
            }
        }
Exemplo n.º 2
0
        private Atom ConvertCharacter(TexFormula formula, ref int position, SourceSpan source)
        {
            var character = source[0];

            position++;
            if (IsSymbol(character) && formula.TextStyle != TexUtilities.TextStyleName)
            {
                // Character is symbol.
                var symbolName = symbols.ElementAtOrDefault(character);
                if (string.IsNullOrEmpty(symbolName))
                {
                    throw new TexParseException($"Unknown character : '{character}'");
                }

                try
                {
                    return(SymbolAtom.GetAtom(symbolName, source));
                }
                catch (SymbolNotFoundException e)
                {
                    throw new TexParseException("The character '"
                                                + character.ToString()
                                                + "' was mapped to an unknown symbol with the name '"
                                                + symbolName + "'!", e);
                }
            }
            else // Character is alpha-numeric or should be rendered as text.
            {
                return(new CharAtom(source, character, formula.TextStyle));
            }
        }
Exemplo n.º 3
0
        public AccentedAtom(Atom baseAtom, string accentName)
        {
            this.BaseAtom   = baseAtom;
            this.AccentAtom = SymbolAtom.GetAtom(accentName);

            if (this.AccentAtom.Type != TexAtomType.Accent)
            {
                throw new ArgumentException("The specified symbol name is not an accent.", "accent");
            }
        }
Exemplo n.º 4
0
        internal static SymbolAtom GetDelimiterSymbol(string name)
        {
            var result = SymbolAtom.GetAtom(name);

            if (!result.IsDelimeter)
            {
                return(null);
            }
            return(result);
        }
Exemplo n.º 5
0
        public void PutDelimiterUnder(TexDelimiter delimiter, TexFormula subscriptName, TexUnit kernUnit, double kern)
        {
            var name = TexFormulaParser.DelimiterNames[(int)delimiter][(int)TexDelimeterType.Under];

            this.Formula.RootAtom = new OverUnderDelimiter(
                this._source,
                this.Formula.RootAtom,
                subscriptName?.RootAtom,
                SymbolAtom.GetAtom(name, null),
                kernUnit,
                kern,
                false);
        }
Exemplo n.º 6
0
        public void PutDelimiterUnder(TexDelimiter delimiter)
        {
            var name = TexFormulaParser.DelimiterNames[(int)delimiter][(int)TexDelimeterType.Under];

            this.Formula.RootAtom = new OverUnderDelimiter(
                this._source,
                this.Formula.RootAtom,
                null,
                SymbolAtom.GetAtom(name, null),
                TexUnit.Ex,
                0.0,
                false);
        }
Exemplo n.º 7
0
        internal static SymbolAtom GetDelimiterSymbol(string name, SourceSpan source)
        {
            if (name == null)
            {
                return(null);
            }

            var result = SymbolAtom.GetAtom(name, source);

            if (!result.IsDelimeter)
            {
                return(null);
            }
            return(result);
        }
Exemplo n.º 8
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));
            }
        }
Exemplo n.º 9
0
        public void PutDelimiterOver(TexDelimeter delimiter, TexFormula superscriptFormula, TexUnit kernUnit, double kern)
        {
            var name = TexFormulaParser.DelimiterNames[(int)delimiter][(int)TexDelimeterType.Over];

            this.Formula.RootAtom = new OverUnderDelimiter(this.Formula.RootAtom,
                                                           superscriptFormula == null ? null : superscriptFormula.RootAtom, SymbolAtom.GetAtom(name), kernUnit, kern,
                                                           true);
        }
Exemplo n.º 10
0
 public void AddSymbol(string name, TexAtomType type)
 {
     Add(new SymbolAtom(SymbolAtom.GetAtom(name), type));
 }
Exemplo n.º 11
0
 public void AddSymbol(string name)
 {
     Add(SymbolAtom.GetAtom(name));
 }
Exemplo n.º 12
0
        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));
            }
        }
Exemplo n.º 13
0
 public void AddSymbol(string name, TexAtomType type)
 {
     this.Add(new SymbolAtom(null, SymbolAtom.GetAtom(name, null), type));
 }
Exemplo n.º 14
0
        private void ProcessEscapeSequence(TexFormula formula, string value, ref int position)
        {
            var result = new StringBuilder();

            position++;
            while (position < value.Length)
            {
                var ch    = value[position];
                var isEnd = position == value.Length - 1;
                if (!char.IsLetter(ch) || isEnd)
                {
                    // Escape sequence has ended
                    // Or it's a symbol. Assuming in this case it will only be a single char.
                    if (isEnd || result.Length == 0)
                    {
                        result.Append(ch);
                        position++;
                    }
                    break;
                }

                result.Append(ch);
                position++;
            }

            var command = result.ToString();

            SymbolAtom symbolAtom        = null;
            TexFormula predefinedFormula = null;

            try
            {
                symbolAtom = SymbolAtom.GetAtom(command);
            }
            catch (SymbolNotFoundException)
            {
            }

            try
            {
                predefinedFormula = GetFormula(command);
            }
            catch (FormulaNotFoundException)
            {
            }

            if (symbolAtom != null)
            {
                // Symbol was found.

                if (symbolAtom.Type == TexAtomType.Accent)
                {
                    TexFormulaHelper helper        = new TexFormulaHelper(formula);
                    TexFormula       accentFormula = ReadScript(formula, value, ref position);
                    helper.AddAccent(accentFormula, symbolAtom.Name);
                }
                else
                {
                    formula.Add(AttachScripts(formula, value, ref position, symbolAtom));
                }
            }
            else if (predefinedFormula != null)
            {
                // Predefined formula was found.

                formula.Add(AttachScripts(formula, value, ref position, predefinedFormula.RootAtom));
            }
            else if (command.Equals("nbsp"))
            {
                // Space was found.

                formula.Add(AttachScripts(formula, value, ref position, new SpaceAtom()));
            }
            else if (textStyles.Contains(command))
            {
                // Text style was found.

                SkipWhiteSpace(value, ref position);
                var styledFormula = Parse(ReadGroup(formula, value, ref position, leftGroupChar, rightGroupChar));
                styledFormula.TextStyle = command;
                formula.Add(AttachScripts(formula, value, ref position, styledFormula.RootAtom));
            }
            else if (commands.Contains(command))
            {
                // Command was found.

                formula.Add(AttachScripts(formula, value, ref position, ProcessCommand(formula, value, ref position,
                                                                                       command)));
            }
            else
            {
                // Escape sequence is invalid.
                throw new TexParseException("Unknown symbol or command or predefined TeXFormula: '" + command + "'");
            }
        }