Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New frontend CSharpMath.VectSharp. #215

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

arklumpus
Copy link

Hi! Thank you for this project, I really like it!

I needed a way to render LaTeX equations for another project (VectSharp.Markdown), and this seemed like a good place to start.

To be able to make it work with my project, I have created a new front end for CSharpMath, which uses VectSharp to render the equations. As a side effect, this makes it possible to output the rendered LaTeX to SVG and PDF documents, as well as to raster images.

For example, to render an equation to an SVG file (requires the VectSharp.SVG NuGet package):

using VectSharp;
using VectSharp.SVG;
using CSharpMath.VectSharp;

var painter = new MathPainter();
painter.LaTeX = @"\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)";

Page pag = painter.DrawToPage();
pag.SaveAsSVG("test.svg");

Or to a PDF file (requires the VectSharp.PDF NuGet package):

using VectSharp;
using VectSharp.PDF;
using CSharpMath.VectSharp;

var painter = new MathPainter();
painter.LaTeX = @"\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)";

Page pag = painter.DrawToPage();
Document doc = new Document();
doc.Pages.Add(pag);

doc.SaveAsPDF("test.pdf");

To a raster image (using ImageSharp, requires the VectSharp.Raster.ImageSharp NuGet package):

using VectSharp;
using VectSharp.Raster.ImageSharp;
using CSharpMath.VectSharp;

var painter = new MathPainter();
painter.LaTeX = @"\left( \sum_{k=1}^n a_k b_k \right)^2 \leq \left( \sum_{k=1}^n a_k^2 \right) \left( \sum_{k=1}^n b_k^2 \right)";

Page pag = painter.DrawToPage();
pag.SaveAsImage("test.png");

Currently, this implementation is very similar to the CSharpMath.SkiaSharp implementation, except that it uses VectSharp rather than SkiaSharp for rendering. Ideally, at some point in the future I would like to fork from CSharpMath.Rendering in order to directly draw the glyphs, which would have the added benefit of making text selectable in PDF files. It will take some time though, because at the very least I need to implement a parser for the MATH OpenType table.

I hope this can be useful! Let me know if there are any issues or you have any questions!

Copy link
Collaborator

@charlesroddie charlesroddie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice work. The potential for this to lead on to drawing glyphs directly is very interesting and might help to explain how the fonts in CSharpMath work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants