///Returning: 0-Not found, 1-Symbol, 2-Command, 3-Font, 4-Misc
    static int FindPossibleEscape(string text, int pos, out int start, out int end)
    {
        start = 0;
        end   = 0;
        pos--;
        if (text.Length <= 0 || pos < 0 || pos >= text.Length)
        {
            return(0);
        }
        if (TexFormulaParser.IsParserReserved(text[pos]))
        {
            if (pos == 0 || text[pos - 1] != '\\')
            {
                if (pos == 0 || text[pos] != '\\')
                {
                    return(0);
                }
                else if (pos < text.Length - 1)
                {
                    pos++;
                }
                else
                {
                    start = pos - 1;
                    end   = pos;
                    return(4);
                }
            }
            else
            {
                start = pos - 1;
                end   = pos;
                return(4);
            }
        }
        end = pos;
        while (true)
        {
            if (char.IsLetter(text[pos]))
            {
                pos--;
                if (pos >= 0)
                {
                    continue;
                }
                else
                {
                    return(0);
                }
            }
            if (text[pos] == '\\')
            {
                if (pos == 0 || text[pos - 1] != '\\')
                {
                    start = pos;
                    break;
                }
            }
            return(0);
        }
        while (end < text.Length && char.IsLetter(text[end]))
        {
            end++;
        }
        end--;
        var s = text.Substring(start + 1, end - start);

        if (TEXPreference.main.GetFontIndexByID(s) >= 0)
        {
            return(3);
        }
        if (TexFormulaParser.isCommandRegistered(s))
        {
            return(2);
        }
        if (TEXPreference.main.GetChar(s) != null)
        {
            return(1);
        }
        return(4);
    }
Example #2
0
    public override string ReplaceString(string original)
    {
        blocks.Clear();

        int line = 0;

        original = m_pattern.Replace(original, (match) =>
        {
            if (!match.Groups[5].Success && match.Length > 2)
            {
                // skip commands, fonts, big operators, delimiter
                var g = match.Groups[1];
                int s;
                var m = TEXPreference.main;
                if (!g.Success || TexFormulaParser.isCommandRegistered(g.Value) ||
                    m.GetFontIndexByID(g.Value) >= 0)
                {
                    return(match.Value);
                }
                else if ((s = m.symbols.GetValue(g.Value)) > 0 &&
                         (m.GetChar(s).nextLargerExist || m.GetChar(s).extensionExist))
                {
                    return(match.Value);
                }
                else if (match.Length > g.Length + 1)
                {
                    // extra spaces can't be tolerated
                    // alternate to custom logic
                    return(Group2WithSpaceElimination(match, g));
                }
            }

            var cmd   = "\\vlink[" + blocks.Count + "]";
            Block dyn = new Block()
            {
                index  = blocks.Count,
                start  = match.Index,
                length = match.Length,
                // see text input cursor
                lineSeparator = -1,
            };

            if (match.Value == "\n")
            {
                dyn.lineSeparator = line++;
                blocks.Add(dyn);
                return("\n" + cmd + "{}");
            }
            else if (match.Value == "\\")
            {
                blocks.Add(dyn);
                return(cmd + "{\\\\}");
            }
            else if (match.Groups[4].Success)
            {
                dyn.start++;
                dyn.length = 0;
                blocks.Add(dyn);
                return("{" + cmd + "{" + emptyPlaceholder + "}}");
            }
            else
            {
                blocks.Add(dyn);
                return(cmd + "{" + match.Value + "}");
            }
        });

        {
            // keep things stable by capturing words
            original = m_wrapholder.Replace(original, @"{$1}");
            // sanitize invalid scripts
            original = m_scriptEscape.Replace(original, @"{$1}$2");
        }
        return(original);
    }