Пример #1
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();
                }
            }
Пример #2
0
 public SCBook WriteLatex(string text, LatexOptions options = null)
 {
     ExecuteSynchronized(options, () => DoWriteLatex(text, options));
     return(this);
 }