/// <summary> /// Creates formula from Xml element /// </summary> /// <param name="e">Element</param> /// <param name="creator">Creator of symbols</param> /// <returns>Formula</returns> public static MathFormula CreateFormula(XElement e, IXmlSymbolCreator creator) { MathFormula f = new MathFormula((byte)0); IEnumerable <XElement> l = e.GetElementsByTagName("S"); foreach (XElement el in l) { if (el.Parent != e) { continue; } MathSymbol s = MathSymbol.CreateSymbol(el, creator); s.AppendWithChildren(f); } return(f); }
/// <summary> /// Converts math formula /// </summary> /// <param name="formula">Initial formula</param> /// <returns>Conversion result</returns> public MathFormula Convert(MathFormula formula) { MathFormula form = formula.Copy(); for (int i = 0; i < form.Count; i++) { List <MathFormula> ch = form[i].Children; if (ch == null) { break; } List <MathFormula> nch = new List <MathFormula>(); foreach (MathFormula ff in ch) { MathFormula fch = Convert(ff); if (fch == null) { break; } nch.Add(fch); } if (nch.Count > 0) { form[i].Children = nch; } } MathFormula f = new MathFormula(0x0, form.Sizes); SimpleSymbol sym = null; MathSymbol symbol = form.First; if (symbol == null) { return(null); } MathSymbol next; while (true) { next = symbol.Next; if (symbol is SimpleSymbol & !(symbol is FieldSymbol) & !(symbol is BinarySymbol) & !(symbol is FractionSymbol) & !(symbol is BracketsSymbol) & !(symbol is RootSymbol) & !(symbol is SeriesSymbol) & !(symbol is SubscriptedSymbol) & !(symbol is IndexedSymbol) & !(symbol is PoweredIndexedSymbol)) { SimpleSymbol ss = symbol as SimpleSymbol; if ((ss.Bold == bold) & (ss.Italic == italic) & (ss.SymbolType == type) & (ss.String.Length <= 1)) { if (sym == null) { sym = ss.Clone() as SimpleSymbol; } else { string str = sym.String + ss.String; sym = new SimpleSymbol(sym.Symbol, type, italic, bold, str); } } else { if (sym != null) { sym.AppendWithChildren(f); sym = null; } symbol.AppendWithChildren(f); } } else { if (sym != null) { sym.AppendWithChildren(f); sym = null; } symbol.AppendWithChildren(f); } symbol = next; if (symbol == null) { if (sym != null) { sym.AppendWithChildren(f); } break; } } return(f); }