예제 #1
0
        public AccentedAtom(Atom baseAtom, string accentName)
        {
            BaseAtom   = baseAtom;
            AccentAtom = SymbolAtom.GetAtom(accentName);

            if (AccentAtom.Type != TexAtomType.Accent)
            {
                throw new ArgumentException(@"The specified symbol name is not an accent.", nameof(accentName));
            }
        }
예제 #2
0
        private Atom ConvertCharacter(TexFormula formula, string value, ref int position, char character)
        {
            position++;
            if (IsSymbol(character))
            {
                // Character is symbol.
                var symbolName = symbols[character];
                if (symbolName is null)
                {
                    throw new TexParseException($"Unknown character : '{character}'");
                }

                try
                {
                    return(SymbolAtom.GetAtom(symbolName));
                } catch (SymbolNotFoundException e)
                {
                    throw new TexParseException(
                              $"The character '{character}' was mapped to an unknown symbol with the name '{symbolName}'!", e);
                }
            }
            // Character is alpha-numeric.
            return(new CharAtom(character, formula.TextStyle));
        }
예제 #3
0
        internal static SymbolAtom GetDelimiterSymbol(string name)
        {
            var result = SymbolAtom.GetAtom(name);

            return(!result.IsDelimeter ? null : result);
        }
예제 #4
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();
            var i             = position + 1;

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

            // Attach prime marks as superscript, if any were found.
            if (primesRowAtom.Elements.Count > 0)
            {
                atom = new ScriptsAtom(atom, null, primesRowAtom);
            }

            TexFormula superscriptFormula = null;
            TexFormula subscriptFormula   = null;

            var ch = value[position];

            switch (ch)
            {
            case 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);
                }
                break;

            case 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);
                }
                break;
            }

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

            // Check whether to return Big Operator or Scripts.
            if (atom.GetRightType() == TexAtomType.BigOperator)
            {
                return(new BigOperatorAtom(atom, subscriptFormula?.RootAtom,
                                           superscriptFormula?.RootAtom));
            }
            return(new ScriptsAtom(atom, subscriptFormula?.RootAtom,
                                   superscriptFormula?.RootAtom));
        }
예제 #5
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.
                    if (isEnd)
                    {
                        result.Append(ch);
                        position++;
                    }
                    break;
                }

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

            var command = result.ToString();

            SymbolAtom symbolAtom = null;

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

            var predefinedFormula = GetFormula(command);

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

                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 + "'");
            }
        }