private void BeforePrepare() { Control control = panelFormula; performer = new FormulaEditor.FormulaEditorPerformer(panelFormula, null); Hashtable table = new Hashtable(); string[,] el = new string[, ] { { "=", "=" }, { "&", "AND" }, { "\u2216", "AND" }, { "\u2217", "OR" }, { "|", "OR" }, { "\u8835", "=>" }, { "l", "LIKE" }, { "p", "=>" }, { "¬", "NOT" }, { "(", "(" }, { ".", "." }, { ":", ":" }, { "?", "?" }, { "<", "<" }, { ">", ">" }, { "+", "+" }, { "-", "-" }, { "*", "*" }, { "=", "=" }, { "/", "/" }, { "﹪", "%" }, { "A", "atan2" }, { "'", "d/dt" } }; for (int i = 0; i < el.GetLength(0); i++) { table[el[i, 0]] = el[i, 1]; } MathFormulaDrawable formula = new MathFormulaDrawable(new MathFormula((byte)0, sizes), DrawableConverter.Object); Color[] cols = new Color[] { Color.FromArgb(216, 203, 187), Color.Black }; performer.SetColors(cols[0], cols[1]); Graphics g = Graphics.FromHwnd(control.Handle); SimpleSymbolDrawable.Prepare(sizes, g); BracketsSymbolDrawable.Prepare(sizes, g); AbsSymbolDrawable.Prepare(sizes, g); }
/// <summary> /// Calculates relative rectangle with its children /// </summary> public static void CalculateFullRelativeRectangle(IDrawableSymbol symbol) { SimpleSymbolDrawable.CalculateFullRelativeRectangle(symbol); //SimpleSymbolDrawable.C symbol.CalculateRelativeRectangle(); if (symbol is RootSymbol | symbol is FractionSymbol | symbol is BinaryFunctionSymbol) { return; } if (!(symbol is BracketsSymbolDrawable)) { return; } BracketsSymbolDrawable sym = symbol as BracketsSymbolDrawable; Rectangle rr = symbol.PureDrawable.RelativeRectangle; int w = rr.Width; int up = rr.Y; MathSymbol ms = symbol as MathSymbol; MathFormulaDrawable df = ms[0] as MathFormulaDrawable; Rectangle r1 = df.FullRelativeRectangle; Rectangle r2 = new Rectangle(); if (ms.Children != null) { if (ms.Count > 1) { if (ms[1] != null) { if (!ms[1].IsEmpty) { MathFormulaDrawable dr = ms[1] as MathFormulaDrawable; r2 = dr.FullRelativeRectangle; } } } } sym.h = (int)((double)sym.down / Math.Sin(40.0 * 3.1415926 / 180.0)); sym.warc = (int)((double)sym.h * (1 - Math.Cos(40.0 * 3.1415926 / 180.0))); int wpow = 0, hpow = 0; if (sym.Count > 1) { sym.h = (int)((double)sym.down / Math.Sin(40.0 * 3.1415926 / 180.0)); sym.warc = (int)((double)sym.h * (1 - Math.Cos(40.0 * 3.1415926 / 180.0))); if (ms.Count > 1) { wpow = r2.Width; hpow = -r2.Y; } } PureDrawableSymbol pDrawable = symbol.PureDrawable; pDrawable.FullRelativeRectangleX = 0; pDrawable.FullRelativeRectangleY = -sym.down - hpow; pDrawable.FullRelativeRectangleWidth = r1.Width + 2 * sym.warc + 4 + wpow; pDrawable.FullRelativeRectangleHeight = 2 * sym.down + sym.hpow; }
/// <summary> /// Calculates its and its children positions /// </summary> public static void CalculatePositions(IDrawableSymbol symbol) { SimpleSymbolDrawable.CalculatePositions(symbol); PureDrawableSymbol pDrawable = symbol.PureDrawable; pDrawable.InsertedRectX = pDrawable.Rectangle.X; pDrawable.InsertedRectY = pDrawable.Rectangle.Y; pDrawable.InsertedRectHeight = pDrawable.Rectangle.Height; pDrawable.InsertedRectWidth = pDrawable.WidthInsert; pDrawable.InsertedRectX = pDrawable.InsertedRect.X - pDrawable.WidthInsert / 2; }
/// <summary> /// Sets location on toolbar /// </summary> /// <param name="x">the x position</param> /// <param name="y">the y position</param> /*public void SetLocationOnTable(int x, int y) * { * pDrawable.RectForShow.Location = new Point(x, y); * }*/ public void CalculatePositions() { SimpleSymbolDrawable.CalculatePositions(this); IDrawableSymbol ds = this; PureDrawableSymbol pDrawable = ds.PureDrawable; pDrawable.InsertedRectX = pDrawable.Rectangle.X; pDrawable.InsertedRectY = pDrawable.Rectangle.Y; pDrawable.InsertedRectHeight = pDrawable.Rectangle.Height; pDrawable.InsertedRectWidth = pDrawable.WidthInsert; pDrawable.InsertedRectX = pDrawable.InsertedRect.X - pDrawable.WidthInsert / 2; }
/// <summary> /// Draws itself /// </summary> /// <param name="g">The graphics to draw</param> public void DrawSelf(Graphics g) { if ((symbol != '\u2216') & (symbol != '\u2217') & (symbol != '\u8835') & (symbol != '\u2295') & (symbol != '\u2297') | MathFormula.Resources.ContainsKey(symbol + "")) { SimpleSymbolDrawable.DrawSelf(this, g); return; } int x = pDrawable.Position.X; int y = pDrawable.Position.Y; Rectangle r = pDrawable.FullRelativeRectangle; int h = r.Height; int w = r.Width; DrawHelp(g, x, y, w, h, symbol); }
/// <summary> /// Creates image from formula /// </summary> /// <param name="formula">Formula</param> /// <returns>Image</returns> public static Image CreateImage(string formula) { Image im = new Bitmap(10, 10); Graphics g = Graphics.FromImage(im); SimpleSymbolDrawable.Prepare(new int[] { 8, 6, 5, 4 }, g); MathFormulaDrawable f = new MathFormulaDrawable(MathFormula.FromString(new int[] { 8, 6, 5, 4 }, formula), DrawableConverter.Object); f.CalculateFullRelativeRectangle(); Rectangle r = f.FullRelativeRectangle; Point p = new Point(10, -r.Y); f.Position = p; f.CalculatePositions(); im = new Bitmap(r.Width + 20, r.Height); Brush brush = new SolidBrush(Color.White); g = Graphics.FromImage(im); g.FillRectangle(brush, 0, 0, im.Width, im.Height); f.Draw(g); SimpleSymbolDrawable.Prepare(MathSymbolFactory.Sizes, g); g.Dispose(); return(im); }
public override void SetToFormula(MathFormula formula) { sizes = formula.Sizes; level = formula.Level; SimpleSymbolDrawable.SetToFormula(this, formula); }
public void CalculatePositions() { SimpleSymbolDrawable.CalculatePositions(this); }
public void Prepare(FormulaEditorPerformer performer) { SimpleSymbolDrawable.Prepare(this, performer); }
/// <summary> /// Draws itself on component /// </summary> /// <param name="g">The graphics to draw</param> /// <param name="brush">The drawing brush</param> /// <param name="pen">The drawing pen</param> public static void DrawOnComponent(IDrawableSymbol sym, Graphics g, Brush brush, Pen pen) { MathSymbol ms = sym as MathSymbol; char ch = ms.Symbol; if ((ch != '\u2216') & (ch != '\u2217') & (ch != '\u8835') & (ch != '\u2295') & (ch != '\u2297') | MathFormula.Resources.ContainsKey(ms.Symbol + "")) { SimpleSymbolDrawable.DrawOnComponent(sym, g, brush, pen); return; } PureDrawableSymbol pDrawable = sym.PureDrawable; int shift = 8; string ss = ms.String; if (ss == null) { ss = "" + ch; } if (ss.Length == 0) { ss += ch; } int w = (int)g.MeasureString(ss, BracketsSymbolDrawable.FontB).Width; int h = pDrawable.RectForShow.Height; int s = (w < h) ? w : h; s = (int)(0.6 * (float)s); int x = pDrawable.RectForShow.X + w / 2; int y = pDrawable.RectForShow.Y + h / 2; if (ch == '\u2216') { g.DrawLine(PureDrawableSymbol.WidePen, pDrawable.RectForShow.X + shift, pDrawable.RectForShow.Y + pDrawable.RectForShow.Height - shift, pDrawable.RectForShow.X + pDrawable.RectForShow.Width / 2, pDrawable.RectForShow.Y + shift); g.DrawLine(PureDrawableSymbol.WidePen, pDrawable.RectForShow.X + pDrawable.RectForShow.Width / 2, pDrawable.RectForShow.Y + shift, pDrawable.RectForShow.X + pDrawable.RectForShow.Width - shift, pDrawable.RectForShow.Y + pDrawable.RectForShow.Height - shift); } else if (ch == '\u2217') { g.DrawLine(PureDrawableSymbol.WidePen, pDrawable.RectForShow.X + shift, pDrawable.RectForShow.Y + shift, pDrawable.RectForShow.X + pDrawable.RectForShow.Width / 2, pDrawable.RectForShow.Y + pDrawable.RectForShow.Height - shift); g.DrawLine(PureDrawableSymbol.WidePen, pDrawable.RectForShow.X + pDrawable.RectForShow.Width / 2, pDrawable.RectForShow.Y + pDrawable.RectForShow.Height - shift, pDrawable.RectForShow.X + pDrawable.RectForShow.Width - shift, pDrawable.RectForShow.Y + shift); } else if (ch == '\u8835') { g.DrawLine(PureDrawableSymbol.WidePen, pDrawable.RectForShow.X + shift, pDrawable.RectForShow.Y + shift, pDrawable.RectForShow.X + pDrawable.RectForShow.Width / 2, pDrawable.RectForShow.Y + pDrawable.RectForShow.Height - shift); g.DrawLine(PureDrawableSymbol.WidePen, pDrawable.RectForShow.X + pDrawable.RectForShow.Width / 2, pDrawable.RectForShow.Y + pDrawable.RectForShow.Height - shift, pDrawable.RectForShow.X + pDrawable.RectForShow.Width - shift, pDrawable.RectForShow.Y + shift); } else if (ch == '\u2295') { DrawCircledPlus(g, PureDrawableSymbol.LinePen, x, y, s); } else if (ch == '\u2297') { DrawCircledProduct(g, PureDrawableSymbol.LinePen, x, y, s); } }
void IDrawableSymbol.CalculatePositions() { SimpleSymbolDrawable.CalculatePositions(this); }
/// <summary> /// Preparation /// </summary> /// <param name="sx">Variable symbols</param> /// <param name="n">Level</param> /// <param name="sSubscript">Subscripted symbols</param> /// <param name="symbols">Text symbols</param> public void Prepare(string sx, int n, string[,] sSubscript, string[] symbols) { string[] all = symbols; Hashtable table = new Hashtable(); string[,] el = new string[, ] { { "=", "=" }, { "&", "AND" }, { "\u2216", "AND" }, { "\u2217", "OR" }, { "|", "OR" }, { "\u8835", "=>" }, { "l", "LIKE" }, { "p", "=>" }, { "¬", "NOT" }, { "(", "(" }, { ".", "." }, { ":", ":" }, { "?", "?" }, { "<", "<" }, { ">", ">" }, { "+", "+" }, { "-", "-" }, { "*", "*" }, { "/", "/" }, { "﹪", "%" }, { "=", "=" }, { "A", "atan2" }, { "'", "d/dt" } }; for (int i = 0; i < el.GetLength(0); i++) { table[el[i, 0]] = el[i, 1]; } // MathFormula.Resources = table; int[] sizes = MathSymbolFactory.Sizes; int sub = (sSubscript == null) ? 0 : sSubscript.GetLength(0); MathFormulaDrawable formula = new MathFormulaDrawable(new MathFormula((byte)0, sizes), DrawableConverter.Object); Control control = this; performer = new FormulaEditor.FormulaEditorPerformer(control, formula); Color[] cols = new Color[] { Color.FromArgb(216, 203, 187), Color.Black }; performer.SetColors(cols[0], cols[1]); Graphics g = Graphics.FromHwnd(control.Handle); SimpleSymbolDrawable.Prepare(sizes, g); BracketsSymbolDrawable.Prepare(sizes, g); AbsSymbolDrawable.Prepare(sizes, g); if (all != null) { foreach (string str in all) { foreach (char c in str) { performer.Add(new SimpleSymbolDrawable( new SimpleSymbol(c, 0x0, false, false))); } } } string ss = sx; for (int i = 0; i < sub; i++) { performer.Add(new SubscriptedSymbolDrawable(new SubscriptedSymbol(sSubscript[i, 0], sSubscript[i, 1]))); } for (int i = 0; i < ss.Length; i++) { performer.Add(new SimpleSymbolDrawable(new SimpleSymbol(ss[i]))); } string elem = "scletqabjkfgw'\u2211\u03B4\u0442"; for (int i = 0; i < elem.Length; i++) { performer.Add(new SimpleSymbolDrawable(new SimpleSymbol(elem[i], false, (byte)4))); } string integerSym = "ABCDEFGH"; performer.Add(new BinaryFunctionSymbolDrawable(new BinaryFunctionSymbol('A', "atan2"))); for (int i = 0; i < integerSym.Length; i++) { char c = integerSym[i]; performer.Add(new SimpleSymbolDrawable(new SimpleSymbol(c, (byte)4, false, ElementaryIntegerOperation.GetString(c)))); } string numbers = "x0123456789ABCDEF"; for (int i = 0; i < numbers.Length; i++) { performer.Add(new SimpleSymbolDrawable(new SimpleSymbol(numbers[i], false, (byte)5))); } string binary = ".+-*×=><\u2264\u2265\u2260?:&|^\u2266\u2267\u2216\u2217\u8835l\u2270"; //string[,] el = new string[,] {{"\u2216", "AND"}, {"\u2217", "OR"}}; //string binary = ".+-*=><?:&|^\u2217l"; for (int i = 0; i < binary.Length; i++) { performer.Add(new BinarySymbolDrawable(new BinarySymbol(binary[i]))); } string unary = "¬~"; for (int i = 0; i < unary.Length; i++) { performer.Add(new SimpleSymbolDrawable(new SimpleSymbol(unary[i], false, (byte)4))); } performer.Add(new BracketsSymbolDrawable(new BracketsSymbol())); performer.Add(new AbsSymbolDrawable(new AbsSymbol())); performer.Add(new RootSymbolDrawable(new RootSymbol())); performer.Add(new FractionSymbolDrawable(new FractionSymbol())); performer.Add(new BinarySymbolDrawable(MathSymbol.Bra as BinarySymbol)); performer.Add(new SimpleSymbolDrawable(MathSymbol.Ket as SimpleSymbol)); performer.Add(new SimpleSymbolDrawable(new SimpleSymbol('e'))); performer.Add(new SimpleSymbolDrawable(new SimpleSymbol('%', (byte)FormulaConstants.Variable, true, "\u03c0"))); for (int i = 0; i < n; i++) { performer.Add(new SeriesSymbolDrawable(new SeriesSymbol(i))); } int x = 10, y = 10; int bottom = 0; IDrawableSymbol symbol = null; int ns = 0; if (all != null) { foreach (string str in all) { foreach (char c in str) { symbol = performer[ns]; Point p = new Point(x, y); MathSymbolDrawable.SetComponentPosition(symbol, p); symbol.PureDrawable.RectForShow = new Rectangle(x, y, symbol.StandardWidth, (int)(1.5 * 20)); x += symbol.PureDrawable.RectForShow.Width; ++ns; } bottom = y + symbol.PureDrawable.RectForShow.Height; y = bottom; x = 10; } } if (sub != 0) { for (int i = ns; i < sub + ns; i++) { symbol = performer[i]; Point p = new Point(x, y); MathSymbolDrawable.SetComponentPosition(symbol, p); symbol.PureDrawable.RectForShow = new Rectangle(x, y, symbol.StandardWidth, (int)(1.5 * 20)); x += symbol.PureDrawable.RectForShow.Width; } bottom = y + symbol.PureDrawable.RectForShow.Height; y = bottom; x = 10; } for (int i = sub + ns; i < sub + ns + ss.Length; i++) { symbol = performer[i]; Point p = new Point(x, y); MathSymbolDrawable.SetComponentPosition(symbol, p); symbol.PureDrawable.RectForShow = new Rectangle(x, y, 30, (int)(1.5 * 20)); x += symbol.PureDrawable.RectForShow.Width; } bottom = y + symbol.PureDrawable.RectForShow.Height; y = bottom; x = 10; for (int i = ss.Length + ns + sub; i < ss.Length + elem.Length + 2 + sub + ns; i++) { symbol = performer[i]; Point p = new Point(x, y); MathSymbolDrawable.SetComponentPosition(symbol, p); symbol.PureDrawable.RectForShow = new Rectangle(x, y, symbol.StandardWidth + 10, (int)(1.5 * 20)); x += symbol.PureDrawable.RectForShow.Width; } bottom = y + symbol.PureDrawable.RectForShow.Height; y = bottom; x = 10; for (int i = sub + ns + ss.Length + elem.Length + 1; i < sub + ns + ss.Length + elem.Length + integerSym.Length + 1; i++) { symbol = performer[i]; Point p = new Point(x, y); MathSymbolDrawable.SetComponentPosition(symbol, p); symbol.PureDrawable.RectForShow = new Rectangle(x, y, symbol.StandardWidth + 10, (int)(1.5 * 20)); x += symbol.PureDrawable.RectForShow.Width; } bottom = y + symbol.PureDrawable.RectForShow.Height; y = bottom; x = 10; for (int i = sub + ns + ss.Length + elem.Length + integerSym.Length + 1; i < sub + ns + ss.Length + elem.Length + numbers.Length + integerSym.Length + 2; i++) { symbol = performer[i]; Point p = new Point(x, y); MathSymbolDrawable.SetComponentPosition(symbol, p); symbol.PureDrawable.RectForShow = new Rectangle(x, y, symbol.StandardWidth + 10, (int)(1.5 * 20)); x += symbol.PureDrawable.RectForShow.Width; } bottom = y + symbol.PureDrawable.RectForShow.Height; y = bottom; x = 10; for (int i = sub + ns + ss.Length + elem.Length + integerSym.Length + numbers.Length + 1; i < sub + ns + ss.Length + elem.Length + numbers.Length + integerSym.Length + binary.Length + unary.Length + 8 + n; i++) { symbol = performer[i]; Point p = new Point(x, y); MathSymbolDrawable.SetComponentPosition(symbol, p); symbol.PureDrawable.RectForShow = new Rectangle(x, y, symbol.StandardWidth + 10, (int)(1.5 * 20)); x += symbol.PureDrawable.RectForShow.Width; } bottom = y + symbol.PureDrawable.RectForShow.Height; Rectangle r = new Rectangle(80, bottom, 1100, control.Height - bottom - 20); performer.FormulaRectangle = r; Image back = new Bitmap(1200, r.Y + r.Height + 10); g = Graphics.FromImage(back); g.FillRectangle(new SolidBrush(Color.LightGray), 0, 0, back.Width, back.Height); g.DrawRectangle(new Pen(Color.Black), 0, 0, back.Width - 1, back.Height - 1); Font f = new Font("Serif", sizes[0], FontStyle.Bold | FontStyle.Italic); int delta = 15; Brush bBrush = new SolidBrush(Color.Black); g.DrawString("f = ", f, bBrush, 10, r.Y + r.Height / 2 + delta); g.Dispose(); performer.Prepare(back); formula = new MathFormulaDrawable(new MathFormula((byte)0, sizes), DrawableConverter.Object); performer.Formula = formula; performer.InitEventHandlers(); performer.DrawFormula(); }