Ejemplo n.º 1
0
        public void Save()
        {
            //Create formula object
            var formula = Formula(InputText);

            if (formula == null)
            {
                return;
            }
            var renderer = formula.GetRenderer(TexStyle.Display, TexScale, "Arial");

            //Choose file
            SaveFileDialog saveFileDialog = new SaveFileDialog()
            {
                Filter = "Image Files(*.BMP)|*.BMP"
            };
            var result = saveFileDialog.ShowDialog();

            if (result == false)
            {
                return;
            }

            //Open Stream
            var filename = saveFileDialog.FileName;

            using (var stream = new FileStream(filename, FileMode.Create))
            {
                switch (saveFileDialog.FilterIndex)
                {
                case 1:
                    var bitmap  = renderer.RenderToBitmap(0.0, 0.0);
                    var encoder = new PngBitmapEncoder
                    {
                        Frames = { BitmapFrame.Create(bitmap) }
                    };
                    encoder.Save(stream);
                    break;

                case 2:
                    var geometry    = renderer.RenderToGeometry(1000, 1000);
                    var converter   = new SVGConverter();
                    var svgPathText = converter.ConvertGeometry(geometry);
                    var svgText     = AddSVGHeader(svgPathText);
                    using (var writer = new StreamWriter(stream))
                        writer.WriteLine(svgText);
                    break;


                default:
                    return;
                }
            }
        }
Ejemplo n.º 2
0
        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            // Choose file
            SaveFileDialog saveFileDialog = new SaveFileDialog()
            {
                Filter = "SVG Files (*.svg)|*.svg|PNG Files (*.png)|*.png"
            };
            var result = saveFileDialog.ShowDialog();

            if (result == false)
            {
                return;
            }

            // Create formula object from input text.
            var formula = ParseFormula(inputTextBox.Text);

            if (formula == null)
            {
                return;
            }
            var renderer = formula.GetRenderer(TexStyle.Display, this.formula.Scale, "Arial");

            // Open stream
            var filename = saveFileDialog.FileName;

            using (var stream = new FileStream(filename, FileMode.Create))
            {
                switch (saveFileDialog.FilterIndex)
                {
                case 1:
                    var geometry    = renderer.RenderToGeometry(0, 0);
                    var converter   = new SVGConverter();
                    var svgPathText = converter.ConvertGeometry(geometry);
                    var svgText     = AddSVGHeader(svgPathText);
                    using (var writer = new StreamWriter(stream))
                        writer.WriteLine(svgText);
                    break;

                case 2:
                    var bitmap  = renderer.RenderToBitmap(0, 0);
                    var encoder = new PngBitmapEncoder
                    {
                        Frames = { BitmapFrame.Create(bitmap) }
                    };
                    encoder.Save(stream);
                    break;

                default:
                    return;
                }
            }
        }
Ejemplo n.º 3
0
        private void saveButton_Click(object sender, RoutedEventArgs e)
        {
            // Choose file
            SaveFileDialog saveFileDialog = new SaveFileDialog()
            {
                Filter = "SVG Files (*.svg)|*.svg|PNG Files (*.png)|*.png"
            };
            var result = saveFileDialog.ShowDialog();

            if (result == false)
            {
                return;
            }

            // Open stream
            var filename = saveFileDialog.FileName;

            switch (saveFileDialog.FilterIndex)
            {
            case 1:
                var svgConv = new SVGConverter(formula.Formula, formula.Scale)
                {
                    SystemTextFontName = formula.SystemTextFontName
                };
                svgConv.SaveGeometry(filename);
                break;

            case 2:
                using (var stream = new FileStream(filename, FileMode.Create))
                {
                    TexFormulaParser formulaParser = new TexFormulaParser();
                    var texFormula = formulaParser.Parse(formula.Formula);
                    var renderer   = texFormula.GetRenderer(TexStyle.Display, formula.Scale, formula.SystemTextFontName);

                    var bitmap  = renderer.RenderToBitmap(0, 0);
                    var encoder = new PngBitmapEncoder
                    {
                        Frames = { BitmapFrame.Create(bitmap) }
                    };
                    encoder.Save(stream);
                }
                break;

            default:
                return;
            }
        }
Ejemplo n.º 4
0
        private static (string, byte[]) renderLatex(string latex, bool onlyReturnDpath = true)
        {
            var parser      = new TexFormulaParser();
            var formula     = parser.Parse(latex);
            var renderer    = formula.GetRenderer(TexStyle.Display, 20, "Arial");
            var pngBytes    = formula.RenderToPng(20.0, 0.0, 0.0, "Arial");
            var geometry    = renderer.RenderToGeometry(0, 0);
            var converter   = new SVGConverter();
            var svgPathText = converter.ConvertGeometry(geometry);

            if (!onlyReturnDpath)
            {
                return(AddSVGHeader(svgPathText), pngBytes);
            }
            var tmp1 = svgPathText.Split(new string[] { "d=\"" }, StringSplitOptions.None)[1];
            var tmp2 = tmp1.Split(new string[] { "\"" }, StringSplitOptions.None)[0];

            return(tmp2, pngBytes);
        }
Ejemplo n.º 5
0
        private async void sendGame_Click(object sender, RoutedEventArgs e)
        {
            List <string> clues = getClues();

            if (String.IsNullOrEmpty(Svg))
            {
                Svg = SVGConverter.ConvertDrawingToSVG(strokes);
            }

            try
            {
                var HttpClient = new HttpClient();
                var infos      = new Game
                {
                    name  = Word.Text,
                    clues = clues,
                    data  = Svg
                };
                var json = await Task.Run(() => JsonConvert.SerializeObject(infos));

                var httpContent = new StringContent(json, Encoding.UTF8, "application/json");

                var res = await HttpClient.PostAsync(Constants.ADDR + "games", httpContent);

                if (res.Content != null)
                {
                    var responseContent = await res.Content.ReadAsStringAsync();

                    Console.WriteLine(responseContent);
                    System.Windows.Forms.MessageBox.Show("Image bien sauvegardée!", "Caption", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            catch (Exception ex)
            {
                System.Windows.Forms.MessageBox.Show(ex.Message);
            }

            strokes.Clear();
        }
Ejemplo n.º 6
0
        protected void DoWriteLatex(string text, LatexOptions options)
        {
            options ??= new LatexOptions();

            var book = options.Book?.Document ?? Document;

            using (new UsingProcessor(() => book.BeginUpdate(), () => book.EndUpdate()))
            {
                DocumentPosition rangeStart = null, rangeEnd = null;

                var parser     = new TexFormulaParser();
                var formula    = parser.Parse(text);

                var renderer   = formula.GetRenderer(TexStyle.Text, options.FontSize, "Tahoma");

                var geometry   = renderer.RenderToGeometry(0, 0);
                var converter  = new SVGConverter();
                var svgPathText = converter.ConvertGeometry(geometry);
                var svgText    = AddSVGHeader(svgPathText);

                var imageLatex = PaintSVG(svgText, options.DPI);
                AddUserCommentsToImage(imageLatex, text);
                var image = book.Images.Append(imageLatex);

                image.ScaleX = options.Scale;
                image.ScaleY = options.Scale;

                var rangeImage = image.Range;
                if (rangeStart == null)
                {
                    rangeStart = rangeImage.Start;
                }

                if (!options.NoLineBreaks)
                {
                    rangeImage = book.AppendText(Environment.NewLine);
                }

                rangeEnd = rangeImage.End;

                if (rangeStart != null && rangeEnd != null)
                {
                    var range = book.CreateRange(rangeStart, rangeEnd.ToInt() - rangeStart.ToInt());

                    if (!string.IsNullOrWhiteSpace(options.ParagraphStyle))
                    {
                        var style = book.ParagraphStyles[options.ParagraphStyle] ?? throw new Exception($"Paragraph style '{options.ParagraphStyle}' does not exist.");
                        var pp    = book.BeginUpdateParagraphs(range);
                        try
                        {
                            pp.Style = style;
                        }
                        finally
                        {
                            book.EndUpdateParagraphs(pp);
                        }
                    }

                    Script.Book.SCBook.AddComments(book, range, options);

                    WriteRangeToConsole(book, range);
                }

                if (rangeEnd != null)
                {
                    book.CaretPosition = rangeEnd;
                    Script.Book.SCBook.ResetBookFormatting(book, rangeEnd);
                    ScrollToCaret();
                }
            }
Ejemplo n.º 7
0
        protected IRichEditDocumentServer AddLatex(ArgumentCollection arguments)
        {
            if (arguments.Count <= 0)
            {
                throw new Exception("'DOCVARIABLE LATEX' requires LaTeX as first argument.");
            }

            var latex = arguments[0].Value;

            float?scale = null, scaleX = null, scaleY = null;
            float dpi = 300, fontSize = 20;

            if (arguments.Count > 1)
            {
                var properties = Utils.SplitNameValueString(arguments[1].Value, ';');

                foreach (var prop in properties)
                {
                    if (string.IsNullOrWhiteSpace(prop.Key))
                    {
                        continue;
                    }

                    switch (prop.Key.ToLower())
                    {
                    case "dpi":
                        dpi = float.Parse(prop.Value, CultureInfo.InvariantCulture);
                        break;

                    case "scale":
                        scale = float.Parse(prop.Value, CultureInfo.InvariantCulture);
                        break;

                    case "scalex":
                        scaleX = float.Parse(prop.Value, CultureInfo.InvariantCulture);
                        break;

                    case "scaley":
                        scaleY = float.Parse(prop.Value, CultureInfo.InvariantCulture);
                        break;

                    case "fontsize":
                        fontSize = float.Parse(prop.Value, CultureInfo.InvariantCulture);
                        break;
                    }
                }
            }

            var parser  = new TexFormulaParser();
            var formula = parser.Parse(latex);

            var renderer = formula.GetRenderer(TexStyle.Text, fontSize, "Tahoma");

            var geometry    = renderer.RenderToGeometry(0, 0);
            var converter   = new SVGConverter();
            var svgPathText = converter.ConvertGeometry(geometry);
            var svgText     = AddSVGHeader(svgPathText);

            var docSVG = new XmlDocument();

            docSVG.LoadXml(svgText);

            var image = PaintSVG(docSVG, Size.Empty, dpi);

            var server   = new RichEditDocumentServer();
            var document = server.Document;
            var docImage = document.Images.Append(image);

            if (scale.HasValue)
            {
                docImage.ScaleX = docImage.ScaleY = scale.Value;
            }
            else
            {
                if (scaleX.HasValue)
                {
                    docImage.ScaleX = scaleX.Value;
                }
                if (scaleY.HasValue)
                {
                    docImage.ScaleY = scaleY.Value;
                }
            }

            return(server);
Ejemplo n.º 8
0
        /// <summary>
        /// This is implemented to perform the component tasks.
        /// </summary>
        /// <param name="document">The XML document with which to work.</param>
        /// <param name="key">The key (member name) of the item being documented.</param>
        public override void Apply(XmlDocument document, string key)
        {
            var latexList = document.SelectNodes("//latex");

            if (latexList == null)
            {
                return;
            }

            foreach (XmlNode code in latexList)
            {
                var attr       = code.Attributes.GetNamedItem("expr");
                var expression = attr == null ? code.InnerText : attr.InnerText;

                if (String.IsNullOrWhiteSpace(expression))
                {
                    continue;
                }

                bool svgOut = true;

                var result      = GetImageName(expression, svgOut);
                var filename    = result.Item2;
                var filenamePng = Path.ChangeExtension(filename, ".png");

                var parser = new TexFormulaParser();

                if (!result.Item1)
                {
                    foreach (var path in _paths)
                    {
                        //TODO: copy instead of running for each path
                        //SafeNativeMethods.CreateGifFromEq(expression, path + filename, _fontSize);
                        //
                        //const string latex = @"\frac{2+2}{2}";
                        //const string fileName = @"T:\Temp\formula.png";
                        TexFormula formula;
                        try
                        {
                            formula = parser.Parse(expression);
                        }
                        catch (Exception ex)
                        {
                            Console.WriteLine("An error occurred while parsing the given input: " + ex.Message);
                            continue;
                        }

                        if (svgOut)
                        {
                            // Output png for fallback.
                            var    renderer = formula.GetRenderer(TexStyle.Display, 20, "Arial");
                            var    bitmap   = renderer.RenderToBitmap(0, 0);
                            double width    = bitmap.Width;
                            double height   = bitmap.Height;

                            renderer = formula.GetRenderer(TexStyle.Display, 20, "Arial");
                            var geometry    = renderer.RenderToGeometry(0, 0);
                            var converter   = new SVGConverter();
                            var svgPathText = converter.ConvertGeometry(geometry);
                            var svgText     = AddSVGHeader(svgPathText, width, height);
                            using (var stream = new FileStream(path + filename, FileMode.Create))
                            {
                                using (var writer = new StreamWriter(stream))
                                    writer.WriteLine(svgText);
                            }

                            // Output png for fallback.
                            var pngBytes = formula.RenderToPng(20.0, 0.0, 0.0, "Arial");
                            File.WriteAllBytes(path + filenamePng, pngBytes);
                        }
                        else
                        {
                            var pngBytes = formula.RenderToPng(20.0, 0.0, 0.0, "Arial");
                            File.WriteAllBytes(path + filename, pngBytes);
                        }
                    }
                }



                var src = document.CreateAttribute("src");
                src.Value = "../html/" + filename;
                var hspace = document.CreateAttribute("hspace");
                hspace.Value = "0";
                var vspace = document.CreateAttribute("vspace");
                vspace.Value = "0";
                var align = document.CreateAttribute("align");
                align.Value = "bottom";
                XmlNode img = document.CreateElement("img");
                img.Attributes.Append(src);
                img.Attributes.Append(hspace);
                img.Attributes.Append(vspace);
                img.Attributes.Append(align);
                if (svgOut)
                {
                    // Add a png fallback
                    var onError = document.CreateAttribute("onerror");
                    onError.Value = "this.src='../html/" + filenamePng + "'";
                    img.Attributes.Append(onError);
                }
                code.ParentNode.ReplaceChild(img, code);
            }
        }