private void RegisterTexFont(TexFormula formula, string value, ref int position, TexFont font)
        {
            SkipWhiteSpace(value, ref position);
            if (position == value.Length)
            {
                return;
            }
            var style = TexUtility.RenderFontStyle;

            if (value [position] == leftBracketChar)
            {
                style = ParseFontStyle(ReadGroup(value, ref position, leftBracketChar, rightBracketChar));
                SkipWhiteSpace(value, ref position);
            }

            var oldType  = TexUtility.RenderFont;
            var oldStyle = TexUtility.RenderFontStyle;

            TexUtility.RenderFont      = font.index;
            TexUtility.RenderFontStyle = style;
            var parsed = Parse(ReadGroup(value, ref position, leftGroupChar, rightGroupChar)).GetRoot;

            TexUtility.RenderFont      = oldType;
            TexUtility.RenderFontStyle = oldStyle;
            formula.Add(parsed);
        }
Beispiel #2
0
        private void ProcessEscapeSequence(TexFormula formula, string value, ref int position)
        {
            position++;

            var command = LookForAWord(value, ref position);

            // Check if there's no command
            if (command.Length == 0)
            {
                if (position < value.Length)
                {
                    var nextChar = value[position];
                    if (IsParserReserved(nextChar))
                    {
                        formula.Add(ConvertCharacter(value, ref position, nextChar));
                    }
                }
                return;
            }

            //Check if the command registered in Commands
            if (isCommandRegistered(command))
            {
                formula.Add(AttachScripts(formula, value, ref position,
                                          ProcessCommand(formula, value, ref position, command)));
                return;
            }

            //Check if the command registered in FontID
            var fontID = TEXPreference.main.GetFontIndexByID(command);

            if (fontID != -1)
            {
                SkipWhiteSpace(value, ref position);
                formula.Add(ParseFontStyle(value, ref position, fontID,
                                           ParseFontStyle(ReadInsideBracket(value, ref position))));
                return;
            }

            SymbolAtom symbolAtom = SymbolAtom.GetAtom(command);

            //Check if the command registered in Symbol database

            if (symbolAtom != null)
            {
                // Symbol was found.
                if (symbolAtom.RightType == CharType.Accent && formula.RootAtom != null)
                {
                    //Accent is Found
                    Atom baseAtom = formula.RootAtom;
                    if (baseAtom is RowAtom)
                    {
                        var row = (RowAtom)baseAtom;
                        baseAtom = MatrixAtom.Last(row.Elements);
                        row.Elements.RemoveAt(row.Elements.Count - 1);
                    }
                    else
                    {
                        formula.RootAtom = null;
                    }

                    formula.Add(AttachScripts(formula, value, ref position, AccentedAtom.Get(baseAtom, symbolAtom)));
                }
                else
                {
                    formula.Add(AttachScripts(formula, value, ref position, symbolAtom));
                }
                return;
            }

            //No lucks, now ...

            {
                // Command aren't defined, use it as command text style
                RowAtom row = RowAtom.Get();
                for (int i = 0; i < command.Length; i++)
                {
                    var charAtom = CharAtom.Get(command[i],
                                                TEXConfiguration.main.Typeface_Commands);
                    row.Add(charAtom);
                }
                formula.Add(AttachScripts(formula, value, ref position, row));
            }
        }
        private Atom ProcessCommand(TexFormula formula, string value, ref int position, string command)
        {
            SkipWhiteSpace(value, ref position);
            if (position == value.Length)
            {
                return(null);
            }

            switch (command)
            {
            case "meta":
                return(Meta(formula, value, ref position));

            case "root":
            case "sqrt":
                return(Root(formula, value, ref position));

            case "vmatrix":
            case "matrix":
                return(Matrix(value, ref position, command == "vmatrix"));

            case "style":
            case "math":
            case "text":
                var idx   = command == "text" ? TEXConfiguration.main.Typeface_Text : (command == "math" ? -1 : -2);
                var style = ParseStyle(value, ref position);
                formula.Add(ParseFontStyle(value, ref position, idx, style), true);
                return(null);    // add by not making a separate block (hence wrappable).

            case "clr":
            case "mclr":
            case "color":
                formula.Add(Color(value, ref position, command == "color" ? 1 : (command == "clr" ? 0 : 2)), true);
                return(null);    // same reason with \style.

            case "bg":
            case "vbg":
                return(BgColor(value, ref position, command == "bg"));

            case "size":
                return(Size(value, ref position));

            case "link":
            case "ulink":
            case "vlink":
                return(Link(value, ref position, command == "ulink", command != "vlink"));

            case "trs":
            case "mtrs":
            case "ltrs":
                return(TRS(value, ref position, command == "trs" ? 0 : (command == "mtrs" ? 1 : 2)));

            case "under":
            case "over":
                return(Not(value, ref position, command == "under" ? 4 : 5, false));

            case "border":
            case "vborder":
                return(Border(value, ref position, command == "border"));
            }

            if (NotFamily.Contains(command))
            {
                return(Not(value, ref position, ParseNotMode(command[0], command[1]), true));
            }

            if (FracFamily.Contains(command))
            {
                int    FracAlignT = 0, FracAlignB = 0;
                bool   FracAlignN = true;
                string prefix     = command.Substring(0, command.Length - 4);
                if (prefix.Length > 0)
                {
                    if (prefix[0] == 'n')
                    {
                        FracAlignN = false;
                        prefix     = prefix.Substring(1);
                    }
                    if (prefix.Length == 1)
                    {
                        FracAlignT = fracP(prefix[0]);
                        FracAlignB = FracAlignT;
                    }
                    else if (prefix.Length == 2)
                    {
                        FracAlignT = fracP(prefix[0]);
                        FracAlignB = fracP(prefix[1]);
                    }
                }
                return(Frac(value, ref position, FracAlignN, (TexAlignment)FracAlignT, (TexAlignment)FracAlignB));
            }

            if (HoldFamily.Contains(command))
            {
                TexAlignment align    = TexAlignment.Center;
                bool         vertical = false;

                holdP(command.Substring(0, command.Length - 3), ref vertical, ref align);

                string size = ReadInsideBracket(value, ref position);
                if (position < value.Length && value[position] == leftGroupChar)
                {
                    return(HolderAtom.Get(Parse(ReadGroup(value, ref position, leftGroupChar, rightGroupChar))
                                          .ExtractRoot(), size, vertical, align));
                }
                else
                {
                    return(HolderAtom.Get(null, size, vertical, align));
                }
            }
            if (TableFamily.Contains(command))
            {
                bool   vertical = false;
                int    align    = 1 + 8 + 64;
                string prefix   = command.Substring(0, command.Length - 5);
                if (prefix.Length > 0)
                {
                    if (prefix[0] == 'v')
                    {
                        vertical = true;
                        prefix   = prefix.Substring(1);
                    }
                    if (prefix.Length == 1)
                    {
                        var pref = fracP(prefix[0]);
                        align = Math.Max(1, pref * 2) + Math.Max(8, pref * 16) + Math.Max(64, pref * 128);
                    }
                    else if (prefix.Length == 3)
                    {
                        var pref0 = fracP(prefix[0]);
                        var pref1 = fracP(prefix[1]);
                        var pref2 = fracP(prefix[2]);
                        align = Math.Max(1, pref0 * 2) + Math.Max(8, pref1 * 16) + Math.Max(64, pref2 * 128);
                    }
                }

                int lineStyleH = 0, lineStyleV = 0;
                if (value[position] == leftBracketChar)
                {
                    string lineOpt;
                    int    lineP = 0;
                    lineOpt = ReadGroup(value, ref position, leftBracketChar, rightBracketChar);
                    for (int i = 0; i < lineOpt.Length; i++)
                    {
                        if (!int.TryParse(lineOpt[i].ToString(), out lineP))
                        {
                            continue;
                        }
                        if (i >= 6)
                        {
                            break;
                        }
                        switch (i)
                        {
                        case 0:
                            lineStyleH += lineP >= 2 ? 17 : lineP;
                            break;

                        case 1:
                            lineStyleH += lineP >= 2 ? 10 : (lineP == 1 ? 2 : 0);
                            break;

                        case 2:
                            lineStyleH += lineP >= 1 ? 4 : 0;
                            break;

                        case 3:
                            lineStyleV += lineP >= 2 ? 17 : lineP;
                            break;

                        case 4:
                            lineStyleV += lineP >= 2 ? 10 : (lineP == 1 ? 2 : 0);
                            break;

                        case 5:
                            lineStyleV += lineP >= 1 ? 4 : 0;
                            break;
                        }
                    }
                    SkipWhiteSpace(value, ref position);
                }
                else
                {
                    lineStyleH = 7;
                    lineStyleV = 7;
                }

                return(Table(value, ref position, align, lineStyleH, lineStyleV, vertical));
            }
            throw new TexParseException("Invalid command.");
        }