Skip to content

New frontend CSharpMath.VectSharp. #215

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CSharpMath.CrossPlatform.slnf
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"CSharpMath.Avalonia/CSharpMath.Avalonia.csproj",
"CSharpMath.Avalonia.Example/CSharpMath.Avalonia.Example.csproj",
"CSharpMath.SkiaSharp/CSharpMath.SkiaSharp.csproj",
"CSharpMath.VectSharp/CSharpMath.VectSharp.csproj",
"CSharpMath.Forms/CSharpMath.Forms.csproj",
"CSharpMath.Forms.Example/CSharpMath.Forms.Example/CSharpMath.Forms.Example.csproj",
"CSharpMath.Forms.Tests/CSharpMath.Forms.Tests.csproj"
Expand Down
14 changes: 14 additions & 0 deletions CSharpMath.VectSharp/CSharpMath.VectSharp.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<Description>The VectSharp front end for CSharpMath.</Description>
<PackageTags>$(PackageTags) vectsharp</PackageTags>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="VectSharp" Version="2.3.2" />
<ProjectReference Include="..\CSharpMath.Rendering\CSharpMath.Rendering.csproj" />
</ItemGroup>

</Project>
35 changes: 35 additions & 0 deletions CSharpMath.VectSharp/Extensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Adapted after https://github.com/verybadcat/CSharpMath/blob/master/CSharpMath.SkiaSharp/Extensions.cs

using System;
using CSharpMath.Rendering.FrontEnd;

namespace CSharpMath.VectSharp
{
public static class Extensions
{
internal static global::VectSharp.Colour ToNative(this System.Drawing.Color color)
{
return global::VectSharp.Colour.FromRgba(color.R, color.G, color.B, color.A);
}

internal static System.Drawing.Color FromNative(this global::VectSharp.Colour colour)
{
return System.Drawing.Color.FromArgb((int)Math.Round(colour.A * 255), (int)Math.Round(colour.R * 255), (int)Math.Round(colour.G * 255), (int)Math.Round(colour.B * 255));
}

public static global::VectSharp.Page DrawToPage<TContent>(this Painter<global::VectSharp.Page, TContent, global::VectSharp.Colour> painter, float textPainterCanvasWidth = TextPainter.DefaultCanvasWidth, TextAlignment alignment = TextAlignment.TopLeft) where TContent : class
{
var size = painter.Measure(textPainterCanvasWidth).Size;

global::VectSharp.Page pag = new global::VectSharp.Page(size.Width, size.Height);

pag.Graphics.Save();

painter.Draw(pag, alignment);

pag.Graphics.Restore();

return pag;
}
}
}
65 changes: 65 additions & 0 deletions CSharpMath.VectSharp/MathPainter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Adapted after https://github.com/verybadcat/CSharpMath/blob/master/CSharpMath.SkiaSharp/MathPainter.cs

using System.Drawing;
using CSharpMath.Rendering.FrontEnd;
using CSharpMath.Rendering.BackEnd;
using CSharpMath.Structures;
using VectSharp;

namespace CSharpMath.VectSharp
{
public class MathPainter : MathPainter<Page, Colour>
{
public bool AntiAlias { get; set; } = true;
public void Draw(Page canvas, global::VectSharp.Point point) => Draw(canvas, (float)point.X, (float)point.Y);
public override Colour UnwrapColor(Color color) => color.ToNative();
public override Color WrapColor(Colour color) => color.FromNative();
public override ICanvas WrapCanvas(Page canvas) =>
new VectSharpCanvas(canvas, AntiAlias);
/// <summary>
/// Ignores the MathList and LaTeX of the <see cref="MathPainter"/> provided.
/// Repositions the <paramref name="display"/>.
/// </summary>
public static void DrawDisplay(MathPainter settings, Display.IDisplay<Fonts, Glyph> display, Page canvas, PointF position)
{
DrawDisplay(settings, display, _ => _.Draw(canvas, position));
}

/// <summary>
/// Ignores the MathList and LaTeX of the <see cref="MathPainter"/> provided.
/// Repositions the <paramref name="display"/>.
/// </summary>
public static void DrawDisplay(MathPainter settings, Display.IDisplay<Fonts, Glyph> display,
Page canvas, global::VectSharp.Point position)
{
DrawDisplay(settings, display, _ => _.Draw(canvas, position));
}

/// <summary>
/// Ignores the MathList and LaTeX of the <see cref="MathPainter"/> provided.
/// Repositions the <paramref name="display"/>.
/// </summary>
public static void DrawDisplay(MathPainter settings, Display.IDisplay<Fonts, Glyph> display, Page canvas, float x, float y)
{
DrawDisplay(settings, display, _ => _.Draw(canvas, x, y));
}

/// <summary>
/// Ignores the MathList and LaTeX of the <see cref="MathPainter"/> provided.
/// Repositions the <paramref name="display"/>.
/// </summary>
public static void DrawDisplay(MathPainter settings, Display.IDisplay<Fonts, Glyph> display, Page canvas, TextAlignment textAlignment = TextAlignment.Center, Thickness padding = default, float offsetX = 0, float offsetY = 0)
{
DrawDisplay(settings, display, _ => _.Draw(canvas, textAlignment, padding, offsetX, offsetY));
}

private static void DrawDisplay(MathPainter settings, Display.IDisplay<Fonts, Glyph> display, System.Action<MathPainter> draw)
{
if (display is null) return;
var original = (settings.Display, settings._displayChanged);
(settings.Display, settings._displayChanged) = (display, false);
draw(settings);
(settings.Display, settings._displayChanged) = original;
}
}
}
15 changes: 15 additions & 0 deletions CSharpMath.VectSharp/TextPainter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Adapted after https://github.com/verybadcat/CSharpMath/blob/master/CSharpMath.SkiaSharp/TextPainter.cs

using System.Drawing;
using CSharpMath.Rendering.FrontEnd;

namespace CSharpMath.VectSharp
{
public class TextPainter : TextPainter<global::VectSharp.Page, global::VectSharp.Colour>
{
public override Color WrapColor(global::VectSharp.Colour color) => color.FromNative();
public override global::VectSharp.Colour UnwrapColor(Color color) => color.ToNative();
public override ICanvas WrapCanvas(global::VectSharp.Page canvas) =>
new VectSharpCanvas(canvas, true);
}
}
53 changes: 53 additions & 0 deletions CSharpMath.VectSharp/VectSharpCanvas.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// Adapted after https://github.com/verybadcat/CSharpMath/blob/master/CSharpMath.SkiaSharp/SkiaCanvas.cs

using System.Drawing;
using CSharpMath.Rendering.FrontEnd;
using VectSharp;

namespace CSharpMath.VectSharp
{
public sealed class VectSharpCanvas : ICanvas
{
public VectSharpCanvas(Page canvas, bool antiAlias)
{
Canvas = canvas;
this._isAntialias = antiAlias;
}
public Page Canvas { get; }
public float Width => (float)Canvas.Width;
public float Height => (float)Canvas.Height;
public Color DefaultColor { get; set; }
public Color? CurrentColor { get; set; }
public PaintStyle CurrentStyle { get; set; }

private readonly bool _isAntialias;

// Canvas methods
public void StrokeRect(float left, float top, float width, float height)
{
this.Canvas.Graphics.StrokeRectangle(left, top, width, height, (this.CurrentColor ?? this.DefaultColor).ToNative());
}

public void FillRect(float left, float top, float width, float height)
{
this.Canvas.Graphics.FillRectangle(left, top, width, height, (this.CurrentColor ?? this.DefaultColor).ToNative());
}

public void DrawLine(float x1, float y1, float x2, float y2, float lineThickness)
{
if (CurrentStyle == PaintStyle.Fill)
{
this.Canvas.Graphics.StrokePath(new GraphicsPath().MoveTo(x1, y1).LineTo(x2, y2), (this.CurrentColor ?? this.DefaultColor).ToNative(), lineThickness);
}
else
{
this.StrokeLineOutline(x1, y1, x2, y2, lineThickness);
}
}
public void Save() => this.Canvas.Graphics.Save();
public void Translate(float dx, float dy) => this.Canvas.Graphics.Translate(dx, dy);
public void Scale(float sx, float sy) => this.Canvas.Graphics.Scale(sx, sy);
public void Restore() => this.Canvas.Graphics.Restore();
public Path StartNewPath() => new VectSharpPath(this);
}
}
39 changes: 39 additions & 0 deletions CSharpMath.VectSharp/VectSharpPath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Adapted after https://github.com/verybadcat/CSharpMath/blob/master/CSharpMath.SkiaSharp/SkiaPath.cs

using System.Drawing;
using VectSharp;

namespace CSharpMath.VectSharp
{
public sealed class VectSharpPath : Rendering.FrontEnd.Path
{
public VectSharpPath(VectSharpCanvas owner) => _owner = owner;
public override Color? Foreground { get; set; }
private readonly VectSharpCanvas _owner;
private readonly GraphicsPath _path = new GraphicsPath();
public override void MoveTo(float x0, float y0) { _path.Close(); _path.MoveTo(x0, y0); }
public override void LineTo(float x1, float y1) => _path.LineTo(x1, y1);
public override void Curve3(float x1, float y1, float x2, float y2)
{
_path.QuadraticBezierTo(x1, y1, x2, y2);
}

public override void Curve4(float x1, float y1, float x2, float y2, float x3, float y3)
{
_path.CubicBezierTo(x1, y1, x2, y2, x3, y3);
}

public override void CloseContour() => _path.Close();
public override void Dispose()
{
if (_owner.CurrentStyle == Rendering.FrontEnd.PaintStyle.Fill)
{
_owner.Canvas.Graphics.FillPath(this._path, (this.Foreground ?? _owner.CurrentColor ?? _owner.DefaultColor).ToNative());
}
else
{
_owner.Canvas.Graphics.StrokePath(this._path, (this.Foreground ?? _owner.CurrentColor ?? _owner.DefaultColor).ToNative());
}
}
}
}
50 changes: 50 additions & 0 deletions CSharpMath.sln
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpMath.Ios", "CSharpMat
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpMath.SkiaSharp", "CSharpMath.SkiaSharp\CSharpMath.SkiaSharp.csproj", "{35B4BB5B-2202-436E-9AFE-00997CA2CC65}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpMath.VectSharp", "CSharpMath.VectSharp\CSharpMath.VectSharp.csproj", "{58F936BB-645A-4419-B621-2EC38FF41E34}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpMath.Playground", "CSharpMath.Playground\CSharpMath.Playground.csproj", "{20986A1A-BF57-4EA7-92E1-E88D3C70874B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CSharpMath.Forms", "CSharpMath.Forms\CSharpMath.Forms.csproj", "{9BAD6846-0B1D-4446-BF62-FCF85C6E9A9F}"
Expand Down Expand Up @@ -387,6 +389,54 @@ Global
{35B4BB5B-2202-436E-9AFE-00997CA2CC65}.Release|x64.Build.0 = Release|Any CPU
{35B4BB5B-2202-436E-9AFE-00997CA2CC65}.Release|x86.ActiveCfg = Release|Any CPU
{35B4BB5B-2202-436E-9AFE-00997CA2CC65}.Release|x86.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|ARM.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|ARM.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|iPhone.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|iPhone.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|iPhoneSimulator.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|x64.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|x64.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|x86.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Ad-Hoc|x86.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|Any CPU.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|Any CPU.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|ARM.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|ARM.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|iPhone.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|iPhone.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|iPhoneSimulator.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|x64.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|x64.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|x86.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.AppStore|x86.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|Any CPU.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|ARM.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|ARM.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|iPhone.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|iPhone.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|iPhoneSimulator.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|iPhoneSimulator.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|x64.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|x64.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|x86.ActiveCfg = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Debug|x86.Build.0 = Debug|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|Any CPU.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|Any CPU.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|ARM.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|ARM.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|iPhone.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|iPhone.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|iPhoneSimulator.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|iPhoneSimulator.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|x64.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|x64.Build.0 = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|x86.ActiveCfg = Release|Any CPU
{58F936BB-645A-4419-B621-2EC38FF41E34}.Release|x86.Build.0 = Release|Any CPU
{20986A1A-BF57-4EA7-92E1-E88D3C70874B}.Ad-Hoc|Any CPU.ActiveCfg = Debug|Any CPU
{20986A1A-BF57-4EA7-92E1-E88D3C70874B}.Ad-Hoc|Any CPU.Build.0 = Debug|Any CPU
{20986A1A-BF57-4EA7-92E1-E88D3C70874B}.Ad-Hoc|ARM.ActiveCfg = Debug|Any CPU
Expand Down