bool IsItRegisteredCommand(string command) { if (command.Length == 1) { return(false); } command = command.Substring(1); if (TexFormulaParser.isCommandRegistered(command)) { return(true); } if (TEXPreference.main.GetFontIndexByID(command) >= 0) { return(true); } return(false); }
// This function can be called recursively private string Parse(string original) { if (string.IsNullOrEmpty(original)) { return(original); } original = original.Replace("\r", ""); var b = new StringBuilder(); int l = 0, e = 0, i = 0; bool onRTL = IsFirstRecognizableCharIsRTL(original, 0), commonlyRTL = onRTL; while (l < original.Length) { var c = original[l]; var p = l == 0 ? '\0' : original[l - 1]; var n = l == original.Length - 1 ? '\0' : original[l + 1]; bool ignored = IsIgnoredChar(c); // If character is a new line if (c == '\n') { if (e != l) { // This means something is left behind. Lets clear-em-up Insert(b, original.Substring(e, l - e), onRTL, commonlyRTL, ref i); e = l; } b.Append('\n'); e = ++l; i = b.Length; if (l < original.Length) { onRTL = commonlyRTL = IsFirstRecognizableCharIsRTL(original, l); } } // If character is an opening brace else if (c == '{' && p != '\\' && n != '}' && n != '\0') { if (e != l) { Insert(b, original.Substring(e, l - e), onRTL, commonlyRTL, ref i); e = l; } var substring = TexFormulaParser.ReadGroup(original, ref l, '{', '}'); e = l; var parsed = "{" + Parse(substring) + "}"; Insert(b, parsed, false, commonlyRTL, ref i); } // If character is a backslash (signals a command) (absolutely specific to TEXDraw behaviour) else if (c == '\\' && p != '\\' && char.IsLetter(n)) { if (e != l) { Insert(b, original.Substring(e, l - e), onRTL, commonlyRTL, ref i); e = l; } l++; var command = TexFormulaParser.LookForAWord(original, ref l); var param1 = string.Empty; var param2 = string.Empty; if (l >= original.Length) { break; } else if (command.Contains("frac")) { param1 = "{" + Parse(TexFormulaParser.ReadGroup(original, ref l, '{', '}')) + "}"; param2 = "{" + Parse(TexFormulaParser.ReadGroup(original, ref l, '{', '}')) + "}"; } else if (command == "rtl" || command == "ltr") { if (e != (l - 4)) { Insert(b, original.Substring(e, (l - 4) - e), onRTL, commonlyRTL, ref i); } SkipWhiteSpace(original, ref l); e = l; onRTL = commonlyRTL = command == "rtl"; continue; } else if (TexFormulaParser.isCommandRegistered(command) || TEXPreference.main.GetFontIndexByID(command) >= 0) { if (original[l] == '[') { if (command == "root") { param1 = "[" + Parse(TexFormulaParser.ReadGroup(original, ref l, '[', ']')) + "]"; } else { param1 = "[" + (TexFormulaParser.ReadGroup(original, ref l, '[', ']')) + "]"; } } if (l < original.Length && original[l] != '{' || !command.Contains("hold")) { param2 = "{" + Parse(TexFormulaParser.ReadGroup(original, ref l, '{', '}')) + "}"; } } var parsed = "\\" + command + param1 + param2; Insert(b, parsed, false, commonlyRTL, ref i); e = l; } // If character is a script sign (also specific to TEXDraw behaviour) else if (c == '_' || c == '^' && p != '\\') { if (e != l) { Insert(b, original.Substring(e, l - e), onRTL, false, ref i); e = l; } var iBackup = i; while (c == '_' || c == '^') { l++; var parsed = Parse(TexFormulaParser.ReadScriptGroup(original, ref l)); Insert(b, new string(c, 1) + parsed, onRTL, false, ref i); c = l == original.Length ? '\0' : original[l]; } e = l; if (commonlyRTL) { i = iBackup; } } // If character/space is different than current RTL mode else if ((IsRTLChar(c) != onRTL && !ignored) || (ignored && l < original.Length - 1 && (onRTL ^ commonlyRTL) && ((IsRTLChar(c = original[l + 1])) != onRTL))) { Insert(b, original.Substring(e, l - e), onRTL, commonlyRTL, ref i); onRTL = IsRTLChar(c); e = l++; } else { l++; } } Insert(b, original.Substring(e), onRTL, commonlyRTL, ref i); var result = b.ToString(); b.Length = 0; return(result); }